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.

266 lines
7.9 KiB

  1. /*************************************************/
  2. /* Functions to Load from file and save to file */
  3. /* the graphic shapes used to draw a component */
  4. /* When using the import/export symbol options */
  5. /* files are the *.sym files */
  6. /*************************************************/
  7. /* fichier symbedit.cpp */
  8. #include "fctsys.h"
  9. #include "appl_wxstruct.h"
  10. #include "common.h"
  11. #include "class_drawpanel.h"
  12. #include "confirm.h"
  13. #include "kicad_string.h"
  14. #include "gestfich.h"
  15. #include "program.h"
  16. #include "libcmp.h"
  17. #include "general.h"
  18. #include "protos.h"
  19. /*
  20. * Read a component shape file (a symbol file *.sym )and add data (graphic
  21. * items) to the current component.
  22. *
  23. * A symbol file *.sym has the same format as a library, and contains only
  24. * one symbol
  25. */
  26. void WinEDA_LibeditFrame::LoadOneSymbol( void )
  27. {
  28. LIB_COMPONENT* Component;
  29. LibEDA_BaseStruct* DrawEntry;
  30. FILE* ImportFile;
  31. wxString msg, err;
  32. CMP_LIBRARY* Lib;
  33. /* Exit if no library entry is selected or a command is in progress. */
  34. if( CurrentLibEntry == NULL
  35. || ( CurrentDrawItem && CurrentDrawItem->m_Flags ) )
  36. return;
  37. DrawPanel->m_IgnoreMouseEvents = TRUE;
  38. wxString default_path = wxGetApp().ReturnLastVisitedLibraryPath();
  39. wxFileDialog dlg( this, _( "Import Symbol Drawings" ), default_path,
  40. wxEmptyString, SymbolFileWildcard,
  41. wxFD_OPEN | wxFD_FILE_MUST_EXIST );
  42. if( dlg.ShowModal() == wxID_CANCEL )
  43. return;
  44. GetScreen()->m_Curseur = wxPoint( 0, 0 );
  45. DrawPanel->MouseToCursorSchema();
  46. DrawPanel->m_IgnoreMouseEvents = FALSE;
  47. wxFileName fn = dlg.GetPath();
  48. wxGetApp().SaveLastVisitedLibraryPath( fn.GetPath() );
  49. /* Load data */
  50. ImportFile = wxFopen( fn.GetFullPath(), wxT( "rt" ) );
  51. if( ImportFile == NULL )
  52. {
  53. msg.Printf( _( "Failed to open Symbol File <%s>" ),
  54. (const wxChar*) fn.GetFullPath() );
  55. DisplayError( this, msg );
  56. return;
  57. }
  58. Lib = new CMP_LIBRARY( LIBRARY_TYPE_SYMBOL, fn );
  59. if( !Lib->Load( err ) )
  60. {
  61. msg.Printf( _( "Error <%s> occurred loading symbol library <%s>." ),
  62. (const wxChar*) err, (const wxChar*) fn.GetName() );
  63. DisplayError( this, msg );
  64. fclose( ImportFile );
  65. delete Lib;
  66. return;
  67. }
  68. fclose( ImportFile );
  69. if( Lib->IsEmpty() )
  70. {
  71. msg.Printf( _( "No components found in symbol library <%s>." ),
  72. (const wxChar*) fn.GetName() );
  73. delete Lib;
  74. return;
  75. }
  76. if( Lib->GetCount() > 1 )
  77. DisplayError( this, _( "Warning: more than 1 part in Symbol File" ) );
  78. Component = (LIB_COMPONENT*) Lib->GetFirstEntry();
  79. DrawEntry = Component->m_Drawings;
  80. while( DrawEntry )
  81. {
  82. if( DrawEntry->m_Unit )
  83. DrawEntry->m_Unit = CurrentUnit;
  84. if( DrawEntry->m_Convert )
  85. DrawEntry->m_Convert = CurrentConvert;
  86. DrawEntry->m_Flags = IS_NEW;
  87. DrawEntry->m_Selected = IS_SELECTED;
  88. if( DrawEntry->Next() == NULL ) /* Fin de liste trouvee */
  89. {
  90. DrawEntry->SetNext( CurrentLibEntry->m_Drawings );
  91. CurrentLibEntry->m_Drawings = Component->m_Drawings;
  92. Component->m_Drawings = NULL;
  93. break;
  94. }
  95. DrawEntry = DrawEntry->Next();
  96. }
  97. // Remove duplicated drawings:
  98. CurrentLibEntry->RemoveDuplicateDrawItems();
  99. // Clear flags
  100. DrawEntry = CurrentLibEntry->m_Drawings;
  101. while( DrawEntry )
  102. {
  103. DrawEntry->m_Flags = 0;
  104. DrawEntry->m_Selected = 0;
  105. DrawEntry = DrawEntry->Next();
  106. }
  107. GetScreen()->SetModify();
  108. DrawPanel->Refresh();
  109. delete Lib;
  110. }
  111. /*
  112. * Save in file the current symbol.
  113. *
  114. * The symbol file format is like the standard libraries, but there is only
  115. * one symbol.
  116. *
  117. * Invisible pins are not saved
  118. */
  119. void WinEDA_LibeditFrame::SaveOneSymbol()
  120. {
  121. LibEDA_BaseStruct* DrawEntry;
  122. wxString msg;
  123. FILE* ExportFile;
  124. if( CurrentLibEntry->m_Drawings == NULL )
  125. return;
  126. /* Creation du fichier symbole */
  127. wxString default_path = wxGetApp().ReturnLastVisitedLibraryPath();
  128. wxFileDialog dlg( this, _( "Export Symbol Drawings" ), default_path,
  129. CurrentLibEntry->m_Name.m_Text, SymbolFileWildcard,
  130. wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
  131. if( dlg.ShowModal() == wxID_CANCEL )
  132. return;
  133. wxFileName fn = dlg.GetPath();
  134. /* The GTK file chooser doesn't return the file extension added to
  135. * file name so add it here. */
  136. if( fn.GetExt().IsEmpty() )
  137. fn.SetExt( SymbolFileExtension );
  138. wxGetApp().SaveLastVisitedLibraryPath( fn.GetPath() );
  139. ExportFile = wxFopen( fn.GetFullPath(), wxT( "wt" ) );
  140. if( ExportFile == NULL )
  141. {
  142. msg.Printf( _( "Unable to create <%s>" ),
  143. (const wxChar*) fn.GetFullPath() );
  144. DisplayError( this, msg );
  145. return;
  146. }
  147. msg.Printf( _( "Save Symbol in [%s]" ), (const wxChar*) fn.GetPath() );
  148. Affiche_Message( msg );
  149. /* Creation de l'entete de la librairie */
  150. char Line[256];
  151. fprintf( ExportFile, "%s %d.%d %s Date: %s\n", LIBFILE_IDENT,
  152. LIB_VERSION_MAJOR, LIB_VERSION_MINOR,
  153. "SYMBOL", DateAndTime( Line ) );
  154. /* Creation du commentaire donnant le nom du composant */
  155. fprintf( ExportFile, "# SYMBOL %s\n#\n",
  156. CONV_TO_UTF8( CurrentLibEntry->m_Name.m_Text ) );
  157. /* Generation des lignes utiles */
  158. fprintf( ExportFile, "DEF %s",
  159. CONV_TO_UTF8( CurrentLibEntry->m_Name.m_Text ) );
  160. if( !CurrentLibEntry->m_Prefix.m_Text.IsEmpty() )
  161. fprintf( ExportFile, " %s",
  162. CONV_TO_UTF8( CurrentLibEntry->m_Prefix.m_Text ) );
  163. else
  164. fprintf( ExportFile, " ~" );
  165. fprintf( ExportFile, " %d %d %c %c %d %d %c\n",
  166. 0, /* unused */
  167. CurrentLibEntry->m_TextInside,
  168. CurrentLibEntry->m_DrawPinNum ? 'Y' : 'N',
  169. CurrentLibEntry->m_DrawPinName ? 'Y' : 'N',
  170. 1, 0 /* unused */, 'N' );
  171. /* Position / orientation / visibilite des champs */
  172. CurrentLibEntry->m_Prefix.Save( ExportFile );
  173. CurrentLibEntry->m_Name.Save( ExportFile );
  174. DrawEntry = CurrentLibEntry->m_Drawings;
  175. if( DrawEntry )
  176. {
  177. fprintf( ExportFile, "DRAW\n" );
  178. for( ; DrawEntry != NULL; DrawEntry = DrawEntry->Next() )
  179. {
  180. /* Elimination des elements non relatifs a l'unite */
  181. if( CurrentUnit && DrawEntry->m_Unit
  182. && ( DrawEntry->m_Unit != CurrentUnit ) )
  183. continue;
  184. if( CurrentConvert && DrawEntry->m_Convert
  185. && ( DrawEntry->m_Convert != CurrentConvert ) )
  186. continue;
  187. DrawEntry->Save( ExportFile );
  188. }
  189. fprintf( ExportFile, "ENDDRAW\n" );
  190. }
  191. fprintf( ExportFile, "ENDDEF\n" );
  192. fclose( ExportFile );
  193. }
  194. /***************************************************************************/
  195. /* Routine de placement du point d'ancrage ( reference des coordonnes pour */
  196. /* le trace) du composant courant */
  197. /* Toutes les coord apparaissant dans les structures sont modifiees */
  198. /* pour repositionner le point repere par le curseur souris au point */
  199. /* d'ancrage ( coord 0,0 ). */
  200. /***************************************************************************/
  201. void WinEDA_LibeditFrame::PlaceAncre()
  202. {
  203. if( CurrentLibEntry == NULL )
  204. return;
  205. wxPoint offset( -GetScreen()->m_Curseur.x, GetScreen()->m_Curseur.y );
  206. GetScreen()->SetModify();
  207. CurrentLibEntry->SetOffset( offset );
  208. /* Redraw the symbol */
  209. GetScreen()->m_Curseur.x = GetScreen()->m_Curseur.y = 0;
  210. Recadre_Trace( TRUE );
  211. GetScreen()->SetRefreshReq();
  212. }