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 
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;
48  m_filled = true;
49 }
50 
51 
53 {
54  int i, island;
55 
56  for( i = 0; i < (int) m_outline.GetCount(); i++ )
57  {
58  delete m_outline[i];
59  }
60 
61  for( island = 0; island < (int) m_cutouts.GetCount(); island++ )
62  {
63  for( i = 0; i < (int) m_cutouts[island]->GetCount(); i++ )
64  {
65  delete (*m_cutouts[island])[i];
66  }
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  {
75  delete (*m_islands[island])[i];
76  }
77 
78  delete m_islands[island];
79  }
80 }
81 
82 void PCB_POLYGON::AssignNet( const wxString& aNetName )
83 {
84  m_net = aNetName;
86 }
87 
88 void PCB_POLYGON::SetOutline( VERTICES_ARRAY* aOutline )
89 {
90  int i;
91 
92  m_outline.Empty();
93 
94  for( i = 0; i < (int) aOutline->GetCount(); i++ )
95  m_outline.Add( new wxRealPoint( (*aOutline)[i]->x, (*aOutline)[i]->y ) );
96 
97  if( m_outline.Count() > 0 )
98  {
99  m_positionX = m_outline[0]->x;
100  m_positionY = m_outline[0]->y;
101  }
102 }
103 
104 void PCB_POLYGON::FormPolygon( XNODE* aNode, VERTICES_ARRAY* aPolygon,
105  const wxString& aDefaultMeasurementUnit,
106  const wxString& aActualConversion )
107 {
108  XNODE* lNode;
109  double x, y;
110 
111  lNode = FindNode( aNode, wxT( "pt" ) );
112 
113  while( lNode )
114  {
115  if( lNode->GetName() == wxT( "pt" ) )
116  {
118  lNode->GetNodeContent(), aDefaultMeasurementUnit, &x, &y, aActualConversion );
119  aPolygon->Add( new wxRealPoint( x, y ) );
120  }
121 
122  lNode = lNode->GetNext();
123  }
124 }
125 
126 
128  const wxString& aDefaultMeasurementUnit,
129  const wxString& aActualConversion )
130 {
131  XNODE* lNode;
132  wxString propValue;
133 
134  lNode = FindNode( aNode, wxT( "netNameRef" ) );
135 
136  if( lNode )
137  {
138  lNode->GetAttribute( wxT( "Name" ), &propValue );
139  propValue.Trim( false );
140  propValue.Trim( true );
141  m_net = propValue;
143  }
144 
145  // retrieve polygon outline
146  FormPolygon( aNode, &m_outline, aDefaultMeasurementUnit, aActualConversion );
147 
148  m_positionX = m_outline[0]->x;
149  m_positionY = m_outline[0]->y;
150 
151  // fill the polygon with the same contour as its outline is
152  m_islands.Add( new VERTICES_ARRAY );
153  FormPolygon( aNode, m_islands[0], aDefaultMeasurementUnit, aActualConversion );
154 
155  return true;
156 }
157 
158 
160 {
162  {
163  EDGE_MODULE* dwg = new EDGE_MODULE( aModule, S_POLYGON );
164  aModule->GraphicalItemsList().PushBack( dwg );
165 
166  dwg->SetWidth( 0 );
167  dwg->SetLayer( m_KiCadLayer );
168 
169  auto outline = new std::vector<wxPoint>;
170  for( auto point : m_outline )
171  outline->push_back( wxPoint( point->x, point->y ) );
172 
173  dwg->SetPolyPoints( *outline );
174  dwg->SetStart0( *outline->begin() );
175  dwg->SetEnd0( outline->back() );
176  dwg->SetDrawCoord();
177 
178  delete( outline );
179  }
180 }
181 
182 
184 {
185  int i = 0;
186 
187  if( m_outline.GetCount() > 0 )
188  {
189  ZONE_CONTAINER* zone = new ZONE_CONTAINER( m_board );
190  m_board->Add( zone, ADD_APPEND );
191 
192  zone->SetTimeStamp( m_timestamp );
193  zone->SetLayer( m_KiCadLayer );
194  zone->SetNetCode( m_netCode );
195 
196  // add outline
197  int outline_hatch = ZONE_CONTAINER::DIAGONAL_EDGE;
198 
199  for( i = 0; i < (int) m_outline.GetCount(); i++ )
200  {
201  zone->AppendCorner( wxPoint( KiROUND( m_outline[i]->x ),
202  KiROUND( m_outline[i]->y ) ), -1 );
203  }
204 
205  zone->SetZoneClearance( m_width );
206 
207  zone->SetPriority( m_priority );
208 
209  zone->SetHatch( outline_hatch, zone->GetDefaultHatchPitch(), true );
210 
211  if ( m_objType == wxT( 'K' ) )
212  {
213  zone->SetIsKeepout( true );
214  zone->SetDoNotAllowTracks( true );
215  zone->SetDoNotAllowVias( true );
216  zone->SetDoNotAllowCopperPour( true );
217  }
218  else if( m_objType == wxT( 'C' ) )
219  {
220  // convert cutouts to keepouts because standalone cutouts are not supported in KiCad
221  zone->SetIsKeepout( true );
222  zone->SetDoNotAllowCopperPour( true );
223  }
224 
225  //if( m_filled )
226  // zone->BuildFilledPolysListData( m_board );
227  }
228 }
229 
230 
232 {
234 
236 }
237 
238 
239 void PCB_POLYGON::SetPosOffset( int aX_offs, int aY_offs )
240 {
241  int i, island;
242 
243  PCB_COMPONENT::SetPosOffset( aX_offs, aY_offs );
244 
245  for( i = 0; i < (int) m_outline.GetCount(); i++ )
246  {
247  m_outline[i]->x += aX_offs;
248  m_outline[i]->y += aY_offs;
249  }
250 
251  for( island = 0; island < (int) m_islands.GetCount(); island++ )
252  {
253  for( i = 0; i < (int) m_islands[island]->GetCount(); i++ )
254  {
255  (*m_islands[island])[i]->x += aX_offs;
256  (*m_islands[island])[i]->y += aY_offs;
257  }
258  }
259 
260  for( island = 0; island < (int) m_cutouts.GetCount(); island++ )
261  {
262  for( i = 0; i < (int) m_cutouts[island]->GetCount(); i++ )
263  {
264  (*m_cutouts[island])[i]->x += aX_offs;
265  (*m_cutouts[island])[i]->y += aY_offs;
266  }
267  }
268 }
269 
270 } // namespace PCAD2KICAD
void SetDoNotAllowTracks(bool aEnable)
Definition: class_zone.h:610
void SetEnd0(const wxPoint &aPoint)
Class ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:60
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:107
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:472
void SetOutline(VERTICES_ARRAY *aOutline)
Definition: pcb_polygon.cpp:88
void AddToBoard() override
void SetHatch(int aHatchStyle, int aHatchPitch, bool aRebuildHatch)
Function SetHatch sets all hatch parameters for the zone.
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:609
void PushBack(T *aNewElement)
Function PushBack puts aNewElement at the end of the list sequence.
Definition: dlist.h:250
void SetPriority(unsigned aPriority)
Function SetPriority.
Definition: class_zone.h:95
virtual void SetPosOffset(int aX_offs, int aY_offs)
VERTICES_ARRAY m_outline
Definition: pcb_polygon.h:47
virtual void SetPosOffset(int aX_offs, int aY_offs) override
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_INSERT) override
Adds an item to the container.
virtual void SetLayer(PCB_LAYER_ID aLayer) override
Function SetLayer sets the layer this item is on.
Definition: class_zone.cpp:199
bool SetNetCode(int aNetCode, bool aNoAssert=false)
Function SetNetCode sets net using a net code.
void SetZoneClearance(int aZoneClearance)
Definition: class_zone.h:193
void SetDoublePrecisionPosition(wxString aStr, const wxString &aDefaultMeasurementUnit, double *aX, double *aY, const wxString &aActualConversion)
void SetPolyPoints(const std::vector< wxPoint > &aPoints)
Class XNODE holds an XML or S-expression element.
Definition: xnode.h:43
void SetDoNotAllowCopperPour(bool aEnable)
Definition: class_zone.h:608
ISLANDS_ARRAY m_islands
Definition: pcb_polygon.h:48
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:169
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)
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)
DLIST< BOARD_ITEM > & GraphicalItemsList()
Definition: class_module.h:157
int GetNetCode(wxString aNetName)
Definition: pcb_component.h:75
void SetIsKeepout(bool aEnable)
Definition: class_zone.h:607
XNODE * GetNext() const
Definition: xnode.h:68
void AssignNet(const wxString &aNetName)
Definition: pcb_polygon.cpp:82
void SetTimeStamp(timestamp_t aNewTimeStamp)
Definition: base_struct.h:230
PCB_POLYGON(PCB_CALLBACKS *aCallbacks, BOARD *aBoard, int aPCadLayer)
Definition: pcb_polygon.cpp:39
void SetWidth(int aWidth)