1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2009 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
5  * Copyright (C) 2009 Jean-Pierre Charras, jean-pierre.charras@inpg.fr
6  * Copyright (C) 2009-2020 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 #ifndef CLASS_NETCLASS_H
27 #define CLASS_NETCLASS_H
28 
29 
30 #include <gal/color4d.h>
31 #include <core/optional.h>
32 #include <macros_swig.h>
33 
34 class LINE_READER;
35 class BOARD;
36 class BOARD_DESIGN_SETTINGS;
37 using KIGFX::COLOR4D;
38 
39 
DECL_SET_FOR_SWIG(STRINGSET,wxString)40 DECL_SET_FOR_SWIG( STRINGSET, wxString )
41 
42 
43 /**
44  * A collection of nets and the parameters used to route or test these nets.
45  */
46 class NETCLASS
47 {
48 public:
49     static const char Default[];        ///< the name of the default NETCLASS
50 
51     /**
52      * Create a NETCLASS instance with \a aName.
53      *
54      * @param aName is the name of this new netclass.
55      */
56     NETCLASS( const wxString& aName );
57 
58     ~NETCLASS();
59 
60     wxString GetClass() const
61     {
62         return wxT( "NETCLASS" );
63     }
64 
65     const wxString GetName() const { return m_Name; }
66     void SetName( const wxString& aName ) { m_Name = aName; }
67 
68     /**
69      * Return the number of nets in this NETCLASS, i.e. using these rules.
70      */
71     unsigned GetCount() const
72     {
73         return m_Members.size();
74     }
75 
76     /**
77      * Empties the collection of members.
78      */
79     void Clear()
80     {
81         m_Members.clear();
82     }
83 
84     /**
85      * Adds \a aNetname to this NETCLASS if it is not already in this NETCLASS.
86      *
87      * It is harmless to try and add a second identical name.
88      */
89     void Add( const wxString& aNetname )
90     {
91         m_Members.insert( aNetname );
92     }
93 
94     typedef STRINGSET::iterator iterator;
95     iterator begin() { return m_Members.begin(); }
96     iterator end()   { return m_Members.end();   }
97 
98     typedef STRINGSET::const_iterator const_iterator;
99     const_iterator begin() const { return m_Members.begin(); }
100     const_iterator end()   const { return m_Members.end();   }
101 
102     /**
103      * Remove NET \a aName from the collection of members.
104      */
105     void Remove( iterator aName )
106     {
107         m_Members.erase( aName );
108     }
109 
110     /**
111      * Remove NET \a aName from the collection of members.
112      */
113     void Remove( const wxString& aName )
114     {
115         m_Members.erase( aName );
116     }
117 
118     STRINGSET& NetNames()                   { return m_Members; }       ///< for SWIG
119 
120     const wxString& GetDescription() const  { return m_Description; }
121     void  SetDescription( const wxString& aDesc ) { m_Description = aDesc; }
122 
123     bool    HasClearance() const { return (bool) m_Clearance; }
124     int     GetClearance() const { return m_Clearance.value_or(-1); }
125     void    SetClearance( int aClearance )  { m_Clearance = aClearance; }
126 
127     bool    HasTrackWidth() const { return (bool) m_TrackWidth; }
128     int     GetTrackWidth() const           { return m_TrackWidth.value_or( -1 ); }
129     void    SetTrackWidth( int aWidth )     { m_TrackWidth = aWidth; }
130 
131     bool    HasViaDiameter() const          { return (bool) m_ViaDia; }
132     int     GetViaDiameter() const          { return m_ViaDia.value_or( -1 ); }
133     void    SetViaDiameter( int aDia )      { m_ViaDia = aDia; }
134 
135     int     HasViaDrill() const             { return (bool) m_ViaDrill; }
136     int     GetViaDrill() const             { return m_ViaDrill.value_or( -1 ); }
137     void    SetViaDrill( int aSize )        { m_ViaDrill = aSize; }
138 
139     bool    HasuViaDiameter() const         { return (bool) m_uViaDia; }
140     int     GetuViaDiameter() const         { return m_uViaDia.value_or( -1 ); }
141     void    SetuViaDiameter( int aSize )    { m_uViaDia = aSize; }
142 
143     bool    HasuViaDrill() const            { return (bool) m_uViaDrill; }
144     int     GetuViaDrill() const            { return m_uViaDrill.value_or( -1 ); }
145     void    SetuViaDrill( int aSize )       { m_uViaDrill = aSize; }
146 
147     bool    HasDiffPairWidth() const        { return (bool) m_diffPairWidth; }
148     int     GetDiffPairWidth() const        { return m_diffPairWidth.value_or( -1 ); }
149     void    SetDiffPairWidth( int aSize )   { m_diffPairWidth = aSize; }
150 
151     bool    HasDiffPairGap() const          { return (bool) m_diffPairGap; }
152     int     GetDiffPairGap() const          { return m_diffPairGap.value_or( -1 ); }
153     void    SetDiffPairGap( int aSize )     { m_diffPairGap = aSize; }
154 
155     bool    HasDiffPairViaGap() const       { return (bool) m_diffPairViaGap; }
156     int     GetDiffPairViaGap() const       { return m_diffPairViaGap.value_or( -1 ); }
157     void    SetDiffPairViaGap( int aSize )  { m_diffPairViaGap = aSize; }
158 
159     COLOR4D GetPcbColor() const             { return m_PcbColor; }
160     void    SetPcbColor( const COLOR4D& aColor ) { m_PcbColor = aColor; }
161 
162     int     GetWireWidth() const            { return m_wireWidth; }
163     void    SetWireWidth( int aWidth )      { m_wireWidth = aWidth; }
164 
165     int     GetBusWidth() const             { return m_busWidth; }
166     void    SetBusWidth( int aWidth )       { m_busWidth = aWidth; }
167 
168     COLOR4D GetSchematicColor() const       { return m_schematicColor; }
169     void    SetSchematicColor( COLOR4D aColor ) { m_schematicColor = aColor; }
170 
171     int     GetLineStyle() const            { return m_lineStyle; }
172     void    SetLineStyle( int aStyle )      { m_lineStyle = aStyle; }
173 
174 #if defined(DEBUG)
175     void Show( int nestLevel, std::ostream& os ) const;
176 #endif
177 
178 protected:
179     wxString    m_Name;                 ///< Name of the net class
180     wxString    m_Description;          ///< what this NETCLASS is for.
181 
182     STRINGSET   m_Members;              ///< names of NET members of this class
183 
184     /// The units on these parameters is Internal Units (1 nm)
185 
186     OPT<int>         m_Clearance;            ///< clearance when routing
187 
188     OPT<int>         m_TrackWidth;           ///< track width used to route NETs in this NETCLASS
189     OPT<int>         m_ViaDia;               ///< via diameter
190     OPT<int>         m_ViaDrill;             ///< via drill hole diameter
191 
192     OPT<int>         m_uViaDia;              ///< microvia diameter
193     OPT<int>         m_uViaDrill;            ///< microvia drill hole diameter
194 
195     OPT<int>         m_diffPairWidth;
196     OPT<int>         m_diffPairGap;
197     OPT<int>         m_diffPairViaGap;
198 
199     int         m_wireWidth;
200     int         m_busWidth;
201     COLOR4D     m_schematicColor;
202     int         m_lineStyle;
203 
204     COLOR4D     m_PcbColor;          ///< Optional color override for this netclass (PCB context)
205 };
206 
207 
DECL_SPTR_FOR_SWIG(NETCLASSPTR,NETCLASS)208 DECL_SPTR_FOR_SWIG( NETCLASSPTR, NETCLASS )
209 DECL_MAP_FOR_SWIG( NETCLASS_MAP, wxString, NETCLASSPTR )
210 
211 
212 /**
213  * A container for NETCLASS instances.
214  *
215  * It owns all its NETCLASSes.  This container will always have a default NETCLASS with the
216  * name given by const NETCLASS::Default.
217  */
218 class NETCLASSES
219 {
220 public:
221     NETCLASSES();
222     ~NETCLASSES();
223 
224     /**
225      * Destroy any contained NETCLASS instances except the default one, and clears any
226      * members from the default one.
227      */
228     void Clear()
229     {
230         m_NetClasses.clear();
231         m_default->Clear();
232     }
233 
234     typedef NETCLASS_MAP::iterator iterator;
235     iterator begin() { return m_NetClasses.begin(); }
236     iterator end()   { return m_NetClasses.end(); }
237 
238     typedef NETCLASS_MAP::const_iterator const_iterator;
239     const_iterator begin() const { return m_NetClasses.begin(); }
240     const_iterator end()   const { return m_NetClasses.end(); }
241 
242     /**
243      * @return the number of netclasses excluding the default one.
244      */
245     unsigned GetCount() const
246     {
247         return m_NetClasses.size();
248     }
249 
250     /**
251      * @return the default net class.
252      */
253     NETCLASSPTR GetDefault() const
254     {
255         return m_default;
256     }
257 
258     NETCLASS* GetDefaultPtr() const
259     {
260         return m_default.get();
261     }
262 
263     /**
264      * Add \a aNetclass and puts it into this NETCLASSES container.
265      *
266      * @param aNetclass is netclass to add
267      * @return true if the name within aNetclass is unique and it could be inserted OK,
268      *         else false because the name was not unique.
269      */
270     bool Add( const NETCLASSPTR& aNetclass );
271 
272     /**
273      * Remove a #NETCLASS from this container but does not destroy/delete it.
274      *
275      * @param aNetName is the name of the net to delete, and it may not be NETCLASS::Default.
276      * @return a pointer to the #NETCLASS associated with \a aNetName if found and removed,
277      *         else NULL.
278      */
279     NETCLASSPTR Remove( const wxString& aNetName );
280 
281     /**
282      * Search this container for a NETCLASS given by \a aName.
283      *
284      * @param aName is the name of the #NETCLASS to search for.
285      * @return a pointer to the #NETCLASS if found, else NULL.
286      */
287     NETCLASSPTR Find( const wxString& aName ) const;
288 
289     /// Provide public access to m_NetClasses so it gets swigged.
290     NETCLASS_MAP&   NetClasses()       { return m_NetClasses; }
291 
292 private:
293     NETCLASS_MAP    m_NetClasses;   // All the netclasses EXCEPT the default one
294     NETCLASSPTR     m_default;
295 };
296 
297 #endif  // CLASS_NETCLASS_H
298