KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_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
26
27#include <io/io_utils.h>
28
29#include <wx/ffile.h>
30#include <wx/filename.h>
31
32#include <project.h>
33#include <reporter.h>
34#include <progress_reporter.h>
35#include <sch_sheet.h>
36#include <sch_sheet_path.h>
37#include <sch_screen.h>
38#include <schematic.h>
40
41
43static const std::vector<uint8_t> DIPTRACE_SCH_HEADER_V7 = { 0x07, 'D', 'T', 'S', 'C', 'H',
44 'E', 'M' };
45
47static const std::vector<uint8_t> DIPTRACE_SCH_HEADER_V11 = { 0x0B, 'D', 'T', 'S', 'C', 'H',
48 'E', 'M' };
49
50
51bool SCH_IO_DIPTRACE::CanReadSchematicFile( const wxString& aFileName ) const
52{
53 if( !SCH_IO::CanReadSchematicFile( aFileName ) )
54 return false;
55
58}
59
60
62 SCHEMATIC* aSchematic,
63 SCH_SHEET* aAppendToMe,
64 const std::map<std::string, UTF8>* aProperties )
65{
66 wxASSERT( !aFileName.IsEmpty() && aSchematic != nullptr );
67
69 {
70 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
71
72 if( !m_progressReporter->KeepRefreshing() )
73 THROW_IO_ERROR( _( "Open canceled by user." ) );
74 }
75
76 SCH_SHEET* rootSheet = nullptr;
77
78 if( aAppendToMe )
79 {
80 wxCHECK_MSG( aSchematic->IsValid(), nullptr,
81 wxT( "Can't append to a schematic with no root!" ) );
82 rootSheet = &aSchematic->Root();
83 }
84 else
85 {
86 // The content root carries a real UUID so SetTopLevelSheets() accepts it as a top-level
87 // sheet. A nil UUID marks the schematic's virtual root, which SetTopLevelSheets() skips,
88 // leaving the imported sheets orphaned and the editor blank.
89 rootSheet = new SCH_SHEET( aSchematic );
90
91 wxFileName newFilename( aFileName );
92 newFilename.SetExt( FILEEXT::KiCadSchematicFileExtension );
93 rootSheet->SetFileName( newFilename.GetFullPath() );
94
95 SCH_SCREEN* screen = new SCH_SCREEN( aSchematic );
96 screen->SetFileName( newFilename.GetFullPath() );
97 rootSheet->SetScreen( screen );
98
99 aSchematic->SetTopLevelSheets( { rootSheet } );
100 }
101
102 if( !rootSheet->GetScreen() )
103 {
104 SCH_SCREEN* screen = new SCH_SCREEN( aSchematic );
105
106 wxFileName newFilename( aFileName );
107 newFilename.SetExt( FILEEXT::KiCadSchematicFileExtension );
108 screen->SetFileName( newFilename.GetFullPath() );
109
110 rootSheet->SetScreen( screen );
111 }
112
113 DIPTRACE::SCH_PARSER parser( aFileName, aSchematic, rootSheet,
115 parser.Parse();
116
117 // The parser creates the final top-level sheet set after installing the initial content root,
118 // so rebuild now for headless and CLI consumers that do not run the editor's post-import refresh.
119 aSchematic->RefreshHierarchy();
120
121 // The editor's foreign-import path, unlike the native loader, does not generate per-sheet
122 // symbol and sheet instance data. Without it, symbols on sub-sheets keep only the transient
123 // root-path instance from construction, so their references, unit selections and page numbers
124 // do not resolve on the sheet they actually live on. Generate it here from the real hierarchy.
125 if( !aAppendToMe )
126 {
127 wxString projectName = aSchematic->Project().GetProjectName();
128
129 if( projectName.IsEmpty() )
130 projectName = wxFileName( aFileName ).GetName();
131
132 SCH_SHEET_LIST sheets = aSchematic->BuildUnorderedSheetList();
133 sheets.AddNewSymbolInstances( SCH_SHEET_PATH(), projectName );
135
136 if( sheets.AllSheetPageNumbersEmpty() )
137 sheets.SetInitialPageNumbers();
138 }
139
140 return rootSheet;
141}
Parser for DipTrace .dch schematic binary files.
void Parse()
Parse the .dch file and populate the schematic with KiCad objects.
REPORTER * m_reporter
Reporter to log errors/warnings to, may be nullptr.
Definition io_base.h:237
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition io_base.h:240
virtual const wxString GetProjectName() const
Return the short name of the project.
Definition project.cpp:199
Holds all the data relating to one schematic.
Definition schematic.h:89
SCH_SHEET_LIST BuildUnorderedSheetList() const
PROJECT & Project() const
Return a reference to the project this schematic is part of.
Definition schematic.h:104
bool IsValid() const
A simple test if the schematic is loaded, not a complete one.
Definition schematic.h:173
void SetTopLevelSheets(const std::vector< SCH_SHEET * > &aSheets)
SCH_SHEET & Root() const
Definition schematic.h:133
void RefreshHierarchy()
SCH_SHEET * LoadSchematicFile(const wxString &aFileName, SCHEMATIC *aSchematic, SCH_SHEET *aAppendToMe=nullptr, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load information from some input file format that this SCH_IO implementation knows about,...
bool CanReadSchematicFile(const wxString &aFileName) const override
Checks if this SCH_IO can read the specified schematic file.
virtual bool CanReadSchematicFile(const wxString &aFileName) const
Checks if this SCH_IO can read the specified schematic file.
Definition sch_io.cpp:45
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void AddNewSymbolInstances(const SCH_SHEET_PATH &aPrefixSheetPath, const wxString &aProjectName)
Attempt to add new symbol instances for all symbols in this list of sheet paths prefixed with aPrefix...
void SetInitialPageNumbers()
Set initial sheet page numbers.
bool AllSheetPageNumbersEmpty() const
Check all of the sheet instance for empty page numbers.
void AddNewSheetInstances(const SCH_SHEET_PATH &aPrefixSheetPath, int aLastVirtualPageNumber)
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:48
void SetFileName(const wxString &aFilename)
Definition sch_sheet.h:380
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:143
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
#define _(s)
static const std::string KiCadSchematicFileExtension
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
bool fileHasBinaryHeader(const wxString &aFilePath, const std::vector< uint8_t > &aHeader, size_t aOffset)
Check if a file starts with a defined binary header.
Definition io_utils.cpp:61
static const std::vector< uint8_t > DIPTRACE_SCH_HEADER_V11
DipTrace schematic legacy magic header: byte(11) + "DTSCHEMx.yy".
static const std::vector< uint8_t > DIPTRACE_SCH_HEADER_V7
DipTrace schematic modern magic header: byte(7) + "DTSCHEM".
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
Definition of file extensions used in Kicad.