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 (C) 2021 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 converted;
49 bool inOverbar = false;
50
51 for( wxString::const_iterator chIt = aString.begin(); chIt != aString.end(); ++chIt )
52 {
53 wxString::const_iterator lookahead = chIt + 1;
54
55 if( lookahead != aString.end() && *lookahead == '\\' )
56 {
57 if( !inOverbar )
58 {
59 converted += "~{";
60 inOverbar = true;
61 }
62
63 converted += *chIt;
64 chIt = lookahead;
65 }
66 else
67 {
68 if( inOverbar )
69 {
70 converted += "}";
71 inOverbar = false;
72 }
73
74 converted += *chIt;
75 }
76 }
77
78 if( inOverbar )
79 converted += "}";
80
81 return converted;
82}
83
84
85// https://www.altium.com/documentation/altium-designer/sch-obj-textstringtext-string-ad#!special-strings
86wxString AltiumSchSpecialStringsToKiCadVariables( const wxString& aString,
87 const std::map<wxString, wxString>& aOverrides )
88{
89 if( aString.IsEmpty() || aString.at( 0 ) != '=' )
90 {
91 return aString;
92 }
93
94 wxString result;
95
96 size_t start = 1;
97 size_t delimiter = 0;
98 size_t escaping_start = 0;
99 do
100 {
101 delimiter = aString.find( "+", start );
102 escaping_start = aString.find( "'", start );
103
104 if( escaping_start < delimiter )
105 {
106 size_t text_start = escaping_start + 1;
107 size_t escaping_end = aString.find( "'", text_start );
108
109 if( escaping_end == wxString::npos )
110 {
111 escaping_end = aString.size();
112 }
113
114 result += aString.substr( text_start, escaping_end - text_start );
115
116 start = escaping_end + 1;
117 }
118 else
119 {
120 wxString specialString = aString.substr( start, delimiter - start ).Trim().Trim( false );
121
122 if( specialString.StartsWith( "\"" ) && specialString.EndsWith( "\"" ) )
123 specialString = specialString.Mid( 1, specialString.Length() - 2 );
124
125 if( !specialString.IsEmpty() )
126 {
127 // Note: Altium variable references are case-insensitive. KiCad matches
128 // case-sensitive OR to all-upper-case, so make the references all-upper-case.
129 specialString.UpperCase();
130
131 auto overrideIt = aOverrides.find( specialString );
132
133 if( overrideIt != aOverrides.end() )
134 specialString = overrideIt->second;
135
136 result += wxString::Format( wxT( "${%s}" ), specialString );
137 }
138
139 start = delimiter + 1;
140 }
141 } while( delimiter != wxString::npos );
142
143 return result;
144}
145
146// https://www.altium.com/documentation/altium-designer/text-objects-pcb
147wxString AltiumPcbSpecialStringsToKiCadStrings( const wxString& aString,
148 const std::map<wxString, wxString>& aOverrides )
149{
150 if( aString.IsEmpty() )
151 {
152 return aString;
153 }
154
155 // special case: string starts with dot -> whole string is special string
156 if( aString.at( 0 ) == '.' )
157 {
158 wxString specialString = aString.substr( 1 );
159
160 specialString.UpperCase(); // matching is implemented using upper case strings
161
162 auto overrideIt = aOverrides.find( specialString );
163
164 if( overrideIt != aOverrides.end() )
165 specialString = overrideIt->second;
166
167 return wxString::Format( wxT( "${%s}" ), specialString );
168 }
169
170 // TODO: implement Concatenated special strings using apostrophe "'".
171
172 return aString;
173}
174
175wxString AltiumPinNamesToKiCad( wxString& aString )
176{
177 wxString output;
178 wxString tempString;
179 bool hasPrev = false;
180 wxUniChar prev;
181
182
183 for( wxString::const_iterator it = aString.begin(); it != aString.end(); ++it )
184 {
185 char ch = 0;
186
187 if( (*it).GetAsChar( &ch ) )
188 {
189 if( ch == '\\' )
190 {
191 if( hasPrev )
192 {
193 tempString += prev;
194 hasPrev = false;
195 }
196
197 continue; // Backslash is ignored and not added to the output
198 }
199 }
200
201 if( hasPrev ) // Two letters in a row with no backslash
202 {
203 if( !tempString.empty() )
204 {
205 output += "~{" + tempString + "}";
206 tempString.Clear();
207 }
208
209 output += prev;
210 }
211
212 prev = *it;
213 hasPrev = true;
214 }
215
216 // Append any leftover escaped string
217 if( !tempString.IsEmpty() )
218 {
219 output += "~{" + tempString + "}";
220 }
221
222 if( hasPrev )
223 {
224 output += prev;
225 }
226
227 return output;
228}
229
230VECTOR2I AltiumGetEllipticalPos( double aMajor, double aMinor, double aAngleRadians )
231{
232 if( aMajor == 0 || aMinor == 0 )
233 return VECTOR2I( 0, 0 );
234
235 double numerator = aMajor * aMinor;
236 double majorTerm = aMajor * sin( aAngleRadians );
237 double minorTerm = aMinor * cos( aAngleRadians );
238 double denominator = sqrt( majorTerm * majorTerm + minorTerm * minorTerm );
239
240 double radius = numerator / denominator;
241
242 VECTOR2I retval( KiROUND( radius * cos( aAngleRadians ) ), KiROUND( radius * sin( aAngleRadians ) ) );
243
244 return retval;
245
246}
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)
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
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:118
VECTOR2< int > VECTOR2I
Definition: vector2d.h:602