KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <core/spinlock.h>
23#include <tool/tool_manager.h>
24#include <zone.h>
25#include <pcb_shape.h>
26#include <footprint.h>
27#include <board_commit.h>
29#include <pcb_painter.h>
30#include <tools/pcb_actions.h>
32#include <view/view_controls.h>
33
35 m_tool( aTool ),
36 m_params( aParams ),
37 m_parentView( *aTool.getView() )
38{
40}
41
42
44{
45 // remove the preview from the view
46 m_parentView.SetVisible( &m_previewItem, false );
47 m_parentView.Remove( &m_previewItem );
48}
49
50
52{
53 PCB_BASE_EDIT_FRAME* frame = m_tool.getEditFrame<PCB_BASE_EDIT_FRAME>();
54 BOARD* board = frame->GetBoard();
55
56 // By default, new zones get the first unused priority
57 std::set<unsigned> priorities;
58
59 for( ZONE* zone : board->Zones() )
60 {
61 if( zone->GetTeardropAreaType() == TEARDROP_TYPE::TD_NONE
62 && ( zone->GetLayerSet() & LSET::AllCuMask() ).any()
63 && !zone->GetIsRuleArea() )
64 {
65 priorities.insert( zone->GetAssignedPriority() );
66 }
67 }
68
69 unsigned priority = 0;
70
71 for( unsigned exist_priority : priorities )
72 {
73 if( priority != exist_priority )
74 break;
75
76 ++priority;
77 }
78
79 aZoneInfo.m_ZonePriority = priority;
80}
81
82
83std::unique_ptr<ZONE> ZONE_CREATE_HELPER::createNewZone( bool aKeepout )
84{
85 PCB_BASE_EDIT_FRAME* frame = m_tool.getEditFrame<PCB_BASE_EDIT_FRAME>();
86 BOARD* board = frame->GetBoard();
87 BOARD_ITEM_CONTAINER* parent = m_tool.m_frame->GetModel();
88 KIGFX::VIEW_CONTROLS* controls = m_tool.GetManager()->GetViewControls();
89 std::set<int> highlightedNets = board->GetHighLightNetCodes();
90
91 // Get the current default settings for zones
92 ZONE_SETTINGS zoneInfo = board->GetDesignSettings().GetDefaultZoneSettings();
93 zoneInfo.m_Layers.reset().set( m_params.m_layer ); // TODO(JE) multilayer defaults?
94 zoneInfo.m_LayerProperties.clear(); // Do not copy over layer properties
95 zoneInfo.m_Netcode = highlightedNets.empty() ? -1 : *highlightedNets.begin();
96 zoneInfo.SetIsRuleArea( m_params.m_keepout );
97
98 // A new zone starts unnamed, do not inherit the last drawn zone's name (issue 23131)
99 zoneInfo.m_Name = wxEmptyString;
100
101 if( m_params.m_thieving )
102 {
104 zoneInfo.m_Netcode = 0;
106 }
107
109 && ( zoneInfo.m_Layers & LSET::AllCuMask() ).any() )
110 {
111 setUniquePriority( zoneInfo );
112 }
113
114 // If we don't have a net from highlighting, maybe we can get one from the selection
115 PCB_SELECTION_TOOL* selectionTool = m_tool.GetManager()->GetTool<PCB_SELECTION_TOOL>();
116
117 if( selectionTool && !selectionTool->GetSelection().Empty() && zoneInfo.m_Netcode == -1 )
118 {
119 EDA_ITEM* item = *selectionTool->GetSelection().GetItems().begin();
120
121 if( BOARD_CONNECTED_ITEM* bci = dynamic_cast<BOARD_CONNECTED_ITEM*>( item ) )
122 zoneInfo.m_Netcode = bci->GetNetCode();
123 }
124
125 if( m_params.m_mode != ZONE_MODE::GRAPHIC_POLYGON && !m_params.m_thieving )
126 {
127 // When drawing a new zone, skip the pre-draw dialog for thieving copper and
128 // graphic polygons as it is more disruptive than useful
129 int dialogResult;
130
131 if( m_params.m_keepout )
132 dialogResult = InvokeRuleAreaEditor( frame, &zoneInfo, m_tool.board() );
133 else if( ( zoneInfo.m_Layers & LSET::AllCuMask() ).any() )
134 dialogResult = InvokeCopperZonesEditor( frame, nullptr, &zoneInfo );
135 else
136 dialogResult = InvokeNonCopperZonesEditor( frame, &zoneInfo );
137
138 if( dialogResult == wxID_CANCEL )
139 return nullptr;
140
141 controls->WarpMouseCursor( controls->GetCursorPosition(), true );
142 frame->GetCanvas()->SetFocus();
143 }
144
145 wxASSERT( !m_tool.m_isFootprintEditor || ( parent->Type() == PCB_FOOTPRINT_T ) );
146
147 std::unique_ptr<ZONE> newZone = std::make_unique<ZONE>( parent );
148
149 // Apply the selected settings
150 zoneInfo.ExportSetting( *newZone );
151
152 return newZone;
153}
154
155
156std::unique_ptr<ZONE> ZONE_CREATE_HELPER::createZoneFromExisting( const ZONE& aSrcZone )
157{
158 BOARD* board = m_tool.getModel<BOARD>();
159
160 std::unique_ptr<ZONE> newZone = std::make_unique<ZONE>( board );
161
162 ZONE_SETTINGS zoneSettings;
163 zoneSettings << aSrcZone;
164
165 zoneSettings.ExportSetting( *newZone );
166
167 return newZone;
168}
169
170
171void ZONE_CREATE_HELPER::performZoneCutout( ZONE& aZone, const ZONE& aCutout )
172{
173 BOARD_COMMIT commit( &m_tool );
174 std::vector<ZONE*> newZones;
175
176 // Clear the selection before removing the old zone
177 TOOL_MANAGER* toolMgr = m_tool.GetManager();
179
180 SHAPE_POLY_SET originalOutline( *aZone.Outline() );
181 SHAPE_POLY_SET cutoutOutline( *aCutout.Outline() );
182
183 // Clipper2 cannot carry arcs through boolean operations when either operand has holes or
184 // the clip side has outlines, so strip arc metadata first. Without this, zones built from
185 // circles (or any arc-bearing outline) get corrupt arc endpoints written to disk and
186 // render as garbage after reload. See SHAPE_POLY_SET::booleanOp.
187 originalOutline.ClearArcs();
188 cutoutOutline.ClearArcs();
189
190 originalOutline.BooleanSubtract( cutoutOutline );
191
192 // After substracting the hole, originalOutline can have more than one main outline.
193 // But a zone can have only one main outline, so create as many zones as originalOutline
194 // contains main outlines:
195 for( int outline = 0; outline < originalOutline.OutlineCount(); outline++ )
196 {
197 SHAPE_POLY_SET* newZoneOutline = new SHAPE_POLY_SET;
198 newZoneOutline->AddOutline( originalOutline.Outline( outline ) );
199
200 // Add holes (if any) to the new zone outline:
201 for (int hole = 0; hole < originalOutline.HoleCount( outline ) ; hole++ )
202 newZoneOutline->AddHole( originalOutline.CHole( outline, hole ) );
203
204 ZONE* newZone = new ZONE( aZone );
205 newZone->SetOutline( newZoneOutline ); // zone takes ownership
206 newZone->SetLocalFlags( 1 );
207 newZone->HatchBorder();
208 newZone->UnFill();
209 newZones.push_back( newZone );
210 commit.Add( newZone );
211 }
212
213 commit.Remove( &aZone );
214 commit.Push( _( "Add Zone Cutout" ) );
215
216 // Select the new zone and set it as the source for the next cutout
217 if( newZones.empty() )
218 {
219 m_params.m_sourceZone = nullptr;
220 }
221 else
222 {
223 m_params.m_sourceZone = newZones[0];
224 toolMgr->RunAction<EDA_ITEM*>( ACTIONS::selectItem, newZones[0] );
225 }
226}
227
228
229void ZONE_CREATE_HELPER::commitZone( std::unique_ptr<ZONE> aZone )
230{
231 switch ( m_params.m_mode )
232 {
234 // For cutouts, subtract from the source
235 performZoneCutout( *m_params.m_sourceZone, *aZone );
236 break;
237
238 case ZONE_MODE::ADD:
240 {
241 BOARD_COMMIT commit( &m_tool );
242
243 aZone->HatchBorder();
244
245 commit.Add( aZone.get() );
246 commit.Push( _( "Draw Zone" ) );
247
248 m_tool.GetManager()->RunAction<EDA_ITEM*>( ACTIONS::selectItem, aZone.release() );
249 break;
250 }
251
253 {
254 BOARD_COMMIT commit( &m_tool );
255 BOARD* board = m_tool.getModel<BOARD>();
256 PCB_LAYER_ID layer = m_params.m_layer;
257 PCB_SHAPE* poly = new PCB_SHAPE( m_tool.m_frame->GetModel() );
258
259 poly->SetShape( SHAPE_T::POLY );
260 poly->SetFilled( layer != Edge_Cuts && layer != F_CrtYd && layer != B_CrtYd );
261
264 poly->SetLayer( layer );
265 poly->SetPolyShape( *aZone->Outline() );
266
267 commit.Add( poly );
268 commit.Push( _( "Draw Polygon" ) );
269
270 m_tool.GetManager()->RunAction<EDA_ITEM*>( ACTIONS::selectItem, poly );
271 break;
272 }
273 }
274}
275
276
278{
279 // if we don't have a zone, create one
280 if( !m_zone )
281 {
282 if( m_params.m_sourceZone )
283 m_zone = createZoneFromExisting( *m_params.m_sourceZone );
284 else
285 m_zone = createNewZone( m_params.m_keepout );
286
287 if( m_zone )
288 {
289 m_tool.GetManager()->RunAction( ACTIONS::selectionClear );
290
291 // set up properties from zone
292 const RENDER_SETTINGS& settings = *m_parentView.GetPainter()->GetSettings();
293 COLOR4D color = settings.GetColor( nullptr, m_zone->GetFirstLayer() );
294
295 m_previewItem.SetStrokeColor( COLOR4D::WHITE );
296 m_previewItem.SetFillColor( color.WithAlpha( 0.2 ) );
297
298 m_parentView.SetVisible( &m_previewItem, true );
299
300 LEADER_MODE mode = m_tool.GetAngleSnapMode();
301
302 aMgr.SetLeaderMode( mode );
303 }
304 }
305
306 return m_zone != nullptr;
307}
308
309
311{
312 // Handle a cancel-interactive
313 if( m_zone && !aMgr.IsPolygonInProgress() )
314 {
315 m_zone = nullptr;
316 m_parentView.SetVisible( &m_previewItem, false );
317 return;
318 }
319
320 // send the points to the preview item
321 m_previewItem.SetPoints( aMgr.GetLockedInPoints(), aMgr.GetLeaderLinePoints(),
322 aMgr.GetLoopLinePoints() );
324}
325
326
328{
329 const SHAPE_LINE_CHAIN& finalPoints = aMgr.GetLockedInPoints();
330
331 if( finalPoints.PointCount() < 3 )
332 {
333 // just scrap the zone in progress
334 m_zone = nullptr;
335 }
336 else
337 {
338 // if m_params.m_mode == DRAWING_TOOL::ZONE_MODE::CUTOUT, m_zone will be merged to the
339 // existing zone as a new hole.
340 m_zone->Outline()->NewOutline();
341 SHAPE_POLY_SET* outline = m_zone->Outline();
342
343 for( int i = 0; i < finalPoints.PointCount(); ++i )
344 outline->Append( finalPoints.CPoint( i ) );
345
346 // In DEG45 mode, we may have intermediate points in the leader that should be included
347 // as they are shown in the preview. These typically maintain the 45 constraint
349 {
350 const SHAPE_LINE_CHAIN leaderPts = aMgr.GetLeaderLinePoints();
351
352 for( int i = 1; i < leaderPts.PointCount(); i++ )
353 outline->Append( leaderPts.CPoint( i ) );
354
355 const SHAPE_LINE_CHAIN loopPts = aMgr.GetLoopLinePoints();
356
357 for( int i = 1; i < loopPts.PointCount() - 1; i++ )
358 outline->Append( loopPts.CPoint( i ) );
359 }
360
361 SHAPE_LINE_CHAIN& chain = outline->Outline( 0 );
362
363 chain.SetClosed( true );
364 chain.Simplify( true );
365
366 // Remove the start point if it lies on the line between neighbouring points.
367 // Simplify doesn't handle that currently.
368 if( chain.PointCount() >= 3 )
369 {
370 SEG seg( chain.CLastPoint(), chain.CPoint( 1 ) );
371
372 if( seg.LineDistance( chain.CPoint( 0 ) ) <= 1 )
373 chain.Remove( 0 );
374 }
375
376 // hand the zone over to the committer
377 commitZone( std::move( m_zone ) );
378 m_zone = nullptr;
379 }
380
381 m_parentView.SetVisible( &m_previewItem, false );
382}
static TOOL_ACTION selectItem
Select an item (specified as the event parameter).
Definition actions.h:223
static TOOL_ACTION selectionClear
Clear the current selection.
Definition actions.h:220
BASE_SET & reset(size_t pos)
Definition base_set.h:143
BASE_SET & set(size_t pos)
Definition base_set.h:116
virtual void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
int GetLineThickness(PCB_LAYER_ID aLayer) const
Return the default graphic segment thickness from the layer class for the given layer.
Abstract interface for BOARD_ITEMs capable of storing other items inside.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:373
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition board.cpp:1149
static const COLOR4D WHITE
Definition color4d.h:401
COMMIT & Remove(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Remove a new item from the model.
Definition commit.h:86
COMMIT & Add(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Add a new item to the model.
Definition commit.h:74
Tool responsible for drawing graphical elements like lines, arcs, circles, etc.
void SetFocus() override
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:96
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:108
virtual void SetFilled(bool aFlag)
Definition eda_shape.h:152
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
Definition color4d.h:308
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
virtual COLOR4D GetColor(const VIEW_ITEM *aItem, int aLayer) const =0
Returns the color that should be used to draw the specific VIEW_ITEM on the specific layer using curr...
An interface for classes handling user events controlling the view behavior such as zooming,...
virtual void WarpMouseCursor(const VECTOR2D &aPosition, bool aWorldCoordinates=false, bool aWarpView=false)=0
If enabled (.
VECTOR2D GetCursorPosition() const
Return the current cursor position in world coordinates.
static LSET AllCuMask(int aCuLayerCount)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition lset.cpp:595
Common, abstract interface for edit frames.
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
BOARD * GetBoard() const
The selection tool: currently supports:
PCB_SELECTION & GetSelection()
void SetShape(SHAPE_T aShape) override
Definition pcb_shape.h:200
void SetPolyShape(const SHAPE_POLY_SET &aShape) override
void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
void SetStroke(const STROKE_PARAMS &aStroke) override
Class that handles the drawing of a polygon, including management of last corner deletion and drawing...
const SHAPE_LINE_CHAIN & GetLockedInPoints() const
Get the "locked-in" points that describe the polygon itself.
LEADER_MODE GetLeaderMode() const
void SetLeaderMode(LEADER_MODE aMode)
Set the leader mode to use when calculating the leader/returner lines.
const SHAPE_LINE_CHAIN & GetLoopLinePoints() const
Get the points from the current cursor position to the polygon start point.
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...
Definition seg.h:38
int LineDistance(const VECTOR2I &aP, bool aDetermineSide=false) const
Return the closest Euclidean distance between point aP and the line defined by the ends of segment (t...
Definition seg.cpp:742
const std::deque< EDA_ITEM * > GetItems() const
Definition selection.h:122
bool Empty() const
Checks if there is anything selected.
Definition selection.h:111
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
int PointCount() const
Return the number of points (vertices) in this line chain.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
Represent a set of closed polygons.
void ClearArcs()
Removes all arc references from all the outlines and holes in the polyset.
int AddOutline(const SHAPE_LINE_CHAIN &aOutline)
Adds a new outline to the set and returns its index.
int HoleCount(int aOutline) const
Returns the number of holes in a given outline.
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)
int AddHole(const SHAPE_LINE_CHAIN &aHole, int aOutline=-1)
Adds a new hole to the given outline (default: last) and returns its index.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
const SHAPE_LINE_CHAIN & CHole(int aOutline, int aHole) const
int OutlineCount() const
Return the number of outlines in the set.
void BooleanSubtract(const SHAPE_POLY_SET &b)
Perform boolean polyset difference.
Simple container to manage line stroke parameters.
Master controller class:
bool RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
void commitZone(std::unique_ptr< ZONE > aZone)
Commit the current zone-in-progress to the BOARD.
PARAMS & m_params
The preview item to display.
bool OnFirstPoint(POLYGON_GEOM_MANAGER &aMgr) override
Called before the first point is added - clients can do initialization here, and can veto the start o...
ZONE_CREATE_HELPER(DRAWING_TOOL &aTool, PARAMS &aParams)
std::unique_ptr< ZONE > createNewZone(bool aKeepout)
Prompt the user for new zone settings, and create a new zone with those settings.
void performZoneCutout(ZONE &aZone, const ZONE &aCutout)
Cut one zone out of another one (i.e.
void setUniquePriority(ZONE_SETTINGS &aZoneInfo)
Choose a new priority for @aZoneInfo.
void OnGeometryChange(const POLYGON_GEOM_MANAGER &aMgr) override
Called when the polygon is complete.
std::unique_ptr< ZONE > m_zone
std::unique_ptr< ZONE > createZoneFromExisting(const ZONE &aSrcZone)
Create a new zone with the settings from an existing zone.
KIGFX::PREVIEW::POLYGON_ITEM m_previewItem
view that show the preview item
void OnComplete(const POLYGON_GEOM_MANAGER &aMgr) override
KIGFX::VIEW & m_parentView
The zone-in-progress.
DRAWING_TOOL & m_tool
Parameters of the zone to be drawn.
ZONE_SETTINGS handles zones parameters.
void SetIsRuleArea(bool aEnable)
void ExportSetting(ZONE &aTarget, bool aFullExport=true) const
Function ExportSetting copy settings to a given zone.
void SetPadConnection(ZONE_CONNECTION aPadConnection)
unsigned m_ZonePriority
std::map< PCB_LAYER_ID, ZONE_LAYER_PROPERTIES > m_LayerProperties
ZONE_FILL_MODE m_FillMode
Handle a list of polygons defining a copper zone.
Definition zone.h:70
bool UnFill()
Removes the zone filling.
Definition zone.cpp:507
void SetLocalFlags(int aFlags)
Definition zone.h:416
void HatchBorder()
Compute the hatch lines depending on the hatch parameters and stores it in the zone's attribute m_bor...
Definition zone.cpp:1520
SHAPE_POLY_SET * Outline()
Definition zone.h:418
void SetOutline(SHAPE_POLY_SET *aOutline)
Definition zone.h:421
int InvokeCopperZonesEditor(PCB_BASE_FRAME *aCaller, ZONE *aZone, ZONE_SETTINGS *aSettings, CONVERT_SETTINGS *aConvertSettings)
Function InvokeCopperZonesEditor invokes up a modal dialog window for copper zone editing.
int InvokeNonCopperZonesEditor(PCB_BASE_FRAME *aParent, ZONE_SETTINGS *aSettings, CONVERT_SETTINGS *aConvertSettings)
Function InvokeNonCopperZonesEditor invokes up a modal dialog window for non-copper zone editing.
int InvokeRuleAreaEditor(PCB_BASE_FRAME *aCaller, ZONE_SETTINGS *aZoneSettings, BOARD *aBoard, CONVERT_SETTINGS *aConvertSettings)
Function InvokeRuleAreaEditor invokes up a modal dialog window for copper zone editing.
#define _(s)
LEADER_MODE
The kind of the leader line.
@ DEG45
45 Degree only
@ DEG90
90 Degree only
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ F_CrtYd
Definition layer_ids.h:112
@ Edge_Cuts
Definition layer_ids.h:108
@ B_CrtYd
Definition layer_ids.h:111
@ GEOMETRY
Position or shape has changed.
Definition view_item.h:51
@ SIMILAR
Add a new zone with the same settings as an existing one.
Definition pcb_actions.h:34
@ CUTOUT
Make a cutout to an existing zone.
Definition pcb_actions.h:33
@ GRAPHIC_POLYGON
Definition pcb_actions.h:35
@ ADD
Add a new zone/keepout with fresh settings.
Definition pcb_actions.h:32
Parameters used to fully describe a zone creation process.
const SHAPE_LINE_CHAIN chain
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:79
@ NONE
Pads are not covered.
Definition zones.h:45