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( 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  wxString aDefaultMeasurementUnit, 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  wxString aDefaultMeasurementUnit,
128  wxString aActualConversion,
129  wxStatusBar* aStatusBar )
130 {
131  XNODE* lNode;
132  wxString propValue;
133 
134  // aStatusBar->SetStatusText( aStatusBar->GetStatusText() + wxT( " Polygon..." ) );
135 
136  lNode = FindNode( aNode, wxT( "netNameRef" ) );
137 
138  if( lNode )
139  {
140  lNode->GetAttribute( wxT( "Name" ), &propValue );
141  propValue.Trim( false );
142  propValue.Trim( true );
143  m_net = propValue;
145  }
146 
147  // retrieve polygon outline
148  FormPolygon( aNode, &m_outline, aDefaultMeasurementUnit, aActualConversion );
149 
150  m_positionX = m_outline[0]->x;
151  m_positionY = m_outline[0]->y;
152 
153  // fill the polygon with the same contour as its outline is
154  m_islands.Add( new VERTICES_ARRAY );
155  FormPolygon( aNode, m_islands[0], aDefaultMeasurementUnit, aActualConversion );
156 
157  return true;
158 }
159 
160 
162 {
163 }
164 
165 
167 {
168  int i = 0;
169 
170  if( m_outline.GetCount() > 0 )
171  {
172  ZONE_CONTAINER* zone = new ZONE_CONTAINER( m_board );
173  m_board->Add( zone, ADD_APPEND );
174 
175  zone->SetTimeStamp( m_timestamp );
176  zone->SetLayer( m_KiCadLayer );
177  zone->SetNetCode( m_netCode );
178 
179  // add outline
180  int outline_hatch = ZONE_CONTAINER::DIAGONAL_EDGE;
181 
182  for( i = 0; i < (int) m_outline.GetCount(); i++ )
183  {
184  zone->AppendCorner( wxPoint( KiROUND( m_outline[i]->x ),
185  KiROUND( m_outline[i]->y ) ), -1 );
186  }
187 
188  zone->SetZoneClearance( m_width );
189 
190  zone->SetPriority( m_priority );
191 
192  zone->SetHatch( outline_hatch, Mils2iu( zone->GetDefaultHatchPitchMils() ), true );
193 
194  if ( m_objType == wxT( 'K' ) )
195  {
196  zone->SetIsKeepout( true );
197  zone->SetDoNotAllowTracks( true );
198  zone->SetDoNotAllowVias( true );
199  zone->SetDoNotAllowCopperPour( true );
200  }
201  else if( m_objType == wxT( 'C' ) )
202  {
203  // convert cutouts to keepouts because standalone cutouts are not supported in KiCad
204  zone->SetIsKeepout( true );
205  zone->SetDoNotAllowCopperPour( true );
206  }
207 
208  //if( m_filled )
209  // zone->BuildFilledPolysListData( m_board );
210  }
211 }
212 
213 
214 void PCB_POLYGON::SetPosOffset( int aX_offs, int aY_offs )
215 {
216  int i, island;
217 
218  PCB_COMPONENT::SetPosOffset( aX_offs, aY_offs );
219 
220  for( i = 0; i < (int) m_outline.GetCount(); i++ )
221  {
222  m_outline[i]->x += aX_offs;
223  m_outline[i]->y += aY_offs;
224  }
225 
226  for( island = 0; island < (int) m_islands.GetCount(); island++ )
227  {
228  for( i = 0; i < (int) m_islands[island]->GetCount(); i++ )
229  {
230  (*m_islands[island])[i]->x += aX_offs;
231  (*m_islands[island])[i]->y += aY_offs;
232  }
233  }
234 
235  for( island = 0; island < (int) m_cutouts.GetCount(); island++ )
236  {
237  for( i = 0; i < (int) m_cutouts[island]->GetCount(); i++ )
238  {
239  (*m_cutouts[island])[i]->x += aX_offs;
240  (*m_cutouts[island])[i]->y += aY_offs;
241  }
242  }
243 }
244 
245 } // namespace PCAD2KICAD
void SetDoNotAllowTracks(bool aEnable)
Definition: class_zone.h:662
Class ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:78
void FormPolygon(XNODE *aNode, VERTICES_ARRAY *aPolygon, wxString aDefaultMeasurementUnit, wxString actualConversion)
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.
void SetOutline(VERTICES_ARRAY *aOutline)
Definition: pcb_polygon.cpp:88
void AddToBoard() override
virtual bool Parse(XNODE *aNode, wxString aDefaultMeasurementUnit, wxString aActualConversion, wxStatusBar *aStatusBar)
void SetHatch(int aHatchStyle, int aHatchPitch, bool aRebuildHatch)
Function SetHatch sets all hatch parameters for the zone.
Definition: class_zone.cpp:920
ISLANDS_ARRAY m_cutouts
Definition: pcb_polygon.h:49
PCB_LAYER_ID GetKiCadLayer()
Definition: pcb_component.h:73
void SetTimeStamp(time_t aNewTimeStamp)
Definition: base_struct.h:203
void SetDoNotAllowVias(bool aEnable)
Definition: class_zone.h:661
void SetPriority(unsigned aPriority)
Function SetPriority.
Definition: class_zone.h:113
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.
bool SetNetCode(int aNetCode, bool aNoAssert=false)
Function SetNetCode sets net using a net code.
void SetZoneClearance(int aZoneClearance)
Definition: class_zone.h:204
Class XNODE holds an XML or S-expression element.
Definition: xnode.h:43
void SetDoNotAllowCopperPour(bool aEnable)
Definition: class_zone.h:660
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 AddToModule(MODULE *aModule) override
The common library.
XNODE * FindNode(XNODE *aChild, wxString aTag)
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:846
int GetNetCode(wxString aNetName)
Definition: pcb_component.h:75
static int GetDefaultHatchPitchMils()
Function GetDefaultHatchPitchMils.
Definition: class_zone.h:680
void AssignNet(wxString aNetName)
Definition: pcb_polygon.cpp:82
void SetIsKeepout(bool aEnable)
Definition: class_zone.h:659
XNODE * GetNext() const
Definition: xnode.h:68
void SetDoublePrecisionPosition(wxString aStr, wxString aDefaultMeasurementUnit, double *aX, double *aY, wxString aActualConversion)
PCB_POLYGON(PCB_CALLBACKS *aCallbacks, BOARD *aBoard, int aPCadLayer)
Definition: pcb_polygon.cpp:39