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-2016 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>
34 
35 #include <class_board.h>
36 #include <class_module.h>
37 #include <class_edge_mod.h>
38 #include <class_text_mod.h>
39 #include <class_pcb_text.h>
40 
41 // Keys to store setup in config
42 #define DXF_IMPORT_LAYER_OPTION_KEY "DxfImportBrdLayer"
43 #define DXF_IMPORT_COORD_ORIGIN_KEY "DxfImportCoordOrigin"
44 #define DXF_IMPORT_LAST_FILE_KEY "DxfImportLastFile"
45 #define DXF_IMPORT_GRID_UNITS_KEY "DxfImportGridUnits"
46 #define DXF_IMPORT_GRID_OFFSET_X_KEY "DxfImportGridOffsetX"
47 #define DXF_IMPORT_GRID_OFFSET_Y_KEY "DxfImportGridOffsetY"
48 
49 
50 // Static members of DIALOG_DXF_IMPORT, to remember
51 // the user's choices during the session
55 
56 
57 DIALOG_DXF_IMPORT::DIALOG_DXF_IMPORT( PCB_BASE_FRAME* aParent, bool aUseModuleItems )
58  : DIALOG_DXF_IMPORT_BASE( aParent )
59 {
60  m_parent = aParent;
61  m_dxfImporter.UseModuleItems( aUseModuleItems );
63  m_PCBGridUnits = 0;
64  m_PCBGridOffsetX = 0.0;
65  m_PCBGridOffsetY = 0.0;
66 
67  if( m_config )
68  {
71  m_dxfFilename = m_config->Read( DXF_IMPORT_LAST_FILE_KEY, wxEmptyString );
75  }
76 
77  m_DXFPCBGridUnits->SetSelection( m_PCBGridUnits );
78  wxString tmpStr;
79  tmpStr << m_PCBGridOffsetX;
80  m_DXFPCBXCoord->SetValue( tmpStr );
81  tmpStr = "";
82  tmpStr << m_PCBGridOffsetY;
83  m_DXFPCBYCoord->SetValue( tmpStr );
84 
85  m_textCtrlFileName->SetValue( m_dxfFilename );
86  m_rbOffsetOption->SetSelection( m_offsetSelection );
87 
88  // Configure the layers list selector
89  m_SelLayerBox->SetLayersHotkeys( false ); // Do not display hotkeys
90  m_SelLayerBox->SetLayerSet( LSET::AllCuMask() ); // Do not use copper layers
93 
95  {
98  }
99 
100  m_sdbSizer1OK->SetDefault();
101  GetSizer()->Fit( this );
102  GetSizer()->SetSizeHints( this );
103  Centre();
104 }
105 
106 
108 {
109  m_offsetSelection = m_rbOffsetOption->GetSelection();
111 
112  if( m_config )
113  {
117 
121  }
122 }
123 
124 
125 void DIALOG_DXF_IMPORT::OnBrowseDxfFiles( wxCommandEvent& event )
126 {
127  wxString path;
128  wxString filename;
129 
130  if( !m_dxfFilename.IsEmpty() )
131  {
132  wxFileName fn( m_dxfFilename );
133  path = fn.GetPath();
134  filename = fn.GetFullName();
135  }
136 
137  wxFileDialog dlg( m_parent,
138  _( "Open File" ),
139  path, filename,
140  "DXF Files (*.dxf)|*.dxf",
141  wxFD_OPEN|wxFD_FILE_MUST_EXIST );
142 
143  if( dlg.ShowModal() != wxID_OK )
144  return;
145 
146  wxString fileName = dlg.GetPath();
147 
148  if( fileName.IsEmpty() )
149  return;
150 
151  m_dxfFilename = fileName;
152  m_textCtrlFileName->SetValue( fileName );
153 }
154 
155 
156 void DIALOG_DXF_IMPORT::OnOKClick( wxCommandEvent& event )
157 {
158  m_dxfFilename = m_textCtrlFileName->GetValue();
159 
160  if( m_dxfFilename.IsEmpty() )
161  return;
162 
163  double offsetX = 0;
164  double offsetY = 0;
165 
166  m_offsetSelection = m_rbOffsetOption->GetSelection();
167 
168  switch( m_offsetSelection )
169  {
170  case 0:
171  offsetX = m_parent->GetPageSizeIU().x * MM_PER_IU / 2;
172  offsetY = m_parent->GetPageSizeIU().y * MM_PER_IU / 2;
173  break;
174 
175  case 1:
176  break;
177 
178  case 2:
179  offsetY = m_parent->GetPageSizeIU().y * MM_PER_IU / 2;
180  break;
181 
182  case 3:
183  offsetY = m_parent->GetPageSizeIU().y * MM_PER_IU;
184  break;
185 
186  case 4:
187  GetPCBGridOffsets( offsetX, offsetY );
188 
189  if( GetPCBGridUnits() )
190  {
191  offsetX *= 25.4;
192  offsetY *= 25.4;
193  }
194  break;
195  }
196 
197  // Set coordinates offset for import (offset is given in mm)
198  m_dxfImporter.SetOffset( offsetX, offsetY );
201 
202  // Read dxf file:
204 
205  EndModal( wxID_OK );
206 }
207 
208 
210 {
211  DIALOG_DXF_IMPORT dlg( aCaller );
212  bool success = ( dlg.ShowModal() == wxID_OK );
213 
214  if( success )
215  {
216  const std::list<BOARD_ITEM*>& list = dlg.GetImportedItems();
217  PICKED_ITEMS_LIST picklist;
218  BOARD* board = aCaller->GetBoard();
219 
220  std::list<BOARD_ITEM*>::const_iterator it, itEnd;
221  for( it = list.begin(), itEnd = list.end(); it != itEnd; ++it )
222  {
223  BOARD_ITEM* item = *it;
224  board->Add( item );
225 
226  ITEM_PICKER itemWrapper( item, UR_NEW );
227  picklist.PushItem( itemWrapper );
228  }
229 
230  aCaller->SaveCopyInUndoList( picklist, UR_NEW, wxPoint( 0, 0 ) );
231  aCaller->OnModify();
232  }
233 
234  return success;
235 }
236 
237 
239 {
240  wxASSERT( aModule );
241 
242  DIALOG_DXF_IMPORT dlg( aCaller, true );
243  bool success = ( dlg.ShowModal() == wxID_OK );
244 
245  if( success )
246  {
247  const std::list<BOARD_ITEM*>& list = dlg.GetImportedItems();
248 
249  aCaller->SaveCopyInUndoList( aModule, UR_CHANGED );
250  aCaller->OnModify();
251 
252  std::list<BOARD_ITEM*>::const_iterator it, itEnd;
253 
254  for( it = list.begin(), itEnd = list.end(); it != itEnd; ++it )
255  {
256  aModule->Add( *it );
257  }
258  }
259 
260  return success;
261 }
262 
263 
264 void DIALOG_DXF_IMPORT::OriginOptionOnUpdateUI( wxUpdateUIEvent& event )
265 {
266  bool enable = m_rbOffsetOption->GetSelection() == 4;
267 
268  m_DXFPCBGridUnits->Enable( enable );
269  m_DXFPCBXCoord->Enable( enable );
270  m_DXFPCBYCoord->Enable( enable );
271 }
272 
273 
275 {
276  return m_DXFPCBGridUnits->GetSelection();
277 }
278 
279 
280 void DIALOG_DXF_IMPORT::GetPCBGridOffsets( double &aXOffset, double &aYOffset )
281 {
282  aXOffset = DoubleValueFromString( UNSCALED_UNITS, m_DXFPCBXCoord->GetValue() );
283  aYOffset = DoubleValueFromString( UNSCALED_UNITS, m_DXFPCBYCoord->GetValue() );
284  return;
285 }
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:639
void Add(BOARD_ITEM *aBoardItem, ADD_MODE aMode=ADD_INSERT) override
>
void GetPCBGridOffsets(double &aXOffset, double &aYOffset)
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...
#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...
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 UseModuleItems(bool aUseModuleItems=true)
Definition: dxf2brd_items.h:60
void OnOKClick(wxCommandEvent &event) override
#define DXF_IMPORT_GRID_OFFSET_Y_KEY
static LAYER_NUM m_layer
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
>
wxConfigBase * KifaceSettings() const
Definition: kiface_i.h:101
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 importede dxf items and Pcbnew.
Definition: dxf2brd_items.h:70
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()
void SetBrdLayer(int aBrdLayer)
Set the layer number to import dxf items.
Definition: dxf2brd_items.h:80
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:166
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.
#define DXF_IMPORT_GRID_UNITS_KEY
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
class PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer...
#define DXF_IMPORT_GRID_OFFSET_X_KEY
#define DXF_IMPORT_COORD_ORIGIN_KEY