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.

437 lines
20 KiB

* 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) 2010-2019 Jean-Pierre Charras jp.charras at wanadoo.fr
  5. * Copyright (C) 1992-2019 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 GERBER_FILE_IMAGE_H
  25. #define GERBER_FILE_IMAGE_H
  26. #include <vector>
  27. #include <set>
  28. #include <dcode.h>
  29. #include <gerber_draw_item.h>
  30. #include <am_primitive.h>
  31. #include <gbr_netlist_metadata.h>
  32. // An useful macro used when reading gerber files;
  33. #define IsNumber( x ) ( ( ( (x) >= '0' ) && ( (x) <='9' ) ) \
  34. || ( (x) == '-' ) || ( (x) == '+' ) || ( (x) == '.' ) )
  35. typedef std::vector<GERBER_DRAW_ITEM*> GERBER_DRAW_ITEMS;
  36. class GERBVIEW_FRAME;
  37. class D_CODE;
  38. /* gerber files have different parameters to define units and how items must be plotted.
  39. * some are for the entire file, and other can change along a file.
  40. * In Gerber world:
  41. * an image is the entire gerber file and its "global" parameters
  42. * a layer (that is very different from a board layer) is just a sub set of a file that
  43. * have specific parameters
  44. * if a Image parameter is set more than once, only the last value is used
  45. * Some parameters can change along a file and are not layer specific: they are stored
  46. * in GERBER_ITEM items, when instancied.
  47. *
  48. * In GerbView, to handle these parameters, there are 2 classes:
  49. * GERBER_FILE_IMAGE : the main class containing most of parameters and data to plot a graphic layer
  50. * Some of them can change along the file
  51. * There is one GERBER_FILE_IMAGE per file and one graphic layer per file or GERBER_FILE_IMAGE
  52. * GerbView does not read and merge 2 gerber file in one graphic layer:
  53. * I believe this is not possible due to the constraints in Image parameters.
  54. * GERBER_LAYER : containing the subset of parameters that is layer speficic
  55. * A GERBER_FILE_IMAGE must include one GERBER_LAYER to define all parameters to plot a file.
  56. * But a GERBER_FILE_IMAGE can use more than one GERBER_LAYER.
  57. */
  58. class GERBER_FILE_IMAGE;
  59. class X2_ATTRIBUTE;
  60. class X2_ATTRIBUTE_FILEFUNCTION;
  61. // For arcs, coordinates need 3 info: start point, end point and center or radius
  62. // In Excellon files it can be a A## value (radius) or I#J# center coordinates (like in gerber)
  63. // We need to know the last read type when reading a list of routing coordinates
  64. enum LAST_EXTRA_ARC_DATA_TYPE
  65. {
  66. ARC_INFO_TYPE_NONE,
  67. ARC_INFO_TYPE_CENTER, // last info is a IJ command: arc center is given
  68. ARC_INFO_TYPE_RADIUS, // last info is a A command: arc radius is given
  69. };
  70. class GERBER_LAYER
  71. {
  72. friend class GERBER_FILE_IMAGE;
  73. public:
  74. // These parameters are layer specfic:
  75. wxString m_LayerName; // Layer name, from LN <name>* command
  76. bool m_LayerNegative; // true = Negative Layer: command LP
  77. wxRealPoint m_StepForRepeat; // X and Y offsets for Step and Repeat command
  78. int m_XRepeatCount; // The repeat count on X axis
  79. int m_YRepeatCount; // The repeat count on Y axis
  80. bool m_StepForRepeatMetric; // false = Inches, true = metric
  81. // needed here because repeated
  82. // gerber items can have coordinates
  83. // in different units than step parameters
  84. // and the actual coordinates calculation must handle this
  85. public:
  86. GERBER_LAYER();
  87. ~GERBER_LAYER();
  88. private:
  89. void ResetDefaultValues();
  90. };
  91. /**
  92. * GERBER_FILE_IMAGE
  93. * holds the Image data and parameters for one gerber file
  94. * and layer parameters (TODO: move them in GERBER_LAYER class
  95. */
  96. class GERBER_FILE_IMAGE : public EDA_ITEM
  97. {
  98. D_CODE* m_Aperture_List[TOOLS_MAX_COUNT]; ///< Dcode (Aperture) List for this layer
  99. ///< (max TOOLS_MAX_COUNT: see dcode.h)
  100. bool m_Exposure; ///< whether an aperture macro tool is flashed on or off
  101. GERBER_LAYER m_GBRLayerParams; // hold params for the current gerber layer
  102. GERBER_DRAW_ITEMS m_drawings; // linked list of Gerber Items to draw
  103. public:
  104. bool m_InUse; // true if this image is currently in use
  105. // (a file is loaded in it)
  106. bool m_IsVisible; // true if the draw layer is visible and must be drawn
  107. // false if it must be not drawn
  108. COLOR4D m_PositiveDrawColor; // The color used to draw positive items
  109. wxString m_FileName; // Full File Name for this layer
  110. wxString m_ImageName; // Image name, from IN <name>* command
  111. bool m_IsX2_file; // true if a X2 gerber attribute was found in file
  112. X2_ATTRIBUTE_FILEFUNCTION* m_FileFunction; // file function parameters, found in a %TF command
  113. // or a G04
  114. wxString m_MD5_value; // MD5 value found in a %TF.MD5 command
  115. wxString m_PartString; // string found in a %TF.Part command
  116. int m_GraphicLayer; // Graphic layer Number
  117. bool m_ImageNegative; // true = Negative image
  118. bool m_ImageJustifyXCenter; // Image Justify Center on X axis (default = false)
  119. bool m_ImageJustifyYCenter; // Image Justify Center on Y axis (default = false)
  120. wxPoint m_ImageJustifyOffset; // Image Justify Offset on XY axis (default = 0,0)
  121. bool m_GerbMetric; // false = Inches, true = metric
  122. bool m_Relative; // false = absolute Coord, true = relative Coord
  123. bool m_NoTrailingZeros; // true: remove tailing zeros.
  124. wxPoint m_ImageOffset; // Coord Offset, from IO command
  125. wxSize m_FmtScale; // Fmt 2.3: m_FmtScale = 3, fmt 3.4: m_FmtScale = 4
  126. wxSize m_FmtLen; // Nb chars per coord. ex fmt 2.3, m_FmtLen = 5
  127. int m_ImageRotation; // Image rotation (0, 90, 180, 270 only) in degrees
  128. double m_LocalRotation; // Local rotation, in degrees, added to m_ImageRotation
  129. // Note this value is stored in 0.1 degrees
  130. wxPoint m_Offset; // Coord Offset, from OF command
  131. wxRealPoint m_Scale; // scale (X and Y) of layer.
  132. bool m_SwapAxis; // false (default) if A = X and B = Y
  133. // true if A = Y, B = X
  134. bool m_MirrorA; // true: miror / axe A (X)
  135. bool m_MirrorB; // true: miror / axe B (Y)
  136. int m_Iterpolation; // Linear, 90 arc, Circ.
  137. int m_Current_Tool; // Current Tool (Dcode) number selected
  138. int m_Last_Pen_Command; // Current or last pen state (0..9, set by Dn option with n <10
  139. int m_CommandState; // state of gerber analysis command.
  140. int m_LineNum; // Line number of the gerber file while reading
  141. wxPoint m_CurrentPos; // current specified coord for plot
  142. wxPoint m_PreviousPos; // old current specified coord for plot
  143. wxPoint m_IJPos; // IJ coord (for arcs & circles )
  144. bool m_LastCoordIsIJPos; // true if a IJ coord was read (for arcs & circles )
  145. int m_ArcRadius; // A value ( = radius in circular routing in Excellon files )
  146. LAST_EXTRA_ARC_DATA_TYPE m_LastArcDataType; // Identifier for arc data type (IJ (center) or A## (radius))
  147. FILE* m_Current_File; // Current file to read
  148. int m_Selected_Tool; // For highlight: current selected Dcode
  149. bool m_Has_DCode; // true = DCodes in file
  150. // false = no DCode -> perhaps deprecated RS274D file
  151. bool m_Has_MissingDCode; // true = some DCodes in file are not defined
  152. // (broken file or deprecated RS274D file)
  153. bool m_360Arc_enbl; // Enbl 360 deg circular interpolation
  154. bool m_AsArcG74G75Cmd; // Set to true when a circular interpolation command
  155. // type is found. Mandatory before drawing an arc.
  156. bool m_PolygonFillMode; // Enable polygon mode (read coord as a polygon descr)
  157. int m_PolygonFillModeState; // In polygon mode: 0 = first segm, 1 = next segm
  158. APERTURE_MACRO_SET m_aperture_macros; ///< a collection of APERTURE_MACROS, sorted by name
  159. GBR_NETLIST_METADATA m_NetAttributeDict; // the net attributes set by a %TO.CN, %TO.C and/or %TO.N
  160. // add object attribute command.
  161. wxString m_AperFunction; // the aperture function set by a %TA.AperFunction, xxx
  162. // (stores thre xxx value).
  163. std::map<wxString, int> m_ComponentsList; // list of components
  164. std::map<wxString, int> m_NetnamesList; // list of net names
  165. private:
  166. wxArrayString m_messagesList; // A list of messages created when reading a file
  167. int m_hasNegativeItems; // true if the image is negative or has some negative items
  168. // Used to optimize drawing, because when there are no
  169. // negative items screen refresh does not need
  170. // to build an intermediate bitmap specfic to this image
  171. // -1 = negative items are
  172. // 0 = no negative items found
  173. // 1 = have negative items found
  174. /**
  175. * test for an end of line
  176. * if a end of line is found:
  177. * read a new line
  178. * @param aBuff = buffer (size = GERBER_BUFZ) to fill with a new line
  179. * @param aText = pointer to the last useful char in aBuff
  180. * on return: points the beginning of the next line.
  181. * @param aBuffSize = the size in bytes of aBuff
  182. * @param aFile = the opened GERBER file to read
  183. * @return a pointer to the beginning of the next line or NULL if end of file
  184. */
  185. char* GetNextLine( char *aBuff, unsigned int aBuffSize, char* aText, FILE* aFile );
  186. bool GetEndOfBlock( char* aBuff, unsigned int aBuffSize, char*& aText, FILE* aGerberFile );
  187. /**
  188. * reads a single RS274X command terminated with a %
  189. */
  190. bool ReadRS274XCommand( char *aBuff, unsigned int aBuffSize, char*& aText );
  191. /**
  192. * executes a RS274X command
  193. */
  194. bool ExecuteRS274XCommand( int aCommand, char* aBuff,
  195. unsigned int aBuffSize, char*& aText );
  196. /**
  197. * reads two bytes of data and assembles them into an int with the first
  198. * byte in the sequence put into the most significant part of a 16 bit value
  199. * to build a RS274X command identifier.
  200. * @param text A reference to a pointer to read bytes from and to advance as
  201. * they are read.
  202. * @return a RS274X command identifier.
  203. */
  204. int ReadXCommandID( char*& text );
  205. /**
  206. * reads in an aperture macro and saves it in m_aperture_macros.
  207. * @param aBuff a character buffer at least GERBER_BUFZ long that can be
  208. * used to read successive lines from the gerber file.
  209. * @param text A reference to a character pointer which gives the initial
  210. * text to read from.
  211. * @param aBuffSize is the size of aBuff
  212. * @param gerber_file Which file to read from for continuation.
  213. * @return bool - true if a macro was read in successfully, else false.
  214. */
  215. bool ReadApertureMacro( char *aBuff, unsigned int aBuffSize,
  216. char* & text, FILE * gerber_file );
  217. // functions to execute G commands or D basic commands:
  218. bool Execute_G_Command( char*& text, int G_command );
  219. bool Execute_DCODE_Command( char*& text, int D_command );
  220. public:
  221. GERBER_FILE_IMAGE( int layer );
  222. virtual ~GERBER_FILE_IMAGE();
  223. wxString GetClass() const override
  224. {
  225. return wxT( "GERBER_FILE_IMAGE" );
  226. }
  227. /**
  228. * Read and load a gerber file.
  229. * @param aFullFileName = the full filename of the Gerber file
  230. * when the file cannot be loaded
  231. * Warning and info messages are stored in m_messagesList
  232. * @return bool if OK, false if the gerber file was not loaded
  233. */
  234. bool LoadGerberFile( const wxString& aFullFileName );
  235. const wxArrayString& GetMessages() const { return m_messagesList; }
  236. /**
  237. * @return the count of Dcode tools in use in the image
  238. */
  239. int GetDcodesCount();
  240. virtual void ResetDefaultValues();
  241. COLOR4D GetPositiveDrawColor() const { return m_PositiveDrawColor; }
  242. /**
  243. * @return a reference to the GERBER_DRAW_ITEMS deque list
  244. */
  245. GERBER_DRAW_ITEMS& GetItems() { return m_drawings; }
  246. /**
  247. * @return the count of GERBER_DRAW_ITEMS in the image
  248. */
  249. int GetItemsCount() { return m_drawings.size(); }
  250. /**
  251. * Add a new GERBER_DRAW_ITEM item to the drawings list
  252. * @param aItem is the GERBER_DRAW_ITEM to add to list
  253. */
  254. void AddItemToList( GERBER_DRAW_ITEM* aItem )
  255. {
  256. m_drawings.push_back( aItem );
  257. }
  258. /**
  259. * @return the last GERBER_DRAW_ITEM* item of the items list
  260. */
  261. GERBER_DRAW_ITEM* GetLastItemInList() const
  262. {
  263. return m_drawings.back();
  264. }
  265. /**
  266. * Function GetLayerParams
  267. * @return the current layers params
  268. */
  269. GERBER_LAYER& GetLayerParams()
  270. {
  271. return m_GBRLayerParams;
  272. }
  273. /**
  274. * Function HasNegativeItems
  275. * @return true if at least one item must be drawn in background color
  276. * used to optimize screen refresh (when no items are in background color
  277. * refresh can be faster)
  278. */
  279. bool HasNegativeItems();
  280. /**
  281. * Function ClearMessageList
  282. * Clear the message list
  283. * Call it before reading a Gerber file
  284. */
  285. void ClearMessageList()
  286. {
  287. m_messagesList.Clear();
  288. }
  289. /**
  290. * Function AddMessageToList
  291. * Add a message to the message list
  292. */
  293. void AddMessageToList( const wxString& aMessage )
  294. {
  295. m_messagesList.Add( aMessage );
  296. }
  297. /**
  298. * Function InitToolTable
  299. */
  300. void InitToolTable();
  301. /**
  302. * Function ReadXYCoord
  303. * Returns the current coordinate type pointed to by XnnYnn Text (XnnnnYmmmm)
  304. * @param aText is a pointer to the text to parse.
  305. * @param aExcellonMode = true to parse a Excellon drill file.
  306. * it force truncation of a digit string to a max len because the exact coordinate
  307. * format is not always known
  308. */
  309. wxPoint ReadXYCoord( char*& aText, bool aExcellonMode = false );
  310. /**
  311. * Returns the current coordinate type pointed to by InnJnn Text (InnnnJmmmm)
  312. * These coordinates are relative, so if coordinate is absent, it's value
  313. * defaults to 0
  314. */
  315. wxPoint ReadIJCoord( char*& Text );
  316. // functions to read G commands or D commands:
  317. int GCodeNumber( char*& Text );
  318. int DCodeNumber( char*& Text );
  319. /**
  320. * Function GetDCODEOrCreate
  321. * returns a pointer to the D_CODE within this GERBER for the given
  322. * \a aDCODE.
  323. * @param aDCODE The numeric value of the D_CODE to look up.
  324. * @param aCreateIfNoExist If true, then create the D_CODE if it does not
  325. * exist in list.
  326. * @return D_CODE* - the one implied by the given \a aDCODE, or NULL
  327. * if the requested \a aDCODE is out of range.
  328. */
  329. D_CODE* GetDCODEOrCreate( int aDCODE, bool aCreateIfNoExist = true );
  330. /**
  331. * Function GetDCODE
  332. * returns a pointer to the D_CODE within this GERBER for the given
  333. * \a aDCODE.
  334. * @param aDCODE The numeric value of the D_CODE to look up.
  335. * @return D_CODE* - the one implied by the given \a aDCODE, or NULL
  336. * if the requested \a aDCODE is out of range.
  337. */
  338. D_CODE* GetDCODE( int aDCODE ) const;
  339. /**
  340. * Function FindApertureMacro
  341. * looks up a previously read in aperture macro.
  342. * @param aLookup A dummy APERTURE_MACRO with [only] the name field set.
  343. * @return APERTURE_MACRO* - the one with a matching name, or NULL if
  344. * not found.
  345. */
  346. APERTURE_MACRO* FindApertureMacro( const APERTURE_MACRO& aLookup );
  347. /**
  348. * Function StepAndRepeatItem
  349. * Gerber format has a command Step an Repeat
  350. * This function must be called when reading a gerber file and
  351. * after creating a new gerber item that must be repeated
  352. * (i.e when m_XRepeatCount or m_YRepeatCount are > 1)
  353. * @param aItem = the item to repeat
  354. */
  355. void StepAndRepeatItem( const GERBER_DRAW_ITEM& aItem );
  356. /**
  357. * Function DisplayImageInfo
  358. * has knowledge about the frame and how and where to put status information
  359. * about this object into the frame's message panel.
  360. * Display info about Image Parameters.
  361. * @param aMainFrame = the GERBVIEW_FRAME to display messages
  362. */
  363. void DisplayImageInfo( GERBVIEW_FRAME* aMainFrame );
  364. /**
  365. * Function RemoveAttribute.
  366. * Called when a %TD command is found the Gerber file
  367. * @param aAttribute is the X2_ATTRIBUTE which stores the full command
  368. * Remove the attribute specified by the %TD command.
  369. * is no attribute, all current attributes specified by the %TO and the %TA
  370. * commands are cleared.
  371. * if a attribute name is specified (for instance %TD.CN*%) is specified,
  372. * only this attribute is cleared
  373. */
  374. void RemoveAttribute( X2_ATTRIBUTE& aAttribute );
  375. ///> @copydoc EDA_ITEM::Visit()
  376. SEARCH_RESULT Visit( INSPECTOR inspector, void* testData, const KICAD_T scanTypes[] ) override;
  377. #if defined(DEBUG)
  378. void Show( int nestLevel, std::ostream& os ) const override { ShowDummy( os ); }
  379. #endif
  380. };
  381. #endif // ifndef GERBER_FILE_IMAGE_H