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 
37 class BUS_ALIAS;
38 class EDA_TEXT;
39 class LABEL_SPIN_STYLE;
40 class LIB_FIELD;
41 class LIB_SYMBOL;
42 class SCH_SYMBOL;
43 class SCH_ITEM;
44 class SCH_FIELD;
45 class SCH_GLOBALLABEL;
46 class SCH_HIERLABEL;
47 class SCH_SHEET;
48 class SCH_SHEET_PATH;
49 class SCH_TEXT;
50 class SCHEMATIC;
51 
53 {
54 public:
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 ) :
60  CADSTAR_SCH_ARCHIVE_PARSER( aFilename )
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 
85 private:
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 
99  wxFileName m_libraryFileName;
100  wxPoint m_designCenter;
101  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;
108  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 wxPoint& aPosition,
130  wxSize 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  double& aComponentOrientationDeciDeg );
154 
155  void loadSymbolFieldAttribute( const ATTRIBUTE_LOCATION& aCadstarAttrLoc,
156  const double& aComponentOrientationDeciDeg, bool aIsMirrored,
157  SCH_FIELD* aKiCadField );
158 
159  int getComponentOrientation( double aOrientAngleDeciDeg, double& aReturnedOrientationDeciDeg );
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 wxPoint& aStartPoint, const wxPoint& aEndPoint,
168  const LINECODE_ID& aCadstarLineCodeID, const LAYER_ID& aCadstarSheetID,
169  const SCH_LAYER_ID& aKiCadSchLayerID, const wxPoint& aMoveVector = { 0, 0 },
170  const double& aRotationAngleDeciDeg = 0.0, const double& aScalingFactor = 1.0,
171  const wxPoint& aTransformCentre = { 0, 0 }, const bool& aMirrorInvert = false );
172 
173  void loadShapeVertices( const std::vector<VERTEX>& aCadstarVertices,
174  LINECODE_ID aCadstarLineCodeID, LAYER_ID aCadstarSheetID, SCH_LAYER_ID aKiCadSchLayerID,
175  const wxPoint& aMoveVector = { 0, 0 }, const double& aRotationAngleDeciDeg = 0.0,
176  const double& aScalingFactor = 1.0, const wxPoint& aTransformCentre = { 0, 0 },
177  const bool& aMirrorInvert = false );
178 
179  void loadFigure( const FIGURE& aCadstarFigure, const LAYER_ID& aCadstarSheetIDOverride,
180  SCH_LAYER_ID aKiCadSchLayerID, const wxPoint& aMoveVector = { 0, 0 },
181  const double& aRotationAngleDeciDeg = 0.0, const double& aScalingFactor = 1.0,
182  const wxPoint& aTransformCentre = { 0, 0 }, const bool& aMirrorInvert = false );
183 
184  //Helper functions for loading text elements
185  void applyTextSettings( EDA_TEXT* aKiCadTextItem,
186  const TEXTCODE_ID& aCadstarTextCodeID,
187  const ALIGNMENT& aCadstarAlignment,
188  const JUSTIFICATION& aCadstarJustification,
189  const long long aCadstarOrientAngle = 0,
190  bool aMirrored = false );
191 
192  SCH_TEXT* getKiCadSchText( const TEXT& aCadstarTextElement );
193 
194 
195  //Helper Functions for obtaining CADSTAR elements from the parsed structures
196  SYMDEF_ID getSymDefFromName( const wxString& aSymdefName, const wxString& aSymDefAlternate );
197  bool isAttributeVisible( const ATTRIBUTE_ID& aCadstarAttributeID );
198 
199  int getLineThickness( const LINECODE_ID& aCadstarLineCodeID );
200  PLOT_DASH_TYPE getLineStyle( const LINECODE_ID& aCadstarLineCodeID );
201  PART getPart( const PART_ID& aCadstarPartID );
202  ROUTECODE getRouteCode( const ROUTECODE_ID& aCadstarRouteCodeID );
203  TEXTCODE getTextCode( const TEXTCODE_ID& aCadstarTextCodeID );
204  int getTextHeightFromTextCode( const TEXTCODE_ID& aCadstarTextCodeID );
205  wxString getAttributeName( const ATTRIBUTE_ID& aCadstarAttributeID );
206 
207  PART::DEFINITION::PIN getPartDefinitionPin(
208  const PART& aCadstarPart, const GATE_ID& aGateID, const TERMINAL_ID& aTerminalID );
209 
210  //Helper Functions for obtaining individual elements as KiCad elements:
212 
213  int getKiCadUnitNumberFromGate( const GATE_ID& aCadstarGateID );
214  LABEL_SPIN_STYLE getSpinStyle( const long long& aCadstarOrientation, bool aMirror );
215  LABEL_SPIN_STYLE getSpinStyleDeciDeg( const double& aOrientationDeciDeg );
216  ALIGNMENT mirrorX( const ALIGNMENT& aCadstarAlignment );
217  ALIGNMENT rotate180( const ALIGNMENT& aCadstarAlignment );
218 
219  //General Graphical manipulation functions
220 
221  LIB_SYMBOL* getScaledLibPart( const LIB_SYMBOL* aSymbol, long long aScalingFactorNumerator,
222  long long aScalingFactorDenominator );
223 
224  void fixUpLibraryPins( LIB_SYMBOL* aSymbolToFix, int aGateNumber );
225 
226  std::pair<wxPoint, wxSize> getFigureExtentsKiCad( const FIGURE& aCadstarFigure );
227 
228  wxPoint getKiCadPoint( const wxPoint& aCadstarPoint );
229 
230  wxPoint getKiCadLibraryPoint( const wxPoint& aCadstarPoint, const wxPoint& aCadstarCentre );
231 
232  wxPoint applyTransform( const wxPoint& aPoint, const wxPoint& aMoveVector = { 0, 0 },
233  const double& aRotationAngleDeciDeg = 0.0, const double& aScalingFactor = 1.0,
234  const wxPoint& aTransformCentre = { 0, 0 }, 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 
254  double getAngleTenthDegree( const long long& aCadstarAngle )
255  {
256  // CADSTAR v6 (which outputted Schematic Format Version 8) and earlier used 1/10 degree
257  // as the unit for angles/orientations. It is assumed that CADSTAR version 7 (i.e. Schematic
258  // Format Version 9 and later) is the version that introduced 1/1000 degree for angles.
259  if( Header.Format.Version > 8 )
260  {
261  return (double) aCadstarAngle / 100.0;
262  }
263  else
264  {
265  return (double) aCadstarAngle;
266  }
267  }
268 
274  double getAngleDegrees( const long long& aCadstarAngle )
275  {
276  return getAngleTenthDegree( aCadstarAngle ) / 10.0;
277  }
278 
279 
280  long long getCadstarAngle( const double& aAngleTenthDegree )
281  {
282  return KiROUND( ( aAngleTenthDegree / getAngleTenthDegree( aAngleTenthDegree ) )
283  * aAngleTenthDegree );
284  }
285 
291  double getPolarAngle( const wxPoint& aPoint );
292 
298  double getPolarRadius( const wxPoint& aPoint );
299 
300 }; // CADSTAR_SCH_ARCHIVE_LOADER
301 
302 
303 #endif // CADSTAR_SCH_ARCHIVE_LOADER_H_
int getKiCadUnitNumberFromGate(const GATE_ID &aCadstarGateID)
wxPoint m_designCenter
Used for calculating the required offset to apply to the Cadstar design so that it fits in KiCad canv...
int sign(T val)
Definition: util.h:104
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:49
ELECTRICAL_PINTYPE getKiCadPinType(const PART::PIN_TYPE &aPinType)
std::map< wxString, LIB_SYMBOL * > m_powerSymLibMap
Map of KiCad Power Symbol Library items.
SCH_SYMBOL * loadSchematicSymbol(const SYMBOL &aCadstarSymbol, const LIB_SYMBOL &aKiCadPart, double &aComponentOrientationDeciDeg)
Plot settings, and plotting engines (PostScript, Gerber, HPGL and DXF)
int getKiCadLength(long long aCadstarLength)
std::map< PART_ID, TERMINAL_TO_PINNUM_MAP > m_pinNumsMap
Map of pin numbers in CADSTAR parts.
std::pair< wxPoint, wxSize > getFigureExtentsKiCad(const FIGURE &aCadstarFigure)
void loadShapeVertices(const std::vector< VERTEX > &aCadstarVertices, LINECODE_ID aCadstarLineCodeID, LAYER_ID aCadstarSheetID, SCH_LAYER_ID aKiCadSchLayerID, const wxPoint &aMoveVector={ 0, 0 }, const double &aRotationAngleDeciDeg=0.0, const double &aScalingFactor=1.0, const wxPoint &aTransformCentre={ 0, 0 }, const bool &aMirrorInvert=false)
PART::DEFINITION::PIN getPartDefinitionPin(const PART &aCadstarPart, const GATE_ID &aGateID, const TERMINAL_ID &aTerminalID)
int getLineThickness(const LINECODE_ID &aCadstarLineCodeID)
Holds all the data relating to one schematic.
Definition: schematic.h:59
wxString getAttributeName(const ATTRIBUTE_ID &aCadstarAttributeID)
double getAngleTenthDegree(const long long &aCadstarAngle)
A progress reporter interface for use in multi-threaded environments.
double getPolarAngle(const wxPoint &aPoint)
std::map< LAYER_ID, SCH_SHEET * > m_sheetMap
Map between Cadstar and KiCad Sheets.
Field object used in symbol libraries.
Definition: lib_field.h:59
TEXTCODE getTextCode(const TEXTCODE_ID &aCadstarTextCodeID)
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 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.
long long getCadstarAngle(const double &aAngleTenthDegree)
void loadSheetAndChildSheets(LAYER_ID aCadstarSheetID, const wxPoint &aPosition, wxSize aSheetSize, const SCH_SHEET_PATH &aParentSheet)
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:70
void applyTextSettings(EDA_TEXT *aKiCadTextItem, const TEXTCODE_ID &aCadstarTextCodeID, const ALIGNMENT &aCadstarAlignment, const JUSTIFICATION &aCadstarJustification, const long long aCadstarOrientAngle=0, bool aMirrored=false)
POINT getLocationOfNetElement(const NET_SCH &aNet, const NETELEMENT_ID &aNetElementID)
wxPoint getKiCadLibraryPoint(const wxPoint &aCadstarPoint, const wxPoint &aCadstarCentre)
int getTextHeightFromTextCode(const TEXTCODE_ID &aCadstarTextCodeID)
std::pair< PART_ID, GATE_ID > PART_GATE_ID
Define a library symbol object.
Definition: lib_symbol.h:96
PLOT_DASH_TYPE getLineStyle(const LINECODE_ID &aCadstarLineCodeID)
double getPolarRadius(const wxPoint &aPoint)
void loadLibrarySymbolShapeVertices(const std::vector< VERTEX > &aCadstarVertices, wxPoint aSymbolOrigin, LIB_SYMBOL *aSymbol, int aGateNumber, int aLineThickness)
std::map< PART_ID, LIB_SYMBOL * > m_partMap
Map between Cadstar and KiCad Parts.
void loadSymbolFieldAttribute(const ATTRIBUTE_LOCATION &aCadstarAttrLoc, const double &aComponentOrientationDeciDeg, bool aIsMirrored, SCH_FIELD *aKiCadField)
void loadGraphicStaightSegment(const wxPoint &aStartPoint, const wxPoint &aEndPoint, const LINECODE_ID &aCadstarLineCodeID, const LAYER_ID &aCadstarSheetID, const SCH_LAYER_ID &aKiCadSchLayerID, const wxPoint &aMoveVector={ 0, 0 }, const double &aRotationAngleDeciDeg=0.0, const double &aScalingFactor=1.0, const wxPoint &aTransformCentre={ 0, 0 }, const bool &aMirrorInvert=false)
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:140
Represents a point in x,y coordinates.
ALIGNMENT
From CADSTAR Help: "Text Alignment enables you to define the position of an alignment origin for all ...
wxPoint applyTransform(const wxPoint &aPoint, const wxPoint &aMoveVector={ 0, 0 }, const double &aRotationAngleDeciDeg=0.0, const double &aScalingFactor=1.0, const wxPoint &aTransformCentre={ 0, 0 }, const bool &aMirrorInvert=false)
SCH_LAYER_ID
Eeschema drawing layers.
Definition: layer_ids.h:324
long TERMINAL_ID
Terminal is the pin identifier in the schematic.
SYMDEF_ID getSymDefFromName(const wxString &aSymdefName, const wxString &aSymDefAlternate)
std::map< SYMBOL_ID, SCH_GLOBALLABEL * > m_globalLabelsMap
Map between Cadstar and KiCad Global Labels.
std::map< BUS_ID, std::shared_ptr< BUS_ALIAS > > m_busesMap
Map of Cadstar and KiCad Buses.
SCH_TEXT * getKiCadSchText(const TEXT &aCadstarTextElement)
int getSheetNumber(LAYER_ID aCadstarSheetID)
void loadChildSheets(LAYER_ID aCadstarSheetID, const SCH_SHEET_PATH &aSheet)
void loadSymDefIntoLibrary(const SYMDEF_ID &aSymdefID, const PART *aCadstarPart, const GATE_ID &aGateID, LIB_SYMBOL *aSymbol)
PART getPart(const PART_ID &aCadstarPartID)
void loadItemOntoKiCadSheet(LAYER_ID aCadstarSheetID, SCH_ITEM *aItem)
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
std::pair< BLOCK_ID, TERMINAL_ID > BLOCK_PIN_ID
ROUTECODE getRouteCode(const ROUTECODE_ID &aCadstarRouteCodeID)
std::map< BLOCK_PIN_ID, SCH_HIERLABEL * > m_sheetPinMap
Map between Cadstar and KiCad Sheets Pins.
Represents a CADSTAR Schematic Archive (*.csa) file.
void applyToLibraryFieldAttribute(const ATTRIBUTE_LOCATION &aCadstarAttrLoc, wxPoint aSymbolOrigin, LIB_FIELD *aKiCadField)
SCH_PLUGIN::SCH_PLUGIN_RELEASER * m_plugin
ALIGNMENT mirrorX(const ALIGNMENT &aCadstarAlignment)
void fixUpLibraryPins(LIB_SYMBOL *aSymbolToFix, int aGateNumber)
std::vector< LAYER_ID > findOrphanSheets()
std::map< SYMBOL_ID, SCH_SYMBOL * > m_powerSymMap
Map between Cadstar and KiCad Power Symbols.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:54
PLOT_DASH_TYPE
Dashed line types.
Definition: plotter.h:104
int KiCadUnitDivider
Use this value to convert units in this CSA file to KiCad units.
constexpr double SCH_IU_PER_MM
Helper object to release a SCH_PLUGIN in the context of a potential thrown exception through its dest...
Definition: sch_io_mgr.h:479
JUSTIFICATION
From CADSTAR Help: "Multi Line Text can also be justified as Left, Centre or Right.
LABEL_SPIN_STYLE getSpinStyle(const long long &aCadstarOrientation, bool aMirror)
Schematic symbol object.
Definition: sch_symbol.h:78
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...
int getComponentOrientation(double aOrientAngleDeciDeg, double &aReturnedOrientationDeciDeg)
ELECTRICAL_PINTYPE
The symbol library pin object electrical types used in ERC tests.
Definition: pin_type.h:35
PROGRESS_REPORTER * m_progressReporter
wxPoint getKiCadPoint(const wxPoint &aCadstarPoint)
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:73
ALIGNMENT rotate180(const ALIGNMENT &aCadstarAlignment)
long Version
Archive version number (e.g.
CADSTAR_SCH_ARCHIVE_LOADER(wxString aFilename, REPORTER *aReporter, PROGRESS_REPORTER *aProgressReporter)
wxString getNetName(const NET_SCH &aNet)
wxString LAYER_ID
ID of a Sheet (if schematic) or board Layer (if PCB)
LABEL_SPIN_STYLE getSpinStyleDeciDeg(const double &aOrientationDeciDeg)
double getAngleDegrees(const long long &aCadstarAngle)
bool isAttributeVisible(const ATTRIBUTE_ID &aCadstarAttributeID)
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:182
void loadFigure(const FIGURE &aCadstarFigure, const LAYER_ID &aCadstarSheetIDOverride, SCH_LAYER_ID aKiCadSchLayerID, const wxPoint &aMoveVector={ 0, 0 }, const double &aRotationAngleDeciDeg=0.0, const double &aScalingFactor=1.0, const wxPoint &aTransformCentre={ 0, 0 }, const bool &aMirrorInvert=false)
LIB_SYMBOL * getScaledLibPart(const LIB_SYMBOL *aSymbol, long long aScalingFactorNumerator, long long aScalingFactorDenominator)