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.

2052 lines
66 KiB

Modular-Kicad milestone B), major portions: *) Rework the set language support, simplify it by using KIWAY. Now any major frame with a "change language" menu can change the language for all KIWAY_PLAYERs in the whole KIWAY. Multiple KIWAYs are not supported yet. *) Simplify "modal wxFrame" support, and add that support exclusively to KIWAY_PLAYER where it is inherited by all derivatives. The function KIWAY_PLAYER::ShowModal() is in the vtable and so is cross module capable. *) Remove the requirements and assumptions that the wxFrame hierarchy always had PCB_EDIT_FRAME and SCH_EDIT_FRAME as immediate parents of their viewers and editors. This is no longer the case, nor required. *) Use KIWAY::Player() everywhere to make KIWAY_PLAYERs, this registers the KIWAY_PLAYER within the KIWAY and makes it very easy to find an open frame quickly. It also gives control to the KIWAY as to frame hierarchical relationships. *) Change single_top to use the KIWAY for loading a KIFACE and instantiating the single KIWAY_PLAYER, see bullet immediately above. *) Add KIWAY::OnKiwayEnd() and call it from PGM_BASE at program termination, this gives the KIFACEs a chance to save their final configuration dope to disk. *) Add dedicated FRAME_T's for the modal frames, so m_Ident can be tested and these modal frames are distinctly different than their non-modal equivalents. KIWAY_PLAYER::IsModal() is !not! a valid test during the wxFrame's constructor, so this is another important reason for having a dedicated FRAME_T for each modal wxFrame. On balance, more lines were deleted than were added to achieve all this.
12 years ago
Added NETINFO_MAPPING, to ease saving nets with consecutive net codes (without modifying the net codes during the run time). Now, nets are saved with consecutive net codes (both modern & legacy plugins). Zones are saved together with their nets, without depending on the fact if there are any pads with such net. Therefore validation of zone net names was removed (pcbnew/class_board.cpp). Performed tests: - Changed a pad's net name from empty to existent - ok, name was changed. - Changed a pad's net name from empty to nonexistent - ok, error message is displayed, net name stays empty. - Changed a pad's net name from existent to empty - ok, net name became empty - Changed a pad's net name from existent to nonexistent - ok, error message is displayed, net name is not changed. - Drawn a zone that belongs to a net, then modified schematics so the net does not exist anymore. After reloading the net list, all pads/tracks are updated. Zones still belongs to the net that does not exist in the schematic (but still exists in .kicad_pcb file). After running DRC, the zone becomes not filled. - Undo & redo affects assignment of a polygon to a specific net (you may change net of a polygon, refill it and undo/redo the changes). - KiCad s-expr & legacy, Eagle, P-CAD boards seem to load without any problem (they also contain correct net names assigned to the appropriate pads). All types of board file formats were loaded, then saved in sexpr format and reopened with a KiCad built from the master branch (without my modifications). - A few boards were also saved using the legacy format and were opened with the master KiCad without any issues. - Change a net name for a pad, restore with undo/redo - ok - Remove everything, restore with undo - ok - Remove everything, reload netlist - ok Differences observed between files saved by the master branch KiCad and this one: - list of nets are not saved in any particular order, so net codes may differ - the default net class does not contain the unconnected net
12 years ago
Modular-Kicad milestone B), major portions: *) Rework the set language support, simplify it by using KIWAY. Now any major frame with a "change language" menu can change the language for all KIWAY_PLAYERs in the whole KIWAY. Multiple KIWAYs are not supported yet. *) Simplify "modal wxFrame" support, and add that support exclusively to KIWAY_PLAYER where it is inherited by all derivatives. The function KIWAY_PLAYER::ShowModal() is in the vtable and so is cross module capable. *) Remove the requirements and assumptions that the wxFrame hierarchy always had PCB_EDIT_FRAME and SCH_EDIT_FRAME as immediate parents of their viewers and editors. This is no longer the case, nor required. *) Use KIWAY::Player() everywhere to make KIWAY_PLAYERs, this registers the KIWAY_PLAYER within the KIWAY and makes it very easy to find an open frame quickly. It also gives control to the KIWAY as to frame hierarchical relationships. *) Change single_top to use the KIWAY for loading a KIFACE and instantiating the single KIWAY_PLAYER, see bullet immediately above. *) Add KIWAY::OnKiwayEnd() and call it from PGM_BASE at program termination, this gives the KIFACEs a chance to save their final configuration dope to disk. *) Add dedicated FRAME_T's for the modal frames, so m_Ident can be tested and these modal frames are distinctly different than their non-modal equivalents. KIWAY_PLAYER::IsModal() is !not! a valid test during the wxFrame's constructor, so this is another important reason for having a dedicated FRAME_T for each modal wxFrame. On balance, more lines were deleted than were added to achieve all this.
12 years ago
Added NETINFO_MAPPING, to ease saving nets with consecutive net codes (without modifying the net codes during the run time). Now, nets are saved with consecutive net codes (both modern & legacy plugins). Zones are saved together with their nets, without depending on the fact if there are any pads with such net. Therefore validation of zone net names was removed (pcbnew/class_board.cpp). Performed tests: - Changed a pad's net name from empty to existent - ok, name was changed. - Changed a pad's net name from empty to nonexistent - ok, error message is displayed, net name stays empty. - Changed a pad's net name from existent to empty - ok, net name became empty - Changed a pad's net name from existent to nonexistent - ok, error message is displayed, net name is not changed. - Drawn a zone that belongs to a net, then modified schematics so the net does not exist anymore. After reloading the net list, all pads/tracks are updated. Zones still belongs to the net that does not exist in the schematic (but still exists in .kicad_pcb file). After running DRC, the zone becomes not filled. - Undo & redo affects assignment of a polygon to a specific net (you may change net of a polygon, refill it and undo/redo the changes). - KiCad s-expr & legacy, Eagle, P-CAD boards seem to load without any problem (they also contain correct net names assigned to the appropriate pads). All types of board file formats were loaded, then saved in sexpr format and reopened with a KiCad built from the master branch (without my modifications). - A few boards were also saved using the legacy format and were opened with the master KiCad without any issues. - Change a net name for a pad, restore with undo/redo - ok - Remove everything, restore with undo - ok - Remove everything, reload netlist - ok Differences observed between files saved by the master branch KiCad and this one: - list of nets are not saved in any particular order, so net codes may differ - the default net class does not contain the unconnected net
12 years ago
Modular-Kicad milestone B), major portions: *) Rework the set language support, simplify it by using KIWAY. Now any major frame with a "change language" menu can change the language for all KIWAY_PLAYERs in the whole KIWAY. Multiple KIWAYs are not supported yet. *) Simplify "modal wxFrame" support, and add that support exclusively to KIWAY_PLAYER where it is inherited by all derivatives. The function KIWAY_PLAYER::ShowModal() is in the vtable and so is cross module capable. *) Remove the requirements and assumptions that the wxFrame hierarchy always had PCB_EDIT_FRAME and SCH_EDIT_FRAME as immediate parents of their viewers and editors. This is no longer the case, nor required. *) Use KIWAY::Player() everywhere to make KIWAY_PLAYERs, this registers the KIWAY_PLAYER within the KIWAY and makes it very easy to find an open frame quickly. It also gives control to the KIWAY as to frame hierarchical relationships. *) Change single_top to use the KIWAY for loading a KIFACE and instantiating the single KIWAY_PLAYER, see bullet immediately above. *) Add KIWAY::OnKiwayEnd() and call it from PGM_BASE at program termination, this gives the KIFACEs a chance to save their final configuration dope to disk. *) Add dedicated FRAME_T's for the modal frames, so m_Ident can be tested and these modal frames are distinctly different than their non-modal equivalents. KIWAY_PLAYER::IsModal() is !not! a valid test during the wxFrame's constructor, so this is another important reason for having a dedicated FRAME_T for each modal wxFrame. On balance, more lines were deleted than were added to achieve all this.
12 years ago
14 years ago
14 years ago
14 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
Added NETINFO_MAPPING, to ease saving nets with consecutive net codes (without modifying the net codes during the run time). Now, nets are saved with consecutive net codes (both modern & legacy plugins). Zones are saved together with their nets, without depending on the fact if there are any pads with such net. Therefore validation of zone net names was removed (pcbnew/class_board.cpp). Performed tests: - Changed a pad's net name from empty to existent - ok, name was changed. - Changed a pad's net name from empty to nonexistent - ok, error message is displayed, net name stays empty. - Changed a pad's net name from existent to empty - ok, net name became empty - Changed a pad's net name from existent to nonexistent - ok, error message is displayed, net name is not changed. - Drawn a zone that belongs to a net, then modified schematics so the net does not exist anymore. After reloading the net list, all pads/tracks are updated. Zones still belongs to the net that does not exist in the schematic (but still exists in .kicad_pcb file). After running DRC, the zone becomes not filled. - Undo & redo affects assignment of a polygon to a specific net (you may change net of a polygon, refill it and undo/redo the changes). - KiCad s-expr & legacy, Eagle, P-CAD boards seem to load without any problem (they also contain correct net names assigned to the appropriate pads). All types of board file formats were loaded, then saved in sexpr format and reopened with a KiCad built from the master branch (without my modifications). - A few boards were also saved using the legacy format and were opened with the master KiCad without any issues. - Change a net name for a pad, restore with undo/redo - ok - Remove everything, restore with undo - ok - Remove everything, reload netlist - ok Differences observed between files saved by the master branch KiCad and this one: - list of nets are not saved in any particular order, so net codes may differ - the default net class does not contain the unconnected net
12 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2012 CERN
  5. * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <fctsys.h>
  25. #include <kicad_string.h>
  26. #include <common.h>
  27. #include <build_version.h> // LEGACY_BOARD_FILE_VERSION
  28. #include <macros.h>
  29. #include <wildcards_and_files_ext.h>
  30. #include <base_units.h>
  31. #include <class_board.h>
  32. #include <class_module.h>
  33. #include <class_pcb_text.h>
  34. #include <class_dimension.h>
  35. #include <class_track.h>
  36. #include <class_zone.h>
  37. #include <class_drawsegment.h>
  38. #include <class_mire.h>
  39. #include <class_edge_mod.h>
  40. #include <pcb_plot_params.h>
  41. #include <zones.h>
  42. #include <kicad_plugin.h>
  43. #include <pcb_parser.h>
  44. #include <wx/dir.h>
  45. #include <wx/filename.h>
  46. #include <wx/wfstream.h>
  47. #include <boost/ptr_container/ptr_map.hpp>
  48. #include <memory.h>
  49. using namespace PCB_KEYS_T;
  50. #define FMTIU BOARD_ITEM::FormatInternalUnits
  51. /**
  52. * Definition for enabling and disabling footprint library trace output. See the
  53. * wxWidgets documentation on using the WXTRACE environment variable.
  54. */
  55. static const wxString traceFootprintLibrary( wxT( "KicadFootprintLib" ) );
  56. ///> Removes empty nets (i.e. with node count equal zero) from net classes
  57. void filterNetClass( const BOARD& aBoard, NETCLASS& aNetClass )
  58. {
  59. for( NETCLASS::iterator it = aNetClass.begin(); it != aNetClass.end(); )
  60. {
  61. NETINFO_ITEM* netinfo = aBoard.FindNet( *it );
  62. if( netinfo && netinfo->GetNodesCount() <= 0 ) // hopefully there are no nets with negative
  63. aNetClass.Remove( it++ ); // node count, but you never know..
  64. else
  65. ++it;
  66. }
  67. }
  68. /**
  69. * Class FP_CACHE_ITEM
  70. * is helper class for creating a footprint library cache.
  71. *
  72. * The new footprint library design is a file path of individual module files
  73. * that contain a single module per file. This class is a helper only for the
  74. * footprint portion of the PLUGIN API, and only for the #PCB_IO plugin. It is
  75. * private to this implementation file so it is not placed into a header.
  76. */
  77. class FP_CACHE_ITEM
  78. {
  79. wxFileName m_file_name; ///< The the full file name and path of the footprint to cache.
  80. wxDateTime m_mod_time; ///< The last file modified time stamp.
  81. std::unique_ptr<MODULE> m_module;
  82. public:
  83. FP_CACHE_ITEM( MODULE* aModule, const wxFileName& aFileName );
  84. wxString GetName() const { return m_file_name.GetDirs().Last(); }
  85. wxFileName GetFileName() const { return m_file_name; }
  86. /// Tell if the disk content or the lib_path has changed.
  87. bool IsModified() const;
  88. MODULE* GetModule() const { return m_module.get(); }
  89. void UpdateModificationTime() { m_mod_time = m_file_name.GetModificationTime(); }
  90. };
  91. FP_CACHE_ITEM::FP_CACHE_ITEM( MODULE* aModule, const wxFileName& aFileName ) :
  92. m_module( aModule )
  93. {
  94. m_file_name = aFileName;
  95. if( m_file_name.FileExists() )
  96. m_mod_time = m_file_name.GetModificationTime();
  97. else
  98. m_mod_time.Now();
  99. }
  100. bool FP_CACHE_ITEM::IsModified() const
  101. {
  102. if( !m_file_name.FileExists() )
  103. return false;
  104. wxLogTrace( traceFootprintLibrary, wxT( "File '%s', m_mod_time %s-%s, file mod time: %s-%s." ),
  105. GetChars( m_file_name.GetFullPath() ),
  106. GetChars( m_mod_time.FormatDate() ), GetChars( m_mod_time.FormatTime() ),
  107. GetChars( m_file_name.GetModificationTime().FormatDate() ),
  108. GetChars( m_file_name.GetModificationTime().FormatTime() ) );
  109. return m_file_name.GetModificationTime() != m_mod_time;
  110. }
  111. typedef boost::ptr_map< std::string, FP_CACHE_ITEM > MODULE_MAP;
  112. typedef MODULE_MAP::iterator MODULE_ITER;
  113. typedef MODULE_MAP::const_iterator MODULE_CITER;
  114. class FP_CACHE
  115. {
  116. PCB_IO* m_owner; /// Plugin object that owns the cache.
  117. wxFileName m_lib_path; /// The path of the library.
  118. wxDateTime m_mod_time; /// Footprint library path modified time stamp.
  119. MODULE_MAP m_modules; /// Map of footprint file name per MODULE*.
  120. public:
  121. FP_CACHE( PCB_IO* aOwner, const wxString& aLibraryPath );
  122. wxString GetPath() const { return m_lib_path.GetPath(); }
  123. wxDateTime GetLastModificationTime() const { return m_mod_time; }
  124. bool IsWritable() const { return m_lib_path.IsOk() && m_lib_path.IsDirWritable(); }
  125. MODULE_MAP& GetModules() { return m_modules; }
  126. // Most all functions in this class throw IO_ERROR exceptions. There are no
  127. // error codes nor user interface calls from here, nor in any PLUGIN.
  128. // Catch these exceptions higher up please.
  129. /// save the entire legacy library to m_lib_name;
  130. void Save();
  131. void Load();
  132. void Remove( const wxString& aFootprintName );
  133. wxDateTime GetLibModificationTime() const;
  134. /**
  135. * Function IsModified
  136. * check if the footprint cache has been modified relative to \a aLibPath
  137. * and \a aFootprintName.
  138. *
  139. * @param aLibPath is a path to test the current cache library path against.
  140. * @param aFootprintName is the footprint name in the cache to test. If the footprint
  141. * name is empty, the all the footprint files in the library are
  142. * checked to see if they have been modified.
  143. * @return true if the cache has been modified.
  144. */
  145. bool IsModified( const wxString& aLibPath,
  146. const wxString& aFootprintName = wxEmptyString ) const;
  147. /**
  148. * Function IsPath
  149. * checks if \a aPath is the same as the current cache path.
  150. *
  151. * This tests paths by converting \a aPath using the native separators. Internally
  152. * #FP_CACHE stores the current path using native separators. This prevents path
  153. * miscompares on Windows due to the fact that paths can be stored with / instead of \\
  154. * in the footprint library table.
  155. *
  156. * @param aPath is the library path to test against.
  157. * @return true if \a aPath is the same as the cache path.
  158. */
  159. bool IsPath( const wxString& aPath ) const;
  160. };
  161. FP_CACHE::FP_CACHE( PCB_IO* aOwner, const wxString& aLibraryPath )
  162. {
  163. m_owner = aOwner;
  164. m_lib_path.SetPath( aLibraryPath );
  165. }
  166. wxDateTime FP_CACHE::GetLibModificationTime() const
  167. {
  168. return m_lib_path.GetModificationTime();
  169. }
  170. void FP_CACHE::Save()
  171. {
  172. if( !m_lib_path.DirExists() && !m_lib_path.Mkdir() )
  173. {
  174. THROW_IO_ERROR( wxString::Format( _( "Cannot create footprint library path '%s'" ),
  175. m_lib_path.GetPath().GetData() ) );
  176. }
  177. if( !m_lib_path.IsDirWritable() )
  178. {
  179. THROW_IO_ERROR( wxString::Format( _( "Footprint library path '%s' is read only" ),
  180. GetChars( m_lib_path.GetPath() ) ) );
  181. }
  182. for( MODULE_ITER it = m_modules.begin(); it != m_modules.end(); ++it )
  183. {
  184. wxFileName fn = it->second->GetFileName();
  185. if( fn.FileExists() && !it->second->IsModified() )
  186. continue;
  187. wxString tempFileName =
  188. #ifdef USE_TMP_FILE
  189. fn.CreateTempFileName( fn.GetPath() );
  190. #else
  191. fn.GetFullPath();
  192. #endif
  193. // Allow file output stream to go out of scope to close the file stream before
  194. // renaming the file.
  195. {
  196. wxLogTrace( traceFootprintLibrary, wxT( "Creating temporary library file %s" ),
  197. GetChars( tempFileName ) );
  198. FILE_OUTPUTFORMATTER formatter( tempFileName );
  199. m_owner->SetOutputFormatter( &formatter );
  200. m_owner->Format( (BOARD_ITEM*) it->second->GetModule() );
  201. }
  202. #ifdef USE_TMP_FILE
  203. wxRemove( fn.GetFullPath() ); // it is not an error if this does not exist
  204. // Even on linux you can see an _intermittent_ error when calling wxRename(),
  205. // and it is fully inexplicable. See if this dodges the error.
  206. wxMilliSleep( 250L );
  207. if( !wxRenameFile( tempFileName, fn.GetFullPath() ) )
  208. {
  209. wxString msg = wxString::Format(
  210. _( "Cannot rename temporary file '%s' to footprint library file '%s'" ),
  211. GetChars( tempFileName ),
  212. GetChars( fn.GetFullPath() )
  213. );
  214. THROW_IO_ERROR( msg );
  215. }
  216. #endif
  217. it->second->UpdateModificationTime();
  218. m_mod_time = GetLibModificationTime();
  219. }
  220. }
  221. void FP_CACHE::Load()
  222. {
  223. wxDir dir( m_lib_path.GetPath() );
  224. if( !dir.IsOpened() )
  225. {
  226. wxString msg = wxString::Format(
  227. _( "Footprint library path '%s' does not exist" ),
  228. GetChars( m_lib_path.GetPath() )
  229. );
  230. THROW_IO_ERROR( msg );
  231. }
  232. wxString fpFileName;
  233. wxString wildcard = wxT( "*." ) + KiCadFootprintFileExtension;
  234. if( dir.GetFirst( &fpFileName, wildcard, wxDIR_FILES ) )
  235. {
  236. do
  237. {
  238. // prepend the libpath into fullPath
  239. wxFileName fullPath( m_lib_path.GetPath(), fpFileName );
  240. FILE_LINE_READER reader( fullPath.GetFullPath() );
  241. m_owner->m_parser->SetLineReader( &reader );
  242. std::string name = TO_UTF8( fullPath.GetName() );
  243. MODULE* footprint = (MODULE*) m_owner->m_parser->Parse();
  244. // The footprint name is the file name without the extension.
  245. footprint->SetFPID( FPID( fullPath.GetName() ) );
  246. m_modules.insert( name, new FP_CACHE_ITEM( footprint, fullPath ) );
  247. } while( dir.GetNext( &fpFileName ) );
  248. // Remember the file modification time of library file when the
  249. // cache snapshot was made, so that in a networked environment we will
  250. // reload the cache as needed.
  251. m_mod_time = GetLibModificationTime();
  252. }
  253. }
  254. void FP_CACHE::Remove( const wxString& aFootprintName )
  255. {
  256. std::string footprintName = TO_UTF8( aFootprintName );
  257. MODULE_CITER it = m_modules.find( footprintName );
  258. if( it == m_modules.end() )
  259. {
  260. wxString msg = wxString::Format(
  261. _( "library '%s' has no footprint '%s' to delete" ),
  262. GetChars( m_lib_path.GetPath() ),
  263. GetChars( aFootprintName )
  264. );
  265. THROW_IO_ERROR( msg );
  266. }
  267. // Remove the module from the cache and delete the module file from the library.
  268. wxString fullPath = it->second->GetFileName().GetFullPath();
  269. m_modules.erase( footprintName );
  270. wxRemoveFile( fullPath );
  271. }
  272. bool FP_CACHE::IsPath( const wxString& aPath ) const
  273. {
  274. // Converts path separators to native path separators
  275. wxFileName newPath;
  276. newPath.AssignDir( aPath );
  277. return m_lib_path == newPath;
  278. }
  279. bool FP_CACHE::IsModified( const wxString& aLibPath, const wxString& aFootprintName ) const
  280. {
  281. // The library is modified if the library path got deleted or changed.
  282. if( !m_lib_path.DirExists() || !IsPath( aLibPath ) )
  283. return true;
  284. // If no footprint was specified, check every file modification time against the time
  285. // it was loaded.
  286. if( aFootprintName.IsEmpty() )
  287. {
  288. for( MODULE_CITER it = m_modules.begin(); it != m_modules.end(); ++it )
  289. {
  290. wxFileName fn = m_lib_path;
  291. fn.SetName( it->second->GetFileName().GetName() );
  292. fn.SetExt( KiCadFootprintFileExtension );
  293. if( !fn.FileExists() )
  294. {
  295. wxLogTrace( traceFootprintLibrary,
  296. wxT( "Footprint cache file '%s' does not exist." ),
  297. fn.GetFullPath().GetData() );
  298. return true;
  299. }
  300. if( it->second->IsModified() )
  301. {
  302. wxLogTrace( traceFootprintLibrary,
  303. wxT( "Footprint cache file '%s' has been modified." ),
  304. fn.GetFullPath().GetData() );
  305. return true;
  306. }
  307. }
  308. }
  309. else
  310. {
  311. MODULE_CITER it = m_modules.find( TO_UTF8( aFootprintName ) );
  312. if( it == m_modules.end() || it->second->IsModified() )
  313. return true;
  314. }
  315. return false;
  316. }
  317. void PCB_IO::Save( const wxString& aFileName, BOARD* aBoard, const PROPERTIES* aProperties )
  318. {
  319. LOCALE_IO toggle; // toggles on, then off, the C locale.
  320. init( aProperties );
  321. m_board = aBoard; // after init()
  322. // Prepare net mapping that assures that net codes saved in a file are consecutive integers
  323. m_mapping->SetBoard( aBoard );
  324. FILE_OUTPUTFORMATTER formatter( aFileName );
  325. m_out = &formatter; // no ownership
  326. m_out->Print( 0, "(kicad_pcb (version %d) (host pcbnew %s)\n", SEXPR_BOARD_FILE_VERSION,
  327. formatter.Quotew( GetBuildVersion() ).c_str() );
  328. Format( aBoard, 1 );
  329. m_out->Print( 0, ")\n" );
  330. }
  331. BOARD_ITEM* PCB_IO::Parse( const wxString& aClipboardSourceInput )
  332. throw( FUTURE_FORMAT_ERROR, PARSE_ERROR, IO_ERROR )
  333. {
  334. std::string input = TO_UTF8( aClipboardSourceInput );
  335. STRING_LINE_READER reader( input, wxT( "clipboard" ) );
  336. m_parser->SetLineReader( &reader );
  337. try
  338. {
  339. return m_parser->Parse();
  340. }
  341. catch( const PARSE_ERROR& parse_error )
  342. {
  343. if( m_parser->IsTooRecent() )
  344. throw FUTURE_FORMAT_ERROR( parse_error, m_parser->GetRequiredVersion() );
  345. else
  346. throw;
  347. }
  348. }
  349. void PCB_IO::Format( BOARD_ITEM* aItem, int aNestLevel ) const
  350. throw( IO_ERROR )
  351. {
  352. LOCALE_IO toggle; // public API function, perform anything convenient for caller
  353. switch( aItem->Type() )
  354. {
  355. case PCB_T:
  356. format( static_cast<BOARD*>( aItem ), aNestLevel );
  357. break;
  358. case PCB_DIMENSION_T:
  359. format( static_cast<DIMENSION*>( aItem ), aNestLevel );
  360. break;
  361. case PCB_LINE_T:
  362. format( static_cast<DRAWSEGMENT*>( aItem ), aNestLevel );
  363. break;
  364. case PCB_MODULE_EDGE_T:
  365. format( static_cast<EDGE_MODULE*>( aItem ), aNestLevel );
  366. break;
  367. case PCB_TARGET_T:
  368. format( static_cast<PCB_TARGET*>( aItem ), aNestLevel );
  369. break;
  370. case PCB_MODULE_T:
  371. format( static_cast<MODULE*>( aItem ), aNestLevel );
  372. break;
  373. case PCB_PAD_T:
  374. format( static_cast<D_PAD*>( aItem ), aNestLevel );
  375. break;
  376. case PCB_TEXT_T:
  377. format( static_cast<TEXTE_PCB*>( aItem ), aNestLevel );
  378. break;
  379. case PCB_MODULE_TEXT_T:
  380. format( static_cast<TEXTE_MODULE*>( aItem ), aNestLevel );
  381. break;
  382. case PCB_TRACE_T:
  383. case PCB_VIA_T:
  384. format( static_cast<TRACK*>( aItem ), aNestLevel );
  385. break;
  386. case PCB_ZONE_AREA_T:
  387. format( static_cast<ZONE_CONTAINER*>( aItem ), aNestLevel );
  388. break;
  389. default:
  390. wxFAIL_MSG( wxT( "Cannot format item " ) + aItem->GetClass() );
  391. }
  392. }
  393. void PCB_IO::formatLayer( const BOARD_ITEM* aItem ) const
  394. {
  395. if( m_ctl & CTL_STD_LAYER_NAMES )
  396. {
  397. LAYER_ID layer = aItem->GetLayer();
  398. // English layer names should never need quoting.
  399. m_out->Print( 0, " (layer %s)", TO_UTF8( BOARD::GetStandardLayerName( layer ) ) );
  400. }
  401. else
  402. m_out->Print( 0, " (layer %s)", m_out->Quotew( aItem->GetLayerName() ).c_str() );
  403. }
  404. void PCB_IO::format( BOARD* aBoard, int aNestLevel ) const
  405. throw( IO_ERROR )
  406. {
  407. const BOARD_DESIGN_SETTINGS& dsnSettings = aBoard->GetDesignSettings();
  408. m_out->Print( 0, "\n" );
  409. m_out->Print( aNestLevel, "(general\n" );
  410. m_out->Print( aNestLevel+1, "(links %d)\n", aBoard->GetRatsnestsCount() );
  411. m_out->Print( aNestLevel+1, "(no_connects %d)\n", aBoard->GetUnconnectedNetCount() );
  412. // Write Bounding box info
  413. m_out->Print( aNestLevel+1, "(area %s %s %s %s)\n",
  414. FMTIU( aBoard->GetBoundingBox().GetX() ).c_str(),
  415. FMTIU( aBoard->GetBoundingBox().GetY() ).c_str(),
  416. FMTIU( aBoard->GetBoundingBox().GetRight() ).c_str(),
  417. FMTIU( aBoard->GetBoundingBox().GetBottom() ).c_str() );
  418. m_out->Print( aNestLevel+1, "(thickness %s)\n",
  419. FMTIU( dsnSettings.GetBoardThickness() ).c_str() );
  420. m_out->Print( aNestLevel+1, "(drawings %d)\n", aBoard->m_Drawings.GetCount() );
  421. m_out->Print( aNestLevel+1, "(tracks %d)\n", aBoard->GetNumSegmTrack() );
  422. m_out->Print( aNestLevel+1, "(zones %d)\n", aBoard->GetNumSegmZone() );
  423. m_out->Print( aNestLevel+1, "(modules %d)\n", aBoard->m_Modules.GetCount() );
  424. m_out->Print( aNestLevel+1, "(nets %d)\n", m_mapping->GetSize() );
  425. m_out->Print( aNestLevel, ")\n\n" );
  426. aBoard->GetPageSettings().Format( m_out, aNestLevel, m_ctl );
  427. aBoard->GetTitleBlock().Format( m_out, aNestLevel, m_ctl );
  428. // Layers.
  429. m_out->Print( aNestLevel, "(layers\n" );
  430. // Save only the used copper layers from front to back.
  431. LSET visible_layers = aBoard->GetVisibleLayers();
  432. for( LSEQ cu = aBoard->GetEnabledLayers().CuStack(); cu; ++cu )
  433. {
  434. LAYER_ID layer = *cu;
  435. m_out->Print( aNestLevel+1, "(%d %s %s", layer,
  436. m_out->Quotew( aBoard->GetLayerName( layer ) ).c_str(),
  437. LAYER::ShowType( aBoard->GetLayerType( layer ) ) );
  438. if( !visible_layers[layer] )
  439. m_out->Print( 0, " hide" );
  440. m_out->Print( 0, ")\n" );
  441. }
  442. // Save used non-copper layers in the order they are defined.
  443. // desired sequence for non Cu BOARD layers.
  444. static const LAYER_ID non_cu[] = {
  445. B_Adhes, // 32
  446. F_Adhes,
  447. B_Paste,
  448. F_Paste,
  449. B_SilkS,
  450. F_SilkS,
  451. B_Mask,
  452. F_Mask,
  453. Dwgs_User,
  454. Cmts_User,
  455. Eco1_User,
  456. Eco2_User,
  457. Edge_Cuts,
  458. Margin,
  459. B_CrtYd,
  460. F_CrtYd,
  461. B_Fab,
  462. F_Fab
  463. };
  464. for( LSEQ seq = aBoard->GetEnabledLayers().Seq( non_cu, DIM( non_cu ) ); seq; ++seq )
  465. {
  466. LAYER_ID layer = *seq;
  467. m_out->Print( aNestLevel+1, "(%d %s user", layer,
  468. m_out->Quotew( aBoard->GetLayerName( layer ) ).c_str() );
  469. if( !visible_layers[layer] )
  470. m_out->Print( 0, " hide" );
  471. m_out->Print( 0, ")\n" );
  472. }
  473. m_out->Print( aNestLevel, ")\n\n" );
  474. // Setup
  475. m_out->Print( aNestLevel, "(setup\n" );
  476. // Save current default track width, for compatibility with older Pcbnew version;
  477. m_out->Print( aNestLevel+1, "(last_trace_width %s)\n",
  478. FMTIU( dsnSettings.GetCurrentTrackWidth() ).c_str() );
  479. // Save custom tracks width list (the first is not saved here: this is the netclass value
  480. for( unsigned ii = 1; ii < dsnSettings.m_TrackWidthList.size(); ii++ )
  481. m_out->Print( aNestLevel+1, "(user_trace_width %s)\n",
  482. FMTIU( dsnSettings.m_TrackWidthList[ii] ).c_str() );
  483. m_out->Print( aNestLevel+1, "(trace_clearance %s)\n",
  484. FMTIU( dsnSettings.GetDefault()->GetClearance() ).c_str() );
  485. // ZONE_SETTINGS
  486. m_out->Print( aNestLevel+1, "(zone_clearance %s)\n",
  487. FMTIU( aBoard->GetZoneSettings().m_ZoneClearance ).c_str() );
  488. m_out->Print( aNestLevel+1, "(zone_45_only %s)\n",
  489. aBoard->GetZoneSettings().m_Zone_45_Only ? "yes" : "no" );
  490. m_out->Print( aNestLevel+1, "(trace_min %s)\n",
  491. FMTIU( dsnSettings.m_TrackMinWidth ).c_str() );
  492. m_out->Print( aNestLevel+1, "(segment_width %s)\n",
  493. FMTIU( dsnSettings.m_DrawSegmentWidth ).c_str() );
  494. m_out->Print( aNestLevel+1, "(edge_width %s)\n",
  495. FMTIU( dsnSettings.m_EdgeSegmentWidth ).c_str() );
  496. // Save current default via size, for compatibility with older Pcbnew version;
  497. m_out->Print( aNestLevel+1, "(via_size %s)\n",
  498. FMTIU( dsnSettings.GetDefault()->GetViaDiameter() ).c_str() );
  499. m_out->Print( aNestLevel+1, "(via_drill %s)\n",
  500. FMTIU( dsnSettings.GetDefault()->GetViaDrill() ).c_str() );
  501. m_out->Print( aNestLevel+1, "(via_min_size %s)\n",
  502. FMTIU( dsnSettings.m_ViasMinSize ).c_str() );
  503. m_out->Print( aNestLevel+1, "(via_min_drill %s)\n",
  504. FMTIU( dsnSettings.m_ViasMinDrill ).c_str() );
  505. // Save custom vias diameters list (the first is not saved here: this is
  506. // the netclass value
  507. for( unsigned ii = 1; ii < dsnSettings.m_ViasDimensionsList.size(); ii++ )
  508. m_out->Print( aNestLevel+1, "(user_via %s %s)\n",
  509. FMTIU( dsnSettings.m_ViasDimensionsList[ii].m_Diameter ).c_str(),
  510. FMTIU( dsnSettings.m_ViasDimensionsList[ii].m_Drill ).c_str() );
  511. // for old versions compatibility:
  512. if( dsnSettings.m_BlindBuriedViaAllowed )
  513. m_out->Print( aNestLevel+1, "(blind_buried_vias_allowed yes)\n" );
  514. m_out->Print( aNestLevel+1, "(uvia_size %s)\n",
  515. FMTIU( dsnSettings.GetDefault()->GetuViaDiameter() ).c_str() );
  516. m_out->Print( aNestLevel+1, "(uvia_drill %s)\n",
  517. FMTIU( dsnSettings.GetDefault()->GetuViaDrill() ).c_str() );
  518. m_out->Print( aNestLevel+1, "(uvias_allowed %s)\n",
  519. ( dsnSettings.m_MicroViasAllowed ) ? "yes" : "no" );
  520. m_out->Print( aNestLevel+1, "(uvia_min_size %s)\n",
  521. FMTIU( dsnSettings.m_MicroViasMinSize ).c_str() );
  522. m_out->Print( aNestLevel+1, "(uvia_min_drill %s)\n",
  523. FMTIU( dsnSettings.m_MicroViasMinDrill ).c_str() );
  524. m_out->Print( aNestLevel+1, "(pcb_text_width %s)\n",
  525. FMTIU( dsnSettings.m_PcbTextWidth ).c_str() );
  526. m_out->Print( aNestLevel+1, "(pcb_text_size %s %s)\n",
  527. FMTIU( dsnSettings.m_PcbTextSize.x ).c_str(),
  528. FMTIU( dsnSettings.m_PcbTextSize.y ).c_str() );
  529. m_out->Print( aNestLevel+1, "(mod_edge_width %s)\n",
  530. FMTIU( dsnSettings.m_ModuleSegmentWidth ).c_str() );
  531. m_out->Print( aNestLevel+1, "(mod_text_size %s %s)\n",
  532. FMTIU( dsnSettings.m_ModuleTextSize.x ).c_str(),
  533. FMTIU( dsnSettings.m_ModuleTextSize.y ).c_str() );
  534. m_out->Print( aNestLevel+1, "(mod_text_width %s)\n",
  535. FMTIU( dsnSettings.m_ModuleTextWidth ).c_str() );
  536. m_out->Print( aNestLevel+1, "(pad_size %s %s)\n",
  537. FMTIU( dsnSettings.m_Pad_Master.GetSize().x ).c_str(),
  538. FMTIU( dsnSettings.m_Pad_Master.GetSize().y ).c_str() );
  539. m_out->Print( aNestLevel+1, "(pad_drill %s)\n",
  540. FMTIU( dsnSettings.m_Pad_Master.GetDrillSize().x ).c_str() );
  541. m_out->Print( aNestLevel+1, "(pad_to_mask_clearance %s)\n",
  542. FMTIU( dsnSettings.m_SolderMaskMargin ).c_str() );
  543. if( dsnSettings.m_SolderMaskMinWidth )
  544. m_out->Print( aNestLevel+1, "(solder_mask_min_width %s)\n",
  545. FMTIU( dsnSettings.m_SolderMaskMinWidth ).c_str() );
  546. if( dsnSettings.m_SolderPasteMargin != 0 )
  547. m_out->Print( aNestLevel+1, "(pad_to_paste_clearance %s)\n",
  548. FMTIU( dsnSettings.m_SolderPasteMargin ).c_str() );
  549. if( dsnSettings.m_SolderPasteMarginRatio != 0 )
  550. m_out->Print( aNestLevel+1, "(pad_to_paste_clearance_ratio %s)\n",
  551. Double2Str( dsnSettings.m_SolderPasteMarginRatio ).c_str() );
  552. m_out->Print( aNestLevel+1, "(aux_axis_origin %s %s)\n",
  553. FMTIU( aBoard->GetAuxOrigin().x ).c_str(),
  554. FMTIU( aBoard->GetAuxOrigin().y ).c_str() );
  555. if( aBoard->GetGridOrigin().x || aBoard->GetGridOrigin().y )
  556. m_out->Print( aNestLevel+1, "(grid_origin %s %s)\n",
  557. FMTIU( aBoard->GetGridOrigin().x ).c_str(),
  558. FMTIU( aBoard->GetGridOrigin().y ).c_str() );
  559. m_out->Print( aNestLevel+1, "(visible_elements %X)\n",
  560. dsnSettings.GetVisibleElements() );
  561. aBoard->GetPlotOptions().Format( m_out, aNestLevel+1 );
  562. m_out->Print( aNestLevel, ")\n\n" );
  563. // Save net codes and names
  564. for( NETINFO_MAPPING::iterator net = m_mapping->begin(), netEnd = m_mapping->end();
  565. net != netEnd; ++net )
  566. {
  567. m_out->Print( aNestLevel, "(net %d %s)\n",
  568. m_mapping->Translate( net->GetNet() ),
  569. m_out->Quotew( net->GetNetname() ).c_str() );
  570. }
  571. m_out->Print( 0, "\n" );
  572. // Save the default net class first.
  573. NETCLASS defaultNC = *dsnSettings.GetDefault();
  574. filterNetClass( *aBoard, defaultNC ); // Remove empty nets (from a copy of a netclass)
  575. defaultNC.Format( m_out, aNestLevel, m_ctl );
  576. // Save the rest of the net classes alphabetically.
  577. for( NETCLASSES::const_iterator it = dsnSettings.m_NetClasses.begin();
  578. it != dsnSettings.m_NetClasses.end();
  579. ++it )
  580. {
  581. NETCLASS netclass = *it->second;
  582. filterNetClass( *aBoard, netclass ); // Remove empty nets (from a copy of a netclass)
  583. netclass.Format( m_out, aNestLevel, m_ctl );
  584. }
  585. // Save the modules.
  586. for( MODULE* module = aBoard->m_Modules; module; module = module->Next() )
  587. {
  588. Format( module, aNestLevel );
  589. m_out->Print( 0, "\n" );
  590. }
  591. // Save the graphical items on the board (not owned by a module)
  592. for( BOARD_ITEM* item = aBoard->m_Drawings; item; item = item->Next() )
  593. Format( item, aNestLevel );
  594. if( aBoard->m_Drawings.GetCount() )
  595. m_out->Print( 0, "\n" );
  596. // Do not save MARKER_PCBs, they can be regenerated easily.
  597. // Save the tracks and vias.
  598. for( TRACK* track = aBoard->m_Track; track; track = track->Next() )
  599. Format( track, aNestLevel );
  600. if( aBoard->m_Track.GetCount() )
  601. m_out->Print( 0, "\n" );
  602. /// @todo Add warning here that the old segment filed zones are no longer supported and
  603. /// will not be saved.
  604. // Save the polygon (which are the newer technology) zones.
  605. for( int i = 0; i < aBoard->GetAreaCount(); ++i )
  606. Format( aBoard->GetArea( i ), aNestLevel );
  607. }
  608. void PCB_IO::format( DIMENSION* aDimension, int aNestLevel ) const
  609. throw( IO_ERROR )
  610. {
  611. m_out->Print( aNestLevel, "(dimension %s (width %s)",
  612. FMT_IU( aDimension->GetValue() ).c_str(),
  613. FMT_IU( aDimension->GetWidth() ).c_str() );
  614. formatLayer( aDimension );
  615. if( aDimension->GetTimeStamp() )
  616. m_out->Print( 0, " (tstamp %lX)", aDimension->GetTimeStamp() );
  617. m_out->Print( 0, "\n" );
  618. Format( &aDimension->Text(), aNestLevel+1 );
  619. m_out->Print( aNestLevel+1, "(feature1 (pts (xy %s %s) (xy %s %s)))\n",
  620. FMT_IU( aDimension->m_featureLineDO.x ).c_str(),
  621. FMT_IU( aDimension->m_featureLineDO.y ).c_str(),
  622. FMT_IU( aDimension->m_featureLineDF.x ).c_str(),
  623. FMT_IU( aDimension->m_featureLineDF.y ).c_str() );
  624. m_out->Print( aNestLevel+1, "(feature2 (pts (xy %s %s) (xy %s %s)))\n",
  625. FMT_IU( aDimension->m_featureLineGO.x ).c_str(),
  626. FMT_IU( aDimension->m_featureLineGO.y ).c_str(),
  627. FMT_IU( aDimension->m_featureLineGF.x ).c_str(),
  628. FMT_IU( aDimension->m_featureLineGF.y ).c_str() );
  629. m_out->Print( aNestLevel+1, "(crossbar (pts (xy %s %s) (xy %s %s)))\n",
  630. FMT_IU( aDimension->m_crossBarO.x ).c_str(),
  631. FMT_IU( aDimension->m_crossBarO.y ).c_str(),
  632. FMT_IU( aDimension->m_crossBarF.x ).c_str(),
  633. FMT_IU( aDimension->m_crossBarF.y ).c_str() );
  634. m_out->Print( aNestLevel+1, "(arrow1a (pts (xy %s %s) (xy %s %s)))\n",
  635. FMT_IU( aDimension->m_crossBarF.x ).c_str(),
  636. FMT_IU( aDimension->m_crossBarF.y ).c_str(),
  637. FMT_IU( aDimension->m_arrowD1F.x ).c_str(),
  638. FMT_IU( aDimension->m_arrowD1F.y ).c_str() );
  639. m_out->Print( aNestLevel+1, "(arrow1b (pts (xy %s %s) (xy %s %s)))\n",
  640. FMT_IU( aDimension->m_crossBarF.x ).c_str(),
  641. FMT_IU( aDimension->m_crossBarF.y ).c_str(),
  642. FMT_IU( aDimension->m_arrowD2F.x ).c_str(),
  643. FMT_IU( aDimension->m_arrowD2F.y ).c_str() );
  644. m_out->Print( aNestLevel+1, "(arrow2a (pts (xy %s %s) (xy %s %s)))\n",
  645. FMT_IU( aDimension->m_crossBarO.x ).c_str(),
  646. FMT_IU( aDimension->m_crossBarO.y ).c_str(),
  647. FMT_IU( aDimension->m_arrowG1F.x ).c_str(),
  648. FMT_IU( aDimension->m_arrowG1F.y ).c_str() );
  649. m_out->Print( aNestLevel+1, "(arrow2b (pts (xy %s %s) (xy %s %s)))\n",
  650. FMT_IU( aDimension->m_crossBarO.x ).c_str(),
  651. FMT_IU( aDimension->m_crossBarO.y ).c_str(),
  652. FMT_IU( aDimension->m_arrowG2F.x ).c_str(),
  653. FMT_IU( aDimension->m_arrowG2F.y ).c_str() );
  654. m_out->Print( aNestLevel, ")\n" );
  655. }
  656. void PCB_IO::format( DRAWSEGMENT* aSegment, int aNestLevel ) const
  657. throw( IO_ERROR )
  658. {
  659. unsigned i;
  660. switch( aSegment->GetShape() )
  661. {
  662. case S_SEGMENT: // Line
  663. m_out->Print( aNestLevel, "(gr_line (start %s) (end %s)",
  664. FMT_IU( aSegment->GetStart() ).c_str(),
  665. FMT_IU( aSegment->GetEnd() ).c_str() );
  666. if( aSegment->GetAngle() != 0.0 )
  667. m_out->Print( 0, " (angle %s)", FMT_ANGLE( aSegment->GetAngle() ).c_str() );
  668. break;
  669. case S_CIRCLE: // Circle
  670. m_out->Print( aNestLevel, "(gr_circle (center %s) (end %s)",
  671. FMT_IU( aSegment->GetStart() ).c_str(),
  672. FMT_IU( aSegment->GetEnd() ).c_str() );
  673. break;
  674. case S_ARC: // Arc
  675. m_out->Print( aNestLevel, "(gr_arc (start %s) (end %s) (angle %s)",
  676. FMT_IU( aSegment->GetStart() ).c_str(),
  677. FMT_IU( aSegment->GetEnd() ).c_str(),
  678. FMT_ANGLE( aSegment->GetAngle() ).c_str() );
  679. break;
  680. case S_POLYGON: // Polygon
  681. m_out->Print( aNestLevel, "(gr_poly (pts" );
  682. for( i = 0; i < aSegment->GetPolyPoints().size(); ++i )
  683. m_out->Print( 0, " (xy %s)", FMT_IU( aSegment->GetPolyPoints()[i] ).c_str() );
  684. m_out->Print( 0, ")" );
  685. break;
  686. case S_CURVE: // Bezier curve
  687. m_out->Print( aNestLevel, "(gr_curve (pts (xy %s) (xy %s) (xy %s) (xy %s))",
  688. FMT_IU( aSegment->GetStart() ).c_str(),
  689. FMT_IU( aSegment->GetBezControl1() ).c_str(),
  690. FMT_IU( aSegment->GetBezControl2() ).c_str(),
  691. FMT_IU( aSegment->GetEnd() ).c_str() );
  692. break;
  693. default:
  694. wxFAIL_MSG( wxT( "Cannot format invalid DRAWSEGMENT type." ) );
  695. };
  696. formatLayer( aSegment );
  697. if( aSegment->GetWidth() != 0 )
  698. m_out->Print( 0, " (width %s)", FMT_IU( aSegment->GetWidth() ).c_str() );
  699. if( aSegment->GetTimeStamp() )
  700. m_out->Print( 0, " (tstamp %lX)", aSegment->GetTimeStamp() );
  701. if( aSegment->GetStatus() )
  702. m_out->Print( 0, " (status %X)", aSegment->GetStatus() );
  703. m_out->Print( 0, ")\n" );
  704. }
  705. void PCB_IO::format( EDGE_MODULE* aModuleDrawing, int aNestLevel ) const
  706. throw( IO_ERROR )
  707. {
  708. switch( aModuleDrawing->GetShape() )
  709. {
  710. case S_SEGMENT: // Line
  711. m_out->Print( aNestLevel, "(fp_line (start %s) (end %s)",
  712. FMT_IU( aModuleDrawing->GetStart0() ).c_str(),
  713. FMT_IU( aModuleDrawing->GetEnd0() ).c_str() );
  714. break;
  715. case S_CIRCLE: // Circle
  716. m_out->Print( aNestLevel, "(fp_circle (center %s) (end %s)",
  717. FMT_IU( aModuleDrawing->GetStart0() ).c_str(),
  718. FMT_IU( aModuleDrawing->GetEnd0() ).c_str() );
  719. break;
  720. case S_ARC: // Arc
  721. m_out->Print( aNestLevel, "(fp_arc (start %s) (end %s) (angle %s)",
  722. FMT_IU( aModuleDrawing->GetStart0() ).c_str(),
  723. FMT_IU( aModuleDrawing->GetEnd0() ).c_str(),
  724. FMT_ANGLE( aModuleDrawing->GetAngle() ).c_str() );
  725. break;
  726. case S_POLYGON: // Polygon
  727. m_out->Print( aNestLevel, "(fp_poly (pts" );
  728. for( unsigned i = 0; i < aModuleDrawing->GetPolyPoints().size(); ++i )
  729. {
  730. int nestLevel = 0;
  731. if( i && !(i%4) ) // newline every 4(pts)
  732. {
  733. nestLevel = aNestLevel + 1;
  734. m_out->Print( 0, "\n" );
  735. }
  736. m_out->Print( nestLevel, "%s(xy %s)",
  737. nestLevel ? "" : " ",
  738. FMT_IU( aModuleDrawing->GetPolyPoints()[i] ).c_str() );
  739. }
  740. m_out->Print( 0, ")" );
  741. break;
  742. case S_CURVE: // Bezier curve
  743. m_out->Print( aNestLevel, "(fp_curve (pts (xy %s) (xy %s) (xy %s) (xy %s))",
  744. FMT_IU( aModuleDrawing->GetStart0() ).c_str(),
  745. FMT_IU( aModuleDrawing->GetBezControl1() ).c_str(),
  746. FMT_IU( aModuleDrawing->GetBezControl2() ).c_str(),
  747. FMT_IU( aModuleDrawing->GetEnd0() ).c_str() );
  748. break;
  749. default:
  750. wxFAIL_MSG( wxT( "Cannot format invalid DRAWSEGMENT type." ) );
  751. };
  752. formatLayer( aModuleDrawing );
  753. m_out->Print( 0, " (width %s)", FMT_IU( aModuleDrawing->GetWidth() ).c_str() );
  754. m_out->Print( 0, ")\n" );
  755. }
  756. void PCB_IO::format( PCB_TARGET* aTarget, int aNestLevel ) const
  757. throw( IO_ERROR )
  758. {
  759. m_out->Print( aNestLevel, "(target %s (at %s) (size %s)",
  760. ( aTarget->GetShape() ) ? "x" : "plus",
  761. FMT_IU( aTarget->GetPosition() ).c_str(),
  762. FMT_IU( aTarget->GetSize() ).c_str() );
  763. if( aTarget->GetWidth() != 0 )
  764. m_out->Print( 0, " (width %s)", FMT_IU( aTarget->GetWidth() ).c_str() );
  765. formatLayer( aTarget );
  766. if( aTarget->GetTimeStamp() )
  767. m_out->Print( 0, " (tstamp %lX)", aTarget->GetTimeStamp() );
  768. m_out->Print( 0, ")\n" );
  769. }
  770. void PCB_IO::format( MODULE* aModule, int aNestLevel ) const
  771. throw( IO_ERROR )
  772. {
  773. if( !( m_ctl & CTL_OMIT_INITIAL_COMMENTS ) )
  774. {
  775. const wxArrayString* initial_comments = aModule->GetInitialComments();
  776. if( initial_comments )
  777. {
  778. for( unsigned i=0; i<initial_comments->GetCount(); ++i )
  779. m_out->Print( aNestLevel, "%s\n", TO_UTF8( (*initial_comments)[i] ) );
  780. m_out->Print( 0, "\n" ); // improve readability?
  781. }
  782. }
  783. m_out->Print( aNestLevel, "(module %s",
  784. m_out->Quotes( aModule->GetFPID().Format() ).c_str() );
  785. if( aModule->IsLocked() )
  786. m_out->Print( 0, " locked" );
  787. if( aModule->IsPlaced() )
  788. m_out->Print( 0, " placed" );
  789. formatLayer( aModule );
  790. m_out->Print( 0, " (tedit %lX)", aModule->GetLastEditTime() );
  791. if( !( m_ctl & CTL_OMIT_TSTAMPS ) )
  792. {
  793. m_out->Print( 0, " (tstamp %lX)\n", aModule->GetTimeStamp() );
  794. }
  795. else
  796. m_out->Print( 0, "\n" );
  797. if( !( m_ctl & CTL_OMIT_AT ) )
  798. {
  799. m_out->Print( aNestLevel+1, "(at %s", FMT_IU( aModule->GetPosition() ).c_str() );
  800. if( aModule->GetOrientation() != 0.0 )
  801. m_out->Print( 0, " %s", FMT_ANGLE( aModule->GetOrientation() ).c_str() );
  802. m_out->Print( 0, ")\n" );
  803. }
  804. if( !aModule->GetDescription().IsEmpty() )
  805. m_out->Print( aNestLevel+1, "(descr %s)\n",
  806. m_out->Quotew( aModule->GetDescription() ).c_str() );
  807. if( !aModule->GetKeywords().IsEmpty() )
  808. m_out->Print( aNestLevel+1, "(tags %s)\n",
  809. m_out->Quotew( aModule->GetKeywords() ).c_str() );
  810. if( !( m_ctl & CTL_OMIT_PATH ) && !!aModule->GetPath() )
  811. m_out->Print( aNestLevel+1, "(path %s)\n",
  812. m_out->Quotew( aModule->GetPath() ).c_str() );
  813. if( aModule->GetPlacementCost90() != 0 )
  814. m_out->Print( aNestLevel+1, "(autoplace_cost90 %d)\n", aModule->GetPlacementCost90() );
  815. if( aModule->GetPlacementCost180() != 0 )
  816. m_out->Print( aNestLevel+1, "(autoplace_cost180 %d)\n", aModule->GetPlacementCost180() );
  817. if( aModule->GetLocalSolderMaskMargin() != 0 )
  818. m_out->Print( aNestLevel+1, "(solder_mask_margin %s)\n",
  819. FMT_IU( aModule->GetLocalSolderMaskMargin() ).c_str() );
  820. if( aModule->GetLocalSolderPasteMargin() != 0 )
  821. m_out->Print( aNestLevel+1, "(solder_paste_margin %s)\n",
  822. FMT_IU( aModule->GetLocalSolderPasteMargin() ).c_str() );
  823. if( aModule->GetLocalSolderPasteMarginRatio() != 0 )
  824. m_out->Print( aNestLevel+1, "(solder_paste_ratio %s)\n",
  825. Double2Str( aModule->GetLocalSolderPasteMarginRatio() ).c_str() );
  826. if( aModule->GetLocalClearance() != 0 )
  827. m_out->Print( aNestLevel+1, "(clearance %s)\n",
  828. FMT_IU( aModule->GetLocalClearance() ).c_str() );
  829. if( aModule->GetZoneConnection() != PAD_ZONE_CONN_INHERITED )
  830. m_out->Print( aNestLevel+1, "(zone_connect %d)\n", aModule->GetZoneConnection() );
  831. if( aModule->GetThermalWidth() != 0 )
  832. m_out->Print( aNestLevel+1, "(thermal_width %s)\n",
  833. FMT_IU( aModule->GetThermalWidth() ).c_str() );
  834. if( aModule->GetThermalGap() != 0 )
  835. m_out->Print( aNestLevel+1, "(thermal_gap %s)\n",
  836. FMT_IU( aModule->GetThermalGap() ).c_str() );
  837. // Attributes
  838. if( aModule->GetAttributes() != MOD_DEFAULT )
  839. {
  840. m_out->Print( aNestLevel+1, "(attr" );
  841. if( aModule->GetAttributes() & MOD_CMS )
  842. m_out->Print( 0, " smd" );
  843. if( aModule->GetAttributes() & MOD_VIRTUAL )
  844. m_out->Print( 0, " virtual" );
  845. m_out->Print( 0, ")\n" );
  846. }
  847. Format( (BOARD_ITEM*) &aModule->Reference(), aNestLevel+1 );
  848. Format( (BOARD_ITEM*) &aModule->Value(), aNestLevel+1 );
  849. // Save drawing elements.
  850. for( BOARD_ITEM* gr = aModule->GraphicalItems(); gr; gr = gr->Next() )
  851. Format( gr, aNestLevel+1 );
  852. // Save pads.
  853. for( D_PAD* pad = aModule->Pads(); pad; pad = pad->Next() )
  854. format( pad, aNestLevel+1 );
  855. // Save 3D info.
  856. std::list<S3D_INFO>::const_iterator bs3D = aModule->Models().begin();
  857. std::list<S3D_INFO>::const_iterator es3D = aModule->Models().end();
  858. while( bs3D != es3D )
  859. {
  860. if( !bs3D->m_Filename.IsEmpty() )
  861. {
  862. m_out->Print( aNestLevel+1, "(model %s\n",
  863. m_out->Quotew( bs3D->m_Filename ).c_str() );
  864. m_out->Print( aNestLevel+2, "(at (xyz %s %s %s))\n",
  865. Double2Str( bs3D->m_Offset.x ).c_str(),
  866. Double2Str( bs3D->m_Offset.y ).c_str(),
  867. Double2Str( bs3D->m_Offset.z ).c_str() );
  868. m_out->Print( aNestLevel+2, "(scale (xyz %s %s %s))\n",
  869. Double2Str( bs3D->m_Scale.x ).c_str(),
  870. Double2Str( bs3D->m_Scale.y ).c_str(),
  871. Double2Str( bs3D->m_Scale.z ).c_str() );
  872. m_out->Print( aNestLevel+2, "(rotate (xyz %s %s %s))\n",
  873. Double2Str( bs3D->m_Rotation.x ).c_str(),
  874. Double2Str( bs3D->m_Rotation.y ).c_str(),
  875. Double2Str( bs3D->m_Rotation.z ).c_str() );
  876. m_out->Print( aNestLevel+1, ")\n" );
  877. }
  878. ++bs3D;
  879. }
  880. m_out->Print( aNestLevel, ")\n" );
  881. }
  882. void PCB_IO::formatLayers( LSET aLayerMask, int aNestLevel ) const
  883. throw( IO_ERROR )
  884. {
  885. std::string output;
  886. if( aNestLevel == 0 )
  887. output += ' ';
  888. output += "(layers";
  889. static const LSET cu_all( LSET::AllCuMask() );
  890. static const LSET fr_bk( 2, B_Cu, F_Cu );
  891. static const LSET adhes( 2, B_Adhes, F_Adhes );
  892. static const LSET paste( 2, B_Paste, F_Paste );
  893. static const LSET silks( 2, B_SilkS, F_SilkS );
  894. static const LSET mask( 2, B_Mask, F_Mask );
  895. static const LSET crt_yd(2, B_CrtYd, F_CrtYd );
  896. static const LSET fab( 2, B_Fab, F_Fab );
  897. LSET cu_mask = cu_all;
  898. if( m_board )
  899. cu_mask &= m_board->GetEnabledLayers();
  900. // output copper layers first, then non copper
  901. if( ( aLayerMask & cu_mask ) == cu_mask )
  902. {
  903. output += " *.Cu";
  904. aLayerMask &= ~cu_all; // clear bits, so they are not output again below
  905. }
  906. else if( ( aLayerMask & cu_mask ) == fr_bk )
  907. {
  908. output += " F&B.Cu";
  909. aLayerMask &= ~fr_bk;
  910. }
  911. if( ( aLayerMask & adhes ) == adhes )
  912. {
  913. output += " *.Adhes";
  914. aLayerMask &= ~adhes;
  915. }
  916. if( ( aLayerMask & paste ) == paste )
  917. {
  918. output += " *.Paste";
  919. aLayerMask &= ~paste;
  920. }
  921. if( ( aLayerMask & silks ) == silks )
  922. {
  923. output += " *.SilkS";
  924. aLayerMask &= ~silks;
  925. }
  926. if( ( aLayerMask & mask ) == mask )
  927. {
  928. output += " *.Mask";
  929. aLayerMask &= ~mask;
  930. }
  931. if( ( aLayerMask & crt_yd ) == crt_yd )
  932. {
  933. output += " *.CrtYd";
  934. aLayerMask &= ~crt_yd;
  935. }
  936. if( ( aLayerMask & fab ) == fab )
  937. {
  938. output += " *.Fab";
  939. aLayerMask &= ~fab;
  940. }
  941. // output any individual layers not handled in wildcard combos above
  942. if( m_board )
  943. aLayerMask &= m_board->GetEnabledLayers();
  944. wxString layerName;
  945. for( LAYER_NUM layer = 0; layer < LAYER_ID_COUNT; ++layer )
  946. {
  947. if( aLayerMask[layer] )
  948. {
  949. if( m_board && !( m_ctl & CTL_STD_LAYER_NAMES ) )
  950. layerName = m_board->GetLayerName( LAYER_ID( layer ) );
  951. else // I am being called from FootprintSave()
  952. layerName = BOARD::GetStandardLayerName( LAYER_ID( layer ) );
  953. output += ' ';
  954. output += m_out->Quotew( layerName );
  955. }
  956. }
  957. m_out->Print( aNestLevel, "%s)", output.c_str() );
  958. }
  959. void PCB_IO::format( D_PAD* aPad, int aNestLevel ) const
  960. throw( IO_ERROR )
  961. {
  962. const char* shape;
  963. switch( aPad->GetShape() )
  964. {
  965. case PAD_SHAPE_CIRCLE: shape = "circle"; break;
  966. case PAD_SHAPE_RECT: shape = "rect"; break;
  967. case PAD_SHAPE_OVAL: shape = "oval"; break;
  968. case PAD_SHAPE_TRAPEZOID: shape = "trapezoid"; break;
  969. case PAD_SHAPE_ROUNDRECT: shape = "roundrect"; break;
  970. default:
  971. THROW_IO_ERROR( wxString::Format( _( "unknown pad type: %d"), aPad->GetShape() ) );
  972. }
  973. const char* type;
  974. switch( aPad->GetAttribute() )
  975. {
  976. case PAD_ATTRIB_STANDARD: type = "thru_hole"; break;
  977. case PAD_ATTRIB_SMD: type = "smd"; break;
  978. case PAD_ATTRIB_CONN: type = "connect"; break;
  979. case PAD_ATTRIB_HOLE_NOT_PLATED: type = "np_thru_hole"; break;
  980. default:
  981. THROW_IO_ERROR( wxString::Format( _( "unknown pad attribute: %d" ),
  982. aPad->GetAttribute() ) );
  983. }
  984. m_out->Print( aNestLevel, "(pad %s %s %s",
  985. m_out->Quotew( aPad->GetPadName() ).c_str(),
  986. type, shape );
  987. m_out->Print( 0, " (at %s", FMT_IU( aPad->GetPos0() ).c_str() );
  988. if( aPad->GetOrientation() != 0.0 )
  989. m_out->Print( 0, " %s", FMT_ANGLE( aPad->GetOrientation() ).c_str() );
  990. m_out->Print( 0, ")" );
  991. m_out->Print( 0, " (size %s)", FMT_IU( aPad->GetSize() ).c_str() );
  992. if( (aPad->GetDelta().GetWidth()) != 0 || (aPad->GetDelta().GetHeight() != 0 ) )
  993. m_out->Print( 0, " (rect_delta %s )", FMT_IU( aPad->GetDelta() ).c_str() );
  994. wxSize sz = aPad->GetDrillSize();
  995. wxPoint shapeoffset = aPad->GetOffset();
  996. if( (sz.GetWidth() > 0) || (sz.GetHeight() > 0) ||
  997. (shapeoffset.x != 0) || (shapeoffset.y != 0) )
  998. {
  999. m_out->Print( 0, " (drill" );
  1000. if( aPad->GetDrillShape() == PAD_DRILL_SHAPE_OBLONG )
  1001. m_out->Print( 0, " oval" );
  1002. if( sz.GetWidth() > 0 )
  1003. m_out->Print( 0, " %s", FMT_IU( sz.GetWidth() ).c_str() );
  1004. if( sz.GetHeight() > 0 && sz.GetWidth() != sz.GetHeight() )
  1005. m_out->Print( 0, " %s", FMT_IU( sz.GetHeight() ).c_str() );
  1006. if( (shapeoffset.x != 0) || (shapeoffset.y != 0) )
  1007. m_out->Print( 0, " (offset %s)", FMT_IU( aPad->GetOffset() ).c_str() );
  1008. m_out->Print( 0, ")" );
  1009. }
  1010. formatLayers( aPad->GetLayerSet(), 0 );
  1011. // Output the radius ratio for rounded rect pads
  1012. if( aPad->GetShape() == PAD_SHAPE_ROUNDRECT )
  1013. {
  1014. m_out->Print( 0, "(roundrect_rratio %s)",
  1015. Double2Str( aPad->GetRoundRectRadiusRatio() ).c_str() );
  1016. }
  1017. std::string output;
  1018. // Unconnected pad is default net so don't save it.
  1019. if( !( m_ctl & CTL_OMIT_NETS ) && aPad->GetNetCode() != NETINFO_LIST::UNCONNECTED )
  1020. StrPrintf( &output, " (net %d %s)", m_mapping->Translate( aPad->GetNetCode() ),
  1021. m_out->Quotew( aPad->GetNetname() ).c_str() );
  1022. if( aPad->GetPadToDieLength() != 0 )
  1023. StrPrintf( &output, " (die_length %s)", FMT_IU( aPad->GetPadToDieLength() ).c_str() );
  1024. if( aPad->GetLocalSolderMaskMargin() != 0 )
  1025. StrPrintf( &output, " (solder_mask_margin %s)", FMT_IU( aPad->GetLocalSolderMaskMargin() ).c_str() );
  1026. if( aPad->GetLocalSolderPasteMargin() != 0 )
  1027. StrPrintf( &output, " (solder_paste_margin %s)", FMT_IU( aPad->GetLocalSolderPasteMargin() ).c_str() );
  1028. if( aPad->GetLocalSolderPasteMarginRatio() != 0 )
  1029. StrPrintf( &output, " (solder_paste_margin_ratio %s)",
  1030. Double2Str( aPad->GetLocalSolderPasteMarginRatio() ).c_str() );
  1031. if( aPad->GetLocalClearance() != 0 )
  1032. StrPrintf( &output, " (clearance %s)", FMT_IU( aPad->GetLocalClearance() ).c_str() );
  1033. if( aPad->GetZoneConnection() != PAD_ZONE_CONN_INHERITED )
  1034. StrPrintf( &output, " (zone_connect %d)", aPad->GetZoneConnection() );
  1035. if( aPad->GetThermalWidth() != 0 )
  1036. StrPrintf( &output, " (thermal_width %s)", FMT_IU( aPad->GetThermalWidth() ).c_str() );
  1037. if( aPad->GetThermalGap() != 0 )
  1038. StrPrintf( &output, " (thermal_gap %s)", FMT_IU( aPad->GetThermalGap() ).c_str() );
  1039. if( output.size() )
  1040. {
  1041. m_out->Print( 0, "\n" );
  1042. m_out->Print( aNestLevel+1, "%s", output.c_str()+1 ); // +1 skips 1st space on 1st element
  1043. }
  1044. m_out->Print( 0, ")\n" );
  1045. }
  1046. void PCB_IO::format( TEXTE_PCB* aText, int aNestLevel ) const
  1047. throw( IO_ERROR )
  1048. {
  1049. m_out->Print( aNestLevel, "(gr_text %s (at %s",
  1050. m_out->Quotew( aText->GetText() ).c_str(),
  1051. FMT_IU( aText->GetTextPosition() ).c_str() );
  1052. if( aText->GetOrientation() != 0.0 )
  1053. m_out->Print( 0, " %s", FMT_ANGLE( aText->GetOrientation() ).c_str() );
  1054. m_out->Print( 0, ")" );
  1055. formatLayer( aText );
  1056. if( aText->GetTimeStamp() )
  1057. m_out->Print( 0, " (tstamp %lX)", aText->GetTimeStamp() );
  1058. m_out->Print( 0, "\n" );
  1059. aText->EDA_TEXT::Format( m_out, aNestLevel, m_ctl );
  1060. m_out->Print( aNestLevel, ")\n" );
  1061. }
  1062. void PCB_IO::format( TEXTE_MODULE* aText, int aNestLevel ) const
  1063. throw( IO_ERROR )
  1064. {
  1065. MODULE* parent = (MODULE*) aText->GetParent();
  1066. double orient = aText->GetOrientation();
  1067. wxString type;
  1068. switch( aText->GetType() )
  1069. {
  1070. case TEXTE_MODULE::TEXT_is_REFERENCE: type = wxT( "reference" ); break;
  1071. case TEXTE_MODULE::TEXT_is_VALUE: type = wxT( "value" ); break;
  1072. case TEXTE_MODULE::TEXT_is_DIVERS: type = wxT( "user" );
  1073. }
  1074. // Due to the Pcbnew history, m_Orient is saved in screen value
  1075. // but it is handled as relative to its parent footprint
  1076. if( parent )
  1077. orient += parent->GetOrientation();
  1078. m_out->Print( aNestLevel, "(fp_text %s %s (at %s",
  1079. m_out->Quotew( type ).c_str(),
  1080. m_out->Quotew( aText->GetText() ).c_str(),
  1081. FMT_IU( aText->GetPos0() ).c_str() );
  1082. if( orient != 0.0 )
  1083. m_out->Print( 0, " %s", FMT_ANGLE( orient ).c_str() );
  1084. m_out->Print( 0, ")" );
  1085. formatLayer( aText );
  1086. if( !aText->IsVisible() )
  1087. m_out->Print( 0, " hide" );
  1088. m_out->Print( 0, "\n" );
  1089. aText->EDA_TEXT::Format( m_out, aNestLevel, m_ctl );
  1090. m_out->Print( aNestLevel, ")\n" );
  1091. }
  1092. void PCB_IO::format( TRACK* aTrack, int aNestLevel ) const
  1093. throw( IO_ERROR )
  1094. {
  1095. if( aTrack->Type() == PCB_VIA_T )
  1096. {
  1097. LAYER_ID layer1, layer2;
  1098. const VIA* via = static_cast<const VIA*>(aTrack);
  1099. BOARD* board = (BOARD*) via->GetParent();
  1100. wxCHECK_RET( board != 0, wxT( "Via " ) + via->GetSelectMenuText() +
  1101. wxT( " has no parent." ) );
  1102. m_out->Print( aNestLevel, "(via" );
  1103. via->LayerPair( &layer1, &layer2 );
  1104. switch( via->GetViaType() )
  1105. {
  1106. case VIA_THROUGH: // Default shape not saved.
  1107. break;
  1108. case VIA_BLIND_BURIED:
  1109. m_out->Print( 0, " blind" );
  1110. break;
  1111. case VIA_MICROVIA:
  1112. m_out->Print( 0, " micro" );
  1113. break;
  1114. default:
  1115. THROW_IO_ERROR( wxString::Format( _( "unknown via type %d" ), via->GetViaType() ) );
  1116. }
  1117. m_out->Print( 0, " (at %s) (size %s)",
  1118. FMT_IU( aTrack->GetStart() ).c_str(),
  1119. FMT_IU( aTrack->GetWidth() ).c_str() );
  1120. if( via->GetDrill() != UNDEFINED_DRILL_DIAMETER )
  1121. m_out->Print( 0, " (drill %s)", FMT_IU( via->GetDrill() ).c_str() );
  1122. m_out->Print( 0, " (layers %s %s)",
  1123. m_out->Quotew( m_board->GetLayerName( layer1 ) ).c_str(),
  1124. m_out->Quotew( m_board->GetLayerName( layer2 ) ).c_str() );
  1125. }
  1126. else
  1127. {
  1128. m_out->Print( aNestLevel, "(segment (start %s) (end %s) (width %s)",
  1129. FMT_IU( aTrack->GetStart() ).c_str(), FMT_IU( aTrack->GetEnd() ).c_str(),
  1130. FMT_IU( aTrack->GetWidth() ).c_str() );
  1131. m_out->Print( 0, " (layer %s)", m_out->Quotew( aTrack->GetLayerName() ).c_str() );
  1132. }
  1133. m_out->Print( 0, " (net %d)", m_mapping->Translate( aTrack->GetNetCode() ) );
  1134. if( aTrack->GetTimeStamp() != 0 )
  1135. m_out->Print( 0, " (tstamp %lX)", aTrack->GetTimeStamp() );
  1136. if( aTrack->GetStatus() != 0 )
  1137. m_out->Print( 0, " (status %X)", aTrack->GetStatus() );
  1138. m_out->Print( 0, ")\n" );
  1139. }
  1140. void PCB_IO::format( ZONE_CONTAINER* aZone, int aNestLevel ) const
  1141. throw( IO_ERROR )
  1142. {
  1143. // Save the NET info; For keepout zones, net code and net name are irrelevant
  1144. // so be sure a dummy value is stored, just for ZONE_CONTAINER compatibility
  1145. // (perhaps netcode and netname should be not stored)
  1146. m_out->Print( aNestLevel, "(zone (net %d) (net_name %s)",
  1147. aZone->GetIsKeepout() ? 0 : m_mapping->Translate( aZone->GetNetCode() ),
  1148. m_out->Quotew( aZone->GetIsKeepout() ? wxT("") : aZone->GetNetname() ).c_str() );
  1149. formatLayer( aZone );
  1150. m_out->Print( 0, " (tstamp %lX)", aZone->GetTimeStamp() );
  1151. // Save the outline aux info
  1152. std::string hatch;
  1153. switch( aZone->GetHatchStyle() )
  1154. {
  1155. default:
  1156. case CPolyLine::NO_HATCH: hatch = "none"; break;
  1157. case CPolyLine::DIAGONAL_EDGE: hatch = "edge"; break;
  1158. case CPolyLine::DIAGONAL_FULL: hatch = "full"; break;
  1159. }
  1160. m_out->Print( 0, " (hatch %s %s)\n", hatch.c_str(),
  1161. FMT_IU( aZone->Outline()->GetHatchPitch() ).c_str() );
  1162. if( aZone->GetPriority() > 0 )
  1163. m_out->Print( aNestLevel+1, "(priority %d)\n", aZone->GetPriority() );
  1164. m_out->Print( aNestLevel+1, "(connect_pads" );
  1165. switch( aZone->GetPadConnection() )
  1166. {
  1167. default:
  1168. case PAD_ZONE_CONN_THERMAL: // Default option not saved or loaded.
  1169. break;
  1170. case PAD_ZONE_CONN_THT_THERMAL:
  1171. m_out->Print( 0, " thru_hole_only" );
  1172. break;
  1173. case PAD_ZONE_CONN_FULL:
  1174. m_out->Print( 0, " yes" );
  1175. break;
  1176. case PAD_ZONE_CONN_NONE:
  1177. m_out->Print( 0, " no" );
  1178. break;
  1179. }
  1180. m_out->Print( 0, " (clearance %s))\n",
  1181. FMT_IU( aZone->GetZoneClearance() ).c_str() );
  1182. m_out->Print( aNestLevel+1, "(min_thickness %s)\n",
  1183. FMT_IU( aZone->GetMinThickness() ).c_str() );
  1184. if( aZone->GetIsKeepout() )
  1185. {
  1186. m_out->Print( aNestLevel+1, "(keepout (tracks %s) (vias %s) (copperpour %s))\n",
  1187. aZone->GetDoNotAllowTracks() ? "not_allowed" : "allowed",
  1188. aZone->GetDoNotAllowVias() ? "not_allowed" : "allowed",
  1189. aZone->GetDoNotAllowCopperPour() ? "not_allowed" : "allowed" );
  1190. }
  1191. m_out->Print( aNestLevel+1, "(fill" );
  1192. // Default is not filled.
  1193. if( aZone->IsFilled() )
  1194. m_out->Print( 0, " yes" );
  1195. // Default is polygon filled.
  1196. if( aZone->GetFillMode() )
  1197. m_out->Print( 0, " (mode segment)" );
  1198. m_out->Print( 0, " (arc_segments %d) (thermal_gap %s) (thermal_bridge_width %s)",
  1199. aZone->GetArcSegmentCount(),
  1200. FMT_IU( aZone->GetThermalReliefGap() ).c_str(),
  1201. FMT_IU( aZone->GetThermalReliefCopperBridge() ).c_str() );
  1202. if( aZone->GetCornerSmoothingType() != ZONE_SETTINGS::SMOOTHING_NONE )
  1203. {
  1204. m_out->Print( 0, " (smoothing" );
  1205. switch( aZone->GetCornerSmoothingType() )
  1206. {
  1207. case ZONE_SETTINGS::SMOOTHING_CHAMFER:
  1208. m_out->Print( 0, " chamfer" );
  1209. break;
  1210. case ZONE_SETTINGS::SMOOTHING_FILLET:
  1211. m_out->Print( 0, " fillet" );
  1212. break;
  1213. default:
  1214. THROW_IO_ERROR( wxString::Format( _( "unknown zone corner smoothing type %d" ),
  1215. aZone->GetCornerSmoothingType() ) );
  1216. }
  1217. m_out->Print( 0, ")" );
  1218. if( aZone->GetCornerRadius() != 0 )
  1219. m_out->Print( 0, " (radius %s)",
  1220. FMT_IU( aZone->GetCornerRadius() ).c_str() );
  1221. }
  1222. m_out->Print( 0, ")\n" );
  1223. const CPOLYGONS_LIST& cv = aZone->Outline()->m_CornersList;
  1224. int newLine = 0;
  1225. if( cv.GetCornersCount() )
  1226. {
  1227. m_out->Print( aNestLevel+1, "(polygon\n");
  1228. m_out->Print( aNestLevel+2, "(pts\n" );
  1229. for( unsigned it = 0; it < cv.GetCornersCount(); ++it )
  1230. {
  1231. if( newLine == 0 )
  1232. m_out->Print( aNestLevel+3, "(xy %s %s)",
  1233. FMT_IU( cv.GetX( it ) ).c_str(), FMT_IU( cv.GetY( it ) ).c_str() );
  1234. else
  1235. m_out->Print( 0, " (xy %s %s)",
  1236. FMT_IU( cv.GetX( it ) ).c_str(), FMT_IU( cv.GetY( it ) ).c_str() );
  1237. if( newLine < 4 )
  1238. {
  1239. newLine += 1;
  1240. }
  1241. else
  1242. {
  1243. newLine = 0;
  1244. m_out->Print( 0, "\n" );
  1245. }
  1246. if( cv.IsEndContour( it ) )
  1247. {
  1248. if( newLine != 0 )
  1249. m_out->Print( 0, "\n" );
  1250. m_out->Print( aNestLevel+2, ")\n" );
  1251. if( it+1 != cv.GetCornersCount() )
  1252. {
  1253. newLine = 0;
  1254. m_out->Print( aNestLevel+1, ")\n" );
  1255. m_out->Print( aNestLevel+1, "(polygon\n" );
  1256. m_out->Print( aNestLevel+2, "(pts" );
  1257. }
  1258. }
  1259. }
  1260. m_out->Print( aNestLevel+1, ")\n" );
  1261. }
  1262. // Save the PolysList
  1263. const SHAPE_POLY_SET& fv = aZone->GetFilledPolysList();
  1264. newLine = 0;
  1265. if( !fv.IsEmpty() )
  1266. {
  1267. m_out->Print( aNestLevel+1, "(filled_polygon\n" );
  1268. m_out->Print( aNestLevel+2, "(pts\n" );
  1269. for( SHAPE_POLY_SET::CONST_ITERATOR it = fv.CIterate(); it; ++it )
  1270. {
  1271. if( newLine == 0 )
  1272. m_out->Print( aNestLevel+3, "(xy %s %s)",
  1273. FMT_IU( it->x ).c_str(), FMT_IU( it->y ).c_str() );
  1274. else
  1275. m_out->Print( 0, " (xy %s %s)",
  1276. FMT_IU( it->x ) .c_str(), FMT_IU( it->y ).c_str() );
  1277. if( newLine < 4 )
  1278. {
  1279. newLine += 1;
  1280. }
  1281. else
  1282. {
  1283. newLine = 0;
  1284. m_out->Print( 0, "\n" );
  1285. }
  1286. if( it.IsEndContour() )
  1287. {
  1288. if( newLine != 0 )
  1289. m_out->Print( 0, "\n" );
  1290. m_out->Print( aNestLevel+2, ")\n" );
  1291. if( !it.IsLastContour() )
  1292. {
  1293. newLine = 0;
  1294. m_out->Print( aNestLevel+1, ")\n" );
  1295. m_out->Print( aNestLevel+1, "(filled_polygon\n" );
  1296. m_out->Print( aNestLevel+2, "(pts\n" );
  1297. }
  1298. }
  1299. }
  1300. m_out->Print( aNestLevel+1, ")\n" );
  1301. }
  1302. // Save the filling segments list
  1303. const std::vector< SEGMENT >& segs = aZone->FillSegments();
  1304. if( segs.size() )
  1305. {
  1306. m_out->Print( aNestLevel+1, "(fill_segments\n" );
  1307. for( std::vector< SEGMENT >::const_iterator it = segs.begin(); it != segs.end(); ++it )
  1308. {
  1309. m_out->Print( aNestLevel+2, "(pts (xy %s) (xy %s))\n",
  1310. FMT_IU( it->m_Start ).c_str(),
  1311. FMT_IU( it->m_End ).c_str() );
  1312. }
  1313. m_out->Print( aNestLevel+1, ")\n" );
  1314. }
  1315. m_out->Print( aNestLevel, ")\n" );
  1316. }
  1317. PCB_IO::PCB_IO( int aControlFlags ) :
  1318. m_cache( 0 ),
  1319. m_ctl( aControlFlags ),
  1320. m_parser( new PCB_PARSER() ),
  1321. m_mapping( new NETINFO_MAPPING() )
  1322. {
  1323. init( 0 );
  1324. m_out = &m_sf;
  1325. }
  1326. PCB_IO::~PCB_IO()
  1327. {
  1328. delete m_cache;
  1329. delete m_parser;
  1330. delete m_mapping;
  1331. }
  1332. BOARD* PCB_IO::Load( const wxString& aFileName, BOARD* aAppendToMe, const PROPERTIES* aProperties )
  1333. {
  1334. FILE_LINE_READER reader( aFileName );
  1335. init( aProperties );
  1336. m_parser->SetLineReader( &reader );
  1337. m_parser->SetBoard( aAppendToMe );
  1338. BOARD* board;
  1339. try
  1340. {
  1341. board = dynamic_cast<BOARD*>( m_parser->Parse() );
  1342. }
  1343. catch( const FUTURE_FORMAT_ERROR& parse_error )
  1344. {
  1345. // Don't wrap a FUTURE_FORMAT_ERROR in another
  1346. throw;
  1347. }
  1348. catch( const PARSE_ERROR& parse_error )
  1349. {
  1350. if( m_parser->IsTooRecent() )
  1351. throw FUTURE_FORMAT_ERROR( parse_error, m_parser->GetRequiredVersion() );
  1352. else
  1353. throw;
  1354. }
  1355. if( !board )
  1356. {
  1357. // The parser loaded something that was valid, but wasn't a board.
  1358. THROW_PARSE_ERROR( _( "this file does not contain a PCB" ),
  1359. m_parser->CurSource(), m_parser->CurLine(),
  1360. m_parser->CurLineNumber(), m_parser->CurOffset() );
  1361. }
  1362. // Give the filename to the board if it's new
  1363. if( !aAppendToMe )
  1364. board->SetFileName( aFileName );
  1365. return board;
  1366. }
  1367. void PCB_IO::init( const PROPERTIES* aProperties )
  1368. {
  1369. m_board = NULL;
  1370. m_reader = NULL;
  1371. m_loading_format_version = SEXPR_BOARD_FILE_VERSION;
  1372. m_props = aProperties;
  1373. }
  1374. void PCB_IO::cacheLib( const wxString& aLibraryPath, const wxString& aFootprintName )
  1375. {
  1376. if( !m_cache || m_cache->IsModified( aLibraryPath, aFootprintName ) )
  1377. {
  1378. // a spectacular episode in memory management:
  1379. delete m_cache;
  1380. m_cache = new FP_CACHE( this, aLibraryPath );
  1381. m_cache->Load();
  1382. }
  1383. }
  1384. wxArrayString PCB_IO::FootprintEnumerate( const wxString& aLibraryPath,
  1385. const PROPERTIES* aProperties )
  1386. {
  1387. LOCALE_IO toggle; // toggles on, then off, the C locale.
  1388. wxArrayString ret;
  1389. wxDir dir( aLibraryPath );
  1390. if( !dir.IsOpened() )
  1391. {
  1392. THROW_IO_ERROR( wxString::Format( _( "footprint library path '%s' does not exist" ),
  1393. GetChars( aLibraryPath ) ) );
  1394. }
  1395. init( aProperties );
  1396. #if 1 // Set to 0 to only read directory contents, not load cache.
  1397. cacheLib( aLibraryPath );
  1398. const MODULE_MAP& mods = m_cache->GetModules();
  1399. for( MODULE_CITER it = mods.begin(); it != mods.end(); ++it )
  1400. {
  1401. ret.Add( FROM_UTF8( it->first.c_str() ) );
  1402. }
  1403. #else
  1404. wxString fpFileName;
  1405. wxString wildcard = wxT( "*." ) + KiCadFootprintFileExtension;
  1406. if( dir.GetFirst( &fpFileName, wildcard, wxDIR_FILES ) )
  1407. {
  1408. do
  1409. {
  1410. wxFileName fn( aLibraryPath, fpFileName );
  1411. ret.Add( fn.GetName() );
  1412. } while( dir.GetNext( &fpFileName ) );
  1413. }
  1414. #endif
  1415. return ret;
  1416. }
  1417. MODULE* PCB_IO::FootprintLoad( const wxString& aLibraryPath, const wxString& aFootprintName,
  1418. const PROPERTIES* aProperties )
  1419. {
  1420. LOCALE_IO toggle; // toggles on, then off, the C locale.
  1421. init( aProperties );
  1422. cacheLib( aLibraryPath, aFootprintName );
  1423. const MODULE_MAP& mods = m_cache->GetModules();
  1424. MODULE_CITER it = mods.find( TO_UTF8( aFootprintName ) );
  1425. if( it == mods.end() )
  1426. {
  1427. return NULL;
  1428. }
  1429. // copy constructor to clone the already loaded MODULE
  1430. return new MODULE( *it->second->GetModule() );
  1431. }
  1432. void PCB_IO::FootprintSave( const wxString& aLibraryPath, const MODULE* aFootprint,
  1433. const PROPERTIES* aProperties )
  1434. {
  1435. LOCALE_IO toggle; // toggles on, then off, the C locale.
  1436. init( aProperties );
  1437. // In this public PLUGIN API function, we can safely assume it was
  1438. // called for saving into a library path.
  1439. m_ctl = CTL_FOR_LIBRARY;
  1440. cacheLib( aLibraryPath );
  1441. if( !m_cache->IsWritable() )
  1442. {
  1443. wxString msg = wxString::Format(
  1444. _( "Library '%s' is read only" ),
  1445. GetChars( aLibraryPath )
  1446. );
  1447. THROW_IO_ERROR( msg );
  1448. }
  1449. std::string footprintName = aFootprint->GetFPID().GetFootprintName();
  1450. MODULE_MAP& mods = m_cache->GetModules();
  1451. // Quietly overwrite module and delete module file from path for any by same name.
  1452. wxFileName fn( aLibraryPath, aFootprint->GetFPID().GetFootprintName(), KiCadFootprintFileExtension );
  1453. if( !fn.IsOk() )
  1454. {
  1455. THROW_IO_ERROR( wxString::Format( _( "Footprint file name '%s' is not valid." ),
  1456. GetChars( fn.GetFullPath() ) ) );
  1457. }
  1458. if( fn.FileExists() && !fn.IsFileWritable() )
  1459. {
  1460. THROW_IO_ERROR( wxString::Format( _( "user does not have write permission to delete file '%s' " ),
  1461. GetChars( fn.GetFullPath() ) ) );
  1462. }
  1463. MODULE_CITER it = mods.find( footprintName );
  1464. if( it != mods.end() )
  1465. {
  1466. wxLogTrace( traceFootprintLibrary, wxT( "Removing footprint library file '%s'." ),
  1467. fn.GetFullPath().GetData() );
  1468. mods.erase( footprintName );
  1469. wxRemoveFile( fn.GetFullPath() );
  1470. }
  1471. // I need my own copy for the cache
  1472. MODULE* module = new MODULE( *aFootprint );
  1473. // and it's time stamp must be 0, it should have no parent, orientation should
  1474. // be zero, and it should be on the front layer.
  1475. module->SetTimeStamp( 0 );
  1476. module->SetParent( 0 );
  1477. module->SetOrientation( 0 );
  1478. if( module->GetLayer() != F_Cu )
  1479. module->Flip( module->GetPosition() );
  1480. wxLogTrace( traceFootprintLibrary, wxT( "Creating s-expression footprint file: %s." ),
  1481. fn.GetFullPath().GetData() );
  1482. mods.insert( footprintName, new FP_CACHE_ITEM( module, fn ) );
  1483. m_cache->Save();
  1484. }
  1485. void PCB_IO::FootprintDelete( const wxString& aLibraryPath, const wxString& aFootprintName, const PROPERTIES* aProperties )
  1486. {
  1487. LOCALE_IO toggle; // toggles on, then off, the C locale.
  1488. init( aProperties );
  1489. cacheLib( aLibraryPath );
  1490. if( !m_cache->IsWritable() )
  1491. {
  1492. THROW_IO_ERROR( wxString::Format( _( "Library '%s' is read only" ),
  1493. aLibraryPath.GetData() ) );
  1494. }
  1495. m_cache->Remove( aFootprintName );
  1496. }
  1497. void PCB_IO::FootprintLibCreate( const wxString& aLibraryPath, const PROPERTIES* aProperties )
  1498. {
  1499. if( wxDir::Exists( aLibraryPath ) )
  1500. {
  1501. THROW_IO_ERROR( wxString::Format( _( "cannot overwrite library path '%s'" ),
  1502. aLibraryPath.GetData() ) );
  1503. }
  1504. LOCALE_IO toggle;
  1505. init( aProperties );
  1506. delete m_cache;
  1507. m_cache = new FP_CACHE( this, aLibraryPath );
  1508. m_cache->Save();
  1509. }
  1510. bool PCB_IO::FootprintLibDelete( const wxString& aLibraryPath, const PROPERTIES* aProperties )
  1511. {
  1512. wxFileName fn;
  1513. fn.SetPath( aLibraryPath );
  1514. // Return if there is no library path to delete.
  1515. if( !fn.DirExists() )
  1516. return false;
  1517. if( !fn.IsDirWritable() )
  1518. {
  1519. THROW_IO_ERROR( wxString::Format( _( "user does not have permission to delete directory '%s'" ),
  1520. aLibraryPath.GetData() ) );
  1521. }
  1522. wxDir dir( aLibraryPath );
  1523. if( dir.HasSubDirs() )
  1524. {
  1525. THROW_IO_ERROR( wxString::Format( _( "library directory '%s' has unexpected sub-directories" ),
  1526. aLibraryPath.GetData() ) );
  1527. }
  1528. // All the footprint files must be deleted before the directory can be deleted.
  1529. if( dir.HasFiles() )
  1530. {
  1531. unsigned i;
  1532. wxFileName tmp;
  1533. wxArrayString files;
  1534. wxDir::GetAllFiles( aLibraryPath, &files );
  1535. for( i = 0; i < files.GetCount(); i++ )
  1536. {
  1537. tmp = files[i];
  1538. if( tmp.GetExt() != KiCadFootprintFileExtension )
  1539. {
  1540. THROW_IO_ERROR( wxString::Format( _( "unexpected file '%s' was found in library path '%s'" ),
  1541. files[i].GetData(), aLibraryPath.GetData() ) );
  1542. }
  1543. }
  1544. for( i = 0; i < files.GetCount(); i++ )
  1545. {
  1546. wxRemoveFile( files[i] );
  1547. }
  1548. }
  1549. wxLogTrace( traceFootprintLibrary, wxT( "Removing footprint library '%s'" ),
  1550. aLibraryPath.GetData() );
  1551. // Some of the more elaborate wxRemoveFile() crap puts up its own wxLog dialog
  1552. // we don't want that. we want bare metal portability with no UI here.
  1553. if( !wxRmdir( aLibraryPath ) )
  1554. {
  1555. THROW_IO_ERROR( wxString::Format( _( "footprint library '%s' cannot be deleted" ),
  1556. aLibraryPath.GetData() ) );
  1557. }
  1558. // For some reason removing a directory in Windows is not immediately updated. This delay
  1559. // prevents an error when attempting to immediately recreate the same directory when over
  1560. // writing an existing library.
  1561. #ifdef __WINDOWS__
  1562. wxMilliSleep( 250L );
  1563. #endif
  1564. if( m_cache && !m_cache->IsPath( aLibraryPath ) )
  1565. {
  1566. delete m_cache;
  1567. m_cache = NULL;
  1568. }
  1569. return true;
  1570. }
  1571. bool PCB_IO::IsFootprintLibWritable( const wxString& aLibraryPath )
  1572. {
  1573. LOCALE_IO toggle;
  1574. init( NULL );
  1575. cacheLib( aLibraryPath );
  1576. return m_cache->IsWritable();
  1577. }