KiCad PCB EDA Suite
dialog_dxf_import.cpp
Go to the documentation of this file.
1 
6 /*
7  * This program source code file is part of KiCad, a free EDA CAD application.
8  *
9  * Copyright (C) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
10  * Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.txt for contributors.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, you may find one here:
24  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
25  * or you may search the http://www.gnu.org website for the version 2 license,
26  * or you may write to the Free Software Foundation, Inc.,
27  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
28  */
29 
30 #include <dialog_dxf_import.h>
31 #include <kiface_i.h>
32 #include <convert_to_biu.h>
35 
36 #include <class_board.h>
37 #include <class_module.h>
38 #include <class_edge_mod.h>
39 #include <class_text_mod.h>
40 #include <class_pcb_text.h>
41 
42 // Keys to store setup in config
43 #define DXF_IMPORT_LAYER_OPTION_KEY "DxfImportBrdLayer"
44 #define DXF_IMPORT_COORD_ORIGIN_KEY "DxfImportCoordOrigin"
45 #define DXF_IMPORT_LAST_FILE_KEY "DxfImportLastFile"
46 #define DXF_IMPORT_IMPORT_UNITS_KEY "DxfImportOffsetUnits"
47 #define DXF_IMPORT_IMPORT_OFFSET_X_KEY "DxfImportOffsetX"
48 #define DXF_IMPORT_IMPORT_OFFSET_Y_KEY "DxfImportOffsetY"
49 #define DXF_IMPORT_LINEWIDTH_UNITS_KEY "DxfImportLineWidthUnits"
50 #define DXF_IMPORT_LINEWIDTH_KEY "DxfImportLineWidth"
51 
52 
53 // Static members of DIALOG_DXF_IMPORT, to remember
54 // the user's choices during the session
58 
59 
60 DIALOG_DXF_IMPORT::DIALOG_DXF_IMPORT( PCB_BASE_FRAME* aParent, bool aImportAsFootprintGraphic )
61  : DIALOG_DXF_IMPORT_BASE( aParent )
62 {
63  m_parent = aParent;
64  m_dxfImporter.ImportAsFootprintGraphic( aImportAsFootprintGraphic );
66  m_PcbImportUnits = 0;
67  m_PcbImportOffsetX = 0.0; // always in mm
68  m_PcbImportOffsetY = 0.0; // always in mm
69 
70  if( m_config )
71  {
74  m_dxfFilename = m_config->Read( DXF_IMPORT_LAST_FILE_KEY, wxEmptyString );
80  }
81 
85 
86  m_DxfPcbPositionUnits->SetSelection( m_PcbImportUnits );
88 
89  m_textCtrlFileName->SetValue( m_dxfFilename );
90  m_rbOffsetOption->SetSelection( m_offsetSelection );
91 
92  // Configure the layers list selector
93  m_SelLayerBox->SetLayersHotkeys( false ); // Do not display hotkeys
94  m_SelLayerBox->SetLayerSet( LSET::AllCuMask() ); // Do not use copper layers
97 
99  {
100  m_layer = Dwgs_User;
102  }
103 
104  m_sdbSizerOK->SetDefault();
105  GetSizer()->Fit( this );
106  GetSizer()->SetSizeHints( this );
107  Centre();
108 }
109 
110 
112 {
113  m_offsetSelection = m_rbOffsetOption->GetSelection();
116 
117  if( m_config )
118  {
122 
126 
130  }
131 }
132 
133 
134 void DIALOG_DXF_IMPORT::DIALOG_DXF_IMPORT::onUnitPositionSelection( wxCommandEvent& event )
135 {
136  // Collect last entered values:
137  getPcbImportOffsets();
138 
139  m_PcbImportUnits = m_DxfPcbPositionUnits->GetSelection();;
140  showPcbImportOffsets();
141 }
142 
143 
145 {
146  double value = DoubleValueFromString( UNSCALED_UNITS, m_textCtrlLineWidth->GetValue() );
147 
148  switch( m_PCBLineWidthUnits )
149  {
150  default:
151  case 0: // display units = mm
152  break;
153 
154  case 1: // display units = mil
155  value *= 25.4 / 1000;
156  break;
157 
158  case 2: // display units = inch
159  value *= 25.4;
160  break;
161  }
162 
163  return value; // value is in mm
164 }
165 
166 
167 void DIALOG_DXF_IMPORT::onUnitWidthSelection( wxCommandEvent& event )
168 {
170 
171  // Switch to new units
172  m_PCBLineWidthUnits = m_choiceUnitLineWidth->GetSelection();
174 }
175 
176 
178 {
179  // Display m_PcbImportOffsetX and m_PcbImportOffsetY values according to
180  // the unit selection:
181  double xoffset = m_PcbImportOffsetX;
182  double yoffset = m_PcbImportOffsetY;
183 
184  if( m_PcbImportUnits ) // Units are inches
185  {
186  xoffset /= 25.4;
187  yoffset /= 25.4;
188  }
189 
190  m_DxfPcbXCoord->SetValue( wxString::Format( "%f", xoffset ) );
191  m_DxfPcbYCoord->SetValue( wxString::Format( "%f", yoffset ) );
192 
193 }
194 
195 
197 {
198  double value;
199 
200  switch( m_PCBLineWidthUnits )
201  {
202  default:
203  case 0: // display units = mm
204  value = m_PCBdefaultLineWidth;
205  break;
206 
207  case 1: // display units = mil
208  value = m_PCBdefaultLineWidth / 25.4 * 1000;
209  break;
210 
211  case 2: // display units = inch
212  value = m_PCBdefaultLineWidth / 25.4;
213  break;
214  }
215 
216  m_textCtrlLineWidth->SetValue( wxString::Format( "%f", value ) );
217 }
218 
219 
220 void DIALOG_DXF_IMPORT::OnBrowseDxfFiles( wxCommandEvent& event )
221 {
222  wxString path;
223  wxString filename;
224 
225  if( !m_dxfFilename.IsEmpty() )
226  {
227  wxFileName fn( m_dxfFilename );
228  path = fn.GetPath();
229  filename = fn.GetFullName();
230  }
231 
232  wxFileDialog dlg( m_parent,
233  _( "Open File" ),
234  path, filename,
235  DxfFileWildcard(),
236  wxFD_OPEN|wxFD_FILE_MUST_EXIST );
237 
238  if( dlg.ShowModal() != wxID_OK )
239  return;
240 
241  wxString fileName = dlg.GetPath();
242 
243  if( fileName.IsEmpty() )
244  return;
245 
246  m_dxfFilename = fileName;
247  m_textCtrlFileName->SetValue( fileName );
248 }
249 
250 
252 {
253  m_dxfFilename = m_textCtrlFileName->GetValue();
254 
255  if( m_dxfFilename.IsEmpty() )
256  {
257  wxMessageBox( _( "Error: No DXF filename!" ) );
258  return false;
259  }
260 
261  double offsetX = 0;
262  double offsetY = 0;
263 
264  m_offsetSelection = m_rbOffsetOption->GetSelection();
265 
266  switch( m_offsetSelection )
267  {
268  case 0:
269  offsetX = m_parent->GetPageSizeIU().x * MM_PER_IU / 2;
270  offsetY = m_parent->GetPageSizeIU().y * MM_PER_IU / 2;
271  break;
272 
273  case 1:
274  break;
275 
276  case 2:
277  offsetY = m_parent->GetPageSizeIU().y * MM_PER_IU / 2;
278  break;
279 
280  case 3:
281  offsetY = m_parent->GetPageSizeIU().y * MM_PER_IU;
282  break;
283 
284  case 4:
286  offsetX = m_PcbImportOffsetX;
287  offsetY = m_PcbImportOffsetY;
288  break;
289  }
290 
291  // Set coordinates offset for import (offset is given in mm)
292  m_dxfImporter.SetOffset( offsetX, offsetY );
297 
298  // Read dxf file:
300 
301  return true;
302 }
303 
304 
306 {
307  DIALOG_DXF_IMPORT dlg( aCaller );
308  bool success = ( dlg.ShowModal() == wxID_OK );
309 
310  if( success )
311  {
312  const std::list<BOARD_ITEM*>& list = dlg.GetImportedItems();
313  PICKED_ITEMS_LIST picklist;
314  BOARD* board = aCaller->GetBoard();
315 
316  std::list<BOARD_ITEM*>::const_iterator it, itEnd;
317  for( it = list.begin(), itEnd = list.end(); it != itEnd; ++it )
318  {
319  BOARD_ITEM* item = *it;
320  board->Add( item );
321 
322  ITEM_PICKER itemWrapper( item, UR_NEW );
323  picklist.PushItem( itemWrapper );
324  }
325 
326  aCaller->SaveCopyInUndoList( picklist, UR_NEW, wxPoint( 0, 0 ) );
327  aCaller->OnModify();
328  }
329 
330  return success;
331 }
332 
333 
335 {
336  wxASSERT( aModule );
337 
338  DIALOG_DXF_IMPORT dlg( aCaller, true );
339  bool success = ( dlg.ShowModal() == wxID_OK );
340 
341  if( success )
342  {
343  const std::list<BOARD_ITEM*>& list = dlg.GetImportedItems();
344 
345  aCaller->SaveCopyInUndoList( aModule, UR_CHANGED );
346  aCaller->OnModify();
347 
348  std::list<BOARD_ITEM*>::const_iterator it, itEnd;
349 
350  for( it = list.begin(), itEnd = list.end(); it != itEnd; ++it )
351  {
352  aModule->Add( *it );
353  }
354  }
355 
356  return success;
357 }
358 
359 
360 void DIALOG_DXF_IMPORT::OriginOptionOnUpdateUI( wxUpdateUIEvent& event )
361 {
362  bool enable = m_rbOffsetOption->GetSelection() == 4;
363 
364  m_DxfPcbPositionUnits->Enable( enable );
365  m_DxfPcbXCoord->Enable( enable );
366  m_DxfPcbYCoord->Enable( enable );
367 }
368 
369 
371 {
374 
375  if( m_PcbImportUnits ) // Units are inches
376  {
377  m_PcbImportOffsetX *= 25.4;
378  m_PcbImportOffsetY *= 25.4;
379  }
380 
381  return;
382 }
DXF2BRD_CONVERTER m_dxfImporter
Class DIALOG_DXF_IMPORT_BASE.
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Function AllCuMask returns a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:646
#define DXF_IMPORT_LINEWIDTH_KEY
PCB_BASE_FRAME * m_parent
TEXTE_PCB class definition.
void OnBrowseDxfFiles(wxCommandEvent &event) override
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
void onUnitWidthSelection(wxCommandEvent &event) override
#define DXF_IMPORT_LAYER_OPTION_KEY
Class BOARD to handle a board.
bool InvokeDXFDialogBoardImport(PCB_BASE_FRAME *aCaller)
Function InvokeDXFDialogBoardImport shows the modal DIALOG_DXF_IMPORT for importing a DXF file to a b...
wxString DxfFileWildcard()
#define DXF_IMPORT_IMPORT_UNITS_KEY
BOARD * GetBoard() const
void PushItem(const ITEM_PICKER &aItem)
Function PushItem pushes aItem to the top of the list.
bool InvokeDXFDialogModuleImport(PCB_BASE_FRAME *aCaller, MODULE *aModule)
Function InvokeDXFDialogModuleImport shows the modal DIALOG_DXF_IMPORT for importing a DXF file as fo...
void SetBoardFrame(PCB_BASE_FRAME *aFrame)
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:52
void ImportAsFootprintGraphic(bool aImportAsFootprintGraphic)
Allows the import DXF items converted to board graphic items or footprint graphic items...
Definition: dxf2brd_items.h:65
static LAYER_NUM m_layer
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_INSERT) override
>
PCB_LAYER_BOX_SELECTOR * m_SelLayerBox
Footprint text class description.
wxConfigBase * m_config
bool ImportDxfFile(const wxString &aFile)
Implementation of the method used for communicate with this filter.
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_INSERT) override
Adds an item to the container.
The common library.
#define DXF_IMPORT_IMPORT_OFFSET_X_KEY
wxConfigBase * KifaceSettings() const
Definition: kiface_i.h:103
int SetLayerSelection(LAYER_NUM layer)
static int m_offsetSelection
#define DXF_IMPORT_LAST_FILE_KEY
void OriginOptionOnUpdateUI(wxUpdateUIEvent &event) override
Class PICKED_ITEMS_LIST is a holder to handle information on schematic or board items.
bool SetLayersHotkeys(bool value)
int LAYER_NUM
Type LAYER_NUM can be replaced with int and removed.
static wxString m_dxfFilename
void SetOffset(double aOffsetX, double aOffsetY)
Set the coordinate offset between the imported dxf items and Pcbnew.
Definition: dxf2brd_items.h:90
virtual void OnModify()
Function OnModify Virtual Must be called after a change in order to set the "modify" flag of the curr...
const std::list< BOARD_ITEM * > & GetImportedItems() const
Function GetImportedItems()
#define DXF_IMPORT_IMPORT_OFFSET_Y_KEY
bool TransferDataFromWindow() override
void SetBrdLayer(int aBrdLayer)
Set the layer number to import dxf items.
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
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:169
#define DXF_IMPORT_LINEWIDTH_UNITS_KEY
LAYER_NUM GetLayerSelection() const
virtual void SaveCopyInUndoList(BOARD_ITEM *aItemToCopy, UNDO_REDO_T aTypeCommand, const wxPoint &aTransformPoint=wxPoint(0, 0))=0
Function SaveCopyInUndoList (virtual pure) Creates a new entry in undo list of commands.
const wxSize GetPageSizeIU() const override
Function GetPageSizeIU works off of GetPageSettings() to return the size of the paper page in the int...
Module description (excepted pads)
DIALOG_DXF_IMPORT(PCB_BASE_FRAME *aParent, bool aUseModuleItems=false)
EDGE_MODULE class definition.
double DoubleValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:301
void SetDefaultLineWidthMM(double aWidth)
Set the default line width when importing dxf items like lines to Pcbnew.
Definition: dxf2brd_items.h:77
class PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer...
#define DXF_IMPORT_COORD_ORIGIN_KEY