1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 1992-2010 Jean-Pierre Charras <jean-pierre.charras@gipsa-lab.inpg.fr>
5  * Copyright (C) 2010 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 1992-2021 KiCad Developers, see change_log.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
24  */
25 
26 /**
27  * @file dcode.h
28  */
29 
30 #ifndef _DCODE_H_
31 #define _DCODE_H_
32 
33 #include <vector>
34 
35 #include <gal/color4d.h>
36 #include <geometry/shape_poly_set.h>
37 
38 using KIGFX::COLOR4D;
39 
40 class wxDC;
41 class GERBER_DRAW_ITEM;
42 class EDA_RECT;
43 
44 
45 /**
46  * The set of all gerber aperture types allowed, according to page 16 of
47  * http://gerbv.sourceforge.net/docs/rs274xrevd_e.pdf
48  */
49 enum APERTURE_T {
50     APT_CIRCLE  = 'C',      // Flashed shape: Circle with or without hole
51     APT_RECT    = 'R',      // Flashed shape: Rectangle with or without hole
52     APT_OVAL    = '0',      // Flashed shape: Oval with or without hole
53     APT_POLYGON = 'P',      // Flashed shape: Regular polygon (3 to 12 edges)
54                             // with or without hole. Can be rotated
55     APT_MACRO   = 'M'       // Complex shape given by a macro definition (see AM_PRIMITIVE_ID)
56 };
57 
58 // In aperture definition, round, oval and rectangular flashed shapes
59 // can have a hole (round or rectangular)
60 // this option is stored in .m_DrillShape D_CODE member
61 enum APERTURE_DEF_HOLETYPE {
62     APT_DEF_NO_HOLE = 0,
63     APT_DEF_ROUND_HOLE,
64     APT_DEF_RECT_HOLE
65 };
66 
67 /* define min and max values for D Codes values.
68  * note: values >= 0 and < FIRST_DCODE can be used for special purposes
69  */
70 #define FIRST_DCODE     10
71 #define LAST_DCODE      10000
72 #define TOOLS_MAX_COUNT (LAST_DCODE + 1)
73 
74 struct APERTURE_MACRO;
75 
76 
77 /**
78  * A gerber DCODE (also called Aperture) definition.
79  */
80 class D_CODE
81 {
82 public:
83     D_CODE( int num_dcode );
84     ~D_CODE();
85     void Clear_D_CODE_Data();
86 
87     /**
88      * Add a parameter to the D_CODE parameter list.
89      *
90      * Used to customize the corresponding aperture macro.
91      */
AppendParam(double aValue)92     void AppendParam( double aValue )
93     {
94         m_am_params.push_back( aValue );
95     }
96 
97     /**
98      * Return the number of parameters stored in parameter list.
99      */
GetParamCount()100     unsigned GetParamCount() const
101     {
102        return  m_am_params.size();
103     }
104 
105     /**
106      * Return a parameter stored in parameter list.
107      *
108      * @param aIdx is the index of parameter.
109      */
GetParam(unsigned aIdx)110     double GetParam( unsigned aIdx ) const
111     {
112         wxASSERT( aIdx <= m_am_params.size() );
113 
114         if( aIdx <= m_am_params.size() )
115             return  m_am_params[aIdx - 1];
116         else
117             return 0;
118     }
119 
SetMacro(APERTURE_MACRO * aMacro)120     void SetMacro( APERTURE_MACRO* aMacro )
121     {
122         m_Macro = aMacro;
123     }
124 
GetMacro()125     APERTURE_MACRO* GetMacro() const { return m_Macro; }
126 
127     /**
128      * Return a character string telling what type of aperture type \a aType is.
129      *
130      * @param aType is the aperture type to show.
131      */
132     static const wxChar* ShowApertureType( APERTURE_T aType );
133 
134     /**
135      * Draw the dcode shape for flashed items.
136      *
137      * When an item is flashed, the DCode shape is the shape of the item.
138      *
139      * @param aParent is the #GERBER_DRAW_ITEM being drawn.
140      * @param aClipBox is the device context clip box (NULL is no clip).
141      * @param aDC is the device context.
142      * @param aColor is the normal color to use.
143      * @param aShapePos is the actual shape position
144      * @param aFilledShape set to true to draw in filled mode, false to draw in sketch mode
145      */
146     void DrawFlashedShape( GERBER_DRAW_ITEM* aParent, EDA_RECT* aClipBox, wxDC* aDC,
147                            const COLOR4D& aColor, const wxPoint& aShapePos, bool aFilledShape );
148 
149     /**
150      * A helper function used to draw the polygon stored in m_PolyCorners.
151      *
152      * Draw some Apertures shapes when they are defined as filled polygons. APT_POLYGON is
153      * always a polygon, but some complex shapes are also converted to polygons (shapes with
154      * holes, some rotated shapes).
155      *
156      * @param aParent is the #GERBER_DRAW_ITEM being drawn.
157      * @param aClipBox is the device context clip box (NULL is no clip).
158      * @param aDC is the device context.
159      * @param aColor is the normal color to use.
160      * @param aFilled set to true to draw in filled mode, false to draw in sketch mode.
161      * @param aPosition is the actual shape position.
162      */
163     void DrawFlashedPolygon( GERBER_DRAW_ITEM* aParent, EDA_RECT* aClipBox, wxDC* aDC,
164                              const COLOR4D& aColor, bool aFilled, const wxPoint& aPosition );
165 
166     /**
167      * Convert a shape to an equivalent polygon.
168      *
169      * Arcs and circles are approximated by segments.  Useful when a shape is not a graphic
170      * primitive (shape with hole, rotated shape ... ) and cannot be easily drawn.
171      */
172     void ConvertShapeToPolygon();
173 
174     /**
175      * Calculate a value that can be used to evaluate the size of text when displaying the
176      * D-Code of an item.
177      *
178      * Due to the complexity of some shapes, one cannot calculate the "size" of a shape (only
179      * a bounding box) but here, the "dimension" of the shape is the diameter of the primitive
180      * or for lines the width of the line if the shape is a line.
181      *
182      * @param aParent is the parent GERBER_DRAW_ITEM which is actually drawn.
183      * @return a dimension, or -1 if no dim to calculate.
184      */
185     int GetShapeDim( GERBER_DRAW_ITEM* aParent );
186 
187 public:
188     wxSize                m_Size;           ///< Horizontal and vertical dimensions.
189     APERTURE_T            m_Shape;          ///< shape ( Line, rectangle, circle , oval .. )
190     int                   m_Num_Dcode;      ///< D code value ( >= 10 )
191     wxSize                m_Drill;          ///< dimension of the hole (if any) (drill file)
192     APERTURE_DEF_HOLETYPE m_DrillShape;     ///< shape of the hole (0 = no hole, round = 1,
193                                             ///< rect = 2).
194     double                m_Rotation;       ///< shape rotation in degrees
195     int                   m_EdgesCount;     ///< in aperture definition Polygon only:
196                                             ///< number of edges for the polygon
197     bool                  m_InUse;          ///< false if the aperture (previously defined)
198                                             ///< is not used to draw something
199     bool                  m_Defined;        ///< false if the aperture is not defined in the header
200     wxString              m_AperFunction;   ///< the aperture attribute (created by a
201                                             ///< %TA.AperFunction command).
202                                             ///< attached to the D_CODE
203     SHAPE_POLY_SET        m_Polygon;        /* Polygon used to draw APT_POLYGON shape and some other
204                                              * complex shapes which are converted to polygon
205                                              * (shapes with hole )
206                                              */
207 
208 private:
209     APERTURE_MACRO* m_Macro;    ///< no ownership, points to GERBER.m_aperture_macros element.
210 
211     /**
212      * parameters used only when this D_CODE holds a reference to an aperture
213      * macro, and these parameters would customize the macro.
214      */
215     std::vector<double>   m_am_params;
216 };
217 
218 
219 #endif  // ifndef _DCODE_H_
220