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 <board_commit.h>
29 #include <pcb_painter.h>
30 
31 #include <tools/pcb_actions.h>
32 #include <tools/selection_tool.h>
33 
34 
36  const PARAMS& aParams ):
37  m_tool( aTool ),
38  m_params( aParams ),
39  m_parentView( *aTool.getView() )
40 {
42 }
43 
44 
46 {
47  // remove the preview from the view
50 }
51 
52 
53 std::unique_ptr<ZONE_CONTAINER> ZONE_CREATE_HELPER::createNewZone( bool aKeepout )
54 {
55  auto& frame = *m_tool.getEditFrame<PCB_BASE_EDIT_FRAME>();
56  auto& board = *m_tool.getModel<BOARD>();
57 
58  // Get the current default settings for zones
59  ZONE_SETTINGS zoneInfo = frame.GetZoneSettings();
60  zoneInfo.m_CurrentZone_Layer = frame.GetScreen()->m_Active_Layer;
61  zoneInfo.m_NetcodeSelection = board.GetHighLightNetCode();
62  zoneInfo.SetIsKeepout( m_params.m_keepout );
63 
64  // Show options dialog
65  ZONE_EDIT_T dialogResult;
66 
67  if( m_params.m_keepout )
68  dialogResult = InvokeKeepoutAreaEditor( &frame, &zoneInfo );
69  else
70  {
71  if( IsCopperLayer( zoneInfo.m_CurrentZone_Layer ) )
72  dialogResult = InvokeCopperZonesEditor( &frame, &zoneInfo );
73  else
74  dialogResult = InvokeNonCopperZonesEditor( &frame, nullptr, &zoneInfo );
75  }
76 
77  if( dialogResult == ZONE_ABORT )
78  {
79  return nullptr;
80  }
81 
82  auto newZone = std::make_unique<ZONE_CONTAINER>( &board );
83 
84  // Apply the selected settings
85  zoneInfo.ExportSetting( *newZone );
86 
87  return newZone;
88 }
89 
90 
91 std::unique_ptr<ZONE_CONTAINER> ZONE_CREATE_HELPER::createZoneFromExisting(
92  const ZONE_CONTAINER& aSrcZone )
93 {
94  auto& board = *m_tool.getModel<BOARD>();
95 
96  auto newZone = std::make_unique<ZONE_CONTAINER>( &board );
97 
98  ZONE_SETTINGS zoneSettings;
99  zoneSettings << aSrcZone;
100 
101  zoneSettings.ExportSetting( *newZone );
102 
103  return newZone;
104 }
105 
106 
108  ZONE_CONTAINER& aCutout )
109 {
110  BOARD* board = m_tool.getModel<BOARD>();
111  int curr_hole = aExistingZone.Outline()->NewHole( 0 );
112 
113  // Copy cutout corners into existing zone, in the new hole
114  for( int ii = 0; ii < aCutout.GetNumCorners(); ii++ )
115  {
116  aExistingZone.Outline()->Append( aCutout.GetCornerPosition( ii ), 0, curr_hole );
117  }
118 
119  // Be sure the current corner list is closed
120  aExistingZone.Outline()->Hole( 0, curr_hole ).SetClosed( true );
121 
122  // Combine holes and simplify the new outline:
123  board->OnAreaPolygonModified( nullptr, &aExistingZone );
124 
125  // Re-fill if needed
126  if( aExistingZone.IsFilled() )
127  {
129  frame->Fill_Zone( &aExistingZone );
130  }
131 }
132 
133 
134 void ZONE_CREATE_HELPER::commitZone( std::unique_ptr<ZONE_CONTAINER> aZone )
135 {
136  auto& frame = *m_tool.getEditFrame<PCB_EDIT_FRAME>();
137 
138  BOARD_COMMIT bCommit( &m_tool );
139 
141  {
142  // For cutouts, subtract from the source
143  bCommit.Modify( m_params.m_sourceZone );
145  bCommit.Push( _( "Add a zone cutout" ) );
147  }
148  else
149  {
150  // Add the zone as a new board item
151  aZone->Hatch();
152 
153  if( !m_params.m_keepout )
154  frame.Fill_Zone( aZone.get() );
155 
156  bCommit.Add( aZone.release() );
157  bCommit.Push( _( "Add a zone" ) );
158  }
159 };
160 
161 
163 {
164  // if we don't have a zone, create one
165  // the user's choice here can affect things like the colour
166  // of the preview
167  if( !m_zone )
168  {
169  if( m_params.m_sourceZone )
171  else
173 
174  if( m_zone )
175  {
176  // set up poperties from zone
177  const auto& settings = *m_parentView.GetPainter()->GetSettings();
178  COLOR4D color = settings.GetColor( nullptr, m_zone->GetLayer() );
179 
180  m_previewItem.SetStrokeColor( color );
181  m_previewItem.SetFillColor( color.WithAlpha( 0.2 ) );
182 
184  }
185  }
186 
187  if( !m_zone )
188  {
189  return false;
190  }
191 
192  return true;
193 }
194 
195 
197 {
198  // send the points to the preview item
201 }
202 
203 
205 {
206  auto& finalPoints = aMgr.GetLockedInPoints();
207 
208  if( finalPoints.size() < 3 )
209  {
210  // just scrap the zone in progress
211  m_zone = nullptr;
212  }
213  else
214  {
215  // if m_params.m_mode == DRAWING_TOOL::ZONE_MODE::CUTOUT, m_zone
216  // will be merged to the existing zone as a new hole.
217  m_zone->Outline()->NewOutline();
218 
219  for( const auto& pt : finalPoints )
220  {
221  m_zone->Outline()->Append( pt );
222  }
223 
224  m_zone->Outline()->Outline( 0 ).SetClosed( true );
225  m_zone->Outline()->RemoveNullSegments();
226 
227  // hand the zone over to the committer
228  commitZone( std::move( m_zone ) );
229  }
230 
232 }
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:78
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:466
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:200
void Remove(VIEW_ITEM *aItem)
Function Remove() Removes a VIEW_ITEM from the view.
Definition: view.cpp:341
SHAPE_POLY_SET * Outline()
Definition: class_zone.h:247
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:506
void performZoneCutout(ZONE_CONTAINER &aExistingZone, ZONE_CONTAINER &aCutout)
Function performZoneCutout()
void SetClosed(bool aClosed)
Function SetClosed()
T * getModel() const
Function getModel()
Definition: tool_base.h:204
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:232
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
int Fill_Zone(ZONE_CONTAINER *aZone)
Function Fill_Zone Calculate the zone filling for the outline zone_container The zone outline is a fr...
void SetFillColor(const COLOR4D &aNewColor)
Set the fill colour to set before drawing preview
void Update(VIEW_ITEM *aItem)
Function Update() For dynamic VIEWs, informs the associated VIEW that the graphical representation of...
Definition: view.cpp:1379
DRAWING_TOOL::ZONE_MODE m_mode
The zone mode to operate in
Class ZONE_SETTINGS handles zones parameters.
ZONE_CREATE_HELPER(DRAWING_TOOL &aTool, const PARAMS &aParams)
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()
Common, abstract interface for edit frames.
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:166
void Hatch()
Function Hatch computes the hatch lines depending on the hatch parameters and stores it in the zone's...
Definition: class_zone.cpp:932
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)
Function SetVisible() 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...
void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Function Add() Adds a VIEW_ITEM to the view.
Definition: view.cpp:311
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?
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:193
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