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.

1058 lines
35 KiB

11 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2012 Wayne Stambaugh <stambaughw@gmail.com>
  5. * Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file gpcb_plugin.cpp
  26. * @brief Geda PCB file plugin implementation file.
  27. */
  28. #include <fctsys.h>
  29. #include <common.h>
  30. #include <macros.h>
  31. #include <trigo.h>
  32. #include <wildcards_and_files_ext.h>
  33. #include <filter_reader.h>
  34. #include <trace_helpers.h>
  35. #include <class_board.h>
  36. #include <class_module.h>
  37. #include <class_pcb_text.h>
  38. #include <class_drawsegment.h>
  39. #include <class_edge_mod.h>
  40. #include <gpcb_plugin.h>
  41. #include <wx/dir.h>
  42. #include <wx/filename.h>
  43. #include <wx/wfstream.h>
  44. #include <boost/ptr_container/ptr_map.hpp>
  45. #include <memory.h>
  46. static inline long parseInt( const wxString& aValue, double aScalar )
  47. {
  48. double value = LONG_MAX;
  49. /*
  50. * In 2011 gEDA/pcb introduced values with units, like "10mm" or "200mil".
  51. * Unit-less values are still centimils (100000 units per inch), like with
  52. * the previous format.
  53. *
  54. * Distinction between the even older format (mils, 1000 units per inch)
  55. * and the pre-2011 format is done in ::parseMODULE already; the
  56. * distinction is by wether an object definition opens with '(' or '['.
  57. * All values with explicite unit open with a '[' so there's no need to
  58. * consider this distinction when parsing them.
  59. *
  60. * The solution here is to watch for a unit and, if present, convert the
  61. * value to centimils. All unit-less values are read unaltered. This way
  62. * the code below can contine to consider all read values to be in mils or
  63. * centimils. It also matches the strategy gEDA/pcb uses for backwards
  64. * compatibility with its own layouts.
  65. *
  66. * Fortunately gEDA/pcb allows only units 'mil' and 'mm' in files, see
  67. * definition of ALLOW_READABLE in gEDA/pcb's pcb_printf.h. So we don't
  68. * have to test for all 11 units gEDA/pcb allows in user dialogs.
  69. */
  70. if( aValue.EndsWith( wxT( "mm" ) ) )
  71. {
  72. aScalar *= 100000.0 / 25.4;
  73. }
  74. else if( aValue.EndsWith( wxT( "mil" ) ) )
  75. {
  76. aScalar *= 100.;
  77. }
  78. // This conversion reports failure on strings as simple as "1000", still
  79. // it returns the right result in &value. Thus, ignore the return value.
  80. aValue.ToCDouble(&value);
  81. if( value == LONG_MAX ) // conversion really failed
  82. {
  83. THROW_IO_ERROR( wxString::Format( _( "Cannot convert \"%s\" to an integer" ),
  84. aValue.GetData() ) );
  85. return 0;
  86. }
  87. return KiROUND( value * aScalar );
  88. }
  89. /**
  90. * Class GPCB_FPL_CACHE_ITEM
  91. * is helper class for creating a footprint library cache.
  92. *
  93. * The new footprint library design is a file path of individual module files
  94. * that contain a single module per file. This class is a helper only for the
  95. * footprint portion of the PLUGIN API, and only for the #PCB_IO plugin. It is
  96. * private to this implementation file so it is not placed into a header.
  97. */
  98. class GPCB_FPL_CACHE_ITEM
  99. {
  100. WX_FILENAME m_filename; ///< The the full file name and path of the footprint to cache.
  101. std::unique_ptr<MODULE> m_module;
  102. public:
  103. GPCB_FPL_CACHE_ITEM( MODULE* aModule, const WX_FILENAME& aFileName );
  104. WX_FILENAME GetFileName() const { return m_filename; }
  105. MODULE* GetModule() const { return m_module.get(); }
  106. };
  107. GPCB_FPL_CACHE_ITEM::GPCB_FPL_CACHE_ITEM( MODULE* aModule, const WX_FILENAME& aFileName ) :
  108. m_filename( aFileName ),
  109. m_module( aModule )
  110. {
  111. }
  112. typedef boost::ptr_map< std::string, GPCB_FPL_CACHE_ITEM > MODULE_MAP;
  113. typedef MODULE_MAP::iterator MODULE_ITER;
  114. typedef MODULE_MAP::const_iterator MODULE_CITER;
  115. class GPCB_FPL_CACHE
  116. {
  117. GPCB_PLUGIN* m_owner; /// Plugin object that owns the cache.
  118. wxFileName m_lib_path; /// The path of the library.
  119. MODULE_MAP m_modules; /// Map of footprint file name per MODULE*.
  120. bool m_cache_dirty; // Stored separately because it's expensive to check
  121. // m_cache_timestamp against all the files.
  122. long long m_cache_timestamp; // A hash of the timestamps for all the footprint
  123. // files.
  124. MODULE* parseMODULE( LINE_READER* aLineReader );
  125. /**
  126. * Function testFlags
  127. * tests \a aFlag for \a aMask or \a aName.
  128. * @param aFlag = List of flags to test against: can be a bit field flag or a list name flag
  129. * a bit field flag is an hexadecimal value: Ox00020000
  130. * a list name flag is a string list of flags, comma separated like square,option1
  131. * @param aMask = flag list to test
  132. * @param aName = flag name to find in list
  133. * @return true if found
  134. */
  135. bool testFlags( const wxString& aFlag, long aMask, const wxChar* aName );
  136. /**
  137. * Function parseParameters
  138. * extracts parameters and tokens from \a aLineReader and adds them to \a aParameterList.
  139. *
  140. * Delimiter characters are:
  141. * [ ] ( ) Begin and end of parameter list and units indicator
  142. * " is a string delimiter
  143. * space is the param separator
  144. * The first word is the keyword
  145. * the second item is one of ( or [
  146. * other are parameters (number or delimited string)
  147. * last parameter is ) or ]
  148. *
  149. * @param aParameterList This list of parameters parsed.
  150. * @param aLineReader The line reader object to parse.
  151. */
  152. void parseParameters( wxArrayString& aParameterList, LINE_READER* aLineReader );
  153. public:
  154. GPCB_FPL_CACHE( GPCB_PLUGIN* aOwner, const wxString& aLibraryPath );
  155. wxString GetPath() const { return m_lib_path.GetPath(); }
  156. bool IsWritable() const { return m_lib_path.IsOk() && m_lib_path.IsDirWritable(); }
  157. MODULE_MAP& GetModules() { return m_modules; }
  158. // Most all functions in this class throw IO_ERROR exceptions. There are no
  159. // error codes nor user interface calls from here, nor in any PLUGIN.
  160. // Catch these exceptions higher up please.
  161. /// Save not implemented for the Geda PCB footprint library format.
  162. void Load();
  163. void Remove( const wxString& aFootprintName );
  164. /**
  165. * Function GetTimestamp
  166. * Generate a timestamp representing all source files in the cache (including the
  167. * parent directory).
  168. * Timestamps should not be considered ordered. They either match or they don't.
  169. */
  170. static long long GetTimestamp( const wxString& aLibPath );
  171. /**
  172. * Function IsModified
  173. * Return true if the cache is not up-to-date.
  174. */
  175. bool IsModified();
  176. };
  177. GPCB_FPL_CACHE::GPCB_FPL_CACHE( GPCB_PLUGIN* aOwner, const wxString& aLibraryPath )
  178. {
  179. m_owner = aOwner;
  180. m_lib_path.SetPath( aLibraryPath );
  181. m_cache_timestamp = 0;
  182. m_cache_dirty = true;
  183. }
  184. void GPCB_FPL_CACHE::Load()
  185. {
  186. m_cache_dirty = false;
  187. m_cache_timestamp = 0;
  188. // Note: like our .pretty footprint libraries, the gpcb footprint libraries are folders,
  189. // and the footprints are the .fp files inside this folder.
  190. wxDir dir( m_lib_path.GetPath() );
  191. if( !dir.IsOpened() )
  192. {
  193. THROW_IO_ERROR( wxString::Format( _( "footprint library path \"%s\" does not exist" ),
  194. m_lib_path.GetPath().GetData() ) );
  195. }
  196. wxString fullName;
  197. wxString fileSpec = wxT( "*." ) + GedaPcbFootprintLibFileExtension;
  198. // wxFileName construction is egregiously slow. Construct it once and just swap out
  199. // the filename thereafter.
  200. WX_FILENAME fn( m_lib_path.GetPath(), wxT( "dummyName" ) );
  201. if( !dir.GetFirst( &fullName, fileSpec ) )
  202. return;
  203. wxString cacheErrorMsg;
  204. do
  205. {
  206. fn.SetFullName( fullName );
  207. // Queue I/O errors so only files that fail to parse don't get loaded.
  208. try
  209. {
  210. // reader now owns fp, will close on exception or return
  211. FILE_LINE_READER reader( fn.GetFullPath() );
  212. std::string name = TO_UTF8( fn.GetName() );
  213. MODULE* footprint = parseMODULE( &reader );
  214. // The footprint name is the file name without the extension.
  215. footprint->SetFPID( LIB_ID( wxEmptyString, fn.GetName() ) );
  216. m_modules.insert( name, new GPCB_FPL_CACHE_ITEM( footprint, fn ) );
  217. }
  218. catch( const IO_ERROR& ioe )
  219. {
  220. if( !cacheErrorMsg.IsEmpty() )
  221. cacheErrorMsg += "\n\n";
  222. cacheErrorMsg += ioe.What();
  223. }
  224. } while( dir.GetNext( &fullName ) );
  225. if( !cacheErrorMsg.IsEmpty() )
  226. THROW_IO_ERROR( cacheErrorMsg );
  227. }
  228. void GPCB_FPL_CACHE::Remove( const wxString& aFootprintName )
  229. {
  230. std::string footprintName = TO_UTF8( aFootprintName );
  231. MODULE_CITER it = m_modules.find( footprintName );
  232. if( it == m_modules.end() )
  233. {
  234. THROW_IO_ERROR( wxString::Format( _( "library \"%s\" has no footprint \"%s\" to delete" ),
  235. m_lib_path.GetPath().GetData(),
  236. aFootprintName.GetData() ) );
  237. }
  238. // Remove the module from the cache and delete the module file from the library.
  239. wxString fullPath = it->second->GetFileName().GetFullPath();
  240. m_modules.erase( footprintName );
  241. wxRemoveFile( fullPath );
  242. }
  243. bool GPCB_FPL_CACHE::IsModified()
  244. {
  245. m_cache_dirty = m_cache_dirty || GetTimestamp( m_lib_path.GetFullPath() ) != m_cache_timestamp;
  246. return m_cache_dirty;
  247. }
  248. long long GPCB_FPL_CACHE::GetTimestamp( const wxString& aLibPath )
  249. {
  250. wxString fileSpec = wxT( "*." ) + GedaPcbFootprintLibFileExtension;
  251. return TimestampDir( aLibPath, fileSpec );
  252. }
  253. MODULE* GPCB_FPL_CACHE::parseMODULE( LINE_READER* aLineReader )
  254. {
  255. #define TEXT_DEFAULT_SIZE ( 40*IU_PER_MILS )
  256. #define OLD_GPCB_UNIT_CONV IU_PER_MILS
  257. // Old version unit = 1 mil, so conv_unit is 10 or 0.1
  258. #define NEW_GPCB_UNIT_CONV ( 0.01*IU_PER_MILS )
  259. int paramCnt;
  260. double conv_unit = NEW_GPCB_UNIT_CONV; // GPCB unit = 0.01 mils and Pcbnew 0.1
  261. wxPoint textPos;
  262. wxString msg;
  263. wxArrayString parameters;
  264. std::unique_ptr<MODULE> module( new MODULE( NULL ) );
  265. if( aLineReader->ReadLine() == NULL )
  266. {
  267. msg = aLineReader->GetSource() + ": empty file";
  268. THROW_IO_ERROR( msg );
  269. }
  270. parameters.Clear();
  271. parseParameters( parameters, aLineReader );
  272. paramCnt = parameters.GetCount();
  273. /* From the Geda PCB documentation, valid Element definitions:
  274. * Element [SFlags "Desc" "Name" "Value" MX MY TX TY TDir TScale TSFlags]
  275. * Element (NFlags "Desc" "Name" "Value" MX MY TX TY TDir TScale TNFlags)
  276. * Element (NFlags "Desc" "Name" "Value" TX TY TDir TScale TNFlags)
  277. * Element (NFlags "Desc" "Name" TX TY TDir TScale TNFlags)
  278. * Element ("Desc" "Name" TX TY TDir TScale TNFlags)
  279. */
  280. if( parameters[0].CmpNoCase( wxT( "Element" ) ) != 0 )
  281. {
  282. msg.Printf( _( "unknown token \"%s\"" ), GetChars( parameters[0] ) );
  283. THROW_PARSE_ERROR( msg, aLineReader->GetSource(), (const char *)aLineReader,
  284. aLineReader->LineNumber(), 0 );
  285. }
  286. if( paramCnt < 10 || paramCnt > 14 )
  287. {
  288. msg.Printf( _( "Element token contains %d parameters." ), paramCnt );
  289. THROW_PARSE_ERROR( msg, aLineReader->GetSource(), (const char *)aLineReader,
  290. aLineReader->LineNumber(), 0 );
  291. }
  292. // Test symbol after "Element": if [ units = 0.01 mils, and if ( units = 1 mil
  293. if( parameters[1] == wxT( "(" ) )
  294. conv_unit = OLD_GPCB_UNIT_CONV;
  295. if( paramCnt > 10 )
  296. {
  297. module->SetDescription( parameters[3] );
  298. module->SetReference( parameters[4] );
  299. }
  300. else
  301. {
  302. module->SetDescription( parameters[2] );
  303. module->SetReference( parameters[3] );
  304. }
  305. // Read value
  306. if( paramCnt > 10 )
  307. module->SetValue( parameters[5] );
  308. // With gEDA/pcb, value is meaningful after instantiation, only, so it's
  309. // often empty in bare footprints.
  310. if( module->Value().GetText().IsEmpty() )
  311. module->Value().SetText( wxT( "Val**" ) );
  312. if( paramCnt == 14 )
  313. {
  314. textPos = wxPoint( parseInt( parameters[8], conv_unit ),
  315. parseInt( parameters[9], conv_unit ) );
  316. }
  317. else
  318. {
  319. textPos = wxPoint( parseInt( parameters[6], conv_unit ),
  320. parseInt( parameters[7], conv_unit ) );
  321. }
  322. int orientation = parseInt( parameters[paramCnt-4], 1.0 );
  323. module->Reference().SetTextAngle( (orientation % 2) ? 900 : 0 );
  324. // Calculate size: default height is 40 mils, width 30 mil.
  325. // real size is: default * ibuf[idx+3] / 100 (size in gpcb is given in percent of default size
  326. int thsize = parseInt( parameters[paramCnt-3], TEXT_DEFAULT_SIZE ) / 100;
  327. thsize = std::max( (int)( 5 * IU_PER_MILS ), thsize ); // Ensure a minimal size = 5 mils
  328. int twsize = thsize * 30 / 40;
  329. int thickness = thsize / 8;
  330. // gEDA/pcb aligns top/left, not pcbnew's default, center/center.
  331. // Compensate for this by shifting the insertion point instead of the
  332. // aligment, because alignment isn't changeable in the GUI.
  333. textPos.x = textPos.x + twsize * module->GetReference().Len() / 2;
  334. textPos.y += thsize / 2;
  335. // gEDA/pcb draws a bit too low/left, while pcbnew draws a bit too
  336. // high/right. Compensate for similar appearance.
  337. textPos.x -= thsize / 10;
  338. textPos.y += thsize / 2;
  339. module->Reference().SetTextPos( textPos );
  340. module->Reference().SetPos0( textPos );
  341. module->Reference().SetTextSize( wxSize( twsize, thsize ) );
  342. module->Reference().SetThickness( thickness );
  343. // gEDA/pcb shows only one of value/reference/description at a time. Which
  344. // one is selectable by a global menu setting. pcbnew needs reference as
  345. // well as value visible, so place the value right below the reference.
  346. module->Value().SetTextAngle( module->Reference().GetTextAngle() );
  347. module->Value().SetTextSize( module->Reference().GetTextSize() );
  348. module->Value().SetThickness( module->Reference().GetThickness() );
  349. textPos.y += thsize * 13 / 10; // 130% line height
  350. module->Value().SetTextPos( textPos );
  351. module->Value().SetPos0( textPos );
  352. while( aLineReader->ReadLine() )
  353. {
  354. parameters.Clear();
  355. parseParameters( parameters, aLineReader );
  356. if( parameters.IsEmpty() || parameters[0] == wxT( "(" ) )
  357. continue;
  358. if( parameters[0] == wxT( ")" ) )
  359. break;
  360. paramCnt = parameters.GetCount();
  361. // Test units value for a string line param (more than 3 parameters : ident [ xx ] )
  362. if( paramCnt > 3 )
  363. {
  364. if( parameters[1] == wxT( "(" ) )
  365. conv_unit = OLD_GPCB_UNIT_CONV;
  366. else
  367. conv_unit = NEW_GPCB_UNIT_CONV;
  368. }
  369. wxLogTrace( traceGedaPcbPlugin, wxT( "%s parameter count = %d." ),
  370. GetChars( parameters[0] ), paramCnt );
  371. // Parse a line with format: ElementLine [X1 Y1 X2 Y2 Thickness]
  372. if( parameters[0].CmpNoCase( wxT( "ElementLine" ) ) == 0 )
  373. {
  374. if( paramCnt != 8 )
  375. {
  376. msg.Printf( wxT( "ElementLine token contains %d parameters." ), paramCnt );
  377. THROW_PARSE_ERROR( msg, aLineReader->GetSource(), (const char *)aLineReader,
  378. aLineReader->LineNumber(), 0 );
  379. }
  380. EDGE_MODULE* drawSeg = new EDGE_MODULE( module.get() );
  381. drawSeg->SetLayer( F_SilkS );
  382. drawSeg->SetShape( S_SEGMENT );
  383. drawSeg->SetStart0( wxPoint( parseInt( parameters[2], conv_unit ),
  384. parseInt( parameters[3], conv_unit ) ) );
  385. drawSeg->SetEnd0( wxPoint( parseInt( parameters[4], conv_unit ),
  386. parseInt( parameters[5], conv_unit ) ) );
  387. drawSeg->SetWidth( parseInt( parameters[6], conv_unit ) );
  388. drawSeg->SetDrawCoord();
  389. module->GraphicalItemsList().PushBack( drawSeg );
  390. continue;
  391. }
  392. // Parse an arc with format: ElementArc [X Y Width Height StartAngle DeltaAngle Thickness]
  393. if( parameters[0].CmpNoCase( wxT( "ElementArc" ) ) == 0 )
  394. {
  395. if( paramCnt != 10 )
  396. {
  397. msg.Printf( wxT( "ElementArc token contains %d parameters." ), paramCnt );
  398. THROW_PARSE_ERROR( msg, aLineReader->GetSource(), (const char *)aLineReader,
  399. aLineReader->LineNumber(), 0 );
  400. }
  401. // Pcbnew does know ellipse so we must have Width = Height
  402. EDGE_MODULE* drawSeg = new EDGE_MODULE( module.get() );
  403. drawSeg->SetLayer( F_SilkS );
  404. drawSeg->SetShape( S_ARC );
  405. module->GraphicalItemsList().PushBack( drawSeg );
  406. // for and arc: ibuf[3] = ibuf[4]. Pcbnew does not know ellipses
  407. int radius = ( parseInt( parameters[4], conv_unit ) +
  408. parseInt( parameters[5], conv_unit ) ) / 2;
  409. wxPoint centre( parseInt( parameters[2], conv_unit ),
  410. parseInt( parameters[3], conv_unit ) );
  411. drawSeg->SetStart0( centre );
  412. // Pcbnew start angles are inverted and 180 degrees from Geda PCB angles.
  413. double start_angle = parseInt( parameters[6], -10.0 ) + 1800.0;
  414. // Pcbnew delta angle direction is the opposite of Geda PCB delta angles.
  415. double sweep_angle = parseInt( parameters[7], -10.0 );
  416. // Geda PCB does not support circles.
  417. if( sweep_angle == -3600.0 )
  418. drawSeg->SetShape( S_CIRCLE );
  419. // Angle value is clockwise in gpcb and Pcbnew.
  420. drawSeg->SetAngle( sweep_angle );
  421. drawSeg->SetEnd0( wxPoint( radius, 0 ) );
  422. // Calculate start point coordinate of arc
  423. wxPoint arcStart( drawSeg->GetEnd0() );
  424. RotatePoint( &arcStart, -start_angle );
  425. drawSeg->SetEnd0( centre + arcStart );
  426. drawSeg->SetWidth( parseInt( parameters[8], conv_unit ) );
  427. drawSeg->SetDrawCoord();
  428. continue;
  429. }
  430. // Parse a Pad with no hole with format:
  431. // Pad [rX1 rY1 rX2 rY2 Thickness Clearance Mask "Name" "Number" SFlags]
  432. // Pad (rX1 rY1 rX2 rY2 Thickness Clearance Mask "Name" "Number" NFlags)
  433. // Pad (aX1 aY1 aX2 aY2 Thickness "Name" "Number" NFlags)
  434. // Pad (aX1 aY1 aX2 aY2 Thickness "Name" NFlags)
  435. if( parameters[0].CmpNoCase( wxT( "Pad" ) ) == 0 )
  436. {
  437. if( paramCnt < 10 || paramCnt > 13 )
  438. {
  439. msg.Printf( wxT( "Pad token contains %d parameters." ), paramCnt );
  440. THROW_PARSE_ERROR( msg, aLineReader->GetSource(), (const char *)aLineReader,
  441. aLineReader->LineNumber(), 0 );
  442. }
  443. D_PAD* pad = new D_PAD( module.get() );
  444. static const LSET pad_front( 3, F_Cu, F_Mask, F_Paste );
  445. static const LSET pad_back( 3, B_Cu, B_Mask, B_Paste );
  446. pad->SetShape( PAD_SHAPE_RECT );
  447. pad->SetAttribute( PAD_ATTRIB_SMD );
  448. pad->SetLayerSet( pad_front );
  449. if( testFlags( parameters[paramCnt-2], 0x0080, wxT( "onsolder" ) ) )
  450. pad->SetLayerSet( pad_back );
  451. // Set the pad name:
  452. // Pcbnew pad name is used for electrical connection calculations.
  453. // Accordingly it should be mapped to gEDA's pin/pad number,
  454. // which is used for the same purpose.
  455. // gEDA also features a pin/pad "name", which is an arbitrary string
  456. // and set to the pin name of the netlist on instantiation. Many gEDA
  457. // bare footprints use identical strings for name and number, so this
  458. // can be a bit confusing.
  459. pad->SetName( parameters[paramCnt-3] );
  460. int x1 = parseInt( parameters[2], conv_unit );
  461. int x2 = parseInt( parameters[4], conv_unit );
  462. int y1 = parseInt( parameters[3], conv_unit );
  463. int y2 = parseInt( parameters[5], conv_unit );
  464. int width = parseInt( parameters[6], conv_unit );
  465. wxPoint delta( x2 - x1, y2 - y1 );
  466. double angle = atan2( (double)delta.y, (double)delta.x );
  467. // Get the pad clearance and the solder mask clearance.
  468. if( paramCnt == 13 )
  469. {
  470. int clearance = parseInt( parameters[7], conv_unit );
  471. // One of gEDA's oddities is that clearance between pad and polygon
  472. // is given as the gap on both sides of the pad together, so for
  473. // KiCad it has to halfed.
  474. pad->SetLocalClearance( clearance / 2 );
  475. // In GEDA, the mask value is the size of the hole in this
  476. // solder mask. In Pcbnew, it is a margin, therefore the distance
  477. // between the copper and the mask
  478. int maskMargin = parseInt( parameters[8], conv_unit );
  479. maskMargin = ( maskMargin - width ) / 2;
  480. pad->SetLocalSolderMaskMargin( maskMargin );
  481. }
  482. // Negate angle (due to Y reversed axis) and convert it to internal units
  483. angle = - RAD2DECIDEG( angle );
  484. pad->SetOrientation( KiROUND( angle ) );
  485. wxPoint padPos( (x1 + x2) / 2, (y1 + y2) / 2 );
  486. pad->SetSize( wxSize( KiROUND( EuclideanNorm( delta ) ) + width,
  487. width ) );
  488. padPos += module->GetPosition();
  489. pad->SetPos0( padPos );
  490. pad->SetPosition( padPos );
  491. if( !testFlags( parameters[paramCnt-2], 0x0100, wxT( "square" ) ) )
  492. {
  493. if( pad->GetSize().x == pad->GetSize().y )
  494. pad->SetShape( PAD_SHAPE_CIRCLE );
  495. else
  496. pad->SetShape( PAD_SHAPE_OVAL );
  497. }
  498. module->Add( pad );
  499. continue;
  500. }
  501. // Parse a Pin with through hole with format:
  502. // Pin [rX rY Thickness Clearance Mask Drill "Name" "Number" SFlags]
  503. // Pin (rX rY Thickness Clearance Mask Drill "Name" "Number" NFlags)
  504. // Pin (aX aY Thickness Drill "Name" "Number" NFlags)
  505. // Pin (aX aY Thickness Drill "Name" NFlags)
  506. // Pin (aX aY Thickness "Name" NFlags)
  507. if( parameters[0].CmpNoCase( wxT( "Pin" ) ) == 0 )
  508. {
  509. if( paramCnt < 8 || paramCnt > 12 )
  510. {
  511. msg.Printf( wxT( "Pin token contains %d parameters." ), paramCnt );
  512. THROW_PARSE_ERROR( msg, aLineReader->GetSource(), (const char *)aLineReader,
  513. aLineReader->LineNumber(), 0 );
  514. }
  515. D_PAD* pad = new D_PAD( module.get() );
  516. pad->SetShape( PAD_SHAPE_CIRCLE );
  517. static const LSET pad_set = LSET::AllCuMask() | LSET( 3, F_SilkS, F_Mask, B_Mask );
  518. pad->SetLayerSet( pad_set );
  519. if( testFlags( parameters[paramCnt-2], 0x0100, wxT( "square" ) ) )
  520. pad->SetShape( PAD_SHAPE_RECT );
  521. // Set the pad name:
  522. // Pcbnew pad name is used for electrical connection calculations.
  523. // Accordingly it should be mapped to gEDA's pin/pad number,
  524. // which is used for the same purpose.
  525. pad->SetName( parameters[paramCnt-3] );
  526. wxPoint padPos( parseInt( parameters[2], conv_unit ),
  527. parseInt( parameters[3], conv_unit ) );
  528. int padSize = parseInt( parameters[4], conv_unit );
  529. pad->SetSize( wxSize( padSize, padSize ) );
  530. int drillSize = 0;
  531. // Get the pad clearance, solder mask clearance, and drill size.
  532. if( paramCnt == 12 )
  533. {
  534. int clearance = parseInt( parameters[5], conv_unit );
  535. // One of gEDA's oddities is that clearance between pad and polygon
  536. // is given as the gap on both sides of the pad together, so for
  537. // KiCad it has to halfed.
  538. pad->SetLocalClearance( clearance / 2 );
  539. // In GEDA, the mask value is the size of the hole in this
  540. // solder mask. In Pcbnew, it is a margin, therefore the distance
  541. // between the copper and the mask
  542. int maskMargin = parseInt( parameters[6], conv_unit );
  543. maskMargin = ( maskMargin - padSize ) / 2;
  544. pad->SetLocalSolderMaskMargin( maskMargin );
  545. drillSize = parseInt( parameters[7], conv_unit );
  546. }
  547. else
  548. {
  549. drillSize = parseInt( parameters[5], conv_unit );
  550. }
  551. pad->SetDrillSize( wxSize( drillSize, drillSize ) );
  552. padPos += module->GetPosition();
  553. pad->SetPos0( padPos );
  554. pad->SetPosition( padPos );
  555. if( pad->GetShape() == PAD_SHAPE_CIRCLE && pad->GetSize().x != pad->GetSize().y )
  556. pad->SetShape( PAD_SHAPE_OVAL );
  557. module->Add( pad );
  558. continue;
  559. }
  560. }
  561. // Recalculate the bounding box
  562. module->CalculateBoundingBox();
  563. return module.release();
  564. }
  565. void GPCB_FPL_CACHE::parseParameters( wxArrayString& aParameterList, LINE_READER* aLineReader )
  566. {
  567. char key;
  568. wxString tmp;
  569. char* line = aLineReader->Line();
  570. // Last line already ready in main parser loop.
  571. while( *line != 0 )
  572. {
  573. key = *line;
  574. line++;
  575. switch( key )
  576. {
  577. case '[':
  578. case '(':
  579. if( !tmp.IsEmpty() )
  580. {
  581. aParameterList.Add( tmp );
  582. tmp.Clear();
  583. }
  584. tmp.Append( key );
  585. aParameterList.Add( tmp );
  586. tmp.Clear();
  587. // Opening delimiter "(" after Element statement. Any other occurrence is part
  588. // of a keyword definition.
  589. if( aParameterList.GetCount() == 1 )
  590. {
  591. wxLogTrace( traceGedaPcbPlugin, dump( aParameterList ) );
  592. return;
  593. }
  594. break;
  595. case ']':
  596. case ')':
  597. if( !tmp.IsEmpty() )
  598. {
  599. aParameterList.Add( tmp );
  600. tmp.Clear();
  601. }
  602. tmp.Append( key );
  603. aParameterList.Add( tmp );
  604. wxLogTrace( traceGedaPcbPlugin, dump( aParameterList ) );
  605. return;
  606. case '\n':
  607. case '\r':
  608. // Element descriptions can span multiple lines.
  609. line = aLineReader->ReadLine();
  610. // Fall through is intentional.
  611. case '\t':
  612. case ' ':
  613. if( !tmp.IsEmpty() )
  614. {
  615. aParameterList.Add( tmp );
  616. tmp.Clear();
  617. }
  618. break;
  619. case '"':
  620. // Handle empty quotes.
  621. if( *line == '"' )
  622. {
  623. line++;
  624. tmp.Clear();
  625. aParameterList.Add( wxEmptyString );
  626. break;
  627. }
  628. while( *line != 0 )
  629. {
  630. key = *line;
  631. line++;
  632. if( key == '"' )
  633. {
  634. aParameterList.Add( tmp );
  635. tmp.Clear();
  636. break;
  637. }
  638. else
  639. {
  640. tmp.Append( key );
  641. }
  642. }
  643. break;
  644. case '#':
  645. line = aLineReader->ReadLine();
  646. break;
  647. default:
  648. tmp.Append( key );
  649. break;
  650. }
  651. }
  652. }
  653. bool GPCB_FPL_CACHE::testFlags( const wxString& aFlag, long aMask, const wxChar* aName )
  654. {
  655. wxString number;
  656. if( aFlag.StartsWith( wxT( "0x" ), &number ) || aFlag.StartsWith( wxT( "0X" ), &number ) )
  657. {
  658. long lflags;
  659. if( number.ToLong( &lflags, 16 ) && ( lflags & aMask ) )
  660. return true;
  661. }
  662. else if( aFlag.Contains( aName ) )
  663. {
  664. return true;
  665. }
  666. return false;
  667. }
  668. GPCB_PLUGIN::GPCB_PLUGIN() :
  669. m_cache( 0 ),
  670. m_ctl( 0 )
  671. {
  672. m_reader = NULL;
  673. init( 0 );
  674. }
  675. GPCB_PLUGIN::GPCB_PLUGIN( int aControlFlags ) :
  676. m_cache( 0 ),
  677. m_ctl( aControlFlags )
  678. {
  679. m_reader = NULL;
  680. init( 0 );
  681. }
  682. GPCB_PLUGIN::~GPCB_PLUGIN()
  683. {
  684. delete m_cache;
  685. }
  686. void GPCB_PLUGIN::init( const PROPERTIES* aProperties )
  687. {
  688. m_props = aProperties;
  689. }
  690. void GPCB_PLUGIN::validateCache( const wxString& aLibraryPath, bool checkModified )
  691. {
  692. if( !m_cache || ( checkModified && m_cache->IsModified() ) )
  693. {
  694. // a spectacular episode in memory management:
  695. delete m_cache;
  696. m_cache = new GPCB_FPL_CACHE( this, aLibraryPath );
  697. m_cache->Load();
  698. }
  699. }
  700. void GPCB_PLUGIN::FootprintEnumerate( wxArrayString& aFootprintNames,
  701. const wxString& aLibraryPath,
  702. const PROPERTIES* aProperties )
  703. {
  704. LOCALE_IO toggle; // toggles on, then off, the C locale.
  705. wxDir dir( aLibraryPath );
  706. if( !dir.IsOpened() )
  707. {
  708. THROW_IO_ERROR( wxString::Format( _( "footprint library path \"%s\" does not exist" ),
  709. GetChars( aLibraryPath ) ) );
  710. }
  711. init( aProperties );
  712. wxString errorMsg;
  713. // Some of the files may have been parsed correctly so we want to add the valid files to
  714. // the library.
  715. try
  716. {
  717. validateCache( aLibraryPath );
  718. }
  719. catch( const IO_ERROR& ioe )
  720. {
  721. errorMsg = ioe.What();
  722. }
  723. const MODULE_MAP& mods = m_cache->GetModules();
  724. for( MODULE_CITER it = mods.begin(); it != mods.end(); ++it )
  725. {
  726. aFootprintNames.Add( FROM_UTF8( it->first.c_str() ) );
  727. }
  728. if( !errorMsg.IsEmpty() )
  729. THROW_IO_ERROR( errorMsg );
  730. }
  731. const MODULE* GPCB_PLUGIN::getFootprint( const wxString& aLibraryPath,
  732. const wxString& aFootprintName,
  733. const PROPERTIES* aProperties,
  734. bool checkModified )
  735. {
  736. LOCALE_IO toggle; // toggles on, then off, the C locale.
  737. init( aProperties );
  738. validateCache( aLibraryPath, checkModified );
  739. const MODULE_MAP& mods = m_cache->GetModules();
  740. MODULE_CITER it = mods.find( TO_UTF8( aFootprintName ) );
  741. if( it == mods.end() )
  742. {
  743. return NULL;
  744. }
  745. return it->second->GetModule();
  746. }
  747. const MODULE* GPCB_PLUGIN::GetEnumeratedFootprint( const wxString& aLibraryPath,
  748. const wxString& aFootprintName,
  749. const PROPERTIES* aProperties )
  750. {
  751. return getFootprint( aLibraryPath, aFootprintName, aProperties, false );
  752. }
  753. MODULE* GPCB_PLUGIN::FootprintLoad( const wxString& aLibraryPath, const wxString& aFootprintName,
  754. const PROPERTIES* aProperties )
  755. {
  756. const MODULE* footprint = getFootprint( aLibraryPath, aFootprintName, aProperties, true );
  757. return footprint ? new MODULE( *footprint ) : nullptr;
  758. }
  759. void GPCB_PLUGIN::FootprintDelete( const wxString& aLibraryPath, const wxString& aFootprintName,
  760. const PROPERTIES* aProperties )
  761. {
  762. LOCALE_IO toggle; // toggles on, then off, the C locale.
  763. init( aProperties );
  764. validateCache( aLibraryPath );
  765. if( !m_cache->IsWritable() )
  766. {
  767. THROW_IO_ERROR( wxString::Format( _( "Library \"%s\" is read only" ),
  768. aLibraryPath.GetData() ) );
  769. }
  770. m_cache->Remove( aFootprintName );
  771. }
  772. bool GPCB_PLUGIN::FootprintLibDelete( const wxString& aLibraryPath, const PROPERTIES* aProperties )
  773. {
  774. wxFileName fn;
  775. fn.SetPath( aLibraryPath );
  776. // Return if there is no library path to delete.
  777. if( !fn.DirExists() )
  778. return false;
  779. if( !fn.IsDirWritable() )
  780. {
  781. THROW_IO_ERROR( wxString::Format( _( "user does not have permission to delete directory \"%s\"" ),
  782. aLibraryPath.GetData() ) );
  783. }
  784. wxDir dir( aLibraryPath );
  785. if( dir.HasSubDirs() )
  786. {
  787. THROW_IO_ERROR( wxString::Format( _( "library directory \"%s\" has unexpected sub-directories" ),
  788. aLibraryPath.GetData() ) );
  789. }
  790. // All the footprint files must be deleted before the directory can be deleted.
  791. if( dir.HasFiles() )
  792. {
  793. unsigned i;
  794. wxFileName tmp;
  795. wxArrayString files;
  796. wxDir::GetAllFiles( aLibraryPath, &files );
  797. for( i = 0; i < files.GetCount(); i++ )
  798. {
  799. tmp = files[i];
  800. if( tmp.GetExt() != KiCadFootprintFileExtension )
  801. {
  802. THROW_IO_ERROR( wxString::Format( _( "unexpected file \"%s\" was found in library path \"%s\"" ),
  803. files[i].GetData(), aLibraryPath.GetData() ) );
  804. }
  805. }
  806. for( i = 0; i < files.GetCount(); i++ )
  807. {
  808. wxRemoveFile( files[i] );
  809. }
  810. }
  811. wxLogTrace( traceGedaPcbPlugin, wxT( "Removing footprint library '%s'" ),
  812. aLibraryPath.GetData() );
  813. // Some of the more elaborate wxRemoveFile() crap puts up its own wxLog dialog
  814. // we don't want that. we want bare metal portability with no UI here.
  815. if( !wxRmdir( aLibraryPath ) )
  816. {
  817. THROW_IO_ERROR( wxString::Format( _( "footprint library \"%s\" cannot be deleted" ),
  818. aLibraryPath.GetData() ) );
  819. }
  820. // For some reason removing a directory in Windows is not immediately updated. This delay
  821. // prevents an error when attempting to immediately recreate the same directory when over
  822. // writing an existing library.
  823. #ifdef __WINDOWS__
  824. wxMilliSleep( 250L );
  825. #endif
  826. if( m_cache && m_cache->GetPath() == aLibraryPath )
  827. {
  828. delete m_cache;
  829. m_cache = NULL;
  830. }
  831. return true;
  832. }
  833. long long GPCB_PLUGIN::GetLibraryTimestamp( const wxString& aLibraryPath ) const
  834. {
  835. return GPCB_FPL_CACHE::GetTimestamp( aLibraryPath );
  836. }
  837. bool GPCB_PLUGIN::IsFootprintLibWritable( const wxString& aLibraryPath )
  838. {
  839. LOCALE_IO toggle;
  840. init( NULL );
  841. validateCache( aLibraryPath );
  842. return m_cache->IsWritable();
  843. }