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.

512 lines
16 KiB

4 years ago
4 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
4 years ago
4 years ago
11 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  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. #include <plotters/plotter.h>
  26. #include <pcbplot.h>
  27. #include <base_units.h>
  28. #include <locale_io.h>
  29. #include <reporter.h>
  30. #include <board.h>
  31. #include <board_design_settings.h>
  32. #include <plotcontroller.h>
  33. #include <pcb_plot_params.h>
  34. #include <wx/ffile.h>
  35. #include <dialog_plot.h>
  36. #include <build_version.h>
  37. #include <gbr_metadata.h>
  38. #include <render_settings.h>
  39. const wxString GetGerberProtelExtension( int aLayer )
  40. {
  41. if( IsCopperLayer( aLayer ) )
  42. {
  43. if( aLayer == F_Cu )
  44. return wxT( "gtl" );
  45. else if( aLayer == B_Cu )
  46. return wxT( "gbl" );
  47. else
  48. return wxString::Format( wxT( "g%d" ), aLayer+1 );
  49. }
  50. else
  51. {
  52. switch( aLayer )
  53. {
  54. case B_Adhes: return wxT( "gba" );
  55. case F_Adhes: return wxT( "gta" );
  56. case B_Paste: return wxT( "gbp" );
  57. case F_Paste: return wxT( "gtp" );
  58. case B_SilkS: return wxT( "gbo" );
  59. case F_SilkS: return wxT( "gto" );
  60. case B_Mask: return wxT( "gbs" );
  61. case F_Mask: return wxT( "gts" );
  62. case Edge_Cuts: return wxT( "gm1" );
  63. case Dwgs_User:
  64. case Cmts_User:
  65. case Eco1_User:
  66. case Eco2_User:
  67. default: return wxT( "gbr" );
  68. }
  69. }
  70. }
  71. const wxString GetGerberFileFunctionAttribute( const BOARD* aBoard, int aLayer )
  72. {
  73. wxString attrib;
  74. switch( aLayer )
  75. {
  76. case F_Adhes:
  77. attrib = wxT( "Glue,Top" );
  78. break;
  79. case B_Adhes:
  80. attrib = wxT( "Glue,Bot" );
  81. break;
  82. case F_SilkS:
  83. attrib = wxT( "Legend,Top" );
  84. break;
  85. case B_SilkS:
  86. attrib = wxT( "Legend,Bot" );
  87. break;
  88. case F_Mask:
  89. attrib = wxT( "Soldermask,Top" );
  90. break;
  91. case B_Mask:
  92. attrib = wxT( "Soldermask,Bot" );
  93. break;
  94. case F_Paste:
  95. attrib = wxT( "Paste,Top" );
  96. break;
  97. case B_Paste:
  98. attrib = wxT( "Paste,Bot" );
  99. break;
  100. case Edge_Cuts:
  101. // Board outline.
  102. // Can be "Profile,NP" (Not Plated: usual) or "Profile,P"
  103. // This last is the exception (Plated)
  104. attrib = wxT( "Profile,NP" );
  105. break;
  106. case Dwgs_User:
  107. attrib = wxT( "OtherDrawing,Comment" );
  108. break;
  109. case Cmts_User:
  110. attrib = wxT( "Other,Comment" );
  111. break;
  112. case Eco1_User:
  113. attrib = wxT( "Other,ECO1" );
  114. break;
  115. case Eco2_User:
  116. attrib = wxT( "Other,ECO2" );
  117. break;
  118. case B_Fab:
  119. // This is actually a assembly layer
  120. attrib = wxT( "AssemblyDrawing,Bot" );
  121. break;
  122. case F_Fab:
  123. // This is actually a assembly layer
  124. attrib = wxT( "AssemblyDrawing,Top" );
  125. break;
  126. case B_Cu:
  127. attrib.Printf( wxT( "Copper,L%d,Bot" ), aBoard->GetCopperLayerCount() );
  128. break;
  129. case F_Cu:
  130. attrib = wxT( "Copper,L1,Top" );
  131. break;
  132. default:
  133. if( IsCopperLayer( aLayer ) )
  134. attrib.Printf( wxT( "Copper,L%d,Inr" ), aLayer+1 );
  135. else
  136. attrib.Printf( wxT( "Other,User" ), aLayer+1 );
  137. break;
  138. }
  139. // This code adds a optional parameter: the type of copper layers.
  140. // Because it is not used by Pcbnew (it can be used only by external autorouters)
  141. // user do not really set this parameter.
  142. // Therefore do not add it.
  143. // However, this code is left here, for perhaps a future usage.
  144. #if 0
  145. // Add the signal type of the layer, if relevant
  146. if( IsCopperLayer( aLayer ) )
  147. {
  148. LAYER_T type = aBoard->GetLayerType( ToLAYER_ID( aLayer ) );
  149. switch( type )
  150. {
  151. case LT_SIGNAL:
  152. attrib += wxT( ",Signal" );
  153. break;
  154. case LT_POWER:
  155. attrib += wxT( ",Plane" );
  156. break;
  157. case LT_MIXED:
  158. attrib += wxT( ",Mixed" );
  159. break;
  160. default:
  161. break; // do nothing (but avoid a warning for unhandled LAYER_T values from GCC)
  162. }
  163. }
  164. #endif
  165. wxString fileFct;
  166. fileFct.Printf( wxT( "%%TF.FileFunction,%s*%%" ), attrib );
  167. return fileFct;
  168. }
  169. static const wxString GetGerberFilePolarityAttribute( int aLayer )
  170. {
  171. /* build the string %TF.FilePolarity,Positive*%
  172. * or %TF.FilePolarity,Negative*%
  173. * an emply string for layers which do not use a polarity
  174. *
  175. * The value of the .FilePolarity specifies whether the image represents the
  176. * presence or absence of material.
  177. * This attribute can only be used when the file represents a pattern in a material layer,
  178. * e.g. copper, solder mask, legend.
  179. * Together with.FileFunction it defines the role of that image in
  180. * the layer structure of the PCB.
  181. * Note that the .FilePolarity attribute does not change the image -
  182. * no attribute does.
  183. * It changes the interpretation of the image.
  184. * For example, in a copper layer in positive polarity a round flash generates a copper pad.
  185. * In a copper layer in negative polarity it generates a clearance.
  186. * Solder mask images usually represent solder mask openings and are then negative.
  187. * This may be counter-intuitive.
  188. */
  189. int polarity = 0;
  190. switch( aLayer )
  191. {
  192. case F_Adhes:
  193. case B_Adhes:
  194. case F_SilkS:
  195. case B_SilkS:
  196. case F_Paste:
  197. case B_Paste:
  198. polarity = 1;
  199. break;
  200. case F_Mask:
  201. case B_Mask:
  202. polarity = -1;
  203. break;
  204. default:
  205. if( IsCopperLayer( aLayer ) )
  206. polarity = 1;
  207. break;
  208. }
  209. wxString filePolarity;
  210. if( polarity == 1 )
  211. filePolarity = wxT( "%TF.FilePolarity,Positive*%" );
  212. if( polarity == -1 )
  213. filePolarity = wxT( "%TF.FilePolarity,Negative*%" );
  214. return filePolarity;
  215. }
  216. /* Add some X2 attributes to the file header, as defined in the
  217. * Gerber file format specification J4 and "Revision 2015.06"
  218. */
  219. // A helper function to convert a X2 attribute string to a X1 structured comment:
  220. static wxString& makeStringCompatX1( wxString& aText, bool aUseX1CompatibilityMode )
  221. {
  222. if( aUseX1CompatibilityMode )
  223. {
  224. aText.Replace( wxT( "%" ), wxEmptyString );
  225. aText.Prepend( wxT( "G04 #@! " ) );
  226. }
  227. return aText;
  228. }
  229. void AddGerberX2Header( PLOTTER* aPlotter, const BOARD* aBoard, bool aUseX1CompatibilityMode )
  230. {
  231. wxString text;
  232. // Creates the TF,.GenerationSoftware. Format is:
  233. // %TF,.GenerationSoftware,<vendor>,<application name>[,<application version>]*%
  234. text.Printf( wxT( "%%TF.GenerationSoftware,KiCad,Pcbnew,%s*%%" ), GetBuildVersion() );
  235. aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
  236. // creates the TF.CreationDate attribute:
  237. text = GbrMakeCreationDateAttributeString( aUseX1CompatibilityMode ? GBR_NC_STRING_FORMAT_X1
  238. : GBR_NC_STRING_FORMAT_X2 );
  239. aPlotter->AddLineToHeader( text );
  240. // Creates the TF,.ProjectId. Format is (from Gerber file format doc):
  241. // %TF.ProjectId,<project id>,<project GUID>,<revision id>*%
  242. // <project id> is the name of the project, restricted to basic ASCII symbols only,
  243. // Rem: <project id> accepts only ASCII 7 code (only basic ASCII codes are allowed in
  244. // gerber files) and comma not accepted.
  245. // All illegal chars will be replaced by underscore.
  246. //
  247. // <project GUID> is a string which is an unique id of a project.
  248. // However Kicad does not handle such a project GUID, so it is built from the board name
  249. wxFileName fn = aBoard->GetFileName();
  250. wxString msg = fn.GetFullName();
  251. // Build a <project GUID>, from the board name
  252. wxString guid = GbrMakeProjectGUIDfromString( msg );
  253. // build the <project id> string: this is the board short filename (without ext)
  254. // and all non ASCII chars and comma are replaced by '_'
  255. msg = fn.GetName();
  256. msg.Replace( wxT( "," ), wxT( "_" ) );
  257. // build the <revision id> string. All non ASCII chars and comma are replaced by '_'
  258. wxString rev = ExpandTextVars( aBoard->GetTitleBlock().GetRevision(), aBoard->GetProject() );
  259. rev.Replace( wxT( "," ), wxT( "_" ) );
  260. if( rev.IsEmpty() )
  261. rev = wxT( "rev?" );
  262. text.Printf( wxT( "%%TF.ProjectId,%s,%s,%s*%%" ), msg.ToAscii(), guid, rev.ToAscii() );
  263. aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
  264. // Add the TF.SameCoordinates to specify that all gerber files uses the same origin and
  265. // orientation, and the registration between files is OK.
  266. // The parameter of TF.SameCoordinates is a string that is common to all files using the
  267. // same registration. The string value has no meaning; it is just a key.
  268. // Because there is no mirroring/rotation in Kicad, only the plot offset origin can create
  269. // incorrect registration, so we create a key from plot offset options.
  270. //
  271. // Currently the key is "Original" when using absolute Pcbnew coordinates, and the PY and PY
  272. // position of the auxiliary axis when using it.
  273. // If we ever add user-settable absolute Pcbnew coordinates, we'll need to change the way
  274. // the key is built to ensure file only using the *same* axis have the same key.
  275. wxString registration_id = wxT( "Original" );
  276. VECTOR2I auxOrigin = aBoard->GetDesignSettings().GetAuxOrigin();
  277. if( aBoard->GetPlotOptions().GetUseAuxOrigin() && auxOrigin.x && auxOrigin.y )
  278. registration_id.Printf( wxT( "PX%xPY%x" ), auxOrigin.x, auxOrigin.y );
  279. text.Printf( wxT( "%%TF.SameCoordinates,%s*%%" ), registration_id.GetData() );
  280. aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
  281. }
  282. void AddGerberX2Attribute( PLOTTER* aPlotter, const BOARD* aBoard, int aLayer,
  283. bool aUseX1CompatibilityMode )
  284. {
  285. AddGerberX2Header( aPlotter, aBoard, aUseX1CompatibilityMode );
  286. wxString text;
  287. // Add the TF.FileFunction
  288. text = GetGerberFileFunctionAttribute( aBoard, aLayer );
  289. aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
  290. // Add the TF.FilePolarity (for layers which support that)
  291. text = GetGerberFilePolarityAttribute( aLayer );
  292. if( !text.IsEmpty() )
  293. aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
  294. }
  295. void BuildPlotFileName( wxFileName* aFilename, const wxString& aOutputDir,
  296. const wxString& aSuffix, const wxString& aExtension )
  297. {
  298. // aFilename contains the base filename only (without path and extension)
  299. // when calling this function.
  300. // It is expected to be a valid filename (this is usually the board filename)
  301. aFilename->SetPath( aOutputDir );
  302. // Set the file extension
  303. aFilename->SetExt( aExtension );
  304. // remove leading and trailing spaces if any from the suffix, if
  305. // something survives add it to the name;
  306. // also the suffix can contain some not allowed chars in filename (/ \ . : and some others),
  307. // so change them to underscore
  308. // Remember it can be called from a python script, so the illegal chars
  309. // have to be filtered here.
  310. wxString suffix = aSuffix;
  311. suffix.Trim( true );
  312. suffix.Trim( false );
  313. wxString badchars = wxFileName::GetForbiddenChars(wxPATH_DOS);
  314. badchars.Append( "%." );
  315. for( unsigned ii = 0; ii < badchars.Len(); ii++ )
  316. suffix.Replace( badchars[ii], wxT("_") );
  317. if( !suffix.IsEmpty() )
  318. aFilename->SetName( aFilename->GetName() + wxT( "-" ) + suffix );
  319. }
  320. PLOT_CONTROLLER::PLOT_CONTROLLER( BOARD* aBoard )
  321. {
  322. m_plotter = nullptr;
  323. m_board = aBoard;
  324. m_plotLayer = UNDEFINED_LAYER;
  325. }
  326. PLOT_CONTROLLER::~PLOT_CONTROLLER()
  327. {
  328. ClosePlot();
  329. }
  330. /*
  331. * IMPORTANT: the locale during plots *MUST* be kept as C/POSIX using a LOCALE_IO object on the
  332. * stack. This even when opening/closing the plotfile, as some drivers do I/O even then.
  333. */
  334. void PLOT_CONTROLLER::ClosePlot()
  335. {
  336. LOCALE_IO toggle;
  337. if( m_plotter )
  338. {
  339. m_plotter->EndPlot();
  340. delete m_plotter->RenderSettings();
  341. delete m_plotter;
  342. m_plotter = nullptr;
  343. }
  344. }
  345. bool PLOT_CONTROLLER::OpenPlotfile( const wxString& aSuffix, PLOT_FORMAT aFormat,
  346. const wxString& aSheetName, const wxString& aSheetPath )
  347. {
  348. LOCALE_IO toggle;
  349. // Save the current format: sadly some plot routines depends on this but the main reason
  350. // is that the StartPlot method uses it to dispatch the plotter creation
  351. GetPlotOptions().SetFormat( aFormat );
  352. // Ensure that the previous plot is closed
  353. ClosePlot();
  354. // Now compute the full filename for the output and start the plot (after ensuring the
  355. // output directory is OK).
  356. std::function<bool( wxString* )> textResolver =
  357. [&]( wxString* token ) -> bool
  358. {
  359. // Handles m_board->GetTitleBlock() *and* m_board->GetProject()
  360. return m_board->ResolveTextVar( token, 0 );
  361. };
  362. wxString outputDirName = GetPlotOptions().GetOutputDirectory();
  363. outputDirName = ExpandTextVars( outputDirName, &textResolver, nullptr, nullptr );
  364. outputDirName = ExpandEnvVarSubstitutions( outputDirName, nullptr );
  365. wxFileName outputDir = wxFileName::DirName( outputDirName );
  366. wxString boardFilename = m_board->GetFileName();
  367. if( EnsureFileDirectoryExists( &outputDir, boardFilename ) )
  368. {
  369. // outputDir contains now the full path of plot files
  370. m_plotFile = boardFilename;
  371. m_plotFile.SetPath( outputDir.GetPath() );
  372. wxString fileExt = GetDefaultPlotExtension( aFormat );
  373. // Gerber format *can* use layer-specific file extensions (this is no longer best
  374. // practice as the official file ext is now .gbr).
  375. if( GetPlotOptions().GetFormat() == PLOT_FORMAT::GERBER
  376. && GetPlotOptions().GetUseGerberProtelExtensions() )
  377. {
  378. fileExt = GetGerberProtelExtension( GetLayer() );
  379. }
  380. // Build plot filenames from the board name and layer names:
  381. BuildPlotFileName( &m_plotFile, outputDir.GetPath(), aSuffix, fileExt );
  382. m_plotter = StartPlotBoard( m_board, &GetPlotOptions(), ToLAYER_ID( GetLayer() ),
  383. m_plotFile.GetFullPath(), aSheetName, aSheetPath );
  384. }
  385. return ( m_plotter != nullptr );
  386. }
  387. bool PLOT_CONTROLLER::PlotLayer()
  388. {
  389. LOCALE_IO toggle;
  390. // No plot open, nothing to do...
  391. if( !m_plotter )
  392. return false;
  393. // Fully delegated to the parent
  394. PlotOneBoardLayer( m_board, m_plotter, ToLAYER_ID( GetLayer() ), GetPlotOptions() );
  395. return true;
  396. }
  397. void PLOT_CONTROLLER::SetColorMode( bool aColorMode )
  398. {
  399. if( !m_plotter )
  400. return;
  401. m_plotter->SetColorMode( aColorMode );
  402. }
  403. bool PLOT_CONTROLLER::GetColorMode()
  404. {
  405. if( !m_plotter )
  406. return false;
  407. return m_plotter->GetColorMode();
  408. }