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.

3255 lines
107 KiB

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