2021-03-14 15:40:36 +00:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
2021-07-29 09:56:22 +00:00
|
|
|
#include <string_utils.h>
|
2021-03-14 15:40:36 +00:00
|
|
|
#include <lib_id.h>
|
|
|
|
|
|
|
|
|
2021-07-27 12:22:27 +00:00
|
|
|
LIB_ID AltiumToKiCadLibID( const wxString& aLibName, const wxString& aLibReference )
|
2021-03-14 15:40:36 +00:00
|
|
|
{
|
2023-08-15 21:45:57 +00:00
|
|
|
wxString libName = LIB_ID::FixIllegalChars( aLibName, true );
|
2021-07-27 12:22:27 +00:00
|
|
|
wxString libReference = EscapeString( aLibReference, CTX_LIBID );
|
2021-03-14 15:40:36 +00:00
|
|
|
|
2023-08-15 21:45:57 +00:00
|
|
|
wxString key = !libName.empty() ? ( libName + ":" + libReference ) : libReference;
|
2021-03-14 15:40:36 +00:00
|
|
|
|
|
|
|
LIB_ID libId;
|
|
|
|
libId.Parse( key, true );
|
|
|
|
|
|
|
|
return libId;
|
|
|
|
}
|
|
|
|
|
2021-07-01 18:41:46 +00:00
|
|
|
|
|
|
|
wxString AltiumPropertyToKiCadString( const wxString& aString )
|
|
|
|
{
|
|
|
|
wxString converted;
|
|
|
|
bool inOverbar = false;
|
|
|
|
|
|
|
|
for( wxString::const_iterator chIt = aString.begin(); chIt != aString.end(); ++chIt )
|
|
|
|
{
|
|
|
|
wxString::const_iterator lookahead = chIt + 1;
|
|
|
|
|
|
|
|
if( lookahead != aString.end() && *lookahead == '\\' )
|
|
|
|
{
|
|
|
|
if( !inOverbar )
|
|
|
|
{
|
|
|
|
converted += "~{";
|
|
|
|
inOverbar = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
converted += *chIt;
|
|
|
|
chIt = lookahead;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( inOverbar )
|
|
|
|
{
|
|
|
|
converted += "}";
|
|
|
|
inOverbar = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
converted += *chIt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-08 20:08:16 +00:00
|
|
|
if( inOverbar )
|
|
|
|
converted += "}";
|
|
|
|
|
2021-07-01 18:41:46 +00:00
|
|
|
return converted;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-14 15:40:36 +00:00
|
|
|
// https://www.altium.com/documentation/altium-designer/sch-obj-textstringtext-string-ad#!special-strings
|
2021-07-08 13:14:27 +00:00
|
|
|
wxString AltiumSpecialStringsToKiCadVariables( const wxString& aString,
|
|
|
|
const std::map<wxString, wxString>& aOverrides )
|
2021-03-14 15:40:36 +00:00
|
|
|
{
|
|
|
|
if( aString.IsEmpty() || aString.at( 0 ) != '=' )
|
|
|
|
{
|
|
|
|
return aString;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxString result;
|
|
|
|
|
|
|
|
size_t start = 1;
|
2021-04-05 13:24:57 +00:00
|
|
|
size_t delimiter = 0;
|
2021-03-20 18:25:15 +00:00
|
|
|
size_t escaping_start = 0;
|
2021-03-14 15:40:36 +00:00
|
|
|
do
|
|
|
|
{
|
2021-04-05 13:24:57 +00:00
|
|
|
delimiter = aString.find( "+", start );
|
2021-03-20 18:25:15 +00:00
|
|
|
escaping_start = aString.find( "'", start );
|
2021-03-14 15:40:36 +00:00
|
|
|
|
2021-04-05 13:24:57 +00:00
|
|
|
if( escaping_start < delimiter )
|
2021-03-14 15:40:36 +00:00
|
|
|
{
|
2021-03-20 18:25:15 +00:00
|
|
|
size_t text_start = escaping_start + 1;
|
|
|
|
size_t escaping_end = aString.find( "'", text_start );
|
2021-07-01 18:41:46 +00:00
|
|
|
|
2021-03-20 18:25:15 +00:00
|
|
|
if( escaping_end == wxString::npos )
|
2021-03-14 15:40:36 +00:00
|
|
|
{
|
2021-03-20 18:25:15 +00:00
|
|
|
escaping_end = aString.size();
|
2021-03-14 15:40:36 +00:00
|
|
|
}
|
2021-03-20 18:25:15 +00:00
|
|
|
|
|
|
|
result += aString.substr( text_start, escaping_end - text_start );
|
|
|
|
|
|
|
|
start = escaping_end + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-08 13:14:27 +00:00
|
|
|
wxString specialString = aString.substr( start, delimiter - start ).Trim().Trim( false );
|
|
|
|
|
|
|
|
if( specialString.StartsWith( "\"" ) && specialString.EndsWith( "\"" ) )
|
|
|
|
specialString = specialString.Mid( 1, specialString.Length() - 2 );
|
2021-03-20 18:25:15 +00:00
|
|
|
|
|
|
|
if( !specialString.IsEmpty() )
|
2021-03-14 15:40:36 +00:00
|
|
|
{
|
2021-07-08 13:14:27 +00:00
|
|
|
// Note: Altium variable references are case-insensitive. KiCad matches
|
Fix various typos
Found via `codespell -q 3 -S *.po,./thirdparty,./Documentation/changelogs -L aactual,acount,aline,alocation,alog,anormal,anumber,aother,apoints,aparent,aray,ba,busses,dout,einstance,leaded,modul,ontext,ot,overide,serie,te,,tesselate,tesselator,tht`
2022-07-03 04:20:05 +00:00
|
|
|
// case-sensitive OR to all-upper-case, so make the references all-upper-case.
|
2021-07-08 13:14:27 +00:00
|
|
|
specialString.UpperCase();
|
|
|
|
|
2021-07-05 10:04:19 +00:00
|
|
|
auto overrideIt = aOverrides.find( specialString );
|
2021-07-01 18:41:46 +00:00
|
|
|
|
2021-07-05 10:04:19 +00:00
|
|
|
if( overrideIt != aOverrides.end() )
|
2021-07-08 13:14:27 +00:00
|
|
|
specialString = overrideIt->second;
|
|
|
|
|
|
|
|
result += wxString::Format( wxT( "${%s}" ), specialString );
|
2021-03-14 15:40:36 +00:00
|
|
|
}
|
2021-07-08 13:14:27 +00:00
|
|
|
|
2021-04-05 13:24:57 +00:00
|
|
|
start = delimiter + 1;
|
2021-03-14 15:40:36 +00:00
|
|
|
}
|
2021-04-05 13:24:57 +00:00
|
|
|
} while( delimiter != wxString::npos );
|
2021-03-14 15:40:36 +00:00
|
|
|
|
|
|
|
return result;
|
2021-07-27 12:22:27 +00:00
|
|
|
}
|