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 #include <fp_shape.h>
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 
44  // P-CAD polygons are similar to zones (and we're going to convert them as such), except
45  // that they don't avoid other copper pours. So effectively they're very-high-priority
46  // zones.
47  m_priority = 100000;
48 
49  m_objType = wxT( 'Z' );
50  m_PCadLayer = aPCadLayer;
52  m_filled = true;
53 }
54 
55 
57 {
58  int i, island;
59 
60  for( i = 0; i < (int) m_outline.GetCount(); i++ )
61  {
62  delete m_outline[i];
63  }
64 
65  for( island = 0; island < (int) m_cutouts.GetCount(); island++ )
66  {
67  for( i = 0; i < (int) m_cutouts[island]->GetCount(); i++ )
68  {
69  delete (*m_cutouts[island])[i];
70  }
71 
72  delete m_cutouts[island];
73  }
74 
75  for( island = 0; island < (int) m_islands.GetCount(); island++ )
76  {
77  for( i = 0; i < (int) m_islands[island]->GetCount(); i++ )
78  {
79  delete (*m_islands[island])[i];
80  }
81 
82  delete m_islands[island];
83  }
84 }
85 
86 void PCB_POLYGON::AssignNet( const wxString& aNetName )
87 {
88  m_net = aNetName;
90 }
91 
92 void PCB_POLYGON::SetOutline( VERTICES_ARRAY* aOutline )
93 {
94  int i;
95 
96  m_outline.Empty();
97 
98  for( i = 0; i < (int) aOutline->GetCount(); i++ )
99  m_outline.Add( new wxRealPoint( (*aOutline)[i]->x, (*aOutline)[i]->y ) );
100 
101  if( m_outline.Count() > 0 )
102  {
103  m_positionX = m_outline[0]->x;
104  m_positionY = m_outline[0]->y;
105  }
106 }
107 
108 void PCB_POLYGON::FormPolygon( XNODE* aNode, VERTICES_ARRAY* aPolygon,
109  const wxString& aDefaultMeasurementUnit,
110  const wxString& aActualConversion )
111 {
112  XNODE* lNode;
113  double x, y;
114 
115  lNode = FindNode( aNode, wxT( "pt" ) );
116 
117  while( lNode )
118  {
119  if( lNode->GetName() == wxT( "pt" ) )
120  {
122  lNode->GetNodeContent(), aDefaultMeasurementUnit, &x, &y, aActualConversion );
123  aPolygon->Add( new wxRealPoint( x, y ) );
124  }
125 
126  lNode = lNode->GetNext();
127  }
128 }
129 
130 
132  const wxString& aDefaultMeasurementUnit,
133  const wxString& aActualConversion )
134 {
135  XNODE* lNode;
136  wxString propValue;
137 
138  lNode = FindNode( aNode, wxT( "netNameRef" ) );
139 
140  if( lNode )
141  {
142  lNode->GetAttribute( wxT( "Name" ), &propValue );
143  propValue.Trim( false );
144  propValue.Trim( true );
145  m_net = propValue;
147  }
148 
149  // retrieve polygon outline
150  FormPolygon( aNode, &m_outline, aDefaultMeasurementUnit, aActualConversion );
151 
152  m_positionX = m_outline[0]->x;
153  m_positionY = m_outline[0]->y;
154 
155  // fill the polygon with the same contour as its outline is
156  m_islands.Add( new VERTICES_ARRAY );
157  FormPolygon( aNode, m_islands[0], aDefaultMeasurementUnit, aActualConversion );
158 
159  return true;
160 }
161 
162 
164 {
166  {
167  FP_SHAPE* dwg = new FP_SHAPE( aFootprint, S_POLYGON );
168  aFootprint->Add( dwg );
169 
170  dwg->SetWidth( 0 );
171  dwg->SetLayer( m_KiCadLayer );
172 
173  auto outline = new std::vector<wxPoint>;
174  for( auto point : m_outline )
175  outline->push_back( wxPoint( point->x, point->y ) );
176 
177  dwg->SetPolyPoints( *outline );
178  dwg->SetStart0( *outline->begin() );
179  dwg->SetEnd0( outline->back() );
180  dwg->SetDrawCoord();
181 
182  delete( outline );
183  }
184 }
185 
186 
188 {
189  int i = 0;
190 
191  if( m_outline.GetCount() > 0 )
192  {
193  ZONE* zone = new ZONE( m_board );
194  m_board->Add( zone, ADD_MODE::APPEND );
195 
196  zone->SetLayer( m_KiCadLayer );
197  zone->SetNetCode( m_netCode );
198 
199  // add outline
200  for( i = 0; i < (int) m_outline.GetCount(); i++ )
201  {
202  zone->AppendCorner( wxPoint( KiROUND( m_outline[i]->x ),
203  KiROUND( m_outline[i]->y ) ), -1 );
204  }
205 
206  zone->SetLocalClearance( m_width );
207 
208  zone->SetPriority( m_priority );
209 
211  zone->GetDefaultHatchPitch(), true );
212 
213  if ( m_objType == wxT( 'K' ) )
214  {
215  zone->SetIsRuleArea( true );
216  zone->SetDoNotAllowTracks( true );
217  zone->SetDoNotAllowVias( true );
218  zone->SetDoNotAllowPads( true );
219  zone->SetDoNotAllowCopperPour( true );
220  zone->SetDoNotAllowFootprints( false );
221  }
222  else if( m_objType == wxT( 'C' ) )
223  {
224  // convert cutouts to keepouts because standalone cutouts are not supported in KiCad
225  zone->SetIsRuleArea( true );
226  zone->SetDoNotAllowCopperPour( true );
227  zone->SetDoNotAllowTracks( false );
228  zone->SetDoNotAllowVias( false );
229  zone->SetDoNotAllowPads( false );
230  zone->SetDoNotAllowFootprints( false );
231  }
232 
233  //if( m_filled )
234  // zone->BuildFilledPolysListData( m_board );
235  }
236 }
237 
238 
240 {
242 
244 }
245 
246 
247 void PCB_POLYGON::SetPosOffset( int aX_offs, int aY_offs )
248 {
249  int i, island;
250 
251  PCB_COMPONENT::SetPosOffset( aX_offs, aY_offs );
252 
253  for( i = 0; i < (int) m_outline.GetCount(); i++ )
254  {
255  m_outline[i]->x += aX_offs;
256  m_outline[i]->y += aY_offs;
257  }
258 
259  for( island = 0; island < (int) m_islands.GetCount(); island++ )
260  {
261  for( i = 0; i < (int) m_islands[island]->GetCount(); i++ )
262  {
263  (*m_islands[island])[i]->x += aX_offs;
264  (*m_islands[island])[i]->y += aY_offs;
265  }
266  }
267 
268  for( island = 0; island < (int) m_cutouts.GetCount(); island++ )
269  {
270  for( i = 0; i < (int) m_cutouts[island]->GetCount(); i++ )
271  {
272  (*m_cutouts[island])[i]->x += aX_offs;
273  (*m_cutouts[island])[i]->y += aY_offs;
274  }
275  }
276 }
277 
278 } // namespace PCAD2KICAD
void SetEnd0(const wxPoint &aPoint)
Definition: fp_shape.h:114
void SetDoNotAllowTracks(bool aEnable)
Definition: zone.h:765
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:194
void SetBorderDisplayStyle(ZONE_BORDER_DISPLAY_STYLE aHatchStyle, int aHatchPitch, bool aRebuildHatch)
Function SetBorderDisplayStyle sets all hatch parameters for the zone.
Definition: zone.cpp:892
PCB_LAYER_ID FlipLayer(PCB_LAYER_ID aLayerId, int aCopperLayersCount)
Definition: lset.cpp:521
void SetOutline(VERTICES_ARRAY *aOutline)
Definition: pcb_polygon.cpp:92
void AddToFootprint(FOOTPRINT *aFootprint) override
polygon (not yet used for tracks, but could be in microwave apps)
Definition: board_item.h:54
bool SetNetCode(int aNetCode, bool aNoAssert)
Set 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)
void SetPriority(unsigned aPriority)
Function SetPriority.
Definition: zone.h:118
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:762
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:563
VERTICES_ARRAY m_outline
Definition: pcb_polygon.h:47
static int GetDefaultHatchPitch()
Function GetDefaultHatchPitchMils.
Definition: zone.cpp:1084
void SetDoNotAllowPads(bool aEnable)
Definition: zone.h:766
int GetNetCode(wxString aNetName) const
Definition: pcb_component.h:71
virtual void SetPosOffset(int aX_offs, int aY_offs) override
void SetDoNotAllowVias(bool aEnable)
Definition: zone.h:764
ZONE handles 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:81
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:70
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:832
ISLANDS_ARRAY m_islands
Definition: pcb_polygon.h:48
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
void SetDoNotAllowCopperPour(bool aEnable)
Definition: zone.h:763
void FormPolygon(XNODE *aNode, VERTICES_ARRAY *aPolygon, const wxString &aDefaultMeasurementUnit, const wxString &actualConversion)
void SetWidth(int aWidth)
Definition: pcb_shape.h:117
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
The common library.
virtual void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
Definition: zone.cpp:235
void SetLocalClearance(int aClearance)
Definition: zone.h:161
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT) override
Removes an item from the container.
Definition: footprint.cpp:467
void AssignNet(const wxString &aNetName)
Definition: pcb_polygon.cpp:86
void SetPolyPoints(const std::vector< wxPoint > &aPoints)
Definition: pcb_shape.cpp:1075
PCB_POLYGON(PCB_CALLBACKS *aCallbacks, BOARD *aBoard, int aPCadLayer)
Definition: pcb_polygon.cpp:39
void SetDoNotAllowFootprints(bool aEnable)
Definition: zone.h:767