KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcad_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-2023 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/pcad_polygon.h>
27
28#include <board.h>
29#include <footprint.h>
30#include <pcb_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
39PCAD_POLYGON::PCAD_POLYGON( PCAD_CALLBACKS* aCallbacks, BOARD* aBoard, int aPCadLayer ) :
40 PCAD_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 delete m_Outline[i];
62
63 for( island = 0; island < (int) m_Cutouts.GetCount(); island++ )
64 {
65 for( i = 0; i < (int) m_Cutouts[island]->GetCount(); i++ )
66 delete (*m_Cutouts[island])[i];
67
68 delete m_Cutouts[island];
69 }
70
71 for( island = 0; island < (int) m_Islands.GetCount(); island++ )
72 {
73 for( i = 0; i < (int) m_Islands[island]->GetCount(); i++ )
74 delete (*m_Islands[island])[i];
75
76 delete m_Islands[island];
77 }
78}
79
80void PCAD_POLYGON::AssignNet( const wxString& aNetName )
81{
82 m_Net = aNetName;
84}
85
86void PCAD_POLYGON::SetOutline( VERTICES_ARRAY* aOutline )
87{
88 int i;
89
90 m_Outline.Empty();
91
92 for( i = 0; i < (int) aOutline->GetCount(); i++ )
93 m_Outline.Add( new wxRealPoint( (*aOutline)[i]->x, (*aOutline)[i]->y ) );
94
95 if( m_Outline.Count() > 0 )
96 {
97 m_PositionX = m_Outline[0]->x;
98 m_PositionY = m_Outline[0]->y;
99 }
100}
101
102void PCAD_POLYGON::FormPolygon( XNODE* aNode, VERTICES_ARRAY* aPolygon,
103 const wxString& aDefaultUnits, const wxString& aActualConversion )
104{
105 XNODE* lNode;
106 double x, y;
107
108 lNode = FindNode( aNode, wxT( "pt" ) );
109
110 while( lNode )
111 {
112 if( lNode->GetName() == wxT( "pt" ) )
113 {
114 SetDoublePrecisionPosition( lNode->GetNodeContent(), aDefaultUnits, &x, &y,
115 aActualConversion );
116 aPolygon->Add( new wxRealPoint( x, y ) );
117 }
118
119 lNode = lNode->GetNext();
120 }
121}
122
123
124bool PCAD_POLYGON::Parse( XNODE* aNode, const wxString& aDefaultUnits,
125 const wxString& aActualConversion )
126{
127 XNODE* lNode;
128 wxString propValue;
129
130 lNode = FindNode( aNode, wxT( "netNameRef" ) );
131
132 if( lNode )
133 {
134 lNode->GetAttribute( wxT( "Name" ), &propValue );
135 propValue.Trim( false );
136 propValue.Trim( true );
137 m_Net = propValue;
139 }
140
141 // retrieve polygon outline
142 FormPolygon( aNode, &m_Outline, aDefaultUnits, aActualConversion );
143
144 m_PositionX = m_Outline[0]->x;
145 m_PositionY = m_Outline[0]->y;
146
147 // fill the polygon with the same contour as its outline is
148 m_Islands.Add( new VERTICES_ARRAY );
149 FormPolygon( aNode, m_Islands[0], aDefaultUnits, aActualConversion );
150
151 return true;
152}
153
154
156{
157 if( m_Outline.GetCount() > 0 )
158 {
159 if( aFootprint )
160 {
161 PCB_SHAPE* dwg = new PCB_SHAPE( aFootprint, SHAPE_T::POLY );
162 aFootprint->Add( dwg );
163
164 dwg->SetStroke( STROKE_PARAMS( 0 ) );
165 dwg->SetLayer( m_KiCadLayer );
166
167 auto outline = new std::vector<VECTOR2I>;
168
169 for( auto point : m_Outline )
170 outline->push_back( VECTOR2I( point->x, point->y ) );
171
172 dwg->SetPolyPoints( *outline );
173 dwg->SetStart( *outline->begin() );
174 dwg->SetEnd( outline->back() );
175 dwg->Rotate( { 0, 0 }, aFootprint->GetOrientation() );
176 dwg->Move( aFootprint->GetPosition() );
177
178 delete( outline );
179 }
180 else
181 {
182 ZONE* zone = new ZONE( m_board );
183 m_board->Add( zone, ADD_MODE::APPEND );
184
185 zone->SetLayer( m_KiCadLayer );
186 zone->SetNetCode( m_NetCode );
187
188 // add outline
189 for( int i = 0; i < (int) m_Outline.GetCount(); i++ )
190 {
191 zone->AppendCorner( VECTOR2I( KiROUND( m_Outline[i]->x ),
192 KiROUND( m_Outline[i]->y ) ), -1 );
193 }
194
195 zone->SetLocalClearance( m_Width );
196
198
199 zone->SetBorderDisplayStyle( ZONE_BORDER_DISPLAY_STYLE::DIAGONAL_EDGE,
200 zone->GetDefaultHatchPitch(), true );
201
202 if ( m_ObjType == wxT( 'K' ) )
203 {
204 zone->SetIsRuleArea( true );
205 zone->SetDoNotAllowTracks( true );
206 zone->SetDoNotAllowVias( true );
207 zone->SetDoNotAllowPads( true );
208 zone->SetDoNotAllowCopperPour( true );
209 zone->SetDoNotAllowFootprints( false );
210 }
211 else if( m_ObjType == wxT( 'C' ) )
212 {
213 // convert cutouts to keepouts because standalone cutouts are not supported in KiCad
214 zone->SetIsRuleArea( true );
215 zone->SetDoNotAllowCopperPour( true );
216 zone->SetDoNotAllowTracks( false );
217 zone->SetDoNotAllowVias( false );
218 zone->SetDoNotAllowPads( false );
219 zone->SetDoNotAllowFootprints( false );
220 }
221
222 //if( m_filled )
223 // zone->BuildFilledPolysListData( m_board );
224 }
225 }
226}
227
228
230{
232
234}
235
236
237void PCAD_POLYGON::SetPosOffset( int aX_offs, int aY_offs )
238{
239 int i, island;
240
241 PCAD_PCB_COMPONENT::SetPosOffset( aX_offs, aY_offs );
242
243 for( i = 0; i < (int) m_Outline.GetCount(); i++ )
244 {
245 m_Outline[i]->x += aX_offs;
246 m_Outline[i]->y += aY_offs;
247 }
248
249 for( island = 0; island < (int) m_Islands.GetCount(); island++ )
250 {
251 for( i = 0; i < (int) m_Islands[island]->GetCount(); i++ )
252 {
253 (*m_Islands[island])[i]->x += aX_offs;
254 (*m_Islands[island])[i]->y += aY_offs;
255 }
256 }
257
258 for( island = 0; island < (int) m_Cutouts.GetCount(); island++ )
259 {
260 for( i = 0; i < (int) m_Cutouts[island]->GetCount(); i++ )
261 {
262 (*m_Cutouts[island])[i]->x += aX_offs;
263 (*m_Cutouts[island])[i]->y += aY_offs;
264 }
265 }
266}
267
268} // namespace PCAD2KICAD
bool SetNetCode(int aNetCode, bool aNoAssert)
Set net using a net code.
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:281
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition: board.cpp:882
void SetStart(const VECTOR2I &aStart)
Definition: eda_shape.h:131
void SetEnd(const VECTOR2I &aEnd)
Definition: eda_shape.h:156
void SetPolyPoints(const std::vector< VECTOR2I > &aPoints)
Definition: eda_shape.cpp:1202
EDA_ANGLE GetOrientation() const
Definition: footprint.h:212
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition: footprint.cpp:969
VECTOR2I GetPosition() const override
Definition: footprint.h:209
virtual void SetPosOffset(int aX_offs, int aY_offs)
PCB_LAYER_ID GetKiCadLayer() const
int GetNetCode(const wxString &aNetName) const
virtual void Flip() override
void SetOutline(VERTICES_ARRAY *aOutline)
virtual bool Parse(XNODE *aNode, const wxString &aDefaultUnits, const wxString &aActualConversion)
ISLANDS_ARRAY m_Islands
Definition: pcad_polygon.h:66
VERTICES_ARRAY m_Outline
Definition: pcad_polygon.h:65
ISLANDS_ARRAY m_Cutouts
Definition: pcad_polygon.h:67
void FormPolygon(XNODE *aNode, VERTICES_ARRAY *aPolygon, const wxString &aDefaultUnits, const wxString &actualConversion)
PCAD_POLYGON(PCAD_CALLBACKS *aCallbacks, BOARD *aBoard, int aPCadLayer)
void AssignNet(const wxString &aNetName)
void AddToBoard(FOOTPRINT *aFootprint=nullptr) override
virtual void SetPosOffset(int aX_offs, int aY_offs) override
void Rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle) override
Rotate this object.
Definition: pcb_shape.cpp:523
void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
Definition: pcb_shape.cpp:314
void Move(const VECTOR2I &aMoveVector) override
Move this object.
Definition: pcb_shape.cpp:452
void SetStroke(const STROKE_PARAMS &aStroke) override
Definition: pcb_shape.h:86
Simple container to manage line stroke parameters.
Definition: stroke_params.h:81
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:72
void SetDoNotAllowPads(bool aEnable)
Definition: zone.h:721
void SetLocalClearance(std::optional< int > aClearance)
Definition: zone.h:154
void SetBorderDisplayStyle(ZONE_BORDER_DISPLAY_STYLE aBorderHatchStyle, int aBorderHatchPitch, bool aRebuilBorderdHatch)
Set all hatch parameters for the zone.
Definition: zone.cpp:875
void SetDoNotAllowCopperPour(bool aEnable)
Definition: zone.h:718
virtual void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
Definition: zone.cpp:261
void SetIsRuleArea(bool aEnable)
Definition: zone.h:717
void SetDoNotAllowTracks(bool aEnable)
Definition: zone.h:720
void SetDoNotAllowVias(bool aEnable)
Definition: zone.h:719
void SetDoNotAllowFootprints(bool aEnable)
Definition: zone.h:722
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:801
void SetAssignedPriority(unsigned aPriority)
Definition: zone.h:114
static int GetDefaultHatchPitch()
Definition: zone.cpp:1051
PCB_LAYER_ID FlipLayer(PCB_LAYER_ID aLayerId, int aCopperLayersCount)
Definition: lset.cpp:634
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:588