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.

577 lines
20 KiB

12 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 1992-2015 KiCad Developers, see change_log.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. //#include <wx/wx.h>
  24. #include <pcb_plot_params_parser.h>
  25. #include <pcb_plot_params.h>
  26. #include <layers_id_colors_and_visibility.h>
  27. #include <plot_common.h>
  28. #include <macros.h>
  29. #include <convert_to_biu.h>
  30. #define PLOT_LINEWIDTH_MIN (0.02*IU_PER_MM) // min value for default line thickness
  31. #define PLOT_LINEWIDTH_MAX (2*IU_PER_MM) // max value for default line thickness
  32. #define PLOT_LINEWIDTH_DEFAULT (0.15*IU_PER_MM) // def. value for default line thickness
  33. #define HPGL_PEN_DIAMETER_MIN 0
  34. #define HPGL_PEN_DIAMETER_MAX 100 // Unit = mil
  35. #define HPGL_PEN_SPEED_MIN 1 // this param is always in cm/s
  36. #define HPGL_PEN_SPEED_MAX 99 // this param is always in cm/s
  37. #define HPGL_PEN_NUMBER_MIN 1
  38. #define HPGL_PEN_NUMBER_MAX 16
  39. /**
  40. * Default line thickness in internal units used to draw or plot items using a
  41. * default thickness line value (Frame references)
  42. */
  43. int g_DrawDefaultLineThickness = PLOT_LINEWIDTH_DEFAULT;
  44. // default trailing digits in Gerber coordinates, when units are mm
  45. // This is also the max usable precision (i.e. internal Pcbnew Units)
  46. static const int gbrDefaultPrecision = 6;
  47. using namespace PCBPLOTPARAMS_T;
  48. static const char* getTokenName( T aTok )
  49. {
  50. return PCB_PLOT_PARAMS_LEXER::TokenName( aTok );
  51. }
  52. static bool setInt( int* aInt, int aValue, int aMin, int aMax )
  53. {
  54. int temp = aValue;
  55. if( aValue < aMin )
  56. temp = aMin;
  57. else if( aValue > aMax )
  58. temp = aMax;
  59. *aInt = temp;
  60. return (temp == aValue);
  61. }
  62. // PCB_PLOT_PARAMS
  63. PCB_PLOT_PARAMS::PCB_PLOT_PARAMS()
  64. {
  65. m_useGerberProtelExtensions = false;
  66. m_useGerberAttributes = false;
  67. m_includeGerberNetlistInfo = false;
  68. m_gerberPrecision = gbrDefaultPrecision;
  69. m_excludeEdgeLayer = true;
  70. m_lineWidth = g_DrawDefaultLineThickness;
  71. m_plotFrameRef = false;
  72. m_plotViaOnMaskLayer = false;
  73. m_plotMode = FILLED;
  74. m_useAuxOrigin = false;
  75. m_HPGLPenNum = 1;
  76. m_HPGLPenSpeed = 20; // this param is always in cm/s
  77. m_HPGLPenDiam = 15; // in mils
  78. m_negative = false;
  79. m_A4Output = false;
  80. m_plotReference = true;
  81. m_plotValue = true;
  82. m_plotInvisibleText = false;
  83. m_plotPadsOnSilkLayer = false;
  84. m_subtractMaskFromSilk = false;
  85. m_format = PLOT_FORMAT_GERBER;
  86. m_mirror = false;
  87. m_drillMarks = SMALL_DRILL_SHAPE;
  88. m_autoScale = false;
  89. m_scale = 1.0;
  90. m_scaleSelection = 1;
  91. m_fineScaleAdjustX = 1.0;
  92. m_fineScaleAdjustY = 1.0;
  93. m_widthAdjust = 0.;
  94. m_outputDirectory.clear();
  95. m_color = BLACK;
  96. m_referenceColor = BLACK;
  97. m_valueColor = BLACK;
  98. m_textMode = PLOTTEXTMODE_DEFAULT;
  99. m_layerSelection = LSET( 2, F_SilkS, B_SilkS) | LSET::AllCuMask();
  100. // This parameter controls if the NPTH pads will be plotted or not
  101. // it is a "local" parameter
  102. m_skipNPTH_Pads = false;
  103. }
  104. void PCB_PLOT_PARAMS::SetGerberPrecision( int aPrecision )
  105. {
  106. // Currently geber files use mm.
  107. // accepted precision is only 6 (max value, this is the resolution of Pcbnew)
  108. // or 5, min value for professional boards, when 6 creates problems
  109. // to board makers.
  110. m_gerberPrecision = aPrecision == gbrDefaultPrecision-1 ? gbrDefaultPrecision-1 :
  111. gbrDefaultPrecision;
  112. }
  113. // PLEASE NOTE: only plot dialog options are processed
  114. void PCB_PLOT_PARAMS::Format( OUTPUTFORMATTER* aFormatter,
  115. int aNestLevel, int aControl ) const throw( IO_ERROR )
  116. {
  117. const char* falseStr = getTokenName( T_false );
  118. const char* trueStr = getTokenName( T_true );
  119. aFormatter->Print( aNestLevel, "(%s\n", getTokenName( T_pcbplotparams ) );
  120. aFormatter->Print( aNestLevel+1, "(%s 0x%s)\n", getTokenName( T_layerselection ),
  121. m_layerSelection.FmtHex().c_str() );
  122. aFormatter->Print( aNestLevel+1, "(%s %s)\n", getTokenName( T_usegerberextensions ),
  123. m_useGerberProtelExtensions ? trueStr : falseStr );
  124. if( m_useGerberAttributes ) // save this option only if active,
  125. // to avoid incompatibility with older Pcbnew version
  126. {
  127. aFormatter->Print( aNestLevel+1, "(%s %s)\n", getTokenName( T_usegerberattributes ), trueStr );
  128. if( GetIncludeGerberNetlistInfo() )
  129. aFormatter->Print( aNestLevel+1, "(%s %s)\n", getTokenName( T_usegerberadvancedattributes ), trueStr );
  130. }
  131. if( m_gerberPrecision != gbrDefaultPrecision ) // save this option only if it is not the default value,
  132. // to avoid incompatibility with older Pcbnew version
  133. aFormatter->Print( aNestLevel+1, "(%s %d)\n",
  134. getTokenName( T_gerberprecision ), m_gerberPrecision );
  135. aFormatter->Print( aNestLevel+1, "(%s %s)\n", getTokenName( T_excludeedgelayer ),
  136. m_excludeEdgeLayer ? trueStr : falseStr );
  137. aFormatter->Print( aNestLevel+1, "(%s %f)\n", getTokenName( T_linewidth ),
  138. m_lineWidth / IU_PER_MM );
  139. aFormatter->Print( aNestLevel+1, "(%s %s)\n", getTokenName( T_plotframeref ),
  140. m_plotFrameRef ? trueStr : falseStr );
  141. aFormatter->Print( aNestLevel+1, "(%s %s)\n", getTokenName( T_viasonmask ),
  142. m_plotViaOnMaskLayer ? trueStr : falseStr );
  143. aFormatter->Print( aNestLevel+1, "(%s %d)\n", getTokenName( T_mode ),
  144. GetPlotMode() == SKETCH ? 2 : 1 ); // Value 0 (LINE mode) no more used
  145. aFormatter->Print( aNestLevel+1, "(%s %s)\n", getTokenName( T_useauxorigin ),
  146. m_useAuxOrigin ? trueStr : falseStr );
  147. aFormatter->Print( aNestLevel+1, "(%s %d)\n", getTokenName( T_hpglpennumber ),
  148. m_HPGLPenNum );
  149. // Obsolete parameter, pen speed is no more managed, because hpgl format
  150. // is now an export format, and for this, pen speed has no meaning
  151. // aFormatter->Print( aNestLevel+1, "(%s %d)\n", getTokenName( T_hpglpenspeed ),
  152. // m_HPGLPenSpeed );
  153. aFormatter->Print( aNestLevel+1, "(%s %d)\n", getTokenName( T_hpglpenspeed ),
  154. m_HPGLPenSpeed );
  155. aFormatter->Print( aNestLevel+1, "(%s %d)\n", getTokenName( T_hpglpendiameter ),
  156. m_HPGLPenDiam );
  157. aFormatter->Print( aNestLevel+1, "(%s %s)\n", getTokenName( T_psnegative ),
  158. m_negative ? trueStr : falseStr );
  159. aFormatter->Print( aNestLevel+1, "(%s %s)\n", getTokenName( T_psa4output ),
  160. m_A4Output ? trueStr : falseStr );
  161. aFormatter->Print( aNestLevel+1, "(%s %s)\n", getTokenName( T_plotreference ),
  162. m_plotReference ? trueStr : falseStr );
  163. aFormatter->Print( aNestLevel+1, "(%s %s)\n", getTokenName( T_plotvalue ),
  164. m_plotValue ? trueStr : falseStr );
  165. aFormatter->Print( aNestLevel+1, "(%s %s)\n", getTokenName( T_plotinvisibletext ),
  166. m_plotInvisibleText ? trueStr : falseStr );
  167. aFormatter->Print( aNestLevel+1, "(%s %s)\n", getTokenName( T_padsonsilk ),
  168. m_plotPadsOnSilkLayer ? trueStr : falseStr );
  169. aFormatter->Print( aNestLevel+1, "(%s %s)\n", getTokenName( T_subtractmaskfromsilk ),
  170. m_subtractMaskFromSilk ? trueStr : falseStr );
  171. aFormatter->Print( aNestLevel+1, "(%s %d)\n", getTokenName( T_outputformat ),
  172. m_format );
  173. aFormatter->Print( aNestLevel+1, "(%s %s)\n", getTokenName( T_mirror ),
  174. m_mirror ? trueStr : falseStr );
  175. aFormatter->Print( aNestLevel+1, "(%s %d)\n", getTokenName( T_drillshape ),
  176. m_drillMarks );
  177. aFormatter->Print( aNestLevel+1, "(%s %d)\n", getTokenName( T_scaleselection ),
  178. m_scaleSelection );
  179. aFormatter->Print( aNestLevel+1, "(%s %s)", getTokenName( T_outputdirectory ),
  180. aFormatter->Quotew( m_outputDirectory ).c_str() );
  181. aFormatter->Print( 0, ")\n" );
  182. }
  183. void PCB_PLOT_PARAMS::Parse( PCB_PLOT_PARAMS_PARSER* aParser )
  184. throw( PARSE_ERROR, IO_ERROR )
  185. {
  186. aParser->Parse( this );
  187. }
  188. bool PCB_PLOT_PARAMS::operator==( const PCB_PLOT_PARAMS &aPcbPlotParams ) const
  189. {
  190. if( m_layerSelection != aPcbPlotParams.m_layerSelection )
  191. return false;
  192. if( m_useGerberProtelExtensions != aPcbPlotParams.m_useGerberProtelExtensions )
  193. return false;
  194. if( m_useGerberAttributes != aPcbPlotParams.m_useGerberAttributes )
  195. return false;
  196. if( m_useGerberAttributes && m_includeGerberNetlistInfo != aPcbPlotParams.m_includeGerberNetlistInfo )
  197. return false;
  198. if( m_gerberPrecision != aPcbPlotParams.m_gerberPrecision )
  199. return false;
  200. if( m_excludeEdgeLayer != aPcbPlotParams.m_excludeEdgeLayer )
  201. return false;
  202. if( m_lineWidth != aPcbPlotParams.m_lineWidth )
  203. return false;
  204. if( m_plotFrameRef != aPcbPlotParams.m_plotFrameRef )
  205. return false;
  206. if( m_plotViaOnMaskLayer != aPcbPlotParams.m_plotViaOnMaskLayer )
  207. return false;
  208. if( m_plotMode != aPcbPlotParams.m_plotMode )
  209. return false;
  210. if( m_useAuxOrigin != aPcbPlotParams.m_useAuxOrigin )
  211. return false;
  212. if( m_HPGLPenNum != aPcbPlotParams.m_HPGLPenNum )
  213. return false;
  214. if( m_HPGLPenSpeed != aPcbPlotParams.m_HPGLPenSpeed )
  215. return false;
  216. if( m_HPGLPenDiam != aPcbPlotParams.m_HPGLPenDiam )
  217. return false;
  218. if( m_negative != aPcbPlotParams.m_negative )
  219. return false;
  220. if( m_A4Output != aPcbPlotParams.m_A4Output )
  221. return false;
  222. if( m_plotReference != aPcbPlotParams.m_plotReference )
  223. return false;
  224. if( m_plotValue != aPcbPlotParams.m_plotValue )
  225. return false;
  226. if( m_plotInvisibleText != aPcbPlotParams.m_plotInvisibleText )
  227. return false;
  228. if( m_plotPadsOnSilkLayer != aPcbPlotParams.m_plotPadsOnSilkLayer )
  229. return false;
  230. if( m_subtractMaskFromSilk != aPcbPlotParams.m_subtractMaskFromSilk )
  231. return false;
  232. if( m_format != aPcbPlotParams.m_format )
  233. return false;
  234. if( m_mirror != aPcbPlotParams.m_mirror )
  235. return false;
  236. if( m_drillMarks != aPcbPlotParams.m_drillMarks )
  237. return false;
  238. if( m_scaleSelection != aPcbPlotParams.m_scaleSelection )
  239. return false;
  240. if( m_autoScale != aPcbPlotParams.m_autoScale )
  241. return false;
  242. if( m_scale != aPcbPlotParams.m_scale )
  243. return false;
  244. if( m_fineScaleAdjustX != aPcbPlotParams.m_fineScaleAdjustX )
  245. return false;
  246. if( m_fineScaleAdjustY != aPcbPlotParams.m_fineScaleAdjustY )
  247. return false;
  248. if( m_widthAdjust != aPcbPlotParams.m_widthAdjust )
  249. return false;
  250. if( m_color != aPcbPlotParams.m_color )
  251. return false;
  252. if( m_referenceColor != aPcbPlotParams.m_referenceColor )
  253. return false;
  254. if( m_valueColor != aPcbPlotParams.m_valueColor )
  255. return false;
  256. if( m_textMode != aPcbPlotParams.m_textMode )
  257. return false;
  258. if( !m_outputDirectory.IsSameAs( aPcbPlotParams.m_outputDirectory ) )
  259. return false;
  260. return true;
  261. }
  262. bool PCB_PLOT_PARAMS::operator!=( const PCB_PLOT_PARAMS &aPcbPlotParams ) const
  263. {
  264. return !( *this == aPcbPlotParams );
  265. }
  266. bool PCB_PLOT_PARAMS::SetHPGLPenDiameter( int aValue )
  267. {
  268. return setInt( &m_HPGLPenDiam, aValue, HPGL_PEN_DIAMETER_MIN, HPGL_PEN_DIAMETER_MAX );
  269. }
  270. bool PCB_PLOT_PARAMS::SetHPGLPenSpeed( int aValue )
  271. {
  272. return setInt( &m_HPGLPenSpeed, aValue, HPGL_PEN_SPEED_MIN, HPGL_PEN_SPEED_MAX );
  273. }
  274. bool PCB_PLOT_PARAMS::SetLineWidth( int aValue )
  275. {
  276. return setInt( &m_lineWidth, aValue, PLOT_LINEWIDTH_MIN, PLOT_LINEWIDTH_MAX );
  277. }
  278. // PCB_PLOT_PARAMS_PARSER
  279. PCB_PLOT_PARAMS_PARSER::PCB_PLOT_PARAMS_PARSER( LINE_READER* aReader ) :
  280. PCB_PLOT_PARAMS_LEXER( aReader )
  281. {
  282. }
  283. PCB_PLOT_PARAMS_PARSER::PCB_PLOT_PARAMS_PARSER( char* aLine, const wxString& aSource ) :
  284. PCB_PLOT_PARAMS_LEXER( aLine, aSource )
  285. {
  286. }
  287. void PCB_PLOT_PARAMS_PARSER::Parse( PCB_PLOT_PARAMS* aPcbPlotParams )
  288. throw( PARSE_ERROR, IO_ERROR )
  289. {
  290. T token;
  291. while( ( token = NextTok() ) != T_RIGHT )
  292. {
  293. if( token == T_EOF)
  294. Unexpected( T_EOF );
  295. if( token == T_LEFT )
  296. token = NextTok();
  297. if( token == T_pcbplotparams )
  298. continue;
  299. bool skip_right = false;
  300. switch( token )
  301. {
  302. case T_layerselection:
  303. {
  304. token = NeedSYMBOLorNUMBER();
  305. const std::string& cur = CurStr();
  306. if( token == T_NUMBER ) // pretty 3 format had legacy Cu stack.
  307. {
  308. // unsigned legacy_mask = atol( cur.c_str() );
  309. /* It's not possible to convert a legacy Cu layer number to a new
  310. Cu layer number without knowing the number or total Cu layers
  311. in the legacy board. We do not have that information here.
  312. So simply set all layers ON. User can turn them off in the UI.
  313. This is one of the superiorities of the new Cu sequence.
  314. aPcbPlotParams->m_layerSelection = LEGACY_PLUGIN::leg_mask2new( cu_count, legacy_mask );
  315. */
  316. // sorry, use the UI once to fix:
  317. aPcbPlotParams->m_layerSelection = LSET( 2, F_SilkS, B_SilkS) | LSET::AllCuMask();
  318. }
  319. else if( cur.find_first_of( "0x" ) == 0 ) // pretty ver. 4.
  320. {
  321. // skip the leading 2 0x bytes.
  322. aPcbPlotParams->m_layerSelection.ParseHex( cur.c_str()+2, cur.size()-2 );
  323. }
  324. else
  325. Expecting( "integer or hex layerSelection" );
  326. }
  327. break;
  328. case T_usegerberextensions:
  329. aPcbPlotParams->m_useGerberProtelExtensions = parseBool();
  330. break;
  331. case T_usegerberattributes:
  332. aPcbPlotParams->m_useGerberAttributes = parseBool();
  333. break;
  334. case T_usegerberadvancedattributes:
  335. aPcbPlotParams->m_includeGerberNetlistInfo = parseBool();
  336. break;
  337. case T_gerberprecision:
  338. aPcbPlotParams->m_gerberPrecision =
  339. parseInt( gbrDefaultPrecision-1, gbrDefaultPrecision);
  340. break;
  341. case T_psa4output:
  342. aPcbPlotParams->m_A4Output = parseBool();
  343. break;
  344. case T_excludeedgelayer:
  345. aPcbPlotParams->m_excludeEdgeLayer = parseBool();
  346. break;
  347. case T_linewidth:
  348. {
  349. // Due to a bug, this (minor) parameter was saved in biu
  350. // and now is saved in mm
  351. // If the read value is outside bounds, force a default value
  352. double tmp = parseDouble();
  353. if( !aPcbPlotParams->SetLineWidth( KiROUND( tmp * IU_PER_MM ) ) )
  354. aPcbPlotParams->SetLineWidth( PLOT_LINEWIDTH_DEFAULT );
  355. }
  356. break;
  357. case T_plotframeref:
  358. aPcbPlotParams->m_plotFrameRef = parseBool();
  359. break;
  360. case T_viasonmask:
  361. aPcbPlotParams->m_plotViaOnMaskLayer = parseBool();
  362. break;
  363. case T_mode:
  364. aPcbPlotParams->SetPlotMode( parseInt( 0, 2 ) > 1 ? SKETCH : FILLED );
  365. break;
  366. case T_useauxorigin:
  367. aPcbPlotParams->m_useAuxOrigin = parseBool();
  368. break;
  369. case T_hpglpennumber:
  370. aPcbPlotParams->m_HPGLPenNum = parseInt( HPGL_PEN_NUMBER_MIN,
  371. HPGL_PEN_NUMBER_MAX );
  372. break;
  373. case T_hpglpenspeed:
  374. aPcbPlotParams->m_HPGLPenSpeed = parseInt( HPGL_PEN_SPEED_MIN,
  375. HPGL_PEN_SPEED_MAX );
  376. break;
  377. case T_hpglpendiameter:
  378. aPcbPlotParams->m_HPGLPenDiam = parseInt( HPGL_PEN_DIAMETER_MIN,
  379. HPGL_PEN_DIAMETER_MAX );
  380. break;
  381. case T_hpglpenoverlay:
  382. // No more used. juste here for compatibility with old versions
  383. parseInt( 0, HPGL_PEN_DIAMETER_MAX );
  384. break;
  385. case T_pscolor:
  386. NeedSYMBOL(); // This actually was never used...
  387. break;
  388. case T_psnegative:
  389. aPcbPlotParams->m_negative = parseBool();
  390. break;
  391. case T_plotreference:
  392. aPcbPlotParams->m_plotReference = parseBool();
  393. break;
  394. case T_plotvalue:
  395. aPcbPlotParams->m_plotValue = parseBool();
  396. break;
  397. case T_plotinvisibletext:
  398. aPcbPlotParams->m_plotInvisibleText = parseBool();
  399. break;
  400. case T_padsonsilk:
  401. aPcbPlotParams->m_plotPadsOnSilkLayer= parseBool();
  402. break;
  403. case T_subtractmaskfromsilk:
  404. aPcbPlotParams->m_subtractMaskFromSilk = parseBool();
  405. break;
  406. case T_outputformat:
  407. aPcbPlotParams->m_format = static_cast<PlotFormat>(
  408. parseInt( PLOT_FIRST_FORMAT, PLOT_LAST_FORMAT ) );
  409. break;
  410. case T_mirror:
  411. aPcbPlotParams->m_mirror = parseBool();
  412. break;
  413. case T_drillshape:
  414. aPcbPlotParams->m_drillMarks = static_cast<PCB_PLOT_PARAMS::DrillMarksType>
  415. ( parseInt( 0, 2 ) );
  416. break;
  417. case T_scaleselection:
  418. aPcbPlotParams->m_scaleSelection = parseInt( 0, 4 );
  419. break;
  420. case T_outputdirectory:
  421. NeedSYMBOL();
  422. aPcbPlotParams->m_outputDirectory = FROM_UTF8( CurText() );
  423. break;
  424. default:
  425. skipCurrent(); // skip unknown or outdated plot parameter
  426. skip_right = true; // the closing right token is already read.
  427. break;
  428. }
  429. if( ! skip_right )
  430. NeedRIGHT();
  431. }
  432. }
  433. bool PCB_PLOT_PARAMS_PARSER::parseBool()
  434. {
  435. T token = NeedSYMBOL();
  436. if( token != T_false && token != T_true )
  437. Expecting( "true|false" );
  438. return token == T_true;
  439. }
  440. int PCB_PLOT_PARAMS_PARSER::parseInt( int aMin, int aMax )
  441. {
  442. T token = NextTok();
  443. if( token != T_NUMBER )
  444. Expecting( T_NUMBER );
  445. int val = atoi( CurText() );
  446. if( val < aMin )
  447. val = aMin;
  448. else if( val > aMax )
  449. val = aMax;
  450. return val;
  451. }
  452. double PCB_PLOT_PARAMS_PARSER::parseDouble()
  453. {
  454. T token = NextTok();
  455. if( token != T_NUMBER )
  456. Expecting( T_NUMBER );
  457. double val = strtod( CurText(), NULL );
  458. return val;
  459. }
  460. void PCB_PLOT_PARAMS_PARSER::skipCurrent() throw( IO_ERROR, PARSE_ERROR )
  461. {
  462. int curr_level = 0;
  463. T token;
  464. while( ( token = NextTok() ) != T_EOF )
  465. {
  466. if( token == T_LEFT )
  467. curr_level--;
  468. if( token == T_RIGHT )
  469. {
  470. curr_level++;
  471. if( curr_level > 0 )
  472. return;
  473. }
  474. }
  475. }