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.

3510 lines
110 KiB

12 years ago
12 years ago
12 years ago
12 years ago
14 years ago
14 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
14 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) 2007-2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  5. * Copyright (C) 2004 Jean-Pierre Charras, jp.charras@wanadoo.fr
  6. * Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. /*
  26. This implements loading and saving a BOARD, behind the PLUGIN interface.
  27. The definitions:
  28. *) a Board Internal Unit (BIU) is a unit of length that is used only internally
  29. to PCBNEW, and is nanometers when this work is done, but deci-mils until done.
  30. The philosophies:
  31. *) BIUs should be typed as such to distinguish them from ints. This is mostly
  32. for human readability, and having the type nearby in the source supports this readability.
  33. *) Do not assume that BIUs will always be int, doing a sscanf() into a BIU
  34. does not make sense in case the size of the BIU changes.
  35. *) variables are put onto the stack in an automatic, even when it might look
  36. more efficient to do otherwise. This is so we can seem them with a debugger.
  37. *) Global variables should not be touched from within a PLUGIN, since it will eventually
  38. be in a DLL/DSO. This includes window information too. The PLUGIN API knows
  39. nothing of wxFrame or globals and all error reporting must be done by throwing
  40. an exception.
  41. *) No wxWindowing calls are made in here, since the UI resides higher up than in here,
  42. and is going to process a bucket of detailed information thrown from down here
  43. in the form of an exception if an error happens.
  44. *) Much of what we do in this source file is for human readability, not performance.
  45. Simply avoiding strtok() more often than the old code washes out performance losses.
  46. Remember strncmp() will bail as soon as a mismatch happens, not going all the way
  47. to end of string unless a full match.
  48. *) angles are in the process of migrating to doubles, and 'int' if used, is
  49. only shortterm, and along with this a change, and transition from from
  50. "tenths of degrees" to simply "degrees" in the double (which has no problem
  51. representing any portion of a degree).
  52. */
  53. #include <cmath>
  54. #include <stdio.h>
  55. #include <string.h>
  56. #include <errno.h>
  57. #include <wx/ffile.h>
  58. #include <wx/string.h>
  59. #include <legacy_plugin.h> // implement this here
  60. #include <kicad_string.h>
  61. #include <macros.h>
  62. #include <properties.h>
  63. #include <zones.h>
  64. #include <class_board.h>
  65. #include <class_module.h>
  66. #include <class_track.h>
  67. #include <class_pcb_text.h>
  68. #include <class_zone.h>
  69. #include <class_dimension.h>
  70. #include <class_drawsegment.h>
  71. #include <class_pcb_target.h>
  72. #include <class_edge_mod.h>
  73. #include <3d_cache/3d_info.h>
  74. #include <pcb_plot_params.h>
  75. #include <pcb_plot_params_parser.h>
  76. #include <convert_to_biu.h>
  77. #include <trigo.h>
  78. #include <build_version.h>
  79. typedef LEGACY_PLUGIN::BIU BIU;
  80. #define VERSION_ERROR_FORMAT _( "File \"%s\" is format version: %d.\nI only support format version <= %d.\nPlease upgrade Pcbnew to load this file." )
  81. #define UNKNOWN_GRAPHIC_FORMAT _( "unknown graphic type: %d")
  82. #define UNKNOWN_PAD_FORMAT _( "unknown pad type: %d")
  83. #define UNKNOWN_PAD_ATTRIBUTE _( "unknown pad attribute: %d" )
  84. typedef unsigned LEG_MASK;
  85. #define FIRST_LAYER 0
  86. #define FIRST_COPPER_LAYER 0
  87. #define LAYER_N_BACK 0
  88. #define LAYER_N_2 1
  89. #define LAYER_N_3 2
  90. #define LAYER_N_4 3
  91. #define LAYER_N_5 4
  92. #define LAYER_N_6 5
  93. #define LAYER_N_7 6
  94. #define LAYER_N_8 7
  95. #define LAYER_N_9 8
  96. #define LAYER_N_10 9
  97. #define LAYER_N_11 10
  98. #define LAYER_N_12 11
  99. #define LAYER_N_13 12
  100. #define LAYER_N_14 13
  101. #define LAYER_N_15 14
  102. #define LAYER_N_FRONT 15
  103. #define LAST_COPPER_LAYER LAYER_N_FRONT
  104. #define FIRST_NON_COPPER_LAYER 16
  105. #define ADHESIVE_N_BACK 16
  106. #define ADHESIVE_N_FRONT 17
  107. #define SOLDERPASTE_N_BACK 18
  108. #define SOLDERPASTE_N_FRONT 19
  109. #define SILKSCREEN_N_BACK 20
  110. #define SILKSCREEN_N_FRONT 21
  111. #define SOLDERMASK_N_BACK 22
  112. #define SOLDERMASK_N_FRONT 23
  113. #define DRAW_N 24
  114. #define COMMENT_N 25
  115. #define ECO1_N 26
  116. #define ECO2_N 27
  117. #define EDGE_N 28
  118. #define LAST_NON_COPPER_LAYER 28
  119. // Masks to identify a layer by a bit map
  120. typedef unsigned LAYER_MSK;
  121. #define LAYER_BACK (1 << LAYER_N_BACK) ///< bit mask for copper layer
  122. #define LAYER_2 (1 << LAYER_N_2) ///< bit mask for layer 2
  123. #define LAYER_3 (1 << LAYER_N_3) ///< bit mask for layer 3
  124. #define LAYER_4 (1 << LAYER_N_4) ///< bit mask for layer 4
  125. #define LAYER_5 (1 << LAYER_N_5) ///< bit mask for layer 5
  126. #define LAYER_6 (1 << LAYER_N_6) ///< bit mask for layer 6
  127. #define LAYER_7 (1 << LAYER_N_7) ///< bit mask for layer 7
  128. #define LAYER_8 (1 << LAYER_N_8) ///< bit mask for layer 8
  129. #define LAYER_9 (1 << LAYER_N_9) ///< bit mask for layer 9
  130. #define LAYER_10 (1 << LAYER_N_10) ///< bit mask for layer 10
  131. #define LAYER_11 (1 << LAYER_N_11) ///< bit mask for layer 11
  132. #define LAYER_12 (1 << LAYER_N_12) ///< bit mask for layer 12
  133. #define LAYER_13 (1 << LAYER_N_13) ///< bit mask for layer 13
  134. #define LAYER_14 (1 << LAYER_N_14) ///< bit mask for layer 14
  135. #define LAYER_15 (1 << LAYER_N_15) ///< bit mask for layer 15
  136. #define LAYER_FRONT (1 << LAYER_N_FRONT) ///< bit mask for component layer
  137. #define ADHESIVE_LAYER_BACK (1 << ADHESIVE_N_BACK)
  138. #define ADHESIVE_LAYER_FRONT (1 << ADHESIVE_N_FRONT)
  139. #define SOLDERPASTE_LAYER_BACK (1 << SOLDERPASTE_N_BACK)
  140. #define SOLDERPASTE_LAYER_FRONT (1 << SOLDERPASTE_N_FRONT)
  141. #define SILKSCREEN_LAYER_BACK (1 << SILKSCREEN_N_BACK)
  142. #define SILKSCREEN_LAYER_FRONT (1 << SILKSCREEN_N_FRONT)
  143. #define SOLDERMASK_LAYER_BACK (1 << SOLDERMASK_N_BACK)
  144. #define SOLDERMASK_LAYER_FRONT (1 << SOLDERMASK_N_FRONT)
  145. #define DRAW_LAYER (1 << DRAW_N)
  146. #define COMMENT_LAYER (1 << COMMENT_N)
  147. #define ECO1_LAYER (1 << ECO1_N)
  148. #define ECO2_LAYER (1 << ECO2_N)
  149. #define EDGE_LAYER (1 << EDGE_N)
  150. // Helpful global layer masks:
  151. // ALL_AUX_LAYERS layers are technical layers, ALL_NO_CU_LAYERS has user
  152. // and edge layers too!
  153. #define ALL_NO_CU_LAYERS 0x1FFF0000
  154. #define ALL_CU_LAYERS 0x0000FFFF
  155. #define FRONT_TECH_LAYERS (SILKSCREEN_LAYER_FRONT | SOLDERMASK_LAYER_FRONT \
  156. | ADHESIVE_LAYER_FRONT | SOLDERPASTE_LAYER_FRONT)
  157. #define BACK_TECH_LAYERS (SILKSCREEN_LAYER_BACK | SOLDERMASK_LAYER_BACK \
  158. | ADHESIVE_LAYER_BACK | SOLDERPASTE_LAYER_BACK)
  159. #define ALL_TECH_LAYERS (FRONT_TECH_LAYERS | BACK_TECH_LAYERS)
  160. #define BACK_LAYERS (LAYER_BACK | BACK_TECH_LAYERS)
  161. #define FRONT_LAYERS (LAYER_FRONT | FRONT_TECH_LAYERS)
  162. #define ALL_USER_LAYERS (DRAW_LAYER | COMMENT_LAYER | ECO1_LAYER | ECO2_LAYER )
  163. #define NO_LAYERS 0x00000000
  164. // Old internal units definition (UI = decimil)
  165. #define PCB_LEGACY_INTERNAL_UNIT 10000
  166. /// Get the length of a string constant, at compile time
  167. #define SZ( x ) (sizeof(x)-1)
  168. static const char delims[] = " \t\r\n";
  169. static bool inline isSpace( int c ) { return strchr( delims, c ) != 0; }
  170. #define MASK(x) (1<<(x))
  171. //-----<BOARD Load Functions>---------------------------------------------------
  172. /// C string compare test for a specific length of characters.
  173. #define TESTLINE( x ) ( !strncasecmp( line, x, SZ( x ) ) && isSpace( line[SZ( x )] ) )
  174. /// C sub-string compare test for a specific length of characters.
  175. #define TESTSUBSTR( x ) ( !strncasecmp( line, x, SZ( x ) ) )
  176. #if 1
  177. #define READLINE( rdr ) rdr->ReadLine()
  178. #else
  179. /// The function and macro which follow comprise a shim which can be a
  180. /// monitor on lines of text read in from the input file.
  181. /// And it can be used as a trap.
  182. static inline char* ReadLine( LINE_READER* rdr, const char* caller )
  183. {
  184. char* ret = rdr->ReadLine();
  185. const char* line = rdr->Line();
  186. printf( "%-6u %s: %s", rdr->LineNumber(), caller, line );
  187. #if 0 // trap
  188. if( !strcmp( "loadSETUP", caller ) && !strcmp( "$EndSETUP\n", line ) )
  189. {
  190. int breakhere = 1;
  191. }
  192. #endif
  193. return ret;
  194. }
  195. #define READLINE( rdr ) ReadLine( rdr, __FUNCTION__ )
  196. #endif
  197. using std::unique_ptr;
  198. static EDA_TEXT_HJUSTIFY_T horizJustify( const char* horizontal )
  199. {
  200. if( !strcmp( "L", horizontal ) )
  201. return GR_TEXT_HJUSTIFY_LEFT;
  202. if( !strcmp( "R", horizontal ) )
  203. return GR_TEXT_HJUSTIFY_RIGHT;
  204. return GR_TEXT_HJUSTIFY_CENTER;
  205. }
  206. static EDA_TEXT_VJUSTIFY_T vertJustify( const char* vertical )
  207. {
  208. if( !strcmp( "T", vertical ) )
  209. return GR_TEXT_VJUSTIFY_TOP;
  210. if( !strcmp( "B", vertical ) )
  211. return GR_TEXT_VJUSTIFY_BOTTOM;
  212. return GR_TEXT_VJUSTIFY_CENTER;
  213. }
  214. /// Count the number of set layers in the mask
  215. inline int layerMaskCountSet( LEG_MASK aMask )
  216. {
  217. int count = 0;
  218. for( int i = 0; aMask; ++i, aMask >>= 1 )
  219. {
  220. if( aMask & 1 )
  221. ++count;
  222. }
  223. return count;
  224. }
  225. // return true if aLegacyLayerNum is a valid copper layer legacy id, therefore
  226. // top, bottom or inner activated layer
  227. inline bool is_leg_copperlayer_valid( int aCu_Count, LAYER_NUM aLegacyLayerNum )
  228. {
  229. return ( aLegacyLayerNum == LAYER_N_FRONT ) || ( aLegacyLayerNum < aCu_Count );
  230. }
  231. PCB_LAYER_ID LEGACY_PLUGIN::leg_layer2new( int cu_count, LAYER_NUM aLayerNum )
  232. {
  233. int newid;
  234. unsigned old = aLayerNum;
  235. // this is a speed critical function, be careful.
  236. if( unsigned( old ) <= unsigned( LAYER_N_FRONT ) )
  237. {
  238. if( old == LAYER_N_FRONT )
  239. newid = F_Cu;
  240. else if( old == LAYER_N_BACK )
  241. newid = B_Cu;
  242. else
  243. {
  244. newid = cu_count - 1 - old;
  245. wxASSERT( newid >= 0 );
  246. }
  247. }
  248. else
  249. {
  250. switch( old )
  251. {
  252. case ADHESIVE_N_BACK: newid = B_Adhes; break;
  253. case ADHESIVE_N_FRONT: newid = F_Adhes; break;
  254. case SOLDERPASTE_N_BACK: newid = B_Paste; break;
  255. case SOLDERPASTE_N_FRONT: newid = F_Paste; break;
  256. case SILKSCREEN_N_BACK: newid = B_SilkS; break;
  257. case SILKSCREEN_N_FRONT: newid = F_SilkS; break;
  258. case SOLDERMASK_N_BACK: newid = B_Mask; break;
  259. case SOLDERMASK_N_FRONT: newid = F_Mask; break;
  260. case DRAW_N: newid = Dwgs_User; break;
  261. case COMMENT_N: newid = Cmts_User; break;
  262. case ECO1_N: newid = Eco1_User; break;
  263. case ECO2_N: newid = Eco2_User; break;
  264. case EDGE_N: newid = Edge_Cuts; break;
  265. default:
  266. // wxASSERT( 0 );
  267. // Remap all illegal non copper layers to comment layer
  268. newid = Cmts_User;
  269. }
  270. }
  271. return PCB_LAYER_ID( newid );
  272. }
  273. LSET LEGACY_PLUGIN::leg_mask2new( int cu_count, unsigned aMask )
  274. {
  275. LSET ret;
  276. if( ( aMask & ALL_CU_LAYERS ) == ALL_CU_LAYERS )
  277. {
  278. ret = LSET::AllCuMask();
  279. aMask &= ~ALL_CU_LAYERS;
  280. }
  281. for( int i=0; aMask; ++i, aMask >>= 1 )
  282. {
  283. if( aMask & 1 )
  284. ret.set( leg_layer2new( cu_count, i ) );
  285. }
  286. return ret;
  287. }
  288. /**
  289. * Function intParse
  290. * parses an ASCII integer string with possible leading whitespace into
  291. * an integer and updates the pointer at \a out if it is not NULL, just
  292. * like "man strtol()". I can use this without casting, and its name says
  293. * what I am doing.
  294. */
  295. static inline int intParse( const char* next, const char** out = NULL )
  296. {
  297. // please just compile this and be quiet, hide casting ugliness:
  298. return (int) strtol( next, (char**) out, 10 );
  299. }
  300. /**
  301. * Function layerParse
  302. * Like intParse but returns a LAYER_NUM
  303. */
  304. static inline LAYER_NUM layerParse( const char* next, const char** out = NULL )
  305. {
  306. return intParse( next, out );
  307. }
  308. /**
  309. * Function hexParse
  310. * parses an ASCII hex integer string with possible leading whitespace into
  311. * a long integer and updates the pointer at \a out if it is not NULL, just
  312. * like "man strtol". I can use this without casting, and its name says
  313. * what I am doing.
  314. */
  315. static inline long hexParse( const char* next, const char** out = NULL )
  316. {
  317. // please just compile this and be quiet, hide casting ugliness:
  318. return strtol( next, (char**) out, 16 );
  319. }
  320. BOARD* LEGACY_PLUGIN::Load( const wxString& aFileName, BOARD* aAppendToMe,
  321. const PROPERTIES* aProperties )
  322. {
  323. LOCALE_IO toggle; // toggles on, then off, the C locale.
  324. init( aProperties );
  325. m_board = aAppendToMe ? aAppendToMe : new BOARD();
  326. // Give the filename to the board if it's new
  327. if( !aAppendToMe )
  328. m_board->SetFileName( aFileName );
  329. // delete on exception, iff I own m_board, according to aAppendToMe
  330. unique_ptr<BOARD> deleter( aAppendToMe ? NULL : m_board );
  331. FILE_LINE_READER reader( aFileName );
  332. m_reader = &reader; // member function accessibility
  333. checkVersion();
  334. loadAllSections( bool( aAppendToMe ) );
  335. deleter.release();
  336. return m_board;
  337. }
  338. void LEGACY_PLUGIN::loadAllSections( bool doAppend )
  339. {
  340. // $GENERAL section is first
  341. // $SHEETDESCR section is next
  342. // $SETUP section is next
  343. // Then follows $EQUIPOT and all the rest
  344. char* line;
  345. while( ( line = READLINE( m_reader ) ) != NULL )
  346. {
  347. // put the more frequent ones at the top, but realize TRACKs are loaded as a group
  348. if( TESTLINE( "$MODULE" ) )
  349. {
  350. unique_ptr<MODULE> module( new MODULE( m_board ) );
  351. LIB_ID fpid;
  352. std::string fpName = StrPurge( line + SZ( "$MODULE" ) );
  353. // The footprint names in legacy libraries can contain the '/' and ':'
  354. // characters which will cause the FPID parser to choke.
  355. ReplaceIllegalFileNameChars( &fpName );
  356. if( !fpName.empty() )
  357. fpid.Parse( fpName, LIB_ID::ID_PCB, true );
  358. module->SetFPID( fpid );
  359. loadMODULE( module.get() );
  360. m_board->Add( module.release(), ADD_APPEND );
  361. }
  362. else if( TESTLINE( "$DRAWSEGMENT" ) )
  363. {
  364. loadPCB_LINE();
  365. }
  366. else if( TESTLINE( "$EQUIPOT" ) )
  367. {
  368. loadNETINFO_ITEM();
  369. }
  370. else if( TESTLINE( "$TEXTPCB" ) )
  371. {
  372. loadPCB_TEXT();
  373. }
  374. else if( TESTLINE( "$TRACK" ) )
  375. {
  376. loadTrackList( PCB_TRACE_T );
  377. }
  378. else if( TESTLINE( "$NCLASS" ) )
  379. {
  380. loadNETCLASS();
  381. }
  382. else if( TESTLINE( "$CZONE_OUTLINE" ) )
  383. {
  384. loadZONE_CONTAINER();
  385. }
  386. else if( TESTLINE( "$COTATION" ) )
  387. {
  388. loadDIMENSION();
  389. }
  390. else if( TESTLINE( "$PCB_TARGET" ) || TESTLINE( "$MIREPCB" ) )
  391. {
  392. loadPCB_TARGET();
  393. }
  394. else if( TESTLINE( "$ZONE" ) )
  395. {
  396. loadTrackList( PCB_SEGZONE_T );
  397. }
  398. else if( TESTLINE( "$GENERAL" ) )
  399. {
  400. loadGENERAL();
  401. }
  402. else if( TESTLINE( "$SHEETDESCR" ) )
  403. {
  404. loadSHEET();
  405. }
  406. else if( TESTLINE( "$SETUP" ) )
  407. {
  408. if( !doAppend )
  409. {
  410. loadSETUP();
  411. }
  412. else
  413. {
  414. while( ( line = READLINE( m_reader ) ) != NULL )
  415. {
  416. // gobble until $EndSetup
  417. if( TESTLINE( "$EndSETUP" ) )
  418. break;
  419. }
  420. }
  421. }
  422. else if( TESTLINE( "$EndBOARD" ) )
  423. return; // preferred exit
  424. }
  425. THROW_IO_ERROR( "Missing '$EndBOARD'" );
  426. }
  427. void LEGACY_PLUGIN::checkVersion()
  428. {
  429. // Read first line and TEST if it is a PCB file format header like this:
  430. // "PCBNEW-BOARD Version 1 ...."
  431. m_reader->ReadLine();
  432. char* line = m_reader->Line();
  433. if( !TESTLINE( "PCBNEW-BOARD" ) )
  434. {
  435. THROW_IO_ERROR( "Unknown file type" );
  436. }
  437. int ver = 1; // if sccanf fails
  438. sscanf( line, "PCBNEW-BOARD Version %d", &ver );
  439. #if !defined(DEBUG)
  440. if( ver > LEGACY_BOARD_FILE_VERSION )
  441. {
  442. // "File \"%s\" is format version: %d.\nI only support format version <= %d.\nPlease upgrade Pcbnew to load this file."
  443. m_error.Printf( VERSION_ERROR_FORMAT,
  444. m_reader->GetSource().GetData(), ver, LEGACY_BOARD_FILE_VERSION );
  445. THROW_IO_ERROR( m_error );
  446. }
  447. #endif
  448. m_loading_format_version = ver;
  449. m_board->SetFileFormatVersionAtLoad( m_loading_format_version );
  450. }
  451. void LEGACY_PLUGIN::loadGENERAL()
  452. {
  453. char* line;
  454. char* saveptr;
  455. bool saw_LayerCount = false;
  456. while( ( line = READLINE( m_reader ) ) != NULL )
  457. {
  458. const char* data;
  459. if( TESTLINE( "Units" ) )
  460. {
  461. // what are the engineering units of the lengths in the BOARD?
  462. data = strtok_r( line + SZ("Units"), delims, &saveptr );
  463. if( !strcmp( data, "mm" ) )
  464. {
  465. diskToBiu = IU_PER_MM;
  466. }
  467. }
  468. else if( TESTLINE( "LayerCount" ) )
  469. {
  470. int tmp = intParse( line + SZ( "LayerCount" ) );
  471. m_board->SetCopperLayerCount( tmp );
  472. // This has to be set early so that leg_layer2new() works OK, and
  473. // that means before parsing "EnabledLayers" and "VisibleLayers".
  474. m_cu_count = tmp;
  475. saw_LayerCount = true;
  476. }
  477. else if( TESTLINE( "EnabledLayers" ) )
  478. {
  479. if( !saw_LayerCount )
  480. THROW_IO_ERROR( "Missing '$GENERAL's LayerCount" );
  481. LEG_MASK enabledLayers = hexParse( line + SZ( "EnabledLayers" ) );
  482. LSET new_mask = leg_mask2new( m_cu_count, enabledLayers );
  483. //DBG( printf( "EnabledLayers: %s\n", new_mask.FmtHex().c_str() );)
  484. m_board->SetEnabledLayers( new_mask );
  485. // layer visibility equals layer usage, unless overridden later via "VisibleLayers"
  486. // Must call SetEnabledLayers() before calling SetVisibleLayers().
  487. m_board->SetVisibleLayers( new_mask );
  488. }
  489. else if( TESTLINE( "VisibleLayers" ) )
  490. {
  491. if( !saw_LayerCount )
  492. THROW_IO_ERROR( "Missing '$GENERAL's LayerCount" );
  493. LEG_MASK visibleLayers = hexParse( line + SZ( "VisibleLayers" ) );
  494. LSET new_mask = leg_mask2new( m_cu_count, visibleLayers );
  495. m_board->SetVisibleLayers( new_mask );
  496. }
  497. else if( TESTLINE( "Ly" ) ) // Old format for Layer count
  498. {
  499. if( !saw_LayerCount )
  500. {
  501. LEG_MASK layer_mask = hexParse( line + SZ( "Ly" ) );
  502. m_cu_count = layerMaskCountSet( layer_mask & ALL_CU_LAYERS );
  503. m_board->SetCopperLayerCount( m_cu_count );
  504. saw_LayerCount = true;
  505. }
  506. }
  507. else if( TESTLINE( "BoardThickness" ) )
  508. {
  509. BIU thickn = biuParse( line + SZ( "BoardThickness" ) );
  510. m_board->GetDesignSettings().SetBoardThickness( thickn );
  511. }
  512. /*
  513. else if( TESTLINE( "Links" ) )
  514. {
  515. // Info only, do nothing, but only for a short while.
  516. }
  517. */
  518. else if( TESTLINE( "NoConn" ) )
  519. {
  520. // ignore
  521. intParse( line + SZ( "NoConn" ) );
  522. }
  523. else if( TESTLINE( "Di" ) )
  524. {
  525. biuParse( line + SZ( "Di" ), &data );
  526. biuParse( data, &data );
  527. biuParse( data, &data );
  528. biuParse( data );
  529. }
  530. /* This is no more usefull, so this info is no more parsed
  531. // Read the number of segments of type DRAW, TRACK, ZONE
  532. else if( TESTLINE( "Ndraw" ) )
  533. {
  534. NbDraw = intParse( line + SZ( "Ndraw" ) );
  535. }
  536. else if( TESTLINE( "Ntrack" ) )
  537. {
  538. NbTrack = intParse( line + SZ( "Ntrack" ) );
  539. }
  540. else if( TESTLINE( "Nzone" ) )
  541. {
  542. NbZone = intParse( line + SZ( "Nzone" ) );
  543. }
  544. else if( TESTLINE( "Nmodule" ) )
  545. {
  546. NbMod = intParse( line + SZ( "Nmodule" ) );
  547. }*/
  548. else if( TESTLINE( "Nnets" ) )
  549. {
  550. m_netCodes.resize( intParse( line + SZ( "Nnets" ) ) );
  551. }
  552. else if( TESTLINE( "Nn" ) ) // id "Nnets" for old .brd files
  553. {
  554. m_netCodes.resize( intParse( line + SZ( "Nn" ) ) );
  555. }
  556. else if( TESTLINE( "$EndGENERAL" ) )
  557. return; // preferred exit
  558. }
  559. THROW_IO_ERROR( "Missing '$EndGENERAL'" );
  560. }
  561. void LEGACY_PLUGIN::loadSHEET()
  562. {
  563. char buf[260];
  564. TITLE_BLOCK tb;
  565. char* line;
  566. char* saveptr;
  567. while( ( line = READLINE( m_reader ) ) != NULL )
  568. {
  569. if( TESTLINE( "Sheet" ) )
  570. {
  571. // e.g. "Sheet A3 16535 11700"
  572. // width and height are in 1/1000th of an inch, always
  573. PAGE_INFO page;
  574. char* sname = strtok_r( line + SZ( "Sheet" ), delims, &saveptr );
  575. if( sname )
  576. {
  577. wxString wname = FROM_UTF8( sname );
  578. if( !page.SetType( wname ) )
  579. {
  580. m_error.Printf( _( "Unknown sheet type \"%s\" on line:%d" ),
  581. wname.GetData(), m_reader->LineNumber() );
  582. THROW_IO_ERROR( m_error );
  583. }
  584. char* width = strtok_r( NULL, delims, &saveptr );
  585. char* height = strtok_r( NULL, delims, &saveptr );
  586. char* orient = strtok_r( NULL, delims, &saveptr );
  587. // only parse the width and height if page size is custom ("User")
  588. if( wname == PAGE_INFO::Custom )
  589. {
  590. if( width && height )
  591. {
  592. // legacy disk file describes paper in mils
  593. // (1/1000th of an inch)
  594. int w = intParse( width );
  595. int h = intParse( height );
  596. page.SetWidthMils( w );
  597. page.SetHeightMils( h );
  598. }
  599. }
  600. if( orient && !strcmp( orient, "portrait" ) )
  601. {
  602. page.SetPortrait( true );
  603. }
  604. m_board->SetPageSettings( page );
  605. }
  606. }
  607. else if( TESTLINE( "Title" ) )
  608. {
  609. ReadDelimitedText( buf, line, sizeof(buf) );
  610. tb.SetTitle( FROM_UTF8( buf ) );
  611. }
  612. else if( TESTLINE( "Date" ) )
  613. {
  614. ReadDelimitedText( buf, line, sizeof(buf) );
  615. tb.SetDate( FROM_UTF8( buf ) );
  616. }
  617. else if( TESTLINE( "Rev" ) )
  618. {
  619. ReadDelimitedText( buf, line, sizeof(buf) );
  620. tb.SetRevision( FROM_UTF8( buf ) );
  621. }
  622. else if( TESTLINE( "Comp" ) )
  623. {
  624. ReadDelimitedText( buf, line, sizeof(buf) );
  625. tb.SetCompany( FROM_UTF8( buf ) );
  626. }
  627. else if( TESTLINE( "Comment1" ) )
  628. {
  629. ReadDelimitedText( buf, line, sizeof(buf) );
  630. tb.SetComment1( FROM_UTF8( buf ) );
  631. }
  632. else if( TESTLINE( "Comment2" ) )
  633. {
  634. ReadDelimitedText( buf, line, sizeof(buf) );
  635. tb.SetComment2( FROM_UTF8( buf ) );
  636. }
  637. else if( TESTLINE( "Comment3" ) )
  638. {
  639. ReadDelimitedText( buf, line, sizeof(buf) );
  640. tb.SetComment3( FROM_UTF8( buf ) );
  641. }
  642. else if( TESTLINE( "Comment4" ) )
  643. {
  644. ReadDelimitedText( buf, line, sizeof(buf) );
  645. tb.SetComment4( FROM_UTF8( buf ) );
  646. }
  647. else if( TESTLINE( "$EndSHEETDESCR" ) )
  648. {
  649. m_board->SetTitleBlock( tb );
  650. return; // preferred exit
  651. }
  652. }
  653. THROW_IO_ERROR( "Missing '$EndSHEETDESCR'" );
  654. }
  655. void LEGACY_PLUGIN::loadSETUP()
  656. {
  657. NETCLASSPTR netclass_default = m_board->GetDesignSettings().GetDefault();
  658. // TODO Orson: is it really necessary to first operate on a copy and then apply it?
  659. // would not it be better to use reference here and apply all the changes instantly?
  660. BOARD_DESIGN_SETTINGS bds = m_board->GetDesignSettings();
  661. ZONE_SETTINGS zs = m_board->GetZoneSettings();
  662. char* line;
  663. char* saveptr;
  664. while( ( line = READLINE( m_reader ) ) != NULL )
  665. {
  666. const char* data;
  667. if( TESTLINE( "PcbPlotParams" ) )
  668. {
  669. PCB_PLOT_PARAMS plot_opts;
  670. PCB_PLOT_PARAMS_PARSER parser( line + SZ( "PcbPlotParams" ), m_reader->GetSource() );
  671. plot_opts.Parse( &parser );
  672. m_board->SetPlotOptions( plot_opts );
  673. }
  674. else if( TESTLINE( "AuxiliaryAxisOrg" ) )
  675. {
  676. BIU gx = biuParse( line + SZ( "AuxiliaryAxisOrg" ), &data );
  677. BIU gy = biuParse( data );
  678. // m_board->SetAuxOrigin( wxPoint( gx, gy ) ); gets overwritten by SetDesignSettings() below
  679. bds.m_AuxOrigin = wxPoint( gx, gy );
  680. }
  681. /* Done from $General above's "LayerCount"
  682. else if( TESTLINE( "Layers" ) )
  683. {
  684. int tmp = intParse( line + SZ( "Layers" ) );
  685. m_board->SetCopperLayerCount( tmp );
  686. m_cu_count = tmp;
  687. }
  688. */
  689. else if( TESTSUBSTR( "Layer[" ) )
  690. {
  691. // eg: "Layer[n] <a_Layer_name_with_no_spaces> <LAYER_T>"
  692. LAYER_NUM layer_num = layerParse( line + SZ( "Layer[" ), &data );
  693. PCB_LAYER_ID layer_id = leg_layer2new( m_cu_count, layer_num );
  694. /*
  695. switch( layer_num )
  696. {
  697. case LAYER_N_BACK:
  698. layer_id = B_Cu;
  699. break;
  700. case LAYER_N_FRONT:
  701. layer_id = F_Cu;
  702. break;
  703. default:
  704. layer_id = PCB_LAYER_ID( layer_num );
  705. }
  706. */
  707. data = strtok_r( (char*) data+1, delims, &saveptr ); // +1 for ']'
  708. if( data )
  709. {
  710. wxString layerName = FROM_UTF8( data );
  711. m_board->SetLayerName( layer_id, layerName );
  712. data = strtok_r( NULL, delims, &saveptr );
  713. if( data ) // optional in old board files
  714. {
  715. LAYER_T type = LAYER::ParseType( data );
  716. m_board->SetLayerType( layer_id, type );
  717. }
  718. }
  719. }
  720. else if( TESTLINE( "TrackWidth" ) )
  721. {
  722. BIU tmp = biuParse( line + SZ( "TrackWidth" ) );
  723. netclass_default->SetTrackWidth( tmp );
  724. }
  725. else if( TESTLINE( "TrackWidthList" ) )
  726. {
  727. BIU tmp = biuParse( line + SZ( "TrackWidthList" ) );
  728. bds.m_TrackWidthList.push_back( tmp );
  729. }
  730. else if( TESTLINE( "TrackClearence" ) )
  731. {
  732. BIU tmp = biuParse( line + SZ( "TrackClearence" ) );
  733. netclass_default->SetClearance( tmp );
  734. }
  735. else if( TESTLINE( "TrackMinWidth" ) )
  736. {
  737. BIU tmp = biuParse( line + SZ( "TrackMinWidth" ) );
  738. bds.m_TrackMinWidth = tmp;
  739. }
  740. else if( TESTLINE( "ZoneClearence" ) )
  741. {
  742. BIU tmp = biuParse( line + SZ( "ZoneClearence" ) );
  743. zs.m_ZoneClearance = tmp;
  744. }
  745. else if( TESTLINE( "Zone_45_Only" ) )
  746. {
  747. bool tmp = (bool) intParse( line + SZ( "Zone_45_Only" ) );
  748. zs.m_Zone_45_Only = tmp;
  749. }
  750. else if( TESTLINE( "DrawSegmWidth" ) )
  751. {
  752. BIU tmp = biuParse( line + SZ( "DrawSegmWidth" ) );
  753. bds.m_LineThickness[ LAYER_CLASS_COPPER ] = tmp;
  754. }
  755. else if( TESTLINE( "EdgeSegmWidth" ) )
  756. {
  757. BIU tmp = biuParse( line + SZ( "EdgeSegmWidth" ) );
  758. bds.m_LineThickness[ LAYER_CLASS_EDGES ] = tmp;
  759. }
  760. else if( TESTLINE( "ViaMinSize" ) )
  761. {
  762. BIU tmp = biuParse( line + SZ( "ViaMinSize" ) );
  763. bds.m_ViasMinSize = tmp;
  764. }
  765. else if( TESTLINE( "MicroViaMinSize" ) )
  766. {
  767. BIU tmp = biuParse( line + SZ( "MicroViaMinSize" ) );
  768. bds.m_MicroViasMinSize = tmp;
  769. }
  770. else if( TESTLINE( "ViaSizeList" ) )
  771. {
  772. // e.g. "ViaSizeList DIAMETER [DRILL]"
  773. BIU drill = 0;
  774. BIU diameter = biuParse( line + SZ( "ViaSizeList" ), &data );
  775. data = strtok_r( (char*) data, delims, &saveptr );
  776. if( data ) // DRILL may not be present ?
  777. drill = biuParse( data );
  778. bds.m_ViasDimensionsList.push_back( VIA_DIMENSION( diameter,
  779. drill ) );
  780. }
  781. else if( TESTLINE( "ViaSize" ) )
  782. {
  783. BIU tmp = biuParse( line + SZ( "ViaSize" ) );
  784. netclass_default->SetViaDiameter( tmp );
  785. }
  786. else if( TESTLINE( "ViaDrill" ) )
  787. {
  788. BIU tmp = biuParse( line + SZ( "ViaDrill" ) );
  789. netclass_default->SetViaDrill( tmp );
  790. }
  791. else if( TESTLINE( "ViaMinDrill" ) )
  792. {
  793. BIU tmp = biuParse( line + SZ( "ViaMinDrill" ) );
  794. bds.m_ViasMinDrill = tmp;
  795. }
  796. else if( TESTLINE( "MicroViaSize" ) )
  797. {
  798. BIU tmp = biuParse( line + SZ( "MicroViaSize" ) );
  799. netclass_default->SetuViaDiameter( tmp );
  800. }
  801. else if( TESTLINE( "MicroViaDrill" ) )
  802. {
  803. BIU tmp = biuParse( line + SZ( "MicroViaDrill" ) );
  804. netclass_default->SetuViaDrill( tmp );
  805. }
  806. else if( TESTLINE( "MicroViaMinDrill" ) )
  807. {
  808. BIU tmp = biuParse( line + SZ( "MicroViaMinDrill" ) );
  809. bds.m_MicroViasMinDrill = tmp;
  810. }
  811. else if( TESTLINE( "MicroViasAllowed" ) )
  812. {
  813. int tmp = intParse( line + SZ( "MicroViasAllowed" ) );
  814. bds.m_MicroViasAllowed = tmp;
  815. }
  816. else if( TESTLINE( "TextPcbWidth" ) )
  817. {
  818. BIU tmp = biuParse( line + SZ( "TextPcbWidth" ) );
  819. bds.m_TextThickness[ LAYER_CLASS_COPPER ] = tmp;
  820. }
  821. else if( TESTLINE( "TextPcbSize" ) )
  822. {
  823. BIU x = biuParse( line + SZ( "TextPcbSize" ), &data );
  824. BIU y = biuParse( data );
  825. bds.m_TextSize[ LAYER_CLASS_COPPER ] = wxSize( x, y );
  826. }
  827. else if( TESTLINE( "EdgeModWidth" ) )
  828. {
  829. BIU tmp = biuParse( line + SZ( "EdgeModWidth" ) );
  830. bds.m_LineThickness[ LAYER_CLASS_SILK ] = tmp;
  831. bds.m_LineThickness[ LAYER_CLASS_OTHERS ] = tmp;
  832. }
  833. else if( TESTLINE( "TextModWidth" ) )
  834. {
  835. BIU tmp = biuParse( line + SZ( "TextModWidth" ) );
  836. bds.m_TextThickness[ LAYER_CLASS_SILK ] = tmp;
  837. bds.m_TextThickness[ LAYER_CLASS_OTHERS ] = tmp;
  838. }
  839. else if( TESTLINE( "TextModSize" ) )
  840. {
  841. BIU x = biuParse( line + SZ( "TextModSize" ), &data );
  842. BIU y = biuParse( data );
  843. bds.m_TextSize[ LAYER_CLASS_SILK ] = wxSize( x, y );
  844. bds.m_TextSize[ LAYER_CLASS_OTHERS ] = wxSize( x, y );
  845. }
  846. else if( TESTLINE( "PadSize" ) )
  847. {
  848. BIU x = biuParse( line + SZ( "PadSize" ), &data );
  849. BIU y = biuParse( data );
  850. bds.m_Pad_Master.SetSize( wxSize( x, y ) );
  851. }
  852. else if( TESTLINE( "PadDrill" ) )
  853. {
  854. BIU tmp = biuParse( line + SZ( "PadDrill" ) );
  855. bds.m_Pad_Master.SetDrillSize( wxSize( tmp, tmp ) );
  856. }
  857. else if( TESTLINE( "Pad2MaskClearance" ) )
  858. {
  859. BIU tmp = biuParse( line + SZ( "Pad2MaskClearance" ) );
  860. bds.m_SolderMaskMargin = tmp;
  861. }
  862. else if( TESTLINE( "SolderMaskMinWidth" ) )
  863. {
  864. BIU tmp = biuParse( line + SZ( "SolderMaskMinWidth" ) );
  865. bds.m_SolderMaskMinWidth = tmp;
  866. }
  867. else if( TESTLINE( "Pad2PasteClearance" ) )
  868. {
  869. BIU tmp = biuParse( line + SZ( "Pad2PasteClearance" ) );
  870. bds.m_SolderPasteMargin = tmp;
  871. }
  872. else if( TESTLINE( "Pad2PasteClearanceRatio" ) )
  873. {
  874. double ratio = atof( line + SZ( "Pad2PasteClearanceRatio" ) );
  875. bds.m_SolderPasteMarginRatio = ratio;
  876. }
  877. else if( TESTLINE( "GridOrigin" ) )
  878. {
  879. BIU x = biuParse( line + SZ( "GridOrigin" ), &data );
  880. BIU y = biuParse( data );
  881. // m_board->SetGridOrigin( wxPoint( x, y ) ); gets overwritten by SetDesignSettings() below
  882. bds.m_GridOrigin = wxPoint( x, y );
  883. }
  884. else if( TESTLINE( "VisibleElements" ) )
  885. {
  886. int visibleElements = hexParse( line + SZ( "VisibleElements" ) );
  887. bds.SetVisibleElements( visibleElements );
  888. }
  889. else if( TESTLINE( "$EndSETUP" ) )
  890. {
  891. m_board->SetDesignSettings( bds );
  892. m_board->SetZoneSettings( zs );
  893. // Very old *.brd file does not have NETCLASSes
  894. // "TrackWidth", "ViaSize", "ViaDrill", "ViaMinSize",
  895. // and "TrackClearence", were defined in SETUP
  896. // these values are put into the default NETCLASS until later board load
  897. // code should override them. *.brd files which have been
  898. // saved with knowledge of NETCLASSes will override these
  899. // defaults, very old boards (before 2009) will not and use the setup values.
  900. // However these values should be the same as default NETCLASS.
  901. return; // preferred exit
  902. }
  903. }
  904. // @todo: this code is currently unreachable, would need a goto, to get here.
  905. // that may be better handled with an #ifdef
  906. /* Ensure tracks and vias sizes lists are ok:
  907. * Sort lists by by increasing value and remove duplicates
  908. * (the first value is not tested, because it is the netclass value
  909. */
  910. BOARD_DESIGN_SETTINGS& designSettings = m_board->GetDesignSettings();
  911. sort( designSettings.m_ViasDimensionsList.begin() + 1, designSettings.m_ViasDimensionsList.end() );
  912. sort( designSettings.m_TrackWidthList.begin() + 1, designSettings.m_TrackWidthList.end() );
  913. for( unsigned ii = 1; ii < designSettings.m_ViasDimensionsList.size() - 1; ii++ )
  914. {
  915. if( designSettings.m_ViasDimensionsList[ii] == designSettings.m_ViasDimensionsList[ii + 1] )
  916. {
  917. designSettings.m_ViasDimensionsList.erase( designSettings.m_ViasDimensionsList.begin() + ii );
  918. ii--;
  919. }
  920. }
  921. for( unsigned ii = 1; ii < designSettings.m_TrackWidthList.size() - 1; ii++ )
  922. {
  923. if( designSettings.m_TrackWidthList[ii] == designSettings.m_TrackWidthList[ii + 1] )
  924. {
  925. designSettings.m_TrackWidthList.erase( designSettings.m_TrackWidthList.begin() + ii );
  926. ii--;
  927. }
  928. }
  929. }
  930. void LEGACY_PLUGIN::loadMODULE( MODULE* aModule )
  931. {
  932. char* line;
  933. char* saveptr;
  934. while( ( line = READLINE( m_reader ) ) != NULL )
  935. {
  936. const char* data;
  937. // most frequently encountered ones at the top
  938. if( TESTSUBSTR( "D" ) && strchr( "SCAP", line[1] ) ) // read a drawing item, e.g. "DS"
  939. {
  940. loadMODULE_EDGE( aModule );
  941. }
  942. else if( TESTLINE( "$PAD" ) )
  943. {
  944. loadPAD( aModule );
  945. }
  946. // Read a footprint text description (ref, value, or drawing)
  947. else if( TESTSUBSTR( "T" ) )
  948. {
  949. // e.g. "T1 6940 -16220 350 300 900 60 M I 20 N "CFCARD"\r\n"
  950. int tnum = intParse( line + SZ( "T" ) );
  951. TEXTE_MODULE* textm = 0;
  952. switch( tnum )
  953. {
  954. case TEXTE_MODULE::TEXT_is_REFERENCE:
  955. textm = &aModule->Reference();
  956. break;
  957. case TEXTE_MODULE::TEXT_is_VALUE:
  958. textm = &aModule->Value();
  959. break;
  960. // All other fields greater than 1.
  961. default:
  962. textm = new TEXTE_MODULE( aModule );
  963. aModule->GraphicalItemsList().PushBack( textm );
  964. }
  965. loadMODULE_TEXT( textm );
  966. }
  967. else if( TESTLINE( "Po" ) )
  968. {
  969. // e.g. "Po 19120 39260 900 0 4E823D06 46EAAFA5 ~~\r\n"
  970. // sscanf( PtLine, "%d %d %d %d %lX %lX %s", &m_Pos.x, &m_Pos.y, &m_Orient, &m_Layer, &m_LastEdit_Time, &m_TimeStamp, BufCar1 );
  971. BIU pos_x = biuParse( line + SZ( "Po" ), &data );
  972. BIU pos_y = biuParse( data, &data );
  973. int orient = intParse( data, &data );
  974. LAYER_NUM layer_num = layerParse( data, &data );
  975. PCB_LAYER_ID layer_id = leg_layer2new( m_cu_count, layer_num );
  976. long edittime = hexParse( data, &data );
  977. timestamp_t timestamp = hexParse( data, &data );
  978. data = strtok_r( (char*) data+1, delims, &saveptr );
  979. // data is now a two character long string
  980. // Note: some old files do not have this field
  981. if( data && data[0] == 'F' )
  982. aModule->SetLocked( true );
  983. if( data && data[1] == 'P' )
  984. aModule->SetIsPlaced( true );
  985. aModule->SetPosition( wxPoint( pos_x, pos_y ) );
  986. aModule->SetLayer( layer_id );
  987. aModule->SetOrientation( orient );
  988. aModule->SetTimeStamp( timestamp );
  989. aModule->SetLastEditTime( edittime );
  990. }
  991. /* footprint name set earlier, immediately after MODULE construction
  992. else if( TESTLINE( "Li" ) ) // Library name of footprint
  993. {
  994. // There can be whitespace in the footprint name on some old libraries.
  995. // Grab everything after "Li" up to end of line:
  996. //aModule->SetFPID( FROM_UTF8( StrPurge( line + SZ( "Li" ) ) ) );
  997. }
  998. */
  999. else if( TESTLINE( "Sc" ) ) // timestamp
  1000. {
  1001. timestamp_t timestamp = hexParse( line + SZ( "Sc" ) );
  1002. aModule->SetTimeStamp( timestamp );
  1003. }
  1004. else if( TESTLINE( "Op" ) ) // (Op)tions for auto placement
  1005. {
  1006. int itmp1 = hexParse( line + SZ( "Op" ), &data );
  1007. int itmp2 = hexParse( data );
  1008. int cntRot180 = itmp2 & 0x0F;
  1009. if( cntRot180 > 10 )
  1010. cntRot180 = 10;
  1011. aModule->SetPlacementCost180( cntRot180 );
  1012. int cntRot90 = itmp1 & 0x0F;
  1013. if( cntRot90 > 10 )
  1014. cntRot90 = 0;
  1015. itmp1 = (itmp1 >> 4) & 0x0F;
  1016. if( itmp1 > 10 )
  1017. itmp1 = 0;
  1018. aModule->SetPlacementCost90( (itmp1 << 4) | cntRot90 );
  1019. }
  1020. else if( TESTLINE( "At" ) ) // (At)tributes of module
  1021. {
  1022. int attrs = MOD_DEFAULT;
  1023. data = line + SZ( "At" );
  1024. if( strstr( data, "SMD" ) )
  1025. attrs |= MOD_CMS;
  1026. if( strstr( data, "VIRTUAL" ) )
  1027. attrs |= MOD_VIRTUAL;
  1028. aModule->SetAttributes( attrs );
  1029. }
  1030. else if( TESTLINE( "AR" ) ) // Alternate Reference
  1031. {
  1032. // e.g. "AR /47BA2624/45525076"
  1033. data = strtok_r( line + SZ( "AR" ), delims, &saveptr );
  1034. if( data )
  1035. aModule->SetPath( FROM_UTF8( data ) );
  1036. }
  1037. else if( TESTLINE( "$SHAPE3D" ) )
  1038. {
  1039. load3D( aModule );
  1040. }
  1041. else if( TESTLINE( "Cd" ) )
  1042. {
  1043. // e.g. "Cd Double rangee de contacts 2 x 4 pins\r\n"
  1044. aModule->SetDescription( FROM_UTF8( StrPurge( line + SZ( "Cd" ) ) ) );
  1045. }
  1046. else if( TESTLINE( "Kw" ) ) // Key words
  1047. {
  1048. aModule->SetKeywords( FROM_UTF8( StrPurge( line + SZ( "Kw" ) ) ) );
  1049. }
  1050. else if( TESTLINE( ".SolderPasteRatio" ) )
  1051. {
  1052. double tmp = atof( line + SZ( ".SolderPasteRatio" ) );
  1053. // Due to a bug in dialog editor in Modedit, fixed in BZR version 3565
  1054. // this parameter can be broken.
  1055. // It should be >= -50% (no solder paste) and <= 0% (full area of the pad)
  1056. if( tmp < -0.50 )
  1057. tmp = -0.50;
  1058. if( tmp > 0.0 )
  1059. tmp = 0.0;
  1060. aModule->SetLocalSolderPasteMarginRatio( tmp );
  1061. }
  1062. else if( TESTLINE( ".SolderPaste" ) )
  1063. {
  1064. BIU tmp = biuParse( line + SZ( ".SolderPaste" ) );
  1065. aModule->SetLocalSolderPasteMargin( tmp );
  1066. }
  1067. else if( TESTLINE( ".SolderMask" ) )
  1068. {
  1069. BIU tmp = biuParse( line + SZ( ".SolderMask" ) );
  1070. aModule->SetLocalSolderMaskMargin( tmp );
  1071. }
  1072. else if( TESTLINE( ".LocalClearance" ) )
  1073. {
  1074. BIU tmp = biuParse( line + SZ( ".LocalClearance" ) );
  1075. aModule->SetLocalClearance( tmp );
  1076. }
  1077. else if( TESTLINE( ".ZoneConnection" ) )
  1078. {
  1079. int tmp = intParse( line + SZ( ".ZoneConnection" ) );
  1080. aModule->SetZoneConnection( (ZoneConnection)tmp );
  1081. }
  1082. else if( TESTLINE( ".ThermalWidth" ) )
  1083. {
  1084. BIU tmp = biuParse( line + SZ( ".ThermalWidth" ) );
  1085. aModule->SetThermalWidth( tmp );
  1086. }
  1087. else if( TESTLINE( ".ThermalGap" ) )
  1088. {
  1089. BIU tmp = biuParse( line + SZ( ".ThermalGap" ) );
  1090. aModule->SetThermalGap( tmp );
  1091. }
  1092. else if( TESTLINE( "$EndMODULE" ) )
  1093. {
  1094. aModule->CalculateBoundingBox();
  1095. return; // preferred exit
  1096. }
  1097. }
  1098. wxString msg = wxString::Format(
  1099. _( "Missing '$EndMODULE' for MODULE \"%s\"" ),
  1100. GetChars( aModule->GetFPID().GetLibItemName() ) );
  1101. THROW_IO_ERROR( msg );
  1102. }
  1103. void LEGACY_PLUGIN::loadPAD( MODULE* aModule )
  1104. {
  1105. unique_ptr<D_PAD> pad( new D_PAD( aModule ) );
  1106. char* line;
  1107. char* saveptr;
  1108. while( ( line = READLINE( m_reader ) ) != NULL )
  1109. {
  1110. const char* data;
  1111. if( TESTLINE( "Sh" ) ) // (Sh)ape and padname
  1112. {
  1113. // e.g. "Sh "A2" C 520 520 0 0 900"
  1114. // or "Sh "1" R 157 1378 0 0 900"
  1115. // mypadname is LATIN1/CRYLIC for BOARD_FORMAT_VERSION 1,
  1116. // but for BOARD_FORMAT_VERSION 2, it is UTF8 from disk.
  1117. // So we have to go through two code paths. Moving forward
  1118. // padnames will be in UTF8 on disk, as are all KiCad strings on disk.
  1119. char mypadname[50];
  1120. data = line + SZ( "Sh" ) + 1; // +1 skips trailing whitespace
  1121. data = data + ReadDelimitedText( mypadname, data, sizeof(mypadname) ) + 1; // +1 trailing whitespace
  1122. // sscanf( PtLine, " %s %d %d %d %d %d", BufCar, &m_Size.x, &m_Size.y, &m_DeltaSize.x, &m_DeltaSize.y, &m_Orient );
  1123. while( isSpace( *data ) )
  1124. ++data;
  1125. unsigned char padchar = (unsigned char) *data++;
  1126. int padshape;
  1127. BIU size_x = biuParse( data, &data );
  1128. BIU size_y = biuParse( data, &data );
  1129. BIU delta_x = biuParse( data, &data );
  1130. BIU delta_y = biuParse( data, &data );
  1131. double orient = degParse( data );
  1132. switch( padchar )
  1133. {
  1134. case 'C': padshape = PAD_SHAPE_CIRCLE; break;
  1135. case 'R': padshape = PAD_SHAPE_RECT; break;
  1136. case 'O': padshape = PAD_SHAPE_OVAL; break;
  1137. case 'T': padshape = PAD_SHAPE_TRAPEZOID; break;
  1138. default:
  1139. m_error.Printf( _( "Unknown padshape '%c=0x%02x' on line: %d of footprint: \"%s\"" ),
  1140. padchar,
  1141. padchar,
  1142. m_reader->LineNumber(),
  1143. GetChars( aModule->GetFPID().GetLibItemName() )
  1144. );
  1145. THROW_IO_ERROR( m_error );
  1146. }
  1147. // go through a wxString to establish a universal character set properly
  1148. wxString padname;
  1149. if( m_loading_format_version == 1 )
  1150. {
  1151. // add 8 bit bytes, file format 1 was KiCad font type byte,
  1152. // simply promote those 8 bit bytes up into UNICODE. (subset of LATIN1)
  1153. const unsigned char* cp = (unsigned char*) mypadname;
  1154. while( *cp )
  1155. {
  1156. padname += *cp++; // unsigned, ls 8 bits only
  1157. }
  1158. }
  1159. else
  1160. {
  1161. // version 2, which is UTF8.
  1162. padname = FROM_UTF8( mypadname );
  1163. }
  1164. // chances are both were ASCII, but why take chances?
  1165. pad->SetName( padname );
  1166. pad->SetShape( PAD_SHAPE_T( padshape ) );
  1167. pad->SetSize( wxSize( size_x, size_y ) );
  1168. pad->SetDelta( wxSize( delta_x, delta_y ) );
  1169. pad->SetOrientation( orient );
  1170. }
  1171. else if( TESTLINE( "Dr" ) ) // (Dr)ill
  1172. {
  1173. // e.g. "Dr 350 0 0" or "Dr 0 0 0 O 0 0"
  1174. // sscanf( PtLine, "%d %d %d %s %d %d", &m_Drill.x, &m_Offset.x, &m_Offset.y, BufCar, &dx, &dy );
  1175. BIU drill_x = biuParse( line + SZ( "Dr" ), &data );
  1176. BIU drill_y = drill_x;
  1177. BIU offs_x = biuParse( data, &data );
  1178. BIU offs_y = biuParse( data, &data );
  1179. PAD_DRILL_SHAPE_T drShape = PAD_DRILL_SHAPE_CIRCLE;
  1180. data = strtok_r( (char*) data, delims, &saveptr );
  1181. if( data ) // optional shape
  1182. {
  1183. if( data[0] == 'O' )
  1184. {
  1185. drShape = PAD_DRILL_SHAPE_OBLONG;
  1186. data = strtok_r( NULL, delims, &saveptr );
  1187. drill_x = biuParse( data );
  1188. data = strtok_r( NULL, delims, &saveptr );
  1189. drill_y = biuParse( data );
  1190. }
  1191. }
  1192. pad->SetDrillShape( drShape );
  1193. pad->SetOffset( wxPoint( offs_x, offs_y ) );
  1194. pad->SetDrillSize( wxSize( drill_x, drill_y ) );
  1195. }
  1196. else if( TESTLINE( "At" ) ) // (At)tribute
  1197. {
  1198. // e.g. "At SMD N 00888000"
  1199. // sscanf( PtLine, "%s %s %X", BufLine, BufCar, &m_layerMask );
  1200. PAD_ATTR_T attribute;
  1201. data = strtok_r( line + SZ( "At" ), delims, &saveptr );
  1202. if( !strcmp( data, "SMD" ) )
  1203. attribute = PAD_ATTRIB_SMD;
  1204. else if( !strcmp( data, "CONN" ) )
  1205. attribute = PAD_ATTRIB_CONN;
  1206. else if( !strcmp( data, "HOLE" ) )
  1207. attribute = PAD_ATTRIB_HOLE_NOT_PLATED;
  1208. else
  1209. attribute = PAD_ATTRIB_STANDARD;
  1210. strtok_r( NULL, delims, &saveptr ); // skip BufCar
  1211. data = strtok_r( NULL, delims, &saveptr );
  1212. LEG_MASK layer_mask = hexParse( data );
  1213. pad->SetLayerSet( leg_mask2new( m_cu_count, layer_mask ) );
  1214. pad->SetAttribute( attribute );
  1215. }
  1216. else if( TESTLINE( "Ne" ) ) // (Ne)tname
  1217. {
  1218. // e.g. "Ne 461 "V5.0"
  1219. char buf[1024]; // can be fairly long
  1220. int netcode = intParse( line + SZ( "Ne" ), &data );
  1221. // Store the new code mapping
  1222. pad->SetNetCode( getNetCode( netcode ) );
  1223. // read Netname
  1224. ReadDelimitedText( buf, data, sizeof(buf) );
  1225. #ifndef NDEBUG
  1226. if( m_board )
  1227. assert( m_board->FindNet( getNetCode( netcode ) )->GetNetname() ==
  1228. FROM_UTF8( StrPurge( buf ) ) );
  1229. #endif /* NDEBUG */
  1230. }
  1231. else if( TESTLINE( "Po" ) ) // (Po)sition
  1232. {
  1233. // e.g. "Po 500 -500"
  1234. wxPoint pos;
  1235. pos.x = biuParse( line + SZ( "Po" ), &data );
  1236. pos.y = biuParse( data );
  1237. pad->SetPos0( pos );
  1238. // pad->SetPosition( pos ); set at function return
  1239. }
  1240. else if( TESTLINE( "Le" ) )
  1241. {
  1242. BIU tmp = biuParse( line + SZ( "Le" ) );
  1243. pad->SetPadToDieLength( tmp );
  1244. }
  1245. else if( TESTLINE( ".SolderMask" ) )
  1246. {
  1247. BIU tmp = biuParse( line + SZ( ".SolderMask" ) );
  1248. pad->SetLocalSolderMaskMargin( tmp );
  1249. }
  1250. else if( TESTLINE( ".SolderPasteRatio" ) )
  1251. {
  1252. double tmp = atof( line + SZ( ".SolderPasteRatio" ) );
  1253. pad->SetLocalSolderPasteMarginRatio( tmp );
  1254. }
  1255. else if( TESTLINE( ".SolderPaste" ) )
  1256. {
  1257. BIU tmp = biuParse( line + SZ( ".SolderPaste" ) );
  1258. pad->SetLocalSolderPasteMargin( tmp );
  1259. }
  1260. else if( TESTLINE( ".LocalClearance" ) )
  1261. {
  1262. BIU tmp = biuParse( line + SZ( ".LocalClearance" ) );
  1263. pad->SetLocalClearance( tmp );
  1264. }
  1265. else if( TESTLINE( ".ZoneConnection" ) )
  1266. {
  1267. int tmp = intParse( line + SZ( ".ZoneConnection" ) );
  1268. pad->SetZoneConnection( (ZoneConnection)tmp );
  1269. }
  1270. else if( TESTLINE( ".ThermalWidth" ) )
  1271. {
  1272. BIU tmp = biuParse( line + SZ( ".ThermalWidth" ) );
  1273. pad->SetThermalWidth( tmp );
  1274. }
  1275. else if( TESTLINE( ".ThermalGap" ) )
  1276. {
  1277. BIU tmp = biuParse( line + SZ( ".ThermalGap" ) );
  1278. pad->SetThermalGap( tmp );
  1279. }
  1280. else if( TESTLINE( "$EndPAD" ) )
  1281. {
  1282. // pad's "Position" is not relative to the module's,
  1283. // whereas Pos0 is relative to the module's but is the unrotated coordinate.
  1284. wxPoint padpos = pad->GetPos0();
  1285. RotatePoint( &padpos, aModule->GetOrientation() );
  1286. pad->SetPosition( padpos + aModule->GetPosition() );
  1287. aModule->PadsList().PushBack( pad.release() );
  1288. return; // preferred exit
  1289. }
  1290. }
  1291. THROW_IO_ERROR( "Missing '$EndPAD'" );
  1292. }
  1293. void LEGACY_PLUGIN::loadMODULE_EDGE( MODULE* aModule )
  1294. {
  1295. STROKE_T shape;
  1296. char* line = m_reader->Line(); // obtain current (old) line
  1297. switch( line[1] )
  1298. {
  1299. case 'S': shape = S_SEGMENT; break;
  1300. case 'C': shape = S_CIRCLE; break;
  1301. case 'A': shape = S_ARC; break;
  1302. case 'P': shape = S_POLYGON; break;
  1303. default:
  1304. m_error.Printf( _( "Unknown EDGE_MODULE type:'%c=0x%02x' on line:%d of footprint:\"%s\"" ),
  1305. (unsigned char) line[1],
  1306. (unsigned char) line[1],
  1307. m_reader->LineNumber(),
  1308. GetChars( aModule->GetFPID().GetLibItemName() )
  1309. );
  1310. THROW_IO_ERROR( m_error );
  1311. }
  1312. unique_ptr<EDGE_MODULE> dwg( new EDGE_MODULE( aModule, shape ) ); // a drawing
  1313. const char* data;
  1314. // common to all cases, and we have to check their values uniformly at end
  1315. BIU width = 1;
  1316. LAYER_NUM layer = FIRST_NON_COPPER_LAYER;
  1317. switch( shape )
  1318. {
  1319. case S_ARC:
  1320. {
  1321. // sscanf( Line + 3, "%d %d %d %d %d %d %d", &m_Start0.x, &m_Start0.y, &m_End0.x, &m_End0.y, &m_Angle, &m_Width, &m_Layer );
  1322. BIU start0_x = biuParse( line + SZ( "DA" ), &data );
  1323. BIU start0_y = biuParse( data, &data );
  1324. BIU end0_x = biuParse( data, &data );
  1325. BIU end0_y = biuParse( data, &data );
  1326. double angle = degParse( data, &data );
  1327. width = biuParse( data, &data );
  1328. layer = layerParse( data );
  1329. dwg->SetAngle( angle );
  1330. dwg->m_Start0 = wxPoint( start0_x, start0_y );
  1331. dwg->m_End0 = wxPoint( end0_x, end0_y );
  1332. }
  1333. break;
  1334. case S_SEGMENT:
  1335. case S_CIRCLE:
  1336. {
  1337. // e.g. "DS -7874 -10630 7874 -10630 50 20\r\n"
  1338. // sscanf( Line + 3, "%d %d %d %d %d %d", &m_Start0.x, &m_Start0.y, &m_End0.x, &m_End0.y, &m_Width, &m_Layer );
  1339. BIU start0_x = biuParse( line + SZ( "DS" ), &data );
  1340. BIU start0_y = biuParse( data, &data );
  1341. BIU end0_x = biuParse( data, &data );
  1342. BIU end0_y = biuParse( data, &data );
  1343. width = biuParse( data, &data );
  1344. layer = layerParse( data );
  1345. dwg->m_Start0 = wxPoint( start0_x, start0_y );
  1346. dwg->m_End0 = wxPoint( end0_x, end0_y );
  1347. }
  1348. break;
  1349. case S_POLYGON:
  1350. {
  1351. // e.g. "DP %d %d %d %d %d %d %d\n"
  1352. // sscanf( Line + 3, "%d %d %d %d %d %d %d", &m_Start0.x, &m_Start0.y, &m_End0.x, &m_End0.y, &pointCount, &m_Width, &m_Layer );
  1353. BIU start0_x = biuParse( line + SZ( "DP" ), &data );
  1354. BIU start0_y = biuParse( data, &data );
  1355. BIU end0_x = biuParse( data, &data );
  1356. BIU end0_y = biuParse( data, &data );
  1357. int ptCount = intParse( data, &data );
  1358. width = biuParse( data, &data );
  1359. layer = layerParse( data );
  1360. dwg->m_Start0 = wxPoint( start0_x, start0_y );
  1361. dwg->m_End0 = wxPoint( end0_x, end0_y );
  1362. std::vector<wxPoint> pts;
  1363. pts.reserve( ptCount );
  1364. for( int ii = 0; ii<ptCount; ++ii )
  1365. {
  1366. if( ( line = READLINE( m_reader ) ) == NULL )
  1367. {
  1368. THROW_IO_ERROR( "S_POLGON point count mismatch." );
  1369. }
  1370. // e.g. "Dl 23 44\n"
  1371. if( !TESTLINE( "Dl" ) )
  1372. {
  1373. THROW_IO_ERROR( "Missing Dl point def" );
  1374. }
  1375. BIU x = biuParse( line + SZ( "Dl" ), &data );
  1376. BIU y = biuParse( data );
  1377. pts.push_back( wxPoint( x, y ) );
  1378. }
  1379. dwg->SetPolyPoints( pts );
  1380. }
  1381. break;
  1382. default:
  1383. // first switch code above prevents us from getting here.
  1384. break;
  1385. }
  1386. // Check for a reasonable width:
  1387. /* @todo no MAX_WIDTH in out of reach header.
  1388. if( width <= 1 )
  1389. width = 1;
  1390. else if( width > MAX_WIDTH )
  1391. width = MAX_WIDTH;
  1392. */
  1393. // Check for a reasonable layer:
  1394. // m_Layer must be >= FIRST_NON_COPPER_LAYER, but because microwave footprints
  1395. // can use the copper layers m_Layer < FIRST_NON_COPPER_LAYER is allowed.
  1396. // @todo: changes use of EDGE_MODULE these footprints and allows only
  1397. // m_Layer >= FIRST_NON_COPPER_LAYER
  1398. if( layer < FIRST_LAYER || layer > LAST_NON_COPPER_LAYER )
  1399. layer = SILKSCREEN_N_FRONT;
  1400. dwg->SetWidth( width );
  1401. dwg->SetLayer( leg_layer2new( m_cu_count, layer ) );
  1402. EDGE_MODULE* em = dwg.release();
  1403. aModule->GraphicalItemsList().PushBack( em );
  1404. // this had been done at the MODULE level before, presumably because the
  1405. // EDGE_MODULE needs to be already added to a module before this function will work.
  1406. em->SetDrawCoord();
  1407. }
  1408. void LEGACY_PLUGIN::loadMODULE_TEXT( TEXTE_MODULE* aText )
  1409. {
  1410. const char* data;
  1411. const char* txt_end;
  1412. const char* line = m_reader->Line(); // current (old) line
  1413. char* saveptr;
  1414. // sscanf( line + 1, "%d %d %d %d %d %d %d %s %s %d %s",
  1415. // &type, &m_Pos0.x, &m_Pos0.y, &m_Size.y, &m_Size.x,
  1416. // &m_Orient, &m_Thickness, BufCar1, BufCar2, &layer, BufCar3 ) >= 10 )
  1417. // e.g. "T1 6940 -16220 350 300 900 60 M I 20 N "CFCARD"\r\n"
  1418. // or T1 0 500 600 400 900 80 M V 20 N"74LS245"
  1419. // ouch, the last example has no space between N and "74LS245" !
  1420. // that is an older version.
  1421. int type = intParse( line+1, &data );
  1422. BIU pos0_x = biuParse( data, &data );
  1423. BIU pos0_y = biuParse( data, &data );
  1424. BIU size0_y = biuParse( data, &data );
  1425. BIU size0_x = biuParse( data, &data );
  1426. double orient = degParse( data, &data );
  1427. BIU thickn = biuParse( data, &data );
  1428. // read the quoted text before the first call to strtok() which introduces
  1429. // NULs into the string and chops it into mutliple C strings, something
  1430. // ReadDelimitedText() cannot traverse.
  1431. // convert the "quoted, escaped, UTF8, text" to a wxString, find it by skipping
  1432. // as far forward as needed until the first double quote.
  1433. txt_end = data + ReadDelimitedText( &m_field, data );
  1434. aText->SetText( m_field );
  1435. // after switching to strtok, there's no easy coming back because of the
  1436. // embedded nul(s?) placed to the right of the current field.
  1437. // (that's the reason why strtok was deprecated...)
  1438. char* mirror = strtok_r( (char*) data, delims, &saveptr );
  1439. char* hide = strtok_r( NULL, delims, &saveptr );
  1440. char* tmp = strtok_r( NULL, delims, &saveptr );
  1441. LAYER_NUM layer_num = tmp ? layerParse( tmp ) : SILKSCREEN_N_FRONT;
  1442. char* italic = strtok_r( NULL, delims, &saveptr );
  1443. char* hjust = strtok_r( (char*) txt_end, delims, &saveptr );
  1444. char* vjust = strtok_r( NULL, delims, &saveptr );
  1445. if( type != TEXTE_MODULE::TEXT_is_REFERENCE
  1446. && type != TEXTE_MODULE::TEXT_is_VALUE )
  1447. type = TEXTE_MODULE::TEXT_is_DIVERS;
  1448. aText->SetType( static_cast<TEXTE_MODULE::TEXT_TYPE>( type ) );
  1449. aText->SetPos0( wxPoint( pos0_x, pos0_y ) );
  1450. aText->SetTextSize( wxSize( size0_x, size0_y ) );
  1451. orient -= ( static_cast<MODULE*>( aText->GetParent() ) )->GetOrientation();
  1452. aText->SetTextAngle( orient );
  1453. // @todo put in accessors?
  1454. // Set a reasonable width:
  1455. if( thickn < 1 )
  1456. thickn = 1;
  1457. /* this is better left to the dialogs UIs
  1458. aText->SetThickness( Clamp_Text_PenSize( thickn, aText->GetSize() ) );
  1459. */
  1460. aText->SetThickness( thickn );
  1461. aText->SetMirrored( mirror && *mirror == 'M' );
  1462. aText->SetVisible( !(hide && *hide == 'I') );
  1463. aText->SetItalic( italic && *italic == 'I' );
  1464. if( hjust )
  1465. aText->SetHorizJustify( horizJustify( hjust ) );
  1466. if( vjust )
  1467. aText->SetVertJustify( vertJustify( vjust ) );
  1468. if( layer_num < FIRST_LAYER )
  1469. layer_num = FIRST_LAYER;
  1470. else if( layer_num > LAST_NON_COPPER_LAYER )
  1471. layer_num = LAST_NON_COPPER_LAYER;
  1472. else if( layer_num == LAYER_N_BACK )
  1473. layer_num = SILKSCREEN_N_BACK;
  1474. else if( layer_num == LAYER_N_FRONT )
  1475. layer_num = SILKSCREEN_N_FRONT;
  1476. aText->SetLayer( leg_layer2new( m_cu_count, layer_num ) );
  1477. // Calculate the actual position.
  1478. aText->SetDrawCoord();
  1479. }
  1480. void LEGACY_PLUGIN::load3D( MODULE* aModule )
  1481. {
  1482. MODULE_3D_SETTINGS t3D;
  1483. char* line;
  1484. while( ( line = READLINE( m_reader ) ) != NULL )
  1485. {
  1486. if( TESTLINE( "Na" ) ) // Shape File Name
  1487. {
  1488. char buf[512];
  1489. ReadDelimitedText( buf, line + SZ( "Na" ), sizeof(buf) );
  1490. t3D.m_Filename = buf;
  1491. }
  1492. else if( TESTLINE( "Sc" ) ) // Scale
  1493. {
  1494. sscanf( line + SZ( "Sc" ), "%lf %lf %lf\n",
  1495. &t3D.m_Scale.x,
  1496. &t3D.m_Scale.y,
  1497. &t3D.m_Scale.z );
  1498. }
  1499. else if( TESTLINE( "Of" ) ) // Offset
  1500. {
  1501. sscanf( line + SZ( "Of" ), "%lf %lf %lf\n",
  1502. &t3D.m_Offset.x,
  1503. &t3D.m_Offset.y,
  1504. &t3D.m_Offset.z );
  1505. }
  1506. else if( TESTLINE( "Ro" ) ) // Rotation
  1507. {
  1508. sscanf( line + SZ( "Ro" ), "%lf %lf %lf\n",
  1509. &t3D.m_Rotation.x,
  1510. &t3D.m_Rotation.y,
  1511. &t3D.m_Rotation.z );
  1512. }
  1513. else if( TESTLINE( "$EndSHAPE3D" ) )
  1514. {
  1515. aModule->Models().push_back( t3D );
  1516. return; // preferred exit
  1517. }
  1518. }
  1519. THROW_IO_ERROR( "Missing '$EndSHAPE3D'" );
  1520. }
  1521. void LEGACY_PLUGIN::loadPCB_LINE()
  1522. {
  1523. /* example:
  1524. $DRAWSEGMENT
  1525. Po 0 57500 -1000 57500 0 150
  1526. De 24 0 900 0 0
  1527. $EndDRAWSEGMENT
  1528. */
  1529. unique_ptr<DRAWSEGMENT> dseg( new DRAWSEGMENT( m_board ) );
  1530. char* line;
  1531. char* saveptr;
  1532. while( ( line = READLINE( m_reader ) ) != NULL )
  1533. {
  1534. const char* data;
  1535. if( TESTLINE( "Po" ) )
  1536. {
  1537. // sscanf( line + 2, " %d %d %d %d %d %d", &m_Shape, &m_Start.x, &m_Start.y, &m_End.x, &m_End.y, &m_Width );
  1538. int shape = intParse( line + SZ( "Po" ), &data );
  1539. BIU start_x = biuParse( data, &data );
  1540. BIU start_y = biuParse( data, &data );
  1541. BIU end_x = biuParse( data, &data );
  1542. BIU end_y = biuParse( data, &data );
  1543. BIU width = biuParse( data );
  1544. if( width < 0 )
  1545. width = 0;
  1546. dseg->SetShape( STROKE_T( shape ) );
  1547. dseg->SetWidth( width );
  1548. dseg->SetStart( wxPoint( start_x, start_y ) );
  1549. dseg->SetEnd( wxPoint( end_x, end_y ) );
  1550. }
  1551. else if( TESTLINE( "De" ) )
  1552. {
  1553. BIU x = 0;
  1554. BIU y;
  1555. data = strtok_r( line + SZ( "De" ), delims, &saveptr );
  1556. for( int i = 0; data; ++i, data = strtok_r( NULL, delims, &saveptr ) )
  1557. {
  1558. switch( i )
  1559. {
  1560. case 0:
  1561. LAYER_NUM layer;
  1562. layer = layerParse( data );
  1563. if( layer < FIRST_NON_COPPER_LAYER )
  1564. layer = FIRST_NON_COPPER_LAYER;
  1565. else if( layer > LAST_NON_COPPER_LAYER )
  1566. layer = LAST_NON_COPPER_LAYER;
  1567. dseg->SetLayer( leg_layer2new( m_cu_count, layer ) );
  1568. break;
  1569. case 1:
  1570. int mtype;
  1571. mtype = intParse( data );
  1572. dseg->SetType( mtype ); // m_Type
  1573. break;
  1574. case 2:
  1575. double angle;
  1576. angle = degParse( data );
  1577. dseg->SetAngle( angle ); // m_Angle
  1578. break;
  1579. case 3:
  1580. timestamp_t timestamp;
  1581. timestamp = hexParse( data );
  1582. dseg->SetTimeStamp( timestamp );
  1583. break;
  1584. case 4:
  1585. STATUS_FLAGS state;
  1586. state = static_cast<STATUS_FLAGS>( hexParse( data ) );
  1587. dseg->SetState( state, true );
  1588. break;
  1589. // Bezier Control Points
  1590. case 5:
  1591. x = biuParse( data );
  1592. break;
  1593. case 6:
  1594. y = biuParse( data );
  1595. dseg->SetBezControl1( wxPoint( x, y ) );
  1596. break;
  1597. case 7:
  1598. x = biuParse( data );
  1599. break;
  1600. case 8:
  1601. y = biuParse( data );
  1602. dseg->SetBezControl2( wxPoint( x, y ) );
  1603. break;
  1604. default:
  1605. break;
  1606. }
  1607. }
  1608. }
  1609. else if( TESTLINE( "$EndDRAWSEGMENT" ) )
  1610. {
  1611. m_board->Add( dseg.release(), ADD_APPEND );
  1612. return; // preferred exit
  1613. }
  1614. }
  1615. THROW_IO_ERROR( "Missing '$EndDRAWSEGMENT'" );
  1616. }
  1617. void LEGACY_PLUGIN::loadNETINFO_ITEM()
  1618. {
  1619. /* a net description is something like
  1620. * $EQUIPOT
  1621. * Na 5 "/BIT1"
  1622. * St ~
  1623. * $EndEQUIPOT
  1624. */
  1625. char buf[1024];
  1626. NETINFO_ITEM* net = NULL;
  1627. char* line;
  1628. int netCode = 0;
  1629. while( ( line = READLINE( m_reader ) ) != NULL )
  1630. {
  1631. const char* data;
  1632. if( TESTLINE( "Na" ) )
  1633. {
  1634. // e.g. "Na 58 "/cpu.sch/PAD7"\r\n"
  1635. netCode = intParse( line + SZ( "Na" ), &data );
  1636. ReadDelimitedText( buf, data, sizeof(buf) );
  1637. if( net == NULL )
  1638. net = new NETINFO_ITEM( m_board, FROM_UTF8( buf ), netCode );
  1639. else
  1640. {
  1641. THROW_IO_ERROR( "Two net definitions in '$EQUIPOT' block" );
  1642. }
  1643. }
  1644. else if( TESTLINE( "$EndEQUIPOT" ) )
  1645. {
  1646. // net 0 should be already in list, so store this net
  1647. // if it is not the net 0, or if the net 0 does not exists.
  1648. if( net && ( net->GetNet() > 0 || m_board->FindNet( 0 ) == NULL ) )
  1649. {
  1650. m_board->Add( net );
  1651. // Be sure we have room to store the net in m_netCodes
  1652. if( (int)m_netCodes.size() <= netCode )
  1653. m_netCodes.resize( netCode+1 );
  1654. m_netCodes[netCode] = net->GetNet();
  1655. net = NULL;
  1656. }
  1657. else
  1658. {
  1659. delete net;
  1660. net = NULL; // Avoid double deletion.
  1661. }
  1662. return; // preferred exit
  1663. }
  1664. }
  1665. // If we are here, there is an error.
  1666. delete net;
  1667. THROW_IO_ERROR( "Missing '$EndEQUIPOT'" );
  1668. }
  1669. void LEGACY_PLUGIN::loadPCB_TEXT()
  1670. {
  1671. /* examples:
  1672. For a single line text:
  1673. ----------------------
  1674. $TEXTPCB
  1675. Te "Text example"
  1676. Po 66750 53450 600 800 150 0
  1677. De 24 1 0 Italic
  1678. $EndTEXTPCB
  1679. For a multi line text:
  1680. ---------------------
  1681. $TEXTPCB
  1682. Te "Text example"
  1683. Nl "Line 2"
  1684. Po 66750 53450 600 800 150 0
  1685. De 24 1 0 Italic
  1686. $EndTEXTPCB
  1687. Nl "line nn" is a line added to the current text
  1688. */
  1689. char text[1024];
  1690. // maybe someday a constructor that takes all this data in one call?
  1691. TEXTE_PCB* pcbtxt = new TEXTE_PCB( m_board );
  1692. m_board->Add( pcbtxt, ADD_APPEND );
  1693. char* line;
  1694. char* saveptr;
  1695. while( ( line = READLINE( m_reader ) ) != NULL )
  1696. {
  1697. const char* data;
  1698. if( TESTLINE( "Te" ) ) // Text line (or first line for multi line texts)
  1699. {
  1700. ReadDelimitedText( text, line + SZ( "Te" ), sizeof(text) );
  1701. pcbtxt->SetText( FROM_UTF8( text ) );
  1702. }
  1703. else if( TESTLINE( "nl" ) ) // next line of the current text
  1704. {
  1705. ReadDelimitedText( text, line + SZ( "nl" ), sizeof(text) );
  1706. pcbtxt->SetText( pcbtxt->GetText() + wxChar( '\n' ) + FROM_UTF8( text ) );
  1707. }
  1708. else if( TESTLINE( "Po" ) )
  1709. {
  1710. // sscanf( line + 2, " %d %d %d %d %d %d", &m_Pos.x, &m_Pos.y, &m_Size.x, &m_Size.y, &m_Thickness, &m_Orient );
  1711. wxSize size;
  1712. BIU pos_x = biuParse( line + SZ( "Po" ), &data );
  1713. BIU pos_y = biuParse( data, &data );
  1714. size.x = biuParse( data, &data );
  1715. size.y = biuParse( data, &data );
  1716. BIU thickn = biuParse( data, &data );
  1717. double angle = degParse( data );
  1718. // Ensure the text has minimal size to see this text on screen:
  1719. /* @todo wait until we are firmly in the nanometer world
  1720. if( sz.x < 5 )
  1721. sz.x = 5;
  1722. if( sz.y < 5 )
  1723. sz.y = 5;
  1724. */
  1725. pcbtxt->SetTextSize( size );
  1726. /* @todo move into an accessor
  1727. // Set a reasonable width:
  1728. if( thickn < 1 )
  1729. thickn = 1;
  1730. thickn = Clamp_Text_PenSize( thickn, size );
  1731. */
  1732. pcbtxt->SetThickness( thickn );
  1733. pcbtxt->SetTextAngle( angle );
  1734. pcbtxt->SetTextPos( wxPoint( pos_x, pos_y ) );
  1735. }
  1736. else if( TESTLINE( "De" ) )
  1737. {
  1738. // e.g. "De 21 1 0 Normal C\r\n"
  1739. // sscanf( line + 2, " %d %d %lX %s %c\n", &m_Layer, &normal_display, &m_TimeStamp, style, &hJustify );
  1740. LAYER_NUM layer_num = layerParse( line + SZ( "De" ), &data );
  1741. int notMirrored = intParse( data, &data );
  1742. timestamp_t timestamp = hexParse( data, &data );
  1743. char* style = strtok_r( (char*) data, delims, &saveptr );
  1744. char* hJustify = strtok_r( NULL, delims, &saveptr );
  1745. char* vJustify = strtok_r( NULL, delims, &saveptr );
  1746. pcbtxt->SetMirrored( !notMirrored );
  1747. pcbtxt->SetTimeStamp( timestamp );
  1748. pcbtxt->SetItalic( !strcmp( style, "Italic" ) );
  1749. if( hJustify )
  1750. pcbtxt->SetHorizJustify( horizJustify( hJustify ) );
  1751. else
  1752. {
  1753. // boom, somebody changed a constructor, I was relying on this:
  1754. wxASSERT( pcbtxt->GetHorizJustify() == GR_TEXT_HJUSTIFY_CENTER );
  1755. }
  1756. if( vJustify )
  1757. pcbtxt->SetVertJustify( vertJustify( vJustify ) );
  1758. if( layer_num < FIRST_COPPER_LAYER )
  1759. layer_num = FIRST_COPPER_LAYER;
  1760. else if( layer_num > LAST_NON_COPPER_LAYER )
  1761. layer_num = LAST_NON_COPPER_LAYER;
  1762. if( layer_num >= FIRST_NON_COPPER_LAYER ||
  1763. is_leg_copperlayer_valid( m_cu_count, layer_num ) )
  1764. pcbtxt->SetLayer( leg_layer2new( m_cu_count, layer_num ) );
  1765. else // not perfect, but putting this text on front layer is a workaround
  1766. pcbtxt->SetLayer( F_Cu );
  1767. }
  1768. else if( TESTLINE( "$EndTEXTPCB" ) )
  1769. {
  1770. return; // preferred exit
  1771. }
  1772. }
  1773. THROW_IO_ERROR( "Missing '$EndTEXTPCB'" );
  1774. }
  1775. void LEGACY_PLUGIN::loadTrackList( int aStructType )
  1776. {
  1777. char* line;
  1778. char* saveptr;
  1779. while( ( line = READLINE( m_reader ) ) != NULL )
  1780. {
  1781. // read two lines per loop iteration, each loop is one TRACK or VIA
  1782. // example first line:
  1783. // e.g. "Po 0 23994 28800 24400 28800 150 -1" for a track
  1784. // e.g. "Po 3 21086 17586 21086 17586 180 -1" for a via (uses sames start and end)
  1785. const char* data;
  1786. if( line[0] == '$' ) // $EndTRACK
  1787. return; // preferred exit
  1788. // int arg_count = sscanf( line + 2, " %d %d %d %d %d %d %d", &shape, &tempStartX, &tempStartY, &tempEndX, &tempEndY, &width, &drill );
  1789. assert( TESTLINE( "Po" ) );
  1790. VIATYPE_T viatype = static_cast<VIATYPE_T>( intParse( line + SZ( "Po" ), &data ));
  1791. BIU start_x = biuParse( data, &data );
  1792. BIU start_y = biuParse( data, &data );
  1793. BIU end_x = biuParse( data, &data );
  1794. BIU end_y = biuParse( data, &data );
  1795. BIU width = biuParse( data, &data );
  1796. // optional 7th drill parameter (must be optional in an old format?)
  1797. data = strtok_r( (char*) data, delims, &saveptr );
  1798. BIU drill = data ? biuParse( data ) : -1; // SetDefault() if < 0
  1799. // Read the 2nd line to determine the exact type, one of:
  1800. // PCB_TRACE_T, PCB_VIA_T, or PCB_SEGZONE_T. The type field in 2nd line
  1801. // differentiates between PCB_TRACE_T and PCB_VIA_T. With virtual
  1802. // functions in use, it is critical to instantiate the PCB_VIA_T
  1803. // exactly.
  1804. READLINE( m_reader );
  1805. line = m_reader->Line();
  1806. // example second line:
  1807. // "De 0 0 463 0 800000\r\n"
  1808. #if 1
  1809. assert( TESTLINE( "De" ) );
  1810. #else
  1811. if( !TESTLINE( "De" ) )
  1812. {
  1813. // mandatory 2nd line is missing
  1814. THROW_IO_ERROR( "Missing 2nd line of a TRACK def" );
  1815. }
  1816. #endif
  1817. int makeType;
  1818. unsigned long timeStamp;
  1819. LAYER_NUM layer_num;
  1820. int type, net_code, flags_int;
  1821. // parse the 2nd line to determine the type of object
  1822. // e.g. "De 15 1 7 0 0" for a via
  1823. sscanf( line + SZ( "De" ), " %d %d %d %lX %X", &layer_num, &type, &net_code,
  1824. &timeStamp, &flags_int );
  1825. STATUS_FLAGS flags;
  1826. flags = static_cast<STATUS_FLAGS>( flags_int );
  1827. if( aStructType==PCB_TRACE_T && type==1 )
  1828. makeType = PCB_VIA_T;
  1829. else
  1830. makeType = aStructType;
  1831. TRACK* newTrack;
  1832. switch( makeType )
  1833. {
  1834. default:
  1835. case PCB_TRACE_T:
  1836. newTrack = new TRACK( m_board );
  1837. break;
  1838. case PCB_VIA_T:
  1839. newTrack = new VIA( m_board );
  1840. break;
  1841. case PCB_SEGZONE_T: // this is now deprecated, but exist in old boards
  1842. newTrack = new SEGZONE( m_board );
  1843. break;
  1844. }
  1845. newTrack->SetTimeStamp( (timestamp_t)timeStamp );
  1846. newTrack->SetPosition( wxPoint( start_x, start_y ) );
  1847. newTrack->SetEnd( wxPoint( end_x, end_y ) );
  1848. newTrack->SetWidth( width );
  1849. if( makeType == PCB_VIA_T ) // Ensure layers are OK when possible:
  1850. {
  1851. VIA *via = static_cast<VIA*>( newTrack );
  1852. via->SetViaType( viatype );
  1853. if( drill < 0 )
  1854. via->SetDrillDefault();
  1855. else
  1856. via->SetDrill( drill );
  1857. if( via->GetViaType() == VIA_THROUGH )
  1858. via->SetLayerPair( F_Cu, B_Cu );
  1859. else
  1860. {
  1861. PCB_LAYER_ID back = leg_layer2new( m_cu_count, (layer_num >> 4) & 0xf );
  1862. PCB_LAYER_ID front = leg_layer2new( m_cu_count, layer_num & 0xf );
  1863. if( is_leg_copperlayer_valid( m_cu_count, back ) &&
  1864. is_leg_copperlayer_valid( m_cu_count, front ) )
  1865. via->SetLayerPair( front, back );
  1866. else
  1867. {
  1868. delete via;
  1869. newTrack = NULL;
  1870. }
  1871. }
  1872. }
  1873. else
  1874. {
  1875. // A few legacy boards can have tracks on non existent layers, because
  1876. // reducing the number of layers does not remove tracks on removed layers
  1877. // If happens, skip them
  1878. if( is_leg_copperlayer_valid( m_cu_count, layer_num ) )
  1879. newTrack->SetLayer( leg_layer2new( m_cu_count, layer_num ) );
  1880. else
  1881. {
  1882. delete newTrack;
  1883. newTrack = NULL;
  1884. }
  1885. }
  1886. if( newTrack )
  1887. {
  1888. newTrack->SetNetCode( getNetCode( net_code ) );
  1889. newTrack->SetState( flags, true );
  1890. m_board->Add( newTrack );
  1891. }
  1892. }
  1893. THROW_IO_ERROR( "Missing '$EndTRACK'" );
  1894. }
  1895. void LEGACY_PLUGIN::loadNETCLASS()
  1896. {
  1897. char buf[1024];
  1898. wxString netname;
  1899. char* line;
  1900. // create an empty NETCLASS without a name, but do not add it to the BOARD
  1901. // yet since that would bypass duplicate netclass name checking within the BOARD.
  1902. // store it temporarily in an unique_ptr until successfully inserted into the BOARD
  1903. // just before returning.
  1904. NETCLASSPTR nc = std::make_shared<NETCLASS>( wxEmptyString );
  1905. while( ( line = READLINE( m_reader ) ) != NULL )
  1906. {
  1907. if( TESTLINE( "AddNet" ) ) // most frequent type of line
  1908. {
  1909. // e.g. "AddNet "V3.3D"\n"
  1910. ReadDelimitedText( buf, line + SZ( "AddNet" ), sizeof(buf) );
  1911. netname = FROM_UTF8( buf );
  1912. nc->Add( netname );
  1913. }
  1914. else if( TESTLINE( "Clearance" ) )
  1915. {
  1916. BIU tmp = biuParse( line + SZ( "Clearance" ) );
  1917. nc->SetClearance( tmp );
  1918. }
  1919. else if( TESTLINE( "TrackWidth" ) )
  1920. {
  1921. BIU tmp = biuParse( line + SZ( "TrackWidth" ) );
  1922. nc->SetTrackWidth( tmp );
  1923. }
  1924. else if( TESTLINE( "ViaDia" ) )
  1925. {
  1926. BIU tmp = biuParse( line + SZ( "ViaDia" ) );
  1927. nc->SetViaDiameter( tmp );
  1928. }
  1929. else if( TESTLINE( "ViaDrill" ) )
  1930. {
  1931. BIU tmp = biuParse( line + SZ( "ViaDrill" ) );
  1932. nc->SetViaDrill( tmp );
  1933. }
  1934. else if( TESTLINE( "uViaDia" ) )
  1935. {
  1936. BIU tmp = biuParse( line + SZ( "uViaDia" ) );
  1937. nc->SetuViaDiameter( tmp );
  1938. }
  1939. else if( TESTLINE( "uViaDrill" ) )
  1940. {
  1941. BIU tmp = biuParse( line + SZ( "uViaDrill" ) );
  1942. nc->SetuViaDrill( tmp );
  1943. }
  1944. else if( TESTLINE( "Name" ) )
  1945. {
  1946. ReadDelimitedText( buf, line + SZ( "Name" ), sizeof(buf) );
  1947. nc->SetName( FROM_UTF8( buf ) );
  1948. }
  1949. else if( TESTLINE( "Desc" ) )
  1950. {
  1951. ReadDelimitedText( buf, line + SZ( "Desc" ), sizeof(buf) );
  1952. nc->SetDescription( FROM_UTF8( buf ) );
  1953. }
  1954. else if( TESTLINE( "$EndNCLASS" ) )
  1955. {
  1956. if( !m_board->GetDesignSettings().m_NetClasses.Add( nc ) )
  1957. {
  1958. // Must have been a name conflict, this is a bad board file.
  1959. // User may have done a hand edit to the file.
  1960. // unique_ptr will delete nc on this code path
  1961. m_error.Printf( _( "duplicate NETCLASS name \"%s\"" ), nc->GetName().GetData() );
  1962. THROW_IO_ERROR( m_error );
  1963. }
  1964. return; // preferred exit
  1965. }
  1966. }
  1967. THROW_IO_ERROR( "Missing '$EndNCLASS'" );
  1968. }
  1969. void LEGACY_PLUGIN::loadZONE_CONTAINER()
  1970. {
  1971. unique_ptr<ZONE_CONTAINER> zc( new ZONE_CONTAINER( m_board ) );
  1972. ZONE_CONTAINER::HATCH_STYLE outline_hatch = ZONE_CONTAINER::NO_HATCH;
  1973. bool endContour = false;
  1974. int holeIndex = -1; // -1 is the main outline; holeIndex >= 0 = hole index
  1975. char buf[1024];
  1976. char* line;
  1977. char* saveptr;
  1978. while( ( line = READLINE( m_reader ) ) != NULL )
  1979. {
  1980. const char* data;
  1981. if( TESTLINE( "ZCorner" ) ) // new corner of the zone outlines found
  1982. {
  1983. // e.g. "ZCorner 25650 49500 0"
  1984. BIU x = biuParse( line + SZ( "ZCorner" ), &data );
  1985. BIU y = biuParse( data, &data );
  1986. if( endContour )
  1987. {
  1988. // the previous corner was the last corner of a contour.
  1989. // so this corner is the first of a new hole
  1990. endContour = false;
  1991. zc->NewHole();
  1992. holeIndex++;
  1993. }
  1994. zc->AppendCorner( wxPoint( x, y ), holeIndex );
  1995. // Is this corner the end of current contour?
  1996. // the next corner (if any) will be stored in a new contour (a hole)
  1997. // intParse( data )returns 0 = usual corner, 1 = last corner of the current contour:
  1998. endContour = intParse( data );
  1999. }
  2000. else if( TESTLINE( "ZInfo" ) ) // general info found
  2001. {
  2002. // e.g. 'ZInfo 479194B1 310 "COMMON"'
  2003. timestamp_t timestamp = hexParse( line + SZ( "ZInfo" ), &data );
  2004. int netcode = intParse( data, &data );
  2005. if( ReadDelimitedText( buf, data, sizeof(buf) ) > (int) sizeof(buf) )
  2006. {
  2007. THROW_IO_ERROR( "ZInfo netname too long" );
  2008. }
  2009. zc->SetTimeStamp( timestamp );
  2010. // Init the net code only, not the netname, to be sure
  2011. // the zone net name is the name read in file.
  2012. // (When mismatch, the user will be prompted in DRC, to fix the actual name)
  2013. zc->BOARD_CONNECTED_ITEM::SetNetCode( getNetCode( netcode ) );
  2014. }
  2015. else if( TESTLINE( "ZLayer" ) ) // layer found
  2016. {
  2017. LAYER_NUM layer_num = layerParse( line + SZ( "ZLayer" ) );
  2018. zc->SetLayer( leg_layer2new( m_cu_count, layer_num ) );
  2019. }
  2020. else if( TESTLINE( "ZAux" ) ) // aux info found
  2021. {
  2022. // e.g. "ZAux 7 E"
  2023. int ignore = intParse( line + SZ( "ZAux" ), &data );
  2024. char* hopt = strtok_r( (char*) data, delims, &saveptr );
  2025. if( !hopt )
  2026. {
  2027. m_error.Printf( _( "Bad ZAux for CZONE_CONTAINER \"%s\"" ), zc->GetNetname().GetData() );
  2028. THROW_IO_ERROR( m_error );
  2029. }
  2030. switch( *hopt ) // upper case required
  2031. {
  2032. case 'N': outline_hatch = ZONE_CONTAINER::NO_HATCH; break;
  2033. case 'E': outline_hatch = ZONE_CONTAINER::DIAGONAL_EDGE; break;
  2034. case 'F': outline_hatch = ZONE_CONTAINER::DIAGONAL_FULL; break;
  2035. default:
  2036. m_error.Printf( _( "Bad ZAux for CZONE_CONTAINER \"%s\"" ), zc->GetNetname().GetData() );
  2037. THROW_IO_ERROR( m_error );
  2038. }
  2039. (void) ignore;
  2040. // Set hatch mode later, after reading corner outline data
  2041. }
  2042. else if( TESTLINE( "ZSmoothing" ) )
  2043. {
  2044. // e.g. "ZSmoothing 0 0"
  2045. int smoothing = intParse( line + SZ( "ZSmoothing" ), &data );
  2046. BIU cornerRadius = biuParse( data );
  2047. if( smoothing >= ZONE_SETTINGS::SMOOTHING_LAST || smoothing < 0 )
  2048. {
  2049. m_error.Printf( _( "Bad ZSmoothing for CZONE_CONTAINER \"%s\"" ), zc->GetNetname().GetData() );
  2050. THROW_IO_ERROR( m_error );
  2051. }
  2052. zc->SetCornerSmoothingType( smoothing );
  2053. zc->SetCornerRadius( cornerRadius );
  2054. }
  2055. else if( TESTLINE( "ZKeepout" ) )
  2056. {
  2057. zc->SetIsKeepout( true );
  2058. // e.g. "ZKeepout tracks N vias N pads Y"
  2059. data = strtok_r( line + SZ( "ZKeepout" ), delims, &saveptr );
  2060. while( data )
  2061. {
  2062. if( !strcmp( data, "tracks" ) )
  2063. {
  2064. data = strtok_r( NULL, delims, &saveptr );
  2065. zc->SetDoNotAllowTracks( data && *data == 'N' );
  2066. }
  2067. else if( !strcmp( data, "vias" ) )
  2068. {
  2069. data = strtok_r( NULL, delims, &saveptr );
  2070. zc->SetDoNotAllowVias( data && *data == 'N' );
  2071. }
  2072. else if( !strcmp( data, "copperpour" ) )
  2073. {
  2074. data = strtok_r( NULL, delims, &saveptr );
  2075. zc->SetDoNotAllowCopperPour( data && *data == 'N' );
  2076. }
  2077. data = strtok_r( NULL, delims, &saveptr );
  2078. }
  2079. }
  2080. else if( TESTLINE( "ZOptions" ) )
  2081. {
  2082. // e.g. "ZOptions 0 32 F 200 200"
  2083. int fillmode = intParse( line + SZ( "ZOptions" ), &data );
  2084. int arcsegcount = intParse( data, &data );
  2085. char fillstate = data[1]; // here e.g. " F"
  2086. BIU thermalReliefGap = biuParse( data += 2 , &data ); // +=2 for " F"
  2087. BIU thermalReliefCopperBridge = biuParse( data );
  2088. zc->SetFillMode( fillmode ? ZFM_SEGMENTS : ZFM_POLYGONS );
  2089. // @todo ARC_APPROX_SEGMENTS_COUNT_HIGHT_DEF: don't really want pcbnew.h
  2090. // in here, after all, its a PLUGIN and global data is evil.
  2091. // put in accessor
  2092. if( arcsegcount >= 32 )
  2093. arcsegcount = 32;
  2094. zc->SetArcSegmentCount( arcsegcount );
  2095. zc->SetIsFilled( fillstate == 'S' );
  2096. zc->SetThermalReliefGap( thermalReliefGap );
  2097. zc->SetThermalReliefCopperBridge( thermalReliefCopperBridge );
  2098. }
  2099. else if( TESTLINE( "ZClearance" ) ) // Clearance and pad options info found
  2100. {
  2101. // e.g. "ZClearance 40 I"
  2102. BIU clearance = biuParse( line + SZ( "ZClearance" ), &data );
  2103. char* padoption = strtok_r( (char*) data, delims, &saveptr ); // data: " I"
  2104. ZoneConnection popt;
  2105. switch( *padoption )
  2106. {
  2107. case 'I': popt = PAD_ZONE_CONN_FULL; break;
  2108. case 'T': popt = PAD_ZONE_CONN_THERMAL; break;
  2109. case 'H': popt = PAD_ZONE_CONN_THT_THERMAL; break;
  2110. case 'X': popt = PAD_ZONE_CONN_NONE; break;
  2111. default:
  2112. m_error.Printf( _( "Bad ZClearance padoption for CZONE_CONTAINER \"%s\"" ),
  2113. zc->GetNetname().GetData() );
  2114. THROW_IO_ERROR( m_error );
  2115. }
  2116. zc->SetZoneClearance( clearance );
  2117. zc->SetPadConnection( popt );
  2118. }
  2119. else if( TESTLINE( "ZMinThickness" ) )
  2120. {
  2121. BIU thickness = biuParse( line + SZ( "ZMinThickness" ) );
  2122. zc->SetMinThickness( thickness );
  2123. }
  2124. else if( TESTLINE( "ZPriority" ) )
  2125. {
  2126. int priority = intParse( line + SZ( "ZPriority" ) );
  2127. zc->SetPriority( priority );
  2128. }
  2129. else if( TESTLINE( "$POLYSCORNERS" ) )
  2130. {
  2131. // Read the PolysList (polygons that are the solid areas in the filled zone)
  2132. SHAPE_POLY_SET polysList;
  2133. bool makeNewOutline = true;
  2134. while( ( line = READLINE( m_reader ) ) != NULL )
  2135. {
  2136. if( TESTLINE( "$endPOLYSCORNERS" ) )
  2137. break;
  2138. // e.g. "39610 43440 0 0"
  2139. BIU x = biuParse( line, &data );
  2140. BIU y = biuParse( data, &data );
  2141. if( makeNewOutline )
  2142. polysList.NewOutline();
  2143. polysList.Append( x, y );
  2144. bool end_contour = intParse( data, &data ); // end_countour was a bool when file saved, so '0' or '1' here
  2145. intParse( data ); // skip corner utility flag
  2146. makeNewOutline = end_contour;
  2147. }
  2148. zc->SetFilledPolysList( polysList );
  2149. }
  2150. else if( TESTLINE( "$FILLSEGMENTS" ) )
  2151. {
  2152. while( ( line = READLINE( m_reader ) ) != NULL )
  2153. {
  2154. if( TESTLINE( "$endFILLSEGMENTS" ) )
  2155. break;
  2156. // e.g. ""%d %d %d %d\n"
  2157. BIU sx = biuParse( line, &data );
  2158. BIU sy = biuParse( data, &data );
  2159. BIU ex = biuParse( data, &data );
  2160. BIU ey = biuParse( data );
  2161. zc->FillSegments().push_back( SEG( VECTOR2I( sx, sy ), VECTOR2I( ex, ey ) ) );
  2162. }
  2163. }
  2164. else if( TESTLINE( "$endCZONE_OUTLINE" ) )
  2165. {
  2166. // Ensure keepout does not have a net
  2167. // (which have no sense for a keepout zone)
  2168. if( zc->GetIsKeepout() )
  2169. zc->SetNetCode( NETINFO_LIST::UNCONNECTED );
  2170. // should always occur, but who knows, a zone without two corners
  2171. // is no zone at all, it's a spot?
  2172. if( zc->GetNumCorners() > 2 )
  2173. {
  2174. if( !zc->IsOnCopperLayer() )
  2175. {
  2176. zc->SetFillMode( ZFM_POLYGONS );
  2177. zc->SetNetCode( NETINFO_LIST::UNCONNECTED );
  2178. }
  2179. // Hatch here, after outlines corners are read
  2180. // Set hatch here, after outlines corners are read
  2181. zc->SetHatch( outline_hatch, ZONE_CONTAINER::GetDefaultHatchPitch(),
  2182. true );
  2183. m_board->Add( zc.release() );
  2184. }
  2185. return; // preferred exit
  2186. }
  2187. }
  2188. THROW_IO_ERROR( "Missing '$endCZONE_OUTLINE'" );
  2189. }
  2190. void LEGACY_PLUGIN::loadDIMENSION()
  2191. {
  2192. unique_ptr<DIMENSION> dim( new DIMENSION( m_board ) );
  2193. char* line;
  2194. char* saveptr;
  2195. while( ( line = READLINE( m_reader ) ) != NULL )
  2196. {
  2197. const char* data;
  2198. if( TESTLINE( "$endCOTATION" ) )
  2199. {
  2200. m_board->Add( dim.release(), ADD_APPEND );
  2201. return; // preferred exit
  2202. }
  2203. else if( TESTLINE( "Va" ) )
  2204. {
  2205. BIU value = biuParse( line + SZ( "Va" ) );
  2206. dim->SetValue( value );
  2207. }
  2208. else if( TESTLINE( "Ge" ) )
  2209. {
  2210. LAYER_NUM layer_num;
  2211. unsigned long timestamp;
  2212. int shape;
  2213. int ilayer;
  2214. sscanf( line + SZ( "Ge" ), " %d %d %lX", &shape, &ilayer, &timestamp );
  2215. if( ilayer < FIRST_NON_COPPER_LAYER )
  2216. layer_num = FIRST_NON_COPPER_LAYER;
  2217. else if( ilayer > LAST_NON_COPPER_LAYER )
  2218. layer_num = LAST_NON_COPPER_LAYER;
  2219. else
  2220. layer_num = ilayer;
  2221. dim->SetLayer( leg_layer2new( m_cu_count, layer_num ) );
  2222. dim->SetTimeStamp( (timestamp_t) timestamp );
  2223. dim->SetShape( shape );
  2224. }
  2225. else if( TESTLINE( "Te" ) )
  2226. {
  2227. char buf[2048];
  2228. ReadDelimitedText( buf, line + SZ( "Te" ), sizeof(buf) );
  2229. dim->SetText( FROM_UTF8( buf ) );
  2230. }
  2231. else if( TESTLINE( "Po" ) )
  2232. {
  2233. // sscanf( Line + 2, " %d %d %d %d %d %d %d", &m_Text->m_Pos.x, &m_Text->m_Pos.y,
  2234. // &m_Text->m_Size.x, &m_Text->m_Size.y, &thickness, &orientation, &normal_display );
  2235. BIU pos_x = biuParse( line + SZ( "Po" ), &data );
  2236. BIU pos_y = biuParse( data, &data );
  2237. BIU width = biuParse( data, &data );
  2238. BIU height = biuParse( data, &data );
  2239. BIU thickn = biuParse( data, &data );
  2240. double orient = degParse( data, &data );
  2241. char* mirror = strtok_r( (char*) data, delims, &saveptr );
  2242. // This sets both DIMENSION's position and internal m_Text's.
  2243. // @todo: But why do we even know about internal m_Text?
  2244. dim->SetPosition( wxPoint( pos_x, pos_y ) );
  2245. dim->SetTextSize( wxSize( width, height ) );
  2246. dim->Text().SetMirrored( mirror && *mirror == '0' );
  2247. dim->Text().SetThickness( thickn );
  2248. dim->Text().SetTextAngle( orient );
  2249. }
  2250. else if( TESTLINE( "Sb" ) )
  2251. {
  2252. // sscanf( Line + 2, " %d %d %d %d %d %d", &Dummy, &m_crossBarOx, &m_crossBarOy, &m_crossBarFx, &m_crossBarFy, &m_Width );
  2253. int ignore = biuParse( line + SZ( "Sb" ), &data );
  2254. BIU crossBarOx = biuParse( data, &data );
  2255. BIU crossBarOy = biuParse( data, &data );
  2256. BIU crossBarFx = biuParse( data, &data );
  2257. BIU crossBarFy = biuParse( data, &data );
  2258. BIU width = biuParse( data );
  2259. dim->m_crossBarO.x = crossBarOx;
  2260. dim->m_crossBarO.y = crossBarOy;
  2261. dim->m_crossBarF.x = crossBarFx;
  2262. dim->m_crossBarF.y = crossBarFy;
  2263. dim->SetWidth( width );
  2264. (void) ignore;
  2265. }
  2266. else if( TESTLINE( "Sd" ) )
  2267. {
  2268. // sscanf( Line + 2, " %d %d %d %d %d %d", &Dummy, &m_featureLineDOx, &m_featureLineDOy, &m_featureLineDFx, &m_featureLineDFy, &Dummy );
  2269. int ignore = intParse( line + SZ( "Sd" ), &data );
  2270. BIU featureLineDOx = biuParse( data, &data );
  2271. BIU featureLineDOy = biuParse( data, &data );
  2272. BIU featureLineDFx = biuParse( data, &data );
  2273. BIU featureLineDFy = biuParse( data );
  2274. dim->m_featureLineDO.x = featureLineDOx;
  2275. dim->m_featureLineDO.y = featureLineDOy;
  2276. dim->m_featureLineDF.x = featureLineDFx;
  2277. dim->m_featureLineDF.y = featureLineDFy;
  2278. (void) ignore;
  2279. }
  2280. else if( TESTLINE( "Sg" ) )
  2281. {
  2282. // sscanf( Line + 2, " %d %d %d %d %d %d", &Dummy, &m_featureLineGOx, &m_featureLineGOy, &m_featureLineGFx, &m_featureLineGFy, &Dummy );
  2283. int ignore = intParse( line + SZ( "Sg" ), &data );
  2284. BIU featureLineGOx = biuParse( data, &data );
  2285. BIU featureLineGOy = biuParse( data, &data );
  2286. BIU featureLineGFx = biuParse( data, &data );
  2287. BIU featureLineGFy = biuParse( data );
  2288. dim->m_featureLineGO.x = featureLineGOx;
  2289. dim->m_featureLineGO.y = featureLineGOy;
  2290. dim->m_featureLineGF.x = featureLineGFx;
  2291. dim->m_featureLineGF.y = featureLineGFy;
  2292. (void) ignore;
  2293. }
  2294. else if( TESTLINE( "S1" ) )
  2295. {
  2296. // sscanf( Line + 2, " %d %d %d %d %d %d", &Dummy, &m_arrowD1Ox, &m_arrowD1Oy, &m_arrowD1Fx, &m_arrowD1Fy, &Dummy );
  2297. int ignore = intParse( line + SZ( "S1" ), &data );
  2298. biuParse( data, &data ); // skipping excessive data
  2299. biuParse( data, &data ); // skipping excessive data
  2300. BIU arrowD1Fx = biuParse( data, &data );
  2301. BIU arrowD1Fy = biuParse( data );
  2302. dim->m_arrowD1F.x = arrowD1Fx;
  2303. dim->m_arrowD1F.y = arrowD1Fy;
  2304. (void) ignore;
  2305. }
  2306. else if( TESTLINE( "S2" ) )
  2307. {
  2308. // sscanf( Line + 2, " %d %d %d %d %d %d", &Dummy, &m_arrowD2Ox, &m_arrowD2Oy, &m_arrowD2Fx, &m_arrowD2Fy, &Dummy );
  2309. int ignore = intParse( line + SZ( "S2" ), &data );
  2310. biuParse( data, &data ); // skipping excessive data
  2311. biuParse( data, &data ); // skipping excessive data
  2312. BIU arrowD2Fx = biuParse( data, &data );
  2313. BIU arrowD2Fy = biuParse( data, &data );
  2314. dim->m_arrowD2F.x = arrowD2Fx;
  2315. dim->m_arrowD2F.y = arrowD2Fy;
  2316. (void) ignore;
  2317. }
  2318. else if( TESTLINE( "S3" ) )
  2319. {
  2320. // sscanf( Line + 2, " %d %d %d %d %d %d\n", &Dummy, &m_arrowG1Ox, &m_arrowG1Oy, &m_arrowG1Fx, &m_arrowG1Fy, &Dummy );
  2321. int ignore = intParse( line + SZ( "S3" ), &data );
  2322. biuParse( data, &data ); // skipping excessive data
  2323. biuParse( data, &data ); // skipping excessive data
  2324. BIU arrowG1Fx = biuParse( data, &data );
  2325. BIU arrowG1Fy = biuParse( data, &data );
  2326. dim->m_arrowG1F.x = arrowG1Fx;
  2327. dim->m_arrowG1F.y = arrowG1Fy;
  2328. (void) ignore;
  2329. }
  2330. else if( TESTLINE( "S4" ) )
  2331. {
  2332. // sscanf( Line + 2, " %d %d %d %d %d %d", &Dummy, &m_arrowG2Ox, &m_arrowG2Oy, &m_arrowG2Fx, &m_arrowG2Fy, &Dummy );
  2333. int ignore = intParse( line + SZ( "S4" ), &data );
  2334. biuParse( data, &data ); // skipping excessive data
  2335. biuParse( data, &data ); // skipping excessive data
  2336. BIU arrowG2Fx = biuParse( data, &data );
  2337. BIU arrowG2Fy = biuParse( data, &data );
  2338. dim->m_arrowG2F.x = arrowG2Fx;
  2339. dim->m_arrowG2F.y = arrowG2Fy;
  2340. (void) ignore;
  2341. }
  2342. }
  2343. THROW_IO_ERROR( "Missing '$endCOTATION'" );
  2344. }
  2345. void LEGACY_PLUGIN::loadPCB_TARGET()
  2346. {
  2347. char* line;
  2348. while( ( line = READLINE( m_reader ) ) != NULL )
  2349. {
  2350. const char* data;
  2351. if( TESTLINE( "$EndPCB_TARGET" ) || TESTLINE( "$EndMIREPCB" ) )
  2352. {
  2353. return; // preferred exit
  2354. }
  2355. else if( TESTLINE( "Po" ) )
  2356. {
  2357. // sscanf( Line + 2, " %X %d %d %d %d %d %lX", &m_Shape, &m_Layer, &m_Pos.x, &m_Pos.y, &m_Size, &m_Width, &m_TimeStamp );
  2358. int shape = intParse( line + SZ( "Po" ), &data );
  2359. LAYER_NUM layer_num = layerParse( data, &data );
  2360. BIU pos_x = biuParse( data, &data );
  2361. BIU pos_y = biuParse( data, &data );
  2362. BIU size = biuParse( data, &data );
  2363. BIU width = biuParse( data, &data );
  2364. timestamp_t timestamp = hexParse( data );
  2365. if( layer_num < FIRST_NON_COPPER_LAYER )
  2366. layer_num = FIRST_NON_COPPER_LAYER;
  2367. else if( layer_num > LAST_NON_COPPER_LAYER )
  2368. layer_num = LAST_NON_COPPER_LAYER;
  2369. PCB_TARGET* t = new PCB_TARGET( m_board, shape, leg_layer2new( m_cu_count, layer_num ),
  2370. wxPoint( pos_x, pos_y ), size, width );
  2371. m_board->Add( t, ADD_APPEND );
  2372. t->SetTimeStamp( timestamp );
  2373. }
  2374. }
  2375. THROW_IO_ERROR( "Missing '$EndDIMENSION'" );
  2376. }
  2377. BIU LEGACY_PLUGIN::biuParse( const char* aValue, const char** nptrptr )
  2378. {
  2379. char* nptr;
  2380. errno = 0;
  2381. double fval = strtod( aValue, &nptr );
  2382. if( errno )
  2383. {
  2384. m_error.Printf( _( "invalid float number in file: \"%s\"\nline: %d, offset: %d" ),
  2385. m_reader->GetSource().GetData(),
  2386. m_reader->LineNumber(), aValue - m_reader->Line() + 1 );
  2387. THROW_IO_ERROR( m_error );
  2388. }
  2389. if( aValue == nptr )
  2390. {
  2391. m_error.Printf( _( "missing float number in file: \"%s\"\nline: %d, offset: %d" ),
  2392. m_reader->GetSource().GetData(),
  2393. m_reader->LineNumber(), aValue - m_reader->Line() + 1 );
  2394. THROW_IO_ERROR( m_error );
  2395. }
  2396. if( nptrptr )
  2397. *nptrptr = nptr;
  2398. fval *= diskToBiu;
  2399. // fval is up into the whole number realm here, and should be bounded
  2400. // within INT_MIN to INT_MAX since BIU's are nanometers.
  2401. return KiROUND( fval );
  2402. }
  2403. double LEGACY_PLUGIN::degParse( const char* aValue, const char** nptrptr )
  2404. {
  2405. char* nptr;
  2406. errno = 0;
  2407. double fval = strtod( aValue, &nptr );
  2408. if( errno )
  2409. {
  2410. m_error.Printf( _( "invalid float number in file: \"%s\"\nline: %d, offset: %d" ),
  2411. m_reader->GetSource().GetData(), m_reader->LineNumber(), aValue - m_reader->Line() + 1 );
  2412. THROW_IO_ERROR( m_error );
  2413. }
  2414. if( aValue == nptr )
  2415. {
  2416. m_error.Printf( _( "missing float number in file: \"%s\"\nline: %d, offset: %d" ),
  2417. m_reader->GetSource().GetData(), m_reader->LineNumber(), aValue - m_reader->Line() + 1 );
  2418. THROW_IO_ERROR( m_error );
  2419. }
  2420. if( nptrptr )
  2421. *nptrptr = nptr;
  2422. return fval;
  2423. }
  2424. void LEGACY_PLUGIN::init( const PROPERTIES* aProperties )
  2425. {
  2426. m_loading_format_version = 0;
  2427. m_cu_count = 16;
  2428. m_board = NULL;
  2429. m_props = aProperties;
  2430. // conversion factor for saving RAM BIUs to KICAD legacy file format.
  2431. biuToDisk = 1.0/IU_PER_MM; // BIUs are nanometers & file is mm
  2432. // Conversion factor for loading KICAD legacy file format into BIUs in RAM
  2433. // Start by assuming the *.brd file is in deci-mils.
  2434. // If we see "Units mm" in the $GENERAL section, set diskToBiu to 1000000.0
  2435. // then, during the file loading process, to start a conversion from
  2436. // mm to nanometers. The deci-mil legacy files have no such "Units" marker
  2437. // so we must assume the file is in deci-mils until told otherwise.
  2438. diskToBiu = IU_PER_MILS / 10; // BIUs are nanometers
  2439. }
  2440. void LEGACY_PLUGIN::SaveModule3D( const MODULE* me ) const
  2441. {
  2442. auto sM = me->Models().begin();
  2443. auto eM = me->Models().end();
  2444. while( sM != eM )
  2445. {
  2446. if( sM->m_Filename.empty() )
  2447. {
  2448. ++sM;
  2449. continue;
  2450. }
  2451. fprintf( m_fp, "$SHAPE3D\n" );
  2452. fprintf( m_fp, "Na %s\n", EscapedUTF8( sM->m_Filename ).c_str() );
  2453. fprintf(m_fp,
  2454. #if defined(DEBUG)
  2455. // use old formats for testing, just to verify compatibility
  2456. // using "diff", then switch to more concise form for release builds.
  2457. "Sc %lf %lf %lf\n",
  2458. #else
  2459. "Sc %.10g %.10g %.10g\n",
  2460. #endif
  2461. sM->m_Scale.x,
  2462. sM->m_Scale.y,
  2463. sM->m_Scale.z );
  2464. fprintf(m_fp,
  2465. #if defined(DEBUG)
  2466. "Of %lf %lf %lf\n",
  2467. #else
  2468. "Of %.10g %.10g %.10g\n",
  2469. #endif
  2470. sM->m_Offset.x,
  2471. sM->m_Offset.y,
  2472. sM->m_Offset.z );
  2473. fprintf(m_fp,
  2474. #if defined(DEBUG)
  2475. "Ro %lf %lf %lf\n",
  2476. #else
  2477. "Ro %.10g %.10g %.10g\n",
  2478. #endif
  2479. sM->m_Rotation.x,
  2480. sM->m_Rotation.y,
  2481. sM->m_Rotation.z );
  2482. fprintf( m_fp, "$EndSHAPE3D\n" );
  2483. ++sM;
  2484. }
  2485. return;
  2486. }
  2487. //-----<FOOTPRINT LIBRARY FUNCTIONS>--------------------------------------------
  2488. /*
  2489. The legacy file format is being obsoleted and this code will have a short
  2490. lifetime, so it only needs to be good enough for a short duration of time.
  2491. Caching all the MODULEs is a bit memory intensive, but it is a considerably
  2492. faster way of fulfilling the API contract. Otherwise, without the cache, you
  2493. would have to re-read the file when searching for any MODULE, and this would
  2494. be very problematic filling a FOOTPRINT_LIST via this PLUGIN API. If memory
  2495. becomes a concern, consider the cache lifetime policy, which determines the
  2496. time that a LP_CACHE is in RAM. Note PLUGIN lifetime also plays a role in
  2497. cache lifetime.
  2498. */
  2499. #include <boost/ptr_container/ptr_map.hpp>
  2500. #include <wx/filename.h>
  2501. typedef boost::ptr_map< std::string, MODULE > MODULE_MAP;
  2502. typedef MODULE_MAP::iterator MODULE_ITER;
  2503. typedef MODULE_MAP::const_iterator MODULE_CITER;
  2504. /**
  2505. * Class LP_CACHE
  2506. * assists only for the footprint portion of the PLUGIN API, and only for the
  2507. * LEGACY_PLUGIN, so therefore is private to this implementation file, i.e. not placed
  2508. * into a header.
  2509. */
  2510. struct LP_CACHE
  2511. {
  2512. LEGACY_PLUGIN* m_owner; // my owner, I need its LEGACY_PLUGIN::loadMODULE()
  2513. wxString m_lib_path;
  2514. MODULE_MAP m_modules; // map or tuple of footprint_name vs. MODULE*
  2515. bool m_writable;
  2516. bool m_cache_dirty; // Stored separately because it's expensive to check
  2517. // m_cache_timestamp against all the files.
  2518. long long m_cache_timestamp; // A hash of the timestamps for all the footprint
  2519. // files.
  2520. LP_CACHE( LEGACY_PLUGIN* aOwner, const wxString& aLibraryPath );
  2521. // Most all functions in this class throw IO_ERROR exceptions. There are no
  2522. // error codes nor user interface calls from here, nor in any PLUGIN.
  2523. // Catch these exceptions higher up please.
  2524. void Load();
  2525. void ReadAndVerifyHeader( LINE_READER* aReader );
  2526. void SkipIndex( LINE_READER* aReader );
  2527. void LoadModules( LINE_READER* aReader );
  2528. bool IsModified();
  2529. static long long GetTimestamp( const wxString& aLibPath );
  2530. };
  2531. LP_CACHE::LP_CACHE( LEGACY_PLUGIN* aOwner, const wxString& aLibraryPath ) :
  2532. m_owner( aOwner ),
  2533. m_lib_path( aLibraryPath ),
  2534. m_writable( true ),
  2535. m_cache_dirty( true ),
  2536. m_cache_timestamp( 0 )
  2537. {
  2538. }
  2539. bool LP_CACHE::IsModified()
  2540. {
  2541. m_cache_dirty = m_cache_dirty || GetTimestamp( m_lib_path ) != m_cache_timestamp;
  2542. return m_cache_dirty;
  2543. }
  2544. long long LP_CACHE::GetTimestamp( const wxString& aLibPath )
  2545. {
  2546. return wxFileName( aLibPath ).GetModificationTime().GetValue().GetValue();
  2547. }
  2548. void LP_CACHE::Load()
  2549. {
  2550. m_cache_dirty = false;
  2551. FILE_LINE_READER reader( m_lib_path );
  2552. ReadAndVerifyHeader( &reader );
  2553. SkipIndex( &reader );
  2554. LoadModules( &reader );
  2555. // Remember the file modification time of library file when the
  2556. // cache snapshot was made, so that in a networked environment we will
  2557. // reload the cache as needed.
  2558. m_cache_timestamp = GetTimestamp( m_lib_path );
  2559. }
  2560. void LP_CACHE::ReadAndVerifyHeader( LINE_READER* aReader )
  2561. {
  2562. char* line = aReader->ReadLine();
  2563. char* saveptr;
  2564. if( !line )
  2565. goto L_bad_library;
  2566. if( !TESTLINE( "PCBNEW-LibModule-V1" ) )
  2567. goto L_bad_library;
  2568. while( ( line = aReader->ReadLine() ) != NULL )
  2569. {
  2570. if( TESTLINE( "Units" ) )
  2571. {
  2572. const char* units = strtok_r( line + SZ( "Units" ), delims, &saveptr );
  2573. if( !strcmp( units, "mm" ) )
  2574. {
  2575. m_owner->diskToBiu = IU_PER_MM;
  2576. }
  2577. }
  2578. else if( TESTLINE( "$INDEX" ) )
  2579. return;
  2580. }
  2581. L_bad_library:
  2582. THROW_IO_ERROR( wxString::Format( _( "File \"%s\" is empty or is not a legacy library" ),
  2583. m_lib_path.GetData() ) );
  2584. }
  2585. void LP_CACHE::SkipIndex( LINE_READER* aReader )
  2586. {
  2587. // Some broken INDEX sections have more than one section, due to prior bugs.
  2588. // So we must read the next line after $EndINDEX tag,
  2589. // to see if this is not a new $INDEX tag.
  2590. bool exit = false;
  2591. char* line = aReader->Line();
  2592. do
  2593. {
  2594. if( TESTLINE( "$INDEX" ) )
  2595. {
  2596. exit = false;
  2597. while( ( line = aReader->ReadLine() ) != NULL )
  2598. {
  2599. if( TESTLINE( "$EndINDEX" ) )
  2600. {
  2601. exit = true;
  2602. break;
  2603. }
  2604. }
  2605. }
  2606. else if( exit )
  2607. break;
  2608. } while( ( line = aReader->ReadLine() ) != NULL );
  2609. }
  2610. void LP_CACHE::LoadModules( LINE_READER* aReader )
  2611. {
  2612. m_owner->SetReader( aReader );
  2613. char* line = aReader->Line();
  2614. do
  2615. {
  2616. // test first for the $MODULE, even before reading because of INDEX bug.
  2617. if( TESTLINE( "$MODULE" ) )
  2618. {
  2619. unique_ptr<MODULE> module( new MODULE( m_owner->m_board ) );
  2620. std::string footprintName = StrPurge( line + SZ( "$MODULE" ) );
  2621. // The footprint names in legacy libraries can contain the '/' and ':'
  2622. // characters which will cause the LIB_ID parser to choke.
  2623. ReplaceIllegalFileNameChars( &footprintName );
  2624. // set the footprint name first thing, so exceptions can use name.
  2625. module->SetFPID( LIB_ID( wxEmptyString, footprintName ) );
  2626. m_owner->loadMODULE( module.get() );
  2627. MODULE* m = module.release(); // exceptions after this are not expected.
  2628. // Not sure why this is asserting on debug builds. The debugger shows the
  2629. // strings are the same. If it's not really needed maybe it can be removed.
  2630. // wxASSERT( footprintName == m->GetFPID().GetLibItemName() );
  2631. /*
  2632. There was a bug in old legacy library management code
  2633. (pre-LEGACY_PLUGIN) which was introducing duplicate footprint names
  2634. in legacy libraries without notification. To best recover from such
  2635. bad libraries, and use them to their fullest, there are a few
  2636. strategies that could be used. (Note: footprints must have unique
  2637. names to be accepted into this cache.) The strategy used here is to
  2638. append a differentiating version counter to the end of the name as:
  2639. _v2, _v3, etc.
  2640. */
  2641. MODULE_CITER it = m_modules.find( footprintName );
  2642. if( it == m_modules.end() ) // footprintName is not present in cache yet.
  2643. {
  2644. std::pair<MODULE_ITER, bool> r = m_modules.insert( footprintName, m );
  2645. wxASSERT_MSG( r.second, wxT( "error doing cache insert using guaranteed unique name" ) );
  2646. (void) r;
  2647. }
  2648. // Bad library has a duplicate of this footprintName, generate a
  2649. // unique footprint name and load it anyway.
  2650. else
  2651. {
  2652. bool nameOK = false;
  2653. int version = 2;
  2654. char buf[48];
  2655. while( !nameOK )
  2656. {
  2657. std::string newName = footprintName;
  2658. newName += "_v";
  2659. sprintf( buf, "%d", version++ );
  2660. newName += buf;
  2661. it = m_modules.find( newName );
  2662. if( it == m_modules.end() )
  2663. {
  2664. nameOK = true;
  2665. m->SetFPID( LIB_ID( wxEmptyString, newName ) );
  2666. std::pair<MODULE_ITER, bool> r = m_modules.insert( newName, m );
  2667. wxASSERT_MSG( r.second, wxT( "error doing cache insert using guaranteed unique name" ) );
  2668. (void) r;
  2669. }
  2670. }
  2671. }
  2672. }
  2673. } while( ( line = aReader->ReadLine() ) != NULL );
  2674. }
  2675. long long LEGACY_PLUGIN::GetLibraryTimestamp( const wxString& aLibraryPath ) const
  2676. {
  2677. return LP_CACHE::GetTimestamp( aLibraryPath );
  2678. }
  2679. void LEGACY_PLUGIN::cacheLib( const wxString& aLibraryPath )
  2680. {
  2681. if( !m_cache || m_cache->m_lib_path != aLibraryPath || m_cache->IsModified() )
  2682. {
  2683. // a spectacular episode in memory management:
  2684. delete m_cache;
  2685. m_cache = new LP_CACHE( this, aLibraryPath );
  2686. m_cache->Load();
  2687. }
  2688. }
  2689. void LEGACY_PLUGIN::FootprintEnumerate( wxArrayString& aFootprintNames,
  2690. const wxString& aLibraryPath,
  2691. const PROPERTIES* aProperties )
  2692. {
  2693. LOCALE_IO toggle; // toggles on, then off, the C locale.
  2694. init( aProperties );
  2695. cacheLib( aLibraryPath );
  2696. const MODULE_MAP& mods = m_cache->m_modules;
  2697. for( MODULE_CITER it = mods.begin(); it != mods.end(); ++it )
  2698. {
  2699. aFootprintNames.Add( FROM_UTF8( it->first.c_str() ) );
  2700. }
  2701. }
  2702. MODULE* LEGACY_PLUGIN::FootprintLoad( const wxString& aLibraryPath,
  2703. const wxString& aFootprintName, const PROPERTIES* aProperties )
  2704. {
  2705. LOCALE_IO toggle; // toggles on, then off, the C locale.
  2706. init( aProperties );
  2707. cacheLib( aLibraryPath );
  2708. const MODULE_MAP& mods = m_cache->m_modules;
  2709. MODULE_CITER it = mods.find( TO_UTF8( aFootprintName ) );
  2710. if( it == mods.end() )
  2711. {
  2712. /*
  2713. THROW_IO_ERROR( wxString::Format( _( "No \"%s\" footprint in library \"%s\"" ),
  2714. aFootprintName.GetData(), aLibraryPath.GetData() ) );
  2715. */
  2716. return NULL;
  2717. }
  2718. // copy constructor to clone the already loaded MODULE
  2719. return new MODULE( *it->second );
  2720. }
  2721. bool LEGACY_PLUGIN::FootprintLibDelete( const wxString& aLibraryPath, const PROPERTIES* aProperties )
  2722. {
  2723. wxFileName fn = aLibraryPath;
  2724. if( !fn.FileExists() )
  2725. return false;
  2726. // Some of the more elaborate wxRemoveFile() crap puts up its own wxLog dialog
  2727. // we don't want that. we want bare metal portability with no UI here.
  2728. if( wxRemove( aLibraryPath ) )
  2729. {
  2730. THROW_IO_ERROR( wxString::Format(
  2731. _( "library \"%s\" cannot be deleted" ),
  2732. aLibraryPath.GetData() ) );
  2733. }
  2734. if( m_cache && m_cache->m_lib_path == aLibraryPath )
  2735. {
  2736. delete m_cache;
  2737. m_cache = 0;
  2738. }
  2739. return true;
  2740. }
  2741. bool LEGACY_PLUGIN::IsFootprintLibWritable( const wxString& aLibraryPath )
  2742. {
  2743. #if 0 // no support for 32 Cu layers in legacy format
  2744. return false;
  2745. #else
  2746. LOCALE_IO toggle;
  2747. init( NULL );
  2748. cacheLib( aLibraryPath );
  2749. return m_cache->m_writable;
  2750. #endif
  2751. }
  2752. LEGACY_PLUGIN::LEGACY_PLUGIN() :
  2753. m_cu_count( 16 ), // for FootprintLoad()
  2754. m_board( 0 ),
  2755. m_props( 0 ),
  2756. m_reader( 0 ),
  2757. m_fp( 0 ),
  2758. m_cache( 0 ),
  2759. m_mapping( new NETINFO_MAPPING() )
  2760. {
  2761. init( NULL );
  2762. }
  2763. LEGACY_PLUGIN::~LEGACY_PLUGIN()
  2764. {
  2765. delete m_cache;
  2766. delete m_mapping;
  2767. }