KiCad PCB EDA Suite
Loading...
Searching...
No Matches
ltspice_schematic.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) 2022 Chetan Subhash Shinde<[email protected]>
5 * Copyright (C) 2023 CERN
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
25
27
29#include <sch_screen.h>
30#include <wx/log.h>
31#include <wx/dir.h>
32#include <wx/tokenzr.h>
34#include <sch_sheet.h>
35#include <sch_sheet_pin.h>
36#include <schematic.h>
37#include <page_info.h>
38#include <project.h>
39#include <lib_id.h>
40#include <richio.h>
41#include <string_utils.h>
42#include <algorithm>
43
44
45// Files are found in this order: foo/amp.asy, then bar/amp.asy.
46// The map is then:
47// "foo/amp" -> foo/amp.asy
48// "amp" -> foo/amp.asy
49// "bar/amp" -> bar/amp.asy
50//
51// "foo/amp" returns "amp", because "amp" is foo/amp.asy.
52// "bar/amp" returns "bar/amp", because "amp" is foo/amp.asy, not bar/amp.asy.
53static wxString getShortestKey( const std::map<wxString, wxString>& aMap, const wxString& aName )
54{
55 wxString key = aName.Lower();
56 key.Replace( '\\', '/' );
57
58 if( aMap.contains( key ) )
59 {
60 while( key.Contains( '/' ) )
61 {
62 wxString shorter = key.AfterFirst( '/' );
63
64 if( !aMap.contains( shorter ) || aMap.at( shorter ) != aMap.at( key ) )
65 break;
66
67 key = shorter;
68 }
69
70 return key;
71 }
72 else
73 {
74 key = key.AfterLast( '/' );
75
76 if( aMap.contains( key ) )
77 return key;
78 }
79
80 return {};
81}
82
83
84wxString LTSPICE_SCHEMATIC::GetSchematicFileName( const wxString& aAscKey ) const
85{
86 wxString name = getShortestKey( m_ascNamesMap, aAscKey );
87
88 if( name.IsEmpty() )
89 name = aAscKey.Lower();
90
92
94}
95
96
97wxString LTSPICE_SCHEMATIC::GetSymbolName( const wxString& aSymbolName ) const
98{
99 wxString name = getShortestKey( m_asyNamesMap, aSymbolName );
100
101 return LIB_ID::FixIllegalChars( name, false ).wx_str();
102}
103
104
105void LTSPICE_SCHEMATIC::Load( SCHEMATIC* aSchematic, SCH_SHEET* aRootSheet, const wxFileName& aLibraryFileName,
106 REPORTER* aReporter )
107{
108 m_ascFileCache.clear();
109 m_asyFileCache.clear();
110 m_ascNamesMap.clear();
111 m_asyNamesMap.clear();
112
113 // Library paths to search (Give highest priority to files contained in same directory)
114 GetAscAndAsyFilePaths( aLibraryFileName.GetPath(), false, m_ascNamesMap, m_asyNamesMap );
115
116 // TODO: Custom paths go here (non-recursive)
117
118 // Default LTspice libs, which are absent when LTspice is not installed
119 for( const wxString libDir : { wxS( "sub" ), wxS( "sym" ) } )
120 {
121 wxString path = m_ltspiceDataDir.GetPathWithSep() + libDir;
122
123 if( wxDirExists( path ) )
125 }
126
127 wxString rootElementName = aLibraryFileName.GetName().Lower();
128 std::vector<wxString> uniqueAscNameKeys;
129
130 std::function<void( const wxString& )> collectHierarchy;
131 collectHierarchy = [&]( const wxString& aAscNameKey )
132 {
133 uniqueAscNameKeys.push_back( aAscNameKey );
134
135 if( !m_ascNamesMap.contains( aAscNameKey ) )
136 {
137 aReporter->Report( wxString::Format( _( "Missing '%s' schematic file" ), aAscNameKey + wxS( ".asc" ) ),
139 return;
140 }
141
142 wxString buffer = SafeReadFile( m_ascNamesMap.at( aAscNameKey ), wxS( "r" ) );
143 m_ascFileCache[m_ascNamesMap.at( aAscNameKey )] = buffer;
144
145 std::vector<LTSPICE_FILE> newSubSchematicElements = GetSchematicElements( buffer );
146
147 for( LTSPICE_FILE& newSubSchematicElement : newSubSchematicElements )
148 {
149 wxString asyKey = getShortestKey( m_asyNamesMap, newSubSchematicElement.ElementName );
150
151 if( alg::contains( uniqueAscNameKeys, asyKey ) )
152 continue;
153
154 if( asyKey.IsEmpty() )
155 continue;
156
157 wxString asyBuffer = SafeReadFile( m_asyNamesMap.at( asyKey ), wxS( "r" ) );
158 m_asyFileCache[m_asyNamesMap.at( asyKey )] = asyBuffer;
159
160 if( IsAsySubsheet( asyBuffer ) )
161 collectHierarchy( asyKey );
162 }
163 };
164
165 collectHierarchy( rootElementName );
166
167 std::map<wxString, LTSPICE_SCHEMATIC::LT_ASC> parsedAscFiles = ParseAscFiles( m_ascFileCache );
168 std::map<wxString, SCH_SCREEN*> mapOfScreens;
169
170 // Create a SCH_SCREEN for each unique .asc file
171 for( const wxString& elementName : uniqueAscNameKeys )
172 {
173 SCH_SCREEN* screen;
174
175 if( elementName == rootElementName )
176 screen = aRootSheet->GetScreen();
177 else
178 screen = new SCH_SCREEN( aSchematic );
179
180 screen->SetFileName( aSchematic->Project().GetProjectPath() + GetSchematicFileName( elementName ) );
181
182 mapOfScreens[elementName] = screen;
183 }
184
185 // Create KiCad items for each .asc file
186 for( const wxString& elementName : uniqueAscNameKeys )
187 {
188 SCH_IO_LTSPICE_PARSER parser( this );
189
190 if( !m_ascNamesMap.contains( elementName ) )
191 continue;
192
193 if( !parsedAscFiles.contains( m_ascNamesMap[elementName] ) )
194 continue;
195
196 LTSPICE_SCHEMATIC::LT_ASC& ascFile = parsedAscFiles[m_ascNamesMap[elementName]];
197 SCH_SCREEN* screen = mapOfScreens[elementName];
198
199 const int grid = schIUScale.MilsToIU( 50 );
200 const int margin = grid * 10;
201 VECTOR2I pageSize;
202 BOX2I bbox;
203
204 bbox.Merge( parser.ToKicadCoords( ascFile.BoundingBox.GetOrigin() ) );
205 bbox.Merge( parser.ToKicadCoords( ascFile.BoundingBox.GetEnd() ) );
206
207 // Set smallest page size to fit all elements
210 {
211 PAGE_INFO pageInfo( sizeType, false );
212
213 screen->SetPageSettings( pageInfo );
214 pageSize = KiROUND( pageInfo.GetSizeIU( schIUScale.IU_PER_MILS ) );
215
216 if( bbox.GetWidth() + margin < pageSize.x && bbox.GetHeight() + margin < pageSize.y )
217 break;
218 }
219
220 VECTOR2I originOffset = ( ( pageSize / 2 ) - bbox.GetCenter() ) / grid * grid;
221
222 try
223 {
224 parser.CreateItems( ascFile, screen, uniqueAscNameKeys, mapOfScreens, originOffset );
225 }
226 catch( IO_ERROR& e )
227 {
228 aReporter->Report( e.What(), RPT_SEVERITY_ERROR );
229 }
230 }
231
232 // Fix label casing
233 std::set<SCH_SCREEN*> visitedScreens;
234
235 std::function<void( SCH_SCREEN*, std::map<wxString, wxString> )> fixLabelCase;
236 fixLabelCase = [&]( SCH_SCREEN* aScreen, std::map<wxString, wxString> aLabelMap )
237 {
238 // Collect additional special casing from labels on screen
239 for( SCH_ITEM* item : aScreen->Items() )
240 {
241 if( SCH_LABEL_BASE* lbl = dynamic_cast<SCH_LABEL_BASE*>( item ) )
242 {
243 wxString lower = lbl->GetText().Lower();
244
245 if( !aLabelMap.contains( lower ) && lbl->GetText() != lower )
246 aLabelMap[lower] = lbl->GetText();
247 }
248 }
249
250 // Apply special casing to all labels and recurse
251 for( SCH_ITEM* item : aScreen->Items() )
252 {
253 if( SCH_LABEL_BASE* lbl = dynamic_cast<SCH_LABEL_BASE*>( item ) )
254 {
255 wxString lower = lbl->GetText().Lower();
256
257 if( aLabelMap.contains( lower ) )
258 lbl->SetText( aLabelMap.at( lower ) );
259 }
260 else if( SCH_SHEET* sheet = dynamic_cast<SCH_SHEET*>( item ) )
261 {
262 SCH_SCREEN* sheetScreen = sheet->GetScreen();
263
264 if( visitedScreens.contains( sheetScreen ) )
265 continue;
266
267 visitedScreens.insert( sheetScreen );
268
269 std::map<wxString, wxString> childLabelMap;
270
271 for( SCH_SHEET_PIN* pin : sheet->GetPins() )
272 childLabelMap[pin->GetText().Lower()] = pin->GetText();
273
274 fixLabelCase( sheetScreen, childLabelMap );
275 }
276 }
277 };
278
279 fixLabelCase( aRootSheet->GetScreen(), {} );
280
281 SCH_SHEET_LIST sheets = aSchematic->BuildUnorderedSheetList();
282
283 // Number sheets in LTspice InstName order (parents before children, X2 before X10).
284 std::sort( sheets.begin(), sheets.end(),
285 []( const SCH_SHEET_PATH& a, const SCH_SHEET_PATH& b )
286 {
287 size_t len = std::min( a.size(), b.size() );
288
289 for( size_t i = 0; i < len; ++i )
290 {
291 SCH_SHEET* sheetA = a.at( i );
292 SCH_SHEET* sheetB = b.at( i );
293
294 if( sheetA->m_Uuid == sheetB->m_Uuid )
295 continue;
296
297 const SCH_FIELD* fieldA = sheetA->GetField( SCH_IO_LTSPICE_PARSER::LT_INSTNAME_FIELD );
298 const SCH_FIELD* fieldB = sheetB->GetField( SCH_IO_LTSPICE_PARSER::LT_INSTNAME_FIELD );
299 int cmp = StrNumCmp( fieldA ? fieldA->GetText() : wxString(),
300 fieldB ? fieldB->GetText() : wxString(), CMP_T::IGNORE_CASE );
301
302 if( cmp != 0 )
303 return cmp < 0;
304
305 return sheetA->m_Uuid < sheetB->m_Uuid;
306 }
307
308 return a.size() < b.size();
309 } );
310
311 sheets.SetInitialPageNumbers();
312
313 sheets.SortByHierarchicalPageNumbers( true );
314 sheets.CheckForMissingSymbolInstances( aSchematic->Project().GetProjectName() );
315
316 // Update references of all symbols in the schematic to include instance path suffixes ("R1_X4_X1")
317 for( SCH_SHEET_PATH& path : sheets )
318 {
319 if( path.size() <= 1 )
320 continue;
321
322 wxString suffix;
323
324 for( size_t i = 1; i < path.size(); ++i )
325 {
326 suffix += wxS( "_" );
327
328 if( SCH_FIELD* instNameField = path.at( i )->GetField( SCH_IO_LTSPICE_PARSER::LT_INSTNAME_FIELD ) )
329 suffix += instNameField->GetText();
330 }
331
332 SCH_SCREEN* screen = path.LastScreen();
333
334 if( !screen )
335 continue;
336
337 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
338 {
339 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
340 wxString ref = symbol->GetBaseRef( &path );
341
342 symbol->SetRef( &path, ref + suffix );
343 }
344 }
345
346 for( const SCH_SHEET_PATH& path : sheets )
347 path.UpdateAllScreenReferences();
348
349 // Fixing all junctions at the end
350 aSchematic->FixupJunctionsAfterImport();
351}
352
353
354void LTSPICE_SCHEMATIC::GetAscAndAsyFilePaths( const wxDir& aDir, bool aRecursive,
355 std::map<wxString, wxString>& aMapOfAscFiles,
356 std::map<wxString, wxString>& aMapOfAsyFiles,
357 const std::vector<wxString>& aBaseDirs )
358{
359 wxString filename;
360
361 if( !aDir.IsOpened() )
362 return;
363
364 bool cont = aDir.GetFirst( &filename, wxEmptyString, wxDIR_FILES | wxDIR_HIDDEN );
365
366 while( cont )
367 {
368 wxFileName path( aDir.GetName(), filename );
369
370 auto logToMap =
371 [&]( std::map<wxString, wxString>& aMapToLogTo, const wxString& aKey )
372 {
373 if( aMapToLogTo.count( aKey ) )
374 {
375 if( m_reporter )
376 {
377 m_reporter->Report( wxString::Format( _( "File at '%s' was ignored. Using previously "
378 "found file at '%s' instead." ),
379 path.GetFullPath(),
380 aMapToLogTo.at( aKey ) ), RPT_SEVERITY_WARNING );
381 }
382 }
383 else
384 {
385 aMapToLogTo.emplace( aKey, path.GetFullPath() );
386 }
387 };
388
389 // Add dir1/dir2/cmp, dir2/cmp, cmp
390 wxString extension = path.GetExt().Lower();
391
392 for( size_t i = 0; i < aBaseDirs.size() + 1; i++ )
393 {
394 wxString alias;
395
396 for( size_t j = i; j < aBaseDirs.size(); j++ )
397 alias << aBaseDirs[j].Lower() << '/';
398
399 alias << path.GetName().Lower();
400
401 if( extension == wxS( "asc" ) )
402 logToMap( aMapOfAscFiles, alias );
403 else if( extension == wxS( "asy" ) )
404 logToMap( aMapOfAsyFiles, alias );
405 }
406
407 cont = aDir.GetNext( &filename );
408 }
409
410 if( aRecursive )
411 {
412 cont = aDir.GetFirst( &filename, wxEmptyString, wxDIR_DIRS | wxDIR_HIDDEN );
413
414 while( cont )
415 {
416 wxFileName path( aDir.GetName(), filename );
417 wxDir subDir( path.GetFullPath() );
418
419 std::vector<wxString> newBase = aBaseDirs;
420 newBase.push_back( filename );
421
422 GetAscAndAsyFilePaths( subDir, true, aMapOfAscFiles, aMapOfAsyFiles, newBase );
423
424 cont = aDir.GetNext( &filename );
425 }
426 }
427}
428
429
430std::map<wxString, wxString> LTSPICE_SCHEMATIC::ReadAsyFiles( const std::vector<LTSPICE_FILE>& aSourceFiles,
431 const std::map<wxString, wxString>& aAsyFileMap )
432{
433 std::map<wxString, wxString> resultantMap;
434
435 for( const LTSPICE_FILE& source : aSourceFiles )
436 {
437 wxString fileName = source.ElementName;
438 wxString mapKey = getShortestKey( aAsyFileMap, fileName );
439
440 if( !mapKey.IsEmpty() )
441 resultantMap[fileName] = SafeReadFile( aAsyFileMap.at( mapKey ), wxS( "r" ) );
442 }
443
444 return resultantMap;
445}
446
447
448std::vector<LTSPICE_FILE> LTSPICE_SCHEMATIC::GetSchematicElements( const wxString& aAscFile )
449{
450 std::vector<LTSPICE_FILE> resultantArray;
451 wxArrayString lines = wxSplit( aAscFile, '\n' );
452
453 for( int i = 0; i < (int) lines.GetCount(); i++ )
454 {
455 wxArrayString tokens = wxSplit( lines[i], ' ' );
456
457 if( tokens.size() >= 4 && tokens[0].Upper() == wxS( "SYMBOL" ) )
458 {
459 wxString elementName( tokens[1] );
460 long posX, posY;
461
462 tokens[2].ToLong( &posX );
463 tokens[3].ToLong( &posY );
464
465 elementName.Replace( '\\', '/' );
466
467 LTSPICE_FILE asyFile( elementName, VECTOR2I( (int) posX, (int) posY ), i + 1 );
468
469 resultantArray.push_back( asyFile );
470 }
471 }
472
473 return resultantArray;
474}
475
476
477bool LTSPICE_SCHEMATIC::IsAsySubsheet( const wxString& aAsyFile )
478{
479 wxStringTokenizer lines( aAsyFile, "\r\n", wxTOKEN_STRTOK );
480
481 while( lines.HasMoreTokens() )
482 {
483 if( lines.GetNextToken().IsSameAs( wxS( "SymbolType BLOCK" ), false ) )
484 return true;
485 }
486
487 return false;
488}
489
490
491int LTSPICE_SCHEMATIC::integerCheck( const wxString& aToken, int aLineNumber, const wxString& aFileName )
492{
493 long result;
494
495 if( !aToken.ToLong( &result ) )
496 THROW_IO_ERRORF( _( "Expecting integer at line %d in file %s" ), aLineNumber, aFileName );
497
498 return (int) result;
499}
500
501
502VECTOR2I LTSPICE_SCHEMATIC::pointCheck( const wxString& aTokenX, const wxString& aTokenY, int aLineNumber,
503 const wxString& aFileName )
504{
505 return VECTOR2I( integerCheck( aTokenX, aLineNumber, aFileName ),
506 integerCheck( aTokenY, aLineNumber, aFileName ) );
507}
508
509
510void LTSPICE_SCHEMATIC::tokensSizeRangeCheck( size_t aActualSize, int aExpectedMin, int aExpectedMax,
511 int aLineNumber, const wxString& aFileName )
512{
513 if( (int) aActualSize < aExpectedMin )
514 THROW_IO_ERRORF( _( "Expected data missing on line %d in file %s" ), aLineNumber, aFileName );
515 else if( (int) aActualSize > aExpectedMax )
516 THROW_IO_ERRORF( _( "Extra data found on line %d in file %s" ), aLineNumber, aFileName );
517}
518
519
520void LTSPICE_SCHEMATIC::aggregateAttributeValue( wxArrayString& aTokens, int aIndex )
521{
522 // Merges a value which is across multiple tokens into one token with spaces in between.
523 for( int i = aIndex + 1; i < (int) aTokens.GetCount(); i++ )
524 aTokens[ aIndex ] += " " + aTokens[i];
525}
526
527
529{
530 std::map<int, LINESTYLE> lineStyleMap;
531
532 lineStyleMap[0] = LINESTYLE::SOLID;
533 lineStyleMap[1] = LINESTYLE::DASH;
534 lineStyleMap[2] = LINESTYLE::DOT;
535 lineStyleMap[3] = LINESTYLE::DASHDOT;
536 lineStyleMap[4] = LINESTYLE::DASHDOTDOT;
537
538 if( lineStyleMap.find( aValue ) == lineStyleMap.end() )
539 THROW_IO_ERROR( _( "Expecting 0, 1, 2, 3 or 4" ) );
540
541 return lineStyleMap[ aValue ];
542}
543
544
546{
547 std::map<wxString, LINEWIDTH> lineWidthMap;
548
549 lineWidthMap["NORMAL"] = LINEWIDTH::Normal;
550 lineWidthMap["WIDE"] = LINEWIDTH::Wide;
551
552 if( lineWidthMap.find( aValue.Upper() ) == lineWidthMap.end() )
553 THROW_IO_ERROR( _( "Expecting NORMAL or WIDE" ) );
554
555 return lineWidthMap[ aValue.Upper() ];
556}
557
558
560{
561 std::map<wxString, POLARITY> polarityMap;
562
563 polarityMap["I"] = POLARITY::PIN_INPUT;
564 polarityMap["O"] = POLARITY::OUTPUT;
565 polarityMap["B"] = POLARITY::BIDIR;
566 polarityMap["IN"] = POLARITY::PIN_INPUT;
567 polarityMap["OUT"] = POLARITY::OUTPUT;
568 polarityMap["BIDIR"] = POLARITY::BIDIR;
569
570 if( polarityMap.find( aValue.Upper() ) == polarityMap.end() )
571 THROW_IO_ERROR( _( "Expecting I, O, B, IN, OUT or BIDIR" ) );
572
573 return polarityMap[ aValue.Upper() ];
574}
575
576
578{
579 std::map<wxString, ORIENTATION> rotationMirrorMap;
580
581 rotationMirrorMap["R0"] = ORIENTATION::R0;
582 rotationMirrorMap["R90"] = ORIENTATION::R90;
583 rotationMirrorMap["R180"] = ORIENTATION::R180;
584 rotationMirrorMap["R270"] = ORIENTATION::R270;
585
586 rotationMirrorMap["M0"] = ORIENTATION::M0;
587 rotationMirrorMap["M90"] = ORIENTATION::M90;
588 rotationMirrorMap["M180"] = ORIENTATION::M180;
589 rotationMirrorMap["M270"] = ORIENTATION::M270;
590
591 if( rotationMirrorMap.find( aValue.Upper() ) == rotationMirrorMap.end() )
592 THROW_IO_ERROR( _( "Expecting R0, R90, R18, R270, M0, M90, M180 or M270" ) );
593
594 return rotationMirrorMap[ aValue.Upper() ];
595}
596
597
599{
600 std::map<wxString, JUSTIFICATION> justificationMap;
601
602 justificationMap["LEFT"] = JUSTIFICATION::LEFT;
603 justificationMap["CENTER"] = JUSTIFICATION::CENTER;
604 justificationMap["RIGHT"] = JUSTIFICATION::RIGHT;
605 justificationMap["VLEFT"] = JUSTIFICATION::VLEFT;
606 justificationMap["VRIGHT"] = JUSTIFICATION::VRIGHT;
607 justificationMap["VCENTER"] = JUSTIFICATION::VCENTER;
608 justificationMap["BOTTOM"] = JUSTIFICATION::BOTTOM;
609 justificationMap["TOP"] = JUSTIFICATION::TOP;
610 justificationMap["VBOTTOM"] = JUSTIFICATION::VBOTTOM;
611 justificationMap["VTOP"] = JUSTIFICATION::VTOP;
612 justificationMap["INVISIBLE"] = JUSTIFICATION::INVISIBLE;
613
614 if( justificationMap.find( aValue.Upper() ) == justificationMap.end() )
615 THROW_IO_ERROR( _( "Expecting LEFT, CENTER, RIGHT, TOP, BOTTOM, VLEFT, VRIGHT, VCENTER, "
616 "VTOP, VBOTTOM or INVISIBLE" ) );
617
618 return justificationMap[ aValue.Upper() ];
619}
620
621
623{
624 std::map<wxString, JUSTIFICATION> pinJustificationMap;
625
626 pinJustificationMap["BOTTOM"] = JUSTIFICATION::BOTTOM;
627 pinJustificationMap["NONE"] = JUSTIFICATION::NONE;
628 pinJustificationMap["LEFT"] = JUSTIFICATION::LEFT;
629 pinJustificationMap["RIGHT"] = JUSTIFICATION::RIGHT;
630 pinJustificationMap["TOP"] = JUSTIFICATION::TOP;
631 pinJustificationMap["VBOTTOM"] = JUSTIFICATION::VBOTTOM;
632 pinJustificationMap["VLEFT"] = JUSTIFICATION::VLEFT;
633 pinJustificationMap["VRIGHT"] = JUSTIFICATION::VRIGHT;
634 pinJustificationMap["VCENTER"] = JUSTIFICATION::VCENTER;
635 pinJustificationMap["VTOP"] = JUSTIFICATION::VTOP;
636
637 if( pinJustificationMap.find( aValue.Upper() ) == pinJustificationMap.end() )
638 THROW_IO_ERROR( _( "Expecting NONE, BOTTOM, TOP, LEFT, RIGHT, VBOTTOM, VTOP, VCENTER, VLEFT or VRIGHT" ) );
639
640 return pinJustificationMap[ aValue.Upper() ];
641}
642
643
645{
646 std::map<wxString, SYMBOLTYPE> symbolTypeMap;
647
648 symbolTypeMap["CELL"] = SYMBOLTYPE::CELL;
649 symbolTypeMap["BLOCK"] = SYMBOLTYPE::BLOCK;
650
651 if( symbolTypeMap.find( aValue.Upper() ) == symbolTypeMap.end() )
652 THROW_IO_ERROR( _( "Expecting CELL or BLOCK" ) );
653
654 return symbolTypeMap[ aValue.Upper() ];
655}
656
657
658void LTSPICE_SCHEMATIC::removeCarriageReturn( wxString& elementFromLine )
659{
660 if( elementFromLine.EndsWith( '\r' ) )
661 elementFromLine = elementFromLine.BeforeLast( '\r' );
662}
663
664
666 const wxString& aAsyFileContent, LT_ASC& aAscFile )
667{
668 LT_SYMBOL lt_symbol;
669 int lineNumber = 1;
670
671 lt_symbol.Name = aSymbolName;
674
675 for( wxString line : wxSplit( aAsyFileContent, '\n' ) )
676 {
677 removeCarriageReturn( line );
678
679 wxArrayString tokens = wxSplit( line, ' ' );
680
681 if( tokens.IsEmpty() )
682 continue;
683
684 wxString element = tokens[0].Upper();
685
686 if( element == "LINE" )
687 {
688 tokensSizeRangeCheck( tokens.size(), 6, 7, lineNumber, aSymbolName );
689
690 wxString lineWidth = tokens[1];
691 wxString startPointX = tokens[2];
692 wxString startPointY = tokens[3];
693 wxString endPointX = tokens[4];
694 wxString endPointY = tokens[5];
695
696 LINE lt_line;
697 lt_line.LineWidth = getLineWidth( lineWidth );
698 lt_line.Start = pointCheck( startPointX, startPointY, lineNumber, aSymbolName );
699 lt_line.End = pointCheck( endPointX, endPointY, lineNumber, aSymbolName );
700
701 int lineStyleNumber = 0; // default
702
703 if( tokens.size() == 7 )
704 {
705 wxString lineStyle = tokens[6];
706 lineStyleNumber = integerCheck( lineStyle, lineNumber, aSymbolName );
707 }
708
709 lt_line.LineStyle = getLineStyle( lineStyleNumber );
710
711 lt_symbol.Lines.push_back( lt_line );
712 }
713 else if( element == "RECTANGLE" )
714 {
715 tokensSizeRangeCheck( tokens.size(), 6, 7, lineNumber, aSymbolName );
716
717 wxString lineWidth = tokens[1];
718 wxString botRightX = tokens[2];
719 wxString botRightY = tokens[3];
720 wxString topLeftX = tokens[4];
721 wxString topRightY = tokens[5];
722
724 rect.LineWidth = getLineWidth( lineWidth );
725 rect.BotRight = pointCheck( botRightX, botRightY, lineNumber, aSymbolName );
726 rect.TopLeft = pointCheck( topLeftX, topRightY, lineNumber, aSymbolName );
727
728 int lineStyleNumber = 0; // default
729
730 if( tokens.size() == 7 )
731 {
732 wxString lineStyle = tokens[6];
733 lineStyleNumber = integerCheck( lineStyle, lineNumber, aSymbolName );
734 }
735
736 rect.LineStyle = getLineStyle( lineStyleNumber );
737
738 lt_symbol.Rectangles.push_back( rect );
739 }
740 else if( element == "CIRCLE" )
741 {
746 tokensSizeRangeCheck( tokens.size(), 6, 7, lineNumber, aSymbolName );
747
748 wxString lineWidth = tokens[1];
749 wxString botRightX = tokens[2];
750 wxString botRightY = tokens[3];
751 wxString topLeftX = tokens[4];
752 wxString topRightY = tokens[5];
753
755 circle.LineWidth = getLineWidth( lineWidth );
756 circle.BotRight = pointCheck( botRightX, botRightY, lineNumber, aSymbolName );
757 circle.TopLeft = pointCheck( topLeftX, topRightY, lineNumber, aSymbolName );
758
759 int lineStyleNumber = 0; // default
760
761 if( tokens.size() == 7 )
762 {
763 wxString lineStyle = tokens[6];
764 lineStyleNumber = integerCheck( lineStyle, lineNumber, aSymbolName );
765 }
766
767 circle.LineStyle = getLineStyle( lineStyleNumber );
768
769 lt_symbol.Circles.push_back( circle );
770 }
771 else if( element == "ARC" )
772 {
778 tokensSizeRangeCheck( tokens.size(), 10, 11, lineNumber, aSymbolName );
779
780 wxString lineWidth = tokens[1];
781 wxString botRightX = tokens[2];
782 wxString botRightY = tokens[3];
783 wxString topLeftX = tokens[4];
784 wxString topRightY = tokens[5];
785 wxString arcStartPointX = tokens[6];
786 wxString arcStartPointY = tokens[7];
787 wxString arcEndPointX = tokens[8];
788 wxString arcEndPointY = tokens[9];
789
790 ARC arc;
791 arc.LineWidth = getLineWidth( lineWidth );
792 arc.BotRight = pointCheck( botRightX, botRightY, lineNumber, aSymbolName );
793 arc.TopLeft = pointCheck( topLeftX, topRightY, lineNumber, aSymbolName );
794 arc.ArcStart = pointCheck( arcStartPointX, arcStartPointY, lineNumber, aSymbolName );
795 arc.ArcEnd = pointCheck( arcEndPointX, arcEndPointY, lineNumber, aSymbolName );
796
797 int lineStyleNumber = 0; // default
798
799 if( tokens.size() == 11 )
800 {
801 wxString lineStyle = tokens[10];
802 lineStyleNumber = integerCheck( lineStyle, lineNumber, aSymbolName );
803 }
804
805 arc.LineStyle = getLineStyle( lineStyleNumber );
806
807 lt_symbol.Arcs.push_back( arc );
808 }
809 else if( element == "WINDOW" )
810 {
811 tokensSizeRangeCheck( tokens.size(), 6, 6, lineNumber, aSymbolName );
812
813 wxString number = tokens[1];
814 wxString windowPosX = tokens[2];
815 wxString windowPosY = tokens[3];
816 wxString justification = tokens[4];
817 wxString fontSize = tokens[5];
818
819 LT_WINDOW window;
820 window.WindowNumber = integerCheck( number, lineNumber, aSymbolName );
821 window.Position = pointCheck( windowPosX, windowPosY, lineNumber, aSymbolName );
822 window.Justification = getTextJustification( justification );
823 window.FontSize = integerCheck( fontSize, lineNumber, aSymbolName );
824
825 // LTSpice appears to ignore hidden property from .asy files
826 if( window.FontSize == 0 )
827 window.FontSize = 2;
828
829 lt_symbol.Windows.push_back( window );
830 }
831 else if( element == "TEXT" )
832 {
833 aggregateAttributeValue( tokens, 5 );
834
835 tokensSizeRangeCheck( tokens.size(), 6, INT_MAX, lineNumber, aSymbolName );
836
837 wxString positionX = tokens[1];
838 wxString positionY = tokens[2];
839 wxString justification = tokens[3];
840 wxString fontSize = tokens[4];
841 wxString value = tokens[5];
842
843 TEXT text;
844 text.Offset = pointCheck( positionX, positionY, lineNumber, aSymbolName );
845 text.Justification = getTextJustification( justification );
846 text.FontSize = integerCheck( fontSize, lineNumber, aSymbolName );
847 text.Value = value;
848
849 text.Value.Replace( wxS( "\\n" ), wxS( "\n" ) );
850
851 lt_symbol.Texts.push_back( text );
852 }
853 else if( element == "SYMATTR" )
854 {
855 aggregateAttributeValue( tokens, 2 );
856
857 tokensSizeRangeCheck( tokens.size(), 3, INT_MAX, lineNumber, aSymbolName );
858
859 wxString key = tokens[1];
860 wxString value = tokens[2];
861
862 if( value == wxS( "\"\"" ) )
863 value = wxEmptyString;
864
865 lt_symbol.SymAttributes[ key.Upper() ] = value;
866 }
867 else if( element == "PIN" )
868 {
869 tokensSizeRangeCheck( tokens.size(), 5, 5, lineNumber, aSymbolName );
870
871 wxString pinLocationX = tokens[1];
872 wxString pinLocationY = tokens[2];
873 wxString Justification = tokens[3];
874 wxString nameOffSet = tokens[4];
875
876 LT_PIN pin;
877 pin.PinLocation = pointCheck( pinLocationX, pinLocationY, lineNumber,aSymbolName );
878 pin.PinJustification = getPinJustification( Justification );
879 pin.NameOffSet = integerCheck( nameOffSet, lineNumber, aSymbolName );
880
881 lt_symbol.Pins.push_back( pin );
882 }
883 else if( element == "PINATTR" )
884 {
885 aggregateAttributeValue( tokens, 2 );
886
887 tokensSizeRangeCheck( tokens.size(), 3, INT_MAX, lineNumber, aSymbolName );
888
889 wxString name = tokens[1];
890 wxString Value = tokens[2];
891
892 lt_symbol.Pins.back().PinAttribute.insert( { name, Value } );
893 }
894 else if( element == "SYMBOLTYPE" )
895 {
896 tokensSizeRangeCheck( tokens.size(), 2, 2, lineNumber, aSymbolName );
897
898 wxString symbolType = tokens[1];
899
900 lt_symbol.SymbolType = getSymbolType( symbolType );
901 }
902
903 lineNumber++;
904 }
905
906 return lt_symbol;
907}
908
909
911{
912 LT_SYMBOL lt_symbol;
913
914 lt_symbol.Name = aAscFileName;
915 lt_symbol.AsyKey = aAscFileName.Lower();
918
920 rect.LineWidth = LINEWIDTH::Wide;
921 rect.LineStyle = LINESTYLE::DASH;
922 rect.BotRight = VECTOR2I( 50, 50 );
923 rect.TopLeft = VECTOR2I( -50, -50 );
924 lt_symbol.Rectangles.push_back( rect );
925
926 return lt_symbol;
927}
928
929
930std::map<wxString, LTSPICE_SCHEMATIC::LT_ASC>
931LTSPICE_SCHEMATIC::ParseAscFiles( const std::map<wxString, wxString>& aAscFilesContent )
932{
933 // Initialising Schematic Struct
934
935 std::map<wxString, LT_ASC> ascFiles;
936
937 for( const auto& [ fileName, contents ] : aAscFilesContent )
938 {
939 LT_ASC ascFile;
940 std::vector<LT_SYMBOL> symbolArray;
941
942 ascFile.SheetSize = VECTOR2I( 0, 0 );
943 ascFile.Symbols = symbolArray;
944 ascFile.Version = 0;
945 ascFile.VersionMinor = 0;
946 ascFile.SheetNumber = 0;
947
948 int lineNumber = 1;
949
950 for( wxString line : wxSplit( contents, '\n' ) )
951 {
952 removeCarriageReturn( line );
953
954 wxArrayString tokens = wxSplit( line, ' ' );
955
956 if( tokens.IsEmpty() )
957 continue;
958
959 wxString element = tokens[0].Upper();
960
961 if( element == "SHEET" )
962 {
963 tokensSizeRangeCheck( tokens.size(), 4, 4, lineNumber, fileName );
964
965 wxString sheetNumber = tokens[1];
966 wxString sheetWidth = tokens[2];
967 wxString sheetHeight = tokens[3];
968
969 ascFile.SheetNumber = integerCheck( sheetNumber, lineNumber, fileName );
970 ascFile.SheetSize = pointCheck( sheetWidth, sheetHeight, lineNumber, fileName );
971 }
972 else if( element == "SYMBOL" )
973 {
974 tokensSizeRangeCheck( tokens.size(), 5, 5, lineNumber, fileName );
975
976 wxString symbolName = tokens[1];
977 wxString posX = tokens[2];
978 wxString posY = tokens[3];
979 wxString rotate_mirror_option = tokens[4];
980
981 symbolName.Replace( '\\', '/' );
982
983 try
984 {
985 wxString key = getShortestKey( m_asyNamesMap, symbolName );
986
987 if( !m_asyNamesMap.contains( key ) )
988 {
989 wxString fullName = wxFileName( fileName ).GetFullName();
990
992 wxString::Format( _( "Missing symbol file '%s.asy', referenced from '%s' at line %d" ),
993 symbolName, fullName, lineNumber ) );
994 }
995
996 if( !m_asyFileCache.contains( m_asyNamesMap.at( key ) ) )
997 THROW_IO_ERROR( wxString::Format( _( "Unable to read '%s'" ), m_asyNamesMap.at( key ) ) );
998
999 LT_SYMBOL lt_symbol =
1000 ParseSymbol( symbolName, m_asyFileCache.at( m_asyNamesMap.at( key ) ), ascFile );
1001
1002 lt_symbol.AsyKey = key;
1003 lt_symbol.Offset = pointCheck( posX, posY, lineNumber, fileName );
1004 lt_symbol.SymbolOrientation = getSymbolRotationOrMirror( rotate_mirror_option );
1005
1006 ascFile.Symbols.push_back( lt_symbol );
1007 ascFile.BoundingBox.Merge( lt_symbol.Offset );
1008 }
1009 catch( IO_ERROR& e )
1010 {
1011 // the box stands in for the part, so this is not an error, and once per symbol is enough
1012 if( m_reporter && m_missingSymbols.insert( symbolName.Lower() ).second )
1013 m_reporter->Report( e.What(), RPT_SEVERITY_WARNING );
1014
1015 // Use dummy symbol
1016 LT_SYMBOL lt_symbol = MakeDummySymbol( symbolName );
1017 lt_symbol.Offset = pointCheck( posX, posY, lineNumber, fileName );
1018 lt_symbol.SymbolOrientation = getSymbolRotationOrMirror( rotate_mirror_option );
1019
1020 ascFile.Symbols.push_back( lt_symbol );
1021 ascFile.BoundingBox.Merge( lt_symbol.Offset );
1022 }
1023 }
1024 else if( element == "WIRE" )
1025 {
1026 tokensSizeRangeCheck( tokens.size(), 5, 5, lineNumber, fileName );
1027
1028 wxString startPointX = tokens[1];
1029 wxString startPointY = tokens[2];
1030 wxString endPointX = tokens[3];
1031 wxString endPointY = tokens[4];
1032
1033 WIRE wire;
1034 wire.Start = pointCheck( startPointX, startPointY, lineNumber, fileName );
1035 wire.End = pointCheck( endPointX, endPointY, lineNumber, fileName );
1036
1037 ascFile.Wires.push_back( wire );
1038 ascFile.BoundingBox.Merge( wire.Start );
1039 ascFile.BoundingBox.Merge( wire.End );
1040 }
1041 else if( element == "FLAG" )
1042 {
1043 tokensSizeRangeCheck( tokens.size(), 4, 4, lineNumber, fileName );
1044
1045 wxString posX = tokens[1];
1046 wxString posY = tokens[2];
1047 wxString name = tokens[3];
1048
1049 FLAG flag;
1050 flag.Offset = pointCheck( posX, posY, lineNumber, fileName );
1051 flag.Value = name;
1052 flag.FontSize = 2;
1053
1054 ascFile.Flags.push_back( flag );
1055 ascFile.BoundingBox.Merge( flag.Offset );
1056 }
1057 else if( element == "DATAFLAG" )
1058 {
1059 tokensSizeRangeCheck( tokens.size(), 4, 4, lineNumber, fileName );
1060
1061 wxString posX = tokens[1];
1062 wxString posY = tokens[2];
1063 wxString expression = tokens[3];
1064
1065 DATAFLAG flag;
1066 flag.Offset = pointCheck( posX, posY, lineNumber, fileName );
1067 flag.Expression = expression;
1068 flag.FontSize = 2;
1069
1070 ascFile.DataFlags.push_back( flag );
1071 ascFile.BoundingBox.Merge( flag.Offset );
1072 }
1073 else if( element == "WINDOW" )
1074 {
1075 tokensSizeRangeCheck( tokens.size(), 6, 6, lineNumber, fileName );
1076
1077 wxString number = tokens[1];
1078 wxString windowPosX = tokens[2];
1079 wxString windowPosY = tokens[3];
1080 wxString justification = tokens[4];
1081 wxString fontSize = tokens[5];
1082
1083 LT_SYMBOL* symbol = &ascFile.Symbols.back();
1084 LT_WINDOW* window = nullptr;
1085 int windowNumber = integerCheck( number, lineNumber, fileName );
1086
1087 // Overwrite an existing window from the symbol definition
1088 for( LT_WINDOW& candidate : symbol->Windows )
1089 {
1090 if( candidate.WindowNumber == windowNumber )
1091 {
1092 window = &candidate;
1093 break;
1094 }
1095 }
1096
1097 if( !window )
1098 {
1099 symbol->Windows.emplace_back( LT_WINDOW() );
1100 window = &symbol->Windows.back();
1101 }
1102
1103 window->WindowNumber = windowNumber;
1104 window->Position = pointCheck( windowPosX, windowPosY, lineNumber, fileName );
1105 window->Justification = getTextJustification( justification );
1106 window->FontSize = integerCheck( fontSize, lineNumber, fileName );
1107
1108 ascFile.BoundingBox.Merge( symbol->Offset + window->Position );
1109 }
1110 else if( element == "SYMATTR" )
1111 {
1112 tokensSizeRangeCheck( tokens.size(), 3, INT_MAX, lineNumber, fileName );
1113
1114 aggregateAttributeValue( tokens, 2 );
1115
1116 wxString name = tokens[1];
1117 wxString value = tokens[2];
1118
1119 if( value == wxS( "\"\"" ) )
1120 value = wxEmptyString;
1121
1122 ascFile.Symbols.back().SymAttributes[ name.Upper() ] = value;
1123 }
1124 else if( element == "LINE" )
1125 {
1126 tokensSizeRangeCheck( tokens.size(), 6, 7, lineNumber, fileName );
1127
1128 wxString lineWidth = tokens[1];
1129 wxString startPointX = tokens[2];
1130 wxString startPointY = tokens[3];
1131 wxString endPointX = tokens[4];
1132 wxString endPointY = tokens[5];
1133
1134 LINE lt_line;
1135 lt_line.LineWidth = getLineWidth( lineWidth );
1136 lt_line.Start = pointCheck( startPointX, startPointY, lineNumber, fileName );
1137 lt_line.End = pointCheck( endPointX, endPointY, lineNumber, fileName );
1138
1139 int lineStyleNumber = 0; // default
1140
1141 if( tokens.size() == 7 )
1142 {
1143 wxString lineStyle = tokens[6];
1144 lineStyleNumber = integerCheck( lineStyle, lineNumber, fileName );
1145 }
1146
1147 lt_line.LineStyle = getLineStyle( lineStyleNumber );
1148
1149 ascFile.Lines.push_back( lt_line );
1150 ascFile.BoundingBox.Merge( lt_line.Start );
1151 ascFile.BoundingBox.Merge( lt_line.End );
1152 }
1153 else if( element == "RECTANGLE" )
1154 {
1155 tokensSizeRangeCheck( tokens.size(), 6, 7, lineNumber, fileName );
1156
1157 // wxString lineWidth = tokens[1];
1158 wxString botRightX = tokens[2];
1159 wxString botRightY = tokens[3];
1160 wxString topLeftX = tokens[4];
1161 wxString topRightY = tokens[5];
1162
1164 rect.LineWidth = getLineWidth( tokens[1] );
1165 rect.BotRight = pointCheck( botRightX, botRightY, lineNumber, fileName );
1166 rect.TopLeft = pointCheck( topLeftX, topRightY, lineNumber, fileName );
1167
1168 int lineStyleNumber = 0; // default
1169
1170 if( tokens.size() == 7 )
1171 {
1172 wxString lineStyle = tokens[6];
1173 lineStyleNumber = integerCheck( lineStyle, lineNumber, fileName );
1174 }
1175
1176 rect.LineStyle = getLineStyle( lineStyleNumber );
1177
1178 ascFile.Rectangles.push_back( rect );
1179 ascFile.BoundingBox.Merge( rect.TopLeft );
1180 ascFile.BoundingBox.Merge( rect.BotRight );
1181 }
1182 else if( element == "CIRCLE" )
1183 {
1184 tokensSizeRangeCheck( tokens.size(), 6, 7, lineNumber, fileName );
1185
1186 wxString lineWidth = tokens[1];
1187 wxString botRightX = tokens[2];
1188 wxString botRightY = tokens[3];
1189 wxString topLeftX = tokens[4];
1190 wxString topRightY = tokens[5];
1191
1192 CIRCLE circle;
1193 circle.LineWidth = getLineWidth( lineWidth );
1194 circle.BotRight = pointCheck( botRightX, botRightY, lineNumber, fileName );
1195 circle.TopLeft = pointCheck( topLeftX, topRightY, lineNumber, fileName );
1196
1197 int lineStyleNumber = 0; // default
1198
1199 if( tokens.size() == 7 )
1200 {
1201 wxString lineStyle = tokens[6];
1202 lineStyleNumber = integerCheck( lineStyle, lineNumber, fileName );
1203 }
1204
1205 circle.LineStyle = getLineStyle( lineStyleNumber );
1206
1207 ascFile.Circles.push_back( circle );
1208 ascFile.BoundingBox.Merge( circle.TopLeft );
1209 ascFile.BoundingBox.Merge( circle.BotRight );
1210 }
1211 else if( element == "ARC" )
1212 {
1218 tokensSizeRangeCheck( tokens.size(), 10, 11, lineNumber, fileName );
1219
1220 wxString lineWidth = tokens[1];
1221 wxString botRightX = tokens[2];
1222 wxString botRightY = tokens[3];
1223 wxString topLeftX = tokens[4];
1224 wxString topRightY = tokens[5];
1225 wxString arcStartPointX = tokens[6];
1226 wxString arcStartPointY = tokens[7];
1227 wxString arcEndPointX = tokens[8];
1228 wxString arcEndPointY = tokens[9];
1229
1230 ARC arc;
1231 arc.LineWidth = getLineWidth( lineWidth );
1232 arc.BotRight = pointCheck( botRightX, botRightY, lineNumber, fileName );
1233 arc.TopLeft = pointCheck( topLeftX, topRightY, lineNumber, fileName );
1234 arc.ArcEnd = pointCheck( arcStartPointX, arcStartPointY, lineNumber, fileName );
1235 arc.ArcStart = pointCheck( arcEndPointX, arcEndPointY, lineNumber, fileName );
1236
1237 int lineStyleNumber = 0; // default
1238
1239 if( tokens.size() == 11 )
1240 {
1241 wxString lineStyle = tokens[10];
1242 lineStyleNumber = integerCheck( lineStyle, lineNumber, fileName );
1243 }
1244
1245 arc.LineStyle = getLineStyle( lineStyleNumber );
1246
1247 ascFile.Arcs.push_back( arc );
1248 ascFile.BoundingBox.Merge( arc.TopLeft );
1249 ascFile.BoundingBox.Merge( arc.BotRight );
1250 }
1251 else if( element == "IOPIN" )
1252 {
1253 tokensSizeRangeCheck( tokens.size(), 4, 4, lineNumber, fileName );
1254
1255 wxString pinLocationX = tokens[1];
1256 wxString pinLocationY = tokens[2];
1257 wxString pinPolarity = tokens[3];
1258
1259 IOPIN iopin;
1260 iopin.Location = pointCheck( pinLocationX, pinLocationY, lineNumber, fileName );
1261 iopin.Polarity = getPolarity( pinPolarity );
1262
1263 ascFile.Iopins.push_back( iopin );
1264 ascFile.BoundingBox.Merge( iopin.Location );
1265 }
1266 else if( element == "TEXT" )
1267 {
1268 aggregateAttributeValue( tokens, 5 );
1269
1270 tokensSizeRangeCheck( tokens.size(), 6, INT_MAX, lineNumber, fileName );
1271
1272 wxString positionX = tokens[1];
1273 wxString positionY = tokens[2];
1274 wxString justification = tokens[3];
1275 wxString fontSize = tokens[4];
1276 wxString value = tokens[5];
1277
1278 TEXT text;
1279 text.Offset = pointCheck( positionX, positionY, lineNumber, fileName );
1280 text.Justification = getTextJustification( justification );
1281 text.FontSize = integerCheck( fontSize, lineNumber, fileName );
1282
1283 if( value.StartsWith( wxS( "!" ), &text.Value ) )
1284 text.Value.Replace( wxS( "! " ), wxS( "\n" ) ); // replace subsequent ! with \n
1285 else if( value.StartsWith( wxS( ";" ), &text.Value ) )
1286 text.Value.Replace( wxS( "; " ), wxS( "\n" ) ); // replace subsequent ; with \n
1287 else
1288 text.Value = value;
1289
1290 text.Value.Replace( wxS( "\\n" ), wxS( "\n" ) );
1291
1292 ascFile.Texts.push_back( text );
1293 ascFile.BoundingBox.Merge( text.Offset );
1294 }
1295 else if( element == "BUSTAP" )
1296 {
1297 tokensSizeRangeCheck( tokens.size(), 5, 5, lineNumber, fileName );
1298
1299 wxString startPointX = tokens[1];
1300 wxString startPointY = tokens[2];
1301 wxString endPointX = tokens[3];
1302 wxString endPointY = tokens[4];
1303
1304 BUSTAP bustap;
1305 bustap.Start = pointCheck( startPointX, startPointY, lineNumber, fileName );
1306 bustap.End = pointCheck( endPointX, endPointY, lineNumber, fileName );
1307
1308 ascFile.Bustap.push_back( bustap );
1309 ascFile.BoundingBox.Merge( bustap.Start );
1310 ascFile.BoundingBox.Merge( bustap.End );
1311 }
1312 else if( element == "VERSION" )
1313 {
1314 wxString versionNumber = tokens[1];
1315 if( versionNumber.Contains( '.' ) )
1316 {
1317 wxString majorStr = versionNumber.BeforeFirst( '.' );
1318 wxString minorStr = versionNumber.AfterFirst( '.' );
1319 ascFile.Version = integerCheck( majorStr, lineNumber, fileName );
1320 ascFile.VersionMinor = integerCheck( minorStr, lineNumber, fileName );
1321 }
1322 else
1323 {
1324 ascFile.Version = integerCheck( versionNumber, lineNumber, fileName );
1325 }
1326 }
1327
1328 lineNumber++;
1329 }
1330
1331 ascFiles[fileName] = std::move( ascFile );
1332 }
1333
1334 return ascFiles;
1335}
1336
1337
const char * name
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:130
BOX2< VECTOR2I > BOX2I
Definition box2.h:917
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:985
constexpr const Vec GetEnd() const
Definition box2.h:215
constexpr size_type GetWidth() const
Definition box2.h:217
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition box2.h:597
constexpr const Vec GetCenter() const
Definition box2.h:233
constexpr size_type GetHeight() const
Definition box2.h:218
constexpr const Vec & GetOrigin() const
Definition box2.h:213
EE_TYPE OfType(KICAD_T aType) const
Definition sch_rtree.h:248
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
static UTF8 FixIllegalChars(const UTF8 &aLibItemName, bool aLib)
Replace illegal LIB_ID item name characters with underscores '_'.
Definition lib_id.cpp:205
static VECTOR2I pointCheck(const wxString &aTokenX, const wxString &aTokenY, int aLineNumber, const wxString &aFileName)
LT_SYMBOL MakeDummySymbol(const wxString &aAscFileName)
static SYMBOLTYPE getSymbolType(const wxString &aValue)
LT_SYMBOL ParseSymbol(const wxString &aSymbolName, const wxString &aAsyFileContent, LT_ASC &aAscFile)
std::map< wxString, wxString > m_ascNamesMap
std::map< wxString, LT_ASC > ParseAscFiles(const std::map< wxString, wxString > &aAscFilesContent)
Build Intermediate data structure.
std::vector< LTSPICE_FILE > GetSchematicElements(const wxString &aAscFile)
Used to get symbols list present in asc file.
POLARITY
Polarity enum represents polarity of pin.
wxString GetSchematicFileName(const wxString &aAscKey) const
Return "amp.kicad_sch" for "foo/amp" when both keys are one .asc, else "foo_amp.kicad_sch".
wxString GetSymbolName(const wxString &aSymbolName) const
Return "amp" for "foo/amp" when both keys are one .asy, else "foo_amp".
JUSTIFICATION
Defines in what ways the PIN or TEXT can be justified.
static ORIENTATION getSymbolRotationOrMirror(const wxString &aValue)
bool IsAsySubsheet(const wxString &aAsyFile)
Check if the asy file content indicates that we need to load subsheet.
std::map< wxString, wxString > m_asyNamesMap
static POLARITY getPolarity(const wxString &aValue)
static void tokensSizeRangeCheck(size_t aActualSize, int aExpectedMin, int aExpectedMax, int aLineNumber, const wxString &aFileName)
Used to check size of the token generated from split function.
std::map< wxString, wxString > m_asyFileCache
static LINEWIDTH getLineWidth(const wxString &aValue)
ORIENTATION
Defines different types or rotation and mirror operation can be applied to a LT_SYMBOL.
static void aggregateAttributeValue(wxArrayString &aTokens, int aIndex)
Join value present across multiple tokens into one.
void GetAscAndAsyFilePaths(const wxDir &aDir, bool aRecursive, std::map< wxString, wxString > &aMapOfAscFiles, std::map< wxString, wxString > &aMapOfAsyFiles, const std::vector< wxString > &aBaseDirs={})
Used to get file path for Asc and Asy files.
void Load(SCHEMATIC *aSchematic, SCH_SHEET *aRootSheet, const wxFileName &aLibraryFileName, REPORTER *aReporter)
The main function responsible for loading the .asc and .asy files.
static int integerCheck(const wxString &aToken, int aLineNumber, const wxString &aFileName)
Used to check if the given token in integer or not.
std::set< wxString > m_missingSymbols
static JUSTIFICATION getTextJustification(const wxString &aValue)
static JUSTIFICATION getPinJustification(const wxString &aValue)
SYMBOLTYPE
Type of Symbol loaded from asc and asy file.
std::map< wxString, wxString > m_ascFileCache
std::map< wxString, wxString > ReadAsyFiles(const std::vector< LTSPICE_FILE > &aSourceFiles, const std::map< wxString, wxString > &aAsyFileMap)
The function returns a map.
static void removeCarriageReturn(wxString &elementFromLine)
static LINESTYLE getLineStyle(int aValue)
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition page_info.h:75
const VECTOR2D GetSizeIU(double aIUScale) const
Gets the page size in internal units.
Definition page_info.h:172
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition project.cpp:277
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:102
Holds all the data relating to one schematic.
Definition schematic.h:155
SCH_SHEET_LIST BuildUnorderedSheetList() const
PROJECT & Project() const
Return a reference to the project this schematic is part of.
Definition schematic.h:177
The class is been used for loading the asc and asy files in intermediate data structure.
static constexpr const wxChar * LT_INSTNAME_FIELD
int ToKicadCoords(int aCoordinate)
Method converts ltspice coordinate(i.e scale) to kicad coordinates.
void CreateItems(LTSPICE_SCHEMATIC::LT_ASC &aAscfile, SCH_SCREEN *aScreen, const std::vector< wxString > &aUniqueAscNames, std::map< wxString, SCH_SCREEN * > &aMapOfScreens, VECTOR2I aOriginOffset)
Create KiCad items based on LTspice data.
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:170
void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition sch_screen.h:145
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:122
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void SetInitialPageNumbers()
Set initial sheet page numbers.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
size_t size() const
Forwarded method from std::vector.
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:50
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:187
Schematic symbol object.
Definition sch_symbol.h:76
wxString GetBaseRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const
void SetRef(const SCH_SHEET_PATH *aSheet, const wxString &aReference)
Set the base reference for the given sheet path for this symbol.
wxString wx_str() const
Definition utf8.cpp:41
#define _(s)
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
#define THROW_IO_ERRORF(msg,...)
static const std::string KiCadSchematicFileExtension
std::string source
static wxString getShortestKey(const std::map< wxString, wxString > &aMap, const wxString &aName)
Loads the asc file and asy files.
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition kicad_algo.h:96
PAGE_SIZE_TYPE
Definition page_info.h:46
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
wxString SafeReadFile(const wxString &aFilePath, const wxString &aReadType)
Nominally opens a file and reads it into a string.
Definition richio.cpp:51
bool ReplaceIllegalFileNameChars(std::string &aName, int aReplaceChar)
Checks aName for illegal file name characters.
The ARC is represented inside a rectangle whose opposite site are given.
The CIRCLE is represented in Ltpsice inside a rectangle whose two opposite points and line style are ...
IOPIN is special contact on symbol used for IO operations.
A struct to hold .asc file definition.
std::vector< CIRCLE > Circles
std::vector< IOPIN > Iopins
std::vector< BUSTAP > Bustap
std::vector< LT_SYMBOL > Symbols
std::vector< DATAFLAG > DataFlags
std::vector< RECTANGLE > Rectangles
A struct to hold SYMBOL definition.
std::map< wxString, wxString > SymAttributes
std::vector< RECTANGLE > Rectangles
std::vector< LT_WINDOW > Windows
std::vector< CIRCLE > Circles
A 4-sided polygon with opposite equal sides, used in representing shapes.
A metallic connection, used for transfer, between two points or pin.
std::string path
KIBIS_PIN * pin
SHAPE_CIRCLE circle(c.m_circle_center, c.m_circle_radius)
wxString result
Test unit parsing edge cases and error handling.
@ SCH_SYMBOL_T
Definition typeinfo.h:168
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
static PIECE rect(double aLeft, double aTop, double aRight, double aBottom, bool aMixed=false)
Definition of file extensions used in Kicad.