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  *
4  * Copyright (C) 2017-2019 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
25 #include <tool/tool_manager.h>
26 #include <class_zone.h>
27 #include <class_drawsegment.h>
28 #include <class_edge_mod.h>
29 #include <board_commit.h>
30 #include <pcb_painter.h>
31 #include <tools/pcb_actions.h>
32 #include <tools/selection_tool.h>
33 #include <zone_filler.h>
34 
36  m_tool( aTool ),
37  m_params( aParams ),
38  m_parentView( *aTool.getView() )
39 {
41 }
42 
43 
45 {
46  // remove the preview from the view
49 }
50 
51 
52 std::unique_ptr<ZONE_CONTAINER> ZONE_CREATE_HELPER::createNewZone( bool aKeepout )
53 {
54  auto& frame = *m_tool.getEditFrame<PCB_BASE_EDIT_FRAME>();
55  auto& board = *m_tool.getModel<BOARD>();
58 
59  // Get the current default settings for zones
60  ZONE_SETTINGS zoneInfo = frame.GetZoneSettings();
62  zoneInfo.m_NetcodeSelection = board.GetHighLightNetCode();
63  zoneInfo.SetIsKeepout( m_params.m_keepout );
65 
67  {
68  // Get the current default settings for zones
69 
70  // Show options dialog
71  int dialogResult;
72 
73  if( m_params.m_keepout )
74  dialogResult = InvokeKeepoutAreaEditor( &frame, &zoneInfo );
75  else
76  {
77  if( IsCopperLayer( zoneInfo.m_CurrentZone_Layer ) )
78  dialogResult = InvokeCopperZonesEditor( &frame, &zoneInfo );
79  else
80  dialogResult = InvokeNonCopperZonesEditor( &frame, &zoneInfo );
81  }
82 
83  if( dialogResult == wxID_CANCEL )
84  return nullptr;
85 
86  controls->WarpCursor( controls->GetCursorPosition(), true );
87  }
88 
89  // The new zone is a ZONE_CONTAINER if created in the board editor
90  // and a MODULE_ZONE_CONTAINER if created in the footprint editor
91  wxASSERT( !m_tool.m_editModules || ( parent->Type() == PCB_MODULE_T ) );
92 
93  auto newZone = m_tool.m_editModules ?
94  std::make_unique<MODULE_ZONE_CONTAINER>( parent ) :
95  std::make_unique<ZONE_CONTAINER>( parent );
96 
97  // Apply the selected settings
98  zoneInfo.ExportSetting( *newZone );
99 
100  return newZone;
101 }
102 
103 
104 std::unique_ptr<ZONE_CONTAINER> ZONE_CREATE_HELPER::createZoneFromExisting(
105  const ZONE_CONTAINER& aSrcZone )
106 {
107  auto& board = *m_tool.getModel<BOARD>();
108 
109  auto newZone = std::make_unique<ZONE_CONTAINER>( &board );
110 
111  ZONE_SETTINGS zoneSettings;
112  zoneSettings << aSrcZone;
113 
114  zoneSettings.ExportSetting( *newZone );
115 
116  return newZone;
117 }
118 
119 
121 {
122  BOARD_COMMIT commit( &m_tool );
123  BOARD* board = m_tool.getModel<BOARD>();
124  std::vector<ZONE_CONTAINER*> newZones;
125 
126  // Clear the selection before removing the old zone
127  auto toolMgr = m_tool.GetManager();
128  toolMgr->RunAction( PCB_ACTIONS::selectionClear, true );
129 
130  SHAPE_POLY_SET originalOutline( *aZone.Outline() );
131  originalOutline.BooleanSubtract( *aCutout.Outline(), SHAPE_POLY_SET::PM_FAST );
132 
133  // After substracting the hole, originalOutline can have more than one
134  // main outline.
135  // But a zone can have only one main outline, so create as many zones as
136  // originalOutline contains main outlines:
137  for( int outline = 0; outline < originalOutline.OutlineCount(); outline++ )
138  {
139  auto newZoneOutline = new SHAPE_POLY_SET;
140  newZoneOutline->AddOutline( originalOutline.Outline( outline ) );
141 
142  // Add holes (if any) to thez new zone outline:
143  for (int hole = 0; hole < originalOutline.HoleCount( outline ) ; hole++ )
144  newZoneOutline->AddHole( originalOutline.CHole( outline, hole ) );
145 
146  auto newZone = new ZONE_CONTAINER( aZone );
147  newZone->SetOutline( newZoneOutline );
148  newZone->SetLocalFlags( 1 );
149  newZone->Hatch();
150  newZones.push_back( newZone );
151  commit.Add( newZone );
152  }
153 
154  commit.Remove( &aZone );
155  commit.Push( _( "Add a zone cutout" ) );
156 
157  ZONE_FILLER filler( board );
158  filler.Fill( newZones );
159 
160  // Select the new zone and set it as the source for the next cutout
161  toolMgr->RunAction( PCB_ACTIONS::selectItem, true, newZones[0] );
162  m_params.m_sourceZone = newZones[0];
163 
164 }
165 
166 
167 void ZONE_CREATE_HELPER::commitZone( std::unique_ptr<ZONE_CONTAINER> aZone )
168 {
169  switch ( m_params.m_mode )
170  {
171  case ZONE_MODE::CUTOUT:
172  // For cutouts, subtract from the source
174  break;
175 
176  case ZONE_MODE::ADD:
177  case ZONE_MODE::SIMILAR:
178  {
179  BOARD_COMMIT bCommit( &m_tool );
180 
181  aZone->Hatch();
182 
183  if( !m_params.m_keepout )
184  {
185  ZONE_FILLER filler( m_tool.getModel<BOARD>() );
186  filler.Fill( { aZone.get() } );
187  }
188 
189  bCommit.Add( aZone.get() );
190  bCommit.Push( _( "Add a zone" ) );
191  m_tool.GetManager()->RunAction( PCB_ACTIONS::selectItem, true, aZone.release() );
192  break;
193  }
194 
196  {
197  BOARD_COMMIT bCommit( &m_tool );
199 
200  if( m_tool.getDrawingLayer() != Edge_Cuts )
201  {
202  auto poly = m_tool.m_editModules ? new EDGE_MODULE( (MODULE *) parent )
203  : new DRAWSEGMENT();
204  poly->SetShape ( S_POLYGON );
205  poly->SetLayer( m_tool.getDrawingLayer() );
206  poly->SetPolyShape ( *aZone->Outline() );
207  bCommit.Add( poly );
209  }
210  else
211  {
212  auto outline = aZone->Outline();
213 
214  for( auto seg = outline->CIterateSegments( 0 ); seg; seg++ )
215  {
216  auto new_seg = m_tool.m_editModules ? new EDGE_MODULE( (MODULE *) parent )
217  : new DRAWSEGMENT();
218  new_seg->SetShape( S_SEGMENT );
219  new_seg->SetLayer( m_tool.getDrawingLayer() );
220  new_seg->SetStart( wxPoint( seg.Get().A.x, seg.Get().A.y ) );
221  new_seg->SetEnd( wxPoint( seg.Get().B.x, seg.Get().B.y ) );
222  bCommit.Add( new_seg );
223  }
224  }
225 
226  bCommit.Push( _( "Add a graphical polygon" ) );
227 
228  break;
229  }
230  }
231 }
232 
233 
235 {
236  // if we don't have a zone, create one
237  // the user's choice here can affect things like the colour of the preview
238  if( !m_zone )
239  {
241 
242  if( m_params.m_sourceZone )
244  else
246 
247  if( m_zone )
248  {
249  // set up poperties from zone
250  const auto& settings = *m_parentView.GetPainter()->GetSettings();
251  COLOR4D color = settings.GetColor( nullptr, m_zone->GetLayer() );
252 
254  m_previewItem.SetFillColor( color.WithAlpha( 0.2 ) );
255 
257 
260  }
261  }
262 
263  return m_zone != nullptr;
264 }
265 
266 
268 {
269  // send the points to the preview item
272 }
273 
274 
276 {
277  auto& finalPoints = aMgr.GetLockedInPoints();
278 
279  if( finalPoints.PointCount() < 3 )
280  {
281  // just scrap the zone in progress
282  m_zone = nullptr;
283  }
284  else
285  {
286  // if m_params.m_mode == DRAWING_TOOL::ZONE_MODE::CUTOUT, m_zone
287  // will be merged to the existing zone as a new hole.
288  m_zone->Outline()->NewOutline();
289  auto* outline = m_zone->Outline();
290 
291  for( int i = 0; i < finalPoints.PointCount(); ++i )
292  outline->Append( finalPoints.CPoint( i ) );
293 
294  // In DEG45 mode, we may have intermediate points in the leader that should be
295  // included as they are shown in the preview. These typically maintain the
296  // 45 constraint
298  {
299  const auto& pts = aMgr.GetLeaderLinePoints();
300  for( int i = 1; i < pts.PointCount(); i++ )
301  outline->Append( pts.CPoint( i ) );
302  }
303 
304  outline->Outline( 0 ).SetClosed( true );
305  outline->RemoveNullSegments();
306  outline->Simplify( SHAPE_POLY_SET::PM_FAST );
307 
308  // hand the zone over to the committer
309  commitZone( std::move( m_zone ) );
310  m_zone = nullptr;
311  }
312 
314 }
static TOOL_ACTION selectionClear
Clears the current selection.
Definition: pcb_actions.h:73
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.
void OnComplete(const POLYGON_GEOM_MANAGER &aMgr) override
Called when the polygon is complete
Add a new zone with the same settings as an existing one.
int InvokeKeepoutAreaEditor(PCB_BASE_FRAME *aCaller, ZONE_SETTINGS *aSettings)
Function InvokeKeepoutAreaEditor invokes up a modal dialog window for copper zone editing.
T * getModel() const
Function getModel()
Definition: tool_base.h:200
void SetStrokeColor(const COLOR4D &aNewColor)
Set the stroke colour to set before drawing preview
COMMIT & Add(EDA_ITEM *aItem)
Adds a new item to the model
Definition: commit.h:78
const SHAPE_LINE_CHAIN & GetLeaderLinePoints() const
Get the points comprising the leader line (the line from the last locked-in point to the current curs...
int InvokeNonCopperZonesEditor(PCB_BASE_FRAME *aParent, ZONE_SETTINGS *aSettings)
Function InvokeNonCopperZonesEditor invokes up a modal dialog window for non-copper zone editing.
ZONE_CREATE_HELPER(DRAWING_TOOL &aTool, PARAMS &aParams)
int color
Definition: DXF_plotter.cpp:61
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:376
SHAPE_POLY_SET * Outline()
Definition: class_zone.h:265
PCB_LAYER_ID m_layer
Layer to begin drawing
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:139
void commitZone(std::unique_ptr< ZONE_CONTAINER > aZone)
Commit the current zone-in-progress to the BOARD.
std::unique_ptr< ZONE_CONTAINER > m_zone
The zone-in-progress
usual segment : line with rounded ends
KIGFX::PREVIEW::POLYGON_ITEM m_previewItem
The preview item to display
Parameters used to fully describe a zone creation process.
void SetLeaderMode(LEADER_MODE aMode)
Set the leader mode to use when calculating the leader/returner lines.
LEADER_MODE GetLeaderMode() const
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:199
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
TOOL_MANAGER * GetManager() const
Function GetManager() Returns the instance of TOOL_MANAGER that takes care of the tool.
Definition: tool_base.h:142
int m_NetcodeSelection
Net code selection for the current zone.
Definition: zone_settings.h:84
void performZoneCutout(ZONE_CONTAINER &aZone, ZONE_CONTAINER &aCutout)
Function performZoneCutout()
class MODULE, a footprint
Definition: typeinfo.h:89
virtual void WarpCursor(const VECTOR2D &aPosition, bool aWorldCoordinates=false, bool aWarpView=false)=0
Function WarpCursor() If enabled (.
Unconstrained point-to-point
DRAWING_TOOL.
Definition: drawing_tool.h:49
SHAPE_POLY_SET.
ZONE_CONTAINER * m_sourceZone
Zone settings source (for similar and cutout zones)
KIGFX::VIEW & m_parentView
view that show the preview item
VIEW_CONTROLS is an interface for classes handling user events controlling the view behaviour (such a...
Add a new zone/keepout with fresh settings.
void SetIsKeepout(bool aEnable)
bool OnFirstPoint(POLYGON_GEOM_MANAGER &aMgr) override
Called before the first point is added - clients can do initialisation here, and can veto the start o...
COMMIT & Remove(EDA_ITEM *aItem)
Removes a new item from the model
Definition: commit.h:90
bool Fill(const std::vector< ZONE_CONTAINER * > &aZones, bool aCheck=false)
void SetFillColor(const COLOR4D &aNewColor)
Set the fill colour to set before drawing preview
void ExportSetting(ZONE_CONTAINER &aTarget, bool aFullExport=true) const
Function ExportSetting copy settings to a given zone.
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:1540
Make a cutout to an existing zone.
PCB_BASE_EDIT_FRAME * m_frame
Definition: drawing_tool.h:226
ZONE_SETTINGS handles zones parameters.
Definition: zone_settings.h:58
PARAMS & m_params
Parameters of the zone to be drawn
int AddOutline(const SHAPE_LINE_CHAIN &aOutline)
Adds a new outline to the set and returns its index
virtual RENDER_SETTINGS * GetSettings()=0
Function GetSettings Returns pointer to current settings that are going to be used when drawing items...
KIGFX::VIEW_CONTROLS * GetViewControls() const
Definition: tool_manager.h:285
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.
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:163
PCB_LAYER_ID getDrawingLayer() const
Selects a non-copper layer for drawing
#define _(s)
Definition: 3d_actions.cpp:33
static TOOL_ACTION selectItem
Selects an item (specified as the event parameter).
Definition: pcb_actions.h:76
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true) override
Executes the changes.
Definition: colors.h:49
void SetPoints(const SHAPE_LINE_CHAIN &aLockedInPts, const SHAPE_LINE_CHAIN &aLeaderPts)
Set the polygon points.
T * getEditFrame() const
Function getEditFrame()
Definition: tool_base.h:186
bool IsCopperLayer(LAYER_NUM aLayerId)
Function IsCopperLayer tests whether a layer is a copper layer.
int InvokeCopperZonesEditor(PCB_BASE_FRAME *aCaller, ZONE_SETTINGS *aSettings)
Function InvokeCopperZonesEditor invokes up a modal dialog window for copper zone editing.
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Sets the item visibility.
Definition: view.cpp:1486
virtual BOARD_ITEM_CONTAINER * GetModel() const =0
Function GetModel()
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Function Add() Adds a VIEW_ITEM to the view.
Definition: view.cpp:346
const SHAPE_LINE_CHAIN & GetLockedInPoints() const
Get the "locked-in" points that describe the polygon itself.
void BooleanSubtract(const SHAPE_POLY_SET &b, POLYGON_MODE aFastMode)
Performs boolean polyset difference For aFastMode meaning, see function booleanOp
Abstract interface for BOARD_ITEMs capable of storing other items inside.
POLYGON_GEOM_MANAGER::LEADER_MODE m_leaderMode
Zone leader mode
bool m_keepout
Should create a keepout zone?
EDGE_MODULE class definition.
PCB_LAYER_ID m_CurrentZone_Layer
Layer used to create the current zone.
Definition: zone_settings.h:88
Class that handles the drawing of a polygon, including management of last corner deletion and drawing...
ZONE_MODE m_mode
The zone mode to operate in
VECTOR2D GetCursorPosition() const
Returns the current cursor position in world coordinates.
Color has changed.
Definition: view_item.h:57
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:40