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-2017 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  &m_shapes3D_list );
83 
84  bLowerSizer3D->Add( m_PreviewPane, 1, wxEXPAND, 5 );
85 
88 
89  m_NoteBook->SetSelection( m_page );
90 
91  m_sdbSizerStdButtonsOK->SetDefault();
92 
93  Layout();
94 
95 }
96 
97 
99 {
100  m_shapes3D_list.clear();
101 
102  // free the memory used by all models, otherwise models which were
103  // browsed but not used would consume memory
104  Prj().Get3DCacheManager()->FlushCache( false );
105 
106  // the GL canvas has to be visible before it is destroyed
107  m_page = m_NoteBook->GetSelection();
108  m_NoteBook->SetSelection( 1 );
109 
110  delete m_referenceCopy;
111  m_referenceCopy = NULL; // just in case, to avoid double-free
112 
113  delete m_valueCopy;
114  m_valueCopy = NULL;
115 
116  delete m_PreviewPane;
117  m_PreviewPane = NULL; // just in case, to avoid double-free
118 
119  // this is already deleted by the board used on preview pane so
120  // no need to delete here
121  // delete m_currentModuleCopy;
122  // m_currentModuleCopy = NULL;
123 }
124 
125 
127 {
128  SetFocus();
129 
130  // Display the default path, given by environment variable KISYS3DMOD
131  wxString default_path;
132  wxGetEnv( KISYS3DMOD, &default_path );
133 #ifdef __WINDOWS__
134  default_path.Replace( wxT( "/" ), wxT( "\\" ) );
135 #endif
136 
138 
139  // Init 3D shape list
140  m_3D_ShapeNameListBox->Clear();
141  std::list<S3D_INFO>::iterator sM = m_currentModule->Models().begin();
142  std::list<S3D_INFO>::iterator eM = m_currentModule->Models().end();
143  m_shapes3D_list.clear();
144 
145 
146  wxString origPath;
147  wxString alias;
148  wxString shortPath;
149  S3D_FILENAME_RESOLVER* res = Prj().Get3DCacheManager()->GetResolver();
150 
151  while( sM != eM )
152  {
153  m_shapes3D_list.push_back( *sM );
154  origPath = sM->m_Filename;
155 
156  if( res && res->SplitAlias( origPath, alias, shortPath ) )
157  {
158  origPath = alias;
159  origPath.append( wxT( ":" ) );
160  origPath.append( shortPath );
161  }
162 
163  m_3D_ShapeNameListBox->Append( origPath );
164  ++sM;
165  }
166 
167  m_DocCtrl->SetValue( m_currentModule->GetDescription() );
168  m_KeywordCtrl->SetValue( m_currentModule->GetKeywords() );
173  m_ReferenceCtrl->SetValue( m_referenceCopy->GetText() );
174  m_ValueCtrl->SetValue( m_valueCopy->GetText() );
175 
179 
180  m_AttributsCtrl->SetItemToolTip( 0, _( "Use this attribute for most non SMD components" ) );
181  m_AttributsCtrl->SetItemToolTip( 1,
182  _( "Use this attribute for SMD components.\n"
183  "Only components with this option are put in the footprint position list file" ) );
184  m_AttributsCtrl->SetItemToolTip( 2,
185  _( "Use this attribute for \"virtual\" components drawn on board\n"
186  "like an edge connector (old ISA PC bus for instance)" ) );
187 
188  // Controls on right side of the dialog
189  switch( m_currentModule->GetAttributes() & 255 )
190  {
191  case 0:
192  m_AttributsCtrl->SetSelection( 0 );
193  break;
194 
195  case MOD_CMS:
196  m_AttributsCtrl->SetSelection( 1 );
197  break;
198 
199  case MOD_VIRTUAL:
200  m_AttributsCtrl->SetSelection( 2 );
201  break;
202 
203  default:
204  m_AttributsCtrl->SetSelection( 0 );
205  break;
206  }
207 
208  m_AutoPlaceCtrl->SetSelection( (m_currentModule->IsLocked()) ? 1 : 0 );
209  m_AutoPlaceCtrl->SetItemToolTip( 0, _( "Enable hotkey move commands and Auto Placement" ) );
210  m_AutoPlaceCtrl->SetItemToolTip( 1, _( "Disable hotkey move commands and Auto Placement" ) );
211 
214 
215  // Initialize dialog relative to masks clearances
219 
220  wxString msg;
223 
224  // These 2 parameters are usually < 0, so prepare entering a negative value, if current is 0
226 
228  m_SolderPasteMarginCtrl->SetValue( wxT( "-" ) + m_SolderPasteMarginCtrl->GetValue() );
229 
231  msg.Printf( wxT( "-%f" ), m_currentModule->GetLocalSolderPasteMarginRatio() * 100.0 );
232  else
233  msg.Printf( wxT( "%f" ), m_currentModule->GetLocalSolderPasteMarginRatio() * 100.0 );
234 
235  m_SolderPasteMarginRatioCtrl->SetValue( msg );
236 
237  // Add solder paste margin ration in per cent
238  // for the usual default value 0.0, display -0.0 (or -0,0 in some countries)
239  msg.Printf( wxT( "%f" ), m_currentModule->GetLocalSolderPasteMarginRatio() * 100.0 );
240 
242  msg[0] == '0') // Sometimes Printf adds a sign if the value is very small (0.0)
243  m_SolderPasteMarginRatioCtrl->SetValue( wxT( "-" ) + msg );
244  else
245  m_SolderPasteMarginRatioCtrl->SetValue( msg );
246 
247  // if m_3D_ShapeNameListBox is not empty, preselect first 3D shape
248  if( m_3D_ShapeNameListBox->GetCount() > 0 )
249  {
252  if( m_PreviewPane )
254  }
255  else
256  {
257  if( m_PreviewPane )
258  m_PreviewPane->ResetModelData( true );
259  }
260 
261  // We have modified the UI, so call Fit() for m_Panel3D
262  // to be sure the m_Panel3D sizers are initialized before opening the dialog
263  m_Panel3D->GetSizer()->Fit( m_Panel3D );
264 }
265 
266 
268 {
270 
271  if( m_lastSelected3DShapeIndex < 0 ) // happens under wxGTK when deleting an item in m_3D_ShapeNameListBox wxListBox
272  {
273  if( m_PreviewPane )
275  return;
276  }
277 
278  if( m_lastSelected3DShapeIndex >= (int)m_shapes3D_list.size() )
279  {
280  wxMessageBox( wxT( "On3DShapeNameSelected() error" ) );
282 
283  if( m_PreviewPane )
285 
286  return;
287  }
288 
290 }
291 
292 
294 {
295  int ii = m_3D_ShapeNameListBox->GetSelection();
296 
297  if( ii < 0 )
298  {
299  if( m_PreviewPane )
300  m_PreviewPane->ResetModelData( true );
301 
302  return;
303  }
304 
305  m_shapes3D_list.erase( m_shapes3D_list.begin() + ii );
306  m_3D_ShapeNameListBox->Delete( ii );
307 
308  if( m_3D_ShapeNameListBox->GetCount() > 0 )
309  {
310  if( ii > 0 )
312  else
314 
316 
317  if( m_PreviewPane )
319  }
320  else
321  {
322  if( m_PreviewPane )
323  m_PreviewPane->ResetModelData( true );
324  }
325 
326  return;
327 }
328 
329 
331 {
332  int idx = m_3D_ShapeNameListBox->GetSelection();
333 
334  if( idx < 0 )
335  return;
336 
337  // Edit filename
338  wxString filename = m_3D_ShapeNameListBox->GetStringSelection();
339  wxTextEntryDialog dlg( this, wxEmptyString, wxEmptyString, filename );
340 
341  bool hasAlias;
342  S3D_FILENAME_RESOLVER* res = Prj().Get3DCacheManager()->GetResolver();
343 
344  if( dlg.ShowModal() != wxID_OK )
345  return;
346 
347  filename = dlg.GetValue();
348 
349  if( filename.empty() )
350  return;
351 
352  if( !res->ValidateFileName( filename, hasAlias ) )
353  {
354  wxString msg = _( "Invalid filename: " );
355  msg.append( filename );
356  wxMessageBox( msg, _( "Edit 3D file name" ) );
357 
358  return;
359  }
360 
361  m_3D_ShapeNameListBox->SetString( idx, filename );
362 
363  // if the user has specified an alias in the name then prepend ':'
364  if( hasAlias )
365  filename.insert( 0, wxT( ":" ) );
366 
367  #ifdef __WINDOWS__
368  // In Kicad files, filenames and paths are stored using Unix notation
369  filename.Replace( wxT( "\\" ), wxT( "/" ) );
370  #endif
371 
372  m_shapes3D_list[idx].m_Filename = filename;
373 
374  // This assumes that the index didn't change and will just update the filename
375  if( m_PreviewPane )
376  m_PreviewPane->UpdateModelName( filename );
377 
378  return;
379 }
380 
381 
383 {
384  PROJECT& prj = Prj();
385  S3D_INFO model;
386 
387  wxString initialpath = prj.GetRString( PROJECT::VIEWER_3D_PATH );
388  wxString sidx = prj.GetRString( PROJECT::VIEWER_3D_FILTER_INDEX );
389  int filter = 0;
390 
391  if( !sidx.empty() )
392  {
393  long tmp;
394  sidx.ToLong( &tmp );
395 
396  if( tmp > 0 && tmp <= 0x7FFFFFFF )
397  filter = (int) tmp;
398  }
399 
400  if( !S3D::Select3DModel( m_PreviewPane, Prj().Get3DCacheManager(),
401  initialpath, filter, &model ) || model.m_Filename.empty() )
402  {
403  return;
404  }
405 
406  prj.SetRString( PROJECT::VIEWER_3D_PATH, initialpath );
407  sidx = wxString::Format( wxT( "%i" ), filter );
409  wxString origPath = model.m_Filename;
410  wxString alias;
411  wxString shortPath;
412  S3D_FILENAME_RESOLVER* res = Prj().Get3DCacheManager()->GetResolver();
413 
414  if( res && res->SplitAlias( origPath, alias, shortPath ) )
415  {
416  origPath = alias;
417  origPath.append( wxT( ":" ) );
418  origPath.append( shortPath );
419  }
420 
421  m_3D_ShapeNameListBox->Append( origPath );
422 
423  #ifdef __WINDOWS__
424  // In Kicad files, filenames and paths are stored using Unix notation
425  model.m_Filename.Replace( wxT( "\\" ), wxT( "/" ) );
426  #endif
427 
428  m_shapes3D_list.push_back( model );
431 
432  if( m_PreviewPane )
434 
435  return;
436 }
437 
438 
440 {
441  wxString msg;
442  // First, test for invalid chars in module name
443  wxString footprintName = m_FootprintNameCtrl->GetValue();
444 
445  if( ! footprintName.IsEmpty() )
446  {
447  if( ! MODULE::IsLibNameValid( footprintName ) )
448  {
449  msg.Printf( _( "Error:\n"
450  "one of invalid chars <%s> found\nin <%s>" ),
452  GetChars( footprintName ) );
453 
454  DisplayError( NULL, msg );
455 
456  return false;
457  }
458  }
459 
460  if( !m_PreviewPane->ValidateWithMessage( msg ) )
461  {
462  DisplayError( NULL, msg );
463  return false;
464  }
465 
466  BOARD_COMMIT commit( m_parent );
467  commit.Modify( m_currentModule );
468 
469  m_currentModule->SetLocked( m_AutoPlaceCtrl->GetSelection() == 1 );
470 
471  switch( m_AttributsCtrl->GetSelection() )
472  {
473  case 0:
475  break;
476 
477  case 1:
479  break;
480 
481  case 2:
483  break;
484  }
485 
488  m_currentModule->SetDescription( m_DocCtrl->GetValue() );
489  m_currentModule->SetKeywords( m_KeywordCtrl->GetValue() );
490 
491  // Init footprint name in library
492  if( ! footprintName.IsEmpty() )
493  {
494  m_currentFPID.SetLibItemName( footprintName, false );
496  }
497 
498  // Init Fields:
499  TEXTE_MODULE& reference = m_currentModule->Reference();
500  reference = *m_referenceCopy;
501  TEXTE_MODULE& value = m_currentModule->Value();
502  value = *m_valueCopy;
503 
504  // Initialize masks clearances
508  double dtmp;
509  msg = m_SolderPasteMarginRatioCtrl->GetValue();
510  msg.ToDouble( &dtmp );
511 
512  // A -50% margin ratio means no paste on a pad, the ratio must be >= -50 %
513  if( dtmp < -50.0 )
514  dtmp = -50.0;
515 
516  // A margin ratio is always <= 0
517  if( dtmp > 0.0 )
518  dtmp = 0.0;
519 
521 
522  std::list<S3D_INFO>* draw3D = &m_currentModule->Models();
523  draw3D->clear();
524  draw3D->insert( draw3D->end(), m_shapes3D_list.begin(), m_shapes3D_list.end() );
525 
527 
528  commit.Push( _( "Modify module properties" ) );
529 
530  return true;
531 }
532 
533 
535 {
540  m_ReferenceCtrl->SetValue( m_referenceCopy->GetText() );
541 }
542 
543 
544 void DIALOG_MODULE_MODULE_EDITOR::OnEditValue(wxCommandEvent& event)
545 {
550  m_ValueCtrl->SetValue( m_valueCopy->GetText() );
551 }
552 
553 
554 void DIALOG_MODULE_MODULE_EDITOR::Cfg3DPath( wxCommandEvent& event )
555 {
556  if( S3D::Configure3DPaths( this, Prj().Get3DCacheManager()->GetResolver() ) )
557  if( m_lastSelected3DShapeIndex >= 0 )
558  if( m_PreviewPane )
560 }
void ResetModelData(bool aReloadPreviewModule=false)
ResetModelData - Clear the values and reload the preview board.
void SetPlacementCost90(int aCost)
Definition: class_module.h:527
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:456
Class PROJECT holds project specific data.
Definition: project.h:52
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes...
Definition: macros.h:53
const wxPoint & GetTextPos() const
Definition: eda_text.h:224
void SetLocalClearance(int aClearance)
Definition: class_module.h:168
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:524
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:167
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:140
int GetPlacementCost90() const
Definition: class_module.h:526
void SetLocalSolderPasteMarginRatio(double aRatio)
Definition: class_module.h:174
double GetLocalSolderPasteMarginRatio() const
Definition: class_module.h:173
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:152
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:455
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:185
const wxString & GetKeywords() const
Definition: class_module.h:158
int GetLocalSolderMaskMargin() const
Definition: class_module.h:164
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:224
void Cfg3DPath(wxCommandEvent &event) override
void SetLocalSolderMaskMargin(int aMargin)
Definition: class_module.h:165
int SetLibItemName(const UTF8 &aLibItemName, bool aTestForRev=true)
Function SetLibItemName.
Definition: lib_id.cpp:232
int GetLocalSolderPasteMargin() const
Definition: class_module.h:170
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:171
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:234
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:155
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:159
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:69
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:523
void On3DShapeNameSelected(wxCommandEvent &event) override
void OnEditReference(wxCommandEvent &event) override
void SetFPID(const LIB_ID &aFPID)
Definition: class_module.h:153
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:186
wxString m_Filename
The 3D shape filename in 3D library.
Definition: 3d_info.h:45
void SetDescription(const wxString &aDoc)
Definition: class_module.h:156