KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_easyedapro_parser.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) 2023 Alex Shvartzkop <[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, see <https://www.gnu.org/licenses/>.
19 */
20
23
24#include <core/map_helpers.h>
25
26#include <sch_io/sch_io_mgr.h>
27#include <schematic.h>
28#include <sch_sheet.h>
29#include <sch_line.h>
30#include <sch_bitmap.h>
31#include <sch_no_connect.h>
32#include <sch_label.h>
33#include <sch_junction.h>
34#include <sch_edit_frame.h>
35#include <sch_shape.h>
36#include <string_utils.h>
37#include <bezier_curves.h>
38#include <wx/base64.h>
39#include <wx/log.h>
40#include <wx/url.h>
41#include <wx/mstream.h>
42#include <gfx_import_utils.h>
46#include <trace_helpers.h>
47
48
50 PROGRESS_REPORTER* aProgressReporter )
51{
52 m_schematic = aSchematic;
53}
54
55
59
60
61double SCH_EASYEDAPRO_PARSER::Convert( wxString aValue )
62{
63 double value = 0;
64
65 if( !aValue.ToCDouble( &value ) )
66 THROW_IO_ERRORF( _( "Failed to parse value: '%s'" ), aValue );
67
68 return value;
69}
70
71
72double SCH_EASYEDAPRO_PARSER::SizeToKi( wxString aValue )
73{
74 return ScaleSize( Convert( aValue ) );
75}
76
77
78static LINE_STYLE ConvertStrokeStyle( int aStyle )
79{
80 if( aStyle == 0 )
81 return LINE_STYLE::SOLID;
82 else if( aStyle == 1 )
83 return LINE_STYLE::DASH;
84 else if( aStyle == 2 )
85 return LINE_STYLE::DOT;
86 else if( aStyle == 3 )
88
90}
91
92
93template <typename T>
94void SCH_EASYEDAPRO_PARSER::ApplyFontStyle( const std::map<wxString, nlohmann::json>& fontStyles,
95 T& text, const wxString& styleStr )
96{
97 auto it = fontStyles.find( styleStr );
98
99 if( it == fontStyles.end() )
100 return;
101
102 nlohmann::json style = it->second;
103
104 if( !style.is_array() )
105 return;
106
107 if( style.size() < 12 )
108 return;
109
110 if( style.at( 3 ).is_string() )
111 {
112 COLOR4D color( style.at( 3 ).get<wxString>() );
113 text->SetTextColor( color );
114 }
115
116 if( style.at( 4 ).is_string() )
117 {
118 wxString fontname = ( style.at( 4 ) );
119
120 // JLCEDA Pro V3 export to format version V1 specifies Arial explicitly instead of null for default font
121 if( fontname != wxS( "Arial" ) && !fontname.IsSameAs( wxS( "default" ), false ) )
122 text->SetFont( KIFONT::FONT::GetFont( fontname ) );
123 }
124
125 if( style.at( 5 ).is_number() )
126 {
127 double size = style.at( 5 ).get<double>() * 0.62;
128 text->SetTextSize( VECTOR2I( ScaleSize( size ), ScaleSize( size ) ) );
129 }
130
131 if( style.at( 10 ).is_number() )
132 {
133 int valign = style.at( 10 );
134
135 if( !text->GetText().Contains( wxS( "\n" ) ) )
136 {
137 if( valign == 0 )
138 text->SetVertJustify( GR_TEXT_V_ALIGN_TOP );
139 else if( valign == 1 )
140 text->SetVertJustify( GR_TEXT_V_ALIGN_CENTER );
141 else if( valign == 2 )
142 text->SetVertJustify( GR_TEXT_V_ALIGN_BOTTOM );
143 }
144 else
145 {
146 text->SetVertJustify( GR_TEXT_V_ALIGN_TOP );
147 // TODO: align by first line
148 }
149 }
150 else
151 {
152 text->SetVertJustify( GR_TEXT_V_ALIGN_BOTTOM );
153 }
154
155 if( style.at( 11 ).is_number() )
156 {
157 int halign = style.at( 11 );
158
159 if( halign == 0 )
160 text->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
161 else if( halign == 1 )
162 text->SetHorizJustify( GR_TEXT_H_ALIGN_CENTER );
163 else if( halign == 2 )
164 text->SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT );
165 }
166 else
167 {
168 text->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
169 }
170}
171
172
173template <typename T>
174void SCH_EASYEDAPRO_PARSER::ApplyLineStyle( const std::map<wxString, nlohmann::json>& lineStyles,
175 T& shape, const wxString& styleStr )
176{
177 auto it = lineStyles.find( styleStr );
178
179 if( it == lineStyles.end() )
180 return;
181
182 nlohmann::json style = it->second;
183
184 if( !style.is_array() )
185 return;
186
187 if( style.size() < 6 )
188 return;
189
190 STROKE_PARAMS stroke = shape->GetStroke();
191
192 if( style.at( 2 ).is_string() )
193 {
194 wxString colorStr = style.at( 2 ).get<wxString>();
195
196 if( !colorStr.empty() && colorStr.starts_with( wxS( "#" ) ) )
197 {
198 COLOR4D color( colorStr );
199 stroke.SetColor( color );
200 }
201 }
202
203 if( style.at( 3 ).is_number() )
204 {
205 int dashStyle = style.at( 3 );
206 stroke.SetLineStyle( ConvertStrokeStyle( dashStyle ) );
207 }
208
209 if( style.at( 5 ).is_number() )
210 {
211 double thickness = style.at( 5 );
212 stroke.SetWidth( ScaleSize( thickness ) );
213 }
214
215 shape->SetStroke( stroke );
216}
217
218
219template <typename T>
220void SCH_EASYEDAPRO_PARSER::ApplyAttrToField( const std::map<wxString, nlohmann::json>& fontStyles,
221 T* field, const EASYEDAPRO::SCH_ATTR& aAttr,
222 bool aIsSym, bool aToSym,
223 const std::map<wxString, wxString>& aDeviceAttributes,
224 SCH_SYMBOL* aParent )
225{
226 EDA_TEXT* text = static_cast<EDA_TEXT*>( field );
227
228 text->SetText( EASYEDAPRO::ResolveDeviceFieldVariables( aAttr.value, aDeviceAttributes ) );
229 text->SetVisible( aAttr.keyVisible || aAttr.valVisible );
230
231 field->SetNameShown( aAttr.keyVisible );
232
233 if( aAttr.position )
234 {
235 field->SetPosition( !aIsSym ? ScalePos( *aAttr.position )
236 : ScalePosSym( *aAttr.position ) );
237 }
238
239 ApplyFontStyle( fontStyles, text, aAttr.fontStyle );
240
241 auto parent = aParent;
242 if( parent && parent->Type() == SCH_SYMBOL_T )
243 {
244 int orient = static_cast<SCH_SYMBOL*>( parent )->GetOrientation();
245
246 if( orient == SYM_ORIENT_180 )
247 {
248 text->SetVertJustify( static_cast<GR_TEXT_V_ALIGN_T>( -text->GetVertJustify() ) );
249 text->SetHorizJustify( static_cast<GR_TEXT_H_ALIGN_T>( -text->GetHorizJustify() ) );
250 }
251 else if( orient == SYM_MIRROR_X + SYM_ORIENT_0 )
252 {
253 text->SetVertJustify( static_cast<GR_TEXT_V_ALIGN_T>( -text->GetVertJustify() ) );
254 }
255 else if( orient == SYM_MIRROR_Y + SYM_ORIENT_0 )
256 {
257 text->SetHorizJustify( static_cast<GR_TEXT_H_ALIGN_T>( -text->GetHorizJustify() ) );
258 }
259 else if( orient == SYM_MIRROR_Y + SYM_ORIENT_180 )
260 {
261 text->SetHorizJustify( static_cast<GR_TEXT_H_ALIGN_T>( text->GetHorizJustify() ) );
262 }
263 else if( orient == SYM_ORIENT_90 )
264 {
265 text->SetTextAngle( ANGLE_VERTICAL );
266 text->SetVertJustify( static_cast<GR_TEXT_V_ALIGN_T>( -text->GetVertJustify() ) );
267 text->SetHorizJustify( static_cast<GR_TEXT_H_ALIGN_T>( -text->GetHorizJustify() ) );
268 }
269 if( orient == SYM_ORIENT_270 )
270 {
271 text->SetTextAngle( ANGLE_VERTICAL );
272 }
273 else if( orient == SYM_MIRROR_X + SYM_ORIENT_90 )
274 {
275 text->SetTextAngle( ANGLE_VERTICAL );
276 text->SetVertJustify( static_cast<GR_TEXT_V_ALIGN_T>( -text->GetVertJustify() ) );
277 text->SetHorizJustify( static_cast<GR_TEXT_H_ALIGN_T>( -text->GetHorizJustify() ) );
278 }
279 else if( orient == SYM_MIRROR_X + SYM_ORIENT_270 )
280 {
281 text->SetTextAngle( ANGLE_VERTICAL );
282 text->SetVertJustify( static_cast<GR_TEXT_V_ALIGN_T>( -text->GetVertJustify() ) );
283 }
284 else if( orient == SYM_MIRROR_Y + SYM_ORIENT_90 )
285 {
286 text->SetTextAngle( ANGLE_VERTICAL );
287 text->SetHorizJustify( static_cast<GR_TEXT_H_ALIGN_T>( -text->GetHorizJustify() ) );
288 }
289 else if( orient == SYM_MIRROR_Y + SYM_ORIENT_270 )
290 {
291 text->SetHorizJustify( static_cast<GR_TEXT_H_ALIGN_T>( text->GetHorizJustify() ) );
292 }
293
294 if( aAttr.rotation == 90 )
295 {
296 if( text->GetTextAngle() == ANGLE_HORIZONTAL )
297 text->SetTextAngle( ANGLE_VERTICAL );
298 else
299 text->SetTextAngle( ANGLE_HORIZONTAL );
300
301 if( orient == SYM_ORIENT_90 )
302 {
303 text->SetVertJustify( static_cast<GR_TEXT_V_ALIGN_T>( -text->GetVertJustify() ) );
304 text->SetHorizJustify( static_cast<GR_TEXT_H_ALIGN_T>( -text->GetHorizJustify() ) );
305 }
306 if( orient == SYM_ORIENT_270 )
307 {
308 text->SetVertJustify( static_cast<GR_TEXT_V_ALIGN_T>( -text->GetVertJustify() ) );
309 text->SetHorizJustify( static_cast<GR_TEXT_H_ALIGN_T>( -text->GetHorizJustify() ) );
310 }
311 else if( orient == SYM_MIRROR_X + SYM_ORIENT_90 )
312 {
313 text->SetVertJustify( static_cast<GR_TEXT_V_ALIGN_T>( -text->GetVertJustify() ) );
314 }
315 }
316 }
317}
318
319
321SCH_EASYEDAPRO_PARSER::ParseSymbol( const std::vector<nlohmann::json>& aLines,
322 const std::map<wxString, wxString>& aDeviceAttributes )
323{
324 EASYEDAPRO::SYM_INFO symInfo;
325
326 std::unique_ptr<LIB_SYMBOL> ksymbolPtr = std::make_unique<LIB_SYMBOL>( wxEmptyString );
327 LIB_SYMBOL* ksymbol = ksymbolPtr.get();
328
329 std::map<wxString, nlohmann::json> lineStyles;
330 std::map<wxString, nlohmann::json> fontStyles;
331 std::map<wxString, int> partUnits;
332
333 std::map<int, std::map<wxString, EASYEDAPRO::SCH_ATTR>> unitAttributes;
334 std::map<int, std::map<wxString, std::vector<nlohmann::json>>> unitParentedLines;
335
336 int totalUnits = 0;
337
338 for( const nlohmann::json& line : aLines )
339 {
340 wxString type = line.at( 0 );
341
342 if( type == wxS( "LINESTYLE" ) )
343 lineStyles[line.at( 1 )] = line;
344 else if( type == wxS( "FONTSTYLE" ) )
345 fontStyles[line.at( 1 )] = line;
346 else if( type == wxS( "PART" ) )
347 partUnits[line.at( 1 )] = ++totalUnits;
348 }
349
350 if( totalUnits == 0 )
351 totalUnits = 1;
352
353 symInfo.partUnits = partUnits;
354 ksymbol->SetUnitCount( totalUnits, false );
355
356 int currentUnit = 1;
357
358 for( const nlohmann::json& line : aLines )
359 {
360 wxString type = line.at( 0 );
361
362 if( type == wxS( "PART" ) )
363 {
364 currentUnit = partUnits.at( line.at( 1 ) );
365 }
366 else if( type == wxS( "RECT" ) )
367 {
368 VECTOR2D start( line.at( 2 ), line.at( 3 ) );
369 VECTOR2D end( line.at( 4 ), line.at( 5 ) );
370 wxString styleStr = line.at( 9 );
371
372 auto rect = std::make_unique<SCH_SHAPE>( SHAPE_T::RECTANGLE, LAYER_DEVICE );
373
374 rect->SetStart( ScalePosSym( start ) );
375 rect->SetEnd( ScalePosSym( end ) );
376
377 rect->SetUnit( currentUnit );
378 ApplyLineStyle( lineStyles, rect, styleStr );
379
380 ksymbol->AddDrawItem( rect.release() );
381 }
382 else if( type == wxS( "CIRCLE" ) )
383 {
384 VECTOR2D center( line.at( 2 ), line.at( 3 ) );
385 double radius = line.at( 4 );
386 wxString styleStr = line.at( 5 );
387
388 auto circle = std::make_unique<SCH_SHAPE>( SHAPE_T::CIRCLE, LAYER_DEVICE );
389
390 circle->SetCenter( ScalePosSym( center ) );
391 circle->SetEnd( circle->GetCenter() + VECTOR2I( ScaleSize( radius ), 0 ) );
392
393 circle->SetUnit( currentUnit );
394 ApplyLineStyle( lineStyles, circle, styleStr );
395
396 ksymbol->AddDrawItem( circle.release() );
397 }
398 else if( type == wxS( "ARC" ) )
399 {
400 VECTOR2D start( line.at( 2 ), line.at( 3 ) );
401 VECTOR2D mid( line.at( 4 ), line.at( 5 ) );
402 VECTOR2D end( line.at( 6 ), line.at( 7 ) );
403 wxString styleStr = line.at( 8 );
404
405 VECTOR2D kstart = ScalePosSym( start );
406 VECTOR2D kmid = ScalePosSym( mid );
407 VECTOR2D kend = ScalePosSym( end );
408
409 auto shape = std::make_unique<SCH_SHAPE>( SHAPE_T::ARC, LAYER_DEVICE );
410
411 shape->SetArcOrLine( kstart, kmid, kend );
412
413 shape->SetUnit( currentUnit );
414 ApplyLineStyle( lineStyles, shape, styleStr );
415
416 ksymbol->AddDrawItem( shape.release() );
417 }
418 else if( type == wxS( "BEZIER" ) )
419 {
420 std::vector<double> points = line.at( 2 );
421 wxString styleStr = line.at( 3 );
422
423 std::unique_ptr<SCH_SHAPE> shape =
424 std::make_unique<SCH_SHAPE>( SHAPE_T::BEZIER, LAYER_DEVICE );
425
426 for( size_t i = 1; i < points.size(); i += 2 )
427 {
428 VECTOR2I pt = ScalePosSym( VECTOR2D( points[i - 1], points[i] ) );
429
430 switch( i )
431 {
432 case 1: shape->SetStart( pt ); break;
433 case 3: shape->SetBezierC1( pt ); break;
434 case 5: shape->SetBezierC2( pt ); break;
435 case 7: shape->SetEnd( pt ); break;
436 }
437 }
438
439 shape->SetUnit( currentUnit );
440 ApplyLineStyle( lineStyles, shape, styleStr );
441
442 ksymbol->AddDrawItem( shape.release() );
443 }
444 else if( type == wxS( "POLY" ) )
445 {
446 std::vector<double> points = line.at( 2 );
447 wxString styleStr = line.at( 4 );
448
449 auto shape = std::make_unique<SCH_SHAPE>( SHAPE_T::POLY, LAYER_DEVICE );
450
451 for( size_t i = 1; i < points.size(); i += 2 )
452 shape->AddPoint( ScalePosSym( VECTOR2D( points[i - 1], points[i] ) ) );
453
454 shape->SetUnit( currentUnit );
455 ApplyLineStyle( lineStyles, shape, styleStr );
456
457 ksymbol->AddDrawItem( shape.release() );
458 }
459 else if( type == wxS( "TEXT" ) )
460 {
461 // an unplaced text has a null position and nothing to draw
462 if( !line.at( 2 ).is_number() || !line.at( 3 ).is_number() )
463 continue;
464
465 VECTOR2D pos( line.at( 2 ), line.at( 3 ) );
466 double angle = line.at( 4 ).is_number() ? line.at( 4 ).get<double>() : 0.0;
467 wxString textStr = line.at( 5 );
468 wxString fontStyleStr = line.at( 6 );
469
470 auto text = std::make_unique<SCH_TEXT>( ScalePosSym( pos ), UnescapeHTML( textStr ),
471 LAYER_DEVICE );
472
473 text->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
474 text->SetVertJustify( GR_TEXT_V_ALIGN_TOP );
475 text->SetTextAngleDegrees( angle );
476
477 text->SetUnit( currentUnit );
478 ApplyFontStyle( fontStyles, text, fontStyleStr );
479
480 ksymbol->AddDrawItem( text.release() );
481 }
482 else if( type == wxS( "OBJ" ) )
483 {
484 VECTOR2D start, size;
485 wxString mimeType, data;
486 //double angle = 0;
487 int upsideDown = 0;
488
489 if( line.at( 3 ).is_number() )
490 {
491 start = VECTOR2D( line.at( 3 ), line.at( 4 ) );
492 size = VECTOR2D( line.at( 5 ), line.at( 6 ) );
493 //angle = line.at( 7 );
494 upsideDown = line.at( 8 );
495
496 wxString imageUrl = line.at( 9 );
497
498 if( imageUrl.BeforeFirst( ':' ) == wxS( "data" ) )
499 {
500 wxArrayString paramsArr =
501 wxSplit( imageUrl.AfterFirst( ':' ).BeforeFirst( ',' ), ';', '\0' );
502
503 data = imageUrl.AfterFirst( ',' );
504
505 if( paramsArr.size() > 0 )
506 {
507 mimeType = paramsArr[0];
508 }
509 }
510 }
511 else if( line.at( 3 ).is_string() )
512 {
513 mimeType = line.at( 3 ).get<wxString>().BeforeFirst( ';' );
514
515 start = VECTOR2D( line.at( 4 ), line.at( 5 ) );
516 size = VECTOR2D( line.at( 6 ), line.at( 7 ) );
517 //angle = line.at( 8 );
518 data = line.at( 9 ).get<wxString>();
519 }
520
521 if( mimeType.empty() || data.empty() )
522 continue;
523
524 wxMemoryBuffer buf = wxBase64Decode( data );
525
526 if( mimeType == wxS( "image/svg+xml" ) )
527 {
528 VECTOR2D offset = ScalePosSym( start );
529
530 SVG_IMPORT_PLUGIN svgImportPlugin;
531 GRAPHICS_IMPORTER_LIB_SYMBOL libsymImporter( ksymbol, 0 );
532
533 svgImportPlugin.SetImporter( &libsymImporter );
534 svgImportPlugin.LoadFromMemory( buf );
535
536 VECTOR2D imSize( svgImportPlugin.GetImageWidth(),
537 svgImportPlugin.GetImageHeight() );
538
539 VECTOR2D pixelScale( schIUScale.IUTomm( ScaleSize( size.x ) ) / imSize.x,
540 schIUScale.IUTomm( ScaleSize( size.y ) ) / imSize.y );
541
542 if( upsideDown )
543 pixelScale.y *= -1;
544
545 libsymImporter.SetScale( pixelScale );
546
547 VECTOR2D offsetMM( schIUScale.IUTomm( offset.x ), schIUScale.IUTomm( offset.y ) );
548
549 libsymImporter.SetImportOffsetMM( offsetMM );
550
551 svgImportPlugin.Import();
552
553 // TODO: rotation
554 for( std::unique_ptr<EDA_ITEM>& item : libsymImporter.GetItems() )
555 ksymbol->AddDrawItem( static_cast<SCH_ITEM*>( item.release() ) );
556 }
557 else
558 {
559 wxMemoryInputStream memis( buf.GetData(), buf.GetDataLen() );
560
561 wxImage::SetDefaultLoadFlags( wxImage::GetDefaultLoadFlags()
562 & ~wxImage::Load_Verbose );
563 wxImage img;
564 if( img.LoadFile( memis, mimeType ) )
565 {
566 int dimMul = img.GetWidth() * img.GetHeight();
567 double maxPixels = 30000;
568
569 if( dimMul > maxPixels )
570 {
571 double scale = sqrt( maxPixels / dimMul );
572 img.Rescale( img.GetWidth() * scale, img.GetHeight() * scale );
573 }
574
575 VECTOR2D pixelScale( ScaleSize( size.x ) / img.GetWidth(),
576 ScaleSize( size.y ) / img.GetHeight() );
577
578 // TODO: rotation
579 ConvertImageToLibShapes( ksymbol, 0, img, pixelScale, ScalePosSym( start ) );
580 }
581 }
582 }
583 else if( type == wxS( "HEAD" ) )
584 {
585 symInfo.head = line;
586 }
587 else if( type == wxS( "PIN" ) )
588 {
589 wxString pinId = line.at( 1 );
590 unitParentedLines[currentUnit][pinId].push_back( line );
591 }
592 else if( type == wxS( "ATTR" ) )
593 {
594 wxString parentId = line.at( 2 );
595
596 if( parentId.empty() )
597 {
598 EASYEDAPRO::SCH_ATTR attr = line;
599 unitAttributes[currentUnit].emplace( attr.key, attr );
600 }
601 else
602 {
603 unitParentedLines[currentUnit][parentId].push_back( line );
604 }
605 }
606 }
607
610 {
611 ksymbol->SetGlobalPower();
612 ksymbol->GetReferenceField().SetText( wxS( "#PWR" ) );
613 ksymbol->GetReferenceField().SetVisible( false );
614 ksymbol->SetKeyWords( wxS( "power-flag" ) );
615 ksymbol->SetShowPinNames( false );
616 ksymbol->SetShowPinNumbers( false );
617
618 if( auto globalNetAttr = get_opt( unitAttributes[1], wxS( "Global Net Name" ) ) )
619 {
620 ApplyAttrToField( fontStyles, &ksymbol->GetValueField(), *globalNetAttr, true, true );
621
622 wxString globalNetname = globalNetAttr->value;
623
624 if( !globalNetname.empty() )
625 {
626 ksymbol->SetDescription( wxString::Format(
627 _( "Power symbol creates a global label with name '%s'" ),
628 globalNetname ) );
629 }
630 }
631 }
632 else
633 {
634 auto designatorAttr = get_opt( unitAttributes[1], wxS( "Designator" ) );
635
636 if( designatorAttr && !designatorAttr->value.empty() )
637 {
638 wxString symbolPrefix = designatorAttr->value;
639
640 if( symbolPrefix.EndsWith( wxS( "?" ) ) )
641 symbolPrefix.RemoveLast();
642
643 ksymbol->GetReferenceField().SetText( symbolPrefix );
644 }
645
647 aDeviceAttributes, true,
648 [&]( const wxString& attrName, const wxString& value )
649 {
650 SCH_FIELD* fd = ksymbol->FindFieldCaseInsensitive( attrName );
651
652 if( !fd )
653 {
654 fd = new SCH_FIELD( ksymbol, FIELD_T::USER, attrName );
655 ksymbol->AddField( fd );
656 }
657
658 fd->SetText( value );
659 fd->SetVisible( false );
660 } );
661 }
662
663 for( auto& [unitId, parentedLines] : unitParentedLines )
664 {
665 for( auto& [pinId, lines] : parentedLines )
666 {
667 std::optional<EASYEDAPRO::SYM_PIN> epin;
668 std::map<wxString, EASYEDAPRO::SCH_ATTR> pinAttributes;
669
670 for( const nlohmann::json& line : lines )
671 {
672 wxString type = line.at( 0 );
673
674 if( type == wxS( "ATTR" ) )
675 {
676 EASYEDAPRO::SCH_ATTR attr = line;
677 pinAttributes.emplace( attr.key, attr );
678 }
679 else if( type == wxS( "PIN" ) )
680 {
681 epin = line;
682 }
683 }
684
685 if( !epin )
686 continue;
687
688 EASYEDAPRO::PIN_INFO pinInfo;
689 pinInfo.pin = *epin;
690
691 std::unique_ptr<SCH_PIN> pin = std::make_unique<SCH_PIN>( ksymbol );
692
693 pin->SetUnit( unitId );
694
695 pin->SetLength( ScaleSize( epin->length ) );
696 pin->SetPosition( ScalePosSym( epin->position ) );
697
699
700 if( epin->rotation == 0 )
702 if( epin->rotation == 90 )
704 if( epin->rotation == 180 )
706 if( epin->rotation == 270 )
708
709 pin->SetOrientation( orient );
710
712 {
713 pin->SetName( ksymbol->GetName() );
714 //pin->SetVisible( false );
715 }
716 else
717 {
718 auto pinNameAttr = get_opt( pinAttributes, "Pin Name" ); // JLCEDA V3
719
720 if( !pinNameAttr )
721 pinNameAttr = get_opt( pinAttributes, "NAME" ); // EasyEDA V2
722
723 if( pinNameAttr )
724 {
725 pin->SetName( pinNameAttr->value );
726 pinInfo.name = pinNameAttr->value;
727
728 if( !pinNameAttr->valVisible )
729 pin->SetNameTextSize( schIUScale.MilsToIU( 1 ) );
730 }
731 }
732
733 auto pinNumAttr = get_opt( pinAttributes, "Pin Number" ); // JLCEDA V3
734
735 if( !pinNumAttr )
736 pinNumAttr = get_opt( pinAttributes, "NUMBER" ); // EasyEDA V2
737
738 if( pinNumAttr )
739 {
740 pin->SetNumber( pinNumAttr->value );
741 pinInfo.number = pinNumAttr->value;
742
743 if( !pinNumAttr->valVisible )
744 pin->SetNumberTextSize( schIUScale.MilsToIU( 1 ) );
745 }
746
748 {
750 }
751 else if( auto pinTypeAttr = get_opt( pinAttributes, "Pin Type" ) )
752 {
753 if( pinTypeAttr->value == wxS( "IN" ) )
755 if( pinTypeAttr->value == wxS( "OUT" ) )
757 if( pinTypeAttr->value == wxS( "BI" ) )
759 }
760
761 if( get_opt( pinAttributes, "NO_CONNECT" ) )
762 pin->SetType( ELECTRICAL_PINTYPE::PT_NC );
763
764 if( pin->GetNumberTextSize() * int( pin->GetNumber().size() ) > pin->GetLength() )
765 pin->SetNumberTextSize( pin->GetLength() / pin->GetNumber().size() );
766
767 symInfo.pins.push_back( pinInfo );
768 ksymbol->AddDrawItem( pin.release() );
769 }
770 }
771
772 symInfo.symbolAttr = get_opt( unitAttributes[1], "Symbol" ); // TODO: per-unit
773
774 /*BOX2I bbox = ksymbol->GetBodyBoundingBox( 0, 0, true, true );
775 bbox.Inflate( schIUScale.MilsToIU( 10 ) );*/
776
777 /*ksymbol->GetReferenceField().SetVertJustify( GR_TEXT_V_ALIGN_BOTTOM );
778 ksymbol->GetReferenceField().SetHorizJustify( GR_TEXT_H_ALIGN_CENTER );
779 ksymbol->GetReferenceField().SetPosition( VECTOR2I( bbox.GetCenter().x, -bbox.GetTop() ) );
780
781 ksymbol->GetValueField().SetVertJustify( GR_TEXT_V_ALIGN_TOP );
782 ksymbol->GetValueField().SetHorizJustify( GR_TEXT_H_ALIGN_CENTER );
783 ksymbol->GetValueField().SetPosition( VECTOR2I( bbox.GetCenter().x, -bbox.GetBottom() ) );*/
784
785 symInfo.libSymbol = std::move( ksymbolPtr );
786
787 return symInfo;
788}
789
790
792 const nlohmann::json& aProject,
793 std::map<wxString, EASYEDAPRO::SYM_INFO>& aSymbolMap,
794 const std::map<wxString, EASYEDAPRO::BLOB>& aBlobMap,
795 const std::vector<nlohmann::json>& aLines,
796 const wxString& aLibName )
797{
798 std::vector<std::unique_ptr<SCH_ITEM>> createdItems;
799
800 std::map<wxString, std::vector<nlohmann::json>> parentedLines;
801 std::map<wxString, std::vector<nlohmann::json>> ruleLines;
802
803 std::map<wxString, nlohmann::json> lineStyles;
804 std::map<wxString, nlohmann::json> fontStyles;
805
806 for( const nlohmann::json& line : aLines )
807 {
808 wxString type = line.at( 0 );
809
810 if( type == wxS( "LINESTYLE" ) )
811 lineStyles[line.at( 1 )] = line;
812 else if( type == wxS( "FONTSTYLE" ) )
813 fontStyles[line.at( 1 )] = line;
814 }
815
816 for( const nlohmann::json& line : aLines )
817 {
818 wxString type = line.at( 0 );
819
820 if( type == wxS( "RECT" ) )
821 {
822 VECTOR2D start( line.at( 2 ), line.at( 3 ) );
823 VECTOR2D end( line.at( 4 ), line.at( 5 ) );
824 wxString styleStr = line.at( 9 );
825
826 std::unique_ptr<SCH_SHAPE> rect = std::make_unique<SCH_SHAPE>( SHAPE_T::RECTANGLE );
827
828 rect->SetStart( ScalePos( start ) );
829 rect->SetEnd( ScalePos( end ) );
830
831 ApplyLineStyle( lineStyles, rect, styleStr );
832
833 createdItems.push_back( std::move( rect ) );
834 }
835 else if( type == wxS( "CIRCLE" ) )
836 {
837 VECTOR2D center( line.at( 2 ), line.at( 3 ) );
838 double radius = line.at( 4 );
839 wxString styleStr = line.at( 5 );
840
841 std::unique_ptr<SCH_SHAPE> circle = std::make_unique<SCH_SHAPE>( SHAPE_T::CIRCLE );
842
843 circle->SetCenter( ScalePos( center ) );
844 circle->SetEnd( circle->GetCenter() + VECTOR2I( ScaleSize( radius ), 0 ) );
845
846 ApplyLineStyle( lineStyles, circle, styleStr );
847
848 createdItems.push_back( std::move( circle ) );
849 }
850 else if( type == wxS( "POLY" ) )
851 {
852 std::vector<double> points = line.at( 2 );
853 wxString styleStr = line.at( 4 );
854
856
857 for( size_t i = 1; i < points.size(); i += 2 )
858 chain.Append( ScalePos( VECTOR2D( points[i - 1], points[i] ) ) );
859
860 for( int segId = 0; segId < chain.SegmentCount(); segId++ )
861 {
862 const SEG& seg = chain.CSegment( segId );
863
864 std::unique_ptr<SCH_LINE> schLine =
865 std::make_unique<SCH_LINE>( seg.A, LAYER_NOTES );
866 schLine->SetEndPoint( seg.B );
867
868 ApplyLineStyle( lineStyles, schLine, styleStr );
869
870 createdItems.push_back( std::move( schLine ) );
871 }
872 }
873 else if( type == wxS( "TEXT" ) )
874 {
875 // an unplaced text has a null position and nothing to draw
876 if( !line.at( 2 ).is_number() || !line.at( 3 ).is_number() )
877 continue;
878
879 VECTOR2D pos( line.at( 2 ), line.at( 3 ) );
880 double angle = line.at( 4 ).is_number() ? line.at( 4 ).get<double>() : 0.0;
881 wxString textStr = line.at( 5 );
882 wxString fontStyleStr = line.at( 6 );
883
884 std::unique_ptr<SCH_TEXT> text =
885 std::make_unique<SCH_TEXT>( ScalePos( pos ), UnescapeHTML( textStr ) );
886
887 text->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
888 text->SetVertJustify( GR_TEXT_V_ALIGN_TOP );
889
890 text->SetTextAngleDegrees( angle );
891
892 ApplyFontStyle( fontStyles, text, fontStyleStr );
893
894 createdItems.push_back( std::move( text ) );
895 }
896 else if( type == wxS( "OBJ" ) )
897 {
898 VECTOR2D start, size;
899 wxString mimeType, base64Data;
900 double angle = 0;
901 int flipped = 0;
902
903 if( line.at( 3 ).is_number() )
904 {
905 start = VECTOR2D( line.at( 3 ), line.at( 4 ) );
906 size = VECTOR2D( line.at( 5 ), line.at( 6 ) );
907 angle = line.at( 7 );
908 flipped = line.at( 8 );
909
910 wxString imageUrl = line.at( 9 );
911
912 if( imageUrl.BeforeFirst( ':' ) == wxS( "data" ) )
913 {
914 wxArrayString paramsArr =
915 wxSplit( imageUrl.AfterFirst( ':' ).BeforeFirst( ',' ), ';', '\0' );
916
917 base64Data = imageUrl.AfterFirst( ',' );
918
919 if( paramsArr.size() > 0 )
920 mimeType = paramsArr[0];
921 }
922 else if( imageUrl.BeforeFirst( ':' ) == wxS( "blob" ) )
923 {
924 wxString objectId = imageUrl.AfterLast( ':' );
925
926 if( auto blob = get_opt( aBlobMap, objectId ) )
927 {
928 wxString blobUrl = blob->url;
929
930 if( blobUrl.BeforeFirst( ':' ) == wxS( "data" ) )
931 {
932 wxArrayString paramsArr = wxSplit(
933 blobUrl.AfterFirst( ':' ).BeforeFirst( ',' ), ';', '\0' );
934
935 base64Data = blobUrl.AfterFirst( ',' );
936
937 if( paramsArr.size() > 0 )
938 mimeType = paramsArr[0];
939 }
940 }
941 }
942 }
943 else if( line.at( 3 ).is_string() )
944 {
945 mimeType = line.at( 3 ).get<wxString>().BeforeFirst( ';' );
946
947 start = VECTOR2D( line.at( 4 ), line.at( 5 ) );
948 size = VECTOR2D( line.at( 6 ), line.at( 7 ) );
949 angle = line.at( 8 );
950 base64Data = line.at( 9 ).get<wxString>();
951 }
952
953 VECTOR2D kstart = ScalePos( start );
954 VECTOR2D ksize = ScaleSize( size );
955
956 if( mimeType.empty() || base64Data.empty() )
957 continue;
958
959 wxMemoryBuffer buf = wxBase64Decode( base64Data );
960
961 if( mimeType == wxS( "image/svg+xml" ) )
962 {
963 SVG_IMPORT_PLUGIN svgImportPlugin;
964 GRAPHICS_IMPORTER_SCH schImporter;
965
966 svgImportPlugin.SetImporter( &schImporter );
967 svgImportPlugin.LoadFromMemory( buf );
968
969 VECTOR2D imSize( svgImportPlugin.GetImageWidth(),
970 svgImportPlugin.GetImageHeight() );
971
972 VECTOR2D pixelScale( schIUScale.IUTomm( ScaleSize( size.x ) ) / imSize.x,
973 schIUScale.IUTomm( ScaleSize( size.y ) ) / imSize.y );
974
975 schImporter.SetScale( pixelScale );
976
977 VECTOR2D offsetMM( schIUScale.IUTomm( kstart.x ), schIUScale.IUTomm( kstart.y ) );
978
979 schImporter.SetImportOffsetMM( offsetMM );
980
981 svgImportPlugin.Import();
982
983 for( std::unique_ptr<EDA_ITEM>& item : schImporter.GetItems() )
984 {
985 SCH_ITEM* schItem = static_cast<SCH_ITEM*>( item.release() );
986
987 for( double i = angle; i > 0; i -= 90 )
988 {
989 if( schItem->Type() == SCH_LINE_T )
990 {
991 // Lines need special handling for some reason
992 schItem->SetFlags( STARTPOINT );
993 schItem->Rotate( kstart, false );
994 schItem->ClearFlags( STARTPOINT );
995
996 schItem->SetFlags( ENDPOINT );
997 schItem->Rotate( kstart, false );
998 schItem->ClearFlags( ENDPOINT );
999 }
1000 else
1001 {
1002 schItem->Rotate( kstart, false );
1003 }
1004 }
1005
1006 if( flipped )
1007 {
1008 // Lines need special handling for some reason
1009 if( schItem->Type() == SCH_LINE_T )
1010 schItem->SetFlags( STARTPOINT | ENDPOINT );
1011
1012 schItem->MirrorHorizontally( kstart.x );
1013
1014 if( schItem->Type() == SCH_LINE_T )
1015 schItem->ClearFlags( STARTPOINT | ENDPOINT );
1016 }
1017
1018 createdItems.emplace_back( schItem );
1019 }
1020 }
1021 else
1022 {
1023 std::unique_ptr<SCH_BITMAP> bitmap = std::make_unique<SCH_BITMAP>();
1024 REFERENCE_IMAGE& refImage = bitmap->GetReferenceImage();
1025
1026 wxImage::SetDefaultLoadFlags( wxImage::GetDefaultLoadFlags()
1027 & ~wxImage::Load_Verbose );
1028
1029 if( refImage.ReadImageFile( buf ) )
1030 {
1031 VECTOR2D kcenter = kstart + ksize / 2;
1032
1033 double scaleFactor = ScaleSize( size.x ) / refImage.GetSize().x;
1034 refImage.SetImageScale( scaleFactor );
1035 bitmap->SetPosition( kcenter );
1036
1037 for( double i = angle; i > 0; i -= 90 )
1038 bitmap->Rotate( kstart, false );
1039
1040 if( flipped )
1041 bitmap->MirrorHorizontally( kstart.x );
1042
1043 createdItems.push_back( std::move( bitmap ) );
1044 }
1045 }
1046 }
1047 if( type == wxS( "WIRE" ) )
1048 {
1049 wxString wireId = line.at( 1 );
1050 parentedLines[wireId].push_back( line );
1051 }
1052 else if( type == wxS( "COMPONENT" ) )
1053 {
1054 wxString compId = line.at( 1 );
1055 parentedLines[compId].push_back( line );
1056 }
1057 else if( type == wxS( "ATTR" ) )
1058 {
1059 wxString compId = line.at( 2 );
1060 parentedLines[compId].push_back( line );
1061 }
1062 }
1063
1064 for( auto& [parentId, lines] : parentedLines )
1065 {
1066 std::optional<EASYEDAPRO::SCH_COMPONENT> component;
1067 std::optional<EASYEDAPRO::SCH_WIRE> wire;
1068 std::map<wxString, EASYEDAPRO::SCH_ATTR> attributes;
1069
1070 for( const nlohmann::json& line : lines )
1071 {
1072 if( line.at( 0 ) == "COMPONENT" )
1073 {
1074 component = line;
1075 }
1076 else if( line.at( 0 ) == "WIRE" )
1077 {
1078 wire = line;
1079 }
1080 else if( line.at( 0 ) == "ATTR" )
1081 {
1082 EASYEDAPRO::SCH_ATTR attr = line;
1083 attributes.emplace( attr.key, attr );
1084 }
1085 }
1086
1087 if( component )
1088 {
1089 auto deviceAttr = get_opt( attributes, "Device" );
1090 auto symbolAttr = get_opt( attributes, "Symbol" );
1091
1092 if( !deviceAttr )
1093 continue;
1094
1095 const std::map<wxString, wxString> prjCompAttrs = EASYEDAPRO::AnyMapToStringMap(
1096 aProject.at( "devices" ).at( deviceAttr->value ).at( "attributes" ) );
1097
1098 // Merge attributes, giving priority to schematic attributes over project attributes
1099 std::map<wxString, wxString> mergedAttrValues;
1100
1101 for( const auto& [key, value] : prjCompAttrs )
1102 mergedAttrValues[key] = value;
1103
1104 for( const auto& [key, attr] : attributes )
1105 mergedAttrValues[key] = attr.value;
1106
1107 wxString symbolId;
1108
1109 if( symbolAttr && !symbolAttr->value.IsEmpty() )
1110 symbolId = symbolAttr->value;
1111 else
1112 symbolId = prjCompAttrs.at( "Symbol" );
1113
1114 auto it = aSymbolMap.find( symbolId );
1115 if( it == aSymbolMap.end() )
1116 {
1117 wxLogTrace( traceEasyEdaIo, "Symbol of '%s' with uuid '%s' not found.", component->name, symbolId );
1118 continue;
1119 }
1120
1121 EASYEDAPRO::SYM_INFO& esymInfo = it->second;
1122 LIB_SYMBOL newLibSymbol = *esymInfo.libSymbol.get();
1123
1124 wxString unitName = component->name;
1125
1126 LIB_ID libId = EASYEDAPRO::ToKiCadLibID( aLibName,
1127 newLibSymbol.GetLibId().GetLibItemName() );
1128
1129 auto schSym = std::make_unique<SCH_SYMBOL>( newLibSymbol, libId,
1130 &aSchematic->CurrentSheet(),
1131 esymInfo.partUnits[unitName] );
1132
1133 schSym->SetFootprintFieldText( newLibSymbol.GetFootprint() );
1134
1135 for( double i = component->rotation; i > 0; i -= 90 )
1136 schSym->Rotate( VECTOR2I(), true );
1137
1138 if( component->mirror )
1139 schSym->MirrorHorizontally( 0 );
1140
1141 schSym->SetPosition( ScalePos( component->position ) );
1142
1144 {
1145 SCH_FIELD* valueField = schSym->GetField( FIELD_T::VALUE );
1146
1147 auto globalNetNameAttr = get_opt( attributes, "Global Net Name" );
1148 wxString globalNetNameFromProject = get_def( prjCompAttrs, "Global Net Name", wxEmptyString );
1149 wxString globalNetName;
1150
1151 // 1. Pick from schematic attr
1152 // 2. Pick from project.json
1153 // 3. Pick from symbol
1154 if( globalNetNameAttr && !globalNetNameAttr->value.IsEmpty() )
1155 {
1156 globalNetName = globalNetNameAttr->value;
1157
1158 ApplyAttrToField( fontStyles, schSym->GetField( FIELD_T::VALUE ), *globalNetNameAttr, false, true,
1159 mergedAttrValues, schSym.get() );
1160 }
1161 else if( !globalNetNameFromProject.IsEmpty() )
1162 {
1163 globalNetName = globalNetNameFromProject;
1164
1165 valueField->SetText( EASYEDAPRO::ResolveDeviceFieldVariables( globalNetName, mergedAttrValues ) );
1166 }
1167 else
1168 {
1169 valueField->SetText( newLibSymbol.GetValueField().GetText() );
1170 }
1171
1172 for( SCH_PIN* pin : schSym->GetAllLibPins() )
1173 pin->SetName( globalNetName );
1174
1175 schSym->SetRef( &aSchematic->CurrentSheet(), wxS( "#PWR?" ) );
1176 schSym->GetField( FIELD_T::REFERENCE )->SetVisible( false );
1177 }
1178 else if( esymInfo.head.symbolType == EASYEDAPRO::SYMBOL_TYPE::NETPORT )
1179 {
1180 auto nameAttr = get_opt( attributes, "Name" );
1181
1182 wxString netName;
1183
1184 if( nameAttr && !nameAttr->value.IsEmpty() )
1185 netName = nameAttr->value;
1186 else
1187 netName = prjCompAttrs.at( "Name" );
1188
1189 std::unique_ptr<SCH_GLOBALLABEL> label = std::make_unique<SCH_GLOBALLABEL>(
1190 ScalePos( component->position ), netName );
1191
1192 std::vector<SCH_PIN*> pins = schSym->GetPins( &aSchematic->CurrentSheet() );
1193
1194 if( pins.size() > 0 )
1195 {
1196 switch( pins[0]->GetType() )
1197 {
1199 label->SetShape( LABEL_FLAG_SHAPE::L_INPUT );
1200 break;
1202 label->SetShape( LABEL_FLAG_SHAPE::L_OUTPUT );
1203 break;
1205 label->SetShape( LABEL_FLAG_SHAPE::L_BIDI );
1206 break;
1207 default:
1208 break;
1209 }
1210 }
1211
1212 BOX2I bbox = schSym->GetBodyAndPinsBoundingBox();
1213 bbox.Offset( -schSym->GetPosition() );
1214 VECTOR2I bboxCenter = bbox.GetCenter();
1215
1217
1218 if( std::abs( bboxCenter.x ) >= std::abs( bboxCenter.y ) )
1219 {
1220 if( bboxCenter.x >= 0 )
1221 spin = SPIN_STYLE::RIGHT;
1222 else
1223 spin = SPIN_STYLE::LEFT;
1224 }
1225 else
1226 {
1227 if( bboxCenter.y >= 0 )
1228 spin = SPIN_STYLE::BOTTOM;
1229 else
1230 spin = SPIN_STYLE::UP;
1231 }
1232
1233 label->SetSpinStyle( spin );
1234
1235 if( nameAttr )
1236 {
1237 nlohmann::json style = fontStyles[nameAttr->fontStyle];
1238
1239 if( !style.is_null() && style.at( 5 ).is_number() )
1240 {
1241 double size = style.at( 5 ).get<double>() * 0.62;
1242 label->SetTextSize( VECTOR2I( ScaleSize( size ), ScaleSize( size ) ) );
1243 }
1244 }
1245
1246 createdItems.push_back( std::move( label ) );
1247
1248 continue;
1249 }
1250 else
1251 {
1253 mergedAttrValues, true,
1254 [&]( const wxString& attrKey, const wxString& value )
1255 {
1256 SCH_FIELD* text = schSym->FindFieldCaseInsensitive( attrKey );
1257
1258 if( !text )
1259 text = schSym->AddField( SCH_FIELD( schSym.get(), FIELD_T::USER, attrKey ) );
1260
1261 text->SetText( value );
1262 text->SetVisible( false );
1263 } );
1264
1265 auto nameAttr = get_opt( attributes, "Name" );
1266 auto valueAttr = get_opt( attributes, "Value" );
1267
1268 if( valueAttr && valueAttr->value.empty() )
1269 valueAttr->value = get_def( prjCompAttrs, "Value", wxString() );
1270
1271 if( nameAttr && nameAttr->value.empty() )
1272 nameAttr->value = get_def( prjCompAttrs, "Name", wxString() );
1273
1274 std::optional<EASYEDAPRO::SCH_ATTR> targetValueAttr;
1275
1276 if( valueAttr && !valueAttr->value.empty() && valueAttr->valVisible )
1277 targetValueAttr = valueAttr;
1278 else if( nameAttr && !nameAttr->value.empty() && nameAttr->valVisible )
1279 targetValueAttr = nameAttr;
1280 else if( valueAttr && !valueAttr->value.empty() )
1281 targetValueAttr = valueAttr;
1282 else if( nameAttr && !nameAttr->value.empty() )
1283 targetValueAttr = nameAttr;
1284
1285 if( targetValueAttr )
1286 {
1287 ApplyAttrToField( fontStyles, schSym->GetField( FIELD_T::VALUE ),
1288 *targetValueAttr, false, true, mergedAttrValues, schSym.get() );
1289 }
1290
1291 if( auto descrAttr = get_opt( attributes, "Description" ) )
1292 {
1293 ApplyAttrToField( fontStyles, schSym->GetField( FIELD_T::DESCRIPTION ),
1294 *descrAttr, false, true, mergedAttrValues, schSym.get() );
1295 }
1296
1297 if( auto designatorAttr = get_opt( attributes, "Designator" ) )
1298 {
1299 ApplyAttrToField( fontStyles, schSym->GetField( FIELD_T::REFERENCE ),
1300 *designatorAttr, false, true, mergedAttrValues, schSym.get() );
1301
1302 schSym->SetRef( &aSchematic->CurrentSheet(), designatorAttr->value );
1303 }
1304
1305 for( auto& [attrKey, attr] : attributes )
1306 {
1307 if( attrKey == wxS( "Name" ) || attrKey == wxS( "Value" )
1308 || attrKey == wxS( "Global Net Name" ) || attrKey == wxS( "Designator" )
1309 || attrKey == wxS( "Description" ) || attrKey == wxS( "Device" )
1310 || attrKey == wxS( "Footprint" ) || attrKey == wxS( "Symbol" )
1311 || attrKey == wxS( "Unique ID" ) )
1312 {
1313 continue;
1314 }
1315
1316 if( attr.value.IsEmpty() )
1317 continue;
1318
1319 SCH_FIELD* text = schSym->FindFieldCaseInsensitive( attrKey );
1320
1321 if( !text )
1322 text = schSym->AddField( SCH_FIELD( schSym.get(), FIELD_T::USER, attrKey ) );
1323
1324 text->SetPosition( schSym->GetPosition() );
1325
1326 ApplyAttrToField( fontStyles, text, attr, false, true, mergedAttrValues,
1327 schSym.get() );
1328 }
1329 }
1330
1331 for( const EASYEDAPRO::PIN_INFO& pinInfo : esymInfo.pins )
1332 {
1333 wxString pinKey = parentId + pinInfo.pin.id;
1334 auto pinLines = get_opt( parentedLines, pinKey );
1335
1336 if( !pinLines )
1337 continue;
1338
1339 for( const nlohmann::json& pinLine : *pinLines )
1340 {
1341 if( pinLine.at( 0 ) != "ATTR" )
1342 continue;
1343
1344 EASYEDAPRO::SCH_ATTR attr = pinLine;
1345
1346 if( attr.key != wxS( "NO_CONNECT" ) )
1347 continue;
1348
1349 for( SCH_PIN* schPin : schSym->GetPinsByNumber( pinInfo.number ) )
1350 {
1351 VECTOR2I pos = schSym->GetPinPhysicalPosition( schPin->GetLibPin() );
1352
1353 std::unique_ptr<SCH_NO_CONNECT> noConn =
1354 std::make_unique<SCH_NO_CONNECT>( pos );
1355
1356 createdItems.push_back( std::move( noConn ) );
1357 }
1358 }
1359 }
1360
1361 createdItems.push_back( std::move( schSym ) );
1362 }
1363 else // Not component
1364 {
1365 std::vector<SHAPE_LINE_CHAIN> wireLines;
1366
1367 if( wire )
1368 {
1369 for( const std::vector<double>& ptArr : wire->geometry )
1370 {
1372
1373 for( size_t i = 1; i < ptArr.size(); i += 2 )
1374 chain.Append( ScalePos( VECTOR2D( ptArr[i - 1], ptArr[i] ) ) );
1375
1376 if( chain.PointCount() < 2 )
1377 continue;
1378
1379 wireLines.push_back( chain );
1380
1381 for( int segId = 0; segId < chain.SegmentCount(); segId++ )
1382 {
1383 const SEG& seg = chain.CSegment( segId );
1384
1385 std::unique_ptr<SCH_LINE> schLine =
1386 std::make_unique<SCH_LINE>( seg.A, LAYER_WIRE );
1387 schLine->SetEndPoint( seg.B );
1388
1389 createdItems.push_back( std::move( schLine ) );
1390 }
1391 }
1392 }
1393
1394 auto netAttr = get_opt( attributes, "NET" );
1395
1396 if( netAttr )
1397 {
1398 if( !netAttr->valVisible || netAttr->value.IsEmpty() )
1399 continue;
1400
1401 VECTOR2I kpos = ScalePos( *netAttr->position );
1402 VECTOR2I nearestPos = kpos;
1403 SEG::ecoord min_dist_sq = VECTOR2I::ECOORD_MAX;
1404
1405 for( const SHAPE_LINE_CHAIN& chain : wireLines )
1406 {
1407 VECTOR2I nearestPt = chain.NearestPoint( kpos, false );
1408 SEG::ecoord dist_sq = ( nearestPt - kpos ).SquaredEuclideanNorm();
1409
1410 if( dist_sq < min_dist_sq )
1411 {
1412 min_dist_sq = dist_sq;
1413 nearestPos = nearestPt;
1414 }
1415 }
1416
1417 std::unique_ptr<SCH_LABEL> label = std::make_unique<SCH_LABEL>();
1418
1419 label->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
1420 label->SetVertJustify( GR_TEXT_V_ALIGN_BOTTOM );
1421
1422 for( double i = netAttr->rotation; i > 0; i -= 90 )
1423 label->Rotate90( true );
1424
1425 label->SetPosition( nearestPos );
1426 label->SetText( netAttr->value );
1427
1428 ApplyFontStyle( fontStyles, label, netAttr->fontStyle );
1429
1430 createdItems.push_back( std::move( label ) );
1431 }
1432 }
1433 }
1434
1435 // Adjust page to content
1436 BOX2I sheetBBox;
1437
1438 for( std::unique_ptr<SCH_ITEM>& ptr : createdItems )
1439 {
1440 if( ptr->Type() == SCH_SYMBOL_T )
1441 sheetBBox.Merge( static_cast<SCH_SYMBOL*>( ptr.get() )->GetBodyAndPinsBoundingBox() );
1442 else
1443 sheetBBox.Merge( ptr->GetBoundingBox() );
1444 }
1445
1446 SCH_SCREEN* screen = aRootSheet->GetScreen();
1447 PAGE_INFO pageInfo = screen->GetPageSettings();
1448
1449 int alignGrid = schIUScale.MilsToIU( 50 );
1450
1451 VECTOR2D offset( -sheetBBox.GetLeft(), -sheetBBox.GetTop() );
1452 offset.x = KiROUND( offset.x / alignGrid ) * alignGrid;
1453 offset.y = KiROUND( offset.y / alignGrid ) * alignGrid;
1454
1455 pageInfo.SetWidthMils( schIUScale.IUToMils( sheetBBox.GetWidth() ) );
1456 pageInfo.SetHeightMils( schIUScale.IUToMils( sheetBBox.GetHeight() ) );
1457
1458 screen->SetPageSettings( pageInfo );
1459
1460 for( std::unique_ptr<SCH_ITEM>& ptr : createdItems )
1461 {
1462 ptr->Move( offset );
1463 screen->Append( ptr.release() );
1464 }
1465}
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 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 coord_type GetLeft() const
Definition box2.h:231
constexpr coord_type GetTop() const
Definition box2.h:232
constexpr void Offset(coord_type dx, coord_type dy)
Definition box2.h:262
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition eda_item.h:158
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition eda_item.h:160
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition eda_text.h:94
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:348
void SetImportOffsetMM(const VECTOR2D &aOffset)
Set the offset in millimeters to add to coordinates when importing graphic items.
void SetScale(const VECTOR2D &aScale)
Set the scale factor affecting the imported shapes.
std::list< std::unique_ptr< EDA_ITEM > > & GetItems()
Return the list of objects representing the imported shapes.
virtual void SetImporter(GRAPHICS_IMPORTER *aImporter)
Set the receiver of the imported shapes.
static FONT * GetFont(const wxString &aFontName=wxEmptyString, bool aBold=false, bool aItalic=false, const std::vector< wxString > *aEmbeddedFiles=nullptr, bool aForDrawingSheet=false)
Definition font.cpp:144
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
const UTF8 & GetLibItemName() const
Definition lib_id.h:98
Define a library symbol object.
Definition lib_symbol.h:114
const LIB_ID & GetLibId() const override
Definition lib_symbol.h:190
void SetGlobalPower()
SCH_FIELD * FindFieldCaseInsensitive(const wxString &aFieldName)
wxString GetName() const override
Definition lib_symbol.h:183
void SetUnitCount(int aCount, bool aDuplicateDrawItems)
Set the units per symbol count.
void SetDescription(const wxString &aDescription)
Set the Description field text value.
void SetKeyWords(const wxString &aKeyWords)
SCH_FIELD & GetValueField()
Definition lib_symbol.h:449
wxString GetFootprint() const override
For items with footprint fields.
void AddField(SCH_FIELD *aField)
Add a field.
void AddDrawItem(SCH_ITEM *aItem, bool aSort=true)
Add a new draw aItem to the draw object list and sort according to aSort.
SCH_FIELD & GetReferenceField()
Definition lib_symbol.h:453
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition page_info.h:75
void SetHeightMils(double aHeightInMils)
void SetWidthMils(double aWidthInMils)
A progress reporter interface for use in multi-threaded environments.
A REFERENCE_IMAGE is a wrapper around a BITMAP_IMAGE that is displayed in an editor as a reference fo...
bool ReadImageFile(const wxString &aFullFilename)
Read and store an image file.
VECTOR2I GetSize() const
void SetImageScale(double aScale)
Set the image "zoom" value.
Holds all the data relating to one schematic.
Definition schematic.h:155
SCH_SHEET_PATH & CurrentSheet() const
Definition schematic.h:399
static double Convert(wxString aValue)
static VECTOR2< T > ScalePos(VECTOR2< T > aValue)
double SizeToKi(wxString units)
static T ScaleSize(T aValue)
void ApplyFontStyle(const std::map< wxString, nlohmann::json > &fontStyles, T &text, const wxString &styleStr)
static VECTOR2< T > ScalePosSym(VECTOR2< T > aValue)
SCH_EASYEDAPRO_PARSER(SCHEMATIC *aSchematic, PROGRESS_REPORTER *aProgressReporter)
EASYEDAPRO::SYM_INFO ParseSymbol(const std::vector< nlohmann::json > &aLines, const std::map< wxString, wxString > &aDeviceAttributes)
void ApplyAttrToField(const std::map< wxString, nlohmann::json > &fontStyles, T *text, const EASYEDAPRO::SCH_ATTR &aAttr, bool aIsSym, bool aToSym, const std::map< wxString, wxString > &aDeviceAttributes={}, SCH_SYMBOL *aParent=nullptr)
void ParseSchematic(SCHEMATIC *aSchematic, SCH_SHEET *aRootSheet, const nlohmann::json &aProject, std::map< wxString, EASYEDAPRO::SYM_INFO > &aSymbolMap, const std::map< wxString, EASYEDAPRO::BLOB > &aBlobMap, const std::vector< nlohmann::json > &aLines, const wxString &aLibName)
void ApplyLineStyle(const std::map< wxString, nlohmann::json > &lineStyles, T &shape, const wxString &styleStr)
virtual const wxString & GetText() const override
Return the string associated with the text object.
Definition sch_field.h:139
void SetText(const wxString &aText) override
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:170
virtual void MirrorHorizontally(int aCenter)
Mirror item horizontally about aCenter.
Definition sch_item.h:430
virtual void Rotate(const VECTOR2I &aCenter, bool aRotateCCW)
Rotate the item around aCenter 90 degrees in the clockwise direction.
Definition sch_item.h:446
const PAGE_INFO & GetPageSettings() const
Definition sch_screen.h:144
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition sch_screen.h:145
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
BOX2I GetBodyAndPinsBoundingBox() const override
Return a bounding box for the symbol body and pins but not the fields.
Definition seg.h:38
VECTOR2I A
Definition seg.h:45
VECTOR2I::extended_type ecoord
Definition seg.h:40
VECTOR2I B
Definition seg.h:46
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
Spin style for labels of all kinds on schematics.
Definition sch_label.h:38
Simple container to manage line stroke parameters.
void SetLineStyle(LINE_STYLE aLineStyle)
void SetWidth(int aWidth)
void SetColor(const KIGFX::COLOR4D &aColor)
bool Import() override
Actually imports the file.
virtual double GetImageWidth() const override
Return image width from original imported file.
bool LoadFromMemory(const wxMemoryBuffer &aMemBuffer) override
Set memory buffer with content for import.
virtual double GetImageHeight() const override
Return image height from original imported file.
virtual void SetShowPinNumbers(bool aShow)
Set or clear the pin number visibility flag.
Definition symbol.h:174
virtual void SetShowPinNames(bool aShow)
Set or clear the pin name visibility flag.
Definition symbol.h:168
static constexpr extended_type ECOORD_MAX
Definition vector2d.h:72
#define _(s)
static constexpr EDA_ANGLE ANGLE_VERTICAL
Definition eda_angle.h:469
static constexpr EDA_ANGLE ANGLE_HORIZONTAL
Definition eda_angle.h:468
#define ENDPOINT
ends. (Used to support dragging.)
#define STARTPOINT
When a line is selected, these flags indicate which.
@ RECTANGLE
Use RECTANGLE instead of RECT to avoid collision in a Windows header.
Definition eda_shape.h:58
void ConvertImageToLibShapes(LIB_SYMBOL *aSymbol, int unit, wxImage img, VECTOR2D pixelScale, VECTOR2D offset)
#define THROW_IO_ERRORF(msg,...)
const wxChar *const traceEasyEdaIo
@ LAYER_DEVICE
Definition layer_ids.h:497
@ LAYER_WIRE
Definition layer_ids.h:483
@ LAYER_NOTES
Definition layer_ids.h:498
wxString get_def(const std::map< wxString, wxString > &aMap, const char *aKey, const char *aDefval="")
Definition map_helpers.h:60
std::optional< V > get_opt(const std::map< wxString, V > &aMap, const wxString &aKey)
Definition map_helpers.h:30
LIB_ID ToKiCadLibID(const wxString &aLibName, const wxString &aLibReference)
void ForEachImportedDeviceField(const std::map< wxString, wxString > &aDeviceAttributes, bool aIncludeValue, const std::function< void(const wxString &aKey, const wxString &aValue)> &aCallback)
Invoke aCallback for each non-empty whitelisted Device field (resolved + normalized).
wxString ResolveDeviceFieldVariables(const wxString &aInput, const std::map< wxString, wxString > &aDeviceAttributes)
Resolve EasyEDA ={Var} / ={A}text{B} field expressions against device attributes.
std::map< wxString, wxString > AnyMapToStringMap(const std::map< wxString, nlohmann::json > &aInput)
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:461
@ PT_INPUT
usual pin input: must be connected
Definition pin_type.h:33
@ PT_NC
not connected (must be left open)
Definition pin_type.h:46
@ PT_OUTPUT
usual output
Definition pin_type.h:34
@ PT_BIDI
input or output (like port for a microprocessor)
Definition pin_type.h:35
@ PT_POWER_IN
power input (GND, VCC for ICs). Must be connected to a power output.
Definition pin_type.h:42
PIN_ORIENTATION
The symbol library pin object orientations.
Definition pin_type.h:101
@ PIN_UP
The pin extends upwards from the connection point: Probably on the bottom side of the symbol.
Definition pin_type.h:123
@ PIN_RIGHT
The pin extends rightwards from the connection point.
Definition pin_type.h:107
@ PIN_LEFT
The pin extends leftwards from the connection point: Probably on the right side of the symbol.
Definition pin_type.h:114
@ PIN_DOWN
The pin extends downwards from the connection: Probably on the top side of the symbol.
Definition pin_type.h:131
static LINE_STYLE ConvertStrokeStyle(const wxString &aStyle)
static LINE_STYLE ConvertStrokeStyle(int aStyle)
@ L_BIDI
Definition sch_label.h:100
@ L_OUTPUT
Definition sch_label.h:99
@ L_INPUT
Definition sch_label.h:98
const int scale
wxString UnescapeHTML(const wxString &aString)
Return a new wxString unescaped from HTML format.
LINE_STYLE
Dashed line types.
EASYEDAPRO::SYM_PIN pin
std::optional< VECTOR2D > position
std::unique_ptr< LIB_SYMBOL > libSymbol
std::vector< PIN_INFO > pins
std::optional< EASYEDAPRO::SCH_ATTR > symbolAttr
std::map< wxString, int > partUnits
EASYEDAPRO::SYM_HEAD head
@ SYM_ORIENT_270
Definition symbol.h:39
@ SYM_MIRROR_Y
Definition symbol.h:41
@ SYM_ORIENT_180
Definition symbol.h:38
@ SYM_MIRROR_X
Definition symbol.h:40
@ SYM_ORIENT_90
Definition symbol.h:37
@ SYM_ORIENT_0
Definition symbol.h:36
@ USER
The field ID hasn't been set yet; field is invalid.
@ DESCRIPTION
Field Description of part, i.e. "1/4W 1% Metal Film Resistor".
@ REFERENCE
Field Reference of part, i.e. "IC21".
@ VALUE
Field Value of part, i.e. "3.3K".
KIBIS_PIN * pin
VECTOR2I center
const SHAPE_LINE_CHAIN chain
int radius
VECTOR2I end
SHAPE_CIRCLE circle(c.m_circle_center, c.m_circle_radius)
GR_TEXT_H_ALIGN_T
This is API surface mapped to common.types.HorizontalAlignment.
@ GR_TEXT_H_ALIGN_CENTER
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
GR_TEXT_V_ALIGN_T
This is API surface mapped to common.types.VertialAlignment.
@ GR_TEXT_V_ALIGN_BOTTOM
@ GR_TEXT_V_ALIGN_CENTER
@ GR_TEXT_V_ALIGN_TOP
wxLogTrace helper definitions.
@ SCH_LINE_T
Definition typeinfo.h:159
@ SCH_SYMBOL_T
Definition typeinfo.h:168
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
VECTOR2< double > VECTOR2D
Definition vector2d.h:707
static PIECE rect(double aLeft, double aTop, double aRight, double aBottom, bool aMixed=false)