KiCad PCB EDA Suite
cadstar_sch_archive_loader.h
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 (C) 2020-2021 Roberto Fernandez Bautista <[email protected]>
5 * Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
26#ifndef CADSTAR_SCH_ARCHIVE_LOADER_H_
27#define CADSTAR_SCH_ARCHIVE_LOADER_H_
28
30
31#include <layer_ids.h> // SCH_LAYER_ID
32#include <plotters/plotter.h> // PLOT_DASH_TYPE
33#include <pin_type.h> // ELECTRICAL_PINTYPE
34#include <sch_io_mgr.h>
35#include <wx/filename.h>
36
37class BUS_ALIAS;
38class EDA_TEXT;
39class TEXT_SPIN_STYLE;
40class LIB_FIELD;
41class LIB_SYMBOL;
42class SCH_SYMBOL;
43class SCH_ITEM;
44class SCH_FIELD;
45class SCH_GLOBALLABEL;
46class SCH_HIERLABEL;
47class SCH_SHEET;
48class SCH_SHEET_PATH;
49class SCH_TEXT;
50class SCHEMATIC;
51
53{
54public:
55 // Size of tiny net labels when none present in original design
56 const int SMALL_LABEL_SIZE = KiROUND( (double) SCH_IU_PER_MM * 0.4 );
57
58 explicit CADSTAR_SCH_ARCHIVE_LOADER( wxString aFilename, REPORTER* aReporter,
59 PROGRESS_REPORTER* aProgressReporter ) :
61 {
62 m_schematic = nullptr;
63 m_rootSheet = nullptr;
64 m_plugin = nullptr;
65 m_designCenter.x = 0;
66 m_designCenter.y = 0;
67 m_reporter = aReporter;
68 m_progressReporter = aProgressReporter;
69 }
70
71
73 {
74 }
75
81 void Load( SCHEMATIC* aSchematic, SCH_SHEET* aRootSheet,
82 SCH_PLUGIN::SCH_PLUGIN_RELEASER* aSchPlugin, const wxFileName& aLibraryFileName );
83
84
85private:
86 typedef std::pair<BLOCK_ID, TERMINAL_ID> BLOCK_PIN_ID;
87 typedef std::pair<PART_ID, GATE_ID> PART_GATE_ID;
88
93 typedef std::map<TERMINAL_ID, wxString> TERMINAL_TO_PINNUM_MAP;
94
103 std::map<LAYER_ID, SCH_SHEET*> m_sheetMap;
104 std::map<BLOCK_PIN_ID, SCH_HIERLABEL*>
106 std::map<PART_ID, LIB_SYMBOL*> m_partMap;
107 std::map<PART_GATE_ID, SYMDEF_ID> m_partSymbolsMap;
109 std::map<PART_ID, TERMINAL_TO_PINNUM_MAP> m_pinNumsMap;
110 std::map<wxString, LIB_SYMBOL*> m_powerSymLibMap;
111 std::map<SYMBOL_ID, SCH_SYMBOL*>
113 std::map<SYMBOL_ID, SCH_GLOBALLABEL*>
115 std::map<BUS_ID, std::shared_ptr<BUS_ALIAS>> m_busesMap;
116
117 void loadSheets();
119 void loadPartsLibrary();
121 void loadBusses();
122 void loadNets();
123 void loadFigures();
124 void loadTexts();
126 void loadTextVariables();
127
128 //Helper Functions for loading sheets
129 void loadSheetAndChildSheets( LAYER_ID aCadstarSheetID, const VECTOR2I& aPosition,
130 VECTOR2I aSheetSize, const SCH_SHEET_PATH& aParentSheet );
131
132 void loadChildSheets( LAYER_ID aCadstarSheetID, const SCH_SHEET_PATH& aSheet );
133
134 std::vector<LAYER_ID> findOrphanSheets();
135
136 int getSheetNumber( LAYER_ID aCadstarSheetID );
137
138 void loadItemOntoKiCadSheet( LAYER_ID aCadstarSheetID, SCH_ITEM* aItem );
139
140 //Helper Functions for loading library items
141 void loadSymDefIntoLibrary( const SYMDEF_ID& aSymdefID, const PART* aCadstarPart,
142 const GATE_ID& aGateID, LIB_SYMBOL* aSymbol );
143
144 void loadLibrarySymbolShapeVertices( const std::vector<VERTEX>& aCadstarVertices,
145 wxPoint aSymbolOrigin, LIB_SYMBOL* aSymbol,
146 int aGateNumber, int aLineThickness );
147
148 void applyToLibraryFieldAttribute( const ATTRIBUTE_LOCATION& aCadstarAttrLoc,
149 wxPoint aSymbolOrigin, LIB_FIELD* aKiCadField );
150
151 //Helper Functions for loading symbols in schematic
152 SCH_SYMBOL* loadSchematicSymbol( const SYMBOL& aCadstarSymbol, const LIB_SYMBOL& aKiCadPart,
153 EDA_ANGLE& aComponentOrientation );
154
155 void loadSymbolFieldAttribute( const ATTRIBUTE_LOCATION& aCadstarAttrLoc,
156 const EDA_ANGLE& aComponentOrientation, bool aIsMirrored,
157 SCH_FIELD* aKiCadField );
158
159 int getComponentOrientation( const EDA_ANGLE& aOrientAngle, EDA_ANGLE& aReturnedOrientation );
160
161 //Helper functions for loading nets
162 POINT getLocationOfNetElement( const NET_SCH& aNet, const NETELEMENT_ID& aNetElementID );
163
164 wxString getNetName( const NET_SCH& aNet );
165
166 //Helper functions for loading figures / graphical items
167 void loadGraphicStaightSegment( const VECTOR2I& aStartPoint, const VECTOR2I& aEndPoint,
168 const LINECODE_ID& aCadstarLineCodeID,
169 const LAYER_ID& aCadstarSheetID,
170 const SCH_LAYER_ID& aKiCadSchLayerID,
171 const VECTOR2I& aMoveVector = { 0, 0 },
172 const EDA_ANGLE& aRotation = ANGLE_0,
173 const double& aScalingFactor = 1.0,
174 const VECTOR2I& aTransformCentre = { 0, 0 },
175 const bool& aMirrorInvert = false );
176
177 void loadShapeVertices( const std::vector<VERTEX>& aCadstarVertices,
178 LINECODE_ID aCadstarLineCodeID, LAYER_ID aCadstarSheetID,
179 SCH_LAYER_ID aKiCadSchLayerID, const VECTOR2I& aMoveVector = { 0, 0 },
180 const EDA_ANGLE& aRotation = ANGLE_0,
181 const double& aScalingFactor = 1.0,
182 const VECTOR2I& aTransformCentre = { 0, 0 },
183 const bool& aMirrorInvert = false );
184
185 void loadFigure( const FIGURE& aCadstarFigure, const LAYER_ID& aCadstarSheetIDOverride,
186 SCH_LAYER_ID aKiCadSchLayerID, const VECTOR2I& aMoveVector = { 0, 0 },
187 const EDA_ANGLE& aRotation = ANGLE_0, const double& aScalingFactor = 1.0,
188 const VECTOR2I& aTransformCentre = { 0, 0 },
189 const bool& aMirrorInvert = false );
190
191 //Helper functions for loading text elements
192 void applyTextSettings( EDA_TEXT* aKiCadTextItem,
193 const TEXTCODE_ID& aCadstarTextCodeID,
194 const ALIGNMENT& aCadstarAlignment,
195 const JUSTIFICATION& aCadstarJustification,
196 const long long aCadstarOrientAngle = 0,
197 bool aMirrored = false );
198
199 SCH_TEXT* getKiCadSchText( const TEXT& aCadstarTextElement );
200
201
202 //Helper Functions for obtaining CADSTAR elements from the parsed structures
203 SYMDEF_ID getSymDefFromName( const wxString& aSymdefName, const wxString& aSymDefAlternate );
204 bool isAttributeVisible( const ATTRIBUTE_ID& aCadstarAttributeID );
205
206 int getLineThickness( const LINECODE_ID& aCadstarLineCodeID );
207 PLOT_DASH_TYPE getLineStyle( const LINECODE_ID& aCadstarLineCodeID );
208 PART getPart( const PART_ID& aCadstarPartID );
209 ROUTECODE getRouteCode( const ROUTECODE_ID& aCadstarRouteCodeID );
210 TEXTCODE getTextCode( const TEXTCODE_ID& aCadstarTextCodeID );
211 int getTextHeightFromTextCode( const TEXTCODE_ID& aCadstarTextCodeID );
212 wxString getAttributeName( const ATTRIBUTE_ID& aCadstarAttributeID );
213
214 PART::DEFINITION::PIN getPartDefinitionPin( const PART& aCadstarPart, const GATE_ID& aGateID,
215 const TERMINAL_ID& aTerminalID );
216
217 //Helper Functions for obtaining individual elements as KiCad elements:
219
220 int getKiCadUnitNumberFromGate( const GATE_ID& aCadstarGateID );
221 TEXT_SPIN_STYLE getSpinStyle( const long long& aCadstarOrientation, bool aMirror );
222 TEXT_SPIN_STYLE getSpinStyle( const EDA_ANGLE& aOrientation );
223 ALIGNMENT mirrorX( const ALIGNMENT& aCadstarAlignment );
224 ALIGNMENT rotate180( const ALIGNMENT& aCadstarAlignment );
225
226 //General Graphical manipulation functions
227
228 LIB_SYMBOL* getScaledLibPart( const LIB_SYMBOL* aSymbol, long long aScalingFactorNumerator,
229 long long aScalingFactorDenominator );
230
231 void fixUpLibraryPins( LIB_SYMBOL* aSymbolToFix, int aGateNumber );
232
233 std::pair<VECTOR2I, wxSize> getFigureExtentsKiCad( const FIGURE& aCadstarFigure );
234
235 VECTOR2I getKiCadPoint( const VECTOR2I& aCadstarPoint );
236
237 wxPoint getKiCadLibraryPoint( const wxPoint& aCadstarPoint, const wxPoint& aCadstarCentre );
238
239 VECTOR2I applyTransform( const VECTOR2I& aPoint, const VECTOR2I& aMoveVector = { 0, 0 },
240 const EDA_ANGLE& aRotation = ANGLE_0,
241 const double& aScalingFactor = 1.0,
242 const VECTOR2I& aTransformCentre = { 0, 0 },
243 const bool& aMirrorInvert = false );
244
245 int getKiCadLength( long long aCadstarLength )
246 {
247 int mod = aCadstarLength % KiCadUnitDivider;
248 int absmod = sign( mod ) * mod;
249 int offset = 0;
250
251 // Round half-way cases away from zero
252 if( absmod >= KiCadUnitDivider / 2 )
253 offset = sign( aCadstarLength );
254
255 return ( aCadstarLength / KiCadUnitDivider ) + offset;
256 }
257
258 EDA_ANGLE getAngle( const long long& aCadstarAngle )
259 {
260 // CADSTAR v6 (which outputted Schematic Format Version 8) and earlier used 1/10 degree
261 // as the unit for angles/orientations. It is assumed that CADSTAR version 7 (i.e. Schematic
262 // Format Version 9 and later) is the version that introduced 1/1000 degree for angles.
263 if( Header.Format.Version > 8 )
264 {
265 return EDA_ANGLE( (double) aCadstarAngle / 1000.0, DEGREES_T );
266 }
267 else
268 {
269 return EDA_ANGLE( (double) aCadstarAngle, TENTHS_OF_A_DEGREE_T );
270 }
271 }
272
273 long long getCadstarAngle( const EDA_ANGLE& aAngle )
274 {
275 // CADSTAR v6 (which outputted Schematic Format Version 8) and earlier used 1/10 degree
276 // as the unit for angles/orientations. It is assumed that CADSTAR version 7 (i.e. Schematic
277 // Format Version 9 and later) is the version that introduced 1/1000 degree for angles.
278 if( Header.Format.Version > 8 )
279 {
280 return KiROUND( aAngle.AsDegrees() * 1000.0 );
281 }
282 else
283 {
284 return aAngle.AsTenthsOfADegree();
285 }
286 }
287
293 double getPolarRadius( const VECTOR2I& aPoint );
294
295}; // CADSTAR_SCH_ARCHIVE_LOADER
296
297
298#endif // CADSTAR_SCH_ARCHIVE_LOADER_H_
long TERMINAL_ID
Terminal is the pin identifier in the schematic.
ALIGNMENT
From CADSTAR Help: "Text Alignment enables you to define the position of an alignment origin for all ...
wxString LAYER_ID
ID of a Sheet (if schematic) or board Layer (if PCB)
JUSTIFICATION
From CADSTAR Help: "Multi Line Text can also be justified as Left, Centre or Right.
PROGRESS_REPORTER * m_progressReporter
ELECTRICAL_PINTYPE getKiCadPinType(const PART::PIN_TYPE &aPinType)
ROUTECODE getRouteCode(const ROUTECODE_ID &aCadstarRouteCodeID)
void applyToLibraryFieldAttribute(const ATTRIBUTE_LOCATION &aCadstarAttrLoc, wxPoint aSymbolOrigin, LIB_FIELD *aKiCadField)
std::pair< VECTOR2I, wxSize > getFigureExtentsKiCad(const FIGURE &aCadstarFigure)
std::map< BLOCK_PIN_ID, SCH_HIERLABEL * > m_sheetPinMap
Map between Cadstar and KiCad Sheets Pins.
VECTOR2I applyTransform(const VECTOR2I &aPoint, const VECTOR2I &aMoveVector={ 0, 0 }, const EDA_ANGLE &aRotation=ANGLE_0, const double &aScalingFactor=1.0, const VECTOR2I &aTransformCentre={ 0, 0 }, const bool &aMirrorInvert=false)
std::map< SYMBOL_ID, SCH_GLOBALLABEL * > m_globalLabelsMap
Map between Cadstar and KiCad Global Labels.
SCH_SYMBOL * loadSchematicSymbol(const SYMBOL &aCadstarSymbol, const LIB_SYMBOL &aKiCadPart, EDA_ANGLE &aComponentOrientation)
SCH_PLUGIN::SCH_PLUGIN_RELEASER * m_plugin
void applyTextSettings(EDA_TEXT *aKiCadTextItem, const TEXTCODE_ID &aCadstarTextCodeID, const ALIGNMENT &aCadstarAlignment, const JUSTIFICATION &aCadstarJustification, const long long aCadstarOrientAngle=0, bool aMirrored=false)
wxString getAttributeName(const ATTRIBUTE_ID &aCadstarAttributeID)
PART getPart(const PART_ID &aCadstarPartID)
std::vector< LAYER_ID > findOrphanSheets()
std::map< BUS_ID, std::shared_ptr< BUS_ALIAS > > m_busesMap
Map of Cadstar and KiCad Buses.
int getSheetNumber(LAYER_ID aCadstarSheetID)
SCH_TEXT * getKiCadSchText(const TEXT &aCadstarTextElement)
std::map< TERMINAL_ID, wxString > TERMINAL_TO_PINNUM_MAP
Map between a terminal ID in a symbol definition to the pin number that should be imported into KiCad...
wxString getNetName(const NET_SCH &aNet)
std::pair< BLOCK_ID, TERMINAL_ID > BLOCK_PIN_ID
std::map< SYMBOL_ID, SCH_SYMBOL * > m_powerSymMap
Map between Cadstar and KiCad Power Symbols.
std::pair< PART_ID, GATE_ID > PART_GATE_ID
PART::DEFINITION::PIN getPartDefinitionPin(const PART &aCadstarPart, const GATE_ID &aGateID, const TERMINAL_ID &aTerminalID)
wxPoint getKiCadLibraryPoint(const wxPoint &aCadstarPoint, const wxPoint &aCadstarCentre)
void Load(SCHEMATIC *aSchematic, SCH_SHEET *aRootSheet, SCH_PLUGIN::SCH_PLUGIN_RELEASER *aSchPlugin, const wxFileName &aLibraryFileName)
Loads a CADSTAR PCB Archive file into the KiCad BOARD object given.
int getTextHeightFromTextCode(const TEXTCODE_ID &aCadstarTextCodeID)
TEXT_SPIN_STYLE getSpinStyle(const long long &aCadstarOrientation, bool aMirror)
double getPolarRadius(const VECTOR2I &aPoint)
CADSTAR_SCH_ARCHIVE_LOADER(wxString aFilename, REPORTER *aReporter, PROGRESS_REPORTER *aProgressReporter)
int getLineThickness(const LINECODE_ID &aCadstarLineCodeID)
TEXTCODE getTextCode(const TEXTCODE_ID &aCadstarTextCodeID)
std::map< wxString, LIB_SYMBOL * > m_powerSymLibMap
Map of KiCad Power Symbol Library items.
PLOT_DASH_TYPE getLineStyle(const LINECODE_ID &aCadstarLineCodeID)
std::map< PART_ID, TERMINAL_TO_PINNUM_MAP > m_pinNumsMap
Map of pin numbers in CADSTAR parts.
void loadSymDefIntoLibrary(const SYMDEF_ID &aSymdefID, const PART *aCadstarPart, const GATE_ID &aGateID, LIB_SYMBOL *aSymbol)
void loadSheetAndChildSheets(LAYER_ID aCadstarSheetID, const VECTOR2I &aPosition, VECTOR2I aSheetSize, const SCH_SHEET_PATH &aParentSheet)
void loadItemOntoKiCadSheet(LAYER_ID aCadstarSheetID, SCH_ITEM *aItem)
int getKiCadLength(long long aCadstarLength)
std::map< PART_ID, LIB_SYMBOL * > m_partMap
Map between Cadstar and KiCad Parts.
void loadGraphicStaightSegment(const VECTOR2I &aStartPoint, const VECTOR2I &aEndPoint, const LINECODE_ID &aCadstarLineCodeID, const LAYER_ID &aCadstarSheetID, const SCH_LAYER_ID &aKiCadSchLayerID, const VECTOR2I &aMoveVector={ 0, 0 }, const EDA_ANGLE &aRotation=ANGLE_0, const double &aScalingFactor=1.0, const VECTOR2I &aTransformCentre={ 0, 0 }, const bool &aMirrorInvert=false)
bool isAttributeVisible(const ATTRIBUTE_ID &aCadstarAttributeID)
VECTOR2I getKiCadPoint(const VECTOR2I &aCadstarPoint)
EDA_ANGLE getAngle(const long long &aCadstarAngle)
void loadFigure(const FIGURE &aCadstarFigure, const LAYER_ID &aCadstarSheetIDOverride, SCH_LAYER_ID aKiCadSchLayerID, const VECTOR2I &aMoveVector={ 0, 0 }, const EDA_ANGLE &aRotation=ANGLE_0, const double &aScalingFactor=1.0, const VECTOR2I &aTransformCentre={ 0, 0 }, const bool &aMirrorInvert=false)
void loadLibrarySymbolShapeVertices(const std::vector< VERTEX > &aCadstarVertices, wxPoint aSymbolOrigin, LIB_SYMBOL *aSymbol, int aGateNumber, int aLineThickness)
std::map< PART_GATE_ID, SYMDEF_ID > m_partSymbolsMap
Map holding the symbols loaded so far for a particular PART_ID and GATE_ID.
wxPoint m_designCenter
Used for calculating the required offset to apply to the Cadstar design so that it fits in KiCad canv...
LIB_SYMBOL * getScaledLibPart(const LIB_SYMBOL *aSymbol, long long aScalingFactorNumerator, long long aScalingFactorDenominator)
SYMDEF_ID getSymDefFromName(const wxString &aSymdefName, const wxString &aSymDefAlternate)
std::map< LAYER_ID, SCH_SHEET * > m_sheetMap
Map between Cadstar and KiCad Sheets.
POINT getLocationOfNetElement(const NET_SCH &aNet, const NETELEMENT_ID &aNetElementID)
void fixUpLibraryPins(LIB_SYMBOL *aSymbolToFix, int aGateNumber)
ALIGNMENT rotate180(const ALIGNMENT &aCadstarAlignment)
long long getCadstarAngle(const EDA_ANGLE &aAngle)
void loadChildSheets(LAYER_ID aCadstarSheetID, const SCH_SHEET_PATH &aSheet)
ALIGNMENT mirrorX(const ALIGNMENT &aCadstarAlignment)
int getComponentOrientation(const EDA_ANGLE &aOrientAngle, EDA_ANGLE &aReturnedOrientation)
void loadShapeVertices(const std::vector< VERTEX > &aCadstarVertices, LINECODE_ID aCadstarLineCodeID, LAYER_ID aCadstarSheetID, SCH_LAYER_ID aKiCadSchLayerID, const VECTOR2I &aMoveVector={ 0, 0 }, const EDA_ANGLE &aRotation=ANGLE_0, const double &aScalingFactor=1.0, const VECTOR2I &aTransformCentre={ 0, 0 }, const bool &aMirrorInvert=false)
int getKiCadUnitNumberFromGate(const GATE_ID &aCadstarGateID)
void loadSymbolFieldAttribute(const ATTRIBUTE_LOCATION &aCadstarAttrLoc, const EDA_ANGLE &aComponentOrientation, bool aIsMirrored, SCH_FIELD *aKiCadField)
Represents a CADSTAR Schematic Archive (*.csa) file.
int KiCadUnitDivider
Use this value to convert units in this CSA file to KiCad units.
int AsTenthsOfADegree() const
Definition: eda_angle.h:151
double AsDegrees() const
Definition: eda_angle.h:149
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:73
Field object used in symbol libraries.
Definition: lib_field.h:60
Define a library symbol object.
Definition: lib_symbol.h:99
A progress reporter interface for use in multi-threaded environments.
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:71
Holds all the data relating to one schematic.
Definition: schematic.h:60
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:50
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:145
Helper object to release a SCH_PLUGIN in the context of a potential thrown exception through its dest...
Definition: sch_io_mgr.h:480
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:55
Schematic symbol object.
Definition: sch_symbol.h:79
constexpr double SCH_IU_PER_MM
@ TENTHS_OF_A_DEGREE_T
Definition: eda_angle.h:30
@ DEGREES_T
Definition: eda_angle.h:31
static constexpr EDA_ANGLE & ANGLE_0
Definition: eda_angle.h:423
SCH_LAYER_ID
Eeschema drawing layers.
Definition: layer_ids.h:341
ELECTRICAL_PINTYPE
The symbol library pin object electrical types used in ERC tests.
Definition: pin_type.h:36
Plot settings, and plotting engines (PostScript, Gerber, HPGL and DXF)
PLOT_DASH_TYPE
Dashed line types.
Definition: stroke_params.h:47
long Version
Archive version number (e.g.
Represents a point in x,y coordinates.
int sign(T val)
Definition: util.h:111
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:80