KiCad PCB EDA Suite
Loading...
Searching...
No Matches
altium_parser_utils.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2021 Thomas Pointhuber <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include "altium_parser_utils.h"
26
27#include <string_utils.h>
28#include <lib_id.h>
29#include <trigo.h>
30#include <math/util.h>
31
32LIB_ID AltiumToKiCadLibID( const wxString& aLibName, const wxString& aLibReference )
33{
34 wxString libName = LIB_ID::FixIllegalChars( aLibName, true );
35 wxString libReference = EscapeString( aLibReference, CTX_LIBID );
36
37 wxString key = !libName.empty() ? ( libName + ":" + libReference ) : libReference;
38
39 LIB_ID libId;
40 libId.Parse( key, true );
41
42 return libId;
43}
44
45
46wxString AltiumPropertyToKiCadString( const wxString& aString )
47{
48 wxString output;
49 wxString tempString;
50 bool hasPrev = false;
51 wxUniChar prev;
52
53 for( wxString::const_iterator it = aString.begin(); it != aString.end(); ++it )
54 {
55 char ch = 0;
56
57 if( (*it).GetAsChar( &ch ) )
58 {
59 if( ch == '\\' )
60 {
61 if( hasPrev )
62 {
63 tempString += prev;
64 hasPrev = false;
65 }
66
67 continue; // Backslash is ignored and not added to the output
68 }
69 }
70
71 if( hasPrev ) // Two letters in a row with no backslash
72 {
73 if( !tempString.empty() )
74 {
75 output += "~{" + tempString + "}";
76 tempString.Clear();
77 }
78
79 output += prev;
80 }
81
82 prev = *it;
83 hasPrev = true;
84 }
85
86 // Append any leftover escaped string
87 if( !tempString.IsEmpty() )
88 output += "~{" + tempString + "}";
89
90 if( hasPrev )
91 output += prev;
92
93 return output;
94}
95
96
97// https://www.altium.com/documentation/altium-designer/sch-obj-textstringtext-string-ad#!special-strings
98wxString AltiumSchSpecialStringsToKiCadVariables( const wxString& aString,
99 const std::map<wxString, wxString>& aOverrides )
100{
101 if( aString.IsEmpty() || aString.at( 0 ) != '=' )
102 {
103 return aString;
104 }
105
106 wxString result;
107
108 size_t start = 1;
109 size_t delimiter = 0;
110 size_t escaping_start = 0;
111 do
112 {
113 delimiter = aString.find( "+", start );
114 escaping_start = aString.find( "'", start );
115
116 if( escaping_start < delimiter )
117 {
118 size_t text_start = escaping_start + 1;
119 size_t escaping_end = aString.find( "'", text_start );
120
121 if( escaping_end == wxString::npos )
122 {
123 escaping_end = aString.size();
124 }
125
126 result += aString.substr( text_start, escaping_end - text_start );
127
128 start = escaping_end + 1;
129 }
130 else
131 {
132 wxString specialString =
133 aString.substr( start, delimiter - start ).Trim().Trim( false );
134
135 if( specialString.StartsWith( "\"" ) && specialString.EndsWith( "\"" ) )
136 specialString = specialString.Mid( 1, specialString.Length() - 2 );
137
138 if( !specialString.IsEmpty() )
139 {
140 // Note: Altium variable references are case-insensitive. KiCad matches
141 // case-sensitive OR to all-upper-case, so make the references all-upper-case.
142 specialString.UpperCase();
143
144 auto overrideIt = aOverrides.find( specialString );
145
146 if( overrideIt != aOverrides.end() )
147 specialString = overrideIt->second;
148
149 result += wxString::Format( wxT( "${%s}" ), specialString );
150 }
151
152 start = delimiter + 1;
153 }
154 } while( delimiter != wxString::npos );
155
156 return result;
157}
158
159
160// https://www.altium.com/documentation/altium-designer/text-objects-pcb
161wxString AltiumPcbSpecialStringsToKiCadStrings( const wxString& aString,
162 const std::map<wxString, wxString>& aOverrides )
163{
164 if( aString.IsEmpty() )
165 return aString;
166
167 // special case: string starts with dot -> whole string is special string
168 if( aString.at( 0 ) == '.' )
169 {
170 wxString specialString = aString.substr( 1 );
171
172 specialString.UpperCase(); // matching is implemented using upper case strings
173
174 auto overrideIt = aOverrides.find( specialString );
175
176 if( overrideIt != aOverrides.end() )
177 specialString = overrideIt->second;
178
179 return wxString::Format( wxT( "${%s}" ), specialString );
180 }
181
182 // TODO: implement Concatenated special strings using apostrophe "'".
183
184 return aString;
185}
186
187
188wxString AltiumPinNamesToKiCad( wxString& aString )
189{
190 if( aString.IsEmpty() )
191 return wxEmptyString;
192
193 wxString rest;
194
195 if( aString.StartsWith( '\\', &rest ) && !rest.Contains( '\\' ) )
196 return "~{" + rest + "}";
197
198 return AltiumPropertyToKiCadString( aString );
199}
200
201
202VECTOR2I AltiumGetEllipticalPos( double aMajor, double aMinor, double aAngleRadians )
203{
204 if( aMajor == 0 || aMinor == 0 )
205 return VECTOR2I( 0, 0 );
206
207 double numerator = aMajor * aMinor;
208 double majorTerm = aMajor * sin( aAngleRadians );
209 double minorTerm = aMinor * cos( aAngleRadians );
210 double denominator = sqrt( majorTerm * majorTerm + minorTerm * minorTerm );
211
212 double radius = numerator / denominator;
213
214 VECTOR2I retval( KiROUND( radius * cos( aAngleRadians ) ),
215 KiROUND( radius * sin( aAngleRadians ) ) );
216
217 return retval;
218
219}
wxString AltiumSchSpecialStringsToKiCadVariables(const wxString &aString, const std::map< wxString, wxString > &aOverrides)
wxString AltiumPropertyToKiCadString(const wxString &aString)
wxString AltiumPinNamesToKiCad(wxString &aString)
VECTOR2I AltiumGetEllipticalPos(double aMajor, double aMinor, double aAngleRadians)
LIB_ID AltiumToKiCadLibID(const wxString &aLibName, const wxString &aLibReference)
wxString AltiumPcbSpecialStringsToKiCadStrings(const wxString &aString, const std::map< wxString, wxString > &aOverrides)
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition: lib_id.cpp:51
static UTF8 FixIllegalChars(const UTF8 &aLibItemName, bool aLib)
Replace illegal LIB_ID item name characters with underscores '_'.
Definition: lib_id.cpp:191
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
@ CTX_LIBID
Definition: string_utils.h:54
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:695