KiCad PCB EDA Suite
pcb_polygon.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2007, 2008 Lubo Racko <[email protected]>
5 * Copyright (C) 2007, 2008, 2012-2013 Alexander Lunev <[email protected]>
6 * Copyright (C) 2017-2020 KiCad Developers, see AUTHORS.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#include <pcad/pcb_polygon.h>
27
28#include <board.h>
29#include <footprint.h>
30#include <fp_shape.h>
31#include <math/util.h> // for KiROUND
32#include <xnode.h>
33#include <zone.h>
34
35#include <wx/string.h>
36
37namespace PCAD2KICAD {
38
39PCB_POLYGON::PCB_POLYGON( PCB_CALLBACKS* aCallbacks, BOARD* aBoard, int aPCadLayer ) :
40 PCB_COMPONENT( aCallbacks, aBoard )
41{
42 m_width = 0;
43
44 // P-CAD polygons are similar to zones (and we're going to convert them as such), except
45 // that they don't avoid other copper pours. So effectively they're very-high-priority
46 // zones.
47 m_priority = 100000;
48
49 m_objType = wxT( 'Z' );
50 m_PCadLayer = aPCadLayer;
52 m_filled = true;
53}
54
55
57{
58 int i, island;
59
60 for( i = 0; i < (int) m_outline.GetCount(); i++ )
61 {
62 delete m_outline[i];
63 }
64
65 for( island = 0; island < (int) m_cutouts.GetCount(); island++ )
66 {
67 for( i = 0; i < (int) m_cutouts[island]->GetCount(); i++ )
68 {
69 delete (*m_cutouts[island])[i];
70 }
71
72 delete m_cutouts[island];
73 }
74
75 for( island = 0; island < (int) m_islands.GetCount(); island++ )
76 {
77 for( i = 0; i < (int) m_islands[island]->GetCount(); i++ )
78 {
79 delete (*m_islands[island])[i];
80 }
81
82 delete m_islands[island];
83 }
84}
85
86void PCB_POLYGON::AssignNet( const wxString& aNetName )
87{
88 m_net = aNetName;
90}
91
92void PCB_POLYGON::SetOutline( VERTICES_ARRAY* aOutline )
93{
94 int i;
95
96 m_outline.Empty();
97
98 for( i = 0; i < (int) aOutline->GetCount(); i++ )
99 m_outline.Add( new wxRealPoint( (*aOutline)[i]->x, (*aOutline)[i]->y ) );
100
101 if( m_outline.Count() > 0 )
102 {
103 m_positionX = m_outline[0]->x;
104 m_positionY = m_outline[0]->y;
105 }
106}
107
108void PCB_POLYGON::FormPolygon( XNODE* aNode, VERTICES_ARRAY* aPolygon,
109 const wxString& aDefaultUnits, const wxString& aActualConversion )
110{
111 XNODE* lNode;
112 double x, y;
113
114 lNode = FindNode( aNode, wxT( "pt" ) );
115
116 while( lNode )
117 {
118 if( lNode->GetName() == wxT( "pt" ) )
119 {
120 SetDoublePrecisionPosition( lNode->GetNodeContent(), aDefaultUnits, &x, &y,
121 aActualConversion );
122 aPolygon->Add( new wxRealPoint( x, y ) );
123 }
124
125 lNode = lNode->GetNext();
126 }
127}
128
129
130bool PCB_POLYGON::Parse( XNODE* aNode, const wxString& aDefaultUnits,
131 const wxString& aActualConversion )
132{
133 XNODE* lNode;
134 wxString propValue;
135
136 lNode = FindNode( aNode, wxT( "netNameRef" ) );
137
138 if( lNode )
139 {
140 lNode->GetAttribute( wxT( "Name" ), &propValue );
141 propValue.Trim( false );
142 propValue.Trim( true );
143 m_net = propValue;
145 }
146
147 // retrieve polygon outline
148 FormPolygon( aNode, &m_outline, aDefaultUnits, aActualConversion );
149
150 m_positionX = m_outline[0]->x;
151 m_positionY = m_outline[0]->y;
152
153 // fill the polygon with the same contour as its outline is
154 m_islands.Add( new VERTICES_ARRAY );
155 FormPolygon( aNode, m_islands[0], aDefaultUnits, aActualConversion );
156
157 return true;
158}
159
160
162{
164 {
165 FP_SHAPE* dwg = new FP_SHAPE( aFootprint, SHAPE_T::POLY );
166 aFootprint->Add( dwg );
167
168 dwg->SetStroke( STROKE_PARAMS( 0 ) );
169 dwg->SetLayer( m_KiCadLayer );
170
171 auto outline = new std::vector<VECTOR2I>;
172 for( auto point : m_outline )
173 outline->push_back( VECTOR2I( point->x, point->y ) );
174
175 dwg->SetPolyPoints( *outline );
176 dwg->SetStart0( *outline->begin() );
177 dwg->SetEnd0( outline->back() );
178 dwg->SetDrawCoord();
179
180 delete( outline );
181 }
182}
183
184
186{
187 int i = 0;
188
189 if( m_outline.GetCount() > 0 )
190 {
191 ZONE* zone = new ZONE( m_board );
192 m_board->Add( zone, ADD_MODE::APPEND );
193
194 zone->SetLayer( m_KiCadLayer );
195 zone->SetNetCode( m_netCode );
196
197 // add outline
198 for( i = 0; i < (int) m_outline.GetCount(); i++ )
199 {
200 zone->AppendCorner( VECTOR2I( KiROUND( m_outline[i]->x ),
201 KiROUND( m_outline[i]->y ) ), -1 );
202 }
203
204 zone->SetLocalClearance( m_width );
205
207
209 zone->GetDefaultHatchPitch(), true );
210
211 if ( m_objType == wxT( 'K' ) )
212 {
213 zone->SetIsRuleArea( true );
214 zone->SetDoNotAllowTracks( true );
215 zone->SetDoNotAllowVias( true );
216 zone->SetDoNotAllowPads( true );
217 zone->SetDoNotAllowCopperPour( true );
218 zone->SetDoNotAllowFootprints( false );
219 }
220 else if( m_objType == wxT( 'C' ) )
221 {
222 // convert cutouts to keepouts because standalone cutouts are not supported in KiCad
223 zone->SetIsRuleArea( true );
224 zone->SetDoNotAllowCopperPour( true );
225 zone->SetDoNotAllowTracks( false );
226 zone->SetDoNotAllowVias( false );
227 zone->SetDoNotAllowPads( false );
228 zone->SetDoNotAllowFootprints( false );
229 }
230
231 //if( m_filled )
232 // zone->BuildFilledPolysListData( m_board );
233 }
234}
235
236
238{
240
242}
243
244
245void PCB_POLYGON::SetPosOffset( int aX_offs, int aY_offs )
246{
247 int i, island;
248
249 PCB_COMPONENT::SetPosOffset( aX_offs, aY_offs );
250
251 for( i = 0; i < (int) m_outline.GetCount(); i++ )
252 {
253 m_outline[i]->x += aX_offs;
254 m_outline[i]->y += aY_offs;
255 }
256
257 for( island = 0; island < (int) m_islands.GetCount(); island++ )
258 {
259 for( i = 0; i < (int) m_islands[island]->GetCount(); i++ )
260 {
261 (*m_islands[island])[i]->x += aX_offs;
262 (*m_islands[island])[i]->y += aY_offs;
263 }
264 }
265
266 for( island = 0; island < (int) m_cutouts.GetCount(); island++ )
267 {
268 for( i = 0; i < (int) m_cutouts[island]->GetCount(); i++ )
269 {
270 (*m_cutouts[island])[i]->x += aX_offs;
271 (*m_cutouts[island])[i]->y += aY_offs;
272 }
273 }
274}
275
276} // namespace PCAD2KICAD
bool SetNetCode(int aNetCode, bool aNoAssert)
Set net using a net code.
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:214
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:265
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition: board.cpp:711
void SetPolyPoints(const std::vector< VECTOR2I > &aPoints)
Definition: eda_shape.cpp:1109
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition: footprint.cpp:561
void SetEnd0(const VECTOR2I &aPoint)
Definition: fp_shape.h:94
void SetStart0(const VECTOR2I &aPoint)
Definition: fp_shape.h:91
virtual void SetDrawCoord()
Set draw coordinates (absolute values ) from relative coordinates.
Definition: fp_shape.cpp:80
int GetNetCode(const wxString &aNetName) const
Definition: pcb_component.h:58
PCB_LAYER_ID GetKiCadLayer() const
Definition: pcb_component.h:56
virtual void SetPosOffset(int aX_offs, int aY_offs)
void FormPolygon(XNODE *aNode, VERTICES_ARRAY *aPolygon, const wxString &aDefaultUnits, const wxString &actualConversion)
void AddToBoard() override
virtual void Flip() override
void AssignNet(const wxString &aNetName)
Definition: pcb_polygon.cpp:86
virtual void SetPosOffset(int aX_offs, int aY_offs) override
PCB_POLYGON(PCB_CALLBACKS *aCallbacks, BOARD *aBoard, int aPCadLayer)
Definition: pcb_polygon.cpp:39
virtual bool Parse(XNODE *aNode, const wxString &aDefaultUnits, const wxString &aActualConversion)
void SetOutline(VERTICES_ARRAY *aOutline)
Definition: pcb_polygon.cpp:92
VERTICES_ARRAY m_outline
Definition: pcb_polygon.h:66
ISLANDS_ARRAY m_cutouts
Definition: pcb_polygon.h:68
void AddToFootprint(FOOTPRINT *aFootprint) override
ISLANDS_ARRAY m_islands
Definition: pcb_polygon.h:67
void SetStroke(const STROKE_PARAMS &aStroke) override
Definition: pcb_shape.h:72
Simple container to manage line stroke parameters.
Definition: stroke_params.h:88
Hold an XML or S-expression element.
Definition: xnode.h:44
XNODE * GetNext() const
Definition: xnode.h:67
Handle a list of polygons defining a copper zone.
Definition: zone.h:57
void SetDoNotAllowPads(bool aEnable)
Definition: zone.h:708
void SetBorderDisplayStyle(ZONE_BORDER_DISPLAY_STYLE aBorderHatchStyle, int aBorderHatchPitch, bool aRebuilBorderdHatch)
Set all hatch parameters for the zone.
Definition: zone.cpp:817
void SetDoNotAllowCopperPour(bool aEnable)
Definition: zone.h:705
virtual void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
Definition: zone.cpp:257
void SetIsRuleArea(bool aEnable)
Definition: zone.h:704
void SetDoNotAllowTracks(bool aEnable)
Definition: zone.h:707
void SetDoNotAllowVias(bool aEnable)
Definition: zone.h:706
void SetLocalClearance(int aClearance)
Definition: zone.h:153
void SetDoNotAllowFootprints(bool aEnable)
Definition: zone.h:709
bool AppendCorner(VECTOR2I aPosition, int aHoleIdx, bool aAllowDuplication=false)
Add a new corner to the zone outline (to the main outline or a hole)
Definition: zone.cpp:757
void SetAssignedPriority(unsigned aPriority)
Definition: zone.h:107
static int GetDefaultHatchPitch()
Definition: zone.cpp:982
bool IsNonCopperLayer(int aLayerId)
Test whether a layer is a non copper layer.
Definition: layer_ids.h:836
PCB_LAYER_ID FlipLayer(PCB_LAYER_ID aLayerId, int aCopperLayersCount)
Definition: lset.cpp:544
XNODE * FindNode(XNODE *aChild, const wxString &aTag)
void SetDoublePrecisionPosition(const wxString &aStr, const wxString &aDefaultMeasurementUnit, double *aX, double *aY, const wxString &aActualConversion)
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:85
VECTOR2< int > VECTOR2I
Definition: vector2d.h:618