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 if( m_params.m_thieving )
99 {
101 zoneInfo.m_Netcode = 0;
103 }
104
106 && ( zoneInfo.m_Layers & LSET::AllCuMask() ).any() )
107 {
108 setUniquePriority( zoneInfo );
109 }
110
111 // If we don't have a net from highlighting, maybe we can get one from the selection
112 PCB_SELECTION_TOOL* selectionTool = m_tool.GetManager()->GetTool<PCB_SELECTION_TOOL>();
113
114 if( selectionTool && !selectionTool->GetSelection().Empty() && zoneInfo.m_Netcode == -1 )
115 {
116 EDA_ITEM* item = *selectionTool->GetSelection().GetItems().begin();
117
118 if( BOARD_CONNECTED_ITEM* bci = dynamic_cast<BOARD_CONNECTED_ITEM*>( item ) )
119 zoneInfo.m_Netcode = bci->GetNetCode();
120 }
121
122 if( m_params.m_mode != ZONE_MODE::GRAPHIC_POLYGON && !m_params.m_thieving )
123 {
124 // When drawing a new zone, skip the pre-draw dialog for thieving copper and
125 // graphic polygons as it is more disruptive than useful
126 int dialogResult;
127
128 if( m_params.m_keepout )
129 dialogResult = InvokeRuleAreaEditor( frame, &zoneInfo, m_tool.board() );
130 else if( ( zoneInfo.m_Layers & LSET::AllCuMask() ).any() )
131 dialogResult = InvokeCopperZonesEditor( frame, nullptr, &zoneInfo );
132 else
133 dialogResult = InvokeNonCopperZonesEditor( frame, &zoneInfo );
134
135 if( dialogResult == wxID_CANCEL )
136 return nullptr;
137
138 controls->WarpMouseCursor( controls->GetCursorPosition(), true );
139 frame->GetCanvas()->SetFocus();
140 }
141
142 wxASSERT( !m_tool.m_isFootprintEditor || ( parent->Type() == PCB_FOOTPRINT_T ) );
143
144 std::unique_ptr<ZONE> newZone = std::make_unique<ZONE>( parent );
145
146 // Apply the selected settings
147 zoneInfo.ExportSetting( *newZone );
148
149 return newZone;
150}
151
152
153std::unique_ptr<ZONE> ZONE_CREATE_HELPER::createZoneFromExisting( const ZONE& aSrcZone )
154{
155 BOARD* board = m_tool.getModel<BOARD>();
156
157 std::unique_ptr<ZONE> newZone = std::make_unique<ZONE>( board );
158
159 ZONE_SETTINGS zoneSettings;
160 zoneSettings << aSrcZone;
161
162 zoneSettings.ExportSetting( *newZone );
163
164 return newZone;
165}
166
167
168void ZONE_CREATE_HELPER::performZoneCutout( ZONE& aZone, const ZONE& aCutout )
169{
170 BOARD_COMMIT commit( &m_tool );
171 std::vector<ZONE*> newZones;
172
173 // Clear the selection before removing the old zone
174 TOOL_MANAGER* toolMgr = m_tool.GetManager();
176
177 SHAPE_POLY_SET originalOutline( *aZone.Outline() );
178 SHAPE_POLY_SET cutoutOutline( *aCutout.Outline() );
179
180 // Clipper2 cannot carry arcs through boolean operations when either operand has holes or
181 // the clip side has outlines, so strip arc metadata first. Without this, zones built from
182 // circles (or any arc-bearing outline) get corrupt arc endpoints written to disk and
183 // render as garbage after reload. See SHAPE_POLY_SET::booleanOp.
184 originalOutline.ClearArcs();
185 cutoutOutline.ClearArcs();
186
187 originalOutline.BooleanSubtract( cutoutOutline );
188
189 // After substracting the hole, originalOutline can have more than one main outline.
190 // But a zone can have only one main outline, so create as many zones as originalOutline
191 // contains main outlines:
192 for( int outline = 0; outline < originalOutline.OutlineCount(); outline++ )
193 {
194 SHAPE_POLY_SET* newZoneOutline = new SHAPE_POLY_SET;
195 newZoneOutline->AddOutline( originalOutline.Outline( outline ) );
196
197 // Add holes (if any) to the new zone outline:
198 for (int hole = 0; hole < originalOutline.HoleCount( outline ) ; hole++ )
199 newZoneOutline->AddHole( originalOutline.CHole( outline, hole ) );
200
201 ZONE* newZone = new ZONE( aZone );
202 newZone->SetOutline( newZoneOutline ); // zone takes ownership
203 newZone->SetLocalFlags( 1 );
204 newZone->HatchBorder();
205 newZone->UnFill();
206 newZones.push_back( newZone );
207 commit.Add( newZone );
208 }
209
210 commit.Remove( &aZone );
211 commit.Push( _( "Add Zone Cutout" ) );
212
213 // Select the new zone and set it as the source for the next cutout
214 if( newZones.empty() )
215 {
216 m_params.m_sourceZone = nullptr;
217 }
218 else
219 {
220 m_params.m_sourceZone = newZones[0];
221 toolMgr->RunAction<EDA_ITEM*>( ACTIONS::selectItem, newZones[0] );
222 }
223}
224
225
226void ZONE_CREATE_HELPER::commitZone( std::unique_ptr<ZONE> aZone )
227{
228 switch ( m_params.m_mode )
229 {
231 // For cutouts, subtract from the source
232 performZoneCutout( *m_params.m_sourceZone, *aZone );
233 break;
234
235 case ZONE_MODE::ADD:
237 {
238 BOARD_COMMIT commit( &m_tool );
239
240 aZone->HatchBorder();
241
242 commit.Add( aZone.get() );
243 commit.Push( _( "Draw Zone" ) );
244
245 m_tool.GetManager()->RunAction<EDA_ITEM*>( ACTIONS::selectItem, aZone.release() );
246 break;
247 }
248
250 {
251 BOARD_COMMIT commit( &m_tool );
252 BOARD* board = m_tool.getModel<BOARD>();
253 PCB_LAYER_ID layer = m_params.m_layer;
254 PCB_SHAPE* poly = new PCB_SHAPE( m_tool.m_frame->GetModel() );
255
256 poly->SetShape( SHAPE_T::POLY );
257 poly->SetFilled( layer != Edge_Cuts && layer != F_CrtYd && layer != B_CrtYd );
258
261 poly->SetLayer( layer );
262 poly->SetPolyShape( *aZone->Outline() );
263
264 commit.Add( poly );
265 commit.Push( _( "Draw Polygon" ) );
266
267 m_tool.GetManager()->RunAction<EDA_ITEM*>( ACTIONS::selectItem, poly );
268 break;
269 }
270 }
271}
272
273
275{
276 // if we don't have a zone, create one
277 if( !m_zone )
278 {
279 if( m_params.m_sourceZone )
280 m_zone = createZoneFromExisting( *m_params.m_sourceZone );
281 else
282 m_zone = createNewZone( m_params.m_keepout );
283
284 if( m_zone )
285 {
286 m_tool.GetManager()->RunAction( ACTIONS::selectionClear );
287
288 // set up properties from zone
289 const RENDER_SETTINGS& settings = *m_parentView.GetPainter()->GetSettings();
290 COLOR4D color = settings.GetColor( nullptr, m_zone->GetFirstLayer() );
291
292 m_previewItem.SetStrokeColor( COLOR4D::WHITE );
293 m_previewItem.SetFillColor( color.WithAlpha( 0.2 ) );
294
295 m_parentView.SetVisible( &m_previewItem, true );
296
297 LEADER_MODE mode = m_tool.GetAngleSnapMode();
298
299 aMgr.SetLeaderMode( mode );
300 }
301 }
302
303 return m_zone != nullptr;
304}
305
306
308{
309 // Handle a cancel-interactive
310 if( m_zone && !aMgr.IsPolygonInProgress() )
311 {
312 m_zone = nullptr;
313 m_parentView.SetVisible( &m_previewItem, false );
314 return;
315 }
316
317 // send the points to the preview item
318 m_previewItem.SetPoints( aMgr.GetLockedInPoints(), aMgr.GetLeaderLinePoints(),
319 aMgr.GetLoopLinePoints() );
321}
322
323
325{
326 const SHAPE_LINE_CHAIN& finalPoints = aMgr.GetLockedInPoints();
327
328 if( finalPoints.PointCount() < 3 )
329 {
330 // just scrap the zone in progress
331 m_zone = nullptr;
332 }
333 else
334 {
335 // if m_params.m_mode == DRAWING_TOOL::ZONE_MODE::CUTOUT, m_zone will be merged to the
336 // existing zone as a new hole.
337 m_zone->Outline()->NewOutline();
338 SHAPE_POLY_SET* outline = m_zone->Outline();
339
340 for( int i = 0; i < finalPoints.PointCount(); ++i )
341 outline->Append( finalPoints.CPoint( i ) );
342
343 // In DEG45 mode, we may have intermediate points in the leader that should be included
344 // as they are shown in the preview. These typically maintain the 45 constraint
346 {
347 const SHAPE_LINE_CHAIN leaderPts = aMgr.GetLeaderLinePoints();
348
349 for( int i = 1; i < leaderPts.PointCount(); i++ )
350 outline->Append( leaderPts.CPoint( i ) );
351
352 const SHAPE_LINE_CHAIN loopPts = aMgr.GetLoopLinePoints();
353
354 for( int i = 1; i < loopPts.PointCount() - 1; i++ )
355 outline->Append( loopPts.CPoint( i ) );
356 }
357
358 SHAPE_LINE_CHAIN& chain = outline->Outline( 0 );
359
360 chain.SetClosed( true );
361 chain.Simplify( true );
362
363 // Remove the start point if it lies on the line between neighbouring points.
364 // Simplify doesn't handle that currently.
365 if( chain.PointCount() >= 3 )
366 {
367 SEG seg( chain.CLastPoint(), chain.CPoint( 1 ) );
368
369 if( seg.LineDistance( chain.CPoint( 0 ) ) <= 1 )
370 chain.Remove( 0 );
371 }
372
373 // hand the zone over to the committer
374 commitZone( std::move( m_zone ) );
375 m_zone = nullptr;
376 }
377
378 m_parentView.SetVisible( &m_previewItem, false );
379}
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:372
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:487
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:1477
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