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.

947 lines
29 KiB

8 years ago
8 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 1992-2016 Jean-Pierre Charras <jp.charras at wanadoo.fr>
  5. * Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /*
  25. * Here is a sample of drill files created by Pcbnew, in decimal format:
  26. * (Note: coordinates formats are same as Gerber, and T commands are near Gerber D commands).
  27. * M48
  28. * ;DRILL file {PCBnew (2011-03-14 BZR 2894)-testing} date 15/03/2011 14:23:22
  29. * ;FORMAT={-:-/ absolute / inch / decimal}
  30. * FMAT,2
  31. * INCH,TZ
  32. * T1C0.02
  33. * T2C0.032
  34. * %
  35. * G90
  36. * G05
  37. * M72
  38. * T1
  39. * X1.580Y-1.360
  40. * X1.580Y-4.860
  41. * X8.680Y-1.360
  42. * X8.680Y-4.860
  43. * T2
  44. * X2.930Y-3.560
  45. * X5.280Y-2.535
  46. * X5.405Y-2.610
  47. * X5.620Y-2.900
  48. * T0
  49. * M30
  50. */
  51. /*
  52. * Note there are some variant of tool definition:
  53. * T1F00S00C0.2 or T1C0.02F00S00 ... Feed Rate and Spindle Speed of Tool 1
  54. * Feed Rate and Spindle Speed are just skipped because they are not used in a viewer
  55. */
  56. /**
  57. * @file excellon_read_drill_file.cpp
  58. * Functions to read drill files (EXCELLON format) created by Pcbnew
  59. * These files use only a subset of EXCELLON commands.
  60. */
  61. #include <fctsys.h>
  62. #include <common.h>
  63. #include <confirm.h>
  64. #include <gerbview.h>
  65. #include <gerbview_frame.h>
  66. #include <gerber_file_image.h>
  67. #include <gerber_file_image_list.h>
  68. #include <excellon_image.h>
  69. #include <kicad_string.h>
  70. #include <X2_gerber_attributes.h>
  71. #include <view/view.h>
  72. #include <cmath>
  73. #include <html_messagebox.h>
  74. // Default format for dimensions: they are the default values, not the actual values
  75. // number of digits in mantissa:
  76. static const int fmtMantissaMM = 3;
  77. static const int fmtMantissaInch = 4;
  78. // number of digits, integer part:
  79. static const int fmtIntegerMM = 3;
  80. static const int fmtIntegerInch = 2;
  81. // A helper function to calculate the arc center of an arc
  82. // known by 2 end points, the radius, and the angle direction (CW or CCW)
  83. // Arc angles are <= 180 degrees in circular interpol.
  84. static wxPoint computeCenter(wxPoint aStart, wxPoint aEnd, int& aRadius, bool aRotCCW )
  85. {
  86. wxPoint center;
  87. VECTOR2D end;
  88. end.x = double(aEnd.x - aStart.x);
  89. end.y = double(aEnd.y - aStart.y);
  90. // Be sure aRadius/2 > dist between aStart and aEnd
  91. double min_radius = end.EuclideanNorm() * 2;
  92. if( min_radius <= aRadius )
  93. {
  94. // Adjust the radius and the arc center for a 180 deg arc between end points
  95. aRadius = KiROUND( min_radius );
  96. center.x = ( aStart.x + aEnd.x + 1 ) / 2;
  97. center.y = ( aStart.y + aEnd.y + 1 ) / 2;
  98. return center;
  99. }
  100. /* to compute the centers position easily:
  101. * rotate the segment (0,0 to end.x,end.y) to make it horizontal (end.y = 0).
  102. * the X center position is end.x/2
  103. * the Y center positions are on the vertical line starting at end.x/2, 0
  104. * and solve aRadius^2 = X^2 + Y^2 (2 values)
  105. */
  106. double seg_angle = end.Angle(); //in radian
  107. VECTOR2D h_segm = end.Rotate( - seg_angle );
  108. double cX = h_segm.x/2;
  109. double cY1 = sqrt( (double)aRadius*aRadius - cX*cX );
  110. double cY2 = -cY1;
  111. VECTOR2D center1( cX, cY1 );
  112. center1 = center1.Rotate( seg_angle );
  113. double arc_angle1 = (end - center1).Angle() - (VECTOR2D(0.0,0.0) - center1).Angle();
  114. VECTOR2D center2( cX, cY2 );
  115. center2 = center2.Rotate( seg_angle );
  116. double arc_angle2 = (end - center2).Angle() - (VECTOR2D(0.0,0.0) - center2).Angle();
  117. if( !aRotCCW )
  118. {
  119. if( arc_angle1 < 0.0 )
  120. arc_angle1 += 2*M_PI;
  121. if( arc_angle2 < 0.0 )
  122. arc_angle2 += 2*M_PI;
  123. }
  124. else
  125. {
  126. if( arc_angle1 > 0.0 )
  127. arc_angle1 -= 2*M_PI;
  128. if( arc_angle2 > 0.0 )
  129. arc_angle2 -= 2*M_PI;
  130. }
  131. // Arc angle must be <= 180.0 degrees.
  132. // So choose the center that create a arc angle <= 180.0
  133. if( std::abs( arc_angle1 ) <= M_PI )
  134. {
  135. center.x = KiROUND( center1.x );
  136. center.y = KiROUND( center1.y );
  137. }
  138. else
  139. {
  140. center.x = KiROUND( center2.x );
  141. center.y = KiROUND( center2.y );
  142. }
  143. return center+aStart;
  144. }
  145. extern int ReadInt( char*& text, bool aSkipSeparator = true );
  146. extern double ReadDouble( char*& text, bool aSkipSeparator = true );
  147. // See rs274d.cpp:
  148. extern void fillFlashedGBRITEM( GERBER_DRAW_ITEM* aGbrItem,
  149. APERTURE_T aAperture,
  150. int Dcode_index,
  151. const wxPoint& aPos,
  152. wxSize aSize,
  153. bool aLayerNegative );
  154. extern void fillLineGBRITEM( GERBER_DRAW_ITEM* aGbrItem,
  155. int Dcode_index,
  156. const wxPoint& aStart,
  157. const wxPoint& aEnd,
  158. wxSize aPenSize,
  159. bool aLayerNegative );
  160. extern void fillArcGBRITEM( GERBER_DRAW_ITEM* aGbrItem, int Dcode_index,
  161. const wxPoint& aStart, const wxPoint& aEnd,
  162. const wxPoint& aRelCenter, wxSize aPenSize,
  163. bool aClockwise, bool aMultiquadrant,
  164. bool aLayerNegative );
  165. // Gerber X2 files have a file attribute which specify the type of image
  166. // (copper, solder paste ... and sides tpo, bottom or inner copper layers)
  167. // Excellon drill files do not have attributes, so, just to identify the image
  168. // In gerbview, we add this attribute, similat to a Gerber drill file
  169. static const char file_attribute[] = ".FileFunction,Other,Drill*";
  170. static EXCELLON_CMD excellonHeaderCmdList[] =
  171. {
  172. { "M0", DRILL_M_END, -1 }, // End of Program - No Rewind
  173. { "M00", DRILL_M_END, -1 }, // End of Program - No Rewind
  174. { "M15", DRILL_M_TOOL_DOWN, 0 }, // tool down (starting a routed hole)
  175. { "M16", DRILL_M_TOOL_UP, 0 }, // tool up (ending a routed hole)
  176. { "M17", DRILL_M_TOOL_UP, 0 }, // tool up similar to M16 for a viewer
  177. { "M30", DRILL_M_ENDFILE, -1 }, // End of File (last line of NC drill)
  178. { "M47", DRILL_M_MESSAGE, -1 }, // Operator Message
  179. { "M45", DRILL_M_LONGMESSAGE, -1 }, // Long Operator message (use more than one line)
  180. { "M48", DRILL_M_HEADER, 0 }, // beginning of a header
  181. { "M95", DRILL_M_ENDHEADER, 0 }, // End of the header
  182. { "METRIC", DRILL_METRIC_HEADER, 1 },
  183. { "INCH", DRILL_IMPERIAL_HEADER, 1 },
  184. { "M71", DRILL_M_METRIC, 1 },
  185. { "M72", DRILL_M_IMPERIAL, 1 },
  186. { "M25", DRILL_M_BEGINPATTERN, 0 }, // Beginning of Pattern
  187. { "M01", DRILL_M_ENDPATTERN, 0 }, // End of Pattern
  188. { "M97", DRILL_M_CANNEDTEXT, -1 },
  189. { "M98", DRILL_M_CANNEDTEXT, -1 },
  190. { "DETECT", DRILL_DETECT_BROKEN, -1 },
  191. { "ICI", DRILL_INCREMENTALHEADER, 1 },
  192. { "FMAT", DRILL_FMT, 1 }, // Use Format command
  193. { "ATC", DRILL_AUTOMATIC_TOOL_CHANGE, 0 },
  194. { "TCST", DRILL_TOOL_CHANGE_STOP, 0 }, // Tool Change Stop
  195. { "AFS", DRILL_AUTOMATIC_SPEED }, // Automatic Feeds and Speeds
  196. { "VER", DRILL_AXIS_VERSION, 1 }, // Selection of X and Y Axis Version
  197. { "R", DRILL_RESET_CMD, -1 }, // Reset commands
  198. { "%", DRILL_REWIND_STOP, -1 }, // Rewind stop. End of the header
  199. { "/", DRILL_SKIP, -1 }, // Clear Tool Linking. End of the header
  200. // Keep this item after all commands starting by 'T':
  201. { "T", DRILL_TOOL_INFORMATION, 0 }, // Tool Information
  202. { "", DRILL_M_UNKNOWN, 0 } // last item in list
  203. };
  204. static EXCELLON_CMD excellon_G_CmdList[] =
  205. {
  206. { "G90", DRILL_G_ABSOLUTE, 0 }, // Absolute Mode
  207. { "G91", DRILL_G_INCREMENTAL, 0 }, // Incremental Input Mode
  208. { "G90", DRILL_G_ZEROSET, 0 }, // Absolute Mode
  209. { "G00", DRILL_G_ROUT, 1 }, // Route Mode
  210. { "G05", DRILL_G_DRILL, 0 }, // Drill Mode
  211. { "G85", DRILL_G_SLOT, 0 }, // Canned Mode slot (oval holes)
  212. { "G01", DRILL_G_LINEARMOVE, 1 }, // Linear (Straight Line) routing Mode
  213. { "G02", DRILL_G_CWMOVE, 1 }, // Circular CW Mode
  214. { "G03", DRILL_G_CCWMOVE, 1 }, // Circular CCW Mode
  215. { "G93", DRILL_G_ZERO_SET, 1 }, // Zero Set (XnnYmm and coordintes origin)
  216. { "", DRILL_G_UNKNOWN, 0 }, // last item in list
  217. };
  218. bool GERBVIEW_FRAME::Read_EXCELLON_File( const wxString& aFullFileName )
  219. {
  220. wxString msg;
  221. int layerId = GetActiveLayer(); // current layer used in GerbView
  222. GERBER_FILE_IMAGE_LIST* images = GetGerberLayout()->GetImagesList();
  223. auto gerber_layer = images->GetGbrImage( layerId );
  224. auto drill_layer = dynamic_cast<EXCELLON_IMAGE*>( gerber_layer );
  225. if( gerber_layer && !drill_layer )
  226. {
  227. // The active layer contains old gerber data we have to clear
  228. Erase_Current_DrawLayer( false );
  229. }
  230. if( drill_layer == nullptr )
  231. {
  232. drill_layer = new EXCELLON_IMAGE( layerId );
  233. layerId = images->AddGbrImage( drill_layer, layerId );
  234. }
  235. if( layerId < 0 )
  236. {
  237. DisplayError( this, _( "No room to load file" ) );
  238. return false;
  239. }
  240. // Read the Excellon drill file:
  241. bool success = drill_layer->LoadFile( aFullFileName );
  242. if( !success )
  243. {
  244. msg.Printf( _( "File %s not found" ), GetChars( aFullFileName ) );
  245. DisplayError( this, msg );
  246. return false;
  247. }
  248. // Display errors list
  249. if( drill_layer->GetMessages().size() > 0 )
  250. {
  251. HTML_MESSAGE_BOX dlg( this, _( "Error reading EXCELLON drill file" ) );
  252. dlg.ListSet( drill_layer->GetMessages() );
  253. dlg.ShowModal();
  254. }
  255. if( success )
  256. {
  257. EDA_DRAW_PANEL_GAL* canvas = GetGalCanvas();
  258. if( canvas )
  259. {
  260. KIGFX::VIEW* view = canvas->GetView();
  261. for( GERBER_DRAW_ITEM* item = drill_layer->GetItemsList(); item; item = item->Next() )
  262. {
  263. view->Add( (KIGFX::VIEW_ITEM*) item );
  264. }
  265. }
  266. }
  267. return success;
  268. }
  269. /*
  270. * Read a EXCELLON file.
  271. * Gerber classes are used because there is likeness between Gerber files
  272. * and Excellon files
  273. * DCode can easily store T code (tool size) as round (or oval) shape
  274. * Drill commands are similar to flashed gerber items
  275. * Routing commands are similar to Gerber polygons
  276. * coordinates have the same format as Gerber, can be given in:
  277. * decimal format (i.i. floating notation format)
  278. * integer 2.4 format in imperial units,
  279. * integer 3.2 or 3.3 format (metric units).
  280. */
  281. bool EXCELLON_IMAGE::LoadFile( const wxString & aFullFileName )
  282. {
  283. // Set the default parmeter values:
  284. ResetDefaultValues();
  285. ClearMessageList();
  286. m_Current_File = wxFopen( aFullFileName, "rt" );
  287. if( m_Current_File == NULL )
  288. return false;
  289. wxString msg;
  290. m_FileName = aFullFileName;
  291. LOCALE_IO toggleIo;
  292. // FILE_LINE_READER will close the file.
  293. FILE_LINE_READER excellonReader( m_Current_File, m_FileName );
  294. while( true )
  295. {
  296. if( excellonReader.ReadLine() == 0 )
  297. break;
  298. char* line = excellonReader.Line();
  299. char* text = StrPurge( line );
  300. if( *text == ';' || *text == 0 ) // comment: skip line or empty malformed line
  301. continue;
  302. if( m_State == EXCELLON_IMAGE::READ_HEADER_STATE )
  303. {
  304. Execute_HEADER_And_M_Command( text );
  305. }
  306. else
  307. {
  308. switch( *text )
  309. {
  310. case 'M':
  311. Execute_HEADER_And_M_Command( text );
  312. break;
  313. case 'G': // Line type Gxx : command
  314. Execute_EXCELLON_G_Command( text );
  315. break;
  316. case 'X':
  317. case 'Y': // command like X12550Y19250
  318. Execute_Drill_Command(text);
  319. break;
  320. case 'I':
  321. case 'J': /* Auxiliary Move command */
  322. m_IJPos = ReadIJCoord( text );
  323. if( *text == '*' ) // command like X35142Y15945J504
  324. {
  325. Execute_Drill_Command( text);
  326. }
  327. break;
  328. case 'T': // Tool command
  329. Select_Tool( text );
  330. break;
  331. case '%':
  332. break;
  333. default:
  334. msg.Printf( "Unexpected symbol 0x%2.2X &lt;%c&gt;", *text, *text );
  335. AddMessageToList( msg );
  336. break;
  337. } // End switch
  338. }
  339. }
  340. // Add our file attribute, to identify the drill file
  341. X2_ATTRIBUTE dummy;
  342. char* text = (char*)file_attribute;
  343. int dummyline = 0;
  344. dummy.ParseAttribCmd( NULL, NULL, 0, text, dummyline );
  345. delete m_FileFunction;
  346. m_FileFunction = new X2_ATTRIBUTE_FILEFUNCTION( dummy );
  347. m_InUse = true;
  348. return true;
  349. }
  350. bool EXCELLON_IMAGE::Execute_HEADER_And_M_Command( char*& text )
  351. {
  352. EXCELLON_CMD* cmd = NULL;
  353. wxString msg;
  354. // Search command in list
  355. for( unsigned ii = 0; ; ii++ )
  356. {
  357. EXCELLON_CMD* candidate = &excellonHeaderCmdList[ii];
  358. int len = candidate->m_Name.size();
  359. if( len == 0 ) // End of list reached
  360. break;
  361. if( candidate->m_Name.compare( 0, len, text, len ) == 0 ) // found.
  362. {
  363. cmd = candidate;
  364. text += len;
  365. break;
  366. }
  367. }
  368. if( !cmd )
  369. {
  370. msg.Printf( _( "Unknown Excellon command &lt;%s&gt;" ), text );
  371. AddMessageToList( msg );
  372. while( *text )
  373. text++;
  374. return false;
  375. }
  376. // Execute command
  377. // some do nothing
  378. switch( cmd->m_Code )
  379. {
  380. case DRILL_SKIP:
  381. case DRILL_M_UNKNOWN:
  382. break;
  383. case DRILL_M_END:
  384. case DRILL_M_ENDFILE:
  385. // if a route command is in progress, finish it
  386. if( m_RouteModeOn )
  387. FinishRouteCommand();
  388. break;
  389. case DRILL_M_MESSAGE:
  390. break;
  391. case DRILL_M_LONGMESSAGE:
  392. break;
  393. case DRILL_M_HEADER:
  394. m_State = READ_HEADER_STATE;
  395. break;
  396. case DRILL_M_ENDHEADER:
  397. m_State = READ_PROGRAM_STATE;
  398. break;
  399. case DRILL_REWIND_STOP: // End of header. No action in a viewer
  400. m_State = READ_PROGRAM_STATE;
  401. break;
  402. case DRILL_M_METRIC:
  403. SelectUnits( true );
  404. break;
  405. case DRILL_IMPERIAL_HEADER: // command like INCH,TZ or INCH,LZ
  406. case DRILL_METRIC_HEADER: // command like METRIC,TZ or METRIC,LZ
  407. SelectUnits( cmd->m_Code == DRILL_METRIC_HEADER ? true : false );
  408. if( *text != ',' )
  409. {
  410. // No TZ or LZ specified. Should be a decimal format
  411. // but this is not always the case. Use default TZ setting as default
  412. m_NoTrailingZeros = false;
  413. break;
  414. }
  415. text++; // skip separator
  416. if( *text == 'T' )
  417. m_NoTrailingZeros = false;
  418. else
  419. m_NoTrailingZeros = true;
  420. break;
  421. case DRILL_M_BEGINPATTERN:
  422. break;
  423. case DRILL_M_ENDPATTERN:
  424. break;
  425. case DRILL_M_CANNEDTEXT:
  426. break;
  427. case DRILL_M_TIPCHECK:
  428. break;
  429. case DRILL_DETECT_BROKEN:
  430. break;
  431. case DRILL_INCREMENTALHEADER:
  432. if( *text != ',' )
  433. {
  434. AddMessageToList( "ICI command has no parameter" );
  435. break;
  436. }
  437. text++; // skip separator
  438. // Parameter should be ON or OFF
  439. if( strncasecmp( text, "OFF", 3 ) == 0 )
  440. m_Relative = false;
  441. else if( strncasecmp( text, "ON", 2 ) == 0 )
  442. m_Relative = true;
  443. else
  444. AddMessageToList( "ICI command has incorrect parameter" );
  445. break;
  446. case DRILL_TOOL_CHANGE_STOP:
  447. break;
  448. case DRILL_AUTOMATIC_SPEED:
  449. break;
  450. case DRILL_AXIS_VERSION:
  451. break;
  452. case DRILL_RESET_CMD:
  453. break;
  454. case DRILL_AUTOMATIC_TOOL_CHANGE:
  455. break;
  456. case DRILL_FMT:
  457. break;
  458. case DRILL_TOOL_INFORMATION:
  459. readToolInformation( text );
  460. break;
  461. case DRILL_M_TOOL_DOWN: // tool down (starting a routed hole or polyline)
  462. // Only the last position is usefull:
  463. if( m_RoutePositions.size() > 1 )
  464. m_RoutePositions.erase( m_RoutePositions.begin(), m_RoutePositions.begin() + m_RoutePositions.size() - 1 );
  465. break;
  466. case DRILL_M_TOOL_UP: // tool up (ending a routed polyline)
  467. FinishRouteCommand();
  468. break;
  469. }
  470. while( *text )
  471. text++;
  472. return true;
  473. }
  474. bool EXCELLON_IMAGE::readToolInformation( char*& aText )
  475. {
  476. // Read a tool definition like T1C0.02 or T1F00S00C0.02 or T1C0.02F00S00
  477. // and enter the TCODE param in list (using the D_CODE param management, which
  478. // is similar to TCODE params.
  479. if( *aText == 'T' ) // This is the beginning of the definition
  480. aText++;
  481. // Read tool number:
  482. int iprm = ReadInt( aText, false );
  483. // Skip Feed rate and Spindle speed, if any here
  484. while( *aText && ( *aText == 'F' || *aText == 'S' ) )
  485. {
  486. aText++;
  487. ReadInt( aText, false );
  488. }
  489. // Read tool shape
  490. if( ! *aText )
  491. AddMessageToList( wxString:: Format(
  492. _( "Tool definition shape not found" ) ) );
  493. else if( *aText != 'C' )
  494. AddMessageToList( wxString:: Format(
  495. _( "Tool definition '%c' not supported" ), *aText ) );
  496. if( *aText )
  497. aText++;
  498. //read tool diameter:
  499. double dprm = ReadDouble( aText, false );
  500. m_Has_DCode = true;
  501. // Initialize Dcode to handle this Tool
  502. // Remember: dcodes are >= FIRST_DCODE
  503. D_CODE* dcode = GetDCODEOrCreate( iprm + FIRST_DCODE );
  504. if( dcode == NULL )
  505. return false;
  506. // conv_scale = scaling factor from inch to Internal Unit
  507. double conv_scale = IU_PER_MILS * 1000;
  508. if( m_GerbMetric )
  509. conv_scale /= 25.4;
  510. dcode->m_Size.x = dcode->m_Size.y = KiROUND( dprm * conv_scale );
  511. dcode->m_Shape = APT_CIRCLE;
  512. dcode->m_Defined = true;
  513. return true;
  514. }
  515. bool EXCELLON_IMAGE::Execute_Drill_Command( char*& text )
  516. {
  517. D_CODE* tool;
  518. GERBER_DRAW_ITEM * gbritem;
  519. while( true )
  520. {
  521. switch( *text )
  522. {
  523. case 'X':
  524. case 'Y':
  525. ReadXYCoord( text, true );
  526. if( *text == 'I' || *text == 'J' )
  527. ReadIJCoord( text );
  528. break;
  529. case 'G': // G85 is found here for oval holes
  530. m_PreviousPos = m_CurrentPos;
  531. Execute_EXCELLON_G_Command( text );
  532. break;
  533. case 0: // E.O.L: execute command
  534. if( m_RouteModeOn )
  535. {
  536. // We are in routing mode, and this is an intermediate point.
  537. // So just store it
  538. int rmode = 0; // linear routing.
  539. if( m_Iterpolation == GERB_INTERPOL_ARC_NEG )
  540. rmode = ROUTE_CW;
  541. else if( m_Iterpolation == GERB_INTERPOL_ARC_POS )
  542. rmode = ROUTE_CCW;
  543. if( m_LastArcDataType == ARC_INFO_TYPE_CENTER )
  544. {
  545. EXCELLON_ROUTE_COORD point( m_CurrentPos, m_IJPos, rmode );
  546. m_RoutePositions.push_back( point );
  547. }
  548. else
  549. {
  550. EXCELLON_ROUTE_COORD point( m_CurrentPos, m_ArcRadius, rmode );
  551. m_RoutePositions.push_back( point );
  552. }
  553. return true;
  554. }
  555. tool = GetDCODE( m_Current_Tool );
  556. if( !tool )
  557. {
  558. wxString msg;
  559. msg.Printf( _( "Tool %d not defined" ), m_Current_Tool );
  560. AddMessageToList( msg );
  561. return false;
  562. }
  563. gbritem = new GERBER_DRAW_ITEM( this );
  564. m_Drawings.Append( gbritem );
  565. if( m_SlotOn ) // Oblong hole
  566. {
  567. fillLineGBRITEM( gbritem, tool->m_Num_Dcode,
  568. m_PreviousPos, m_CurrentPos,
  569. tool->m_Size, false );
  570. // the hole is made: reset the slot on command (G85)
  571. // (it is needed for each oblong hole)
  572. m_SlotOn = false;
  573. }
  574. else
  575. {
  576. fillFlashedGBRITEM( gbritem, tool->m_Shape, tool->m_Num_Dcode,
  577. m_CurrentPos, tool->m_Size, false );
  578. }
  579. StepAndRepeatItem( *gbritem );
  580. m_PreviousPos = m_CurrentPos;
  581. return true;
  582. break;
  583. default:
  584. text++;
  585. break;
  586. }
  587. }
  588. return true;
  589. }
  590. bool EXCELLON_IMAGE::Select_Tool( char*& text )
  591. {
  592. // Select the tool from the command line Tn, with n = 1 ... TOOLS_MAX_COUNT - 1
  593. // Because some drill file have an embedded TCODE definition (like T1C.008F0S0)
  594. // in tool selection command, if the tool is not defined in list,
  595. // and the definition is embedded, it will be entered in list
  596. char * startline = text; // the tool id starts here.
  597. int tool_id = TCodeNumber( text );
  598. // T0 is legal, but is not a selection tool. it is a special command
  599. if( tool_id >= 0 )
  600. {
  601. int dcode_id = tool_id + FIRST_DCODE; // Remember: dcodes are >= FIRST_DCODE
  602. if( dcode_id > (TOOLS_MAX_COUNT - 1) )
  603. dcode_id = TOOLS_MAX_COUNT - 1;
  604. m_Current_Tool = dcode_id;
  605. D_CODE* currDcode = GetDCODEOrCreate( dcode_id, true );
  606. if( currDcode == NULL && tool_id > 0 ) // if the definition is embedded, enter it
  607. {
  608. text = startline; // text starts at the beginning of the command
  609. readToolInformation( text );
  610. currDcode = GetDCODE( dcode_id );
  611. }
  612. if( currDcode )
  613. currDcode->m_InUse = true;
  614. }
  615. while( *text )
  616. text++;
  617. return tool_id >= 0;
  618. }
  619. bool EXCELLON_IMAGE::Execute_EXCELLON_G_Command( char*& text )
  620. {
  621. EXCELLON_CMD* cmd = NULL;
  622. bool success = false;
  623. int id = DRILL_G_UNKNOWN;
  624. // Search command in list
  625. EXCELLON_CMD* candidate;
  626. char * gcmd = text; // gcmd points the G command, for error messages.
  627. for( unsigned ii = 0; ; ii++ )
  628. {
  629. candidate = &excellon_G_CmdList[ii];
  630. int len = candidate->m_Name.size();
  631. if( len == 0 ) // End of list reached
  632. break;
  633. if( candidate->m_Name.compare( 0, len, text, len ) == 0 ) // found.
  634. {
  635. cmd = candidate;
  636. text += len;
  637. success = true;
  638. id = cmd->m_Code;
  639. break;
  640. }
  641. }
  642. switch( id )
  643. {
  644. case DRILL_G_ZERO_SET:
  645. ReadXYCoord( text, true );
  646. m_Offset = m_CurrentPos;
  647. break;
  648. case DRILL_G_ROUT:
  649. m_SlotOn = false;
  650. if( m_RouteModeOn )
  651. FinishRouteCommand();
  652. m_RouteModeOn = true;
  653. m_RoutePositions.clear();
  654. m_LastArcDataType = ARC_INFO_TYPE_NONE;
  655. ReadXYCoord( text, true );
  656. // This is the first point (starting point) of routing
  657. m_RoutePositions.push_back( EXCELLON_ROUTE_COORD( m_CurrentPos ) );
  658. break;
  659. case DRILL_G_DRILL:
  660. m_SlotOn = false;
  661. if( m_RouteModeOn )
  662. FinishRouteCommand();
  663. m_RouteModeOn = false;
  664. m_RoutePositions.clear();
  665. m_LastArcDataType = ARC_INFO_TYPE_NONE;
  666. break;
  667. case DRILL_G_SLOT:
  668. m_SlotOn = true;
  669. break;
  670. case DRILL_G_LINEARMOVE:
  671. m_LastArcDataType = ARC_INFO_TYPE_NONE;
  672. m_Iterpolation = GERB_INTERPOL_LINEAR_1X;
  673. ReadXYCoord( text, true );
  674. m_RoutePositions.push_back( EXCELLON_ROUTE_COORD( m_CurrentPos ) );
  675. break;
  676. case DRILL_G_CWMOVE:
  677. m_Iterpolation = GERB_INTERPOL_ARC_NEG;
  678. ReadXYCoord( text, true );
  679. if( *text == 'I' || *text == 'J' )
  680. ReadIJCoord( text );
  681. if( m_LastArcDataType == ARC_INFO_TYPE_CENTER )
  682. m_RoutePositions.push_back( EXCELLON_ROUTE_COORD( m_CurrentPos, m_IJPos, ROUTE_CW ) );
  683. else
  684. m_RoutePositions.push_back( EXCELLON_ROUTE_COORD( m_CurrentPos, m_ArcRadius, ROUTE_CW ) );
  685. break;
  686. case DRILL_G_CCWMOVE:
  687. m_Iterpolation = GERB_INTERPOL_ARC_POS;
  688. ReadXYCoord( text, true );
  689. if( *text == 'I' || *text == 'J' )
  690. ReadIJCoord( text );
  691. if( m_LastArcDataType == ARC_INFO_TYPE_CENTER )
  692. m_RoutePositions.push_back( EXCELLON_ROUTE_COORD( m_CurrentPos, m_IJPos, ROUTE_CCW ) );
  693. else
  694. m_RoutePositions.push_back( EXCELLON_ROUTE_COORD( m_CurrentPos, m_ArcRadius, ROUTE_CCW ) );
  695. break;
  696. case DRILL_G_ABSOLUTE:
  697. m_Relative = false; // false = absolute coord
  698. break;
  699. case DRILL_G_INCREMENTAL:
  700. m_Relative = true; // true = relative coord
  701. break;
  702. case DRILL_G_UNKNOWN:
  703. default:
  704. AddMessageToList( wxString::Format( _( "Unknown Excellon G Code: &lt;%s&gt;" ), FROM_UTF8(gcmd) ) );
  705. while( *text )
  706. text++;
  707. return false;
  708. }
  709. return success;
  710. }
  711. void EXCELLON_IMAGE::SelectUnits( bool aMetric )
  712. {
  713. /* Coordinates are measured either in inch or metric (millimeters).
  714. * Inch coordinates are in six digits (00.0000) with increments
  715. * as small as 0.0001 (1/10,000).
  716. * Metric coordinates can be measured in microns (thousandths of a millimeter)
  717. * in one of the following three ways:
  718. * Five digit 10 micron resolution (000.00)
  719. * Six digit 10 micron resolution (0000.00)
  720. * Six digit micron resolution (000.000)
  721. *
  722. * Inches: Default fmt = 2.4 for X and Y axis: 6 digits with 0.0001 resolution
  723. * metric: Default fmt = 3.3 for X and Y axis: 6 digits, 1 micron resolution
  724. */
  725. if( aMetric )
  726. {
  727. m_GerbMetric = true;
  728. // number of digits in mantissa
  729. m_FmtScale.x = m_FmtScale.y = fmtMantissaMM;
  730. // number of digits (mantissa+interger)
  731. m_FmtLen.x = m_FmtLen.y = fmtIntegerMM+fmtMantissaMM;
  732. }
  733. else
  734. {
  735. m_GerbMetric = false;
  736. m_FmtScale.x = m_FmtScale.y = fmtMantissaInch;
  737. m_FmtLen.x = m_FmtLen.y = fmtIntegerInch+fmtMantissaInch;
  738. }
  739. }
  740. void EXCELLON_IMAGE::FinishRouteCommand()
  741. {
  742. // Ends a route command started by M15 ot G01, G02 or G03 command
  743. // if a route command is not in progress, do nothing
  744. if( !m_RouteModeOn )
  745. return;
  746. D_CODE* tool = GetDCODE( m_Current_Tool );
  747. if( !tool )
  748. {
  749. AddMessageToList( wxString::Format( "Unknown tool code %d", m_Current_Tool ) );
  750. return;
  751. }
  752. for( size_t ii = 1; ii < m_RoutePositions.size(); ii++ )
  753. {
  754. GERBER_DRAW_ITEM* gbritem = new GERBER_DRAW_ITEM( this );
  755. if( m_RoutePositions[ii].m_rmode == 0 ) // linear routing
  756. {
  757. fillLineGBRITEM( gbritem, tool->m_Num_Dcode,
  758. m_RoutePositions[ii-1].GetPos(), m_RoutePositions[ii].GetPos(),
  759. tool->m_Size, false );
  760. }
  761. else // circular (cw or ccw) routing
  762. {
  763. bool rot_ccw = m_RoutePositions[ii].m_rmode == ROUTE_CW;
  764. int radius = m_RoutePositions[ii].m_radius; // Can be adjusted by computeCenter.
  765. wxPoint center;
  766. if( m_RoutePositions[ii].m_arc_type_info == ARC_INFO_TYPE_CENTER )
  767. center = wxPoint( m_RoutePositions[ii].m_cx, m_RoutePositions[ii].m_cy );
  768. else
  769. center = computeCenter( m_RoutePositions[ii-1].GetPos(),
  770. m_RoutePositions[ii].GetPos(), radius, rot_ccw );
  771. fillArcGBRITEM( gbritem, tool->m_Num_Dcode,
  772. m_RoutePositions[ii-1].GetPos(), m_RoutePositions[ii].GetPos(),
  773. center - m_RoutePositions[ii-1].GetPos(),
  774. tool->m_Size, not rot_ccw , true,
  775. false );
  776. }
  777. m_Drawings.Append( gbritem );
  778. StepAndRepeatItem( *gbritem );
  779. }
  780. m_RoutePositions.clear();
  781. m_RouteModeOn = false;
  782. }