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.

492 lines
14 KiB

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