KiCad PCB EDA Suite
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_marker.h>
33#include <pad.h>
34#include <zone.h>
36#include <tool/tool_manager.h>
37#include <tools/pcb_actions.h>
40#include <widgets/lib_tree.h>
42#include <pcb_dimension.h>
44
45using namespace std::placeholders;
46
47
49{
50 LoadFootprintFromBoard( nullptr );
51}
52
53
55{
56 bool is_last_fp_from_brd = IsCurrentFPFromBoard();
57
58 FOOTPRINT* footprint = LoadFootprint( aFPID );
59
60 if( !footprint )
61 return;
62
63 if( !Clear_Pcb( true ) )
64 return;
65
67 AddFootprintToBoard( footprint );
68
69 footprint->ClearFlags();
70
71 // if either m_Reference or m_Value are gone, reinstall them -
72 // otherwise you cannot see what you are doing on board
73 FP_TEXT* ref = &footprint->Reference();
74 FP_TEXT* val = &footprint->Value();
75
76 if( val && ref )
77 {
78 ref->SetType( FP_TEXT::TEXT_is_REFERENCE ); // just in case ...
79
80 if( ref->GetLength() == 0 )
81 ref->SetText( wxT( "Ref**" ) );
82
83 val->SetType( FP_TEXT::TEXT_is_VALUE ); // just in case ...
84
85 if( val->GetLength() == 0 )
86 val->SetText( wxT( "Val**" ) );
87 }
88
89 Zoom_Automatique( false );
90
91 Update3DView( true, true );
92
93 GetScreen()->SetContentModified( false );
94
95 UpdateView();
96 GetCanvas()->Refresh();
97
98 // Update the save items if needed.
99 if( is_last_fp_from_brd )
100 {
103 }
104
105 m_treePane->GetLibTree()->ExpandLibId( aFPID );
106
107 m_centerItemOnIdle = aFPID;
108 Bind( wxEVT_IDLE, &FOOTPRINT_EDIT_FRAME::centerItemIdleHandler, this );
109
110 m_treePane->GetLibTree()->RefreshLibTree(); // update highlighting
111}
112
113
115{
117 Unbind( wxEVT_IDLE, &FOOTPRINT_EDIT_FRAME::centerItemIdleHandler, this );
118}
119
120
121
122void FOOTPRINT_EDIT_FRAME::SelectLayer( wxCommandEvent& event )
123{
125
126 if( GetDisplayOptions().m_ContrastModeDisplay != HIGH_CONTRAST_MODE::NORMAL )
127 GetCanvas()->Refresh();
128}
129
130
132{
133 SaveFootprintToBoard( true );
134}
135
136
138{
139public:
141 {
142 wxASSERT( aFootprint );
143
144 m_nickname = aFootprint->GetFPID().GetLibNickname().wx_str();
145 m_fpname = aFootprint->GetFPID().GetLibItemName().wx_str();
148 m_keywords = aFootprint->GetKeywords();
149 m_doc = aFootprint->GetDescription();
150 m_loaded = true;
151 }
152};
153
154
155void FOOTPRINT_EDIT_FRAME::UpdateLibraryTree( const wxDataViewItem& aTreeItem,
156 FOOTPRINT* aFootprint )
157{
158 wxCHECK( aFootprint, /* void */ );
159
160 BASIC_FOOTPRINT_INFO footprintInfo( aFootprint );
161
162 if( aTreeItem.IsOk() ) // Can be not found in tree if the current footprint is imported
163 // from file therefore not yet in tree.
164 {
165 static_cast<LIB_TREE_NODE_LIB_ID*>( aTreeItem.GetID() )->Update( &footprintInfo );
167 }
168}
169
170
172{
173 LIB_ID oldFPID = aFootprint->GetFPID();
174
175 DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR dialog( this, aFootprint );
176 dialog.ShowModal();
177
178 // Update library tree and title in case of a name change
179 wxDataViewItem treeItem = m_adapter->FindItem( oldFPID );
180 UpdateLibraryTree( treeItem, aFootprint );
181 UpdateTitle();
182
184}
185
186
188{
189 switch( aItem->Type() )
190 {
191 case PCB_BITMAP_T:
193 break;
194
195 case PCB_PAD_T:
196 ShowPadPropertiesDialog( static_cast<PAD*>( aItem ) );
197 break;
198
199 case PCB_FOOTPRINT_T:
200 editFootprintProperties( static_cast<FOOTPRINT*>( aItem ) );
201 GetCanvas()->Refresh();
202 break;
203
204 case PCB_FP_TEXT_T:
206 break;
207
208 case PCB_FP_TEXTBOX_T:
210 break;
211
212 case PCB_FP_SHAPE_T :
214 break;
215
221 {
222 DIALOG_DIMENSION_PROPERTIES dlg( this, static_cast<PCB_DIMENSION_BASE*>( aItem ) );
223 dlg.ShowQuasiModal();
224 break;
225 }
226
227 case PCB_FP_ZONE_T:
228 {
229 ZONE* zone = static_cast<ZONE*>( aItem );
230 bool success = false;
231 ZONE_SETTINGS zoneSettings;
232
233 zoneSettings << *static_cast<ZONE*>( aItem );
234
235 if( zone->GetIsRuleArea() )
236 {
237 success = InvokeRuleAreaEditor( this, &zoneSettings ) == wxID_OK;
238 }
239 else if( zone->IsOnCopperLayer() )
240 {
241 success = InvokeCopperZonesEditor( this, &zoneSettings ) == wxID_OK;
242 }
243 else
244 {
245 success = InvokeNonCopperZonesEditor( this, &zoneSettings ) == wxID_OK;
246 }
247
248 if( success )
249 {
250 BOARD_COMMIT commit( this );
251 commit.Modify( zone );
252 commit.Push( _( "Edit Zone" ) );
253 zoneSettings.ExportSetting( *static_cast<ZONE*>( aItem ) );
254 }
255
256 break;
257 }
258
259 case PCB_GROUP_T:
261 break;
262
263 case PCB_MARKER_T:
264 m_toolManager->GetTool<FOOTPRINT_EDITOR_CONTROL>()->CrossProbe( static_cast<PCB_MARKER*>( aItem ) );
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
276{
278}
279
280
282{
284
286
287 m_toolManager->RunAction( PCB_ACTIONS::layerChanged ); // notify other tools
288 GetCanvas()->SetFocus(); // allow capture of hotkeys
289 GetCanvas()->SetHighContrastLayer( aLayer );
290 GetCanvas()->Refresh();
291}
292
293
294bool FOOTPRINT_EDIT_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, int aCtl )
295{
296 if( !Clear_Pcb( true ) )
297 return false; // this command is aborted
298
300 ImportFootprint( aFileSet[ 0 ] );
301
302 if( GetBoard()->GetFirstFootprint() )
304
305 GetScreen()->SetContentModified( false );
306 Zoom_Automatique( false );
307 GetCanvas()->Refresh();
308
309 return true;
310}
311
312
314{
315 const std::string& payload = mail.GetPayload();
316
317 switch( mail.Command() )
318 {
319 case MAIL_FP_EDIT:
320 if( !payload.empty() )
321 {
322 wxFileName fpFileName( payload );
323 wxString libNickname;
324 wxString msg;
325
326 FP_LIB_TABLE* libTable = Prj().PcbFootprintLibs();
327 const LIB_TABLE_ROW* libTableRow = libTable->FindRowByURI( fpFileName.GetPath() );
328
329 if( !libTableRow )
330 {
331 msg.Printf( _( "The current configuration does not include a library named '%s'.\n"
332 "Use Manage Footprint Libraries to edit the configuration." ),
333 fpFileName.GetPath() );
334 DisplayErrorMessage( this, _( "Library not found in footprint library table." ),
335 msg );
336 break;
337 }
338
339 libNickname = libTableRow->GetNickName();
340
341 if( !libTable->HasLibrary( libNickname, true ) )
342 {
343 msg.Printf( _( "The library '%s' is not enabled in the current configuration.\n"
344 "Use Manage Footprint Libraries to edit the configuration." ),
345 libNickname );
346 DisplayErrorMessage( this, _( "Footprint library not enabled." ), msg );
347 break;
348 }
349
350 LIB_ID fpId( libNickname, fpFileName.GetName() );
351
352 if( m_treePane )
353 {
355 wxCommandEvent event( SYMBOL_SELECTED );
356 wxPostEvent( m_treePane, event );
357 }
358 }
359
360 break;
361
362 default:
363 break;
364 }
365}
@ NORMAL
Inactive layers are shown normally (no high-contrast mode)
void SetContentModified(bool aModified=true)
Definition: base_screen.h:59
BASIC_FOOTPRINT_INFO(FOOTPRINT *aFootprint)
virtual void Push(const wxString &aMessage=wxT("A commit"), 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:58
FOOTPRINT * GetFirstFootprint() const
Get the first footprint on the board or nullptr.
Definition: board.h:397
COLOR4D GetColor(int aLayer) const
COMMIT & Modify(EDA_ITEM *aItem)
Create an undo entry for an item that has been already modified.
Definition: commit.h:103
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
Update the board display after modifying it by a python script (note: it is automatically called by a...
void SetFocus() override
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: eda_item.h:141
virtual wxString GetClass() const =0
Return the class name.
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:163
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 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)
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
bool Clear_Pcb(bool aQuery)
Delete all and reinitialize the current board.
Definition: initpcb.cpp:97
PCB_LAYER_BOX_SELECTOR * m_selLayerBox
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
unsigned GetPadCount(INCLUDE_NPTH_T aIncludeNPTH=INCLUDE_NPTH_T(INCLUDE_NPTH)) const
Return the number of pads.
Definition: footprint.cpp:1211
wxString GetDescription() const
Definition: footprint.h:218
const LIB_ID & GetFPID() const
Definition: footprint.h:212
unsigned GetUniquePadCount(INCLUDE_NPTH_T aIncludeNPTH=INCLUDE_NPTH_T(INCLUDE_NPTH)) const
Return the number of unique non-blank pads.
Definition: footprint.cpp:1230
FP_TEXT & Value()
read/write accessors:
Definition: footprint.h:567
wxString GetKeywords() const
Definition: footprint.h:221
FP_TEXT & Reference()
Definition: footprint.h:568
void SetType(TEXT_TYPE aType)
Definition: fp_text.h:119
@ TEXT_is_REFERENCE
Definition: fp_text.h:49
@ TEXT_is_VALUE
Definition: fp_text.h:50
int GetLength() const
Definition: fp_text.cpp:197
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:39
std::string & GetPayload()
Return the payload, which can be any text but it typically self identifying s-expression.
Definition: kiway_express.h:57
MAIL_T Command()
Returns the MAIL_T associated with this mail.
Definition: kiway_express.h:49
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
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:313
void CenterLibId(const LIB_ID &aLibId)
Ensure that an item is visible (preferably centered).
Definition: lib_tree.cpp:236
void SelectLibId(const LIB_ID &aLibId)
Select an item in the tree widget.
Definition: lib_tree.cpp:230
void ExpandLibId(const LIB_ID &aLibId)
Expand and item i the tree widget.
Definition: lib_tree.cpp:248
Definition: pad.h:59
static TOOL_ACTION layerChanged
Definition: pcb_actions.h:328
static TOOL_ACTION groupProperties
Definition: pcb_actions.h:253
int ShowTextBoxPropertiesDialog(BOARD_ITEM *aText)
void ShowGraphicItemPropertiesDialog(BOARD_ITEM *aItem)
APPEARANCE_CONTROLS * m_appearancePanel
void ShowBitmapPropertiesDialog(BOARD_ITEM *aBitmap)
Set the angle used for rotate operations.
void ShowTextPropertiesDialog(BOARD_ITEM *aText)
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.
Definition: pcb_dimension.h:96
virtual void SetHighContrastLayer(int aLayer) override
SetHighContrastLayer(), with some extra smarts for PCB.
virtual FP_LIB_TABLE * PcbFootprintLibs(KIWAY &aKiway)
Return the table of footprint libraries.
Definition: project.cpp:324
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:170
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Run the specified action.
Definition: tool_manager.h:142
wxString wx_str() const
Definition: utf8.cpp:46
ZONE_SETTINGS handles zones parameters.
Definition: zone_settings.h:70
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:57
bool GetIsRuleArea() const
Accessors to parameters used in Rule Area zones:
Definition: zone.h:697
bool IsOnCopperLayer() const override
Definition: zone.cpp:254
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:325
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:57
@ LAYER_GRID
Definition: layer_ids.h:205
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:59
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:932
@ MAIL_FP_EDIT
Definition: mail_type.h:54
@ PCB_FP_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition: typeinfo.h:95
@ PCB_FP_SHAPE_T
class FP_SHAPE, a footprint edge
Definition: typeinfo.h:94
@ PCB_FP_TEXTBOX_T
class FP_TEXTBOX, wrapped text in a footprint
Definition: typeinfo.h:93
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:115
@ PCB_FP_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition: typeinfo.h:97
@ PCB_MARKER_T
class PCB_MARKER, a marker used to show something
Definition: typeinfo.h:104
@ PCB_FP_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition: typeinfo.h:99
@ PCB_FP_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:96
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition: typeinfo.h:86
@ PCB_FP_ZONE_T
class ZONE, managed by a footprint
Definition: typeinfo.h:100
@ PCB_BITMAP_T
class PCB_BITMAP, bitmap on a layer
Definition: typeinfo.h:89
@ PCB_FP_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition: typeinfo.h:98
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition: typeinfo.h:87
@ PCB_FP_TEXT_T
class FP_TEXT, text in a footprint
Definition: typeinfo.h:92
VECTOR2< double > VECTOR2D
Definition: vector2d.h:617