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.

218 lines
6.6 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  1. /*********************************************/
  2. /* eesave.cpp Module to Save EESchema files */
  3. /*********************************************/
  4. #include "fctsys.h"
  5. #include "gr_basic.h"
  6. #include "common.h"
  7. #include "program.h"
  8. #include "libcmp.h"
  9. #include "general.h"
  10. #include "macros.h"
  11. #include "protos.h"
  12. /* Fonctions Locales */
  13. static void SaveLayers( FILE* f );
  14. /* Variables locales */
  15. /*****************************************************************************
  16. * Routine to save an EESchema file. *
  17. * FileSave controls how the file is to be saved - under what name. *
  18. * Returns TRUE if the file has been saved. *
  19. *****************************************************************************/
  20. bool WinEDA_SchematicFrame::SaveEEFile( SCH_SCREEN* screen, int FileSave )
  21. {
  22. wxString msg;
  23. wxString Name, BakName;
  24. FILE* f;
  25. wxString dirbuf;
  26. if( screen == NULL )
  27. screen = (SCH_SCREEN*) GetScreen();
  28. /* If no name exists in the window yet - save as new. */
  29. if( screen->m_FileName.IsEmpty() )
  30. FileSave = FILE_SAVE_NEW;
  31. switch( FileSave )
  32. {
  33. case FILE_SAVE_AS:
  34. dirbuf = wxGetCwd() + STRING_DIR_SEP;
  35. Name = MakeFileName( dirbuf, screen->m_FileName, g_SchExtBuffer );
  36. /* Rename the old file to a '.bak' one: */
  37. BakName = Name;
  38. if( wxFileExists( Name ) )
  39. {
  40. ChangeFileNameExt( BakName, wxT( ".bak" ) );
  41. wxRemoveFile( BakName ); /* delete Old .bak file */
  42. if( !wxRenameFile( Name, BakName ) )
  43. {
  44. DisplayError( this, wxT( "Warning: unable to rename old file" ), 10 );
  45. }
  46. }
  47. break;
  48. case FILE_SAVE_NEW:
  49. {
  50. wxString mask = wxT( "*" ) + g_SchExtBuffer;
  51. Name = EDA_FileSelector( _( "Schematic files:" ),
  52. wxEmptyString, /* Chemin par defaut */
  53. screen->m_FileName, /* nom fichier par defaut, et resultat */
  54. g_SchExtBuffer, /* extension par defaut */
  55. mask, /* Masque d'affichage */
  56. this,
  57. wxFD_SAVE,
  58. FALSE
  59. );
  60. if( Name.IsEmpty() )
  61. return FALSE;
  62. screen->m_FileName = Name;
  63. dirbuf = wxGetCwd() + STRING_DIR_SEP;
  64. Name = MakeFileName( dirbuf, Name, g_SchExtBuffer );
  65. break;
  66. }
  67. default:
  68. break;
  69. }
  70. if( ( f = wxFopen( Name, wxT( "wt" ) ) ) == NULL )
  71. {
  72. msg = _( "Failed to create file " ) + Name;
  73. DisplayError( this, msg );
  74. return FALSE;
  75. }
  76. if( FileSave == FILE_SAVE_NEW )
  77. screen->m_FileName = Name;
  78. bool success = screen->Save( f );
  79. if( !success )
  80. DisplayError( this, _( "File write operation failed." ) );
  81. else
  82. screen->ClrModify();
  83. fclose( f );
  84. return success;
  85. }
  86. /*****************************************/
  87. bool SCH_SCREEN::Save( FILE* aFile ) const
  88. /*****************************************/
  89. /**
  90. * Function Save
  91. * writes the data structures for this object out to a FILE in "*.brd" format.
  92. * @param aFile The FILE to write to.
  93. * @return bool - true if success writing else false.
  94. */
  95. {
  96. const wxChar** LibNames;
  97. wxString Name, msg;
  98. Ki_PageDescr* PlotSheet;
  99. LibNames = GetLibNames();
  100. for( int ii = 0; LibNames[ii] != NULL; ii++ )
  101. {
  102. if( ii > 0 )
  103. Name += wxT( "," );
  104. Name += LibNames[ii];
  105. }
  106. MyFree( LibNames );
  107. // Creates header
  108. if( fprintf( aFile, "%s %s %d\n", EESCHEMA_FILE_STAMP,
  109. SCHEMATIC_HEAD_STRING, EESCHEMA_VERSION ) == EOF
  110. || fprintf( aFile, "LIBS:%s\n", CONV_TO_UTF8( Name ) ) == EOF )
  111. {
  112. return FALSE;
  113. }
  114. SaveLayers( aFile );
  115. /* Write page info */
  116. PlotSheet = m_CurrentSheetDesc;
  117. fprintf( aFile, "$Descr %s %d %d\n", CONV_TO_UTF8( PlotSheet->m_Name ),
  118. PlotSheet->m_Size.x, PlotSheet->m_Size.y );
  119. /* Write ScreenNumber and NumberOfScreen; not very meaningfull for SheetNumber and Sheet Count
  120. * in a complex hierarchy, but usefull in simple hierarchy and flat hierarchy
  121. * Used also to serach the root sheet ( ScreenNumber = 1 ) withing the files
  122. */
  123. fprintf( aFile, "Sheet %d %d\n", m_ScreenNumber, m_NumberOfScreen );
  124. fprintf( aFile, "Title \"%s\"\n", CONV_TO_UTF8( m_Title ) );
  125. fprintf( aFile, "Date \"%s\"\n", CONV_TO_UTF8( m_Date ) );
  126. fprintf( aFile, "Rev \"%s\"\n", CONV_TO_UTF8( m_Revision ) );
  127. fprintf( aFile, "Comp \"%s\"\n", CONV_TO_UTF8( m_Company ) );
  128. fprintf( aFile, "Comment1 \"%s\"\n", CONV_TO_UTF8( m_Commentaire1 ) );
  129. fprintf( aFile, "Comment2 \"%s\"\n", CONV_TO_UTF8( m_Commentaire2 ) );
  130. fprintf( aFile, "Comment3 \"%s\"\n", CONV_TO_UTF8( m_Commentaire3 ) );
  131. fprintf( aFile, "Comment4 \"%s\"\n", CONV_TO_UTF8( m_Commentaire4 ) );
  132. fprintf( aFile, "$EndDescr\n" );
  133. /* Saving schematic items */
  134. bool failed = FALSE;
  135. for( SCH_ITEM* item = EEDrawList; item; item = item->Next() )
  136. {
  137. switch( item->Type() )
  138. {
  139. case TYPE_SCH_COMPONENT: /* Its a library item. */
  140. case DRAW_SHEET_STRUCT_TYPE: /* Its a Sheet item. */
  141. case DRAW_SEGMENT_STRUCT_TYPE: /* Its a Segment item. */
  142. case DRAW_BUSENTRY_STRUCT_TYPE: /* Its a Raccord item. */
  143. case DRAW_POLYLINE_STRUCT_TYPE: /* Its a polyline item. */
  144. case DRAW_JUNCTION_STRUCT_TYPE: /* Its a connection item. */
  145. case DRAW_NOCONNECT_STRUCT_TYPE: /* Its a NoConnection item. */
  146. case TYPE_SCH_TEXT: /* Its a text item. */
  147. case TYPE_SCH_LABEL: /* Its a label item. */
  148. case TYPE_SCH_GLOBALLABEL: /* Its a Global label item. */
  149. case TYPE_SCH_HIERLABEL: /* Its a Hierarchical label item. */
  150. case DRAW_MARKER_STRUCT_TYPE: /* Its a marker item. */
  151. if( !item->Save( aFile ) )
  152. failed = TRUE;
  153. break;
  154. /*
  155. * case DRAW_HIERARCHICAL_PIN_SHEET_STRUCT_TYPE:
  156. * case DRAW_PICK_ITEM_STRUCT_TYPE:
  157. * break;
  158. */
  159. default:
  160. break;
  161. }
  162. if( failed )
  163. break;
  164. }
  165. if( fprintf( aFile, "$EndSCHEMATC\n" ) == EOF )
  166. failed = TRUE;
  167. return !failed;
  168. }
  169. /****************************/
  170. static void SaveLayers( FILE* f )
  171. /****************************/
  172. /* Save a Layer Structure to a file
  173. * theses infos are not used in eeschema
  174. */
  175. {
  176. fprintf( f, "EELAYER %2d %2d\n", g_LayerDescr.NumberOfLayers, g_LayerDescr.CurrentLayer );
  177. fprintf( f, "EELAYER END\n" );
  178. }