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.

2264 lines
73 KiB

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
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
13 years ago
13 years ago
13 years ago
13 years ago
13 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-2018 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 <trace_helpers.h>
  32. #include <class_board.h>
  33. #include <class_module.h>
  34. #include <class_pcb_text.h>
  35. #include <class_dimension.h>
  36. #include <class_track.h>
  37. #include <class_zone.h>
  38. #include <class_drawsegment.h>
  39. #include <class_pcb_target.h>
  40. #include <class_edge_mod.h>
  41. #include <pcb_plot_params.h>
  42. #include <zones.h>
  43. #include <kicad_plugin.h>
  44. #include <pcb_parser.h>
  45. #include <wx/dir.h>
  46. #include <wx/filename.h>
  47. #include <wx/wfstream.h>
  48. #include <boost/ptr_container/ptr_map.hpp>
  49. #include <memory.h>
  50. #include <connectivity_data.h>
  51. using namespace PCB_KEYS_T;
  52. #define FMT_IU BOARD_ITEM::FormatInternalUnits
  53. #define FMT_ANGLE BOARD_ITEM::FormatAngle
  54. ///> Removes empty nets (i.e. with node count equal zero) from net classes
  55. void filterNetClass( const BOARD& aBoard, NETCLASS& aNetClass )
  56. {
  57. auto connectivity = aBoard.GetConnectivity();
  58. for( NETCLASS::iterator it = aNetClass.begin(); it != aNetClass.end(); )
  59. {
  60. NETINFO_ITEM* netinfo = aBoard.FindNet( *it );
  61. if( netinfo && connectivity->GetNodeCount( netinfo->GetNet() ) <= 0 ) // hopefully there are no nets with negative
  62. aNetClass.Remove( it++ ); // node count, but you never know..
  63. else
  64. ++it;
  65. }
  66. }
  67. /**
  68. * Class FP_CACHE_ITEM
  69. * is helper class for creating a footprint library cache.
  70. *
  71. * The new footprint library design is a file path of individual module files
  72. * that contain a single module per file. This class is a helper only for the
  73. * footprint portion of the PLUGIN API, and only for the #PCB_IO plugin. It is
  74. * private to this implementation file so it is not placed into a header.
  75. */
  76. class FP_CACHE_ITEM
  77. {
  78. wxFileName m_file_name; ///< The the full file name and path of the footprint to cache.
  79. std::unique_ptr<MODULE> m_module;
  80. public:
  81. FP_CACHE_ITEM( MODULE* aModule, const wxFileName& aFileName );
  82. wxString GetName() const { return m_file_name.GetDirs().Last(); }
  83. wxFileName GetFileName() const { return m_file_name; }
  84. MODULE* GetModule() const { return m_module.get(); }
  85. };
  86. FP_CACHE_ITEM::FP_CACHE_ITEM( MODULE* aModule, const wxFileName& aFileName ) :
  87. m_module( aModule )
  88. {
  89. m_file_name = aFileName;
  90. }
  91. typedef boost::ptr_map< wxString, FP_CACHE_ITEM > MODULE_MAP;
  92. typedef MODULE_MAP::iterator MODULE_ITER;
  93. typedef MODULE_MAP::const_iterator MODULE_CITER;
  94. class FP_CACHE
  95. {
  96. PCB_IO* m_owner; /// Plugin object that owns the cache.
  97. wxFileName m_lib_path; /// The path of the library.
  98. MODULE_MAP m_modules; /// Map of footprint file name per MODULE*.
  99. bool m_cache_dirty; // Stored separately because it's expensive to check
  100. // m_cache_timestamp against all the files.
  101. long long m_cache_timestamp; // A hash of the timestamps for all the footprint
  102. // files.
  103. public:
  104. FP_CACHE( PCB_IO* aOwner, const wxString& aLibraryPath );
  105. wxString GetPath() const { return m_lib_path.GetPath(); }
  106. bool IsWritable() const { return m_lib_path.IsOk() && m_lib_path.IsDirWritable(); }
  107. MODULE_MAP& GetModules() { return m_modules; }
  108. // Most all functions in this class throw IO_ERROR exceptions. There are no
  109. // error codes nor user interface calls from here, nor in any PLUGIN.
  110. // Catch these exceptions higher up please.
  111. /**
  112. * Function Save
  113. * Save the footprint cache or a single module from it to disk
  114. *
  115. * @param aModule if set, save only this module, otherwise, save the full library
  116. */
  117. void Save( MODULE* aModule = NULL );
  118. void Load();
  119. void Remove( const wxString& aFootprintName );
  120. /**
  121. * Function GetTimestamp
  122. * Generate a timestamp representing all source files in the cache (including the
  123. * parent directory).
  124. * Timestamps should not be considered ordered. They either match or they don't.
  125. */
  126. long long GetTimestamp();
  127. /**
  128. * Function IsModified
  129. * Return true if the cache is not up-to-date.
  130. */
  131. bool IsModified();
  132. /**
  133. * Function IsPath
  134. * checks if \a aPath is the same as the current cache path.
  135. *
  136. * This tests paths by converting \a aPath using the native separators. Internally
  137. * #FP_CACHE stores the current path using native separators. This prevents path
  138. * miscompares on Windows due to the fact that paths can be stored with / instead of \\
  139. * in the footprint library table.
  140. *
  141. * @param aPath is the library path to test against.
  142. * @return true if \a aPath is the same as the cache path.
  143. */
  144. bool IsPath( const wxString& aPath ) const;
  145. };
  146. FP_CACHE::FP_CACHE( PCB_IO* aOwner, const wxString& aLibraryPath )
  147. {
  148. m_owner = aOwner;
  149. m_lib_path.SetPath( aLibraryPath );
  150. m_cache_timestamp = 0;
  151. m_cache_dirty = true;
  152. }
  153. void FP_CACHE::Save( MODULE* aModule )
  154. {
  155. m_cache_timestamp = 0;
  156. if( !m_lib_path.DirExists() && !m_lib_path.Mkdir() )
  157. {
  158. THROW_IO_ERROR( wxString::Format( _( "Cannot create footprint library path \"%s\"" ),
  159. m_lib_path.GetPath().GetData() ) );
  160. }
  161. if( !m_lib_path.IsDirWritable() )
  162. {
  163. THROW_IO_ERROR( wxString::Format( _( "Footprint library path \"%s\" is read only" ),
  164. GetChars( m_lib_path.GetPath() ) ) );
  165. }
  166. for( MODULE_ITER it = m_modules.begin(); it != m_modules.end(); ++it )
  167. {
  168. if( aModule && aModule != it->second->GetModule() )
  169. continue;
  170. wxFileName fn = it->second->GetFileName();
  171. wxString tempFileName =
  172. #ifdef USE_TMP_FILE
  173. fn.CreateTempFileName( fn.GetPath() );
  174. #else
  175. fn.GetFullPath();
  176. #endif
  177. // Allow file output stream to go out of scope to close the file stream before
  178. // renaming the file.
  179. {
  180. wxLogTrace( traceKicadPcbPlugin, wxT( "Creating temporary library file %s" ),
  181. GetChars( tempFileName ) );
  182. FILE_OUTPUTFORMATTER formatter( tempFileName );
  183. m_owner->SetOutputFormatter( &formatter );
  184. m_owner->Format( (BOARD_ITEM*) it->second->GetModule() );
  185. }
  186. #ifdef USE_TMP_FILE
  187. wxRemove( fn.GetFullPath() ); // it is not an error if this does not exist
  188. // Even on linux you can see an _intermittent_ error when calling wxRename(),
  189. // and it is fully inexplicable. See if this dodges the error.
  190. wxMilliSleep( 250L );
  191. if( !wxRenameFile( tempFileName, fn.GetFullPath() ) )
  192. {
  193. wxString msg = wxString::Format(
  194. _( "Cannot rename temporary file \"%s\" to footprint library file \"%s\"" ),
  195. GetChars( tempFileName ),
  196. GetChars( fn.GetFullPath() )
  197. );
  198. THROW_IO_ERROR( msg );
  199. }
  200. #endif
  201. m_cache_timestamp += fn.GetModificationTime().GetValue().GetValue();
  202. }
  203. m_cache_timestamp += m_lib_path.GetModificationTime().GetValue().GetValue();
  204. // If we've saved the full cache, we clear the dirty flag.
  205. if( !aModule )
  206. m_cache_dirty = false;
  207. }
  208. void FP_CACHE::Load()
  209. {
  210. wxDir dir( m_lib_path.GetPath() );
  211. if( !dir.IsOpened() )
  212. {
  213. m_cache_timestamp = 0;
  214. m_cache_dirty = false;
  215. wxString msg = wxString::Format(
  216. _( "Footprint library path \"%s\" does not exist" ),
  217. GetChars( m_lib_path.GetPath() )
  218. );
  219. THROW_IO_ERROR( msg );
  220. }
  221. else
  222. {
  223. m_cache_timestamp = m_lib_path.GetModificationTime().GetValue().GetValue();
  224. m_cache_dirty = false;
  225. }
  226. wxString fpFileName;
  227. wxString wildcard = wxT( "*." ) + KiCadFootprintFileExtension;
  228. if( dir.GetFirst( &fpFileName, wildcard, wxDIR_FILES ) )
  229. {
  230. wxString cacheError;
  231. do
  232. {
  233. // prepend the libpath into fullPath
  234. wxFileName fullPath( m_lib_path.GetPath(), fpFileName );
  235. // Queue I/O errors so only files that fail to parse don't get loaded.
  236. try
  237. {
  238. FILE_LINE_READER reader( fullPath.GetFullPath() );
  239. m_owner->m_parser->SetLineReader( &reader );
  240. MODULE* footprint = (MODULE*) m_owner->m_parser->Parse();
  241. // The footprint name is the file name without the extension.
  242. wxString fpName = fullPath.GetName();
  243. footprint->SetFPID( LIB_ID( fpName ) );
  244. m_modules.insert( fpName, new FP_CACHE_ITEM( footprint, fullPath ) );
  245. m_cache_timestamp += fullPath.GetModificationTime().GetValue().GetValue();
  246. }
  247. catch( const IO_ERROR& ioe )
  248. {
  249. if( !cacheError.IsEmpty() )
  250. cacheError += "\n\n";
  251. cacheError += ioe.What();
  252. }
  253. } while( dir.GetNext( &fpFileName ) );
  254. if( !cacheError.IsEmpty() )
  255. THROW_IO_ERROR( cacheError );
  256. }
  257. }
  258. void FP_CACHE::Remove( const wxString& aFootprintName )
  259. {
  260. MODULE_CITER it = m_modules.find( aFootprintName );
  261. if( it == m_modules.end() )
  262. {
  263. wxString msg = wxString::Format(
  264. _( "library \"%s\" has no footprint \"%s\" to delete" ),
  265. GetChars( m_lib_path.GetPath() ),
  266. GetChars( aFootprintName )
  267. );
  268. THROW_IO_ERROR( msg );
  269. }
  270. // Remove the module from the cache and delete the module file from the library.
  271. wxString fullPath = it->second->GetFileName().GetFullPath();
  272. m_modules.erase( aFootprintName );
  273. wxRemoveFile( fullPath );
  274. }
  275. bool FP_CACHE::IsPath( const wxString& aPath ) const
  276. {
  277. // Converts path separators to native path separators
  278. wxFileName newPath;
  279. newPath.AssignDir( aPath );
  280. return m_lib_path == newPath;
  281. }
  282. bool FP_CACHE::IsModified()
  283. {
  284. if( m_cache_dirty )
  285. return true;
  286. else
  287. return GetTimestamp() != m_cache_timestamp;
  288. }
  289. long long FP_CACHE::GetTimestamp()
  290. {
  291. // Avoid expensive GetModificationTime checks if we already know we're dirty
  292. if( m_cache_dirty )
  293. return wxDateTime::Now().GetValue().GetValue();
  294. long long files_timestamp = 0;
  295. if( m_lib_path.DirExists() )
  296. {
  297. files_timestamp = m_lib_path.GetModificationTime().GetValue().GetValue();
  298. for( MODULE_CITER it = m_modules.begin(); it != m_modules.end(); ++it )
  299. {
  300. wxFileName moduleFile = it->second->GetFileName();
  301. if( moduleFile.FileExists() )
  302. files_timestamp += moduleFile.GetModificationTime().GetValue().GetValue();
  303. }
  304. }
  305. // If the new timestamp doesn't match the cache timestamp, then save ourselves the
  306. // expensive calls next time
  307. if( m_cache_timestamp != files_timestamp )
  308. m_cache_dirty = true;
  309. return files_timestamp;
  310. }
  311. void PCB_IO::Save( const wxString& aFileName, BOARD* aBoard, const PROPERTIES* aProperties )
  312. {
  313. LOCALE_IO toggle; // toggles on, then off, the C locale.
  314. init( aProperties );
  315. m_board = aBoard; // after init()
  316. // Prepare net mapping that assures that net codes saved in a file are consecutive integers
  317. m_mapping->SetBoard( aBoard );
  318. FILE_OUTPUTFORMATTER formatter( aFileName );
  319. m_out = &formatter; // no ownership
  320. m_out->Print( 0, "(kicad_pcb (version %d) (host pcbnew %s)\n", SEXPR_BOARD_FILE_VERSION,
  321. formatter.Quotew( GetBuildVersion() ).c_str() );
  322. Format( aBoard, 1 );
  323. m_out->Print( 0, ")\n" );
  324. }
  325. BOARD_ITEM* PCB_IO::Parse( const wxString& aClipboardSourceInput )
  326. {
  327. std::string input = TO_UTF8( aClipboardSourceInput );
  328. STRING_LINE_READER reader( input, wxT( "clipboard" ) );
  329. m_parser->SetLineReader( &reader );
  330. try
  331. {
  332. return m_parser->Parse();
  333. }
  334. catch( const PARSE_ERROR& parse_error )
  335. {
  336. if( m_parser->IsTooRecent() )
  337. throw FUTURE_FORMAT_ERROR( parse_error, m_parser->GetRequiredVersion() );
  338. else
  339. throw;
  340. }
  341. }
  342. void PCB_IO::Format( BOARD_ITEM* aItem, int aNestLevel ) const
  343. {
  344. LOCALE_IO toggle; // public API function, perform anything convenient for caller
  345. switch( aItem->Type() )
  346. {
  347. case PCB_T:
  348. format( static_cast<BOARD*>( aItem ), aNestLevel );
  349. break;
  350. case PCB_DIMENSION_T:
  351. format( static_cast<DIMENSION*>( aItem ), aNestLevel );
  352. break;
  353. case PCB_LINE_T:
  354. format( static_cast<DRAWSEGMENT*>( aItem ), aNestLevel );
  355. break;
  356. case PCB_MODULE_EDGE_T:
  357. format( static_cast<EDGE_MODULE*>( aItem ), aNestLevel );
  358. break;
  359. case PCB_TARGET_T:
  360. format( static_cast<PCB_TARGET*>( aItem ), aNestLevel );
  361. break;
  362. case PCB_MODULE_T:
  363. format( static_cast<MODULE*>( aItem ), aNestLevel );
  364. break;
  365. case PCB_PAD_T:
  366. format( static_cast<D_PAD*>( aItem ), aNestLevel );
  367. break;
  368. case PCB_TEXT_T:
  369. format( static_cast<TEXTE_PCB*>( aItem ), aNestLevel );
  370. break;
  371. case PCB_MODULE_TEXT_T:
  372. format( static_cast<TEXTE_MODULE*>( aItem ), aNestLevel );
  373. break;
  374. case PCB_TRACE_T:
  375. case PCB_VIA_T:
  376. format( static_cast<TRACK*>( aItem ), aNestLevel );
  377. break;
  378. case PCB_ZONE_AREA_T:
  379. format( static_cast<ZONE_CONTAINER*>( aItem ), aNestLevel );
  380. break;
  381. default:
  382. wxFAIL_MSG( wxT( "Cannot format item " ) + aItem->GetClass() );
  383. }
  384. }
  385. void PCB_IO::formatLayer( const BOARD_ITEM* aItem ) const
  386. {
  387. if( m_ctl & CTL_STD_LAYER_NAMES )
  388. {
  389. PCB_LAYER_ID layer = aItem->GetLayer();
  390. // English layer names should never need quoting.
  391. m_out->Print( 0, " (layer %s)", TO_UTF8( BOARD::GetStandardLayerName( layer ) ) );
  392. }
  393. else
  394. m_out->Print( 0, " (layer %s)", m_out->Quotew( aItem->GetLayerName() ).c_str() );
  395. }
  396. void PCB_IO::formatSetup( BOARD* aBoard, int aNestLevel ) const
  397. {
  398. const BOARD_DESIGN_SETTINGS& dsnSettings = aBoard->GetDesignSettings();
  399. // Setup
  400. m_out->Print( aNestLevel, "(setup\n" );
  401. // Save current default track width, for compatibility with older Pcbnew version;
  402. m_out->Print( aNestLevel+1, "(last_trace_width %s)\n",
  403. FMT_IU( dsnSettings.GetCurrentTrackWidth() ).c_str() );
  404. // Save custom tracks width list (the first is not saved here: this is the netclass value
  405. for( unsigned ii = 1; ii < dsnSettings.m_TrackWidthList.size(); ii++ )
  406. m_out->Print( aNestLevel+1, "(user_trace_width %s)\n",
  407. FMT_IU( dsnSettings.m_TrackWidthList[ii] ).c_str() );
  408. m_out->Print( aNestLevel+1, "(trace_clearance %s)\n",
  409. FMT_IU( dsnSettings.GetDefault()->GetClearance() ).c_str() );
  410. // ZONE_SETTINGS
  411. m_out->Print( aNestLevel+1, "(zone_clearance %s)\n",
  412. FMT_IU( aBoard->GetZoneSettings().m_ZoneClearance ).c_str() );
  413. m_out->Print( aNestLevel+1, "(zone_45_only %s)\n",
  414. aBoard->GetZoneSettings().m_Zone_45_Only ? "yes" : "no" );
  415. m_out->Print( aNestLevel+1, "(trace_min %s)\n",
  416. FMT_IU( dsnSettings.m_TrackMinWidth ).c_str() );
  417. m_out->Print( aNestLevel+1, "(segment_width %s)\n",
  418. FMT_IU( dsnSettings.m_DrawSegmentWidth ).c_str() );
  419. m_out->Print( aNestLevel+1, "(edge_width %s)\n",
  420. FMT_IU( dsnSettings.m_EdgeSegmentWidth ).c_str() );
  421. // Save current default via size, for compatibility with older Pcbnew version;
  422. m_out->Print( aNestLevel+1, "(via_size %s)\n",
  423. FMT_IU( dsnSettings.GetDefault()->GetViaDiameter() ).c_str() );
  424. m_out->Print( aNestLevel+1, "(via_drill %s)\n",
  425. FMT_IU( dsnSettings.GetDefault()->GetViaDrill() ).c_str() );
  426. m_out->Print( aNestLevel+1, "(via_min_size %s)\n",
  427. FMT_IU( dsnSettings.m_ViasMinSize ).c_str() );
  428. m_out->Print( aNestLevel+1, "(via_min_drill %s)\n",
  429. FMT_IU( dsnSettings.m_ViasMinDrill ).c_str() );
  430. // Save custom vias diameters list (the first is not saved here: this is
  431. // the netclass value
  432. for( unsigned ii = 1; ii < dsnSettings.m_ViasDimensionsList.size(); ii++ )
  433. m_out->Print( aNestLevel+1, "(user_via %s %s)\n",
  434. FMT_IU( dsnSettings.m_ViasDimensionsList[ii].m_Diameter ).c_str(),
  435. FMT_IU( dsnSettings.m_ViasDimensionsList[ii].m_Drill ).c_str() );
  436. // for old versions compatibility:
  437. if( dsnSettings.m_BlindBuriedViaAllowed )
  438. m_out->Print( aNestLevel+1, "(blind_buried_vias_allowed yes)\n" );
  439. m_out->Print( aNestLevel+1, "(uvia_size %s)\n",
  440. FMT_IU( dsnSettings.GetDefault()->GetuViaDiameter() ).c_str() );
  441. m_out->Print( aNestLevel+1, "(uvia_drill %s)\n",
  442. FMT_IU( dsnSettings.GetDefault()->GetuViaDrill() ).c_str() );
  443. m_out->Print( aNestLevel+1, "(uvias_allowed %s)\n",
  444. ( dsnSettings.m_MicroViasAllowed ) ? "yes" : "no" );
  445. m_out->Print( aNestLevel+1, "(uvia_min_size %s)\n",
  446. FMT_IU( dsnSettings.m_MicroViasMinSize ).c_str() );
  447. m_out->Print( aNestLevel+1, "(uvia_min_drill %s)\n",
  448. FMT_IU( dsnSettings.m_MicroViasMinDrill ).c_str() );
  449. m_out->Print( aNestLevel+1, "(pcb_text_width %s)\n",
  450. FMT_IU( dsnSettings.m_PcbTextWidth ).c_str() );
  451. m_out->Print( aNestLevel+1, "(pcb_text_size %s %s)\n",
  452. FMT_IU( dsnSettings.m_PcbTextSize.x ).c_str(),
  453. FMT_IU( dsnSettings.m_PcbTextSize.y ).c_str() );
  454. m_out->Print( aNestLevel+1, "(mod_edge_width %s)\n",
  455. FMT_IU( dsnSettings.m_ModuleSegmentWidth ).c_str() );
  456. m_out->Print( aNestLevel+1, "(mod_text_size %s %s)\n",
  457. FMT_IU( dsnSettings.m_ModuleTextSize.x ).c_str(),
  458. FMT_IU( dsnSettings.m_ModuleTextSize.y ).c_str() );
  459. m_out->Print( aNestLevel+1, "(mod_text_width %s)\n",
  460. FMT_IU( dsnSettings.m_ModuleTextWidth ).c_str() );
  461. m_out->Print( aNestLevel+1, "(pad_size %s %s)\n",
  462. FMT_IU( dsnSettings.m_Pad_Master.GetSize().x ).c_str(),
  463. FMT_IU( dsnSettings.m_Pad_Master.GetSize().y ).c_str() );
  464. m_out->Print( aNestLevel+1, "(pad_drill %s)\n",
  465. FMT_IU( dsnSettings.m_Pad_Master.GetDrillSize().x ).c_str() );
  466. m_out->Print( aNestLevel+1, "(pad_to_mask_clearance %s)\n",
  467. FMT_IU( dsnSettings.m_SolderMaskMargin ).c_str() );
  468. if( dsnSettings.m_SolderMaskMinWidth )
  469. m_out->Print( aNestLevel+1, "(solder_mask_min_width %s)\n",
  470. FMT_IU( dsnSettings.m_SolderMaskMinWidth ).c_str() );
  471. if( dsnSettings.m_SolderPasteMargin != 0 )
  472. m_out->Print( aNestLevel+1, "(pad_to_paste_clearance %s)\n",
  473. FMT_IU( dsnSettings.m_SolderPasteMargin ).c_str() );
  474. if( dsnSettings.m_SolderPasteMarginRatio != 0 )
  475. m_out->Print( aNestLevel+1, "(pad_to_paste_clearance_ratio %s)\n",
  476. Double2Str( dsnSettings.m_SolderPasteMarginRatio ).c_str() );
  477. m_out->Print( aNestLevel+1, "(aux_axis_origin %s %s)\n",
  478. FMT_IU( aBoard->GetAuxOrigin().x ).c_str(),
  479. FMT_IU( aBoard->GetAuxOrigin().y ).c_str() );
  480. if( aBoard->GetGridOrigin().x || aBoard->GetGridOrigin().y )
  481. m_out->Print( aNestLevel+1, "(grid_origin %s %s)\n",
  482. FMT_IU( aBoard->GetGridOrigin().x ).c_str(),
  483. FMT_IU( aBoard->GetGridOrigin().y ).c_str() );
  484. m_out->Print( aNestLevel+1, "(visible_elements %X)\n",
  485. dsnSettings.GetVisibleElements() );
  486. aBoard->GetPlotOptions().Format( m_out, aNestLevel+1 );
  487. m_out->Print( aNestLevel, ")\n\n" );
  488. }
  489. void PCB_IO::formatGeneral( BOARD* aBoard, int aNestLevel ) const
  490. {
  491. const BOARD_DESIGN_SETTINGS& dsnSettings = aBoard->GetDesignSettings();
  492. m_out->Print( 0, "\n" );
  493. m_out->Print( aNestLevel, "(general\n" );
  494. // Write Bounding box info
  495. m_out->Print( aNestLevel+1, "(thickness %s)\n",
  496. FMT_IU( dsnSettings.GetBoardThickness() ).c_str() );
  497. m_out->Print( aNestLevel+1, "(drawings %d)\n", aBoard->Drawings().Size() );
  498. m_out->Print( aNestLevel+1, "(tracks %d)\n", aBoard->GetNumSegmTrack() );
  499. m_out->Print( aNestLevel+1, "(zones %d)\n", aBoard->GetNumSegmZone() );
  500. m_out->Print( aNestLevel+1, "(modules %d)\n", aBoard->m_Modules.GetCount() );
  501. m_out->Print( aNestLevel+1, "(nets %d)\n", m_mapping->GetSize() );
  502. m_out->Print( aNestLevel, ")\n\n" );
  503. aBoard->GetPageSettings().Format( m_out, aNestLevel, m_ctl );
  504. aBoard->GetTitleBlock().Format( m_out, aNestLevel, m_ctl );
  505. }
  506. void PCB_IO::formatBoardLayers( BOARD* aBoard, int aNestLevel ) const
  507. {
  508. m_out->Print( aNestLevel, "(layers\n" );
  509. // Save only the used copper layers from front to back.
  510. LSET visible_layers = aBoard->GetVisibleLayers();
  511. for( LSEQ cu = aBoard->GetEnabledLayers().CuStack(); cu; ++cu )
  512. {
  513. PCB_LAYER_ID layer = *cu;
  514. m_out->Print( aNestLevel+1, "(%d %s %s", layer,
  515. m_out->Quotew( aBoard->GetLayerName( layer ) ).c_str(),
  516. LAYER::ShowType( aBoard->GetLayerType( layer ) ) );
  517. if( !visible_layers[layer] )
  518. m_out->Print( 0, " hide" );
  519. m_out->Print( 0, ")\n" );
  520. }
  521. // Save used non-copper layers in the order they are defined.
  522. // desired sequence for non Cu BOARD layers.
  523. static const PCB_LAYER_ID non_cu[] =
  524. {
  525. B_Adhes, // 32
  526. F_Adhes,
  527. B_Paste,
  528. F_Paste,
  529. B_SilkS,
  530. F_SilkS,
  531. B_Mask,
  532. F_Mask,
  533. Dwgs_User,
  534. Cmts_User,
  535. Eco1_User,
  536. Eco2_User,
  537. Edge_Cuts,
  538. Margin,
  539. B_CrtYd,
  540. F_CrtYd,
  541. B_Fab,
  542. F_Fab
  543. };
  544. for( LSEQ seq = aBoard->GetEnabledLayers().Seq( non_cu, DIM( non_cu ) ); seq; ++seq )
  545. {
  546. PCB_LAYER_ID layer = *seq;
  547. m_out->Print( aNestLevel+1, "(%d %s user", layer,
  548. m_out->Quotew( aBoard->GetLayerName( layer ) ).c_str() );
  549. if( !visible_layers[layer] )
  550. m_out->Print( 0, " hide" );
  551. m_out->Print( 0, ")\n" );
  552. }
  553. m_out->Print( aNestLevel, ")\n\n" );
  554. }
  555. void PCB_IO::formatNetInformation( BOARD* aBoard, int aNestLevel ) const
  556. {
  557. const BOARD_DESIGN_SETTINGS& dsnSettings = aBoard->GetDesignSettings();
  558. for( NETINFO_MAPPING::iterator net = m_mapping->begin(), netEnd = m_mapping->end();
  559. net != netEnd; ++net )
  560. {
  561. m_out->Print( aNestLevel, "(net %d %s)\n",
  562. m_mapping->Translate( net->GetNet() ),
  563. m_out->Quotew( net->GetNetname() ).c_str() );
  564. }
  565. m_out->Print( 0, "\n" );
  566. // Save the default net class first.
  567. NETCLASS defaultNC = *dsnSettings.GetDefault();
  568. filterNetClass( *aBoard, defaultNC ); // Remove empty nets (from a copy of a netclass)
  569. defaultNC.Format( m_out, aNestLevel, m_ctl );
  570. // Save the rest of the net classes alphabetically.
  571. for( NETCLASSES::const_iterator it = dsnSettings.m_NetClasses.begin();
  572. it != dsnSettings.m_NetClasses.end();
  573. ++it )
  574. {
  575. NETCLASS netclass = *it->second;
  576. filterNetClass( *aBoard, netclass ); // Remove empty nets (from a copy of a netclass)
  577. netclass.Format( m_out, aNestLevel, m_ctl );
  578. }
  579. }
  580. void PCB_IO::formatHeader( BOARD* aBoard, int aNestLevel ) const
  581. {
  582. formatGeneral( aBoard, aNestLevel );
  583. // Layers.
  584. formatBoardLayers( aBoard, aNestLevel );
  585. // Setup
  586. formatSetup( aBoard, aNestLevel );
  587. // Save net codes and names
  588. formatNetInformation( aBoard, aNestLevel );
  589. }
  590. void PCB_IO::format( BOARD* aBoard, int aNestLevel ) const
  591. {
  592. formatHeader( aBoard, aNestLevel );
  593. // Save the modules.
  594. for( MODULE* module = aBoard->m_Modules; module; module = module->Next() )
  595. {
  596. Format( module, aNestLevel );
  597. m_out->Print( 0, "\n" );
  598. }
  599. // Save the graphical items on the board (not owned by a module)
  600. for( auto item : aBoard->Drawings() )
  601. Format( item, aNestLevel );
  602. if( aBoard->Drawings().Size() )
  603. m_out->Print( 0, "\n" );
  604. // Do not save MARKER_PCBs, they can be regenerated easily.
  605. // Save the tracks and vias.
  606. for( TRACK* track = aBoard->m_Track; track; track = track->Next() )
  607. Format( track, aNestLevel );
  608. if( aBoard->m_Track.GetCount() )
  609. m_out->Print( 0, "\n" );
  610. /// @todo Add warning here that the old segment filed zones are no longer supported and
  611. /// will not be saved.
  612. // Save the polygon (which are the newer technology) zones.
  613. for( int i = 0; i < aBoard->GetAreaCount(); ++i )
  614. Format( aBoard->GetArea( i ), aNestLevel );
  615. }
  616. void PCB_IO::format( DIMENSION* aDimension, int aNestLevel ) const
  617. {
  618. m_out->Print( aNestLevel, "(dimension %s (width %s)",
  619. FMT_IU( aDimension->GetValue() ).c_str(),
  620. FMT_IU( aDimension->GetWidth() ).c_str() );
  621. formatLayer( aDimension );
  622. if( aDimension->GetTimeStamp() )
  623. m_out->Print( 0, " (tstamp %lX)", (unsigned long)aDimension->GetTimeStamp() );
  624. m_out->Print( 0, "\n" );
  625. Format( &aDimension->Text(), aNestLevel+1 );
  626. m_out->Print( aNestLevel+1, "(feature1 (pts (xy %s %s) (xy %s %s)))\n",
  627. FMT_IU( aDimension->m_featureLineDO.x ).c_str(),
  628. FMT_IU( aDimension->m_featureLineDO.y ).c_str(),
  629. FMT_IU( aDimension->m_featureLineDF.x ).c_str(),
  630. FMT_IU( aDimension->m_featureLineDF.y ).c_str() );
  631. m_out->Print( aNestLevel+1, "(feature2 (pts (xy %s %s) (xy %s %s)))\n",
  632. FMT_IU( aDimension->m_featureLineGO.x ).c_str(),
  633. FMT_IU( aDimension->m_featureLineGO.y ).c_str(),
  634. FMT_IU( aDimension->m_featureLineGF.x ).c_str(),
  635. FMT_IU( aDimension->m_featureLineGF.y ).c_str() );
  636. m_out->Print( aNestLevel+1, "(crossbar (pts (xy %s %s) (xy %s %s)))\n",
  637. FMT_IU( aDimension->m_crossBarO.x ).c_str(),
  638. FMT_IU( aDimension->m_crossBarO.y ).c_str(),
  639. FMT_IU( aDimension->m_crossBarF.x ).c_str(),
  640. FMT_IU( aDimension->m_crossBarF.y ).c_str() );
  641. m_out->Print( aNestLevel+1, "(arrow1a (pts (xy %s %s) (xy %s %s)))\n",
  642. FMT_IU( aDimension->m_crossBarF.x ).c_str(),
  643. FMT_IU( aDimension->m_crossBarF.y ).c_str(),
  644. FMT_IU( aDimension->m_arrowD1F.x ).c_str(),
  645. FMT_IU( aDimension->m_arrowD1F.y ).c_str() );
  646. m_out->Print( aNestLevel+1, "(arrow1b (pts (xy %s %s) (xy %s %s)))\n",
  647. FMT_IU( aDimension->m_crossBarF.x ).c_str(),
  648. FMT_IU( aDimension->m_crossBarF.y ).c_str(),
  649. FMT_IU( aDimension->m_arrowD2F.x ).c_str(),
  650. FMT_IU( aDimension->m_arrowD2F.y ).c_str() );
  651. m_out->Print( aNestLevel+1, "(arrow2a (pts (xy %s %s) (xy %s %s)))\n",
  652. FMT_IU( aDimension->m_crossBarO.x ).c_str(),
  653. FMT_IU( aDimension->m_crossBarO.y ).c_str(),
  654. FMT_IU( aDimension->m_arrowG1F.x ).c_str(),
  655. FMT_IU( aDimension->m_arrowG1F.y ).c_str() );
  656. m_out->Print( aNestLevel+1, "(arrow2b (pts (xy %s %s) (xy %s %s)))\n",
  657. FMT_IU( aDimension->m_crossBarO.x ).c_str(),
  658. FMT_IU( aDimension->m_crossBarO.y ).c_str(),
  659. FMT_IU( aDimension->m_arrowG2F.x ).c_str(),
  660. FMT_IU( aDimension->m_arrowG2F.y ).c_str() );
  661. m_out->Print( aNestLevel, ")\n" );
  662. }
  663. void PCB_IO::format( DRAWSEGMENT* aSegment, int aNestLevel ) const
  664. {
  665. switch( aSegment->GetShape() )
  666. {
  667. case S_SEGMENT: // Line
  668. m_out->Print( aNestLevel, "(gr_line (start %s) (end %s)",
  669. FMT_IU( aSegment->GetStart() ).c_str(),
  670. FMT_IU( aSegment->GetEnd() ).c_str() );
  671. if( aSegment->GetAngle() != 0.0 )
  672. m_out->Print( 0, " (angle %s)", FMT_ANGLE( aSegment->GetAngle() ).c_str() );
  673. break;
  674. case S_CIRCLE: // Circle
  675. m_out->Print( aNestLevel, "(gr_circle (center %s) (end %s)",
  676. FMT_IU( aSegment->GetStart() ).c_str(),
  677. FMT_IU( aSegment->GetEnd() ).c_str() );
  678. break;
  679. case S_ARC: // Arc
  680. m_out->Print( aNestLevel, "(gr_arc (start %s) (end %s) (angle %s)",
  681. FMT_IU( aSegment->GetStart() ).c_str(),
  682. FMT_IU( aSegment->GetEnd() ).c_str(),
  683. FMT_ANGLE( aSegment->GetAngle() ).c_str() );
  684. break;
  685. case S_POLYGON: // Polygon
  686. if( aSegment->IsPolyShapeValid() )
  687. {
  688. SHAPE_POLY_SET& poly = aSegment->GetPolyShape();
  689. SHAPE_LINE_CHAIN& outline = poly.Outline( 0 );
  690. int pointsCount = outline.PointCount();
  691. m_out->Print( aNestLevel, "(gr_poly (pts" );
  692. for( int ii = 0; ii < pointsCount; ++ii )
  693. {
  694. m_out->Print( 0, " (xy %s)", FMT_IU( outline.CPoint( ii ) ).c_str() );
  695. }
  696. m_out->Print( 0, ")" );
  697. }
  698. else
  699. {
  700. wxFAIL_MSG( wxT( "Cannot format invalid polygon." ) );
  701. return;
  702. }
  703. break;
  704. case S_CURVE: // Bezier curve
  705. m_out->Print( aNestLevel, "(gr_curve (pts (xy %s) (xy %s) (xy %s) (xy %s))",
  706. FMT_IU( aSegment->GetStart() ).c_str(),
  707. FMT_IU( aSegment->GetBezControl1() ).c_str(),
  708. FMT_IU( aSegment->GetBezControl2() ).c_str(),
  709. FMT_IU( aSegment->GetEnd() ).c_str() );
  710. break;
  711. default:
  712. wxFAIL_MSG( wxT( "Cannot format invalid DRAWSEGMENT type." ) );
  713. return;
  714. };
  715. formatLayer( aSegment );
  716. m_out->Print( 0, " (width %s)", FMT_IU( aSegment->GetWidth() ).c_str() );
  717. if( aSegment->GetTimeStamp() )
  718. m_out->Print( 0, " (tstamp %lX)", (unsigned long)aSegment->GetTimeStamp() );
  719. if( aSegment->GetStatus() )
  720. m_out->Print( 0, " (status %X)", aSegment->GetStatus() );
  721. m_out->Print( 0, ")\n" );
  722. }
  723. void PCB_IO::format( EDGE_MODULE* aModuleDrawing, int aNestLevel ) const
  724. {
  725. switch( aModuleDrawing->GetShape() )
  726. {
  727. case S_SEGMENT: // Line
  728. m_out->Print( aNestLevel, "(fp_line (start %s) (end %s)",
  729. FMT_IU( aModuleDrawing->GetStart0() ).c_str(),
  730. FMT_IU( aModuleDrawing->GetEnd0() ).c_str() );
  731. break;
  732. case S_CIRCLE: // Circle
  733. m_out->Print( aNestLevel, "(fp_circle (center %s) (end %s)",
  734. FMT_IU( aModuleDrawing->GetStart0() ).c_str(),
  735. FMT_IU( aModuleDrawing->GetEnd0() ).c_str() );
  736. break;
  737. case S_ARC: // Arc
  738. m_out->Print( aNestLevel, "(fp_arc (start %s) (end %s) (angle %s)",
  739. FMT_IU( aModuleDrawing->GetStart0() ).c_str(),
  740. FMT_IU( aModuleDrawing->GetEnd0() ).c_str(),
  741. FMT_ANGLE( aModuleDrawing->GetAngle() ).c_str() );
  742. break;
  743. case S_POLYGON: // Polygonal segment
  744. if( aModuleDrawing->IsPolyShapeValid() )
  745. {
  746. SHAPE_POLY_SET& poly = aModuleDrawing->GetPolyShape();
  747. SHAPE_LINE_CHAIN& outline = poly.Outline( 0 );
  748. int pointsCount = outline.PointCount();
  749. m_out->Print( aNestLevel, "(fp_poly (pts" );
  750. for( int ii = 0; ii < pointsCount; ++ii )
  751. {
  752. int nestLevel = 0;
  753. if( ii && !( ii%4 ) ) // newline every 4 pts
  754. {
  755. nestLevel = aNestLevel + 1;
  756. m_out->Print( 0, "\n" );
  757. }
  758. m_out->Print( nestLevel, "%s(xy %s)",
  759. nestLevel ? "" : " ", FMT_IU( outline.CPoint( ii ) ).c_str() );
  760. }
  761. m_out->Print( 0, ")" );
  762. }
  763. else
  764. {
  765. wxFAIL_MSG( wxT( "Cannot format invalid polygon." ) );
  766. return;
  767. }
  768. break;
  769. case S_CURVE: // Bezier curve
  770. m_out->Print( aNestLevel, "(fp_curve (pts (xy %s) (xy %s) (xy %s) (xy %s))",
  771. FMT_IU( aModuleDrawing->GetStart0() ).c_str(),
  772. FMT_IU( aModuleDrawing->GetBezControl1() ).c_str(),
  773. FMT_IU( aModuleDrawing->GetBezControl2() ).c_str(),
  774. FMT_IU( aModuleDrawing->GetEnd0() ).c_str() );
  775. break;
  776. default:
  777. wxFAIL_MSG( wxT( "Cannot format invalid DRAWSEGMENT type." ) );
  778. return;
  779. };
  780. formatLayer( aModuleDrawing );
  781. m_out->Print( 0, " (width %s)", FMT_IU( aModuleDrawing->GetWidth() ).c_str() );
  782. m_out->Print( 0, ")\n" );
  783. }
  784. void PCB_IO::format( PCB_TARGET* aTarget, int aNestLevel ) const
  785. {
  786. m_out->Print( aNestLevel, "(target %s (at %s) (size %s)",
  787. ( aTarget->GetShape() ) ? "x" : "plus",
  788. FMT_IU( aTarget->GetPosition() ).c_str(),
  789. FMT_IU( aTarget->GetSize() ).c_str() );
  790. if( aTarget->GetWidth() != 0 )
  791. m_out->Print( 0, " (width %s)", FMT_IU( aTarget->GetWidth() ).c_str() );
  792. formatLayer( aTarget );
  793. if( aTarget->GetTimeStamp() )
  794. m_out->Print( 0, " (tstamp %lX)", (unsigned long)aTarget->GetTimeStamp() );
  795. m_out->Print( 0, ")\n" );
  796. }
  797. void PCB_IO::format( MODULE* aModule, int aNestLevel ) const
  798. {
  799. if( !( m_ctl & CTL_OMIT_INITIAL_COMMENTS ) )
  800. {
  801. const wxArrayString* initial_comments = aModule->GetInitialComments();
  802. if( initial_comments )
  803. {
  804. for( unsigned i=0; i<initial_comments->GetCount(); ++i )
  805. m_out->Print( aNestLevel, "%s\n", TO_UTF8( (*initial_comments)[i] ) );
  806. m_out->Print( 0, "\n" ); // improve readability?
  807. }
  808. }
  809. m_out->Print( aNestLevel, "(module %s",
  810. m_out->Quotes( aModule->GetFPID().Format() ).c_str() );
  811. if( aModule->IsLocked() )
  812. m_out->Print( 0, " locked" );
  813. if( aModule->IsPlaced() )
  814. m_out->Print( 0, " placed" );
  815. formatLayer( aModule );
  816. m_out->Print( 0, " (tedit %lX)", (unsigned long)aModule->GetLastEditTime() );
  817. if( !( m_ctl & CTL_OMIT_TSTAMPS ) )
  818. {
  819. m_out->Print( 0, " (tstamp %lX)\n", (unsigned long)aModule->GetTimeStamp() );
  820. }
  821. else
  822. m_out->Print( 0, "\n" );
  823. if( !( m_ctl & CTL_OMIT_AT ) )
  824. {
  825. m_out->Print( aNestLevel+1, "(at %s", FMT_IU( aModule->GetPosition() ).c_str() );
  826. if( aModule->GetOrientation() != 0.0 )
  827. m_out->Print( 0, " %s", FMT_ANGLE( aModule->GetOrientation() ).c_str() );
  828. m_out->Print( 0, ")\n" );
  829. }
  830. if( !aModule->GetDescription().IsEmpty() )
  831. m_out->Print( aNestLevel+1, "(descr %s)\n",
  832. m_out->Quotew( aModule->GetDescription() ).c_str() );
  833. if( !aModule->GetKeywords().IsEmpty() )
  834. m_out->Print( aNestLevel+1, "(tags %s)\n",
  835. m_out->Quotew( aModule->GetKeywords() ).c_str() );
  836. if( !( m_ctl & CTL_OMIT_PATH ) && !!aModule->GetPath() )
  837. m_out->Print( aNestLevel+1, "(path %s)\n",
  838. m_out->Quotew( aModule->GetPath() ).c_str() );
  839. if( aModule->GetPlacementCost90() != 0 )
  840. m_out->Print( aNestLevel+1, "(autoplace_cost90 %d)\n", aModule->GetPlacementCost90() );
  841. if( aModule->GetPlacementCost180() != 0 )
  842. m_out->Print( aNestLevel+1, "(autoplace_cost180 %d)\n", aModule->GetPlacementCost180() );
  843. if( aModule->GetLocalSolderMaskMargin() != 0 )
  844. m_out->Print( aNestLevel+1, "(solder_mask_margin %s)\n",
  845. FMT_IU( aModule->GetLocalSolderMaskMargin() ).c_str() );
  846. if( aModule->GetLocalSolderPasteMargin() != 0 )
  847. m_out->Print( aNestLevel+1, "(solder_paste_margin %s)\n",
  848. FMT_IU( aModule->GetLocalSolderPasteMargin() ).c_str() );
  849. if( aModule->GetLocalSolderPasteMarginRatio() != 0 )
  850. m_out->Print( aNestLevel+1, "(solder_paste_ratio %s)\n",
  851. Double2Str( aModule->GetLocalSolderPasteMarginRatio() ).c_str() );
  852. if( aModule->GetLocalClearance() != 0 )
  853. m_out->Print( aNestLevel+1, "(clearance %s)\n",
  854. FMT_IU( aModule->GetLocalClearance() ).c_str() );
  855. if( aModule->GetZoneConnection() != PAD_ZONE_CONN_INHERITED )
  856. m_out->Print( aNestLevel+1, "(zone_connect %d)\n", aModule->GetZoneConnection() );
  857. if( aModule->GetThermalWidth() != 0 )
  858. m_out->Print( aNestLevel+1, "(thermal_width %s)\n",
  859. FMT_IU( aModule->GetThermalWidth() ).c_str() );
  860. if( aModule->GetThermalGap() != 0 )
  861. m_out->Print( aNestLevel+1, "(thermal_gap %s)\n",
  862. FMT_IU( aModule->GetThermalGap() ).c_str() );
  863. // Attributes
  864. if( aModule->GetAttributes() != MOD_DEFAULT )
  865. {
  866. m_out->Print( aNestLevel+1, "(attr" );
  867. if( aModule->GetAttributes() & MOD_CMS )
  868. m_out->Print( 0, " smd" );
  869. if( aModule->GetAttributes() & MOD_VIRTUAL )
  870. m_out->Print( 0, " virtual" );
  871. m_out->Print( 0, ")\n" );
  872. }
  873. Format( (BOARD_ITEM*) &aModule->Reference(), aNestLevel+1 );
  874. Format( (BOARD_ITEM*) &aModule->Value(), aNestLevel+1 );
  875. // Save drawing elements.
  876. for( BOARD_ITEM* gr = aModule->GraphicalItemsList(); gr; gr = gr->Next() )
  877. Format( gr, aNestLevel+1 );
  878. // Save pads.
  879. for( D_PAD* pad = aModule->PadsList(); pad; pad = pad->Next() )
  880. format( pad, aNestLevel+1 );
  881. // Save 3D info.
  882. auto bs3D = aModule->Models().begin();
  883. auto es3D = aModule->Models().end();
  884. while( bs3D != es3D )
  885. {
  886. if( !bs3D->m_Filename.IsEmpty() )
  887. {
  888. m_out->Print( aNestLevel+1, "(model %s\n",
  889. m_out->Quotew( bs3D->m_Filename ).c_str() );
  890. /* Write 3D model offset in mm
  891. * 4.0.x wrote "at" which was actually in inches
  892. * 5.0.x onwards, 3D model offset is written using "offset"
  893. *
  894. * If the offset is all zero, write "at" (fewer file changes)
  895. * Otherwise, write "offset"
  896. */
  897. wxString offsetTag = "offset";
  898. if( bs3D->m_Offset.x == 0 &&
  899. bs3D->m_Offset.y == 0 &&
  900. bs3D->m_Offset.z == 0 )
  901. {
  902. offsetTag = "at";
  903. }
  904. m_out->Print( aNestLevel+2, "(%s (xyz %s %s %s))\n",
  905. offsetTag.ToStdString().c_str(),
  906. Double2Str( bs3D->m_Offset.x ).c_str(),
  907. Double2Str( bs3D->m_Offset.y ).c_str(),
  908. Double2Str( bs3D->m_Offset.z ).c_str() );
  909. m_out->Print( aNestLevel+2, "(scale (xyz %s %s %s))\n",
  910. Double2Str( bs3D->m_Scale.x ).c_str(),
  911. Double2Str( bs3D->m_Scale.y ).c_str(),
  912. Double2Str( bs3D->m_Scale.z ).c_str() );
  913. m_out->Print( aNestLevel+2, "(rotate (xyz %s %s %s))\n",
  914. Double2Str( bs3D->m_Rotation.x ).c_str(),
  915. Double2Str( bs3D->m_Rotation.y ).c_str(),
  916. Double2Str( bs3D->m_Rotation.z ).c_str() );
  917. m_out->Print( aNestLevel+1, ")\n" );
  918. }
  919. ++bs3D;
  920. }
  921. m_out->Print( aNestLevel, ")\n" );
  922. }
  923. void PCB_IO::formatLayers( LSET aLayerMask, int aNestLevel ) const
  924. {
  925. std::string output;
  926. if( aNestLevel == 0 )
  927. output += ' ';
  928. output += "(layers";
  929. static const LSET cu_all( LSET::AllCuMask() );
  930. static const LSET fr_bk( 2, B_Cu, F_Cu );
  931. static const LSET adhes( 2, B_Adhes, F_Adhes );
  932. static const LSET paste( 2, B_Paste, F_Paste );
  933. static const LSET silks( 2, B_SilkS, F_SilkS );
  934. static const LSET mask( 2, B_Mask, F_Mask );
  935. static const LSET crt_yd(2, B_CrtYd, F_CrtYd );
  936. static const LSET fab( 2, B_Fab, F_Fab );
  937. LSET cu_mask = cu_all;
  938. // output copper layers first, then non copper
  939. if( ( aLayerMask & cu_mask ) == cu_mask )
  940. {
  941. output += " *.Cu";
  942. aLayerMask &= ~cu_all; // clear bits, so they are not output again below
  943. }
  944. else if( ( aLayerMask & cu_mask ) == fr_bk )
  945. {
  946. output += " F&B.Cu";
  947. aLayerMask &= ~fr_bk;
  948. }
  949. if( ( aLayerMask & adhes ) == adhes )
  950. {
  951. output += " *.Adhes";
  952. aLayerMask &= ~adhes;
  953. }
  954. if( ( aLayerMask & paste ) == paste )
  955. {
  956. output += " *.Paste";
  957. aLayerMask &= ~paste;
  958. }
  959. if( ( aLayerMask & silks ) == silks )
  960. {
  961. output += " *.SilkS";
  962. aLayerMask &= ~silks;
  963. }
  964. if( ( aLayerMask & mask ) == mask )
  965. {
  966. output += " *.Mask";
  967. aLayerMask &= ~mask;
  968. }
  969. if( ( aLayerMask & crt_yd ) == crt_yd )
  970. {
  971. output += " *.CrtYd";
  972. aLayerMask &= ~crt_yd;
  973. }
  974. if( ( aLayerMask & fab ) == fab )
  975. {
  976. output += " *.Fab";
  977. aLayerMask &= ~fab;
  978. }
  979. // output any individual layers not handled in wildcard combos above
  980. wxString layerName;
  981. for( LAYER_NUM layer = 0; layer < PCB_LAYER_ID_COUNT; ++layer )
  982. {
  983. if( aLayerMask[layer] )
  984. {
  985. if( m_board && !( m_ctl & CTL_STD_LAYER_NAMES ) )
  986. layerName = m_board->GetLayerName( PCB_LAYER_ID( layer ) );
  987. else // I am being called from FootprintSave()
  988. layerName = BOARD::GetStandardLayerName( PCB_LAYER_ID( layer ) );
  989. output += ' ';
  990. output += m_out->Quotew( layerName );
  991. }
  992. }
  993. m_out->Print( aNestLevel, "%s)", output.c_str() );
  994. }
  995. void PCB_IO::format( D_PAD* aPad, int aNestLevel ) const
  996. {
  997. const char* shape;
  998. switch( aPad->GetShape() )
  999. {
  1000. case PAD_SHAPE_CIRCLE: shape = "circle"; break;
  1001. case PAD_SHAPE_RECT: shape = "rect"; break;
  1002. case PAD_SHAPE_OVAL: shape = "oval"; break;
  1003. case PAD_SHAPE_TRAPEZOID: shape = "trapezoid"; break;
  1004. case PAD_SHAPE_ROUNDRECT: shape = "roundrect"; break;
  1005. case PAD_SHAPE_CUSTOM: shape = "custom"; break;
  1006. default:
  1007. THROW_IO_ERROR( wxString::Format( _( "unknown pad type: %d"), aPad->GetShape() ) );
  1008. }
  1009. const char* type;
  1010. switch( aPad->GetAttribute() )
  1011. {
  1012. case PAD_ATTRIB_STANDARD: type = "thru_hole"; break;
  1013. case PAD_ATTRIB_SMD: type = "smd"; break;
  1014. case PAD_ATTRIB_CONN: type = "connect"; break;
  1015. case PAD_ATTRIB_HOLE_NOT_PLATED: type = "np_thru_hole"; break;
  1016. default:
  1017. THROW_IO_ERROR( wxString::Format( _( "unknown pad attribute: %d" ),
  1018. aPad->GetAttribute() ) );
  1019. }
  1020. m_out->Print( aNestLevel, "(pad %s %s %s",
  1021. m_out->Quotew( aPad->GetName() ).c_str(),
  1022. type, shape );
  1023. m_out->Print( 0, " (at %s", FMT_IU( aPad->GetPos0() ).c_str() );
  1024. if( aPad->GetOrientation() != 0.0 )
  1025. m_out->Print( 0, " %s", FMT_ANGLE( aPad->GetOrientation() ).c_str() );
  1026. m_out->Print( 0, ")" );
  1027. m_out->Print( 0, " (size %s)", FMT_IU( aPad->GetSize() ).c_str() );
  1028. if( (aPad->GetDelta().GetWidth()) != 0 || (aPad->GetDelta().GetHeight() != 0 ) )
  1029. m_out->Print( 0, " (rect_delta %s )", FMT_IU( aPad->GetDelta() ).c_str() );
  1030. wxSize sz = aPad->GetDrillSize();
  1031. wxPoint shapeoffset = aPad->GetOffset();
  1032. if( (sz.GetWidth() > 0) || (sz.GetHeight() > 0) ||
  1033. (shapeoffset.x != 0) || (shapeoffset.y != 0) )
  1034. {
  1035. m_out->Print( 0, " (drill" );
  1036. if( aPad->GetDrillShape() == PAD_DRILL_SHAPE_OBLONG )
  1037. m_out->Print( 0, " oval" );
  1038. if( sz.GetWidth() > 0 )
  1039. m_out->Print( 0, " %s", FMT_IU( sz.GetWidth() ).c_str() );
  1040. if( sz.GetHeight() > 0 && sz.GetWidth() != sz.GetHeight() )
  1041. m_out->Print( 0, " %s", FMT_IU( sz.GetHeight() ).c_str() );
  1042. if( (shapeoffset.x != 0) || (shapeoffset.y != 0) )
  1043. m_out->Print( 0, " (offset %s)", FMT_IU( aPad->GetOffset() ).c_str() );
  1044. m_out->Print( 0, ")" );
  1045. }
  1046. formatLayers( aPad->GetLayerSet() );
  1047. // Output the radius ratio for rounded rect pads
  1048. if( aPad->GetShape() == PAD_SHAPE_ROUNDRECT )
  1049. {
  1050. m_out->Print( 0, " (roundrect_rratio %s)",
  1051. Double2Str( aPad->GetRoundRectRadiusRatio() ).c_str() );
  1052. }
  1053. std::string output;
  1054. // Unconnected pad is default net so don't save it.
  1055. if( !( m_ctl & CTL_OMIT_NETS ) && aPad->GetNetCode() != NETINFO_LIST::UNCONNECTED )
  1056. StrPrintf( &output, " (net %d %s)", m_mapping->Translate( aPad->GetNetCode() ),
  1057. m_out->Quotew( aPad->GetNetname() ).c_str() );
  1058. if( aPad->GetPadToDieLength() != 0 )
  1059. StrPrintf( &output, " (die_length %s)", FMT_IU( aPad->GetPadToDieLength() ).c_str() );
  1060. if( aPad->GetLocalSolderMaskMargin() != 0 )
  1061. StrPrintf( &output, " (solder_mask_margin %s)",
  1062. FMT_IU( aPad->GetLocalSolderMaskMargin() ).c_str() );
  1063. if( aPad->GetLocalSolderPasteMargin() != 0 )
  1064. StrPrintf( &output, " (solder_paste_margin %s)",
  1065. FMT_IU( aPad->GetLocalSolderPasteMargin() ).c_str() );
  1066. if( aPad->GetLocalSolderPasteMarginRatio() != 0 )
  1067. StrPrintf( &output, " (solder_paste_margin_ratio %s)",
  1068. Double2Str( aPad->GetLocalSolderPasteMarginRatio() ).c_str() );
  1069. if( aPad->GetLocalClearance() != 0 )
  1070. StrPrintf( &output, " (clearance %s)", FMT_IU( aPad->GetLocalClearance() ).c_str() );
  1071. if( aPad->GetZoneConnection() != PAD_ZONE_CONN_INHERITED )
  1072. StrPrintf( &output, " (zone_connect %d)", aPad->GetZoneConnection() );
  1073. if( aPad->GetThermalWidth() != 0 )
  1074. StrPrintf( &output, " (thermal_width %s)", FMT_IU( aPad->GetThermalWidth() ).c_str() );
  1075. if( aPad->GetThermalGap() != 0 )
  1076. StrPrintf( &output, " (thermal_gap %s)", FMT_IU( aPad->GetThermalGap() ).c_str() );
  1077. if( output.size() )
  1078. {
  1079. m_out->Print( 0, "\n" );
  1080. m_out->Print( aNestLevel+1, "%s", output.c_str()+1 ); // +1 skips 1st space on 1st element
  1081. }
  1082. if( aPad->GetShape() == PAD_SHAPE_CUSTOM )
  1083. {
  1084. m_out->Print( 0, "\n");
  1085. m_out->Print( aNestLevel+1, "(options" );
  1086. if( aPad->GetCustomShapeInZoneOpt() == CUST_PAD_SHAPE_IN_ZONE_CONVEXHULL )
  1087. m_out->Print( 0, " (clearance convexhull)" );
  1088. #if 1 // Set to 1 to output the default option
  1089. else
  1090. m_out->Print( 0, " (clearance outline)" );
  1091. #endif
  1092. // Output the anchor pad shape (circle/rect)
  1093. if( aPad->GetAnchorPadShape() == PAD_SHAPE_RECT )
  1094. shape = "rect";
  1095. else
  1096. shape = "circle";
  1097. m_out->Print( 0, " (anchor %s)", shape );
  1098. m_out->Print( 0, ")"); // end of (options ...
  1099. // Output graphic primitive of the pad shape
  1100. m_out->Print( 0, "\n");
  1101. m_out->Print( aNestLevel+1, "(primitives" );
  1102. int nested_level = aNestLevel+2;
  1103. // Output all basic shapes
  1104. for( unsigned icnt = 0; icnt < aPad->GetPrimitives().size(); ++icnt )
  1105. {
  1106. m_out->Print( 0, "\n");
  1107. const PAD_CS_PRIMITIVE& primitive = aPad->GetPrimitives()[icnt];
  1108. switch( primitive.m_Shape )
  1109. {
  1110. case S_SEGMENT: // usual segment : line with rounded ends
  1111. m_out->Print( nested_level, "(gr_line (start %s) (end %s) (width %s))",
  1112. FMT_IU( primitive.m_Start ).c_str(),
  1113. FMT_IU( primitive.m_End ).c_str(),
  1114. FMT_IU( primitive.m_Thickness ).c_str() );
  1115. break;
  1116. case S_ARC: // Arc with rounded ends
  1117. m_out->Print( nested_level, "(gr_arc (start %s) (end %s) (angle %s) (width %s))",
  1118. FMT_IU( primitive.m_Start ).c_str(),
  1119. FMT_IU( primitive.m_End ).c_str(),
  1120. FMT_ANGLE( primitive.m_ArcAngle ).c_str(),
  1121. FMT_IU( primitive.m_Thickness ).c_str() );
  1122. break;
  1123. case S_CIRCLE: // ring or circle (circle if width == 0
  1124. m_out->Print( nested_level, "(gr_circle (center %s) (end %s %s) (width %s))",
  1125. FMT_IU( primitive.m_Start ).c_str(),
  1126. FMT_IU( primitive.m_Start.x + primitive.m_Radius ).c_str(),
  1127. FMT_IU( primitive.m_Start.y ).c_str(),
  1128. FMT_IU( primitive.m_Thickness ).c_str() );
  1129. break;
  1130. case S_POLYGON: // polygon
  1131. if( primitive.m_Poly.size() < 2 )
  1132. break; // Malformed polygon.
  1133. {
  1134. m_out->Print( nested_level, "(gr_poly (pts\n");
  1135. // Write the polygon corners coordinates:
  1136. const std::vector< wxPoint>& poly = primitive.m_Poly;
  1137. int newLine = 0;
  1138. for( unsigned ii = 0; ii < poly.size(); ii++ )
  1139. {
  1140. if( newLine == 0 )
  1141. m_out->Print( nested_level+1, " (xy %s)",
  1142. FMT_IU( wxPoint( poly[ii].x, poly[ii].y ) ).c_str() );
  1143. else
  1144. m_out->Print( 0, " (xy %s)",
  1145. FMT_IU( wxPoint( poly[ii].x, poly[ii].y ) ).c_str() );
  1146. if( ++newLine > 4 )
  1147. {
  1148. newLine = 0;
  1149. m_out->Print( 0, "\n" );
  1150. }
  1151. }
  1152. m_out->Print( 0, ") (width %s))", FMT_IU( primitive.m_Thickness ).c_str() );
  1153. }
  1154. break;
  1155. default:
  1156. break;
  1157. }
  1158. }
  1159. m_out->Print( 0, "\n");
  1160. m_out->Print( aNestLevel+1, ")" ); // end of (basic_shapes
  1161. }
  1162. m_out->Print( 0, ")\n" );
  1163. }
  1164. void PCB_IO::format( TEXTE_PCB* aText, int aNestLevel ) const
  1165. {
  1166. m_out->Print( aNestLevel, "(gr_text %s (at %s",
  1167. m_out->Quotew( aText->GetText() ).c_str(),
  1168. FMT_IU( aText->GetTextPos() ).c_str() );
  1169. if( aText->GetTextAngle() != 0.0 )
  1170. m_out->Print( 0, " %s", FMT_ANGLE( aText->GetTextAngle() ).c_str() );
  1171. m_out->Print( 0, ")" );
  1172. formatLayer( aText );
  1173. if( aText->GetTimeStamp() )
  1174. m_out->Print( 0, " (tstamp %lX)", (unsigned long)aText->GetTimeStamp() );
  1175. m_out->Print( 0, "\n" );
  1176. aText->EDA_TEXT::Format( m_out, aNestLevel, m_ctl );
  1177. m_out->Print( aNestLevel, ")\n" );
  1178. }
  1179. void PCB_IO::format( TEXTE_MODULE* aText, int aNestLevel ) const
  1180. {
  1181. wxString type;
  1182. switch( aText->GetType() )
  1183. {
  1184. case TEXTE_MODULE::TEXT_is_REFERENCE: type = "reference"; break;
  1185. case TEXTE_MODULE::TEXT_is_VALUE: type = "value"; break;
  1186. case TEXTE_MODULE::TEXT_is_DIVERS: type = "user";
  1187. }
  1188. m_out->Print( aNestLevel, "(fp_text %s %s (at %s",
  1189. m_out->Quotew( type ).c_str(),
  1190. m_out->Quotew( aText->GetText() ).c_str(),
  1191. FMT_IU( aText->GetPos0() ).c_str() );
  1192. // Due to Pcbnew history, fp_text angle is saved as an absolute on screen angle,
  1193. // but internally the angle is held relative to its parent footprint. parent
  1194. // may be NULL when saving a footprint outside a BOARD.
  1195. double orient = aText->GetTextAngle();
  1196. MODULE* parent = (MODULE*) aText->GetParent();
  1197. if( parent )
  1198. {
  1199. // GetTextAngle() is always in -360..+360 range because of
  1200. // TEXTE_MODULE::SetTextAngle(), but summing that angle with an
  1201. // additional board angle could kick sum up >= 360 or <= -360, so to have
  1202. // consistent results, normalize again for the BOARD save. A footprint
  1203. // save does not use this code path since parent is NULL.
  1204. #if 0
  1205. // This one could be considered reasonable if you like positive angles
  1206. // in your board text.
  1207. orient = NormalizeAnglePos( orient + parent->GetOrientation() );
  1208. #else
  1209. // Choose compatibility for now, even though this is only a 720 degree clamp
  1210. // with two possible values for every angle.
  1211. orient = NormalizeAngle360Min( orient + parent->GetOrientation() );
  1212. #endif
  1213. }
  1214. if( orient != 0.0 )
  1215. m_out->Print( 0, " %s", FMT_ANGLE( orient ).c_str() );
  1216. if( aText->IsUnlocked() )
  1217. m_out->Print( 0, " unlocked" );
  1218. m_out->Print( 0, ")" );
  1219. formatLayer( aText );
  1220. if( !aText->IsVisible() )
  1221. m_out->Print( 0, " hide" );
  1222. m_out->Print( 0, "\n" );
  1223. aText->EDA_TEXT::Format( m_out, aNestLevel, m_ctl | CTL_OMIT_HIDE );
  1224. m_out->Print( aNestLevel, ")\n" );
  1225. }
  1226. void PCB_IO::format( TRACK* aTrack, int aNestLevel ) const
  1227. {
  1228. if( aTrack->Type() == PCB_VIA_T )
  1229. {
  1230. PCB_LAYER_ID layer1, layer2;
  1231. const VIA* via = static_cast<const VIA*>( aTrack );
  1232. BOARD* board = (BOARD*) via->GetParent();
  1233. wxCHECK_RET( board != 0, wxT( "Via " ) + via->GetSelectMenuText() +
  1234. wxT( " has no parent." ) );
  1235. m_out->Print( aNestLevel, "(via" );
  1236. via->LayerPair( &layer1, &layer2 );
  1237. switch( via->GetViaType() )
  1238. {
  1239. case VIA_THROUGH: // Default shape not saved.
  1240. break;
  1241. case VIA_BLIND_BURIED:
  1242. m_out->Print( 0, " blind" );
  1243. break;
  1244. case VIA_MICROVIA:
  1245. m_out->Print( 0, " micro" );
  1246. break;
  1247. default:
  1248. THROW_IO_ERROR( wxString::Format( _( "unknown via type %d" ), via->GetViaType() ) );
  1249. }
  1250. m_out->Print( 0, " (at %s) (size %s)",
  1251. FMT_IU( aTrack->GetStart() ).c_str(),
  1252. FMT_IU( aTrack->GetWidth() ).c_str() );
  1253. if( via->GetDrill() != UNDEFINED_DRILL_DIAMETER )
  1254. m_out->Print( 0, " (drill %s)", FMT_IU( via->GetDrill() ).c_str() );
  1255. m_out->Print( 0, " (layers %s %s)",
  1256. m_out->Quotew( m_board->GetLayerName( layer1 ) ).c_str(),
  1257. m_out->Quotew( m_board->GetLayerName( layer2 ) ).c_str() );
  1258. }
  1259. else
  1260. {
  1261. m_out->Print( aNestLevel, "(segment (start %s) (end %s) (width %s)",
  1262. FMT_IU( aTrack->GetStart() ).c_str(), FMT_IU( aTrack->GetEnd() ).c_str(),
  1263. FMT_IU( aTrack->GetWidth() ).c_str() );
  1264. m_out->Print( 0, " (layer %s)", m_out->Quotew( aTrack->GetLayerName() ).c_str() );
  1265. }
  1266. m_out->Print( 0, " (net %d)", m_mapping->Translate( aTrack->GetNetCode() ) );
  1267. if( aTrack->GetTimeStamp() != 0 )
  1268. m_out->Print( 0, " (tstamp %lX)", (unsigned long)aTrack->GetTimeStamp() );
  1269. if( aTrack->GetStatus() != 0 )
  1270. m_out->Print( 0, " (status %X)", aTrack->GetStatus() );
  1271. m_out->Print( 0, ")\n" );
  1272. }
  1273. void PCB_IO::format( ZONE_CONTAINER* aZone, int aNestLevel ) const
  1274. {
  1275. // Save the NET info; For keepout zones, net code and net name are irrelevant
  1276. // so be sure a dummy value is stored, just for ZONE_CONTAINER compatibility
  1277. // (perhaps netcode and netname should be not stored)
  1278. m_out->Print( aNestLevel, "(zone (net %d) (net_name %s)",
  1279. aZone->GetIsKeepout() ? 0 : m_mapping->Translate( aZone->GetNetCode() ),
  1280. m_out->Quotew( aZone->GetIsKeepout() ? wxT("") : aZone->GetNetname() ).c_str() );
  1281. // If a zone exists on multiple layers, format accordingly
  1282. if( aZone->GetLayerSet().count() > 1 )
  1283. {
  1284. formatLayers( aZone->GetLayerSet() );
  1285. }
  1286. else
  1287. {
  1288. formatLayer( aZone );
  1289. }
  1290. m_out->Print( 0, " (tstamp %lX)", (unsigned long) aZone->GetTimeStamp() );
  1291. // Save the outline aux info
  1292. std::string hatch;
  1293. switch( aZone->GetHatchStyle() )
  1294. {
  1295. default:
  1296. case ZONE_CONTAINER::NO_HATCH: hatch = "none"; break;
  1297. case ZONE_CONTAINER::DIAGONAL_EDGE: hatch = "edge"; break;
  1298. case ZONE_CONTAINER::DIAGONAL_FULL: hatch = "full"; break;
  1299. }
  1300. m_out->Print( 0, " (hatch %s %s)\n", hatch.c_str(),
  1301. FMT_IU( aZone->GetHatchPitch() ).c_str() );
  1302. if( aZone->GetPriority() > 0 )
  1303. m_out->Print( aNestLevel+1, "(priority %d)\n", aZone->GetPriority() );
  1304. m_out->Print( aNestLevel+1, "(connect_pads" );
  1305. switch( aZone->GetPadConnection() )
  1306. {
  1307. default:
  1308. case PAD_ZONE_CONN_THERMAL: // Default option not saved or loaded.
  1309. break;
  1310. case PAD_ZONE_CONN_THT_THERMAL:
  1311. m_out->Print( 0, " thru_hole_only" );
  1312. break;
  1313. case PAD_ZONE_CONN_FULL:
  1314. m_out->Print( 0, " yes" );
  1315. break;
  1316. case PAD_ZONE_CONN_NONE:
  1317. m_out->Print( 0, " no" );
  1318. break;
  1319. }
  1320. m_out->Print( 0, " (clearance %s))\n",
  1321. FMT_IU( aZone->GetZoneClearance() ).c_str() );
  1322. m_out->Print( aNestLevel+1, "(min_thickness %s)\n",
  1323. FMT_IU( aZone->GetMinThickness() ).c_str() );
  1324. if( aZone->GetIsKeepout() )
  1325. {
  1326. m_out->Print( aNestLevel+1, "(keepout (tracks %s) (vias %s) (copperpour %s))\n",
  1327. aZone->GetDoNotAllowTracks() ? "not_allowed" : "allowed",
  1328. aZone->GetDoNotAllowVias() ? "not_allowed" : "allowed",
  1329. aZone->GetDoNotAllowCopperPour() ? "not_allowed" : "allowed" );
  1330. }
  1331. m_out->Print( aNestLevel+1, "(fill" );
  1332. // Default is not filled.
  1333. if( aZone->IsFilled() )
  1334. m_out->Print( 0, " yes" );
  1335. // Default is polygon filled.
  1336. if( aZone->GetFillMode() )
  1337. m_out->Print( 0, " (mode segment)" );
  1338. m_out->Print( 0, " (arc_segments %d) (thermal_gap %s) (thermal_bridge_width %s)",
  1339. aZone->GetArcSegmentCount(),
  1340. FMT_IU( aZone->GetThermalReliefGap() ).c_str(),
  1341. FMT_IU( aZone->GetThermalReliefCopperBridge() ).c_str() );
  1342. if( aZone->GetCornerSmoothingType() != ZONE_SETTINGS::SMOOTHING_NONE )
  1343. {
  1344. m_out->Print( 0, " (smoothing" );
  1345. switch( aZone->GetCornerSmoothingType() )
  1346. {
  1347. case ZONE_SETTINGS::SMOOTHING_CHAMFER:
  1348. m_out->Print( 0, " chamfer" );
  1349. break;
  1350. case ZONE_SETTINGS::SMOOTHING_FILLET:
  1351. m_out->Print( 0, " fillet" );
  1352. break;
  1353. default:
  1354. THROW_IO_ERROR( wxString::Format( _( "unknown zone corner smoothing type %d" ),
  1355. aZone->GetCornerSmoothingType() ) );
  1356. }
  1357. m_out->Print( 0, ")" );
  1358. if( aZone->GetCornerRadius() != 0 )
  1359. m_out->Print( 0, " (radius %s)",
  1360. FMT_IU( aZone->GetCornerRadius() ).c_str() );
  1361. }
  1362. m_out->Print( 0, ")\n" );
  1363. int newLine = 0;
  1364. if( aZone->GetNumCorners() )
  1365. {
  1366. bool new_polygon = true;
  1367. bool is_closed = false;
  1368. for( auto iterator = aZone->IterateWithHoles(); iterator; iterator++ )
  1369. {
  1370. if( new_polygon )
  1371. {
  1372. newLine = 0;
  1373. m_out->Print( aNestLevel+1, "(polygon\n" );
  1374. m_out->Print( aNestLevel+2, "(pts\n" );
  1375. new_polygon = false;
  1376. is_closed = false;
  1377. }
  1378. if( newLine == 0 )
  1379. m_out->Print( aNestLevel+3, "(xy %s %s)",
  1380. FMT_IU( iterator->x ).c_str(), FMT_IU( iterator->y ).c_str() );
  1381. else
  1382. m_out->Print( 0, " (xy %s %s)",
  1383. FMT_IU( iterator->x ).c_str(), FMT_IU( iterator->y ).c_str() );
  1384. if( newLine < 4 )
  1385. {
  1386. newLine += 1;
  1387. }
  1388. else
  1389. {
  1390. newLine = 0;
  1391. m_out->Print( 0, "\n" );
  1392. }
  1393. if( iterator.IsEndContour() )
  1394. {
  1395. is_closed = true;
  1396. if( newLine != 0 )
  1397. m_out->Print( 0, "\n" );
  1398. m_out->Print( aNestLevel+2, ")\n" );
  1399. m_out->Print( aNestLevel+1, ")\n" );
  1400. new_polygon = true;
  1401. }
  1402. }
  1403. if( !is_closed ) // Should not happen, but...
  1404. m_out->Print( aNestLevel+1, ")\n" );
  1405. }
  1406. // Save the PolysList (filled areas)
  1407. const SHAPE_POLY_SET& fv = aZone->GetFilledPolysList();
  1408. newLine = 0;
  1409. if( !fv.IsEmpty() )
  1410. {
  1411. bool new_polygon = true;
  1412. bool is_closed = false;
  1413. for( auto it = fv.CIterate(); it; ++it )
  1414. {
  1415. if( new_polygon )
  1416. {
  1417. newLine = 0;
  1418. m_out->Print( aNestLevel+1, "(filled_polygon\n" );
  1419. m_out->Print( aNestLevel+2, "(pts\n" );
  1420. new_polygon = false;
  1421. is_closed = false;
  1422. }
  1423. if( newLine == 0 )
  1424. m_out->Print( aNestLevel+3, "(xy %s %s)",
  1425. FMT_IU( it->x ).c_str(), FMT_IU( it->y ).c_str() );
  1426. else
  1427. m_out->Print( 0, " (xy %s %s)",
  1428. FMT_IU( it->x ) .c_str(), FMT_IU( it->y ).c_str() );
  1429. if( newLine < 4 )
  1430. {
  1431. newLine += 1;
  1432. }
  1433. else
  1434. {
  1435. newLine = 0;
  1436. m_out->Print( 0, "\n" );
  1437. }
  1438. if( it.IsEndContour() )
  1439. {
  1440. is_closed = true;
  1441. if( newLine != 0 )
  1442. m_out->Print( 0, "\n" );
  1443. m_out->Print( aNestLevel+2, ")\n" );
  1444. m_out->Print( aNestLevel+1, ")\n" );
  1445. new_polygon = true;
  1446. }
  1447. }
  1448. if( !is_closed ) // Should not happen, but...
  1449. m_out->Print( aNestLevel+1, ")\n" );
  1450. }
  1451. // Save the filling segments list
  1452. const auto& segs = aZone->FillSegments();
  1453. if( segs.size() )
  1454. {
  1455. m_out->Print( aNestLevel+1, "(fill_segments\n" );
  1456. for( ZONE_SEGMENT_FILL::const_iterator it = segs.begin(); it != segs.end(); ++it )
  1457. {
  1458. m_out->Print( aNestLevel+2, "(pts (xy %s) (xy %s))\n",
  1459. FMT_IU( wxPoint( it->A ) ).c_str(),
  1460. FMT_IU( wxPoint( it->B ) ).c_str() );
  1461. }
  1462. m_out->Print( aNestLevel+1, ")\n" );
  1463. }
  1464. m_out->Print( aNestLevel, ")\n" );
  1465. }
  1466. PCB_IO::PCB_IO( int aControlFlags ) :
  1467. m_cache( 0 ),
  1468. m_ctl( aControlFlags ),
  1469. m_parser( new PCB_PARSER() ),
  1470. m_mapping( new NETINFO_MAPPING() )
  1471. {
  1472. init( 0 );
  1473. m_out = &m_sf;
  1474. }
  1475. PCB_IO::~PCB_IO()
  1476. {
  1477. delete m_cache;
  1478. delete m_parser;
  1479. delete m_mapping;
  1480. }
  1481. BOARD* PCB_IO::Load( const wxString& aFileName, BOARD* aAppendToMe, const PROPERTIES* aProperties )
  1482. {
  1483. FILE_LINE_READER reader( aFileName );
  1484. init( aProperties );
  1485. m_parser->SetLineReader( &reader );
  1486. m_parser->SetBoard( aAppendToMe );
  1487. BOARD* board;
  1488. try
  1489. {
  1490. board = dynamic_cast<BOARD*>( m_parser->Parse() );
  1491. }
  1492. catch( const FUTURE_FORMAT_ERROR& )
  1493. {
  1494. // Don't wrap a FUTURE_FORMAT_ERROR in another
  1495. throw;
  1496. }
  1497. catch( const PARSE_ERROR& parse_error )
  1498. {
  1499. if( m_parser->IsTooRecent() )
  1500. throw FUTURE_FORMAT_ERROR( parse_error, m_parser->GetRequiredVersion() );
  1501. else
  1502. throw;
  1503. }
  1504. if( !board )
  1505. {
  1506. // The parser loaded something that was valid, but wasn't a board.
  1507. THROW_PARSE_ERROR( _( "this file does not contain a PCB" ),
  1508. m_parser->CurSource(), m_parser->CurLine(),
  1509. m_parser->CurLineNumber(), m_parser->CurOffset() );
  1510. }
  1511. // Give the filename to the board if it's new
  1512. if( !aAppendToMe )
  1513. board->SetFileName( aFileName );
  1514. return board;
  1515. }
  1516. void PCB_IO::init( const PROPERTIES* aProperties )
  1517. {
  1518. m_board = NULL;
  1519. m_reader = NULL;
  1520. m_loading_format_version = SEXPR_BOARD_FILE_VERSION;
  1521. m_props = aProperties;
  1522. }
  1523. void PCB_IO::validateCache( const wxString& aLibraryPath, bool checkModified )
  1524. {
  1525. if( !m_cache || !m_cache->IsPath( aLibraryPath ) || ( checkModified && m_cache->IsModified() ) )
  1526. {
  1527. // a spectacular episode in memory management:
  1528. delete m_cache;
  1529. m_cache = new FP_CACHE( this, aLibraryPath );
  1530. m_cache->Load();
  1531. }
  1532. }
  1533. void PCB_IO::FootprintEnumerate( wxArrayString& aFootprintNames,
  1534. const wxString& aLibraryPath,
  1535. const PROPERTIES* aProperties )
  1536. {
  1537. LOCALE_IO toggle; // toggles on, then off, the C locale.
  1538. wxDir dir( aLibraryPath );
  1539. init( aProperties );
  1540. wxString errorMsg;
  1541. try
  1542. {
  1543. validateCache( aLibraryPath );
  1544. }
  1545. catch( const IO_ERROR& ioe )
  1546. {
  1547. errorMsg = ioe.What();
  1548. }
  1549. // Some of the files may have been parsed correctly so we want to add the valid files to
  1550. // the library.
  1551. const MODULE_MAP& mods = m_cache->GetModules();
  1552. for( MODULE_CITER it = mods.begin(); it != mods.end(); ++it )
  1553. {
  1554. aFootprintNames.Add( it->first );
  1555. }
  1556. if( !errorMsg.IsEmpty() )
  1557. THROW_IO_ERROR( errorMsg );
  1558. }
  1559. MODULE* PCB_IO::doLoadFootprint( const wxString& aLibraryPath,
  1560. const wxString& aFootprintName,
  1561. const PROPERTIES* aProperties,
  1562. bool checkModified )
  1563. {
  1564. LOCALE_IO toggle; // toggles on, then off, the C locale.
  1565. init( aProperties );
  1566. try
  1567. {
  1568. validateCache( aLibraryPath, checkModified );
  1569. }
  1570. catch( const IO_ERROR& )
  1571. {
  1572. // do nothing with the error
  1573. }
  1574. const MODULE_MAP& mods = m_cache->GetModules();
  1575. MODULE_CITER it = mods.find( aFootprintName );
  1576. if( it == mods.end() )
  1577. {
  1578. return NULL;
  1579. }
  1580. // copy constructor to clone the already loaded MODULE
  1581. return new MODULE( *it->second->GetModule() );
  1582. }
  1583. MODULE* PCB_IO::LoadEnumeratedFootprint( const wxString& aLibraryPath,
  1584. const wxString& aFootprintName,
  1585. const PROPERTIES* aProperties )
  1586. {
  1587. return doLoadFootprint( aLibraryPath, aFootprintName, aProperties, false );
  1588. }
  1589. MODULE* PCB_IO::FootprintLoad( const wxString& aLibraryPath, const wxString& aFootprintName,
  1590. const PROPERTIES* aProperties )
  1591. {
  1592. return doLoadFootprint( aLibraryPath, aFootprintName, aProperties, true );
  1593. }
  1594. void PCB_IO::FootprintSave( const wxString& aLibraryPath, const MODULE* aFootprint,
  1595. const PROPERTIES* aProperties )
  1596. {
  1597. LOCALE_IO toggle; // toggles on, then off, the C locale.
  1598. init( aProperties );
  1599. // In this public PLUGIN API function, we can safely assume it was
  1600. // called for saving into a library path.
  1601. m_ctl = CTL_FOR_LIBRARY;
  1602. validateCache( aLibraryPath );
  1603. if( !m_cache->IsWritable() )
  1604. {
  1605. wxString msg = wxString::Format(
  1606. _( "Library \"%s\" is read only" ),
  1607. GetChars( aLibraryPath )
  1608. );
  1609. THROW_IO_ERROR( msg );
  1610. }
  1611. wxString footprintName = aFootprint->GetFPID().GetLibItemName();
  1612. MODULE_MAP& mods = m_cache->GetModules();
  1613. // Quietly overwrite module and delete module file from path for any by same name.
  1614. wxFileName fn( aLibraryPath, aFootprint->GetFPID().GetLibItemName(),
  1615. KiCadFootprintFileExtension );
  1616. if( !fn.IsOk() )
  1617. {
  1618. THROW_IO_ERROR( wxString::Format( _( "Footprint file name \"%s\" is not valid." ),
  1619. GetChars( fn.GetFullPath() ) ) );
  1620. }
  1621. if( fn.FileExists() && !fn.IsFileWritable() )
  1622. {
  1623. THROW_IO_ERROR( wxString::Format( _( "user does not have write permission to delete file \"%s\" " ),
  1624. GetChars( fn.GetFullPath() ) ) );
  1625. }
  1626. MODULE_CITER it = mods.find( footprintName );
  1627. if( it != mods.end() )
  1628. {
  1629. wxLogTrace( traceKicadPcbPlugin, wxT( "Removing footprint library file '%s'." ),
  1630. fn.GetFullPath().GetData() );
  1631. mods.erase( footprintName );
  1632. wxRemoveFile( fn.GetFullPath() );
  1633. }
  1634. // I need my own copy for the cache
  1635. MODULE* module = new MODULE( *aFootprint );
  1636. // and it's time stamp must be 0, it should have no parent, orientation should
  1637. // be zero, and it should be on the front layer.
  1638. module->SetTimeStamp( 0 );
  1639. module->SetParent( 0 );
  1640. module->SetOrientation( 0 );
  1641. if( module->GetLayer() != F_Cu )
  1642. module->Flip( module->GetPosition() );
  1643. wxLogTrace( traceKicadPcbPlugin, wxT( "Creating s-expression footprint file: %s." ),
  1644. fn.GetFullPath().GetData() );
  1645. mods.insert( footprintName, new FP_CACHE_ITEM( module, fn ) );
  1646. m_cache->Save( module );
  1647. }
  1648. void PCB_IO::FootprintDelete( const wxString& aLibraryPath, const wxString& aFootprintName,
  1649. const PROPERTIES* aProperties )
  1650. {
  1651. LOCALE_IO toggle; // toggles on, then off, the C locale.
  1652. init( aProperties );
  1653. validateCache( aLibraryPath );
  1654. if( !m_cache->IsWritable() )
  1655. {
  1656. THROW_IO_ERROR( wxString::Format( _( "Library \"%s\" is read only" ),
  1657. aLibraryPath.GetData() ) );
  1658. }
  1659. m_cache->Remove( aFootprintName );
  1660. }
  1661. long long PCB_IO::GetLibraryTimestamp( const wxString& aLibraryPath ) const
  1662. {
  1663. // If we have no cache, return a number which won't match any stored timestamps
  1664. if( !m_cache || !m_cache->IsPath( aLibraryPath ) )
  1665. return wxDateTime::Now().GetValue().GetValue();
  1666. return m_cache->GetTimestamp();
  1667. }
  1668. void PCB_IO::FootprintLibCreate( const wxString& aLibraryPath, const PROPERTIES* aProperties )
  1669. {
  1670. if( wxDir::Exists( aLibraryPath ) )
  1671. {
  1672. THROW_IO_ERROR( wxString::Format( _( "cannot overwrite library path \"%s\"" ),
  1673. aLibraryPath.GetData() ) );
  1674. }
  1675. LOCALE_IO toggle;
  1676. init( aProperties );
  1677. delete m_cache;
  1678. m_cache = new FP_CACHE( this, aLibraryPath );
  1679. m_cache->Save();
  1680. }
  1681. bool PCB_IO::FootprintLibDelete( const wxString& aLibraryPath, const PROPERTIES* aProperties )
  1682. {
  1683. wxFileName fn;
  1684. fn.SetPath( aLibraryPath );
  1685. // Return if there is no library path to delete.
  1686. if( !fn.DirExists() )
  1687. return false;
  1688. if( !fn.IsDirWritable() )
  1689. {
  1690. THROW_IO_ERROR( wxString::Format( _( "user does not have permission to delete directory \"%s\"" ),
  1691. aLibraryPath.GetData() ) );
  1692. }
  1693. wxDir dir( aLibraryPath );
  1694. if( dir.HasSubDirs() )
  1695. {
  1696. THROW_IO_ERROR( wxString::Format( _( "library directory \"%s\" has unexpected sub-directories" ),
  1697. aLibraryPath.GetData() ) );
  1698. }
  1699. // All the footprint files must be deleted before the directory can be deleted.
  1700. if( dir.HasFiles() )
  1701. {
  1702. unsigned i;
  1703. wxFileName tmp;
  1704. wxArrayString files;
  1705. wxDir::GetAllFiles( aLibraryPath, &files );
  1706. for( i = 0; i < files.GetCount(); i++ )
  1707. {
  1708. tmp = files[i];
  1709. if( tmp.GetExt() != KiCadFootprintFileExtension )
  1710. {
  1711. THROW_IO_ERROR( wxString::Format( _( "unexpected file \"%s\" was found in library path \"%s\"" ),
  1712. files[i].GetData(), aLibraryPath.GetData() ) );
  1713. }
  1714. }
  1715. for( i = 0; i < files.GetCount(); i++ )
  1716. {
  1717. wxRemoveFile( files[i] );
  1718. }
  1719. }
  1720. wxLogTrace( traceKicadPcbPlugin, wxT( "Removing footprint library \"%s\"" ),
  1721. aLibraryPath.GetData() );
  1722. // Some of the more elaborate wxRemoveFile() crap puts up its own wxLog dialog
  1723. // we don't want that. we want bare metal portability with no UI here.
  1724. if( !wxRmdir( aLibraryPath ) )
  1725. {
  1726. THROW_IO_ERROR( wxString::Format( _( "footprint library \"%s\" cannot be deleted" ),
  1727. aLibraryPath.GetData() ) );
  1728. }
  1729. // For some reason removing a directory in Windows is not immediately updated. This delay
  1730. // prevents an error when attempting to immediately recreate the same directory when over
  1731. // writing an existing library.
  1732. #ifdef __WINDOWS__
  1733. wxMilliSleep( 250L );
  1734. #endif
  1735. if( m_cache && !m_cache->IsPath( aLibraryPath ) )
  1736. {
  1737. delete m_cache;
  1738. m_cache = NULL;
  1739. }
  1740. return true;
  1741. }
  1742. bool PCB_IO::IsFootprintLibWritable( const wxString& aLibraryPath )
  1743. {
  1744. LOCALE_IO toggle;
  1745. init( NULL );
  1746. validateCache( aLibraryPath );
  1747. return m_cache->IsWritable();
  1748. }