2020-09-08 19:51:22 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2021-01-31 20:47:36 +00:00
|
|
|
* Copyright (C) 2020-2021 Roberto Fernandez Bautista <roberto.fer.bau@gmail.com>
|
|
|
|
* Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-09-08 19:51:22 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file cadstar_sch_archive_loader.h
|
|
|
|
* @brief Loads a csa file into a KiCad SCHEMATIC object
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CADSTAR_SCH_ARCHIVE_LOADER_H_
|
|
|
|
#define CADSTAR_SCH_ARCHIVE_LOADER_H_
|
|
|
|
|
|
|
|
#include <sch_plugins/cadstar/cadstar_sch_archive_parser.h>
|
|
|
|
|
2020-09-26 18:23:13 +00:00
|
|
|
#include <layers_id_colors_and_visibility.h> // SCH_LAYER_ID
|
|
|
|
#include <plotter.h> // PLOT_DASH_TYPE
|
2021-02-02 15:37:10 +00:00
|
|
|
#include <pin_type.h> // ELECTRICAL_PINTYPE
|
2020-09-20 21:08:46 +00:00
|
|
|
#include <sch_io_mgr.h>
|
2020-10-24 14:45:37 +00:00
|
|
|
#include <wx/filename.h>
|
2020-09-20 21:08:46 +00:00
|
|
|
|
2020-11-26 18:11:06 +00:00
|
|
|
class BUS_ALIAS;
|
2020-09-20 21:08:46 +00:00
|
|
|
class EDA_TEXT;
|
|
|
|
class LABEL_SPIN_STYLE;
|
|
|
|
class LIB_FIELD;
|
2021-06-10 18:51:46 +00:00
|
|
|
class LIB_SYMBOL;
|
2021-06-10 14:10:55 +00:00
|
|
|
class SCH_SYMBOL;
|
2020-09-27 02:35:24 +00:00
|
|
|
class SCH_ITEM;
|
2020-09-20 21:08:46 +00:00
|
|
|
class SCH_FIELD;
|
2020-09-23 20:39:11 +00:00
|
|
|
class SCH_GLOBALLABEL;
|
|
|
|
class SCH_HIERLABEL;
|
|
|
|
class SCH_SHEET;
|
2020-11-22 19:55:21 +00:00
|
|
|
class SCH_SHEET_PATH;
|
2020-09-27 02:35:24 +00:00
|
|
|
class SCH_TEXT;
|
2020-09-20 21:08:46 +00:00
|
|
|
class SCHEMATIC;
|
2020-09-08 19:51:22 +00:00
|
|
|
|
|
|
|
class CADSTAR_SCH_ARCHIVE_LOADER : public CADSTAR_SCH_ARCHIVE_PARSER
|
|
|
|
{
|
|
|
|
public:
|
2021-03-31 21:33:30 +00:00
|
|
|
explicit CADSTAR_SCH_ARCHIVE_LOADER( wxString aFilename, REPORTER* aReporter )
|
2020-09-08 19:51:22 +00:00
|
|
|
: CADSTAR_SCH_ARCHIVE_PARSER( aFilename )
|
|
|
|
{
|
2021-02-13 20:00:17 +00:00
|
|
|
m_schematic = nullptr;
|
|
|
|
m_rootSheet = nullptr;
|
|
|
|
m_plugin = nullptr;
|
|
|
|
m_designCenter.x = 0;
|
|
|
|
m_designCenter.y = 0;
|
2021-03-31 21:33:30 +00:00
|
|
|
m_reporter = aReporter;
|
2020-09-08 19:51:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
~CADSTAR_SCH_ARCHIVE_LOADER()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Loads a CADSTAR PCB Archive file into the KiCad BOARD object given
|
2020-10-24 14:45:37 +00:00
|
|
|
* @param aSchematic Schematic to add the design onto
|
|
|
|
* @param aRootSheet Root sheet to add the design onto
|
2020-09-08 19:51:22 +00:00
|
|
|
*/
|
2021-02-13 20:00:17 +00:00
|
|
|
void Load( SCHEMATIC* aSchematic, SCH_SHEET* aRootSheet,
|
2020-10-27 00:25:08 +00:00
|
|
|
SCH_PLUGIN::SCH_PLUGIN_RELEASER* aSchPlugin, const wxFileName& aLibraryFileName );
|
2020-09-08 19:51:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
private:
|
2020-09-23 20:39:11 +00:00
|
|
|
typedef std::pair<BLOCK_ID, TERMINAL_ID> BLOCK_PIN_ID;
|
2021-03-28 18:47:48 +00:00
|
|
|
typedef std::pair<PART_ID, GATE_ID> PART_GATE_ID;
|
2021-02-13 20:00:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Map between a terminal ID in a symbol definition to the pin number that should
|
|
|
|
* be imported into KiCad.
|
|
|
|
*/
|
2021-02-03 20:06:41 +00:00
|
|
|
typedef std::map<TERMINAL_ID, wxString> TERMINAL_TO_PINNUM_MAP;
|
2020-09-23 20:39:11 +00:00
|
|
|
|
2021-03-31 21:33:30 +00:00
|
|
|
REPORTER* m_reporter;
|
2021-02-13 20:00:17 +00:00
|
|
|
SCHEMATIC* m_schematic;
|
|
|
|
SCH_SHEET* m_rootSheet;
|
|
|
|
SCH_PLUGIN::SCH_PLUGIN_RELEASER* m_plugin;
|
|
|
|
wxFileName m_libraryFileName;
|
|
|
|
wxPoint m_designCenter; ///< Used for calculating the required
|
|
|
|
///< offset to apply to the Cadstar design
|
|
|
|
///< so that it fits in KiCad canvas
|
|
|
|
std::map<LAYER_ID, SCH_SHEET*> m_sheetMap; ///< Map between Cadstar and KiCad Sheets
|
2020-09-23 20:39:11 +00:00
|
|
|
std::map<BLOCK_PIN_ID, SCH_HIERLABEL*>
|
2021-02-13 20:00:17 +00:00
|
|
|
m_sheetPinMap; ///< Map between Cadstar and KiCad Sheets Pins
|
2021-06-10 18:51:46 +00:00
|
|
|
std::map<PART_ID, LIB_SYMBOL*> m_partMap; ///< Map between Cadstar and KiCad Parts
|
2021-03-28 18:47:48 +00:00
|
|
|
std::map<PART_GATE_ID, SYMDEF_ID> m_partSymbolsMap; ///< Map holding the symbols loaded so far
|
|
|
|
/// for a particular PART_ID and GATE_ID
|
2021-02-13 20:00:17 +00:00
|
|
|
std::map<PART_ID, TERMINAL_TO_PINNUM_MAP> m_pinNumsMap; ///< Map of pin numbers in CADSTAR parts
|
2021-06-10 18:51:46 +00:00
|
|
|
std::map<wxString, LIB_SYMBOL*> m_powerSymLibMap; ///< Map of KiCad Power Symbol Library items
|
2021-06-10 14:10:55 +00:00
|
|
|
std::map<SYMBOL_ID, SCH_SYMBOL*>
|
2021-02-13 20:00:17 +00:00
|
|
|
m_powerSymMap; ///< Map between Cadstar and KiCad Power Symbols
|
2020-09-23 20:39:11 +00:00
|
|
|
std::map<SYMBOL_ID, SCH_GLOBALLABEL*>
|
2021-02-13 20:00:17 +00:00
|
|
|
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
|
2020-09-19 22:05:02 +00:00
|
|
|
|
|
|
|
void loadSheets();
|
2020-09-23 20:39:11 +00:00
|
|
|
void loadHierarchicalSheetPins();
|
2020-09-20 21:08:46 +00:00
|
|
|
void loadPartsLibrary();
|
|
|
|
void loadSchematicSymbolInstances();
|
2020-09-26 13:27:03 +00:00
|
|
|
void loadBusses();
|
2020-09-23 20:39:11 +00:00
|
|
|
void loadNets();
|
2020-09-26 18:23:13 +00:00
|
|
|
void loadFigures();
|
2020-09-27 02:35:24 +00:00
|
|
|
void loadTexts();
|
2020-09-27 12:19:51 +00:00
|
|
|
void loadDocumentationSymbols();
|
2020-12-13 17:28:00 +00:00
|
|
|
void loadTextVariables();
|
2020-09-19 22:05:02 +00:00
|
|
|
|
2020-09-20 21:08:46 +00:00
|
|
|
//Helper Functions for loading sheets
|
2020-09-19 22:05:02 +00:00
|
|
|
void loadSheetAndChildSheets( LAYER_ID aCadstarSheetID, wxPoint aPosition, wxSize aSheetSize,
|
2020-11-22 19:55:21 +00:00
|
|
|
const SCH_SHEET_PATH& aParentSheet );
|
2020-09-27 12:19:51 +00:00
|
|
|
|
2020-11-22 19:55:21 +00:00
|
|
|
void loadChildSheets( LAYER_ID aCadstarSheetID, const SCH_SHEET_PATH& aSheet );
|
2020-09-27 12:19:51 +00:00
|
|
|
|
2020-09-19 22:05:02 +00:00
|
|
|
std::vector<LAYER_ID> findOrphanSheets();
|
2020-09-27 12:19:51 +00:00
|
|
|
|
|
|
|
int getSheetNumber( LAYER_ID aCadstarSheetID );
|
|
|
|
|
|
|
|
void loadItemOntoKiCadSheet( LAYER_ID aCadstarSheetID, SCH_ITEM* aItem );
|
2020-09-19 22:05:02 +00:00
|
|
|
|
2020-09-23 20:39:11 +00:00
|
|
|
//Helper Functions for loading library items
|
|
|
|
void loadSymDefIntoLibrary( const SYMDEF_ID& aSymdefID, const PART* aCadstarPart,
|
2021-06-10 18:51:46 +00:00
|
|
|
const GATE_ID& aGateID, LIB_SYMBOL* aSymbol );
|
2020-09-27 12:19:51 +00:00
|
|
|
|
2020-09-23 20:39:11 +00:00
|
|
|
void loadLibrarySymbolShapeVertices( const std::vector<VERTEX>& aCadstarVertices,
|
2021-06-10 18:51:46 +00:00
|
|
|
wxPoint aSymbolOrigin, LIB_SYMBOL* aSymbol, int aGateNumber );
|
2020-09-27 12:19:51 +00:00
|
|
|
|
2021-01-27 00:09:58 +00:00
|
|
|
void applyToLibraryFieldAttribute( const ATTRIBUTE_LOCATION& aCadstarAttrLoc,
|
2020-09-23 20:39:11 +00:00
|
|
|
wxPoint aSymbolOrigin, LIB_FIELD* aKiCadField );
|
|
|
|
|
|
|
|
//Helper Functions for loading symbols in schematic
|
2021-06-10 18:51:46 +00:00
|
|
|
SCH_SYMBOL* loadSchematicSymbol( const SYMBOL& aCadstarSymbol, const LIB_SYMBOL& aKiCadPart,
|
2020-09-20 21:08:46 +00:00
|
|
|
double& aComponentOrientationDeciDeg );
|
2020-09-27 12:19:51 +00:00
|
|
|
|
|
|
|
void loadSymbolFieldAttribute( const ATTRIBUTE_LOCATION& aCadstarAttrLoc,
|
2021-01-27 00:09:58 +00:00
|
|
|
const double& aComponentOrientationDeciDeg, bool aIsMirrored,
|
|
|
|
SCH_FIELD* aKiCadField );
|
2020-09-27 12:19:51 +00:00
|
|
|
|
2020-11-22 01:50:04 +00:00
|
|
|
int getComponentOrientation( double aOrientAngleDeciDeg, double& aReturnedOrientationDeciDeg );
|
2020-09-20 21:08:46 +00:00
|
|
|
|
2020-09-23 20:39:11 +00:00
|
|
|
//Helper functions for loading nets
|
2020-09-27 12:19:51 +00:00
|
|
|
POINT getLocationOfNetElement( const NET_SCH& aNet, const NETELEMENT_ID& aNetElementID );
|
|
|
|
|
2020-09-23 20:39:11 +00:00
|
|
|
wxString getNetName( const NET_SCH& aNet );
|
2020-09-19 22:05:02 +00:00
|
|
|
|
2020-09-26 18:23:13 +00:00
|
|
|
//Helper functions for loading figures / graphical items
|
2020-11-21 22:56:56 +00:00
|
|
|
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 );
|
|
|
|
|
2020-09-26 18:23:13 +00:00
|
|
|
void loadShapeVertices( const std::vector<VERTEX>& aCadstarVertices,
|
2020-09-27 12:19:51 +00:00
|
|
|
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 );
|
|
|
|
|
2020-09-27 02:35:24 +00:00
|
|
|
void loadFigure( const FIGURE& aCadstarFigure, const LAYER_ID& aCadstarSheetIDOverride,
|
2020-09-27 12:19:51 +00:00
|
|
|
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 );
|
|
|
|
|
|
|
|
//Helper functions for loading text elements
|
2021-04-02 18:50:30 +00:00
|
|
|
void applyTextSettings( EDA_TEXT* aKiCadTextItem,
|
|
|
|
const TEXTCODE_ID& aCadstarTextCodeID,
|
|
|
|
const ALIGNMENT& aCadstarAlignment,
|
|
|
|
const JUSTIFICATION& aCadstarJustification,
|
|
|
|
const long long aCadstarOrientAngle = 0,
|
|
|
|
bool aMirrored = false );
|
|
|
|
|
2020-09-27 12:19:51 +00:00
|
|
|
SCH_TEXT* getKiCadSchText( const TEXT& aCadstarTextElement );
|
|
|
|
|
2020-09-26 18:23:13 +00:00
|
|
|
|
2020-09-23 20:39:11 +00:00
|
|
|
//Helper Functions for obtaining CADSTAR elements from the parsed structures
|
2020-09-20 21:08:46 +00:00
|
|
|
SYMDEF_ID getSymDefFromName( const wxString& aSymdefName, const wxString& aSymDefAlternate );
|
2021-01-27 00:09:58 +00:00
|
|
|
bool isAttributeVisible( const ATTRIBUTE_ID& aCadstarAttributeID );
|
2020-09-27 12:19:51 +00:00
|
|
|
|
|
|
|
int getLineThickness( const LINECODE_ID& aCadstarLineCodeID );
|
|
|
|
PLOT_DASH_TYPE getLineStyle( const LINECODE_ID& aCadstarLineCodeID );
|
|
|
|
PART getPart( const PART_ID& aCadstarPartID );
|
|
|
|
ROUTECODE getRouteCode( const ROUTECODE_ID& aCadstarRouteCodeID );
|
|
|
|
TEXTCODE getTextCode( const TEXTCODE_ID& aCadstarTextCodeID );
|
|
|
|
wxString getAttributeName( const ATTRIBUTE_ID& aCadstarAttributeID );
|
|
|
|
|
2020-09-20 21:08:46 +00:00
|
|
|
PART::DEFINITION::PIN getPartDefinitionPin(
|
|
|
|
const PART& aCadstarPart, const GATE_ID& aGateID, const TERMINAL_ID& aTerminalID );
|
2020-09-19 22:05:02 +00:00
|
|
|
|
2020-09-27 13:10:59 +00:00
|
|
|
//Helper Functions for obtaining individual elements as KiCad elements:
|
2021-02-02 15:37:10 +00:00
|
|
|
ELECTRICAL_PINTYPE getKiCadPinType( const PART::PIN_TYPE& aPinType );
|
|
|
|
|
2020-09-20 21:08:46 +00:00
|
|
|
int getKiCadUnitNumberFromGate( const GATE_ID& aCadstarGateID );
|
|
|
|
LABEL_SPIN_STYLE getSpinStyle( const long long& aCadstarOrientation, bool aMirror );
|
2020-09-26 13:27:03 +00:00
|
|
|
LABEL_SPIN_STYLE getSpinStyleDeciDeg( const double& aOrientationDeciDeg );
|
2021-04-02 18:50:30 +00:00
|
|
|
ALIGNMENT mirrorX( const ALIGNMENT& aCadstarAlignment );
|
|
|
|
ALIGNMENT rotate180( const ALIGNMENT& aCadstarAlignment );
|
2020-09-27 02:35:24 +00:00
|
|
|
|
2020-09-27 13:10:59 +00:00
|
|
|
//General Graphical manipulation functions
|
2021-05-28 15:19:04 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL* getScaledLibPart( const LIB_SYMBOL* aSymbol, long long aScalingFactorNumerator,
|
|
|
|
long long aScalingFactorDenominator );
|
2021-05-28 15:19:04 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
void fixUpLibraryPins( LIB_SYMBOL* aSymbolToFix, int aGateNumber );
|
2021-05-28 18:39:08 +00:00
|
|
|
|
2020-09-19 22:05:02 +00:00
|
|
|
std::pair<wxPoint, wxSize> getFigureExtentsKiCad( const FIGURE& aCadstarFigure );
|
|
|
|
|
|
|
|
wxPoint getKiCadPoint( wxPoint aCadstarPoint );
|
|
|
|
|
2020-09-20 21:08:46 +00:00
|
|
|
wxPoint getKiCadLibraryPoint( wxPoint aCadstarPoint, wxPoint aCadstarCentre );
|
|
|
|
|
2020-09-27 12:19:51 +00:00
|
|
|
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 );
|
2020-09-19 22:05:02 +00:00
|
|
|
|
|
|
|
int getKiCadLength( long long aCadstarLength )
|
|
|
|
{
|
2021-02-18 18:02:49 +00:00
|
|
|
int mod = aCadstarLength % KiCadUnitDivider;
|
|
|
|
int absmod = sign( mod ) * mod;
|
|
|
|
int offset = 0;
|
|
|
|
|
|
|
|
// Round half-way cases away from zero
|
|
|
|
if( absmod >= KiCadUnitDivider / 2 )
|
|
|
|
offset = sign( aCadstarLength );
|
|
|
|
|
|
|
|
return ( aCadstarLength / KiCadUnitDivider ) + offset;
|
2020-09-19 22:05:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-24 14:45:37 +00:00
|
|
|
* @brief
|
|
|
|
* @param aCadstarAngle
|
|
|
|
* @return
|
2020-09-19 22:05:02 +00:00
|
|
|
*/
|
|
|
|
double getAngleTenthDegree( const long long& aCadstarAngle )
|
|
|
|
{
|
2020-11-22 01:50:04 +00:00
|
|
|
// CADSTAR v6 (which outputted Schematic Format Version 8) and earlier used 1/10 degree
|
|
|
|
// as the unit for angles/orientations. It is assumed that CADSTAR version 7 (i.e. Schematic
|
|
|
|
// Format Version 9 and later) is the version that introduced 1/1000 degree for angles.
|
|
|
|
if( Header.Format.Version > 8 )
|
|
|
|
{
|
|
|
|
return (double) aCadstarAngle / 100.0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return (double) aCadstarAngle;
|
|
|
|
}
|
2020-09-19 22:05:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-24 14:45:37 +00:00
|
|
|
* @brief
|
|
|
|
* @param aCadstarAngle
|
|
|
|
* @return
|
2020-09-19 22:05:02 +00:00
|
|
|
*/
|
|
|
|
double getAngleDegrees( const long long& aCadstarAngle )
|
|
|
|
{
|
2020-11-22 01:50:04 +00:00
|
|
|
return getAngleTenthDegree( aCadstarAngle ) / 10.0;
|
2020-09-19 22:05:02 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 18:50:30 +00:00
|
|
|
|
|
|
|
long long getCadstarAngle( const double& aAngleTenthDegree )
|
|
|
|
{
|
|
|
|
return KiROUND( ( aAngleTenthDegree / getAngleTenthDegree( aAngleTenthDegree ) )
|
|
|
|
* aAngleTenthDegree );
|
|
|
|
}
|
|
|
|
|
2020-09-19 22:05:02 +00:00
|
|
|
/**
|
2020-10-24 14:45:37 +00:00
|
|
|
* @brief
|
|
|
|
* @param aPoint
|
2020-09-19 22:05:02 +00:00
|
|
|
* @return Angle in decidegrees of the polar representation of the point, scaled 0..360
|
|
|
|
*/
|
|
|
|
double getPolarAngle( wxPoint aPoint );
|
2020-09-08 19:51:22 +00:00
|
|
|
|
2020-09-20 21:08:46 +00:00
|
|
|
/**
|
2020-10-24 14:45:37 +00:00
|
|
|
* @brief
|
|
|
|
* @param aPoint
|
2020-09-20 21:08:46 +00:00
|
|
|
* @return Radius of polar representation of the point
|
|
|
|
*/
|
|
|
|
double getPolarRadius( wxPoint aPoint );
|
|
|
|
|
2020-09-08 19:51:22 +00:00
|
|
|
}; // CADSTAR_SCH_ARCHIVE_LOADER
|
|
|
|
|
|
|
|
|
|
|
|
#endif // CADSTAR_SCH_ARCHIVE_LOADER_H_
|