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 // Convert a 'special string' to a KiCad variable, substituting any override.
168 const auto getVariable = [&]( const wxString& aSpecialString )
169 {
170 wxString str = aSpecialString;
171 str.UpperCase(); // matching is implemented using upper case strings
172
173 auto it = aOverrides.find( str );
174 if( it != aOverrides.end() )
175 str = it->second;
176
177 return wxString::Format( wxT( "${%s}" ), str );
178 };
179
180 // special case: string starts with dot -> whole string is special string
181 if( aString.at( 0 ) == '.' )
182 {
183 wxString specialString = aString.substr( 1 );
184 return getVariable( specialString );
185 }
186
187 // Strings can also have one or more special strings using apostrophes to
188 // delineate them, e.g. "foo '.bar' '.baz' = '.qux' quux"
189
190 // In the common case, the string is a simple string with no special strings,
191 // so bail out early.
192 if( !aString.Contains( "'." ) )
193 {
194 return aString;
195 }
196
197 wxString stringCopy = aString;
198
199 // Given a position of a dot, check if it is a special string variable
200 // and replace it with a variable name if defined.
201 const auto tryReplacement = [&]( wxString& aStr, unsigned aDotPos )
202 {
203 // Check that the dot has an apostrophe before it, if not, it's just a dot
204 if( aDotPos == 0 || aStr.at( aDotPos - 1 ) != '\'' )
205 return;
206
207 // Scan forward for the next apostrophe
208 size_t apostrophePos = aStr.find( '\'', aDotPos + 1 );
209
210 // Didn't find it
211 if( apostrophePos == wxString::npos )
212 return;
213
214 // Extract the special string
215 wxString specialString = aStr.substr( aDotPos + 1, apostrophePos - aDotPos - 1 );
216 wxString replacement = getVariable( specialString );
217
218 aStr.replace( aDotPos - 1, apostrophePos - aDotPos + 2, replacement );
219 };
220
221 // Work backwards through the string checking any dots
222 // (so we don't mess up the positions of the dots as we replace them)
223 for( size_t pos = stringCopy.size() - 1; pos > 0; --pos )
224 {
225 if( stringCopy[pos] == '.' )
226 {
227 tryReplacement( stringCopy, pos );
228 }
229 }
230
231 return stringCopy;
232}
233
234
235wxString AltiumPinNamesToKiCad( wxString& aString )
236{
237 if( aString.IsEmpty() )
238 return wxEmptyString;
239
240 wxString rest;
241
242 if( aString.StartsWith( '\\', &rest ) && !rest.Contains( '\\' ) )
243 return "~{" + rest + "}";
244
245 return AltiumPropertyToKiCadString( aString );
246}
247
248
249VECTOR2I AltiumGetEllipticalPos( double aMajor, double aMinor, double aAngleRadians )
250{
251 if( aMajor == 0 || aMinor == 0 )
252 return VECTOR2I( 0, 0 );
253
254 double numerator = aMajor * aMinor;
255 double majorTerm = aMajor * sin( aAngleRadians );
256 double minorTerm = aMinor * cos( aAngleRadians );
257 double denominator = sqrt( majorTerm * majorTerm + minorTerm * minorTerm );
258
259 double radius = numerator / denominator;
260
261 VECTOR2I retval( KiROUND( radius * cos( aAngleRadians ) ),
262 KiROUND( radius * sin( aAngleRadians ) ) );
263
264 return retval;
265
266}
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:52
static UTF8 FixIllegalChars(const UTF8 &aLibItemName, bool aLib)
Replace illegal LIB_ID item name characters with underscores '_'.
Definition lib_id.cpp:192
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
int radius
wxString result
Test unit parsing edge cases and error handling.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695