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 (C) 1992-2022 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_table.h>
36#include <pcb_shape.h>
37#include <pad.h>
38#include <zone.h>
40#include <tool/tool_manager.h>
41#include <tools/pcb_actions.h>
44#include <widgets/lib_tree.h>
46#include <pcb_dimension.h>
47#include <project_pcb.h>
48#include <view/view_controls.h>
50#include <dialogs/dialog_table_properties.h>
51
52using namespace std::placeholders;
53
54
56{
57 LoadFootprintFromBoard( nullptr );
58}
59
60
62{
63 bool is_last_fp_from_brd = IsCurrentFPFromBoard();
64
65 FOOTPRINT* footprint = LoadFootprint( aFPID );
66
67 if( !footprint )
68 return;
69
70 if( !Clear_Pcb( true ) )
71 return;
72
74 AddFootprintToBoard( footprint );
75
76 footprint->ClearFlags();
77
78 // if either reference or value are missing, reinstall them -
79 // otherwise you cannot see what you are doing on board
80 if( footprint->Reference().GetText().IsEmpty() )
81 footprint->SetReference( wxT( "Ref**" ) );
82
83 if( footprint->Value().GetText().IsEmpty() )
84 footprint->SetValue( wxT( "Val**" ) );
85
86 Zoom_Automatique( false );
87
88 Update3DView( true, true );
89
90 GetScreen()->SetContentModified( false );
91
92 UpdateView();
93 GetCanvas()->Refresh();
94
95 // Update the save items if needed.
96 if( is_last_fp_from_brd )
97 {
100 }
101
102 m_treePane->GetLibTree()->ExpandLibId( aFPID );
103
104 m_centerItemOnIdle = aFPID;
105 Bind( wxEVT_IDLE, &FOOTPRINT_EDIT_FRAME::centerItemIdleHandler, this );
106
107 m_treePane->GetLibTree()->RefreshLibTree(); // update highlighting
108}
109
110
112{
114 Unbind( wxEVT_IDLE, &FOOTPRINT_EDIT_FRAME::centerItemIdleHandler, this );
115}
116
117
118
119void FOOTPRINT_EDIT_FRAME::SelectLayer( wxCommandEvent& event )
120{
122
123 if( GetDisplayOptions().m_ContrastModeDisplay != HIGH_CONTRAST_MODE::NORMAL )
124 GetCanvas()->Refresh();
125}
126
127
129{
130 SaveFootprintToBoard( true );
131}
132
133
135{
136public:
138 {
139 wxASSERT( aFootprint );
140
141 m_nickname = aFootprint->GetFPID().GetLibNickname().wx_str();
142 m_fpname = aFootprint->GetFPID().GetLibItemName().wx_str();
145 m_keywords = aFootprint->GetKeywords();
146 m_doc = aFootprint->GetLibDescription();
147 m_loaded = true;
148 }
149};
150
151
152void FOOTPRINT_EDIT_FRAME::UpdateLibraryTree( const wxDataViewItem& aTreeItem,
153 FOOTPRINT* aFootprint )
154{
155 wxCHECK( aFootprint, /* void */ );
156
157 BASIC_FOOTPRINT_INFO footprintInfo( aFootprint );
158
159 if( aTreeItem.IsOk() ) // Can be not found in tree if the current footprint is imported
160 // from file therefore not yet in tree.
161 {
162 static_cast<LIB_TREE_NODE_ITEM*>( aTreeItem.GetID() )->Update( &footprintInfo );
164 }
165}
166
167
169{
170 LIB_ID oldFPID = aFootprint->GetFPID();
171
172 DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR dialog( this, aFootprint );
173 dialog.ShowModal();
174
175 // Update library tree and title in case of a name change
176 wxDataViewItem treeItem = m_adapter->FindItem( oldFPID );
177 UpdateLibraryTree( treeItem, aFootprint );
178 UpdateTitle();
179
181}
182
183
185{
186 switch( aItem->Type() )
187 {
190 break;
191
192 case PCB_PAD_T:
193 ShowPadPropertiesDialog( static_cast<PAD*>( aItem ) );
194 break;
195
196 case PCB_FOOTPRINT_T:
197 editFootprintProperties( static_cast<FOOTPRINT*>( aItem ) );
198 GetCanvas()->Refresh();
199 break;
200
201 case PCB_FIELD_T:
202 case PCB_TEXT_T:
203 ShowTextPropertiesDialog( static_cast<PCB_TEXT*>( aItem ) );
204 break;
205
206 case PCB_TEXTBOX_T:
207 ShowTextBoxPropertiesDialog( static_cast<PCB_TEXTBOX*>( aItem ) );
208 break;
209
210 case PCB_TABLE_T:
211 {
212 DIALOG_TABLE_PROPERTIES dlg( this, static_cast<PCB_TABLE*>( aItem ) );
213
214 //QuasiModal required for Scintilla auto-complete
215 dlg.ShowQuasiModal();
216 break;
217 }
218
219 case PCB_SHAPE_T :
220 ShowGraphicItemPropertiesDialog( static_cast<PCB_SHAPE*>( aItem ) );
221 break;
222
224 case PCB_DIM_CENTER_T:
225 case PCB_DIM_RADIAL_T:
227 case PCB_DIM_LEADER_T:
228 {
229 DIALOG_DIMENSION_PROPERTIES dlg( this, static_cast<PCB_DIMENSION_BASE*>( aItem ) );
230
231 // TODO: why is this QuasiModal?
232 dlg.ShowQuasiModal();
233 break;
234 }
235
236 case PCB_ZONE_T:
237 {
238 ZONE* zone = static_cast<ZONE*>( aItem );
239 bool success = false;
240 ZONE_SETTINGS zoneSettings;
241
242 zoneSettings << *static_cast<ZONE*>( aItem );
243
244 if( zone->GetIsRuleArea() )
245 {
246 success = InvokeRuleAreaEditor( this, &zoneSettings ) == wxID_OK;
247 }
248 else if( zone->IsOnCopperLayer() )
249 {
250 success = InvokeCopperZonesEditor( this, &zoneSettings ) == wxID_OK;
251 }
252 else
253 {
254 success = InvokeNonCopperZonesEditor( this, &zoneSettings ) == wxID_OK;
255 }
256
257 if( success )
258 {
259 BOARD_COMMIT commit( this );
260 commit.Modify( zone );
261 commit.Push( _( "Edit Zone" ) );
262 zoneSettings.ExportSetting( *static_cast<ZONE*>( aItem ) );
263 }
264
265 break;
266 }
267
268 case PCB_GROUP_T:
270 break;
271
272 case PCB_MARKER_T:
273 m_toolManager->GetTool<FOOTPRINT_EDITOR_CONTROL>()->CrossProbe( static_cast<PCB_MARKER*>( aItem ) );
274 break;
275
276 default:
277 wxFAIL_MSG( wxT( "FOOTPRINT_EDIT_FRAME::OnEditItemRequest: unsupported item type " )
278 + aItem->GetClass() );
279 break;
280 }
281}
282
283
285{
287}
288
289
291{
293
295
296 m_toolManager->PostAction( PCB_ACTIONS::layerChanged ); // notify other tools
297 GetCanvas()->SetFocus(); // allow capture of hotkeys
298 GetCanvas()->SetHighContrastLayer( aLayer );
299 GetCanvas()->Refresh();
300}
301
302
303bool FOOTPRINT_EDIT_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, int aCtl )
304{
305 if( !Clear_Pcb( true ) )
306 return false; // this command is aborted
307
309 ImportFootprint( aFileSet[ 0 ] );
310
311 if( GetBoard()->GetFirstFootprint() )
313
314 GetScreen()->SetContentModified( false );
315 Zoom_Automatique( false );
316 GetCanvas()->Refresh();
317
318 return true;
319}
320
321
323{
324 const std::string& payload = mail.GetPayload();
325
326 switch( mail.Command() )
327 {
328 case MAIL_FP_EDIT:
329 if( !payload.empty() )
330 {
331 wxFileName fpFileName( payload );
332 wxString libNickname;
333 wxString msg;
334
336 const LIB_TABLE_ROW* libTableRow = libTable->FindRowByURI( fpFileName.GetPath() );
337
338 if( !libTableRow )
339 {
340 msg.Printf( _( "The current configuration does not include the footprint library '%s'." ),
341 fpFileName.GetPath() );
342 msg += wxS( "\n" ) + _( "Use Manage Footprint Libraries to edit the configuration." );
343 DisplayErrorMessage( this, _( "Library not found in footprint library table." ),
344 msg );
345 break;
346 }
347
348 libNickname = libTableRow->GetNickName();
349
350 if( !libTable->HasLibrary( libNickname, true ) )
351 {
352 msg.Printf( _( "The footprint library '%s' is not enabled in the current configuration." ),
353 libNickname );
354 msg += wxS( "\n" ) + _( "Use Manage Footprint Libraries to edit the configuration." );
355 DisplayErrorMessage( this, _( "Footprint library not enabled." ), msg );
356 break;
357 }
358
359 LIB_ID fpId( libNickname, fpFileName.GetName() );
360
361 if( m_treePane )
362 {
364 wxCommandEvent event( EVT_LIBITEM_CHOSEN );
365 wxPostEvent( m_treePane, event );
366 }
367 }
368
369 break;
370
371 case MAIL_RELOAD_LIB:
372 SyncLibraryTree( true );
374 break;
375
376 default:
377 break;
378 }
379}
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
Revert the commit by restoring the modified items state.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:77
FOOTPRINT * GetFirstFootprint() const
Get the first footprint on the board or nullptr.
Definition: board.h:433
COLOR4D GetColor(int aLayer) const
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Create an undo entry for an item that has been already modified.
Definition: commit.h:105
int ShowQuasiModal()
void ReCreateMenuBar()
Recreates the menu bar.
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
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:100
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: eda_item.h:128
virtual wxString GetClass() const =0
Return the class name.
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 SelectLayer(wxCommandEvent &event)
bool SaveFootprintToBoard(bool aAddNew)
void LoadFootprintFromLibrary(LIB_ID aFPID)
void OnLoadFootprintFromBoard(wxCommandEvent &event)
Called from the main toolbar to load a footprint from board mainly to edit it.
FOOTPRINT * ImportFootprint(const wxString &aName=wxT(""))
Read a file containing only one footprint.
void ReCreateHToolbar() override
Create the main horizontal toolbar for the footprint editor.
void OnSaveFootprintToBoard(wxCommandEvent &event)
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:104
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
COLOR4D GetGridColor() override
PCB_LAYER_BOX_SELECTOR * m_selLayerBox
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.
bool LoadFootprintFromBoard(FOOTPRINT *aFootprint)
Load a footprint from the main board into the Footprint Editor.
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
LIB_TREE * GetLibTree() const
wxString GetLibDescription() const
Definition: footprint.h:242
unsigned GetPadCount(INCLUDE_NPTH_T aIncludeNPTH=INCLUDE_NPTH_T(INCLUDE_NPTH)) const
Return the number of pads.
Definition: footprint.cpp:1833
PCB_FIELD & Value()
read/write accessors:
Definition: footprint.h:623
const LIB_ID & GetFPID() const
Definition: footprint.h:233
void SetReference(const wxString &aReference)
Definition: footprint.h:593
unsigned GetUniquePadCount(INCLUDE_NPTH_T aIncludeNPTH=INCLUDE_NPTH_T(INCLUDE_NPTH)) const
Return the number of unique non-blank pads.
Definition: footprint.cpp:1883
void SetValue(const wxString &aValue)
Definition: footprint.h:614
PCB_FIELD & Reference()
Definition: footprint.h:624
wxString GetKeywords() const
Definition: footprint.h:245
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.
Carry a payload from one KIWAY_PLAYER to another within a PROJECT.
Definition: kiway_express.h:40
std::string & GetPayload()
Return the payload, which can be any text but it typically self identifying s-expression.
Definition: kiway_express.h:58
MAIL_T Command()
Returns the MAIL_T associated with this mail.
Definition: kiway_express.h:50
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 RefreshLibTree()
Refreshes the tree (mainly to update highlighting and asterisking)
Definition: lib_tree.cpp:438
void CenterLibId(const LIB_ID &aLibId)
Ensure that an item is visible (preferably centered).
Definition: lib_tree.cpp:349
void SelectLibId(const LIB_ID &aLibId)
Select an item in the tree widget.
Definition: lib_tree.cpp:343
void ExpandLibId(const LIB_ID &aLibId)
Expand and item i the tree widget.
Definition: lib_tree.cpp:361
Definition: pad.h:53
static TOOL_ACTION layerChanged
Definition: pcb_actions.h:381
static TOOL_ACTION groupProperties
Definition: pcb_actions.h:301
int ShowTextBoxPropertiesDialog(PCB_TEXTBOX *aTextBox)
APPEARANCE_CONTROLS * m_appearancePanel
void ShowReferenceImagePropertiesDialog(BOARD_ITEM *aBitmap)
Set the angle used for rotate operations.
void ShowGraphicItemPropertiesDialog(PCB_SHAPE *aShape)
void ShowPadPropertiesDialog(PAD *aPad)
const PCB_DISPLAY_OPTIONS & GetDisplayOptions() const
Display options control the way tracks, vias, outlines and other things are shown (for instance solid...
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 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:51
static FP_LIB_TABLE * PcbFootprintLibs(PROJECT *aProject)
Return the table of footprint libraries without Kiway.
Definition: project_pcb.cpp:37
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:167
bool RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
Definition: tool_manager.h:150
bool PostAction(const std::string &aActionName, T aParam)
Run the specified action after the current action (coroutine) ends.
Definition: tool_manager.h:235
wxString wx_str() const
Definition: utf8.cpp:45
ZONE_SETTINGS handles zones parameters.
Definition: zone_settings.h:71
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:72
bool GetIsRuleArea() const
Accessors to parameters used in Rule Area zones:
Definition: zone.h:710
bool IsOnCopperLayer() const override
Definition: zone.cpp:255
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:195
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, CONVERT_SETTINGS *aConvertSettings)
Function InvokeRuleAreaEditor invokes up a modal dialog window for copper zone editing.
#define _(s)
@ DO_NOT_INCLUDE_NPTH
Definition: footprint.h:61
PROJECT & Prj()
Definition: kicad.cpp:595
@ LAYER_GRID
Definition: layer_ids.h:209
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:1022
@ MAIL_FP_EDIT
Definition: mail_type.h:55
@ MAIL_RELOAD_LIB
Definition: mail_type.h:56
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:105
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:102
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition: typeinfo.h:103
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:110
@ 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:107
@ 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_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:101
@ 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_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition: typeinfo.h:104
VECTOR2< double > VECTOR2D
Definition: vector2d.h:601