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 
30 #include <wx/wx.h>
31 
32 #include <common.h>
33 #include <math/util.h> // for KiROUND
34 
35 #include <pcb_polygon.h>
36 
37 namespace PCAD2KICAD {
38 
39 PCB_POLYGON::PCB_POLYGON( PCB_CALLBACKS* aCallbacks, BOARD* aBoard, int aPCadLayer ) :
40  PCB_COMPONENT( aCallbacks, aBoard )
41 {
42  m_width = 0;
43  m_priority = 0;
44  m_objType = wxT( 'Z' );
45  m_PCadLayer = aPCadLayer;
47  m_filled = true;
48 }
49 
50 
52 {
53  int i, island;
54 
55  for( i = 0; i < (int) m_outline.GetCount(); i++ )
56  {
57  delete m_outline[i];
58  }
59 
60  for( island = 0; island < (int) m_cutouts.GetCount(); island++ )
61  {
62  for( i = 0; i < (int) m_cutouts[island]->GetCount(); i++ )
63  {
64  delete (*m_cutouts[island])[i];
65  }
66 
67  delete m_cutouts[island];
68  }
69 
70  for( island = 0; island < (int) m_islands.GetCount(); island++ )
71  {
72  for( i = 0; i < (int) m_islands[island]->GetCount(); i++ )
73  {
74  delete (*m_islands[island])[i];
75  }
76 
77  delete m_islands[island];
78  }
79 }
80 
81 void PCB_POLYGON::AssignNet( const wxString& aNetName )
82 {
83  m_net = aNetName;
85 }
86 
87 void PCB_POLYGON::SetOutline( VERTICES_ARRAY* aOutline )
88 {
89  int i;
90 
91  m_outline.Empty();
92 
93  for( i = 0; i < (int) aOutline->GetCount(); i++ )
94  m_outline.Add( new wxRealPoint( (*aOutline)[i]->x, (*aOutline)[i]->y ) );
95 
96  if( m_outline.Count() > 0 )
97  {
98  m_positionX = m_outline[0]->x;
99  m_positionY = m_outline[0]->y;
100  }
101 }
102 
103 void PCB_POLYGON::FormPolygon( XNODE* aNode, VERTICES_ARRAY* aPolygon,
104  const wxString& aDefaultMeasurementUnit,
105  const wxString& aActualConversion )
106 {
107  XNODE* lNode;
108  double x, y;
109 
110  lNode = FindNode( aNode, wxT( "pt" ) );
111 
112  while( lNode )
113  {
114  if( lNode->GetName() == wxT( "pt" ) )
115  {
117  lNode->GetNodeContent(), aDefaultMeasurementUnit, &x, &y, aActualConversion );
118  aPolygon->Add( new wxRealPoint( x, y ) );
119  }
120 
121  lNode = lNode->GetNext();
122  }
123 }
124 
125 
127  const wxString& aDefaultMeasurementUnit,
128  const wxString& aActualConversion )
129 {
130  XNODE* lNode;
131  wxString propValue;
132 
133  lNode = FindNode( aNode, wxT( "netNameRef" ) );
134 
135  if( lNode )
136  {
137  lNode->GetAttribute( wxT( "Name" ), &propValue );
138  propValue.Trim( false );
139  propValue.Trim( true );
140  m_net = propValue;
142  }
143 
144  // retrieve polygon outline
145  FormPolygon( aNode, &m_outline, aDefaultMeasurementUnit, aActualConversion );
146 
147  m_positionX = m_outline[0]->x;
148  m_positionY = m_outline[0]->y;
149 
150  // fill the polygon with the same contour as its outline is
151  m_islands.Add( new VERTICES_ARRAY );
152  FormPolygon( aNode, m_islands[0], aDefaultMeasurementUnit, aActualConversion );
153 
154  return true;
155 }
156 
157 
159 {
161  {
162  EDGE_MODULE* dwg = new EDGE_MODULE( aModule, S_POLYGON );
163  aModule->Add( dwg );
164 
165  dwg->SetWidth( 0 );
166  dwg->SetLayer( m_KiCadLayer );
167 
168  auto outline = new std::vector<wxPoint>;
169  for( auto point : m_outline )
170  outline->push_back( wxPoint( point->x, point->y ) );
171 
172  dwg->SetPolyPoints( *outline );
173  dwg->SetStart0( *outline->begin() );
174  dwg->SetEnd0( outline->back() );
175  dwg->SetDrawCoord();
176 
177  delete( outline );
178  }
179 }
180 
181 
183 {
184  int i = 0;
185 
186  if( m_outline.GetCount() > 0 )
187  {
188  ZONE_CONTAINER* zone = new ZONE_CONTAINER( m_board );
189  m_board->Add( zone, ADD_MODE::APPEND );
190 
191  zone->SetLayer( m_KiCadLayer );
192  zone->SetNetCode( m_netCode );
193 
194  // add outline
195  for( i = 0; i < (int) m_outline.GetCount(); i++ )
196  {
197  zone->AppendCorner( wxPoint( KiROUND( m_outline[i]->x ),
198  KiROUND( m_outline[i]->y ) ), -1 );
199  }
200 
201  zone->SetZoneClearance( m_width );
202 
203  zone->SetPriority( m_priority );
204 
206  zone->GetDefaultHatchPitch(), true );
207 
208  if ( m_objType == wxT( 'K' ) )
209  {
210  zone->SetIsKeepout( true );
211  zone->SetDoNotAllowTracks( true );
212  zone->SetDoNotAllowVias( true );
213  zone->SetDoNotAllowPads( true );
214  zone->SetDoNotAllowCopperPour( true );
215  zone->SetDoNotAllowFootprints( false );
216  }
217  else if( m_objType == wxT( 'C' ) )
218  {
219  // convert cutouts to keepouts because standalone cutouts are not supported in KiCad
220  zone->SetIsKeepout( true );
221  zone->SetDoNotAllowCopperPour( true );
222  zone->SetDoNotAllowTracks( false );
223  zone->SetDoNotAllowVias( false );
224  zone->SetDoNotAllowPads( false );
225  zone->SetDoNotAllowFootprints( false );
226  }
227 
228  //if( m_filled )
229  // zone->BuildFilledPolysListData( m_board );
230  }
231 }
232 
233 
235 {
237 
239 }
240 
241 
242 void PCB_POLYGON::SetPosOffset( int aX_offs, int aY_offs )
243 {
244  int i, island;
245 
246  PCB_COMPONENT::SetPosOffset( aX_offs, aY_offs );
247 
248  for( i = 0; i < (int) m_outline.GetCount(); i++ )
249  {
250  m_outline[i]->x += aX_offs;
251  m_outline[i]->y += aY_offs;
252  }
253 
254  for( island = 0; island < (int) m_islands.GetCount(); island++ )
255  {
256  for( i = 0; i < (int) m_islands[island]->GetCount(); i++ )
257  {
258  (*m_islands[island])[i]->x += aX_offs;
259  (*m_islands[island])[i]->y += aY_offs;
260  }
261  }
262 
263  for( island = 0; island < (int) m_cutouts.GetCount(); island++ )
264  {
265  for( i = 0; i < (int) m_cutouts[island]->GetCount(); i++ )
266  {
267  (*m_cutouts[island])[i]->x += aX_offs;
268  (*m_cutouts[island])[i]->y += aY_offs;
269  }
270  }
271 }
272 
273 } // namespace PCAD2KICAD
void SetDoNotAllowTracks(bool aEnable)
Definition: class_zone.h:720
void SetEnd0(const wxPoint &aPoint)
ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:61
virtual void SetLayer(PCB_LAYER_ID aLayer)
Function SetLayer sets the layer this item is on.
void SetDoNotAllowFootprints(bool aEnable)
Definition: class_zone.h:722
polygon (not yet used for tracks, but could be in microwave apps)
PCB_LAYER_ID FlipLayer(PCB_LAYER_ID aLayerId, int aCopperLayersCount)
Function FlippedLayerNumber.
Definition: lset.cpp:487
void SetOutline(VERTICES_ARRAY *aOutline)
Definition: pcb_polygon.cpp:87
bool SetNetCode(int aNetCode, bool aNoAssert)
Sets net using a net code.
void AddToBoard() override
ISLANDS_ARRAY m_cutouts
Definition: pcb_polygon.h:49
virtual bool Parse(XNODE *aNode, const wxString &aDefaultMeasurementUnit, const wxString &aActualConversion)
PCB_LAYER_ID GetKiCadLayer()
Definition: pcb_component.h:73
static int GetDefaultHatchPitch()
Function GetDefaultHatchPitchMils.
virtual void Flip() override
bool IsNonCopperLayer(LAYER_NUM aLayerId)
Function IsNonCopperLayer tests whether a layer is a non copper layer.
void SetDoNotAllowVias(bool aEnable)
Definition: class_zone.h:719
void SetPriority(unsigned aPriority)
Function SetPriority.
Definition: class_zone.h:100
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.
VERTICES_ARRAY m_outline
Definition: pcb_polygon.h:47
virtual void SetPosOffset(int aX_offs, int aY_offs) override
virtual void SetLayer(PCB_LAYER_ID aLayer) override
Function SetLayer sets the layer this item is on.
Definition: class_zone.cpp:232
void SetZoneClearance(int aZoneClearance)
Definition: class_zone.h:196
void SetDoNotAllowPads(bool aEnable)
Definition: class_zone.h:721
void SetBorderDisplayStyle(ZONE_BORDER_DISPLAY_STYLE aHatchStyle, int aHatchPitch, bool aRebuildHatch)
Function SetBorderDisplayStyle sets all hatch parameters for the zone.
Definition: class_zone.cpp:930
void SetDoublePrecisionPosition(wxString aStr, const wxString &aDefaultMeasurementUnit, double *aX, double *aY, const wxString &aActualConversion)
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT) override
void SetPolyPoints(const std::vector< wxPoint > &aPoints)
XNODE holds an XML or S-expression element.
Definition: xnode.h:43
void SetDoNotAllowCopperPour(bool aEnable)
Definition: class_zone.h:718
XNODE * GetNext() const
Definition: xnode.h:68
ISLANDS_ARRAY m_islands
Definition: pcb_polygon.h:48
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:184
void FormPolygon(XNODE *aNode, VERTICES_ARRAY *aPolygon, const wxString &aDefaultMeasurementUnit, const wxString &actualConversion)
void SetDrawCoord()
Set draw coordinates (absolute values ) from relative coordinates.
void SetStart0(const wxPoint &aPoint)
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:68
void AddToModule(MODULE *aModule) override
The common library.
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: class_zone.cpp:873
int GetNetCode(wxString aNetName)
Definition: pcb_component.h:74
void SetIsKeepout(bool aEnable)
Definition: class_zone.h:717
void AssignNet(const wxString &aNetName)
Definition: pcb_polygon.cpp:81
PCB_POLYGON(PCB_CALLBACKS *aCallbacks, BOARD *aBoard, int aPCadLayer)
Definition: pcb_polygon.cpp:39
void SetWidth(int aWidth)