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 const double ARC_ACCURACY = SCH_IU_PER_MM * 0.01; // 0.01mm
58
59 explicit CADSTAR_SCH_ARCHIVE_LOADER( wxString aFilename, REPORTER* aReporter,
60 PROGRESS_REPORTER* aProgressReporter ) :
62 {
63 m_schematic = nullptr;
64 m_rootSheet = nullptr;
65 m_plugin = nullptr;
66 m_designCenter.x = 0;
67 m_designCenter.y = 0;
68 m_reporter = aReporter;
69 m_progressReporter = aProgressReporter;
70 }
71
72
74 {
75 }
76
82 void Load( SCHEMATIC* aSchematic, SCH_SHEET* aRootSheet,
83 SCH_PLUGIN::SCH_PLUGIN_RELEASER* aSchPlugin, const wxFileName& aLibraryFileName );
84
85
86private:
87 typedef std::pair<BLOCK_ID, TERMINAL_ID> BLOCK_PIN_ID;
88 typedef std::pair<PART_ID, GATE_ID> PART_GATE_ID;
89
94 typedef std::map<TERMINAL_ID, wxString> TERMINAL_TO_PINNUM_MAP;
95
104 std::map<LAYER_ID, SCH_SHEET*> m_sheetMap;
105 std::map<BLOCK_PIN_ID, SCH_HIERLABEL*>
107 std::map<PART_ID, LIB_SYMBOL*> m_partMap;
108 std::map<PART_GATE_ID, SYMDEF_ID> m_partSymbolsMap;
110 std::map<PART_ID, TERMINAL_TO_PINNUM_MAP> m_pinNumsMap;
111 std::map<wxString, LIB_SYMBOL*> m_powerSymLibMap;
112 std::map<SYMBOL_ID, SCH_SYMBOL*>
114 std::map<SYMBOL_ID, SCH_GLOBALLABEL*>
116 std::map<BUS_ID, std::shared_ptr<BUS_ALIAS>> m_busesMap;
117
118 void loadSheets();
120 void loadPartsLibrary();
122 void loadBusses();
123 void loadNets();
124 void loadFigures();
125 void loadTexts();
127 void loadTextVariables();
128
129 //Helper Functions for loading sheets
130 void loadSheetAndChildSheets( LAYER_ID aCadstarSheetID, const VECTOR2I& aPosition,
131 VECTOR2I aSheetSize, const SCH_SHEET_PATH& aParentSheet );
132
133 void loadChildSheets( LAYER_ID aCadstarSheetID, const SCH_SHEET_PATH& aSheet );
134
135 std::vector<LAYER_ID> findOrphanSheets();
136
137 int getSheetNumber( LAYER_ID aCadstarSheetID );
138
139 void loadItemOntoKiCadSheet( LAYER_ID aCadstarSheetID, SCH_ITEM* aItem );
140
141 //Helper Functions for loading library items
142 void loadSymDefIntoLibrary( const SYMDEF_ID& aSymdefID, const PART* aCadstarPart,
143 const GATE_ID& aGateID, LIB_SYMBOL* aSymbol );
144
145 void loadLibrarySymbolShapeVertices( const std::vector<VERTEX>& aCadstarVertices,
146 VECTOR2I aSymbolOrigin, LIB_SYMBOL* aSymbol,
147 int aGateNumber, int aLineThickness );
148
149 void applyToLibraryFieldAttribute( const ATTRIBUTE_LOCATION& aCadstarAttrLoc,
150 VECTOR2I aSymbolOrigin, LIB_FIELD* aKiCadField );
151
152 //Helper Functions for loading symbols in schematic
153 SCH_SYMBOL* loadSchematicSymbol( const SYMBOL& aCadstarSymbol, const LIB_SYMBOL& aKiCadPart,
154 EDA_ANGLE& aComponentOrientation );
155
156 void loadSymbolFieldAttribute( const ATTRIBUTE_LOCATION& aCadstarAttrLoc,
157 const EDA_ANGLE& aComponentOrientation, bool aIsMirrored,
158 SCH_FIELD* aKiCadField );
159
160 int getComponentOrientation( const EDA_ANGLE& aOrientAngle, EDA_ANGLE& aReturnedOrientation );
161
162 //Helper functions for loading nets
163 POINT getLocationOfNetElement( const NET_SCH& aNet, const NETELEMENT_ID& aNetElementID );
164
165 wxString getNetName( const NET_SCH& aNet );
166
167 //Helper functions for loading figures / graphical items
168 void loadShapeVertices( const std::vector<VERTEX>& aCadstarVertices,
169 LINECODE_ID aCadstarLineCodeID, LAYER_ID aCadstarSheetID,
170 SCH_LAYER_ID aKiCadSchLayerID, const VECTOR2I& aMoveVector = { 0, 0 },
171 const EDA_ANGLE& aRotation = ANGLE_0,
172 const double& aScalingFactor = 1.0,
173 const VECTOR2I& aTransformCentre = { 0, 0 },
174 const bool& aMirrorInvert = false );
175
176 void loadFigure( const FIGURE& aCadstarFigure, const LAYER_ID& aCadstarSheetIDOverride,
177 SCH_LAYER_ID aKiCadSchLayerID, const VECTOR2I& aMoveVector = { 0, 0 },
178 const EDA_ANGLE& aRotation = ANGLE_0, const double& aScalingFactor = 1.0,
179 const VECTOR2I& aTransformCentre = { 0, 0 },
180 const bool& aMirrorInvert = false );
181
182 //Helper functions for loading text elements
183 void applyTextSettings( EDA_TEXT* aKiCadTextItem,
184 const TEXTCODE_ID& aCadstarTextCodeID,
185 const ALIGNMENT& aCadstarAlignment,
186 const JUSTIFICATION& aCadstarJustification,
187 const long long aCadstarOrientAngle = 0,
188 bool aMirrored = false );
189
190 SCH_TEXT* getKiCadSchText( const TEXT& aCadstarTextElement );
191
192
193 //Helper Functions for obtaining CADSTAR elements from the parsed structures
194 SYMDEF_ID getSymDefFromName( const wxString& aSymdefName, const wxString& aSymDefAlternate );
195 bool isAttributeVisible( const ATTRIBUTE_ID& aCadstarAttributeID );
196
197 int getLineThickness( const LINECODE_ID& aCadstarLineCodeID );
198 PLOT_DASH_TYPE getLineStyle( const LINECODE_ID& aCadstarLineCodeID );
199 PART getPart( const PART_ID& aCadstarPartID );
200 ROUTECODE getRouteCode( const ROUTECODE_ID& aCadstarRouteCodeID );
201 TEXTCODE getTextCode( const TEXTCODE_ID& aCadstarTextCodeID );
202 int getTextHeightFromTextCode( const TEXTCODE_ID& aCadstarTextCodeID );
203 wxString getAttributeName( const ATTRIBUTE_ID& aCadstarAttributeID );
204
205 PART::DEFINITION::PIN getPartDefinitionPin( const PART& aCadstarPart, const GATE_ID& aGateID,
206 const TERMINAL_ID& aTerminalID );
207
208 //Helper Functions for obtaining individual elements as KiCad elements:
210
211 int getKiCadUnitNumberFromGate( const GATE_ID& aCadstarGateID );
212 TEXT_SPIN_STYLE getSpinStyle( const long long& aCadstarOrientation, bool aMirror );
213 TEXT_SPIN_STYLE getSpinStyle( const EDA_ANGLE& aOrientation );
214 ALIGNMENT mirrorX( const ALIGNMENT& aCadstarAlignment );
215 ALIGNMENT rotate180( const ALIGNMENT& aCadstarAlignment );
216
217 //General Graphical manipulation functions
218
219 LIB_SYMBOL* getScaledLibPart( const LIB_SYMBOL* aSymbol, long long aScalingFactorNumerator,
220 long long aScalingFactorDenominator );
221
222 void fixUpLibraryPins( LIB_SYMBOL* aSymbolToFix, int aGateNumber );
223
224 std::pair<VECTOR2I, VECTOR2I> getFigureExtentsKiCad( const FIGURE& aCadstarFigure );
225
226 VECTOR2I getKiCadPoint( const VECTOR2I& aCadstarPoint );
227
228 VECTOR2I getKiCadLibraryPoint( const VECTOR2I& aCadstarPoint, const VECTOR2I& aCadstarCentre );
229
230 VECTOR2I applyTransform( const VECTOR2I& aPoint, const VECTOR2I& aMoveVector = { 0, 0 },
231 const EDA_ANGLE& aRotation = ANGLE_0,
232 const double& aScalingFactor = 1.0,
233 const VECTOR2I& aTransformCentre = { 0, 0 },
234 const bool& aMirrorInvert = false );
235
236 int getKiCadLength( long long aCadstarLength )
237 {
238 int mod = aCadstarLength % KiCadUnitDivider;
239 int absmod = sign( mod ) * mod;
240 int offset = 0;
241
242 // Round half-way cases away from zero
243 if( absmod >= KiCadUnitDivider / 2 )
244 offset = sign( aCadstarLength );
245
246 return ( aCadstarLength / KiCadUnitDivider ) + offset;
247 }
248
249 EDA_ANGLE getAngle( const long long& aCadstarAngle )
250 {
251 // CADSTAR v6 (which outputted Schematic Format Version 8) and earlier used 1/10 degree
252 // as the unit for angles/orientations. It is assumed that CADSTAR version 7 (i.e. Schematic
253 // Format Version 9 and later) is the version that introduced 1/1000 degree for angles.
254 if( Header.Format.Version > 8 )
255 {
256 return EDA_ANGLE( (double) aCadstarAngle / 1000.0, DEGREES_T );
257 }
258 else
259 {
260 return EDA_ANGLE( (double) aCadstarAngle, TENTHS_OF_A_DEGREE_T );
261 }
262 }
263
264 long long getCadstarAngle( const EDA_ANGLE& aAngle )
265 {
266 // CADSTAR v6 (which outputted Schematic Format Version 8) and earlier used 1/10 degree
267 // as the unit for angles/orientations. It is assumed that CADSTAR version 7 (i.e. Schematic
268 // Format Version 9 and later) is the version that introduced 1/1000 degree for angles.
269 if( Header.Format.Version > 8 )
270 {
271 return KiROUND( aAngle.AsDegrees() * 1000.0 );
272 }
273 else
274 {
275 return aAngle.AsTenthsOfADegree();
276 }
277 }
278
284 double getPolarRadius( const VECTOR2I& aPoint );
285
286}; // CADSTAR_SCH_ARCHIVE_LOADER
287
288
289#endif // CADSTAR_SCH_ARCHIVE_LOADER_H_
constexpr double SCH_IU_PER_MM
Definition: base_units.h:73
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)
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
void loadLibrarySymbolShapeVertices(const std::vector< VERTEX > &aCadstarVertices, VECTOR2I aSymbolOrigin, LIB_SYMBOL *aSymbol, int aGateNumber, int aLineThickness)
VECTOR2I getKiCadLibraryPoint(const VECTOR2I &aCadstarPoint, const VECTOR2I &aCadstarCentre)
PART::DEFINITION::PIN getPartDefinitionPin(const PART &aCadstarPart, const GATE_ID &aGateID, const TERMINAL_ID &aTerminalID)
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.
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)
std::map< PART_GATE_ID, SYMDEF_ID > m_partSymbolsMap
Map holding the symbols loaded so far for a particular PART_ID and GATE_ID.
void applyToLibraryFieldAttribute(const ATTRIBUTE_LOCATION &aCadstarAttrLoc, VECTOR2I aSymbolOrigin, LIB_FIELD *aKiCadField)
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::pair< VECTOR2I, VECTOR2I > getFigureExtentsKiCad(const FIGURE &aCadstarFigure)
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:72
Field object used in symbol libraries.
Definition: lib_field.h:61
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:61
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:51
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:147
Helper object to release a SCH_PLUGIN in the context of a potential thrown exception through its dest...
Definition: sch_io_mgr.h:535
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:57
Schematic symbol object.
Definition: sch_symbol.h:81
@ 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:429
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:48
long Version
Archive version number (e.g.
Represents a point in x,y coordinates.
int sign(T val)
Definition: util.h:124
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:85