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 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 #include <wx/config.h>
32 
33 #include <common.h>
34 #include <math/util.h> // for KiROUND
35 
36 #include <pcb_polygon.h>
37 
38 namespace PCAD2KICAD {
39 
40 PCB_POLYGON::PCB_POLYGON( PCB_CALLBACKS* aCallbacks, BOARD* aBoard, int aPCadLayer ) :
41  PCB_COMPONENT( aCallbacks, aBoard )
42 {
43  m_width = 0;
44  m_priority = 0;
45  m_objType = wxT( 'Z' );
46  m_PCadLayer = aPCadLayer;
49  m_filled = true;
50 }
51 
52 
54 {
55  int i, island;
56 
57  for( i = 0; i < (int) m_outline.GetCount(); i++ )
58  {
59  delete m_outline[i];
60  }
61 
62  for( island = 0; island < (int) m_cutouts.GetCount(); island++ )
63  {
64  for( i = 0; i < (int) m_cutouts[island]->GetCount(); i++ )
65  {
66  delete (*m_cutouts[island])[i];
67  }
68 
69  delete m_cutouts[island];
70  }
71 
72  for( island = 0; island < (int) m_islands.GetCount(); island++ )
73  {
74  for( i = 0; i < (int) m_islands[island]->GetCount(); i++ )
75  {
76  delete (*m_islands[island])[i];
77  }
78 
79  delete m_islands[island];
80  }
81 }
82 
83 void PCB_POLYGON::AssignNet( const wxString& aNetName )
84 {
85  m_net = aNetName;
87 }
88 
89 void PCB_POLYGON::SetOutline( VERTICES_ARRAY* aOutline )
90 {
91  int i;
92 
93  m_outline.Empty();
94 
95  for( i = 0; i < (int) aOutline->GetCount(); i++ )
96  m_outline.Add( new wxRealPoint( (*aOutline)[i]->x, (*aOutline)[i]->y ) );
97 
98  if( m_outline.Count() > 0 )
99  {
100  m_positionX = m_outline[0]->x;
101  m_positionY = m_outline[0]->y;
102  }
103 }
104 
105 void PCB_POLYGON::FormPolygon( XNODE* aNode, VERTICES_ARRAY* aPolygon,
106  const wxString& aDefaultMeasurementUnit,
107  const wxString& aActualConversion )
108 {
109  XNODE* lNode;
110  double x, y;
111 
112  lNode = FindNode( aNode, wxT( "pt" ) );
113 
114  while( lNode )
115  {
116  if( lNode->GetName() == wxT( "pt" ) )
117  {
119  lNode->GetNodeContent(), aDefaultMeasurementUnit, &x, &y, aActualConversion );
120  aPolygon->Add( new wxRealPoint( x, y ) );
121  }
122 
123  lNode = lNode->GetNext();
124  }
125 }
126 
127 
129  const wxString& aDefaultMeasurementUnit,
130  const wxString& aActualConversion )
131 {
132  XNODE* lNode;
133  wxString propValue;
134 
135  lNode = FindNode( aNode, wxT( "netNameRef" ) );
136 
137  if( lNode )
138  {
139  lNode->GetAttribute( wxT( "Name" ), &propValue );
140  propValue.Trim( false );
141  propValue.Trim( true );
142  m_net = propValue;
144  }
145 
146  // retrieve polygon outline
147  FormPolygon( aNode, &m_outline, aDefaultMeasurementUnit, aActualConversion );
148 
149  m_positionX = m_outline[0]->x;
150  m_positionY = m_outline[0]->y;
151 
152  // fill the polygon with the same contour as its outline is
153  m_islands.Add( new VERTICES_ARRAY );
154  FormPolygon( aNode, m_islands[0], aDefaultMeasurementUnit, aActualConversion );
155 
156  return true;
157 }
158 
159 
161 {
163  {
164  EDGE_MODULE* dwg = new EDGE_MODULE( aModule, S_POLYGON );
165  aModule->Add( dwg );
166 
167  dwg->SetWidth( 0 );
168  dwg->SetLayer( m_KiCadLayer );
169 
170  auto outline = new std::vector<wxPoint>;
171  for( auto point : m_outline )
172  outline->push_back( wxPoint( point->x, point->y ) );
173 
174  dwg->SetPolyPoints( *outline );
175  dwg->SetStart0( *outline->begin() );
176  dwg->SetEnd0( outline->back() );
177  dwg->SetDrawCoord();
178 
179  delete( outline );
180  }
181 }
182 
183 
185 {
186  int i = 0;
187 
188  if( m_outline.GetCount() > 0 )
189  {
190  ZONE_CONTAINER* zone = new ZONE_CONTAINER( m_board );
191  m_board->Add( zone, ADD_MODE::APPEND );
192 
193  zone->SetTimeStamp( m_timestamp );
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( wxPoint( KiROUND( m_outline[i]->x ),
201  KiROUND( m_outline[i]->y ) ), -1 );
202  }
203 
204  zone->SetZoneClearance( m_width );
205 
206  zone->SetPriority( m_priority );
207 
209 
210  if ( m_objType == wxT( 'K' ) )
211  {
212  zone->SetIsKeepout( true );
213  zone->SetDoNotAllowTracks( true );
214  zone->SetDoNotAllowVias( true );
215  zone->SetDoNotAllowCopperPour( true );
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  }
223 
224  //if( m_filled )
225  // zone->BuildFilledPolysListData( m_board );
226  }
227 }
228 
229 
231 {
233 
235 }
236 
237 
238 void PCB_POLYGON::SetPosOffset( int aX_offs, int aY_offs )
239 {
240  int i, island;
241 
242  PCB_COMPONENT::SetPosOffset( aX_offs, aY_offs );
243 
244  for( i = 0; i < (int) m_outline.GetCount(); i++ )
245  {
246  m_outline[i]->x += aX_offs;
247  m_outline[i]->y += aY_offs;
248  }
249 
250  for( island = 0; island < (int) m_islands.GetCount(); island++ )
251  {
252  for( i = 0; i < (int) m_islands[island]->GetCount(); i++ )
253  {
254  (*m_islands[island])[i]->x += aX_offs;
255  (*m_islands[island])[i]->y += aY_offs;
256  }
257  }
258 
259  for( island = 0; island < (int) m_cutouts.GetCount(); island++ )
260  {
261  for( i = 0; i < (int) m_cutouts[island]->GetCount(); i++ )
262  {
263  (*m_cutouts[island])[i]->x += aX_offs;
264  (*m_cutouts[island])[i]->y += aY_offs;
265  }
266  }
267 }
268 
269 } // namespace PCAD2KICAD
void SetDoNotAllowTracks(bool aEnable)
Definition: class_zone.h:639
void SetEnd0(const wxPoint &aPoint)
ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:60
void SetHatch(ZONE_HATCH_STYLE aHatchStyle, int aHatchPitch, bool aRebuildHatch)
Function SetHatch sets all hatch parameters for the zone.
Definition: class_zone.cpp:958
virtual void SetLayer(PCB_LAYER_ID aLayer)
Function SetLayer sets the layer this item is on.
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:485
void SetOutline(VERTICES_ARRAY *aOutline)
Definition: pcb_polygon.cpp:89
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:638
void SetPriority(unsigned aPriority)
Function SetPriority.
Definition: class_zone.h:94
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:231
bool SetNetCode(int aNetCode, bool aNoAssert=false)
Sets net using a net code.
void SetZoneClearance(int aZoneClearance)
Definition: class_zone.h:177
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:637
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:160
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:61
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:915
int GetNetCode(wxString aNetName)
Definition: pcb_component.h:75
void SetIsKeepout(bool aEnable)
Definition: class_zone.h:636
void AssignNet(const wxString &aNetName)
Definition: pcb_polygon.cpp:83
void SetTimeStamp(timestamp_t aNewTimeStamp)
Definition: base_struct.h:212
PCB_POLYGON(PCB_CALLBACKS *aCallbacks, BOARD *aBoard, int aPCadLayer)
Definition: pcb_polygon.cpp:40
void SetWidth(int aWidth)