KiCad PCB EDA Suite
zone_create_helper.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  * Copyright (C) 2017 KiCad Developers, see AUTHORS.txt for contributors.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, you may find one here:
17  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
18  * or you may search the http://www.gnu.org website for the version 2 license,
19  * or you may write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22 
24 
25 #include <view/view.h>
26 #include <tool/tool_manager.h>
27 #include <class_zone.h>
28 #include <class_drawsegment.h>
29 #include <class_edge_mod.h>
30 #include <class_pad.h>
31 #include <board_commit.h>
32 #include <pcb_painter.h>
33 
34 #include <tools/pcb_actions.h>
35 #include <tools/selection_tool.h>
36 
37 #include <zone_filler.h>
38 
40  const PARAMS& aParams ):
41  m_tool( aTool ),
42  m_params( aParams ),
43  m_parentView( *aTool.getView() )
44 {
46 }
47 
48 
50 {
51  // remove the preview from the view
54 }
55 
56 
57 std::unique_ptr<ZONE_CONTAINER> ZONE_CREATE_HELPER::createNewZone( bool aKeepout )
58 {
59  auto& frame = *m_tool.getEditFrame<PCB_BASE_EDIT_FRAME>();
60  auto& board = *m_tool.getModel<BOARD>();
61 
62  // Get the current default settings for zones
63  ZONE_SETTINGS zoneInfo = frame.GetZoneSettings();
64  zoneInfo.m_CurrentZone_Layer = frame.GetScreen()->m_Active_Layer;
65  zoneInfo.m_NetcodeSelection = board.GetHighLightNetCode();
66  zoneInfo.SetIsKeepout( m_params.m_keepout );
67 
69  {
70  // Get the current default settings for zones
71 
72  // Show options dialog
73  ZONE_EDIT_T dialogResult;
74 
75  if( m_params.m_keepout )
76  dialogResult = InvokeKeepoutAreaEditor( &frame, &zoneInfo );
77  else
78  {
79  if( IsCopperLayer( zoneInfo.m_CurrentZone_Layer ) )
80  dialogResult = InvokeCopperZonesEditor( &frame, &zoneInfo );
81  else
82  dialogResult = InvokeNonCopperZonesEditor( &frame, nullptr, &zoneInfo );
83  }
84 
85  if( dialogResult == ZONE_ABORT )
86  {
87  return nullptr;
88  }
89  }
90 
91  auto newZone = std::make_unique<ZONE_CONTAINER>( &board );
92 
93  // Apply the selected settings
94  zoneInfo.ExportSetting( *newZone );
95 
96  return newZone;
97 }
98 
99 
100 std::unique_ptr<ZONE_CONTAINER> ZONE_CREATE_HELPER::createZoneFromExisting(
101  const ZONE_CONTAINER& aSrcZone )
102 {
103  auto& board = *m_tool.getModel<BOARD>();
104 
105  auto newZone = std::make_unique<ZONE_CONTAINER>( &board );
106 
107  ZONE_SETTINGS zoneSettings;
108  zoneSettings << aSrcZone;
109 
110  zoneSettings.ExportSetting( *newZone );
111 
112  return newZone;
113 }
114 
115 
117  ZONE_CONTAINER& aCutout )
118 {
119  BOARD* board = m_tool.getModel<BOARD>();
120  int curr_hole = aExistingZone.Outline()->NewHole( 0 );
121 
122  // Copy cutout corners into existing zone, in the new hole
123  for( int ii = 0; ii < aCutout.GetNumCorners(); ii++ )
124  {
125  aExistingZone.Outline()->Append( aCutout.GetCornerPosition( ii ), 0, curr_hole );
126  }
127 
128  // Be sure the current corner list is closed
129  aExistingZone.Outline()->Hole( 0, curr_hole ).SetClosed( true );
130 
131  // Combine holes and simplify the new outline:
132  board->OnAreaPolygonModified( nullptr, &aExistingZone );
133 
134  // Re-fill if needed
135  if( aExistingZone.IsFilled() )
136  {
137  ZONE_FILLER filler( board );
138  filler.Fill( { &aExistingZone } );
139  }
140 }
141 
142 
143 void ZONE_CREATE_HELPER::commitZone( std::unique_ptr<ZONE_CONTAINER> aZone )
144 {
145  auto& frame = *m_tool.getEditFrame<PCB_EDIT_FRAME>();
146  auto board = m_tool.getModel<BOARD>();
147 
148  BOARD_COMMIT bCommit( &m_tool );
149 
150  switch ( m_params.m_mode )
151  {
153  // For cutouts, subtract from the source
154  bCommit.Modify( m_params.m_sourceZone );
156  bCommit.Push( _( "Add a zone cutout" ) );
158  break;
159 
162  aZone->Hatch();
163 
164  if( !m_params.m_keepout )
165  {
166  ZONE_FILLER filler( board );
167  filler.Fill( { aZone.get() } );
168  }
169 
170  bCommit.Add( aZone.release() );
171  bCommit.Push( _( "Add a zone" ) );
172  break;
173 
175  {
176  BOARD_ITEM_CONTAINER* parent = frame.GetModel();
177  auto poly = m_tool.m_editModules ? new EDGE_MODULE( (MODULE *) parent ) : new DRAWSEGMENT();
178 
179  poly->SetShape ( S_POLYGON );
180  poly->SetLayer( aZone->GetLayer() );
181  poly->SetPolyShape ( *aZone->Outline() );
182 
183  bCommit.Add( poly );
184  bCommit.Push( _( "Add a graphical polygon" ) );
185 
186  break;
187  }
188  }
189 }
190 
191 
193 {
194  // if we don't have a zone, create one
195  // the user's choice here can affect things like the colour
196  // of the preview
197  if( !m_zone )
198  {
199  if( m_params.m_sourceZone )
201  else
203 
204  if( m_zone )
205  {
206  // set up poperties from zone
207  const auto& settings = *m_parentView.GetPainter()->GetSettings();
208  COLOR4D color = settings.GetColor( nullptr, m_zone->GetLayer() );
209 
210  m_previewItem.SetStrokeColor( color );
211  m_previewItem.SetFillColor( color.WithAlpha( 0.2 ) );
212 
214  }
215  }
216 
217  if( !m_zone )
218  {
219  return false;
220  }
221 
222  return true;
223 }
224 
225 
227 {
228  // send the points to the preview item
231 }
232 
233 
235 {
236  auto& finalPoints = aMgr.GetLockedInPoints();
237 
238  if( finalPoints.size() < 3 )
239  {
240  // just scrap the zone in progress
241  m_zone = nullptr;
242  }
243  else
244  {
245  // if m_params.m_mode == DRAWING_TOOL::ZONE_MODE::CUTOUT, m_zone
246  // will be merged to the existing zone as a new hole.
247  m_zone->Outline()->NewOutline();
248 
249  for( const auto& pt : finalPoints )
250  {
251  m_zone->Outline()->Append( pt );
252  }
253 
254  m_zone->Outline()->Outline( 0 ).SetClosed( true );
255  m_zone->Outline()->RemoveNullSegments();
256 
257  // hand the zone over to the committer
258  commitZone( std::move( m_zone ) );
259  }
260 
262 }
int NewHole(int aOutline=-1)
Creates a new hole in a given outline
Class ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:60
std::unique_ptr< ZONE_CONTAINER > createZoneFromExisting(const ZONE_CONTAINER &aSrcZone)
Function createZoneFromExisting.
int GetNumCorners(void) const
Access to m_Poly parameters.
Definition: class_zone.h:414
void OnComplete(const POLYGON_GEOM_MANAGER &aMgr) override
Called when the polygon is complete
void SetStrokeColor(const COLOR4D &aNewColor)
Set the stroke colour to set before drawing preview
bool OnAreaPolygonModified(PICKED_ITEMS_LIST *aModifiedZonesList, ZONE_CONTAINER *modified_area)
Function OnAreaPolygonModified Process an area that has been modified, by normalizing its polygon and...
ZONE_EDIT_T
Exit codes for zone editing dialogs.
Definition: zones.h:47
bool IsFilled() const
Definition: class_zone.h:189
polygon (not yet used for tracks, but could be in microwave apps)
virtual void Remove(VIEW_ITEM *aItem)
Function Remove() Removes a VIEW_ITEM from the view.
Definition: view.cpp:342
SHAPE_POLY_SET * Outline()
Definition: class_zone.h:236
void commitZone(std::unique_ptr< ZONE_CONTAINER > aZone)
Commit the current zone-in-progress to the BOARD.
SHAPE_LINE_CHAIN & Hole(int aOutline, int aHole)
Returns the reference to aHole-th hole in the aIndex-th outline
Classes to handle copper zones.
std::unique_ptr< ZONE_CONTAINER > m_zone
The zone-in-progress
KIGFX::PREVIEW::POLYGON_ITEM m_previewItem
The preview item to display
void SetPoints(const std::vector< VECTOR2I > &aLockedInPts, const std::vector< VECTOR2I > &aLeaderPts)
Set the polygon points.
Parameters used to fully describe a zone creation process.
const std::vector< VECTOR2I > & GetLeaderLinePoints() const
Get the points comprising the leader line (the line from the last locked-in point to the current curs...
const std::vector< VECTOR2I > & GetLockedInPoints() const
Get the "locked-in" points that describe the polygon itself.
const VECTOR2I & GetCornerPosition(int aCornerIndex) const
Definition: class_zone.h:454
void performZoneCutout(ZONE_CONTAINER &aExistingZone, ZONE_CONTAINER &aCutout)
Function performZoneCutout()
Add a new zone with the same settings as an existing one.
void SetClosed(bool aClosed)
Function SetClosed()
T * getModel() const
Function getModel()
Definition: tool_base.h:199
Class DRAWING_TOOL.
Definition: drawing_tool.h:49
ZONE_CONTAINER * m_sourceZone
Zone settings source (for similar and cutout zones)
KIGFX::VIEW & m_parentView
view that show the preview item
COLOR4D WithAlpha(double aAlpha) const
Function WithAlpha Returns a colour with the same colour, but the given alpha.
Definition: color4d.h:237
if no change
Definition: zones.h:48
ZONE_EDIT_T InvokeNonCopperZonesEditor(PCB_BASE_FRAME *aParent, ZONE_CONTAINER *aZone, ZONE_SETTINGS *aSettings)
Function InvokeNonCopperZonesEditor invokes up a modal dialog window for non-copper zone editing...
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:196
void SetFillColor(const COLOR4D &aNewColor)
Set the fill colour to set before drawing preview
virtual void Update(VIEW_ITEM *aItem, int aUpdateFlags)
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: view.cpp:1385
Pad object description.
DRAWING_TOOL::ZONE_MODE m_mode
The zone mode to operate in
bool m_editModules
Definition: pcb_tool.h:142
Class ZONE_SETTINGS handles zones parameters.
ZONE_CREATE_HELPER(DRAWING_TOOL &aTool, const PARAMS &aParams)
void Fill(std::vector< ZONE_CONTAINER * > aZones)
Definition: zone_filler.cpp:96
const PARAMS & m_params
Parameters of the zone to be drawn
virtual RENDER_SETTINGS * GetSettings()=0
Function GetSettings Returns pointer to current settings that are going to be used when drawing items...
void OnGeometryChange(const POLYGON_GEOM_MANAGER &aMgr) override
Sent when the polygon geometry changes
std::unique_ptr< ZONE_CONTAINER > createNewZone(bool aKeepout)
Function createNewZone()
Class to handle a graphic segment.
Common, abstract interface for edit frames.
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:169
void Hatch()
Function Hatch computes the hatch lines depending on the hatch parameters and stores it in the zone's...
ZONE_EDIT_T InvokeCopperZonesEditor(PCB_BASE_FRAME *aCaller, ZONE_SETTINGS *aSettings)
Function InvokeCopperZonesEditor invokes up a modal dialog window for copper zone editing...
bool IsCopperLayer(LAYER_NUM aLayerId)
Function IsCopperLayer tests whether a layer is a copper layer.
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Sets the item visibility.
Definition: view.cpp:1331
ZONE_EDIT_T InvokeKeepoutAreaEditor(PCB_BASE_FRAME *aCaller, ZONE_SETTINGS *aSettings)
Function InvokeKeepoutAreaEditor invokes up a modal dialog window for copper zone editing...
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Function Add() Adds a VIEW_ITEM to the view.
Definition: view.cpp:312
Abstract interface for BOARD_ITEMs capable of storing other items inside.
bool OnFirstPoint() override
Called before the first point is added - clients can do initialisation here, and can veto the start o...
bool m_keepout
Should create a keepout zone?
EDGE_MODULE class definition.
Add a new zone/keepout with fresh settings.
Make a cutout to an existing zone.
PCB_LAYER_ID m_CurrentZone_Layer
Layer used to create the current zone.
Class that handles the drawing of a polygon, including management of last corner deletion and drawing...
Color has changed.
Definition: view_item.h:57
T * getEditFrame() const
Function getEditFrame()
Definition: tool_base.h:188
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Appends a vertex at the end of the given outline/hole (default: the last outline) ...
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39