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, see <https://www.gnu.org/licenses/>.
18 */
19
24
27
28#include <board.h>
29#include <ki_exception.h>
30#include <progress_reporter.h>
31
32#include <wx/string.h>
33#include <wx/wfstream.h>
34#include <wx/file.h>
35#include <wx/filename.h>
36#include <wx/log.h>
37
38
40{
41}
42
43
47
48
49bool PCB_IO_DIPTRACE::CanReadBoard( const wxString& aFileName ) const
50{
51 if( !PCB_IO::CanReadBoard( aFileName ) )
52 return false;
53
54 // DipTrace .dip files start with: 0x07 "DTBOARD" or legacy 0x0B "DTBOARDx.yy".
55 // This is a binary format, so we read the file header directly rather
56 // than using IO_UTILS::fileStartsWithPrefix (which is for text files).
57 wxFileInputStream stream( aFileName );
58
59 if( !stream.IsOk() || stream.GetLength() < 8 )
60 return false;
61
62 uint8_t header[12] = {};
63 stream.Read( header, sizeof( header ) );
64
65 if( stream.LastRead() < 8 )
66 return false;
67
68 if( header[0] != 0x07 && header[0] != 0x0B )
69 return false;
70
71 return ( header[1] == 'D' && header[2] == 'T' && header[3] == 'B'
72 && header[4] == 'O' && header[5] == 'A' && header[6] == 'R'
73 && header[7] == 'D' );
74}
75
76
77BOARD* PCB_IO_DIPTRACE::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
78 const std::map<std::string, UTF8>* aProperties,
79 PROJECT* aProject )
80{
81 m_props = aProperties;
82
83 m_board = aAppendToMe ? aAppendToMe : new BOARD();
84
85 // Give the filename to the board if it's new
86 if( !aAppendToMe )
87 m_board->SetFileName( aFileName );
88
90 {
91 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
92
93 if( !m_progressReporter->KeepRefreshing() )
94 THROW_IO_ERROR( _( "File import canceled by user." ) );
95 }
96
97 DIPTRACE::PCB_PARSER parser( aFileName, m_board );
98 parser.Parse();
99
100 // Emit a sidecar .kicad_dru for per-zone DipTrace properties KiCad cannot
101 // store natively. Skip when appending so an existing project's rules survive.
102 if( !aAppendToMe )
103 {
104 wxString rules = parser.GenerateDesignRules();
105
106 if( !rules.IsEmpty() )
107 {
108 wxFileName drcPath( aFileName );
109 drcPath.SetExt( wxT( "kicad_dru" ) );
110
111 wxFile drcFile( drcPath.GetFullPath(), wxFile::write );
112
113 if( drcFile.IsOpened() )
114 drcFile.Write( rules );
115 else
116 wxLogWarning( _( "DipTrace import: could not write design rules to '%s'; "
117 "imported board is missing DipTrace-specific clearance rules." ),
118 drcPath.GetFullPath() );
119 }
120 }
121
122 return m_board;
123}
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:372
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:349
virtual bool CanReadBoard(const wxString &aFileName) const
Checks if this PCB_IO can read the specified board file.
Definition pcb_io.cpp:38
PCB_IO(const wxString &aName)
Definition pcb_io.h:342
const std::map< std::string, UTF8 > * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
Definition pcb_io.h:352
Container for project specific data.
Definition project.h:62
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