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 <developer@lura.sk>
5  * Copyright (C) 2007, 2008, 2012-2013 Alexander Lunev <al.lunev@yahoo.com>
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 <common.h>
30 #include <footprint.h>
31 #include <fp_shape.h>
32 #include <math/util.h> // for KiROUND
33 #include <xnode.h>
34 #include <zone.h>
35 
36 #include <wx/gdicmn.h>
37 #include <wx/string.h>
38 
39 namespace PCAD2KICAD {
40 
41 PCB_POLYGON::PCB_POLYGON( PCB_CALLBACKS* aCallbacks, BOARD* aBoard, int aPCadLayer ) :
42  PCB_COMPONENT( aCallbacks, aBoard )
43 {
44  m_width = 0;
45 
46  // P-CAD polygons are similar to zones (and we're going to convert them as such), except
47  // that they don't avoid other copper pours. So effectively they're very-high-priority
48  // zones.
49  m_priority = 100000;
50 
51  m_objType = wxT( 'Z' );
52  m_PCadLayer = aPCadLayer;
54  m_filled = true;
55 }
56 
57 
59 {
60  int i, island;
61 
62  for( i = 0; i < (int) m_outline.GetCount(); i++ )
63  {
64  delete m_outline[i];
65  }
66 
67  for( island = 0; island < (int) m_cutouts.GetCount(); island++ )
68  {
69  for( i = 0; i < (int) m_cutouts[island]->GetCount(); i++ )
70  {
71  delete (*m_cutouts[island])[i];
72  }
73 
74  delete m_cutouts[island];
75  }
76 
77  for( island = 0; island < (int) m_islands.GetCount(); island++ )
78  {
79  for( i = 0; i < (int) m_islands[island]->GetCount(); i++ )
80  {
81  delete (*m_islands[island])[i];
82  }
83 
84  delete m_islands[island];
85  }
86 }
87 
88 void PCB_POLYGON::AssignNet( const wxString& aNetName )
89 {
90  m_net = aNetName;
92 }
93 
94 void PCB_POLYGON::SetOutline( VERTICES_ARRAY* aOutline )
95 {
96  int i;
97 
98  m_outline.Empty();
99 
100  for( i = 0; i < (int) aOutline->GetCount(); i++ )
101  m_outline.Add( new wxRealPoint( (*aOutline)[i]->x, (*aOutline)[i]->y ) );
102 
103  if( m_outline.Count() > 0 )
104  {
105  m_positionX = m_outline[0]->x;
106  m_positionY = m_outline[0]->y;
107  }
108 }
109 
110 void PCB_POLYGON::FormPolygon( XNODE* aNode, VERTICES_ARRAY* aPolygon,
111  const wxString& aDefaultMeasurementUnit,
112  const wxString& aActualConversion )
113 {
114  XNODE* lNode;
115  double x, y;
116 
117  lNode = FindNode( aNode, wxT( "pt" ) );
118 
119  while( lNode )
120  {
121  if( lNode->GetName() == wxT( "pt" ) )
122  {
124  lNode->GetNodeContent(), aDefaultMeasurementUnit, &x, &y, aActualConversion );
125  aPolygon->Add( new wxRealPoint( x, y ) );
126  }
127 
128  lNode = lNode->GetNext();
129  }
130 }
131 
132 
134  const wxString& aDefaultMeasurementUnit,
135  const wxString& aActualConversion )
136 {
137  XNODE* lNode;
138  wxString propValue;
139 
140  lNode = FindNode( aNode, wxT( "netNameRef" ) );
141 
142  if( lNode )
143  {
144  lNode->GetAttribute( wxT( "Name" ), &propValue );
145  propValue.Trim( false );
146  propValue.Trim( true );
147  m_net = propValue;
149  }
150 
151  // retrieve polygon outline
152  FormPolygon( aNode, &m_outline, aDefaultMeasurementUnit, aActualConversion );
153 
154  m_positionX = m_outline[0]->x;
155  m_positionY = m_outline[0]->y;
156 
157  // fill the polygon with the same contour as its outline is
158  m_islands.Add( new VERTICES_ARRAY );
159  FormPolygon( aNode, m_islands[0], aDefaultMeasurementUnit, aActualConversion );
160 
161  return true;
162 }
163 
164 
166 {
168  {
169  FP_SHAPE* dwg = new FP_SHAPE( aFootprint, PCB_SHAPE_TYPE::POLYGON );
170  aFootprint->Add( dwg );
171 
172  dwg->SetWidth( 0 );
173  dwg->SetLayer( m_KiCadLayer );
174 
175  auto outline = new std::vector<wxPoint>;
176  for( auto point : m_outline )
177  outline->push_back( wxPoint( point->x, point->y ) );
178 
179  dwg->SetPolyPoints( *outline );
180  dwg->SetStart0( *outline->begin() );
181  dwg->SetEnd0( outline->back() );
182  dwg->SetDrawCoord();
183 
184  delete( outline );
185  }
186 }
187 
188 
190 {
191  int i = 0;
192 
193  if( m_outline.GetCount() > 0 )
194  {
195  ZONE* zone = new ZONE( m_board );
196  m_board->Add( zone, ADD_MODE::APPEND );
197 
198  zone->SetLayer( m_KiCadLayer );
199  zone->SetNetCode( m_netCode );
200 
201  // add outline
202  for( i = 0; i < (int) m_outline.GetCount(); i++ )
203  {
204  zone->AppendCorner( wxPoint( KiROUND( m_outline[i]->x ),
205  KiROUND( m_outline[i]->y ) ), -1 );
206  }
207 
208  zone->SetLocalClearance( m_width );
209 
210  zone->SetPriority( m_priority );
211 
213  zone->GetDefaultHatchPitch(), true );
214 
215  if ( m_objType == wxT( 'K' ) )
216  {
217  zone->SetIsRuleArea( true );
218  zone->SetDoNotAllowTracks( true );
219  zone->SetDoNotAllowVias( true );
220  zone->SetDoNotAllowPads( true );
221  zone->SetDoNotAllowCopperPour( true );
222  zone->SetDoNotAllowFootprints( false );
223  }
224  else if( m_objType == wxT( 'C' ) )
225  {
226  // convert cutouts to keepouts because standalone cutouts are not supported in KiCad
227  zone->SetIsRuleArea( true );
228  zone->SetDoNotAllowCopperPour( true );
229  zone->SetDoNotAllowTracks( false );
230  zone->SetDoNotAllowVias( false );
231  zone->SetDoNotAllowPads( false );
232  zone->SetDoNotAllowFootprints( false );
233  }
234 
235  //if( m_filled )
236  // zone->BuildFilledPolysListData( m_board );
237  }
238 }
239 
240 
242 {
244 
246 }
247 
248 
249 void PCB_POLYGON::SetPosOffset( int aX_offs, int aY_offs )
250 {
251  int i, island;
252 
253  PCB_COMPONENT::SetPosOffset( aX_offs, aY_offs );
254 
255  for( i = 0; i < (int) m_outline.GetCount(); i++ )
256  {
257  m_outline[i]->x += aX_offs;
258  m_outline[i]->y += aY_offs;
259  }
260 
261  for( island = 0; island < (int) m_islands.GetCount(); island++ )
262  {
263  for( i = 0; i < (int) m_islands[island]->GetCount(); i++ )
264  {
265  (*m_islands[island])[i]->x += aX_offs;
266  (*m_islands[island])[i]->y += aY_offs;
267  }
268  }
269 
270  for( island = 0; island < (int) m_cutouts.GetCount(); island++ )
271  {
272  for( i = 0; i < (int) m_cutouts[island]->GetCount(); i++ )
273  {
274  (*m_cutouts[island])[i]->x += aX_offs;
275  (*m_cutouts[island])[i]->y += aY_offs;
276  }
277  }
278 }
279 
280 } // namespace PCAD2KICAD
void SetEnd0(const wxPoint &aPoint)
Definition: fp_shape.h:114
void SetDoNotAllowTracks(bool aEnable)
Definition: zone.h:742
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:192
void SetBorderDisplayStyle(ZONE_BORDER_DISPLAY_STYLE aHatchStyle, int aHatchPitch, bool aRebuildHatch)
Set all hatch parameters for the zone.
Definition: zone.cpp:898
PCB_LAYER_ID FlipLayer(PCB_LAYER_ID aLayerId, int aCopperLayersCount)
Definition: lset.cpp:521
void SetOutline(VERTICES_ARRAY *aOutline)
Definition: pcb_polygon.cpp:94
void AddToFootprint(FOOTPRINT *aFootprint) override
bool SetNetCode(int aNetCode, bool aNoAssert)
Set net using a net code.
void AddToBoard() override
ISLANDS_ARRAY m_cutouts
Definition: pcb_polygon.h:68
virtual bool Parse(XNODE *aNode, const wxString &aDefaultMeasurementUnit, const wxString &aActualConversion)
void SetPriority(unsigned aPriority)
Definition: zone.h:117
virtual void Flip() override
bool IsNonCopperLayer(LAYER_NUM aLayerId)
Test whether a layer is a non copper layer.
void SetIsRuleArea(bool aEnable)
Definition: zone.h:739
polygon (not yet used for tracks, but could be in microwave apps)
virtual void SetPosOffset(int aX_offs, int aY_offs)
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT) override
Adds an item to the container.
Definition: board.cpp:606
VERTICES_ARRAY m_outline
Definition: pcb_polygon.h:66
static int GetDefaultHatchPitch()
Definition: zone.cpp:1092
void SetDoNotAllowPads(bool aEnable)
Definition: zone.h:743
int GetNetCode(wxString aNetName) const
Definition: pcb_component.h:57
virtual void SetPosOffset(int aX_offs, int aY_offs) override
void SetDoNotAllowVias(bool aEnable)
Definition: zone.h:741
Handle a list of polygons defining a copper zone.
Definition: zone.h:57
void SetDrawCoord()
Set draw coordinates (absolute values ) from relative coordinates.
Definition: fp_shape.cpp:82
void SetStart0(const wxPoint &aPoint)
Definition: fp_shape.h:111
void SetDoublePrecisionPosition(wxString aStr, const wxString &aDefaultMeasurementUnit, double *aX, double *aY, const wxString &aActualConversion)
PCB_LAYER_ID GetKiCadLayer() const
Definition: pcb_component.h:56
Hold an XML or S-expression element.
Definition: xnode.h:43
XNODE * GetNext() const
Definition: xnode.h:67
bool AppendCorner(wxPoint aPosition, int aHoleIdx, bool aAllowDuplication=false)
Add a new corner to the zone outline (to the main outline or a hole)
Definition: zone.cpp:838
ISLANDS_ARRAY m_islands
Definition: pcb_polygon.h:67
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
void SetDoNotAllowCopperPour(bool aEnable)
Definition: zone.h:740
void FormPolygon(XNODE *aNode, VERTICES_ARRAY *aPolygon, const wxString &aDefaultMeasurementUnit, const wxString &actualConversion)
void SetWidth(int aWidth)
Definition: pcb_shape.h:96
XNODE * FindNode(XNODE *aChild, const wxString &aTag)
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:70
The common library.
virtual void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
Definition: zone.cpp:238
void SetLocalClearance(int aClearance)
Definition: zone.h:158
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT) override
Removes an item from the container.
Definition: footprint.cpp:472
void AssignNet(const wxString &aNetName)
Definition: pcb_polygon.cpp:88
void SetPolyPoints(const std::vector< wxPoint > &aPoints)
Definition: pcb_shape.cpp:1103
PCB_POLYGON(PCB_CALLBACKS *aCallbacks, BOARD *aBoard, int aPCadLayer)
Definition: pcb_polygon.cpp:41
void SetDoNotAllowFootprints(bool aEnable)
Definition: zone.h:744