You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

286 lines
7.6 KiB

  1. /****************************/
  2. /* EESCHEMA - files-io.cpp */
  3. /****************************/
  4. #include "fctsys.h"
  5. #include "common.h"
  6. #include "class_drawpanel.h"
  7. #include "confirm.h"
  8. #include "gestfich.h"
  9. #include "program.h"
  10. #include "general.h"
  11. #include "protos.h"
  12. #include "eeschema_id.h"
  13. #include "class_library.h"
  14. #include "libeditfrm.h"
  15. /* Commands to save project or the current page.
  16. */
  17. void WinEDA_SchematicFrame::Save_File( wxCommandEvent& event )
  18. {
  19. int id = event.GetId();
  20. switch( id )
  21. {
  22. case ID_SAVE_PROJECT: /* Update Schematic File */
  23. SaveProject();
  24. break;
  25. case ID_SAVE_ONE_SHEET: /* Update Schematic File */
  26. SaveEEFile( NULL, FILE_SAVE_AS );
  27. break;
  28. case ID_SAVE_ONE_SHEET_AS: /* Save EED (new name) */
  29. SaveEEFile( NULL, FILE_SAVE_NEW );
  30. break;
  31. default:
  32. DisplayError( this, wxT( "WinEDA_SchematicFrame::Save_File Internal Error" ) );
  33. break;
  34. }
  35. }
  36. /**
  37. * Load an entire project
  38. *
  39. * Schematic root file and its subhierarchies, the configuration and the libs
  40. * which are not already loaded)
  41. */
  42. int WinEDA_SchematicFrame::LoadOneEEProject( const wxString& FileName,
  43. bool IsNew )
  44. {
  45. SCH_SCREEN* screen;
  46. wxString FullFileName, msg;
  47. bool LibCacheExist = false;
  48. EDA_ScreenList ScreenList;
  49. for( screen = ScreenList.GetFirst(); screen != NULL;
  50. screen = ScreenList.GetNext() )
  51. {
  52. if( screen->IsModify() )
  53. break;
  54. }
  55. if( screen )
  56. {
  57. if( !IsOK( this, _( "Clear schematic hierarchy?" ) ) )
  58. return FALSE;
  59. if( g_RootSheet->m_AssociatedScreen->m_FileName != m_DefaultSchematicFileName )
  60. SetLastProject( g_RootSheet->m_AssociatedScreen->m_FileName );
  61. }
  62. FullFileName = FileName;
  63. if( ( FullFileName.IsEmpty() ) && !IsNew )
  64. {
  65. wxFileDialog dlg( this, _( "Open Schematic" ), wxGetCwd(),
  66. wxEmptyString, SchematicFileWildcard,
  67. wxFD_OPEN | wxFD_FILE_MUST_EXIST );
  68. if( dlg.ShowModal() == wxID_CANCEL )
  69. return 0;
  70. FullFileName = dlg.GetPath();
  71. }
  72. if( g_RootSheet )
  73. {
  74. SAFE_DELETE( g_RootSheet );
  75. }
  76. CreateScreens();
  77. ActiveScreen = GetScreen();
  78. screen = (SCH_SCREEN*) GetScreen();
  79. wxSetWorkingDirectory( wxPathOnly( FullFileName ) );
  80. GetScreen()->m_FileName = FullFileName;
  81. g_RootSheet->SetFileName( FullFileName );
  82. Affiche_Message( wxEmptyString );
  83. ClearMsgPanel();
  84. memset( &g_EESchemaVar, 0, sizeof(g_EESchemaVar) );
  85. GetScreen()->ClrModify();
  86. if( IsNew )
  87. {
  88. screen->m_CurrentSheetDesc = &g_Sheet_A4;
  89. screen->SetZoom( 32 );
  90. screen->SetGrid( ID_POPUP_GRID_LEVEL_1000 + m_LastGridSizeId );
  91. screen->m_Title = wxT( "noname.sch" );
  92. GetScreen()->m_FileName = screen->m_Title;
  93. screen->m_Company.Empty();
  94. screen->m_Commentaire1.Empty();
  95. screen->m_Commentaire2.Empty();
  96. screen->m_Commentaire3.Empty();
  97. screen->m_Commentaire4.Empty();
  98. LoadProjectFile( wxEmptyString, TRUE );
  99. Zoom_Automatique( TRUE );
  100. SetSheetNumberAndCount();
  101. DrawPanel->Refresh();
  102. return 1;
  103. }
  104. // Reloading configuration.
  105. msg = _( "Ready\nWorking dir: \n" ) + wxGetCwd();
  106. PrintMsg( msg );
  107. LoadProjectFile( wxEmptyString, FALSE );
  108. // Clear (if needed) the current active library in libedit because it could be
  109. // removed from memory
  110. WinEDA_LibeditFrame::EnsureActiveLibExists();
  111. // Delete old caches.
  112. CMP_LIBRARY::RemoveCacheLibrary();
  113. if( IsNew )
  114. {
  115. if( DrawPanel )
  116. DrawPanel->Refresh( true );
  117. return 1;
  118. }
  119. /* Loading the project library cache
  120. * until apr 2009 the lib is named <root_name>.cache.lib
  121. * and after (due to code change): <root_name>-cache.lib
  122. * so if the <name>-cache.lib is not found, the old way will be tried
  123. */
  124. bool use_oldcachename = false;
  125. wxFileName fn = g_RootSheet->m_AssociatedScreen->m_FileName;
  126. wxString cachename = fn.GetName() + wxT("-cache");
  127. fn.SetName( cachename );
  128. fn.SetExt( CompLibFileExtension );
  129. if( ! fn.FileExists() )
  130. {
  131. fn = g_RootSheet->m_AssociatedScreen->m_FileName;
  132. fn.SetExt( wxT( "cache.lib" ) );
  133. use_oldcachename = true;
  134. }
  135. if( fn.FileExists() )
  136. {
  137. wxString errMsg;
  138. wxLogDebug( wxT( "Load schematic cache library file <%s>" ),
  139. GetChars( fn.GetFullPath() ) );
  140. msg = wxT( "Load " ) + fn.GetFullPath();
  141. CMP_LIBRARY* LibCache = CMP_LIBRARY::LoadLibrary( fn, errMsg );
  142. if( LibCache )
  143. {
  144. LibCache->SetCache();
  145. msg += wxT( " OK" );
  146. if ( use_oldcachename ) // set the new name
  147. {
  148. fn.SetName(cachename);
  149. fn.SetExt( CompLibFileExtension );
  150. LibCache->SetFileName( fn );
  151. }
  152. LibCacheExist = true;
  153. CMP_LIBRARY::GetLibraryList().push_back( LibCache );
  154. }
  155. else
  156. {
  157. wxString prompt;
  158. prompt.Printf( _( "Component library <%s> failed to load.\n\n\
  159. Error: %s" ),
  160. GetChars( fn.GetFullPath() ),
  161. GetChars( errMsg ) );
  162. DisplayError( this, prompt );
  163. msg += wxT( " ->Error" );
  164. }
  165. PrintMsg( msg );
  166. }
  167. if( !wxFileExists( g_RootSheet->m_AssociatedScreen->m_FileName )
  168. && !LibCacheExist )
  169. {
  170. Zoom_Automatique( FALSE );
  171. msg.Printf( _( "File <%s> not found." ),
  172. g_RootSheet->m_AssociatedScreen->m_FileName.GetData() );
  173. DisplayInfoMessage( this, msg, 0 );
  174. return -1;
  175. }
  176. // load the project.
  177. SAFE_DELETE( g_RootSheet->m_AssociatedScreen );
  178. bool diag = g_RootSheet->Load( this );
  179. /* Redraw base screen (ROOT) if necessary. */
  180. ActiveScreen = GetScreen();
  181. ActiveScreen->SetGrid( ID_POPUP_GRID_LEVEL_1000 + m_LastGridSizeId );
  182. Zoom_Automatique( FALSE );
  183. SetSheetNumberAndCount();
  184. DrawPanel->Refresh( true );
  185. return diag;
  186. }
  187. /* Create a new screen
  188. *
  189. * This screen is chained with OldScreen. The timestamp value is assigned to
  190. * the parameter NewScreen-> TimeStamp
  191. */
  192. SCH_SCREEN* WinEDA_SchematicFrame::CreateNewScreen( SCH_SCREEN* OldScreen,
  193. int TimeStamp )
  194. {
  195. SCH_SCREEN* NewScreen;
  196. NewScreen = new SCH_SCREEN();
  197. NewScreen->SetRefreshReq();
  198. if( OldScreen )
  199. NewScreen->m_Company = OldScreen->m_Company;
  200. NewScreen->m_TimeStamp = TimeStamp;
  201. NewScreen->SetBack( OldScreen );
  202. return NewScreen;
  203. }
  204. /**
  205. * Save the entire project and create an archive for components.
  206. *
  207. * The library archive name is <root_name>.cache.lib
  208. */
  209. void WinEDA_SchematicFrame::SaveProject()
  210. {
  211. SCH_SCREEN* screen;
  212. wxFileName fn;
  213. EDA_ScreenList ScreenList;
  214. for( screen = ScreenList.GetFirst(); screen != NULL;
  215. screen = ScreenList.GetNext() )
  216. {
  217. D( printf( "SaveEEFile, %s\n", CONV_TO_UTF8( screen->m_FileName ) ); )
  218. SaveEEFile( screen, FILE_SAVE_AS );
  219. }
  220. /* Archive components in current directory. */
  221. fn = g_RootSheet->GetFileName();
  222. wxString cachename = fn.GetName() + wxT("-cache");
  223. fn.SetName( cachename );
  224. fn.SetExt( CompLibFileExtension );
  225. LibArchive( this, fn.GetFullPath() );
  226. }
  227. /**
  228. * Return the number of components in the schematic.
  229. *
  230. * Power components are not included.
  231. */
  232. int CountCmpNumber()
  233. {
  234. return g_RootSheet->ComponentCount();
  235. }