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, see <https://www.gnu.org/licenses/>.
18 */
19
22
23#include <io/io_utils.h>
24
25#include <wx/ffile.h>
26#include <wx/filename.h>
27
28#include <project.h>
29#include <reporter.h>
30#include <progress_reporter.h>
31#include <sch_sheet.h>
32#include <sch_sheet_path.h>
33#include <sch_screen.h>
34#include <schematic.h>
36
37
39static const std::vector<uint8_t> DIPTRACE_SCH_HEADER_V7 = { 0x07, 'D', 'T', 'S', 'C', 'H',
40 'E', 'M' };
41
43static const std::vector<uint8_t> DIPTRACE_SCH_HEADER_V11 = { 0x0B, 'D', 'T', 'S', 'C', 'H',
44 'E', 'M' };
45
46
47bool SCH_IO_DIPTRACE::CanReadSchematicFile( const wxString& aFileName ) const
48{
49 if( !SCH_IO::CanReadSchematicFile( aFileName ) )
50 return false;
51
54}
55
56
58 SCHEMATIC* aSchematic,
59 SCH_SHEET* aAppendToMe,
60 const std::map<std::string, UTF8>* aProperties )
61{
62 wxASSERT( !aFileName.IsEmpty() && aSchematic != nullptr );
63
65 {
66 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
67
68 if( !m_progressReporter->KeepRefreshing() )
70 }
71
72 SCH_SHEET* rootSheet = nullptr;
73
74 if( aAppendToMe )
75 {
76 wxCHECK_MSG( aSchematic->IsValid(), nullptr,
77 wxT( "Can't append to a schematic with no root!" ) );
78 rootSheet = &aSchematic->Root();
79 }
80 else
81 {
82 // The content root carries a real UUID so SetTopLevelSheets() accepts it as a top-level
83 // sheet. A nil UUID marks the schematic's virtual root, which SetTopLevelSheets() skips,
84 // leaving the imported sheets orphaned and the editor blank.
85 rootSheet = new SCH_SHEET( aSchematic );
86
87 wxFileName newFilename( aFileName );
88 newFilename.SetExt( FILEEXT::KiCadSchematicFileExtension );
89 rootSheet->SetFileName( newFilename.GetFullPath() );
90
91 SCH_SCREEN* screen = new SCH_SCREEN( aSchematic );
92 screen->SetFileName( newFilename.GetFullPath() );
93 rootSheet->SetScreen( screen );
94
95 aSchematic->SetTopLevelSheets( { rootSheet } );
96 }
97
98 if( !rootSheet->GetScreen() )
99 {
100 SCH_SCREEN* screen = new SCH_SCREEN( aSchematic );
101
102 wxFileName newFilename( aFileName );
103 newFilename.SetExt( FILEEXT::KiCadSchematicFileExtension );
104 screen->SetFileName( newFilename.GetFullPath() );
105
106 rootSheet->SetScreen( screen );
107 }
108
109 DIPTRACE::SCH_PARSER parser( aFileName, aSchematic, rootSheet,
111 parser.Parse();
112
113 // The parser creates the final top-level sheet set after installing the initial content root,
114 // so rebuild now for headless and CLI consumers that do not run the editor's post-import refresh.
115 aSchematic->RefreshHierarchy();
116
117 // The editor's foreign-import path, unlike the native loader, does not generate per-sheet
118 // symbol and sheet instance data. Without it, symbols on sub-sheets keep only the transient
119 // root-path instance from construction, so their references, unit selections and page numbers
120 // do not resolve on the sheet they actually live on. Generate it here from the real hierarchy.
121 if( !aAppendToMe )
122 {
123 wxString projectName = aSchematic->Project().GetProjectName();
124
125 if( projectName.IsEmpty() )
126 projectName = wxFileName( aFileName ).GetName();
127
128 SCH_SHEET_LIST sheets = aSchematic->BuildUnorderedSheetList();
129 sheets.AddNewSymbolInstances( SCH_SHEET_PATH(), projectName );
131
132 if( sheets.AllSheetPageNumbersEmpty() )
133 sheets.SetInitialPageNumbers();
134 }
135
136 return rootSheet;
137}
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:264
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition io_base.h:267
virtual const wxString GetProjectName() const
Return the short name of the project.
Definition project.cpp:289
Holds all the data relating to one schematic.
Definition schematic.h:155
SCH_SHEET_LIST BuildUnorderedSheetList() const
PROJECT & Project() const
Return a reference to the project this schematic is part of.
Definition schematic.h:177
bool IsValid() const
A simple test if the schematic is loaded, not a complete one.
Definition schematic.h:384
void SetTopLevelSheets(const std::vector< SCH_SHEET * > &aSheets)
Replace the top level sheets, rebuilding the hierarchy and connectivity around them.
SCH_SHEET & Root() const
Definition schematic.h:282
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:47
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 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)
void AddNewSymbolInstances(const SCH_SHEET_PATH &aPrefixSheetPath, const wxString &aProjectName, bool aStripRefSuffixes=false)
Attempt to add new symbol instances for all symbols in this list of sheet paths prefixed with aPrefix...
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:50
void SetFileName(const wxString &aFilename)
Definition sch_sheet.h:434
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:187
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
#define _(s)
#define THROW_IO_CANCELLED()
static const std::string KiCadSchematicFileExtension
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:60
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.