KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_io_diptrace.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
28
31
32#include <board.h>
33#include <ki_exception.h>
34#include <progress_reporter.h>
35
36#include <wx/string.h>
37#include <wx/wfstream.h>
38#include <wx/file.h>
39#include <wx/filename.h>
40
41
43{
44}
45
46
50
51
52bool PCB_IO_DIPTRACE::CanReadBoard( const wxString& aFileName ) const
53{
54 if( !PCB_IO::CanReadBoard( aFileName ) )
55 return false;
56
57 // DipTrace .dip files start with: 0x07 "DTBOARD" or legacy 0x0B "DTBOARDx.yy".
58 // This is a binary format, so we read the file header directly rather
59 // than using IO_UTILS::fileStartsWithPrefix (which is for text files).
60 wxFileInputStream stream( aFileName );
61
62 if( !stream.IsOk() || stream.GetLength() < 8 )
63 return false;
64
65 uint8_t header[12] = {};
66 stream.Read( header, sizeof( header ) );
67
68 if( stream.LastRead() < 8 )
69 return false;
70
71 if( header[0] != 0x07 && header[0] != 0x0B )
72 return false;
73
74 return ( header[1] == 'D' && header[2] == 'T' && header[3] == 'B'
75 && header[4] == 'O' && header[5] == 'A' && header[6] == 'R'
76 && header[7] == 'D' );
77}
78
79
80BOARD* PCB_IO_DIPTRACE::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
81 const std::map<std::string, UTF8>* aProperties,
82 PROJECT* aProject )
83{
84 m_props = aProperties;
85
86 m_board = aAppendToMe ? aAppendToMe : new BOARD();
87
88 // Give the filename to the board if it's new
89 if( !aAppendToMe )
90 m_board->SetFileName( aFileName );
91
93 {
94 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
95
96 if( !m_progressReporter->KeepRefreshing() )
97 THROW_IO_ERROR( _( "File import canceled by user." ) );
98 }
99
100 DIPTRACE::PCB_PARSER parser( aFileName, m_board );
101 parser.Parse();
102
103 // Emit a sidecar .kicad_dru for per-zone DipTrace properties KiCad cannot
104 // store natively. Skip when appending so an existing project's rules survive.
105 if( !aAppendToMe )
106 {
107 wxString rules = parser.GenerateDesignRules();
108
109 if( !rules.IsEmpty() )
110 {
111 wxFileName drcPath( aFileName );
112 drcPath.SetExt( wxT( "kicad_dru" ) );
113
114 wxFile drcFile( drcPath.GetFullPath(), wxFile::write );
115
116 if( drcFile.IsOpened() )
117 drcFile.Write( rules );
118 }
119 }
120
121 return m_board;
122}
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:323
Parses a DipTrace .dip binary board file and populates a KiCad BOARD.
wxString GenerateDesignRules() const
Build a KiCad custom design-rule (.kicad_dru) document for the per-zone DipTrace properties that have...
void Parse()
Parse the file and populate the board. Throws IO_ERROR on failure.
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition io_base.h:240
BOARD * LoadBoard(const wxString &aFileName, BOARD *aAppendToMe, const std::map< std::string, UTF8 > *aProperties=nullptr, PROJECT *aProject=nullptr) override
Load information from some input file format that this PCB_IO implementation knows about into either ...
bool CanReadBoard(const wxString &aFileName) const override
Checks if this PCB_IO can read the specified board file.
BOARD * m_board
The board BOARD being worked on, no ownership here.
Definition pcb_io.h:353
virtual bool CanReadBoard(const wxString &aFileName) const
Checks if this PCB_IO can read the specified board file.
Definition pcb_io.cpp:42
PCB_IO(const wxString &aName)
Definition pcb_io.h:346
const std::map< std::string, UTF8 > * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
Definition pcb_io.h:356
Container for project specific data.
Definition project.h:66
Parser for DipTrace binary .dip board files.
#define _(s)
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
Pcbnew PCB_IO for DipTrace binary .dip board files.
std::vector< std::string > header