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.

403 lines
15 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2011-2016 Jean-Pierre Charras jp.charras at wanadoo.fr
  5. * Copyright (C) 1992-2021 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. #ifndef EXCELLON_IMAGE_H
  25. #define EXCELLON_IMAGE_H
  26. struct EXCELLON_DEFAULTS;
  27. enum drill_M_code_t {
  28. DRILL_M_UNKNOWN,
  29. DRILL_M_END,
  30. DRILL_M_TOOL_DOWN, // tool down (starting a routed hole)
  31. DRILL_M_TOOL_UP, // tool up (ending a routed hole)
  32. DRILL_M_ENDFILE,
  33. DRILL_M_MESSAGE,
  34. DRILL_M_LONGMESSAGE,
  35. DRILL_M_HEADER,
  36. DRILL_M_ENDHEADER,
  37. DRILL_M_BEGINPATTERN,
  38. DRILL_M_ENDPATTERN,
  39. DRILL_M_CANNEDTEXT,
  40. DRILL_M_TIPCHECK,
  41. DRILL_M_METRIC,
  42. DRILL_M_IMPERIAL,
  43. DRILL_METRIC_HEADER,
  44. DRILL_IMPERIAL_HEADER,
  45. DRILL_DETECT_BROKEN,
  46. DRILL_INCREMENTALHEADER,
  47. DRILL_REWIND_STOP,
  48. DRILL_TOOL_CHANGE_STOP,
  49. DRILL_AUTOMATIC_SPEED,
  50. DRILL_AXIS_VERSION,
  51. DRILL_RESET_CMD,
  52. DRILL_AUTOMATIC_TOOL_CHANGE,
  53. DRILL_FMT,
  54. DRILL_FORMAT_ALTIUM,
  55. DRILL_HEADER_SKIP,
  56. DRILL_SKIP,
  57. DRILL_TOOL_INFORMATION,
  58. DRILL_M_END_LIST // not used: sentinel
  59. };
  60. enum drill_G_code_t {
  61. DRILL_G_UNKNOWN = DRILL_M_END_LIST+1, // Use next available value
  62. DRILL_G_ABSOLUTE,
  63. DRILL_G_INCREMENTAL,
  64. DRILL_G_ZEROSET,
  65. DRILL_G_ROUT,
  66. DRILL_G_DRILL,
  67. DRILL_G_SLOT,
  68. DRILL_G_ZERO_SET,
  69. DRILL_G_LINEARMOVE,
  70. DRILL_G_CWMOVE,
  71. DRILL_G_CCWMOVE
  72. };
  73. // Helper struct to analyze Excellon commands
  74. struct EXCELLON_CMD
  75. {
  76. std::string m_Name; // key string
  77. int m_Code; // internal code, used as id in functions
  78. int m_asParams; // 0 = no param, -1 = skip params, 1 = read params
  79. };
  80. // Helper struct to store Excellon points in routing mode
  81. #define ROUTE_CCW 1
  82. #define ROUTE_CW -1
  83. struct EXCELLON_ROUTE_COORD
  84. {
  85. int m_x; // X coordinate
  86. int m_y; // y coordinate
  87. int m_cx; // center X coordinate in circular routing mode
  88. // (when the IJ command is used)
  89. int m_cy; // center y coordinate in circular routing mode
  90. // (when the IJ command is used)
  91. int m_radius; // radius in circular routing mode (when the A## command is used)
  92. int m_rmode; // routing mode: 0 = circular, ROUTE_CCW (1) = ccw, ROUTE_CW (-1) = cw
  93. int m_arc_type_info; // arc using radius or center coordinates
  94. EXCELLON_ROUTE_COORD():
  95. m_x( 0 ), m_y( 0 ), m_cx( 0 ), m_cy( 0 ), m_radius( 0 ),
  96. m_rmode( 0 ), m_arc_type_info( 0 )
  97. {}
  98. EXCELLON_ROUTE_COORD( const VECTOR2I& aPos ) :
  99. m_x( aPos.x ), m_y( aPos.y ),
  100. m_cx( 0 ), m_cy( 0 ), m_radius( 0 ), m_rmode( 0 ),
  101. m_arc_type_info( ARC_INFO_TYPE_NONE )
  102. {}
  103. EXCELLON_ROUTE_COORD( const VECTOR2I& aPos, const VECTOR2I& aCenter, int aMode ) :
  104. m_x( aPos.x ), m_y( aPos.y ),
  105. m_cx( aCenter.x ), m_cy( aCenter.y ), m_radius( 0 ), m_rmode( aMode ),
  106. m_arc_type_info( ARC_INFO_TYPE_CENTER )
  107. {}
  108. EXCELLON_ROUTE_COORD( const VECTOR2I& aPos, int aRadius, int aMode ) :
  109. m_x( aPos.x ), m_y( aPos.y ),
  110. m_cx( 0 ), m_cy( 0 ), m_radius( aRadius ), m_rmode( aMode ),
  111. m_arc_type_info( ARC_INFO_TYPE_RADIUS )
  112. {}
  113. VECTOR2I GetPos() { return VECTOR2I( m_x, m_y ); }
  114. };
  115. /**
  116. * Handle a drill image.
  117. *
  118. * It is derived from #GERBER_FILE_IMAGE because there is a lot of likeness between EXCELLON
  119. * files and GERBER files.
  120. * DCode apertures are also similar to T Codes.
  121. */
  122. class EXCELLON_IMAGE : public GERBER_FILE_IMAGE
  123. {
  124. public: EXCELLON_IMAGE( int layer ) :
  125. GERBER_FILE_IMAGE( layer )
  126. {
  127. m_State = READ_HEADER_STATE;
  128. m_SlotOn = false;
  129. m_RouteModeOn = false;
  130. m_hasFormat = false;
  131. }
  132. ~EXCELLON_IMAGE() {};
  133. /**
  134. * Set all parameters to a default value, before reading a file
  135. */
  136. virtual void ResetDefaultValues() override;
  137. /**
  138. * @brief Performs a heuristics-based check of whether the file is an Excellon drill file.
  139. *
  140. * Does not invoke the full parser.
  141. *
  142. * @param aFullFileName aFullFileName is the full filename of the Excellon file.
  143. * @return True if drill file, false otherwise
  144. */
  145. static bool TestFileIsExcellon( const wxString& aFullFileName );
  146. /**
  147. * Read and load a drill (EXCELLON format) file.
  148. *
  149. * When the file cannot be loaded, warning and info messages are stored in m_Messages.
  150. *
  151. * @param aFullFileName is the full filename of the Excellon file.
  152. * @param aDefaults is the default values when not found in file.
  153. * @return true if OK, false if the gerber file was not loaded.
  154. */
  155. bool LoadFile( const wxString& aFullFileName, EXCELLON_DEFAULTS* aDefaults );
  156. private:
  157. bool Execute_HEADER_And_M_Command( char*& text );
  158. bool Select_Tool( char*& text );
  159. bool Execute_EXCELLON_G_Command( char*& text );
  160. bool Execute_Drill_Command( char*& text );
  161. /**
  162. * Read an Altium-specific FILE_FORMAT=X:X attribute that specifies the length
  163. * and mantissa of the numbers in the gerber file
  164. *
  165. * @param aText Text containing format and mantissa
  166. */
  167. void readFileFormat( char*& aText );
  168. /**
  169. * Read a tool definition like T1C0.02 or T1F00S00C0.02 or T1C0.02F00S00
  170. * and enter params in TCODE list.
  171. */
  172. bool readToolInformation( char*& aText );
  173. /**
  174. * End a route command started by M15 ot G01, G02 or G03 command.
  175. */
  176. void FinishRouteCommand();
  177. /**
  178. * Switch unit selection, and the coordinate format (nn:mm) if not yet set
  179. */
  180. void SelectUnits( bool aMetric, EXCELLON_DEFAULTS* aDefaults );
  181. private:
  182. enum EXCELLON_STATE {
  183. READ_HEADER_STATE, // When we are in this state, we are reading header
  184. READ_PROGRAM_STATE // When we are in this state, we are reading drill data
  185. };
  186. EXCELLON_STATE m_State; // state of excellon file analysis
  187. bool m_SlotOn; // true during an oblong drill definition
  188. // by G85 (canned slot) command
  189. bool m_RouteModeOn; // true during a route mode (for instance a oval hole) or
  190. // a cutout.
  191. std::vector<EXCELLON_ROUTE_COORD> m_RoutePositions; // The list of points in a route mode
  192. /// Excellon file do not have a format statement to specify the coordinate format
  193. /// like nn:mm.
  194. /// However Altium files have a comment to specify it (";FILE_FORMET_"
  195. /// m_hasFormat is set to true if this comment is found, and coordinate format is known.
  196. bool m_hasFormat;
  197. };
  198. /*
  199. * EXCELLON commands are given here.
  200. * Pcbnew uses only few excellon commands
  201. */
  202. /*
  203. * see http://www.excellon.com/manuals/program.htm
  204. */
  205. /* coordinates units:
  206. * Coordinates are measured either in inch or metric (millimeters).
  207. * Inch coordinates are in six digits (00.0000) with increments as small as 0.0001 (1/10,000).
  208. * Metric coordinates can be measured in microns (thousandths of a millimeter)
  209. * in one of the following three ways:
  210. * Five digit 10 micron resolution (000.00)
  211. * Six digit 10 micron resolution (0000.00)
  212. * Six digit micron resolution (000.000)
  213. *
  214. * Leading and trailing zeros:
  215. * Excellon (CNC-7) uses inches in six digits and metric in five or six digits.
  216. * The zeros to the left of the coordinate are called leading zeros (LZ).
  217. * The zeros to right of the coordinate are called trailing zeros (TZ).
  218. * The CNC-7 uses leading zeros unless you specify otherwise through a part program.
  219. * You can do so with the INCH/METRIC command.
  220. * With leading zeros, the leading zeros must always be included.
  221. * Trailing zeros are unneeded and may be left off.
  222. * For trailing zeros, the reverse of the above is true.
  223. */
  224. /*
  225. * EXCELLON Commands Used in a Header
  226. * The following table provides you with a list of commands which
  227. * are the most used in a part program header.
  228. * COMMAND DESCRIPTION
  229. * AFS Automatic Feeds and Speeds
  230. * ATC Automatic Tool Change
  231. * BLKD Delete all Blocks starting with a slash (/)
  232. * CCW Clockwise or Counter-clockwise Routing
  233. * CP Cutter Compensation
  234. * DETECT Broken Tool Detection
  235. * DN Down Limit Set
  236. * DTMDIST Maximum Rout Distance Before Toolchange
  237. * EXDA Extended Drill Area
  238. * FMAT Format 1 or 2
  239. * FSB Turns the Feed/Speed Buttons off
  240. * HPCK Home Pulse Check
  241. * ICI Incremental Input of Part Program Coordinates
  242. * INCH Measure Everything in Inches
  243. * METRIC Measure Everything in Metric
  244. * M48 Beginning of Part Program Header
  245. * M95 End of Header
  246. * NCSL NC Slope Enable/Disable
  247. * OM48 Override Part Program Header
  248. * OSTOP Optional Stop Switch
  249. * OTCLMP Override Table Clamp
  250. * PCKPARAM Set up pecking tool,depth,infeed and retract parameters
  251. * PF Floating Pressure Foot Switch
  252. * PPR Programmable Plunge Rate Enable
  253. * PVS Pre-vacuum Shut-off Switch
  254. * R,C Reset Clocks
  255. * R,CP Reset Program Clocks
  256. * R,CR Reset Run Clocks
  257. * R,D Reset All Cutter Distances
  258. * R,H Reset All Hit Counters
  259. * R,T Reset Tool Data
  260. * SBK Single Block Mode Switch
  261. * SG Spindle Group Mode
  262. * SIXM Input From External Source
  263. * T Tool Information
  264. * TCST Tool Change Stop
  265. * UP Upper Limit Set
  266. * VER Selection of X and Y Axis Version
  267. * Z Zero Set
  268. * ZA Auxiliary Zero
  269. * ZC Zero Correction
  270. * ZS Zero Preset
  271. * Z+# or Z-# Set Depth Offset
  272. * % Rewind Stop
  273. * #/#/# Link Tool for Automatic Tool Change
  274. * / Clear Tool Linking
  275. */
  276. /*
  277. * Beyond The Header: The Part Program Body
  278. * COMMAND DESCRIPTION
  279. * A# Arc Radius
  280. * B# Retract Rate
  281. * C# Tool Diameter
  282. * F# Table Feed Rate;Z Axis Infeed Rate
  283. * G00X#Y# Route Mode; XY is the starting point
  284. * G01X#Y# Linear (Straight Line) Route Mode YX is the ending point
  285. * G02X#Y#... Circular CW Mode. Radius value (A#) or Center position (I#J#) follows
  286. * G03X#Y#... Circular CCW Mode. Radius value (A#) or Center position (I#J#) follows
  287. * G04X# Variable Dwell
  288. * G05 Drill Mode
  289. * G07 Override current tool feed or speed
  290. * G32X#Y#A# Routed Circle Canned Cycle
  291. * CW G33X#Y#A# Routed Circle Canned Cycle
  292. * CCW G34,#(,#) Select Vision Tool
  293. * G35(X#Y#) Single Point Vision Offset (Relative to Work Zero)
  294. * G36(X#Y#) Multipoint Vision Translation (Relative to Work Zero)
  295. * G37 Cancel Vision Translation or Offset (From G35 or G36)
  296. * G38(X#Y#) Vision Corrected Single Hole Drilling (Relative to Work Zero)
  297. * G39(X#Y#) Vision System Autocalibration
  298. * G40 Cutter Compensation Off
  299. * G41 Cutter Compensation Left
  300. * G42 Cutter Compensation Right
  301. * G45(X#Y#) Single Point Vision Offset (Relative to G35 or G36)
  302. * G46(X#Y#) Multipoint Vision Translation (Relative to G35 or G36)
  303. * G47 Cancel Vision Translation or Offset (From G45 or G46)
  304. * G48(X#Y#) Vision Corrected Single Hole Drilling (Relative to G35 or G36)
  305. * G82(G81) Dual In Line Package
  306. * G83 Eight Pin L Pack
  307. * G84 Circle
  308. * G85 Slot
  309. * G87 Routed Step Slot Canned Cycle
  310. * G90 Absolute Mode
  311. * G91 Incremental Input Mode
  312. * G93X#Y# Zero Set
  313. * H# Maximum hit count
  314. * I#J# Arc Center Offset
  315. * M00(X#Y#) End of Program - No Rewind
  316. * M01 End of Pattern
  317. * M02X#Y# Repeat Pattern Offset
  318. * M06(X#Y#) Optional Stop
  319. * M08 End of Step and Repeat
  320. * M09(X#Y#) Stop for Inspection
  321. * M14 Z Axis Route Position With Depth Controlled Contouring
  322. * M15 Z Axis Route Position
  323. * M16 Retract With Clamping
  324. * M17 Retract Without Clamping
  325. * M18 Command tool tip check
  326. * M25 Beginning of Pattern
  327. * M30(X#Y#) End of Program Rewind
  328. * M45,long message\ Long Operator message on multiple\ part program lines
  329. * M47,text Operator Message
  330. * M50,# Vision Step and Repeat Pattern Start
  331. * M51,# Vision Step and Repeat Rewind
  332. * M52(#) Vision Step and Repeat Offset Counter Control
  333. * M02XYM70 Swap Axes
  334. * M60 Reference Scaling enable
  335. * M61 Reference Scaling disable
  336. * M62 Turn on peck drilling
  337. * M63 Turn off peck drilling
  338. * M71 Metric Measuring Mode
  339. * M72 Inch Measuring Mode
  340. * M02XYM80 Mirror Image X Axis
  341. * M02XYM90 Mirror Image Y Axis
  342. * M97,text Canned Text
  343. * M98,text Canned Text
  344. * M99,subprogram User Defined Stored Pattern
  345. * P#X#(Y#) Repeat Stored Pattern
  346. * R#M02X#Y# Repeat Pattern (S&R)
  347. * R#(X#Y#) Repeat Hole
  348. * S# Spindle RPM
  349. * T# Tool Selection; Cutter Index
  350. * Z+# or Z-# Depth Offset
  351. * % Beginning of Pattern (see M25 command)
  352. * / Block Delete
  353. */
  354. /*
  355. * Example of a Header
  356. * COMMAND PURPOSE
  357. * M48 The beginning of a header
  358. * INCH,LZ Use the inch measuring system with leading zeros
  359. * VER,1 Use Version 1 X and Y axis layout
  360. * FMAT,2 Use Format 2 commands
  361. * 1/2/3 Link tools 1, 2, and 3
  362. * T1C.04F200S65 Set Tool 1 for 0.040" with infeed rate of 200 inch/min Speed of 65,000 RPM
  363. * DETECT,ON Detect broken tools
  364. * M95 End of the header
  365. */
  366. #endif // EXCELLON_IMAGE_H