dialog_dxf_import.cpp 7.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/**
 * @file dialog_dxf_import.cpp
 * @brief Dialog to import a dxf file on a given board layer.
 */

/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
 * Copyright (C) 1992-2013 KiCad Developers, see AUTHORS.txt for contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

30
#include <dialog_dxf_import.h>
31 32
//#include <pgm_base.h>
#include <kiface_i.h>
33
#include <convert_from_iu.h>
34
#include <class_pcb_layer_box_selector.h>
35
#include <class_draw_panel_gal.h>
36

37
#include <class_board.h>
38 39 40 41
#include <class_module.h>
#include <class_edge_mod.h>
#include <class_text_mod.h>
#include <class_pcb_text.h>
42

43 44 45 46 47
// Keys to store setup in config
#define DXF_IMPORT_LAYER_OPTION_KEY wxT("DxfImportBrdLayer")
#define DXF_IMPORT_COORD_ORIGIN_KEY wxT("DxfImportCoordOrigin")
#define DXF_IMPORT_LAST_FILE_KEY wxT("DxfImportLastFile")

48 49 50 51
// Static members of DIALOG_DXF_IMPORT, to remember
// the user's choices during the session
wxString DIALOG_DXF_IMPORT::m_dxfFilename;
int DIALOG_DXF_IMPORT::m_offsetSelection = 4;
Dick Hollenbeck's avatar
Dick Hollenbeck committed
52
LAYER_NUM DIALOG_DXF_IMPORT::m_layer = Dwgs_User;
53 54


55 56
DIALOG_DXF_IMPORT::DIALOG_DXF_IMPORT( PCB_BASE_FRAME* aParent )
    : DIALOG_DXF_IMPORT_BASE( aParent )
57 58
{
    m_parent = aParent;
59
    m_config = Kiface().KifaceSettings();
60 61 62

    if( m_config )
    {
Dick Hollenbeck's avatar
Dick Hollenbeck committed
63
        m_layer = m_config->Read( DXF_IMPORT_LAYER_OPTION_KEY, (long)Dwgs_User );
64 65 66 67
        m_offsetSelection = m_config->Read( DXF_IMPORT_COORD_ORIGIN_KEY, 3 );
        m_dxfFilename =  m_config->Read( DXF_IMPORT_LAST_FILE_KEY, wxEmptyString );
    }

68 69 70 71
    m_textCtrlFileName->SetValue( m_dxfFilename );
    m_rbOffsetOption->SetSelection( m_offsetSelection );

    // Configure the layers list selector
Dick Hollenbeck's avatar
Dick Hollenbeck committed
72 73
    m_SelLayerBox->SetLayersHotkeys( false );           // Do not display hotkeys
    m_SelLayerBox->SetLayerSet( LSET::AllCuMask() );    // Do not use copper layers
74 75
    m_SelLayerBox->SetBoardFrame( m_parent );
    m_SelLayerBox->Resync();
76

77 78
    if( m_SelLayerBox->SetLayerSelection( m_layer ) < 0 )
    {
Dick Hollenbeck's avatar
Dick Hollenbeck committed
79
        m_layer = Dwgs_User;
80 81 82 83 84 85 86 87 88 89 90 91 92
        m_SelLayerBox->SetLayerSelection( m_layer );
    }

    GetSizer()->Fit( this );
    GetSizer()->SetSizeHints( this );
    Centre();
}


DIALOG_DXF_IMPORT::~DIALOG_DXF_IMPORT()
{
    m_offsetSelection = m_rbOffsetOption->GetSelection();
    m_layer = m_SelLayerBox->GetLayerSelection();
93 94 95 96 97 98 99

    if( m_config )
    {
        m_config->Write( DXF_IMPORT_LAYER_OPTION_KEY, (long)m_layer );
        m_config->Write( DXF_IMPORT_COORD_ORIGIN_KEY, m_offsetSelection );
        m_config->Write( DXF_IMPORT_LAST_FILE_KEY, m_dxfFilename );
    }
100 101 102 103 104
}


void DIALOG_DXF_IMPORT::OnBrowseDxfFiles( wxCommandEvent& event )
{
105
    wxString path;
106
    wxString filename;
107 108 109 110 111

    if( !m_dxfFilename.IsEmpty() )
    {
        wxFileName fn( m_dxfFilename );
        path = fn.GetPath();
112
        filename = fn.GetFullName();
113
    }
114
    wxFileDialog dlg( m_parent,
115
                      wxT( "Open File" ),
116
                      path, filename,
117
                      wxT( "dxf Files (*.dxf)|*.dxf" ),
118 119 120 121 122
                      wxFD_OPEN|wxFD_FILE_MUST_EXIST );
    dlg.ShowModal();

    wxString fileName = dlg.GetPath();

123 124 125 126 127 128 129
    if( fileName.IsEmpty() )
        return;

    m_dxfFilename = fileName;
    m_textCtrlFileName->SetValue( fileName );
}

