From 5c9346902adcbfd5fdd4e5d2ff1149bd89de37f7 Mon Sep 17 00:00:00 2001 From: WhiteChairFromIkea Date: Tue, 9 Aug 2022 22:21:36 +0300 Subject: [PATCH] Import HARNESS PORT as textboxes, preserving original formatting + typo fixes --- .../sch_plugins/altium/altium_parser_sch.cpp | 11 +- .../sch_plugins/altium/altium_parser_sch.h | 8 +- .../sch_plugins/altium/sch_altium_plugin.cpp | 115 +++++++++++++----- .../sch_plugins/altium/sch_altium_plugin.h | 7 ++ 4 files changed, 111 insertions(+), 30 deletions(-) diff --git a/eeschema/sch_plugins/altium/altium_parser_sch.cpp b/eeschema/sch_plugins/altium/altium_parser_sch.cpp index 167e62ae1c..07e7ec7e1c 100644 --- a/eeschema/sch_plugins/altium/altium_parser_sch.cpp +++ b/eeschema/sch_plugins/altium/altium_parser_sch.cpp @@ -426,7 +426,7 @@ ASCH_SIGNAL_HARNESS::ASCH_SIGNAL_HARNESS( const std::map& aP -ReadKiCadUnitFrac( aProps, "Y" + si ) ); } - indexinsheet = ALTIUM_PARSER::ReadInt( aProps, "INDEXINSHEET", 0 ); + indexInSheet = ALTIUM_PARSER::ReadInt( aProps, "INDEXINSHEET", 0 ); color = ALTIUM_PARSER::ReadInt( aProps, "COLOR", 0 ); lineWidth = ReadKiCadUnitFrac( aProps, "LINEWIDTH" ); @@ -587,6 +587,13 @@ ASCH_PORT::ASCH_PORT( const std::map& aProps ) iotype = ReadEnum( aProps, "IOTYPE", 0, 3, ASCH_PORT_IOTYPE::UNSPECIFIED ); style = ReadEnum( aProps, "STYLE", 0, 7, ASCH_PORT_STYLE::NONE_HORIZONTAL ); + + areaColor = ALTIUM_PARSER::ReadInt( aProps, "AREACOLOR", 0 ); + color = ALTIUM_PARSER::ReadInt( aProps, "COLOR", 0 ); + fontId = ALTIUM_PARSER::ReadInt( aProps, "TEXTFONTID", 0 ); + textColor = ALTIUM_PARSER::ReadInt( aProps, "TEXTCOLOR", 0 ); + + alignment = ReadEnum( aProps, "ALIGNMENT", 1, 3, ASCH_TEXT_FRAME_ALIGNMENT::LEFT ); } @@ -698,6 +705,8 @@ ASCH_SHEET_FONT::ASCH_SHEET_FONT( const std::map& aProps, in italic = ALTIUM_PARSER::ReadBool( aProps, "ITALIC" + sid, false ); bold = ALTIUM_PARSER::ReadBool( aProps, "BOLD" + sid, false ); underline = ALTIUM_PARSER::ReadBool( aProps, "UNDERLINE" + sid, false ); + + areaColor = ALTIUM_PARSER::ReadInt( aProps, "AREACOLOR" + sid, 0 ); } VECTOR2I ASchSheetGetSize( ASCH_SHEET_SIZE aSheetSize ) diff --git a/eeschema/sch_plugins/altium/altium_parser_sch.h b/eeschema/sch_plugins/altium/altium_parser_sch.h index 1a3dd44809..0b73f566e2 100644 --- a/eeschema/sch_plugins/altium/altium_parser_sch.h +++ b/eeschema/sch_plugins/altium/altium_parser_sch.h @@ -425,7 +425,7 @@ struct ASCH_SIGNAL_HARNESS std::vector points; int color; - int indexinsheet; + int indexInSheet; int lineWidth; explicit ASCH_SIGNAL_HARNESS( const std::map& aProps ); @@ -602,6 +602,12 @@ struct ASCH_PORT VECTOR2I location; int width; int height; + int areaColor; + int color; + int textColor; + int fontId; + + ASCH_TEXT_FRAME_ALIGNMENT alignment; ASCH_PORT_IOTYPE iotype; ASCH_PORT_STYLE style; diff --git a/eeschema/sch_plugins/altium/sch_altium_plugin.cpp b/eeschema/sch_plugins/altium/sch_altium_plugin.cpp index 3d70620633..afa13de403 100644 --- a/eeschema/sch_plugins/altium/sch_altium_plugin.cpp +++ b/eeschema/sch_plugins/altium/sch_altium_plugin.cpp @@ -48,6 +48,7 @@ #include #include #include +#include #include #include @@ -394,6 +395,10 @@ void SCH_ALTIUM_PLUGIN::ParseAdditional( const ALTIUM_COMPOUND_FILE& aAltiumSchF } } + // Handle harness Ports + for( const ASCH_PORT& port : m_altiumHarnessPortsCurrentSheet ) + ParseHarnessPort( port ); + if( reader.HasParsingError() ) THROW_IO_ERROR( "stream was not parsed correctly!" ); @@ -1499,8 +1504,13 @@ void SCH_ALTIUM_PLUGIN::ParseHarnessConnector( int aIndex, const std::mapSetBackgroundColor( GetColorFromInt( elem.areaColor ) ); - sheet->SetBorderColor( GetColorFromInt( elem.color ) ); + // Harness ports are drawn the same colors as harness connectors, discarding properties, found in Altium's file, + // so keep color settings for use in harness ports + m_harnessConnectorBackgroundColor = GetColorFromInt( elem.areaColor ); + m_harnessConnectorBorderColor = GetColorFromInt( elem.color ); + + sheet->SetBackgroundColor( m_harnessConnectorBackgroundColor ); + sheet->SetBorderColor( m_harnessConnectorBorderColor ); sheet->SetScreen( screen ); @@ -1520,8 +1530,7 @@ void SCH_ALTIUM_PLUGIN::ParseHarnessConnector( int aIndex, const std::mapReport( - _( "Harness connector, belonging to the part is not currently supported." ), + m_reporter->Report( _( "Harness connector, belonging to the part is not currently supported." ), RPT_SEVERITY_ERROR ); } } @@ -1535,7 +1544,7 @@ void SCH_ALTIUM_PLUGIN::ParseHarnessEntry( const std::map& a if( sheetIt == m_sheets.end() ) { - m_reporter->Report( wxString::Format( _( "Harness entry's paren (%d) not found." ), + m_reporter->Report( wxString::Format( _( "Harness entry's parent (%d) not found." ), SCH_ALTIUM_PLUGIN::m_harnessEntryParent ), RPT_SEVERITY_ERROR ); return; @@ -1599,12 +1608,11 @@ void SCH_ALTIUM_PLUGIN::ParseHarnessType( const std::map& aP SetTextPositioning( &sheetNameField, ASCH_LABEL_JUSTIFICATION::BOTTOM_LEFT, ASCH_RECORD_ORIENTATION::RIGHTWARDS ); sheetNameField.SetTextColor( GetColorFromInt( elem.color ) ); - m_reporter->Report( - wxString::Format( _( "Altium's Harness Connector (%s) was imported as " + m_reporter->Report( wxString::Format( _( "Altium's Harness Connector (%s) was imported as " "Hierarchical sheet. Please review imported schematic, as " - "KiCad does natively support these Altium elements." ), - elem.text ), - RPT_SEVERITY_WARNING ); + "KiCad does not natively support these Altium elements." ), + elem.text ), + RPT_SEVERITY_WARNING ); } @@ -2059,9 +2067,73 @@ void SCH_ALTIUM_PLUGIN::ParsePowerPort( const std::map& aPro } +void SCH_ALTIUM_PLUGIN::ParseHarnessPort( const ASCH_PORT& aElem ) +{ + SCH_TEXTBOX* textBox = new SCH_TEXTBOX(); + + textBox->SetText( aElem.name ); + textBox->SetTextColor( GetColorFromInt( aElem.textColor ) ); + + int height = aElem.height; + if( height <= 0 ) + height = Mils2iu( 100 ); // chose default 50 grid + + textBox->SetStartX( ( aElem.location + m_sheetOffset ).x ); + textBox->SetStartY( ( aElem.location + m_sheetOffset ).y - ( height / 2 ) ); + textBox->SetEndX( ( aElem.location + m_sheetOffset ).x + ( aElem.width ) ); + textBox->SetEndY( ( aElem.location + m_sheetOffset ).y + ( height / 2 ) ); + + textBox->SetFillColor( m_harnessConnectorBackgroundColor ); + textBox->SetFilled( true ); + + textBox->SetStroke( STROKE_PARAMS( 2, PLOT_DASH_TYPE::DEFAULT, m_harnessConnectorBorderColor ) ); + + switch( aElem.alignment ) + { + default: + case ASCH_TEXT_FRAME_ALIGNMENT::LEFT: + textBox->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT ); + break; + case ASCH_TEXT_FRAME_ALIGNMENT::CENTER: + textBox->SetHorizJustify( GR_TEXT_H_ALIGN_CENTER ); + break; + case ASCH_TEXT_FRAME_ALIGNMENT::RIGHT: + textBox->SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT ); + break; + } + + size_t fontId = static_cast( aElem.fontId ); + + if( m_altiumSheet && fontId > 0 && fontId <= m_altiumSheet->fonts.size() ) + { + const ASCH_SHEET_FONT& font = m_altiumSheet->fonts.at( fontId - 1 ); + textBox->SetItalic( font.italic ); + textBox->SetBold( font.bold ); + textBox->SetTextSize( { font.size / 2, font.size / 2 } ); + //textBox->SetFont( //how to set font, we have a font mane here: ( font.fontname ); + } + + textBox->SetFlags( IS_NEW ); + + m_currentSheet->GetScreen()->Append( textBox ); + + m_reporter->Report( wxString::Format( _( "Altium's Harness port (%s) was imported as " + "Text box. Please review imported schematic, as " + "KiCad does not natively support these Altium elements." ), + aElem.name ), + RPT_SEVERITY_WARNING ); +} + + void SCH_ALTIUM_PLUGIN::ParsePort( const ASCH_PORT& aElem ) { - bool isHarness = !aElem.harnessType.IsEmpty(); + if( !aElem.harnessType.IsEmpty() ) + { + // Parse harness ports after "Additional" compound section is parsed + m_altiumHarnessPortsCurrentSheet.emplace_back( aElem ); + return; + } + VECTOR2I start = aElem.location + m_sheetOffset; VECTOR2I end = start; @@ -2098,7 +2170,7 @@ void SCH_ALTIUM_PLUGIN::ParsePort( const ASCH_PORT& aElem ) || endIsWireTerminal || endIsBusTerminal; - if( !isHarness && !connectionFound ) + if( !connectionFound ) { m_reporter->Report( wxString::Format( _( "Port %s has no connections." ), aElem.name ), RPT_SEVERITY_WARNING ); @@ -2108,26 +2180,13 @@ void SCH_ALTIUM_PLUGIN::ParsePort( const ASCH_PORT& aElem ) VECTOR2I position = ( startIsWireTerminal || startIsBusTerminal ) ? start : end; SCH_LABEL_BASE* label; - if( isHarness ) - { - label = new SCH_DIRECTIVE_LABEL( position ); - - std::vector& fields = label->GetFields(); - - fields.emplace_back( SCH_FIELD( { 0, 0 }, 0, label, wxT( "Harness" ) ) ); - fields[0].SetText( aElem.harnessType ); - fields[0].SetVisible( true ); - } // TODO: detect correct label type depending on sheet settings, etc. //{ // label = new SCH_HIERLABEL( elem.location + m_sheetOffset, elem.name ); //} - else - { - - label = new SCH_GLOBALLABEL( position, aElem.name ); - } - + + label = new SCH_GLOBALLABEL( position, aElem.name ); + switch( aElem.iotype ) { default: diff --git a/eeschema/sch_plugins/altium/sch_altium_plugin.h b/eeschema/sch_plugins/altium/sch_altium_plugin.h index 9a144728f7..7d2ddb4d35 100644 --- a/eeschema/sch_plugins/altium/sch_altium_plugin.h +++ b/eeschema/sch_plugins/altium/sch_altium_plugin.h @@ -125,6 +125,7 @@ private: void ParseHarnessConnector( int aIndex, const std::map& aProperties ); void ParseHarnessEntry( const std::map& aProperties ); void ParseHarnessType( const std::map& aProperties ); + void ParseHarnessPort( const ASCH_PORT& aElem ); void ParseRectangle( const std::map& aProperties ); void ParseSheetSymbol( int aIndex, const std::map& aProperties ); void ParseSheetEntry( const std::map& aProperties ); @@ -172,9 +173,15 @@ private: std::map m_altiumComponents; std::map m_altiumImplementationList; std::vector m_altiumPortsCurrentSheet; // we require all connections first + std::vector m_altiumHarnessPortsCurrentSheet; // parse harness ports after "FileHeader" was parsed, in 2nd run int m_harnessOwnerIndexOffset; // Add offset to all harness ownerIndex'es after parsing FileHeader int m_harnessEntryParent; // used to identify harness connector for harness entry element + + // Harness ports are drawn the same colors as harness connectors, discarding port local + // settings found in altium's file, so keep color settings to draw harness ports correctly + COLOR4D m_harnessConnectorBackgroundColor; + COLOR4D m_harnessConnectorBorderColor; }; #endif // _SCH_ALTIUM_PLUGIN_H_