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.

427 lines
14 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 < DIM( 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 < DIM( 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) DIM( 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) DIM( 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_LineNum = 0; // line number in file being read
  184. m_Current_File = NULL; // Gerber file to read
  185. m_PolygonFillMode = false;
  186. m_PolygonFillModeState = 0;
  187. m_Selected_Tool = 0;
  188. m_Last_Pen_Command = 0;
  189. m_Exposure = false;
  190. }
  191. /* Function HasNegativeItems
  192. * return true if at least one item must be drawn in background color
  193. * used to optimize screen refresh
  194. */
  195. bool GERBER_FILE_IMAGE::HasNegativeItems()
  196. {
  197. if( m_hasNegativeItems < 0 ) // negative items are not yet searched: find them if any
  198. {
  199. if( m_ImageNegative ) // A negative layer is expected having always negative objects.
  200. m_hasNegativeItems = 1;
  201. else
  202. {
  203. m_hasNegativeItems = 0;
  204. for( GERBER_DRAW_ITEM* item = GetItemsList(); item; item = item->Next() )
  205. {
  206. if( item->GetLayer() != m_GraphicLayer )
  207. continue;
  208. if( item->HasNegativeItems() )
  209. {
  210. m_hasNegativeItems = 1;
  211. break;
  212. }
  213. }
  214. }
  215. }
  216. return m_hasNegativeItems == 1;
  217. }
  218. int GERBER_FILE_IMAGE::GetDcodesCount()
  219. {
  220. int count = 0;
  221. for( unsigned ii = 0; ii < DIM( m_Aperture_List ); ii++ )
  222. {
  223. if( m_Aperture_List[ii] )
  224. if( m_Aperture_List[ii]->m_InUse || m_Aperture_List[ii]->m_Defined )
  225. ++count;
  226. }
  227. return count;
  228. }
  229. void GERBER_FILE_IMAGE::InitToolTable()
  230. {
  231. for( int count = 0; count < TOOLS_MAX_COUNT; count++ )
  232. {
  233. if( m_Aperture_List[count] == NULL )
  234. continue;
  235. m_Aperture_List[count]->m_Num_Dcode = count + FIRST_DCODE;
  236. m_Aperture_List[count]->Clear_D_CODE_Data();
  237. }
  238. m_aperture_macros.clear();
  239. }
  240. /**
  241. * Function StepAndRepeatItem
  242. * Gerber format has a command Step an Repeat
  243. * This function must be called when reading a gerber file and
  244. * after creating a new gerber item that must be repeated
  245. * (i.e when m_XRepeatCount or m_YRepeatCount are > 1)
  246. * @param aItem = the item to repeat
  247. */
  248. void GERBER_FILE_IMAGE::StepAndRepeatItem( const GERBER_DRAW_ITEM& aItem )
  249. {
  250. if( GetLayerParams().m_XRepeatCount < 2 &&
  251. GetLayerParams().m_YRepeatCount < 2 )
  252. return; // Nothing to repeat
  253. // Duplicate item:
  254. for( int ii = 0; ii < GetLayerParams().m_XRepeatCount; ii++ )
  255. {
  256. for( int jj = 0; jj < GetLayerParams().m_YRepeatCount; jj++ )
  257. {
  258. // the first gerber item already exists (this is the template)
  259. // create duplicate only if ii or jj > 0
  260. if( jj == 0 && ii == 0 )
  261. continue;
  262. GERBER_DRAW_ITEM* dupItem = new GERBER_DRAW_ITEM( aItem );
  263. wxPoint move_vector;
  264. move_vector.x = scaletoIU( ii * GetLayerParams().m_StepForRepeat.x,
  265. GetLayerParams().m_StepForRepeatMetric );
  266. move_vector.y = scaletoIU( jj * GetLayerParams().m_StepForRepeat.y,
  267. GetLayerParams().m_StepForRepeatMetric );
  268. dupItem->MoveXY( move_vector );
  269. m_Drawings.Append( dupItem );
  270. }
  271. }
  272. }
  273. /**
  274. * Function DisplayImageInfo
  275. * has knowledge about the frame and how and where to put status information
  276. * about this object into the frame's message panel.
  277. * Display info about Image Parameters.
  278. * These parameters are valid for the entire file, and must set only once
  279. * (If more than once, only the last value is used)
  280. */
  281. void GERBER_FILE_IMAGE::DisplayImageInfo( GERBVIEW_FRAME* aMainFrame )
  282. {
  283. wxString msg;
  284. aMainFrame->ClearMsgPanel();
  285. // Display Image name (Image specific)
  286. aMainFrame->AppendMsgPanel( _( "Image name" ), m_ImageName, CYAN );
  287. // Display graphic layer number used to draw this Image
  288. // (not a Gerber parameter but is also image specific)
  289. msg.Printf( wxT( "%d" ), m_GraphicLayer + 1 );
  290. aMainFrame->AppendMsgPanel( _( "Graphic layer" ), msg, BROWN );
  291. // Display Image rotation (Image specific)
  292. msg.Printf( wxT( "%d" ), m_ImageRotation );
  293. aMainFrame->AppendMsgPanel( _( "Img Rot." ), msg, CYAN );
  294. // Display Image polarity (Image specific)
  295. msg = m_ImageNegative ? _("Negative") : _("Normal");
  296. aMainFrame->AppendMsgPanel( _( "Polarity" ), msg, BROWN );
  297. // Display Image justification and offset for justification (Image specific)
  298. msg = m_ImageJustifyXCenter ? _("Center") : _("Normal");
  299. aMainFrame->AppendMsgPanel( _( "X Justify" ), msg, DARKRED );
  300. msg = m_ImageJustifyYCenter ? _("Center") : _("Normal");
  301. aMainFrame->AppendMsgPanel( _( "Y Justify" ), msg, DARKRED );
  302. if( g_UserUnit == INCHES )
  303. msg.Printf( wxT( "X=%f Y=%f" ), Iu2Mils( m_ImageJustifyOffset.x ) / 1000.0,
  304. Iu2Mils( m_ImageJustifyOffset.y ) / 1000.0 );
  305. else
  306. msg.Printf( wxT( "X=%f Y=%f" ), Iu2Millimeter( m_ImageJustifyOffset.x ),
  307. Iu2Millimeter( m_ImageJustifyOffset.y ) );
  308. aMainFrame->AppendMsgPanel( _( "Image Justify Offset" ), msg, DARKRED );
  309. }
  310. void GERBER_FILE_IMAGE::RemoveAttribute( X2_ATTRIBUTE& aAttribute )
  311. {
  312. /* Called when a %TD command is found
  313. * Remove the attribute specified by the %TD command.
  314. * is no attribute, all current attributes specified by the %TO and the %TA
  315. * commands are cleared.
  316. * if a attribute name is specified (for instance %TD.CN*%) is specified,
  317. * only this attribute is cleared
  318. */
  319. m_NetAttributeDict.ClearAttribute( &aAttribute.GetPrm( 1 ) );
  320. if( aAttribute.GetPrm( 1 ).IsEmpty() || aAttribute.GetPrm( 1 ) == ".AperFunction" )
  321. m_AperFunction.Clear();
  322. }
  323. SEARCH_RESULT GERBER_FILE_IMAGE::Visit( INSPECTOR inspector, void* testData, const KICAD_T scanTypes[] )
  324. {
  325. KICAD_T stype;
  326. SEARCH_RESULT result = SEARCH_CONTINUE;
  327. const KICAD_T* p = scanTypes;
  328. bool done = false;
  329. #if 0 && defined(DEBUG)
  330. std::cout << GetClass().mb_str() << ' ';
  331. #endif
  332. while( !done )
  333. {
  334. stype = *p;
  335. switch( stype )
  336. {
  337. case GERBER_IMAGE_T:
  338. case GERBER_IMAGE_LIST_T:
  339. ++p;
  340. break;
  341. case GERBER_DRAW_ITEM_T:
  342. result = IterateForward( &m_Drawings[0], inspector, testData, p );
  343. ++p;
  344. break;
  345. default: // catch EOT or ANY OTHER type here and return.
  346. done = true;
  347. break;
  348. }
  349. if( result == SEARCH_QUIT )
  350. break;
  351. }
  352. return result;
  353. }