altium: Handle special strings and convert them into KiCad variables. Use Comment for VALUE.

Fixes: https://gitlab.com/kicad/code/kicad/-/issues/6256
Fixes: https://gitlab.com/kicad/code/kicad/-/issues/7736
This commit is contained in:
Thomas Pointhuber 2021-03-14 16:40:36 +01:00
parent 2528e1fc98
commit 4482b3baba
6 changed files with 213 additions and 21 deletions

View File

@ -284,6 +284,7 @@ set( PLOTTERS_CONTROL_SRCS
set( PLUGINS_ALTIUM_SRCS
plugins/altium/altium_parser.cpp
plugins/altium/altium_parser_utils.cpp
)
set( PLUGINS_CADSTAR_SRCS

View File

@ -0,0 +1,80 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2021 Thomas Pointhuber <thomas.pointhuber@gmx.at>
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
*
* 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 2
* 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, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "altium_parser_utils.h"
#include <kicad_string.h>
#include <lib_id.h>
LIB_ID AltiumToKiCadLibID( wxString aLibName, wxString aLibReference )
{
ReplaceIllegalFileNameChars( aLibName, '_' );
ReplaceIllegalFileNameChars( aLibReference, '_' );
wxString key = !aLibName.empty() ? ( aLibName + ":" + aLibReference ) : aLibReference;
LIB_ID libId;
libId.Parse( key, true );
return libId;
}
// https://www.altium.com/documentation/altium-designer/sch-obj-textstringtext-string-ad#!special-strings
wxString AltiumSpecialStringsToKiCadVariables( const wxString& aString,
const std::map<wxString, wxString>& aOverride )
{
if( aString.IsEmpty() || aString.at( 0 ) != '=' )
{
return aString;
}
wxString result;
size_t start = 1;
size_t delemiter = 0;
do
{
delemiter = aString.find( "+", start );
wxString specialString = aString.substr( start, delemiter - start ).Trim( true );
if( !specialString.IsEmpty() )
{
auto variableOverride = aOverride.find( specialString );
if( variableOverride == aOverride.end() )
{
result += wxString::Format( wxT( "${%s}" ), specialString );
}
else
{
result += variableOverride->second;
}
}
start = delemiter + 1;
} while( delemiter != wxString::npos );
return result;
}

View File

@ -1,8 +1,8 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2020 Thomas Pointhuber <thomas.pointhuber@gmx.at>
* Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
* Copyright (C) 2020-2021 Thomas Pointhuber <thomas.pointhuber@gmx.at>
* Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -25,22 +25,15 @@
#ifndef ALTIUM_PARSER_UTILS_H
#define ALTIUM_PARSER_UTILS_H
#include <map>
#include <kicad_string.h>
#include <lib_id.h>
LIB_ID AltiumToKiCadLibID( wxString aLibName, wxString aLibReference )
{
ReplaceIllegalFileNameChars( aLibName, '_' );
ReplaceIllegalFileNameChars( aLibReference, '_' );
wxString key = !aLibName.empty() ? ( aLibName + ":" + aLibReference ) : aLibReference;
LIB_ID libId;
libId.Parse( key, true );
return libId;
}
LIB_ID AltiumToKiCadLibID( wxString aLibName, wxString aLibReference );
wxString AltiumSpecialStringsToKiCadVariables( const wxString& aString,
const std::map<wxString, wxString>& aOverride );
#endif //ALTIUM_PARSER_UTILS_H

View File

@ -2051,6 +2051,12 @@ void SCH_ALTIUM_PLUGIN::ParseParameter( const std::map<wxString, wxString>& aPro
{
ASCH_PARAMETER elem( aProperties );
// TODO: fill in replacements from variant, sheet and project
std::map<wxString, wxString> stringReplacement = {
{ "Comment", "${VALUE}" },
{ "Value", "${Altium_Value}" },
};
if( elem.ownerindex <= 0 && elem.ownerpartid == ALTIUM_COMPONENT_NONE )
{
// This is some sheet parameter
@ -2088,7 +2094,7 @@ void SCH_ALTIUM_PLUGIN::ParseParameter( const std::map<wxString, wxString>& aPro
const wxPoint position = elem.location + m_sheetOffset;
SCH_FIELD* field = nullptr;
if( elem.name == "Value" )
if( elem.name == "Comment" )
{
field = component->GetField( VALUE_FIELD );
field->SetPosition( position );
@ -2096,14 +2102,12 @@ void SCH_ALTIUM_PLUGIN::ParseParameter( const std::map<wxString, wxString>& aPro
else
{
int fieldIdx = component->GetFieldCount();
field = component->AddField( { position, fieldIdx, component, elem.name } );
wxString fieldName = elem.name == "Value" ? "Altium_Value" : elem.name;
field = component->AddField( { position, fieldIdx, component, fieldName } );
}
// TODO: improve text replacement (https://gitlab.com/kicad/code/kicad/-/issues/6256)
if( elem.text == "=Value" && field->GetId() != VALUE_FIELD )
field->SetText( "${VALUE}" );
else
field->SetText( elem.text );
wxString kicadText = AltiumSpecialStringsToKiCadVariables( elem.text, stringReplacement );
field->SetText( kicadText );
field->SetVisible( !elem.isHidden );
field->SetHorizJustify( EDA_TEXT_HJUSTIFY_T::GR_TEXT_HJUSTIFY_LEFT );

View File

@ -46,6 +46,7 @@ set( common_srcs
libeval/test_numeric_evaluator.cpp
plugins/altium/test_altium_parser.cpp
plugins/altium/test_altium_parser_utils.cpp
view/test_zoom_controller.cpp
)

View File

@ -0,0 +1,113 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2021 KiCad Developers, see CHANGELOG.TXT for contributors.
*
* 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 2
* 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, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @file test_altium_parser_utils.cpp
* Test suite for #ALTIUM_PARSER
*/
#include <unit_test_utils/unit_test_utils.h>
#include <common/plugins/altium/altium_parser_utils.h>
struct ALTIUM_PARSER_UTILS_FIXTURE
{
ALTIUM_PARSER_UTILS_FIXTURE() {}
};
/**
* Declares the struct as the Boost test fixture.
*/
BOOST_FIXTURE_TEST_SUITE( AltiumParserUtils, ALTIUM_PARSER_UTILS_FIXTURE )
struct SPECIAL_STRINGS_TO_KICAD
{
wxString input;
wxString exp_result;
std::map<wxString, wxString> override;
};
/**
* A list of valid test strings and the expected results
*/
static const std::vector<SPECIAL_STRINGS_TO_KICAD> special_string_to_kicad_property = {
// Empty
{ "", "", {} },
// No Special Strings
{ "A", "A", {} },
{ " A", " A", {} },
{ "A ", "A ", {} },
{ "A=B", "A=B", {} },
{ "A=B+C", "A=B+C", {} },
{ "A\nB", "A\nB", {} },
{ "A\tB", "A\tB", {} },
{ "This is a long text with spaces", "This is a long text with spaces", {} },
// Text format (underscore,...), TODO: add
// Escaping, TODO: add
{ "A=B", "A=B", {} },
// Simple special strings
{ "=A", "${A}", {} },
{ "=A", "C", { { "A", "C" } } },
{ "=A", "${C}", { { "A", "${C}" } } },
{ "=A_B", "${A_B}", {} },
// Combined special strings
{ "=A+B", "${A}${B}", {} },
{ "=A+B", "${A}${B}", {} },
{ "=A+B", "C${B}", { { "A", "C" } } },
{ "=A+B", "CD", { { "A", "C" }, { "B", "D" } } },
// Some special cases we do not know yet how to handle correctly. But we should not crash ;)
{ "=+", "", {} },
{ "=++", "", {} },
{ "=+++", "", {} },
{ "=B+", "${B}", {} },
{ "=+B", "${B}", {} },
{ "=B++", "${B}", {} },
{ "=+B+", "${B}", {} },
{ "=++B", "${B}", {} },
{ " =", " =", {} },
{ "= ", "", {} },
{ "= A", "${ A}", {} },
{ "=A ", "${A}", {} },
};
/**
* Test conversation from Altium Special String to a KiCad String with variables
*/
BOOST_AUTO_TEST_CASE( AltiumSpecialStringsToKiCadVariablesProperties )
{
for( const auto& c : special_string_to_kicad_property )
{
BOOST_TEST_CONTEXT( wxString::Format( wxT( "'%s' -> '%s'" ), c.input, c.exp_result ) )
{
wxString result = AltiumSpecialStringsToKiCadVariables( c.input, c.override );
// These are all valid
BOOST_CHECK_EQUAL( result, c.exp_result );
}
}
}
BOOST_AUTO_TEST_SUITE_END()