130

131 132 133 134 135 136 137 138 139 140 141 142
void DIALOG_DXF_IMPORT::OnOKClick( wxCommandEvent& event )
{
    m_dxfFilename = m_textCtrlFileName->GetValue();

    if( m_dxfFilename.IsEmpty() )
        return;

    double offsetX = 0;
    double offsetY = 0;

    m_offsetSelection = m_rbOffsetOption->GetSelection();
    switch( m_offsetSelection )
143
    {
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
        case 0:
            break;

        case 1:
            offsetY = m_parent->GetPageSizeIU().y * MM_PER_IU / 2;
            break;

        case 2:
            offsetX = m_parent->GetPageSizeIU().x * MM_PER_IU / 2;
            offsetY = m_parent->GetPageSizeIU().y * MM_PER_IU / 2;
            break;

        case 3:
            offsetY = m_parent->GetPageSizeIU().y * MM_PER_IU;
            break;
159 160
    }

161
    // Set coordinates offset for import (offset is given in mm)
162
    m_dxfImporter.SetOffset( offsetX, offsetY );
163
    m_layer = m_SelLayerBox->GetLayerSelection();
164
    m_dxfImporter.SetBrdLayer( m_layer );
165 166

    // Read dxf file:
167
    m_dxfImporter.ImportDxfFile( m_dxfFilename );
168

169 170 171 172
    EndModal( wxID_OK );
}


173
bool InvokeDXFDialogBoardImport( PCB_BASE_FRAME* aCaller )
174 175
{
    DIALOG_DXF_IMPORT dlg( aCaller );
176
    bool success = ( dlg.ShowModal() == wxID_OK );
177 178

    if( success )
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    {
        const std::list<BOARD_ITEM*>& list = dlg.GetImportedItems();
        PICKED_ITEMS_LIST picklist;

        BOARD* board = aCaller->GetBoard();
        KIGFX::VIEW* view = aCaller->GetGalCanvas()->GetView();

        std::list<BOARD_ITEM*>::const_iterator it, itEnd;
        for( it = list.begin(), itEnd = list.end(); it != itEnd; ++it )
        {
            BOARD_ITEM* item = *it;
            board->Add( item );

            ITEM_PICKER itemWrapper( item, UR_NEW );
            picklist.PushItem( itemWrapper );

            if( aCaller->IsGalCanvasActive() )
                view->Add( item );
        }

        aCaller->SaveCopyInUndoList( picklist, UR_NEW, wxPoint( 0, 0 ) );
200
        aCaller->OnModify();
201
    }
202 203

    return success;
204
}
205 206 207 208


bool InvokeDXFDialogModuleImport( PCB_BASE_FRAME* aCaller, MODULE* aModule )
{
209 210
    wxASSERT( aModule );

211 212 213 214 215 216 217 218
    DIALOG_DXF_IMPORT dlg( aCaller );
    bool success = ( dlg.ShowModal() == wxID_OK );

    if( success )
    {
        const std::list<BOARD_ITEM*>& list = dlg.GetImportedItems();
        KIGFX::VIEW* view = aCaller->GetGalCanvas()->GetView();

219
        aCaller->SaveCopyInUndoList( aModule, UR_MODEDIT );
220 221 222 223 224 225 226 227 228 229 230 231 232 233
        aCaller->OnModify();

        std::list<BOARD_ITEM*>::const_iterator it, itEnd;
        for( it = list.begin(), itEnd = list.end(); it != itEnd; ++it )
        {
            BOARD_ITEM* item = *it;
            BOARD_ITEM* converted = NULL;

            // Modules use different types for the same things,
            // so we need to convert imported items to appropriate classes.
            switch( item->Type() )
            {
            case PCB_LINE_T:
            {
234
                converted = new EDGE_MODULE( aModule );
235
                *static_cast<DRAWSEGMENT*>( converted ) = *static_cast<DRAWSEGMENT*>( item );
236
                aModule->Add( converted );
237 238 239 240 241 242 243
                static_cast<EDGE_MODULE*>( converted )->SetLocalCoord();
                delete item;
                break;
            }

            case PCB_TEXT_T:
            {
244
                converted = new TEXTE_MODULE( aModule );
245
                *static_cast<TEXTE_PCB*>( converted ) = *static_cast<TEXTE_PCB*>( item );
246
                aModule->Add( converted );
247 248 249 250 251 252
                static_cast<TEXTE_MODULE*>( converted )->SetLocalCoord();
                delete item;
                break;
            }

            default:
253
                wxLogDebug( wxT( "type %d currently not handled" ), item->Type() );
254 255 256
                break;
            }

257
            if( aCaller->IsGalCanvasActive() && converted )
258 259 260 261 262 263
                view->Add( converted );
        }
    }

    return success;
}