From 52d85015637c150e4f1fd1ca941ab1355078b25f Mon Sep 17 00:00:00 2001 From: Thomas Pointhuber Date: Mon, 26 Oct 2020 18:26:49 +0100 Subject: [PATCH] altium: initial parsing of designator and some parameters --- .../sch_plugins/altium/altium_parser_sch.cpp | 28 +++- .../sch_plugins/altium/altium_parser_sch.h | 21 ++- .../sch_plugins/altium/sch_altium_plugin.cpp | 125 +++++++++++++++--- .../sch_plugins/altium/sch_altium_plugin.h | 5 + 4 files changed, 160 insertions(+), 19 deletions(-) diff --git a/eeschema/sch_plugins/altium/altium_parser_sch.cpp b/eeschema/sch_plugins/altium/altium_parser_sch.cpp index 5b4b10ae76..2d2bcde5fc 100644 --- a/eeschema/sch_plugins/altium/altium_parser_sch.cpp +++ b/eeschema/sch_plugins/altium/altium_parser_sch.cpp @@ -567,6 +567,7 @@ ASCH_SHEET::ASCH_SHEET( const std::map& aProperties ) aProperties, "WORKSPACEORIENTATION", 0, 1, ASCH_SHEET_WORKSPACEORIENTATION::LANDSCAPE ); } + ASCH_DESIGNATOR::ASCH_DESIGNATOR( const std::map& aProperties ) { wxASSERT( PropertiesReadRecord( aProperties ) == ALTIUM_SCH_RECORD::DESIGNATOR ); @@ -578,12 +579,14 @@ ASCH_DESIGNATOR::ASCH_DESIGNATOR( const std::map& aPropertie name = ALTIUM_PARSER::PropertiesReadString( aProperties, "NAME", "" ); text = ALTIUM_PARSER::PropertiesReadString( aProperties, "TEXT", "" ); - orientation = ALTIUM_PARSER::PropertiesReadInt( aProperties, "ORIENTATION", 0 ); + orientation = PropertiesReadEnum( + aProperties, "ORIENTATION", 0, 3, ASCH_RECORD_ORIENTATION::RIGHTWARDS ); location = wxPoint( PropertiesReadKiCadUnitFrac( aProperties, "LOCATION.X" ), -PropertiesReadKiCadUnitFrac( aProperties, "LOCATION.Y" ) ); } + ASCH_BUS_ENTRY::ASCH_BUS_ENTRY( const std::map& aProperties ) { wxASSERT( PropertiesReadRecord( aProperties ) == ALTIUM_SCH_RECORD::BUS_ENTRY ); @@ -592,4 +595,27 @@ ASCH_BUS_ENTRY::ASCH_BUS_ENTRY( const std::map& aProperties -PropertiesReadKiCadUnitFrac( aProperties, "LOCATION.Y" ) ); corner = wxPoint( PropertiesReadKiCadUnitFrac( aProperties, "CORNER.X" ), -PropertiesReadKiCadUnitFrac( aProperties, "CORNER.Y" ) ); +} + + +ASCH_PARAMETER::ASCH_PARAMETER( const std::map& aProperties ) +{ + wxASSERT( PropertiesReadRecord( aProperties ) == ALTIUM_SCH_RECORD::PARAMETER ); + + ownerindex = + ALTIUM_PARSER::PropertiesReadInt( aProperties, "OWNERINDEX", ALTIUM_COMPONENT_NONE ); + ownerpartid = ALTIUM_PARSER::PropertiesReadInt( aProperties, "OWNERPARTID", 0 ); + + location = wxPoint( PropertiesReadKiCadUnitFrac( aProperties, "LOCATION.X" ), + -PropertiesReadKiCadUnitFrac( aProperties, "LOCATION.Y" ) ); + + orientation = PropertiesReadEnum( + aProperties, "ORIENTATION", 0, 3, ASCH_RECORD_ORIENTATION::RIGHTWARDS ); + + name = ALTIUM_PARSER::PropertiesReadString( aProperties, "NAME", "" ); + text = ALTIUM_PARSER::PropertiesReadString( aProperties, "TEXT", "" ); + + isHidden = ALTIUM_PARSER::PropertiesReadBool( aProperties, "ISHIDDEN", false ); + isMirrored = ALTIUM_PARSER::PropertiesReadBool( aProperties, "ISMIRRORED", false ); + isShowName = ALTIUM_PARSER::PropertiesReadBool( aProperties, "SHOWNAME", false ); } \ No newline at end of file diff --git a/eeschema/sch_plugins/altium/altium_parser_sch.h b/eeschema/sch_plugins/altium/altium_parser_sch.h index 57ce71c8df..cdf19f8236 100644 --- a/eeschema/sch_plugins/altium/altium_parser_sch.h +++ b/eeschema/sch_plugins/altium/altium_parser_sch.h @@ -568,7 +568,7 @@ struct ASCH_DESIGNATOR wxString name; wxString text; - int orientation; + ASCH_RECORD_ORIENTATION orientation; wxPoint location; explicit ASCH_DESIGNATOR( const std::map& aProperties ); @@ -583,4 +583,23 @@ struct ASCH_BUS_ENTRY explicit ASCH_BUS_ENTRY( const std::map& aProperties ); }; + +struct ASCH_PARAMETER +{ + int ownerindex; + int ownerpartid; + + wxPoint location; + ASCH_RECORD_ORIENTATION orientation; + + wxString name; + wxString text; + + bool isHidden; + bool isMirrored; + bool isShowName; + + explicit ASCH_PARAMETER( const std::map& aProperties ); +}; + #endif //ALTIUM_PARSER_SCH_H \ No newline at end of file diff --git a/eeschema/sch_plugins/altium/sch_altium_plugin.cpp b/eeschema/sch_plugins/altium/sch_altium_plugin.cpp index f132313815..a42e78ef32 100644 --- a/eeschema/sch_plugins/altium/sch_altium_plugin.cpp +++ b/eeschema/sch_plugins/altium/sch_altium_plugin.cpp @@ -204,7 +204,9 @@ SCH_SHEET* SCH_ALTIUM_PLUGIN::Load( const wxString& aFileName, SCHEMATIC* aSchem } m_currentSheet = m_rootSheet; + m_currentTitleBlock = std::make_unique(); ParseAltiumSch( aFileName ); + m_currentSheet->GetScreen()->SetTitleBlock( *m_currentTitleBlock ); m_pi->SaveLibrary( getLibFileName().GetFullPath() ); @@ -382,6 +384,7 @@ void SCH_ALTIUM_PLUGIN::Parse( const CFB::CompoundFileReader& aReader ) case ALTIUM_SCH_RECORD::TEMPLATE: break; case ALTIUM_SCH_RECORD::PARAMETER: + ParseParameter( properties ); break; case ALTIUM_SCH_RECORD::WARNING_SIGN: break; @@ -1771,29 +1774,48 @@ void SCH_ALTIUM_PLUGIN::ParseDesignator( const std::map& aPr { ASCH_DESIGNATOR elem( aProperties ); - if( elem.ownerpartid == ALTIUM_COMPONENT_NONE ) - { - return; // TODO: what to do? - } - - const auto& component = m_components.find( elem.ownerpartid ); - if( component == m_components.end() ) + const auto& symbol = m_symbols.find( elem.ownerindex ); + if( symbol == m_symbols.end() ) { // TODO: e.g. can depend on Template (RECORD=39 - THROW_IO_ERROR( wxString::Format( - "Designator tries to access component with ownerpartid %d which does not exist", - elem.ownerpartid ) ); + wxLogWarning( wxString::Format( + "Designator tries to access symbol with ownerindex %d which does not exist", + elem.ownerindex ) ); + return; } - LIB_PART* symbol = m_symbols.at( elem.ownerpartid ); - // TODO: component->second->SetRef(m_sheet, elem.name); + const auto& component = m_components.at( symbol->first ); + + SCH_SHEET_PATH sheetpath; + m_rootSheet->LocatePathOfScreen( m_currentSheet->GetScreen(), &sheetpath ); + + component->SetRef( &sheetpath, elem.text ); - LIB_TEXT* text = new LIB_TEXT( symbol ); - symbol->AddDrawItem( text ); + SCH_FIELD* refField = component->GetField( REFERENCE ); - text->SetPosition( elem.location + m_sheetOffset ); - text->SetTextAngle( elem.orientation * 90. ); - text->SetText( elem.name ); // TODO: use variable + refField->SetPosition( elem.location + m_sheetOffset ); + refField->SetVisible( true ); + + refField->SetHorizJustify( EDA_TEXT_HJUSTIFY_T::GR_TEXT_HJUSTIFY_LEFT ); + refField->SetVertJustify( EDA_TEXT_VJUSTIFY_T::GR_TEXT_VJUSTIFY_BOTTOM ); + + switch( elem.orientation ) + { + case ASCH_RECORD_ORIENTATION::RIGHTWARDS: + refField->SetTextAngle( 0 ); + break; + case ASCH_RECORD_ORIENTATION::UPWARDS: + refField->SetTextAngle( 90 ); + break; + case ASCH_RECORD_ORIENTATION::LEFTWARDS: + refField->SetTextAngle( 180 ); + break; + case ASCH_RECORD_ORIENTATION::DOWNWARDS: + refField->SetTextAngle( 270 ); + break; + default: + break; + } } @@ -1809,3 +1831,72 @@ void SCH_ALTIUM_PLUGIN::ParseBusEntry( const std::map& aProp busWireEntry->SetFlags( IS_NEW ); m_currentSheet->GetScreen()->Append( busWireEntry ); } + + +void SCH_ALTIUM_PLUGIN::ParseParameter( const std::map& aProperties ) +{ + ASCH_PARAMETER elem( aProperties ); + + if( elem.ownerindex <= 0 && elem.ownerpartid == ALTIUM_COMPONENT_NONE ) + { + // This is some sheet parameter + if( elem.text == "*" ) + return; // indicates parameter not set? + + SCH_SHEET_PATH sheetpath; + m_rootSheet->LocatePathOfScreen( m_currentSheet->GetScreen(), &sheetpath ); + + if( elem.name == "SheetNumber" ) + m_rootSheet->SetPageNumber( sheetpath, elem.text ); + else if( elem.name == "Title" ) + m_currentTitleBlock->SetTitle( elem.text ); + else if( elem.name == "Revision" ) + m_currentTitleBlock->SetRevision( elem.text ); + else if( elem.name == "Date" ) + m_currentTitleBlock->SetDate( elem.text ); + else if( elem.name == "CompanyName" ) + m_currentTitleBlock->SetCompany( elem.text ); + // TODO: parse other parameters + // TODO: handle parameters in labels + } + else + { + const auto& symbol = m_symbols.find( elem.ownerindex ); + if( symbol == m_symbols.end() ) + { + // TODO: e.g. can depend on Template (RECORD=39 + return; + } + + const auto& component = m_components.at( symbol->first ); + + int fieldIdx = component->GetFieldCount() + 1; + + // TODO: location not correct? + SCH_FIELD field( elem.location + m_sheetOffset, fieldIdx, component, elem.name ); + field.SetText( elem.text ); + field.SetVisible( !elem.isHidden ); + field.SetMirrored( elem.isMirrored ); + field.SetHorizJustify( EDA_TEXT_HJUSTIFY_T::GR_TEXT_HJUSTIFY_LEFT ); + + switch( elem.orientation ) + { + case ASCH_RECORD_ORIENTATION::RIGHTWARDS: + field.SetTextAngle( 0 ); + break; + case ASCH_RECORD_ORIENTATION::UPWARDS: + field.SetTextAngle( 90 ); + break; + case ASCH_RECORD_ORIENTATION::LEFTWARDS: + field.SetTextAngle( 180 ); + break; + case ASCH_RECORD_ORIENTATION::DOWNWARDS: + field.SetTextAngle( 270 ); + break; + default: + break; + } + + component->AddField( field ); + } +} \ No newline at end of file diff --git a/eeschema/sch_plugins/altium/sch_altium_plugin.h b/eeschema/sch_plugins/altium/sch_altium_plugin.h index 53de2d52b1..c3df2bd966 100644 --- a/eeschema/sch_plugins/altium/sch_altium_plugin.h +++ b/eeschema/sch_plugins/altium/sch_altium_plugin.h @@ -33,6 +33,7 @@ class SCH_COMPONENT; +class TITLE_BLOCK; namespace CFB { @@ -124,6 +125,7 @@ private: void ParseSheet( const std::map& aProperties ); void ParseDesignator( const std::map& aProperties ); void ParseBusEntry( const std::map& aProperties ); + void ParseParameter( const std::map& aProperties ); private: SCH_SHEET* m_rootSheet; ///< The root sheet of the schematic being loaded.. @@ -135,6 +137,9 @@ private: SCH_PLUGIN::SCH_PLUGIN_RELEASER m_pi; ///< Plugin to create the KiCad symbol library. std::unique_ptr m_properties; ///< Library plugin properties. + std::unique_ptr + m_currentTitleBlock; /// Will be assigned at the end of parsing a sheet + wxPoint m_sheetOffset; std::unique_ptr m_altiumSheet; std::map m_components;