KiCad PCB EDA Suite
Loading...
Searching...
No Matches
footprint_editor_utils.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, 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
24#include <board_commit.h>
25#include <confirm.h>
28#include <footprint_tree_pane.h>
29#include <fp_lib_table.h>
30#include <functional>
31#include <kiway_express.h>
32#include <pcb_group.h>
33#include <pcb_marker.h>
34#include <pcb_textbox.h>
35#include <pcb_barcode.h>
36#include <pcb_table.h>
37#include <pcb_shape.h>
38#include <pad.h>
39#include <zone.h>
41#include <tool/tool_manager.h>
42#include <tools/pcb_actions.h>
45#include <widgets/lib_tree.h>
47#include <pcb_dimension.h>
48#include <project_pcb.h>
49#include <view/view_controls.h>
52
53using namespace std::placeholders;
54
55
57{
58 bool is_last_fp_from_brd = IsCurrentFPFromBoard();
59
60 FOOTPRINT* footprint = LoadFootprint( aFPID );
61
62 if( !footprint )
63 return;
64
65 if( !Clear_Pcb( true ) )
66 return;
67
69 AddFootprintToBoard( footprint );
70
71 footprint->ClearFlags();
72
73 // if either reference or value are missing, reinstall them -
74 // otherwise you cannot see what you are doing on board
75 if( footprint->Reference().GetText().IsEmpty() )
76 footprint->SetReference( wxT( "Ref**" ) );
77
78 if( footprint->Value().GetText().IsEmpty() )
79 footprint->SetValue( wxT( "Val**" ) );
80
81 Zoom_Automatique( false );
82
83 Update3DView( true, true );
84
85 GetScreen()->SetContentModified( false );
86
87 UpdateView();
88 GetCanvas()->Refresh();
89
90 // Update the save items if needed.
91 if( is_last_fp_from_brd )
92 {
95 }
96
97 m_treePane->GetLibTree()->ExpandLibId( aFPID );
98
99 m_centerItemOnIdle = aFPID;
100 Bind( wxEVT_IDLE, &FOOTPRINT_EDIT_FRAME::centerItemIdleHandler, this );
101
102 m_treePane->GetLibTree()->RefreshLibTree(); // update highlighting
103}
104
105
107{
108 m_treePane->GetLibTree()->CenterLibId( m_centerItemOnIdle );
109 Unbind( wxEVT_IDLE, &FOOTPRINT_EDIT_FRAME::centerItemIdleHandler, this );
110}
111
112
114{
115public:
117 {
118 wxASSERT( aFootprint );
119
120 m_nickname = aFootprint->GetFPID().GetLibNickname().wx_str();
121 m_fpname = aFootprint->GetFPID().GetLibItemName().wx_str();
124 m_keywords = aFootprint->GetKeywords();
125 m_doc = aFootprint->GetLibDescription();
126 m_loaded = true;
127 }
128};
129
130
131void FOOTPRINT_EDIT_FRAME::UpdateLibraryTree( const wxDataViewItem& aTreeItem,
132 FOOTPRINT* aFootprint )
133{
134 wxCHECK( aFootprint, /* void */ );
135
136 BASIC_FOOTPRINT_INFO footprintInfo( aFootprint );
137
138 if( aTreeItem.IsOk() ) // Can be not found in tree if the current footprint is imported
139 // from file therefore not yet in tree.
140 {
141 static_cast<LIB_TREE_NODE_ITEM*>( aTreeItem.GetID() )->Update( &footprintInfo );
142 m_treePane->GetLibTree()->RefreshLibTree();
143 }
144}
145
146
148{
149 LIB_ID oldFPID = aFootprint->GetFPID();
150
151 DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR dialog( this, aFootprint );
152 dialog.ShowQuasiModal();
153
154 // Update design settings for footprint layers
156
157 // Update library tree and title in case of a name change
158 wxDataViewItem treeItem = m_adapter->FindItem( oldFPID );
159 UpdateLibraryTree( treeItem, aFootprint );
160 UpdateTitle();
161
164}
165
166
168{
169 switch( aItem->Type() )
170 {
173 break;
174
175 case PCB_BARCODE_T:
176 ShowBarcodePropertiesDialog( static_cast<PCB_BARCODE*>( aItem ) );
177 break;
178
179 case PCB_PAD_T:
180 ShowPadPropertiesDialog( static_cast<PAD*>( aItem ) );
181 break;
182
183 case PCB_FOOTPRINT_T:
184 editFootprintProperties( static_cast<FOOTPRINT*>( aItem ) );
185 GetCanvas()->Refresh();
186 break;
187
188 case PCB_FIELD_T:
189 case PCB_TEXT_T:
190 ShowTextPropertiesDialog( static_cast<PCB_TEXT*>( aItem ) );
191 break;
192
193 case PCB_TEXTBOX_T:
194 ShowTextBoxPropertiesDialog( static_cast<PCB_TEXTBOX*>( aItem ) );
195 break;
196
197 case PCB_TABLE_T:
198 {
199 DIALOG_TABLE_PROPERTIES dlg( this, static_cast<PCB_TABLE*>( aItem ) );
200
201 //QuasiModal required for Scintilla auto-complete
202 dlg.ShowQuasiModal();
203 break;
204 }
205
206 case PCB_SHAPE_T :
207 ShowGraphicItemPropertiesDialog( static_cast<PCB_SHAPE*>( aItem ) );
208 break;
209
211 case PCB_DIM_CENTER_T:
212 case PCB_DIM_RADIAL_T:
214 case PCB_DIM_LEADER_T:
215 {
216 DIALOG_DIMENSION_PROPERTIES dlg( this, static_cast<PCB_DIMENSION_BASE*>( aItem ) );
217
218 // TODO: why is this QuasiModal?
219 dlg.ShowQuasiModal();
220 break;
221 }
222
223 case PCB_ZONE_T:
224 {
225 ZONE* zone = static_cast<ZONE*>( aItem );
226 bool success = false;
227 ZONE_SETTINGS zoneSettings;
228
229 zoneSettings << *static_cast<ZONE*>( aItem );
230
231 if( zone->GetIsRuleArea() )
232 {
233 success = InvokeRuleAreaEditor( this, &zoneSettings ) == wxID_OK;
234 }
235 else if( zone->IsOnCopperLayer() )
236 {
237 success = InvokeCopperZonesEditor( this, &zoneSettings ) == wxID_OK;
238 }
239 else
240 {
241 success = InvokeNonCopperZonesEditor( this, &zoneSettings ) == wxID_OK;
242 }
243
244 if( success )
245 {
246 BOARD_COMMIT commit( this );
247 commit.Modify( zone );
248 commit.Push( _( "Edit Zone" ) );
249 zoneSettings.ExportSetting( *static_cast<ZONE*>( aItem ) );
250 }
251
252 break;
253 }
254
255 case PCB_GROUP_T:
257 static_cast<EDA_GROUP*>( static_cast<PCB_GROUP*>( aItem ) ) );
258 break;
259
260 case PCB_MARKER_T:
261 m_toolManager->GetTool<FOOTPRINT_EDITOR_CONTROL>()->CrossProbe( static_cast<PCB_MARKER*>( aItem ) );
262 break;
263
264 case PCB_POINT_T:
265 break;
266
267 default:
268 wxFAIL_MSG( wxT( "FOOTPRINT_EDIT_FRAME::OnEditItemRequest: unsupported item type " )
269 + aItem->GetClass() );
270 break;
271 }
272}
273
274
279
280
282{
283 const PCB_LAYER_ID oldLayer = GetActiveLayer();
284
285 if( oldLayer == aLayer )
286 return;
287
289
290 /*
291 * Follow the PCB editor logic for showing/hiding clearance layers: show only for
292 * the active copper layer or a front/back non-copper layer.
293 */
294 const auto getClearanceLayerForActive = []( PCB_LAYER_ID aActiveLayer ) -> std::optional<int>
295 {
296 if( IsCopperLayer( aActiveLayer ) )
297 return CLEARANCE_LAYER_FOR( aActiveLayer );
298
299 return std::nullopt;
300 };
301
302 if( std::optional<int> oldClearanceLayer = getClearanceLayerForActive( oldLayer ) )
303 GetCanvas()->GetView()->SetLayerVisible( *oldClearanceLayer, false );
304
305 if( std::optional<int> newClearanceLayer = getClearanceLayerForActive( aLayer ) )
306 GetCanvas()->GetView()->SetLayerVisible( *newClearanceLayer, true );
307
308 m_appearancePanel->OnLayerChanged();
309
310 m_toolManager->RunAction( PCB_ACTIONS::layerChanged ); // notify other tools
311 GetCanvas()->SetFocus(); // allow capture of hotkeys
312 GetCanvas()->SetHighContrastLayer( aLayer );
313 GetCanvas()->Refresh();
314}
315
316
317bool FOOTPRINT_EDIT_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, int aCtl )
318{
319 if( !Clear_Pcb( true ) )
320 return false; // this command is aborted
321
323 ImportFootprint( aFileSet[ 0 ] );
324
325 if( GetBoard()->GetFirstFootprint() )
327
328 GetScreen()->SetContentModified( false );
329 Zoom_Automatique( false );
330 GetCanvas()->Refresh();
331
332 return true;
333}
334
335
337{
338 const std::string& payload = mail.GetPayload();
339
340 switch( mail.Command() )
341 {
342 case MAIL_FP_EDIT:
343 if( !payload.empty() )
344 {
345 wxFileName fpFileName( payload );
346 wxString libNickname;
347 wxString msg;
348
350 const LIB_TABLE_ROW* libTableRow = libTable->FindRowByURI( fpFileName.GetPath() );
351
352 if( !libTableRow )
353 {
354 msg.Printf( _( "The current configuration does not include the footprint library '%s'." ),
355 fpFileName.GetPath() );
356 msg += wxS( "\n" ) + _( "Use Manage Footprint Libraries to edit the configuration." );
357 DisplayErrorMessage( this, _( "Library not found in footprint library table." ),
358 msg );
359 break;
360 }
361
362 libNickname = libTableRow->GetNickName();
363
364 if( !libTable->HasLibrary( libNickname, true ) )
365 {
366 msg.Printf( _( "The footprint library '%s' is not enabled in the current configuration." ),
367 libNickname );
368 msg += wxS( "\n" ) + _( "Use Manage Footprint Libraries to edit the configuration." );
369 DisplayErrorMessage( this, _( "Footprint library not enabled." ), msg );
370 break;
371 }
372
373 LIB_ID fpId( libNickname, fpFileName.GetName() );
374
375 if( m_treePane )
376 {
377 m_treePane->GetLibTree()->SelectLibId( fpId );
378 wxCommandEvent event( EVT_LIBITEM_CHOSEN );
379 wxPostEvent( m_treePane, event );
380 }
381 }
382
383 break;
384
385 case MAIL_RELOAD_LIB:
386 SyncLibraryTree( true );
388 break;
389
390 default:
391 break;
392 }
393}
static TOOL_ACTION groupProperties
Definition actions.h:246
void SetContentModified(bool aModified=true)
Definition base_screen.h:59
BASIC_FOOTPRINT_INFO(FOOTPRINT *aFootprint)
virtual void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:79
FOOTPRINT * GetFirstFootprint() const
Get the first footprint on the board or nullptr.
Definition board.h:505
COLOR4D GetColor(int aLayer) const
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition commit.h:106
void ReCreateMenuBar()
Recreate the menu bar.
virtual void ReCreateHToolbar()
virtual void Zoom_Automatique(bool aWarpPointer)
Redraw the screen with best zoom level and the best centering that shows all the page or the board.
KIGFX::VIEW_CONTROLS * GetViewControls() const
Return a pointer to the #VIEW_CONTROLS instance used in the panel.
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
void SetFocus() override
A set of EDA_ITEMs (i.e., without duplicates).
Definition eda_group.h:46
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition eda_item.h:144
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition eda_text.h:98
Module editor specific tools.
void editFootprintProperties(FOOTPRINT *aFootprint)
Run the Footprint Properties dialog and handle changes made in it.
void UpdateLibraryTree(const wxDataViewItem &treeItem, FOOTPRINT *aFootprint)
Update a single node in the library tree.
void SyncLibraryTree(bool aProgress)
Synchronize the footprint library tree to the current state of the footprint library table.
void UpdateMsgPanel() override
Redraw the message panel.
void KiwayMailIn(KIWAY_EXPRESS &mail) override
Receive KIWAY_EXPRESS messages from other players.
void LoadFootprintFromLibrary(LIB_ID aFPID)
FOOTPRINT * ImportFootprint(const wxString &aName=wxT(""))
Read a file containing only one footprint.
void UpdateUserInterface()
Update the layer manager and other widgets from the board setup (layer and items visibility,...
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > m_adapter
void centerItemIdleHandler(wxIdleEvent &aEvent)
bool Clear_Pcb(bool doAskAboutUnsavedChanges)
Delete all and reinitialize the current board.
Definition initpcb.cpp:108
void OnEditItemRequest(BOARD_ITEM *aItem) override
Install the corresponding dialog editor for the given item.
void SetActiveLayer(PCB_LAYER_ID aLayer) override
void AddFootprintToBoard(FOOTPRINT *aFootprint) override
Override from PCB_BASE_EDIT_FRAME which adds a footprint to the editor's dummy board,...
FOOTPRINT_TREE_PANE * m_treePane
void RefreshLibraryTree()
Redisplay the library tree.
bool OpenProjectFiles(const std::vector< wxString > &aFileSet, int aCtl=0) override
Load a KiCad board (.kicad_pcb) from aFileName.
COLOR_SETTINGS * GetColorSettings(bool aForceRefresh=false) const override
Helper to retrieve the current color settings.
wxString m_doc
Footprint description.
wxString m_fpname
Module name.
wxString m_keywords
Footprint keywords.
unsigned m_unique_pad_count
Number of unique pads.
unsigned m_pad_count
Number of pads.
wxString m_nickname
library as known in FP_LIB_TABLE
wxString GetLibDescription() const
Definition footprint.h:278
unsigned GetPadCount(INCLUDE_NPTH_T aIncludeNPTH=INCLUDE_NPTH_T(INCLUDE_NPTH)) const
Return the number of pads.
PCB_FIELD & Value()
read/write accessors:
Definition footprint.h:697
const LIB_ID & GetFPID() const
Definition footprint.h:269
void SetReference(const wxString &aReference)
Definition footprint.h:667
unsigned GetUniquePadCount(INCLUDE_NPTH_T aIncludeNPTH=INCLUDE_NPTH_T(INCLUDE_NPTH)) const
Return the number of unique non-blank pads.
void SetValue(const wxString &aValue)
Definition footprint.h:688
PCB_FIELD & Reference()
Definition footprint.h:698
wxString GetKeywords() const
Definition footprint.h:281
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:104
virtual void SetCrossHairCursorPosition(const VECTOR2D &aPosition, bool aWarpView=true)=0
Move the graphic crosshair cursor to the requested position expressed in world coordinates.
virtual wxString GetClass() const =0
Return the class name.
void SetLayerVisible(int aLayer, bool aVisible=true)
Control the visibility of a particular layer.
Definition view.h:400
Carry a payload from one KIWAY_PLAYER to another within a PROJECT.
std::string & GetPayload()
Return the payload, which can be any text but it typically self identifying s-expression.
MAIL_T Command()
Returns the MAIL_T associated with this mail.
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:49
const UTF8 & GetLibItemName() const
Definition lib_id.h:102
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition lib_id.h:87
Hold a record identifying a library accessed by the appropriate plug in object in the LIB_TABLE.
const wxString & GetNickName() const
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library table.
const LIB_TABLE_ROW * FindRowByURI(const wxString &aURI)
Node type: LIB_ID.
void Update(LIB_TREE_ITEM *aItem)
Update the node using data from a LIB_ALIAS object.
Definition pad.h:54
static TOOL_ACTION layerChanged
int ShowTextBoxPropertiesDialog(PCB_TEXTBOX *aTextBox)
APPEARANCE_CONTROLS * m_appearancePanel
void ShowReferenceImagePropertiesDialog(BOARD_ITEM *aBitmap)
void ShowBarcodePropertiesDialog(PCB_BARCODE *aText)
void ShowGraphicItemPropertiesDialog(PCB_SHAPE *aShape)
void ShowPadPropertiesDialog(PAD *aPad)
virtual PCB_LAYER_ID GetActiveLayer() const
FOOTPRINT * LoadFootprint(const LIB_ID &aFootprintId)
Attempt to load aFootprintId from the footprint library table.
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
PCB_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
BOARD * GetBoard() const
virtual void SetActiveLayer(PCB_LAYER_ID aLayer)
virtual void Update3DView(bool aMarkDirty, bool aRefresh, const wxString *aTitle=nullptr)
Update the 3D view, if the viewer is opened by this frame.
Abstract dimension API.
virtual KIGFX::PCB_VIEW * GetView() const override
Return a pointer to the #VIEW instance used in the panel.
virtual void SetHighContrastLayer(int aLayer) override
SetHighContrastLayer(), with some extra smarts for PCB.
A set of BOARD_ITEMs (i.e., without duplicates).
Definition pcb_group.h:53
static FP_LIB_TABLE * PcbFootprintLibs(PROJECT *aProject)
Return the table of footprint libraries without Kiway.
TOOL_MANAGER * m_toolManager
wxString wx_str() const
Definition utf8.cpp:45
ZONE_SETTINGS handles zones parameters.
void ExportSetting(ZONE &aTarget, bool aFullExport=true) const
Function ExportSetting copy settings to a given zone.
Handle a list of polygons defining a copper zone.
Definition zone.h:74
bool GetIsRuleArea() const
Accessors to parameters used in Rule Area zones:
Definition zone.h:704
bool IsOnCopperLayer() const override
Definition zone.cpp:499
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:194
This file is part of the common library.
int InvokeCopperZonesEditor(PCB_BASE_FRAME *aCaller, 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)
@ DO_NOT_INCLUDE_NPTH
Definition footprint.h:70
PROJECT & Prj()
Definition kicad.cpp:612
bool IsCopperLayer(int aLayerId)
Test whether a layer is a copper layer.
Definition layer_ids.h:676
@ LAYER_GRID
Definition layer_ids.h:254
#define CLEARANCE_LAYER_FOR(boardLayer)
Definition layer_ids.h:371
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
@ MAIL_FP_EDIT
Definition mail_type.h:56
@ MAIL_RELOAD_LIB
Definition mail_type.h:57
BARCODE class definition.
Class to handle a set of BOARD_ITEMs.
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:88
@ PCB_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition typeinfo.h:106
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition typeinfo.h:103
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition typeinfo.h:104
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition typeinfo.h:111
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition typeinfo.h:93
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition typeinfo.h:108
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition typeinfo.h:92
@ PCB_REFERENCE_IMAGE_T
class PCB_REFERENCE_IMAGE, bitmap on a layer
Definition typeinfo.h:89
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition typeinfo.h:90
@ PCB_MARKER_T
class PCB_MARKER, a marker used to show something
Definition typeinfo.h:99
@ PCB_BARCODE_T
class PCB_BARCODE, a barcode (graphic item)
Definition typeinfo.h:101
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:86
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition typeinfo.h:102
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:87
@ PCB_TABLE_T
class PCB_TABLE, table of PCB_TABLECELLs
Definition typeinfo.h:94
@ PCB_POINT_T
class PCB_POINT, a 0-dimensional point
Definition typeinfo.h:113
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition typeinfo.h:105
VECTOR2< double > VECTOR2D
Definition vector2d.h:694