KiCad PCB EDA Suite
dialog_edit_module_for_Modedit.cpp
Go to the documentation of this file.
1 
7 /*
8  * This program source code file is part of KiCad, a free EDA CAD application.
9  *
10  * Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
11  * Copyright (C) 2015 Dick Hollenbeck, dick@softplc.com
12  * Copyright (C) 2008-2016 Wayne Stambaugh <stambaughw@verizon.net>
13  * Copyright (C) 2004-2016 KiCad Developers, see AUTHORS.txt for contributors.
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, you may find one here:
27  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
28  * or you may search the http://www.gnu.org website for the version 2 license,
29  * or you may write to the Free Software Foundation, Inc.,
30  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
31  */
32 
33 
34 #include <fctsys.h>
35 #include <class_drawpanel.h>
36 #include <confirm.h>
37 #include <pcbnew.h>
38 #include <kiface_i.h>
39 #include <gestfich.h>
40 #include <3d_viewer.h>
41 #include <wxPcbStruct.h>
42 #include <base_units.h>
43 #include <macros.h>
44 #include <validators.h>
45 #include <kicad_string.h>
46 #include <board_commit.h>
47 #include <bitmaps.h>
48 
49 #include <class_module.h>
50 #include <class_text_mod.h>
51 #include <module_editor_frame.h>
54 #include <pgm_base.h>
57 #include "3d_cache/3d_cache.h"
59 
60 size_t DIALOG_MODULE_MODULE_EDITOR::m_page = 0; // remember the last open page during session
61 
62 
64  MODULE* aModule ) :
66 {
67  m_parent = aParent;
68  m_currentModule = aModule;
69 
70  // Give an icon
71  wxIcon icon;
72  icon.CopyFromBitmap( KiBitmap( icon_modedit_xpm ) );
73  SetIcon( icon );
74 
75  aParent->Prj().Get3DCacheManager()->GetResolver()->SetProgramBase( &Pgm() );
76 
77  m_currentModuleCopy = new MODULE( *aModule );
78 
80  aParent->Prj().Get3DCacheManager(),
82  &aParent->Settings().Colors(),
83  &m_shapes3D_list );
84 
85  bLowerSizer3D->Add( m_PreviewPane, 1, wxEXPAND, 5 );
86 
89 
90  m_NoteBook->SetSelection( m_page );
91 
92  m_sdbSizerStdButtonsOK->SetDefault();
93 
94  Layout();
95 
96 }
97 
98 
100 {
101  m_shapes3D_list.clear();
102 
103  // free the memory used by all models, otherwise models which were
104  // browsed but not used would consume memory
105  Prj().Get3DCacheManager()->FlushCache( false );
106 
107  // the GL canvas has to be visible before it is destroyed
108  m_page = m_NoteBook->GetSelection();
109  m_NoteBook->SetSelection( 1 );
110 
111  delete m_referenceCopy;
112  m_referenceCopy = NULL; // just in case, to avoid double-free
113 
114  delete m_valueCopy;
115  m_valueCopy = NULL;
116 
117  delete m_PreviewPane;
118  m_PreviewPane = NULL; // just in case, to avoid double-free
119 
120  // this is already deleted by the board used on preview pane so
121  // no need to delete here
122  // delete m_currentModuleCopy;
123  // m_currentModuleCopy = NULL;
124 }
125 
126 
128 {
129  SetFocus();
130 
131  // Display the default path, given by environment variable KISYS3DMOD
132  wxString default_path;
133  wxGetEnv( KISYS3DMOD, &default_path );
134 #ifdef __WINDOWS__
135  default_path.Replace( wxT( "/" ), wxT( "\\" ) );
136 #endif
137 
139 
140  // Init 3D shape list
141  m_3D_ShapeNameListBox->Clear();
142  std::list<S3D_INFO>::iterator sM = m_currentModule->Models().begin();
143  std::list<S3D_INFO>::iterator eM = m_currentModule->Models().end();
144  m_shapes3D_list.clear();
145 
146 
147  wxString origPath;
148  wxString alias;
149  wxString shortPath;
150  S3D_FILENAME_RESOLVER* res = Prj().Get3DCacheManager()->GetResolver();
151 
152  while( sM != eM )
153  {
154  m_shapes3D_list.push_back( *sM );
155  origPath = sM->m_Filename;
156 
157  if( res && res->SplitAlias( origPath, alias, shortPath ) )
158  {
159  origPath = alias;
160  origPath.append( wxT( ":" ) );
161  origPath.append( shortPath );
162  }
163 
164  m_3D_ShapeNameListBox->Append( origPath );
165  ++sM;
166  }
167 
168  m_DocCtrl->SetValue( m_currentModule->GetDescription() );
169  m_KeywordCtrl->SetValue( m_currentModule->GetKeywords() );
174  m_ReferenceCtrl->SetValue( m_referenceCopy->GetText() );
175  m_ValueCtrl->SetValue( m_valueCopy->GetText() );
176 
180 
181  m_AttributsCtrl->SetItemToolTip( 0, _( "Use this attribute for most non SMD components" ) );
182  m_AttributsCtrl->SetItemToolTip( 1,
183  _( "Use this attribute for SMD components.\n"
184  "Only components with this option are put in the footprint position list file" ) );
185  m_AttributsCtrl->SetItemToolTip( 2,
186  _( "Use this attribute for \"virtual\" components drawn on board\n"
187  "like an edge connector (old ISA PC bus for instance)" ) );
188 
189  // Controls on right side of the dialog
190  switch( m_currentModule->GetAttributes() & 255 )
191  {
192  case 0:
193  m_AttributsCtrl->SetSelection( 0 );
194  break;
195 
196  case MOD_CMS:
197  m_AttributsCtrl->SetSelection( 1 );
198  break;
199 
200  case MOD_VIRTUAL:
201  m_AttributsCtrl->SetSelection( 2 );
202  break;
203 
204  default:
205  m_AttributsCtrl->SetSelection( 0 );
206  break;
207  }
208 
209  m_AutoPlaceCtrl->SetSelection( (m_currentModule->IsLocked()) ? 1 : 0 );
210  m_AutoPlaceCtrl->SetItemToolTip( 0, _( "Enable hotkey move commands and Auto Placement" ) );
211  m_AutoPlaceCtrl->SetItemToolTip( 1, _( "Disable hotkey move commands and Auto Placement" ) );
212 
215 
216  // Initialize dialog relative to masks clearances
220 
221  wxString msg;
224 
225  // These 2 parameters are usually < 0, so prepare entering a negative value, if current is 0
227 
229  m_SolderPasteMarginCtrl->SetValue( wxT( "-" ) + m_SolderPasteMarginCtrl->GetValue() );
230 
232  msg.Printf( wxT( "-%f" ), m_currentModule->GetLocalSolderPasteMarginRatio() * 100.0 );
233  else
234  msg.Printf( wxT( "%f" ), m_currentModule->GetLocalSolderPasteMarginRatio() * 100.0 );
235 
236  m_SolderPasteMarginRatioCtrl->SetValue( msg );
237 
238  // Add solder paste margin ration in per cent
239  // for the usual default value 0.0, display -0.0 (or -0,0 in some countries)
240  msg.Printf( wxT( "%f" ), m_currentModule->GetLocalSolderPasteMarginRatio() * 100.0 );
241 
243  msg[0] == '0') // Sometimes Printf adds a sign if the value is very small (0.0)
244  m_SolderPasteMarginRatioCtrl->SetValue( wxT( "-" ) + msg );
245  else
246  m_SolderPasteMarginRatioCtrl->SetValue( msg );
247 
248  // if m_3D_ShapeNameListBox is not empty, preselect first 3D shape
249  if( m_3D_ShapeNameListBox->GetCount() > 0 )
250  {
253  if( m_PreviewPane )
255  }
256  else
257  {
258  if( m_PreviewPane )
259  m_PreviewPane->ResetModelData( true );
260  }
261 
262  // We have modified the UI, so call Fit() for m_Panel3D
263  // to be sure the m_Panel3D sizers are initialized before opening the dialog
264  m_Panel3D->GetSizer()->Fit( m_Panel3D );
265 }
266 
267 
269 {
271 
272  if( m_lastSelected3DShapeIndex < 0 ) // happens under wxGTK when deleting an item in m_3D_ShapeNameListBox wxListBox
273  {
274  if( m_PreviewPane )
276  return;
277  }
278 
279  if( m_lastSelected3DShapeIndex >= (int)m_shapes3D_list.size() )
280  {
281  wxMessageBox( wxT( "On3DShapeNameSelected() error" ) );
283 
284  if( m_PreviewPane )
286 
287  return;
288  }
289 
291 }
292 
293 
295 {
296  int ii = m_3D_ShapeNameListBox->GetSelection();
297 
298  if( ii < 0 )
299  {
300  if( m_PreviewPane )
301  m_PreviewPane->ResetModelData( true );
302 
303  return;
304  }
305 
306  m_shapes3D_list.erase( m_shapes3D_list.begin() + ii );
307  m_3D_ShapeNameListBox->Delete( ii );
308 
309  if( m_3D_ShapeNameListBox->GetCount() > 0 )
310  {
311  if( ii > 0 )
313  else
315 
317 
318  if( m_PreviewPane )
320  }
321  else
322  {
323  if( m_PreviewPane )
324  m_PreviewPane->ResetModelData( true );
325  }
326 
327  return;
328 }
329 
330 
332 {
333  int idx = m_3D_ShapeNameListBox->GetSelection();
334 
335  if( idx < 0 )
336  return;
337 
338  // Edit filename
339  wxString filename = m_3D_ShapeNameListBox->GetStringSelection();
340  wxTextEntryDialog dlg( this, wxEmptyString, wxEmptyString, filename );
341 
342  bool hasAlias;
343  S3D_FILENAME_RESOLVER* res = Prj().Get3DCacheManager()->GetResolver();
344 
345  if( dlg.ShowModal() != wxID_OK )
346  return;
347 
348  filename = dlg.GetValue();
349 
350  if( filename.empty() )
351  return;
352 
353  if( !res->ValidateFileName( filename, hasAlias ) )
354  {
355  wxString msg = _( "Invalid filename: " );
356  msg.append( filename );
357  wxMessageBox( msg, _( "Edit 3D file name" ) );
358 
359  return;
360  }
361 
362  m_3D_ShapeNameListBox->SetString( idx, filename );
363 
364  // if the user has specified an alias in the name then prepend ':'
365  if( hasAlias )
366  filename.insert( 0, wxT( ":" ) );
367 
368  #ifdef __WINDOWS__
369  // In Kicad files, filenames and paths are stored using Unix notation
370  filename.Replace( wxT( "\\" ), wxT( "/" ) );
371  #endif
372 
373  m_shapes3D_list[idx].m_Filename = filename;
374 
375  // This assumes that the index didn't change and will just update the filename
376  if( m_PreviewPane )
377  m_PreviewPane->UpdateModelName( filename );
378 
379  return;
380 }
381 
382 
384 {
385  PROJECT& prj = Prj();
386  S3D_INFO model;
387 
388  wxString initialpath = prj.GetRString( PROJECT::VIEWER_3D_PATH );
389  wxString sidx = prj.GetRString( PROJECT::VIEWER_3D_FILTER_INDEX );
390  int filter = 0;
391 
392  if( !sidx.empty() )
393  {
394  long tmp;
395  sidx.ToLong( &tmp );
396 
397  if( tmp > 0 && tmp <= 0x7FFFFFFF )
398  filter = (int) tmp;
399  }
400 
401  if( !S3D::Select3DModel( m_PreviewPane, Prj().Get3DCacheManager(),
402  initialpath, filter, &model ) || model.m_Filename.empty() )
403  {
404  return;
405  }
406 
407  prj.SetRString( PROJECT::VIEWER_3D_PATH, initialpath );
408  sidx = wxString::Format( wxT( "%i" ), filter );
410  wxString origPath = model.m_Filename;
411  wxString alias;
412  wxString shortPath;
413  S3D_FILENAME_RESOLVER* res = Prj().Get3DCacheManager()->GetResolver();
414 
415  if( res && res->SplitAlias( origPath, alias, shortPath ) )
416  {
417  origPath = alias;
418  origPath.append( wxT( ":" ) );
419  origPath.append( shortPath );
420  }
421 
422  m_3D_ShapeNameListBox->Append( origPath );
423 
424  #ifdef __WINDOWS__
425  // In Kicad files, filenames and paths are stored using Unix notation
426  model.m_Filename.Replace( wxT( "\\" ), wxT( "/" ) );
427  #endif
428 
429  m_shapes3D_list.push_back( model );
432 
433  if( m_PreviewPane )
435 
436  return;
437 }
438 
439 
441 {
442  wxString msg;
443  // First, test for invalid chars in module name
444  wxString footprintName = m_FootprintNameCtrl->GetValue();
445 
446  if( ! footprintName.IsEmpty() )
447  {
448  if( ! MODULE::IsLibNameValid( footprintName ) )
449  {
450  msg.Printf( _( "Error:\n"
451  "one of invalid chars <%s> found\nin <%s>" ),
453  GetChars( footprintName ) );
454 
455  DisplayError( NULL, msg );
456 
457  return false;
458  }
459  }
460 
461  if( !m_PreviewPane->ValidateWithMessage( msg ) )
462  {
463  DisplayError( NULL, msg );
464  return false;
465  }
466 
467  BOARD_COMMIT commit( m_parent );
468  commit.Modify( m_currentModule );
469 
470  m_currentModule->SetLocked( m_AutoPlaceCtrl->GetSelection() == 1 );
471 
472  switch( m_AttributsCtrl->GetSelection() )
473  {
474  case 0:
476  break;
477 
478  case 1:
480  break;
481 
482  case 2:
484  break;
485  }
486 
489  m_currentModule->SetDescription( m_DocCtrl->GetValue() );
490  m_currentModule->SetKeywords( m_KeywordCtrl->GetValue() );
491 
492  // Init footprint name in library
493  if( ! footprintName.IsEmpty() )
494  {
495  m_currentFPID.SetLibItemName( footprintName, false );
497  }
498 
499  // Init Fields:
500  TEXTE_MODULE& reference = m_currentModule->Reference();
501  reference = *m_referenceCopy;
502  TEXTE_MODULE& value = m_currentModule->Value();
503  value = *m_valueCopy;
504 
505  // Initialize masks clearances
509  double dtmp;
510  msg = m_SolderPasteMarginRatioCtrl->GetValue();
511  msg.ToDouble( &dtmp );
512 
513  // A -50% margin ratio means no paste on a pad, the ratio must be >= -50 %
514  if( dtmp < -50.0 )
515  dtmp = -50.0;
516 
517  // A margin ratio is always <= 0
518  if( dtmp > 0.0 )
519  dtmp = 0.0;
520 
522 
523  std::list<S3D_INFO>* draw3D = &m_currentModule->Models();
524  draw3D->clear();
525  draw3D->insert( draw3D->end(), m_shapes3D_list.begin(), m_shapes3D_list.end() );
526 
528 
529  commit.Push( _( "Modify module properties" ) );
530 
531  return true;
532 }
533 
534 
536 {
541  m_ReferenceCtrl->SetValue( m_referenceCopy->GetText() );
542 }
543 
544 
545 void DIALOG_MODULE_MODULE_EDITOR::OnEditValue(wxCommandEvent& event)
546 {
551  m_ValueCtrl->SetValue( m_valueCopy->GetText() );
552 }
553 
554 
555 void DIALOG_MODULE_MODULE_EDITOR::Cfg3DPath( wxCommandEvent& event )
556 {
557  if( S3D::Configure3DPaths( this, Prj().Get3DCacheManager()->GetResolver() ) )
558  if( m_lastSelected3DShapeIndex >= 0 )
559  if( m_PreviewPane )
561 }
void ResetModelData(bool aReloadPreviewModule=false)
ResetModelData - Clear the values and reload the preview board.
void SetPlacementCost90(int aCost)
Definition: class_module.h:534
void Remove3DShape(wxCommandEvent &event) override
Definition of class FOOTPRINT_EDIT_FRAME.
COMMIT & Modify(EDA_ITEM *aItem)
Modifies a given item in the model.
Definition: commit.h:103
TEXTE_MODULE & Reference()
Definition: class_module.h:463
Class PROJECT holds project specific data.
Definition: project.h:56
const wxPoint & GetTextPos() const
Definition: eda_text.h:224
void SetLocalClearance(int aClearance)
Definition: class_module.h:180
This file is part of the common library TODO brief description.
DIALOG_MODULE_MODULE_EDITOR(FOOTPRINT_EDIT_FRAME *aParent, MODULE *aModule)
void SetPlacementCost180(int aCost)
Definition: class_module.h:531
Implementation of conversion functions that require both schematic and board internal units...
This file is part of the common library.
int GetLocalClearance() const
Definition: class_module.h:179
void CalculateBoundingBox()
Function CalculateBoundingBox calculates the bounding box in board coordinates.
bool ValidateWithMessage(wxString &aErrorMessage)
verify X,Y and Z scale factors are acceptable (> 0.001 and < 1000.0)
Set for modules listed in the automatic insertion list (usually SMD footprints)
Definition: class_module.h:77
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: kicad.cpp:65
static const wxChar * StringLibNameInvalidChars(bool aUserReadable)
static function StringLibNameInvalidChars Test for validity of the name in a library of the footprint...
std::list< S3D_INFO > & Models()
Definition: class_module.h:150
int GetPlacementCost90() const
Definition: class_module.h:533
void SetLocalSolderPasteMarginRatio(double aRatio)
Definition: class_module.h:186
double GetLocalSolderPasteMarginRatio() const
Definition: class_module.h:185
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true) override
Executes the changes.
This file contains miscellaneous commonly used macros and functions.
Class FILE_NAME_CHAR_VALIDATOR.
Definition: validators.h:43
bool Configure3DPaths(wxWindow *aParent, S3D_FILENAME_RESOLVER *aResolver)
bool ValidateFileName(const wxString &aFileName, bool &hasAlias)
Function ValidateName returns true if the given path is a valid aliased relative path.
bool SplitAlias(const wxString &aFileName, wxString &anAlias, wxString &aRelPath)
Function SplitAlias returns true if the given name contains an alias and populates the string anAlias...
const LIB_ID & GetFPID() const
Definition: class_module.h:164
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Function KiBitmap constructs a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:36
void SetModelDataIdx(int idx, bool aReloadPreviewModule=false)
SetModelDataIdx - This will set the index of the INFO list that was set on the parent.
Footprint text class description.
Defines a panel which is to be added to a wxFileDialog via SetExtraControl(); The panel shows a previ...
void InstallTextModOptionsFrame(TEXTE_MODULE *TextMod, wxDC *DC)
void SetParent(EDA_ITEM *aParent)
Definition: base_struct.h:213
bool Select3DModel(wxWindow *aParent, S3D_CACHE *aCache, wxString &prevModelSelectDir, int &prevModelWildcard, S3D_INFO *aModel)
TEXTE_MODULE & Value()
read/write accessors:
Definition: class_module.h:462
PCB_GENERAL_SETTINGS & Settings()
const UTF8 & GetLibItemName() const
Function GetLibItemName.
Definition: lib_id.h:129
const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:130
wxString GetAbbreviatedUnitsLabel(EDA_UNITS_T aUnit)
Definition: base_units.cpp:479
#define KISYS3DMOD
A variable name whose value holds the path of 3D shape files.
Definition: 3d_viewer.h:38
The common library.
int GetAttributes() const
Definition: class_module.h:197
const wxString & GetKeywords() const
Definition: class_module.h:170
int GetLocalSolderMaskMargin() const
Definition: class_module.h:176
int ValueFromTextCtrl(const wxTextCtrl &aTextCtr)
Convert the number Value in a string according to the internal units and the selected unit (g_UserUni...
Definition: base_units.cpp:384
bool IsLocked() const override
Function IsLocked.
Definition: class_module.h:236
void Cfg3DPath(wxCommandEvent &event) override
void SetLocalSolderMaskMargin(int aMargin)
Definition: class_module.h:177
int SetLibItemName(const UTF8 &aLibItemName, bool aTestForRev=true)
Function SetLibItemName.
Definition: lib_id.cpp:232
COLORS_DESIGN_SETTINGS & Colors()
int GetLocalSolderPasteMargin() const
Definition: class_module.h:182
VTBL_ENTRY void SetRString(RSTRING_T aStringId, const wxString &aString)
Function SetRString stores a "retained string", which is any session and project specific string iden...
Definition: project.cpp:162
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56
static bool IsLibNameValid(const wxString &aName)
static function IsLibNameValid Test for validity of a name of a footprint to be used in a footprint l...
void PutValueInLocalUnits(wxTextCtrl &aTextCtr, int aValue)
Function PutValueInLocalUnits converts aValue from internal units to user units and append the units ...
Definition: base_units.cpp:265
void SetLocalSolderPasteMargin(int aMargin)
Definition: class_module.h:183
VTBL_ENTRY const wxString & GetRString(RSTRING_T aStringId)
Function GetRString returns a "retained string", which is any session and project specific string ide...
Definition: project.cpp:177
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
see class PGM_BASE
void SetLocked(bool isLocked) override
Function SetLocked sets the MODULE_is_LOCKED bit in the m_ModuleStatus.
Definition: class_module.h:246
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
const wxString & GetDescription() const
Definition: class_module.h:167
provides an extensible class to resolve 3D model paths.
Virtual component: when created by copper shapes on board (Like edge card connectors, mounting hole...)
Definition: class_module.h:79
defines the display data cache manager for 3D models
void SetKeywords(const wxString &aKeywords)
Definition: class_module.h:171
Module description (excepted pads)
const UTF8 & GetLibNickname() const
Function GetLibNickname.
Definition: lib_id.h:108
void SetCrossHairPosition(const wxPoint &aPosition, bool aSnapToGrid=true)
Function SetCrossHairPosition sets the screen cross hair position to aPosition in logical (drawing) u...
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:71
wxPoint GetCrossHairPosition(bool aInvertY=false) const
Function GetCrossHairPosition return the current cross hair position in logical (drawing) coordinates...
void OnEditValue(wxCommandEvent &event) override
int GetPlacementCost180() const
Definition: class_module.h:530
void On3DShapeNameSelected(wxCommandEvent &event) override
void OnEditReference(wxCommandEvent &event) override
void SetFPID(const LIB_ID &aFPID)
Definition: class_module.h:165
Custom text control validator definitions.
Class DIALOG_MODULE_MODULE_EDITOR_BASE.
Declaration of the eda_3d_viewer class.
void UpdateModelName(wxString const &aModel)
void SetAttributes(int aAttributes)
Definition: class_module.h:198
wxString m_Filename
The 3D shape filename in 3D library.
Definition: 3d_info.h:45
void SetDescription(const wxString &aDoc)
Definition: class_module.h:168