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.

432 lines
15 KiB

  1. /**
  2. * @file gerber_file_image.cpp
  3. * a GERBER class handle for a given layer info about used D_CODES and how the layer is drawn
  4. */
  5. /*
  6. * This program source code file is part of KiCad, a free EDA CAD application.
  7. *
  8. * Copyright (C) 1992-2016 Jean-Pierre Charras jp.charras at wanadoo.fr
  9. * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, you may find one here:
  23. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  24. * or you may search the http://www.gnu.org website for the version 2 license,
  25. * or you may write to the Free Software Foundation, Inc.,
  26. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  27. */
  28. #include <fctsys.h>
  29. #include <common.h>
  30. #include <class_drawpanel.h>
  31. #include <macros.h>
  32. #include <convert_to_biu.h>
  33. #include <gerbview.h>
  34. #include <gerbview_frame.h>
  35. #include <gerber_file_image.h>
  36. #include <X2_gerber_attributes.h>
  37. #include <algorithm>
  38. #include <map>
  39. /**
  40. * Function scaletoIU
  41. * converts a distance given in floating point to our internal units
  42. */
  43. extern int scaletoIU( double aCoord, bool isMetric ); // defined it rs274d_read_XY_and_IJ_coordiantes.cpp
  44. /* Format Gerber: NOTES:
  45. * Tools and D_CODES
  46. * tool number (identification of shapes)
  47. * 1 to 999
  48. *
  49. * D_CODES:
  50. * D01 ... D9 = action codes:
  51. * D01 = activating light (lower pen) when di placement
  52. * D02 = light extinction (lift pen) when di placement
  53. * D03 Flash
  54. * D09 = VAPE Flash
  55. * D10 ... = Indentification Tool (Opening)
  56. *
  57. * For tools:
  58. * DCode min = D10
  59. * DCode max = 999
  60. */
  61. GERBER_LAYER::GERBER_LAYER()
  62. {
  63. ResetDefaultValues();
  64. }
  65. GERBER_LAYER::~GERBER_LAYER()
  66. {
  67. }
  68. void GERBER_LAYER::ResetDefaultValues()
  69. {
  70. m_LayerName = wxT( "no name" ); // Layer name from the LN command
  71. m_LayerNegative = false; // true = Negative Layer
  72. m_StepForRepeat.x = m_StepForRepeat.y = 0; // X and Y offsets for Step and Repeat command
  73. m_XRepeatCount = 1; // The repeat count on X axis
  74. m_YRepeatCount = 1; // The repeat count on Y axis
  75. m_StepForRepeatMetric = false; // false = Inches, true = metric
  76. }
  77. GERBER_FILE_IMAGE::GERBER_FILE_IMAGE( int aLayer ) :
  78. EDA_ITEM( (EDA_ITEM*)NULL, GERBER_IMAGE_T )
  79. {
  80. m_GraphicLayer = aLayer; // Graphic layer Number
  81. m_IsVisible = true; // must be drawn
  82. m_PositiveDrawColor = WHITE; // The color used to draw positive items for this image
  83. m_Selected_Tool = 0;
  84. m_FileFunction = NULL; // file function parameters
  85. ResetDefaultValues();
  86. for( unsigned ii = 0; ii < arrayDim( m_Aperture_List ); ii++ )
  87. m_Aperture_List[ii] = 0;
  88. }
  89. GERBER_FILE_IMAGE::~GERBER_FILE_IMAGE()
  90. {
  91. m_Drawings.DeleteAll();
  92. for( unsigned ii = 0; ii < arrayDim( m_Aperture_List ); ii++ )
  93. {
  94. delete m_Aperture_List[ii];
  95. }
  96. delete m_FileFunction;
  97. }
  98. /*
  99. * Function GetItemsList
  100. * returns the first GERBER_DRAW_ITEM * item of the items list
  101. */
  102. GERBER_DRAW_ITEM * GERBER_FILE_IMAGE::GetItemsList()
  103. {
  104. return m_Drawings;
  105. }
  106. D_CODE* GERBER_FILE_IMAGE::GetDCODEOrCreate( int aDCODE, bool aCreateIfNoExist )
  107. {
  108. unsigned ndx = aDCODE - FIRST_DCODE;
  109. if( ndx < (unsigned) arrayDim( m_Aperture_List ) )
  110. {
  111. // lazily create the D_CODE if it does not exist.
  112. if( aCreateIfNoExist )
  113. {
  114. if( m_Aperture_List[ndx] == NULL )
  115. m_Aperture_List[ndx] = new D_CODE( ndx + FIRST_DCODE );
  116. }
  117. return m_Aperture_List[ndx];
  118. }
  119. return NULL;
  120. }
  121. D_CODE* GERBER_FILE_IMAGE::GetDCODE( int aDCODE ) const
  122. {
  123. unsigned ndx = aDCODE - FIRST_DCODE;
  124. if( ndx < (unsigned) arrayDim( m_Aperture_List ) )
  125. {
  126. return m_Aperture_List[ndx];
  127. }
  128. return NULL;
  129. }
  130. APERTURE_MACRO* GERBER_FILE_IMAGE::FindApertureMacro( const APERTURE_MACRO& aLookup )
  131. {
  132. APERTURE_MACRO_SET::iterator iter = m_aperture_macros.find( aLookup );
  133. if( iter != m_aperture_macros.end() )
  134. {
  135. APERTURE_MACRO* pam = (APERTURE_MACRO*) &(*iter);
  136. return pam;
  137. }
  138. return NULL; // not found
  139. }
  140. void GERBER_FILE_IMAGE::ResetDefaultValues()
  141. {
  142. m_InUse = false;
  143. m_GBRLayerParams.ResetDefaultValues();
  144. m_FileName.Empty();
  145. m_ImageName = wxT( "no name" ); // Image name from the IN command
  146. m_ImageNegative = false; // true = Negative image
  147. m_IsX2_file = false; // true only if a %TF, %TA or %TD command
  148. delete m_FileFunction; // file function parameters
  149. m_FileFunction = NULL;
  150. m_MD5_value.Empty(); // MD5 value found in a %TF.MD5 command
  151. m_PartString.Empty(); // string found in a %TF.Part command
  152. m_hasNegativeItems = -1; // set to uninitialized
  153. m_ImageJustifyOffset = wxPoint(0,0); // Image justify Offset
  154. m_ImageJustifyXCenter = false; // Image Justify Center on X axis (default = false)
  155. m_ImageJustifyYCenter = false; // Image Justify Center on Y axis (default = false)
  156. m_GerbMetric = false; // false = Inches (default), true = metric
  157. m_Relative = false; // false = absolute Coord,
  158. // true = relative Coord
  159. m_NoTrailingZeros = false; // true: trailing zeros deleted
  160. m_ImageOffset.x = m_ImageOffset.y = 0; // Coord Offset, from IO command
  161. m_ImageRotation = 0; // Allowed 0, 90, 180, 270 (in degree)
  162. m_LocalRotation = 0.0; // Layer totation from RO command (in 0.1 degree)
  163. m_Offset.x = 0;
  164. m_Offset.y = 0; // Coord Offset, from OF command
  165. m_Scale.x = m_Scale.y = 1.0; // scale (A and B) this layer
  166. m_MirrorA = false; // true: miror / axe A (default = X)
  167. m_MirrorB = false; // true: miror / axe B (default = Y)
  168. m_SwapAxis = false; // false if A = X, B = Y; true if A =Y, B = Y
  169. m_Has_DCode = false; // true = DCodes in file
  170. // false = no DCode->
  171. // search for separate DCode file
  172. m_FmtScale.x = m_FmtScale.y = 4; // Initialize default format to 3.4 => 4
  173. m_FmtLen.x = m_FmtLen.y = 3 + 4; // Initialize default format len = 3+4
  174. m_Iterpolation = GERB_INTERPOL_LINEAR_1X; // Linear, 90 arc, Circ.
  175. m_360Arc_enbl = false; // 360 deg circular
  176. // interpolation disable
  177. m_Current_Tool = 0; // Current Dcode selected
  178. m_CommandState = 0; // State of the current command
  179. m_CurrentPos.x = m_CurrentPos.y = 0; // current specified coord
  180. m_PreviousPos.x = m_PreviousPos.y = 0; // last specified coord
  181. m_IJPos.x = m_IJPos.y = 0; // current centre coord for
  182. // plot arcs & circles
  183. m_LastCoordIsIJPos = false; // True only after a IJ coordinate is read
  184. m_ArcRadius = 0; // radius of arcs in circular interpol (given by A## command).
  185. // in command like X##Y##A##
  186. m_LastArcDataType = ARC_INFO_TYPE_NONE; // Extra coordinate info type for arcs
  187. // (radius or IJ center coord)
  188. m_LineNum = 0; // line number in file being read
  189. m_Current_File = NULL; // Gerber file to read
  190. m_PolygonFillMode = false;
  191. m_PolygonFillModeState = 0;
  192. m_Selected_Tool = 0;
  193. m_Last_Pen_Command = 0;
  194. m_Exposure = false;
  195. }
  196. /* Function HasNegativeItems
  197. * return true if at least one item must be drawn in background color
  198. * used to optimize screen refresh
  199. */
  200. bool GERBER_FILE_IMAGE::HasNegativeItems()
  201. {
  202. if( m_hasNegativeItems < 0 ) // negative items are not yet searched: find them if any
  203. {
  204. if( m_ImageNegative ) // A negative layer is expected having always negative objects.
  205. m_hasNegativeItems = 1;
  206. else
  207. {
  208. m_hasNegativeItems = 0;
  209. for( GERBER_DRAW_ITEM* item = GetItemsList(); item; item = item->Next() )
  210. {
  211. if( item->GetLayer() != m_GraphicLayer )
  212. continue;
  213. if( item->HasNegativeItems() )
  214. {
  215. m_hasNegativeItems = 1;
  216. break;
  217. }
  218. }
  219. }
  220. }
  221. return m_hasNegativeItems == 1;
  222. }
  223. int GERBER_FILE_IMAGE::GetDcodesCount()
  224. {
  225. int count = 0;
  226. for( unsigned ii = 0; ii < arrayDim( m_Aperture_List ); ii++ )
  227. {
  228. if( m_Aperture_List[ii] )
  229. if( m_Aperture_List[ii]->m_InUse || m_Aperture_List[ii]->m_Defined )
  230. ++count;
  231. }
  232. return count;
  233. }
  234. void GERBER_FILE_IMAGE::InitToolTable()
  235. {
  236. for( int count = 0; count < TOOLS_MAX_COUNT; count++ )
  237. {
  238. if( m_Aperture_List[count] == NULL )
  239. continue;
  240. m_Aperture_List[count]->m_Num_Dcode = count + FIRST_DCODE;
  241. m_Aperture_List[count]->Clear_D_CODE_Data();
  242. }
  243. m_aperture_macros.clear();
  244. }
  245. /**
  246. * Function StepAndRepeatItem
  247. * Gerber format has a command Step an Repeat
  248. * This function must be called when reading a gerber file and
  249. * after creating a new gerber item that must be repeated
  250. * (i.e when m_XRepeatCount or m_YRepeatCount are > 1)
  251. * @param aItem = the item to repeat
  252. */
  253. void GERBER_FILE_IMAGE::StepAndRepeatItem( const GERBER_DRAW_ITEM& aItem )
  254. {
  255. if( GetLayerParams().m_XRepeatCount < 2 &&
  256. GetLayerParams().m_YRepeatCount < 2 )
  257. return; // Nothing to repeat
  258. // Duplicate item:
  259. for( int ii = 0; ii < GetLayerParams().m_XRepeatCount; ii++ )
  260. {
  261. for( int jj = 0; jj < GetLayerParams().m_YRepeatCount; jj++ )
  262. {
  263. // the first gerber item already exists (this is the template)
  264. // create duplicate only if ii or jj > 0
  265. if( jj == 0 && ii == 0 )
  266. continue;
  267. GERBER_DRAW_ITEM* dupItem = new GERBER_DRAW_ITEM( aItem );
  268. wxPoint move_vector;
  269. move_vector.x = scaletoIU( ii * GetLayerParams().m_StepForRepeat.x,
  270. GetLayerParams().m_StepForRepeatMetric );
  271. move_vector.y = scaletoIU( jj * GetLayerParams().m_StepForRepeat.y,
  272. GetLayerParams().m_StepForRepeatMetric );
  273. dupItem->MoveXY( move_vector );
  274. m_Drawings.Append( dupItem );
  275. }
  276. }
  277. }
  278. /**
  279. * Function DisplayImageInfo
  280. * has knowledge about the frame and how and where to put status information
  281. * about this object into the frame's message panel.
  282. * Display info about Image Parameters.
  283. * These parameters are valid for the entire file, and must set only once
  284. * (If more than once, only the last value is used)
  285. */
  286. void GERBER_FILE_IMAGE::DisplayImageInfo( GERBVIEW_FRAME* aMainFrame )
  287. {
  288. wxString msg;
  289. aMainFrame->ClearMsgPanel();
  290. // Display Image name (Image specific)
  291. aMainFrame->AppendMsgPanel( _( "Image name" ), m_ImageName, CYAN );
  292. // Display graphic layer number used to draw this Image
  293. // (not a Gerber parameter but is also image specific)
  294. msg.Printf( wxT( "%d" ), m_GraphicLayer + 1 );
  295. aMainFrame->AppendMsgPanel( _( "Graphic layer" ), msg, BROWN );
  296. // Display Image rotation (Image specific)
  297. msg.Printf( wxT( "%d" ), m_ImageRotation );
  298. aMainFrame->AppendMsgPanel( _( "Img Rot." ), msg, CYAN );
  299. // Display Image polarity (Image specific)
  300. msg = m_ImageNegative ? _("Negative") : _("Normal");
  301. aMainFrame->AppendMsgPanel( _( "Polarity" ), msg, BROWN );
  302. // Display Image justification and offset for justification (Image specific)
  303. msg = m_ImageJustifyXCenter ? _("Center") : _("Normal");
  304. aMainFrame->AppendMsgPanel( _( "X Justify" ), msg, DARKRED );
  305. msg = m_ImageJustifyYCenter ? _("Center") : _("Normal");
  306. aMainFrame->AppendMsgPanel( _( "Y Justify" ), msg, DARKRED );
  307. if( aMainFrame->GetUserUnits() == INCHES )
  308. msg.Printf( wxT( "X=%f Y=%f" ), Iu2Mils( m_ImageJustifyOffset.x ) / 1000.0,
  309. Iu2Mils( m_ImageJustifyOffset.y ) / 1000.0 );
  310. else
  311. msg.Printf( wxT( "X=%f Y=%f" ), Iu2Millimeter( m_ImageJustifyOffset.x ),
  312. Iu2Millimeter( m_ImageJustifyOffset.y ) );
  313. aMainFrame->AppendMsgPanel( _( "Image Justify Offset" ), msg, DARKRED );
  314. }
  315. void GERBER_FILE_IMAGE::RemoveAttribute( X2_ATTRIBUTE& aAttribute )
  316. {
  317. /* Called when a %TD command is found
  318. * Remove the attribute specified by the %TD command.
  319. * is no attribute, all current attributes specified by the %TO and the %TA
  320. * commands are cleared.
  321. * if a attribute name is specified (for instance %TD.CN*%) is specified,
  322. * only this attribute is cleared
  323. */
  324. m_NetAttributeDict.ClearAttribute( &aAttribute.GetPrm( 1 ) );
  325. if( aAttribute.GetPrm( 1 ).IsEmpty() || aAttribute.GetPrm( 1 ) == ".AperFunction" )
  326. m_AperFunction.Clear();
  327. }
  328. SEARCH_RESULT GERBER_FILE_IMAGE::Visit( INSPECTOR inspector, void* testData, const KICAD_T scanTypes[] )
  329. {
  330. KICAD_T stype;
  331. SEARCH_RESULT result = SEARCH_CONTINUE;
  332. const KICAD_T* p = scanTypes;
  333. bool done = false;
  334. #if 0 && defined(DEBUG)
  335. std::cout << GetClass().mb_str() << ' ';
  336. #endif
  337. while( !done )
  338. {
  339. stype = *p;
  340. switch( stype )
  341. {
  342. case GERBER_IMAGE_T:
  343. case GERBER_IMAGE_LIST_T:
  344. ++p;
  345. break;
  346. case GERBER_DRAW_ITEM_T:
  347. result = IterateForward( &m_Drawings[0], inspector, testData, p );
  348. ++p;
  349. break;
  350. default: // catch EOT or ANY OTHER type here and return.
  351. done = true;
  352. break;
  353. }
  354. if( result == SEARCH_QUIT )
  355. break;
  356. }
  357. return result;
  358. }