322 const std::map<wxString, wxString>& aDeviceAttributes )
326 std::unique_ptr<LIB_SYMBOL> ksymbolPtr = std::make_unique<LIB_SYMBOL>( wxEmptyString );
329 std::map<wxString, nlohmann::json> lineStyles;
330 std::map<wxString, nlohmann::json> fontStyles;
331 std::map<wxString, int> partUnits;
333 std::map<int, std::map<wxString, EASYEDAPRO::SCH_ATTR>> unitAttributes;
334 std::map<int, std::map<wxString, std::vector<nlohmann::json>>> unitParentedLines;
338 for(
const nlohmann::json& line : aLines )
340 wxString type = line.at( 0 );
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;
350 if( totalUnits == 0 )
358 for(
const nlohmann::json& line : aLines )
360 wxString type = line.at( 0 );
362 if( type == wxS(
"PART" ) )
364 currentUnit = partUnits.at( line.at( 1 ) );
366 else if( type == wxS(
"RECT" ) )
368 VECTOR2D start( line.at( 2 ), line.at( 3 ) );
370 wxString styleStr = line.at( 9 );
377 rect->SetUnit( currentUnit );
382 else if( type == wxS(
"CIRCLE" ) )
385 double radius = line.at( 4 );
386 wxString styleStr = line.at( 5 );
393 circle->SetUnit( currentUnit );
398 else if( type == wxS(
"ARC" ) )
400 VECTOR2D start( line.at( 2 ), line.at( 3 ) );
401 VECTOR2D mid( line.at( 4 ), line.at( 5 ) );
403 wxString styleStr = line.at( 8 );
411 shape->SetArcOrLine( kstart, kmid, kend );
413 shape->SetUnit( currentUnit );
418 else if( type == wxS(
"BEZIER" ) )
420 std::vector<double> points = line.at( 2 );
421 wxString styleStr = line.at( 3 );
423 std::unique_ptr<SCH_SHAPE> shape =
426 for(
size_t i = 1; i < points.size(); i += 2 )
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;
439 shape->SetUnit( currentUnit );
444 else if( type == wxS(
"POLY" ) )
446 std::vector<double> points = line.at( 2 );
447 wxString styleStr = line.at( 4 );
451 for(
size_t i = 1; i < points.size(); i += 2 )
454 shape->SetUnit( currentUnit );
459 else if( type == wxS(
"TEXT" ) )
462 if( !line.at( 2 ).is_number() || !line.at( 3 ).is_number() )
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 );
475 text->SetTextAngleDegrees( angle );
477 text->SetUnit( currentUnit );
482 else if( type == wxS(
"OBJ" ) )
485 wxString mimeType, data;
489 if( line.at( 3 ).is_number() )
491 start =
VECTOR2D( line.at( 3 ), line.at( 4 ) );
492 size =
VECTOR2D( line.at( 5 ), line.at( 6 ) );
494 upsideDown = line.at( 8 );
496 wxString imageUrl = line.at( 9 );
498 if( imageUrl.BeforeFirst(
':' ) == wxS(
"data" ) )
500 wxArrayString paramsArr =
501 wxSplit( imageUrl.AfterFirst(
':' ).BeforeFirst(
',' ),
';',
'\0' );
503 data = imageUrl.AfterFirst(
',' );
505 if( paramsArr.size() > 0 )
507 mimeType = paramsArr[0];
511 else if( line.at( 3 ).is_string() )
513 mimeType = line.at( 3 ).get<wxString>().BeforeFirst(
';' );
515 start =
VECTOR2D( line.at( 4 ), line.at( 5 ) );
516 size =
VECTOR2D( line.at( 6 ), line.at( 7 ) );
518 data = line.at( 9 ).get<wxString>();
521 if( mimeType.empty() || data.empty() )
524 wxMemoryBuffer buf = wxBase64Decode( data );
526 if( mimeType == wxS(
"image/svg+xml" ) )
545 libsymImporter.
SetScale( pixelScale );
554 for( std::unique_ptr<EDA_ITEM>& item : libsymImporter.
GetItems() )
559 wxMemoryInputStream memis( buf.GetData(), buf.GetDataLen() );
561 wxImage::SetDefaultLoadFlags( wxImage::GetDefaultLoadFlags()
562 & ~wxImage::Load_Verbose );
564 if( img.LoadFile( memis, mimeType ) )
566 int dimMul = img.GetWidth() * img.GetHeight();
567 double maxPixels = 30000;
569 if( dimMul > maxPixels )
571 double scale = sqrt( maxPixels / dimMul );
572 img.Rescale( img.GetWidth() *
scale, img.GetHeight() *
scale );
583 else if( type == wxS(
"HEAD" ) )
587 else if( type == wxS(
"PIN" ) )
589 wxString pinId = line.at( 1 );
590 unitParentedLines[currentUnit][pinId].push_back( line );
592 else if( type == wxS(
"ATTR" ) )
594 wxString parentId = line.at( 2 );
596 if( parentId.empty() )
599 unitAttributes[currentUnit].emplace( attr.
key, attr );
603 unitParentedLines[currentUnit][parentId].push_back( line );
618 if(
auto globalNetAttr =
get_opt( unitAttributes[1], wxS(
"Global Net Name" ) ) )
622 wxString globalNetname = globalNetAttr->value;
624 if( !globalNetname.empty() )
627 _(
"Power symbol creates a global label with name '%s'" ),
634 auto designatorAttr =
get_opt( unitAttributes[1], wxS(
"Designator" ) );
636 if( designatorAttr && !designatorAttr->value.empty() )
638 wxString symbolPrefix = designatorAttr->value;
640 if( symbolPrefix.EndsWith( wxS(
"?" ) ) )
641 symbolPrefix.RemoveLast();
647 aDeviceAttributes,
true,
648 [&](
const wxString& attrName,
const wxString& value )
663 for(
auto& [unitId, parentedLines] : unitParentedLines )
665 for(
auto& [pinId, lines] : parentedLines )
667 std::optional<EASYEDAPRO::SYM_PIN> epin;
668 std::map<wxString, EASYEDAPRO::SCH_ATTR> pinAttributes;
670 for(
const nlohmann::json& line : lines )
672 wxString type = line.at( 0 );
674 if( type == wxS(
"ATTR" ) )
677 pinAttributes.emplace( attr.
key, attr );
679 else if( type == wxS(
"PIN" ) )
691 std::unique_ptr<SCH_PIN>
pin = std::make_unique<SCH_PIN>( ksymbol );
693 pin->SetUnit( unitId );
700 if( epin->rotation == 0 )
702 if( epin->rotation == 90 )
704 if( epin->rotation == 180 )
706 if( epin->rotation == 270 )
709 pin->SetOrientation( orient );
718 auto pinNameAttr =
get_opt( pinAttributes,
"Pin Name" );
721 pinNameAttr =
get_opt( pinAttributes,
"NAME" );
725 pin->SetName( pinNameAttr->value );
726 pinInfo.
name = pinNameAttr->value;
728 if( !pinNameAttr->valVisible )
733 auto pinNumAttr =
get_opt( pinAttributes,
"Pin Number" );
736 pinNumAttr =
get_opt( pinAttributes,
"NUMBER" );
740 pin->SetNumber( pinNumAttr->value );
741 pinInfo.
number = pinNumAttr->value;
743 if( !pinNumAttr->valVisible )
751 else if(
auto pinTypeAttr =
get_opt( pinAttributes,
"Pin Type" ) )
753 if( pinTypeAttr->value == wxS(
"IN" ) )
755 if( pinTypeAttr->value == wxS(
"OUT" ) )
757 if( pinTypeAttr->value == wxS(
"BI" ) )
761 if(
get_opt( pinAttributes,
"NO_CONNECT" ) )
764 if(
pin->GetNumberTextSize() *
int(
pin->GetNumber().size() ) >
pin->GetLength() )
765 pin->SetNumberTextSize(
pin->GetLength() /
pin->GetNumber().size() );
767 symInfo.
pins.push_back( pinInfo );
785 symInfo.
libSymbol = std::move( ksymbolPtr );
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 )
798 std::vector<std::unique_ptr<SCH_ITEM>> createdItems;
800 std::map<wxString, std::vector<nlohmann::json>> parentedLines;
801 std::map<wxString, std::vector<nlohmann::json>> ruleLines;
803 std::map<wxString, nlohmann::json> lineStyles;
804 std::map<wxString, nlohmann::json> fontStyles;
806 for(
const nlohmann::json& line : aLines )
808 wxString type = line.at( 0 );
810 if( type == wxS(
"LINESTYLE" ) )
811 lineStyles[line.at( 1 )] = line;
812 else if( type == wxS(
"FONTSTYLE" ) )
813 fontStyles[line.at( 1 )] = line;
816 for(
const nlohmann::json& line : aLines )
818 wxString type = line.at( 0 );
820 if( type == wxS(
"RECT" ) )
822 VECTOR2D start( line.at( 2 ), line.at( 3 ) );
824 wxString styleStr = line.at( 9 );
833 createdItems.push_back( std::move(
rect ) );
835 else if( type == wxS(
"CIRCLE" ) )
838 double radius = line.at( 4 );
839 wxString styleStr = line.at( 5 );
848 createdItems.push_back( std::move(
circle ) );
850 else if( type == wxS(
"POLY" ) )
852 std::vector<double> points = line.at( 2 );
853 wxString styleStr = line.at( 4 );
857 for(
size_t i = 1; i < points.size(); i += 2 )
860 for(
int segId = 0; segId <
chain.SegmentCount(); segId++ )
862 const SEG& seg =
chain.CSegment( segId );
864 std::unique_ptr<SCH_LINE> schLine =
866 schLine->SetEndPoint( seg.
B );
870 createdItems.push_back( std::move( schLine ) );
873 else if( type == wxS(
"TEXT" ) )
876 if( !line.at( 2 ).is_number() || !line.at( 3 ).is_number() )
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 );
884 std::unique_ptr<SCH_TEXT>
text =
890 text->SetTextAngleDegrees( angle );
894 createdItems.push_back( std::move(
text ) );
896 else if( type == wxS(
"OBJ" ) )
899 wxString mimeType, base64Data;
903 if( line.at( 3 ).is_number() )
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 );
910 wxString imageUrl = line.at( 9 );
912 if( imageUrl.BeforeFirst(
':' ) == wxS(
"data" ) )
914 wxArrayString paramsArr =
915 wxSplit( imageUrl.AfterFirst(
':' ).BeforeFirst(
',' ),
';',
'\0' );
917 base64Data = imageUrl.AfterFirst(
',' );
919 if( paramsArr.size() > 0 )
920 mimeType = paramsArr[0];
922 else if( imageUrl.BeforeFirst(
':' ) == wxS(
"blob" ) )
924 wxString objectId = imageUrl.AfterLast(
':' );
926 if(
auto blob =
get_opt( aBlobMap, objectId ) )
928 wxString blobUrl = blob->url;
930 if( blobUrl.BeforeFirst(
':' ) == wxS(
"data" ) )
932 wxArrayString paramsArr = wxSplit(
933 blobUrl.AfterFirst(
':' ).BeforeFirst(
',' ),
';',
'\0' );
935 base64Data = blobUrl.AfterFirst(
',' );
937 if( paramsArr.size() > 0 )
938 mimeType = paramsArr[0];
943 else if( line.at( 3 ).is_string() )
945 mimeType = line.at( 3 ).get<wxString>().BeforeFirst(
';' );
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>();
956 if( mimeType.empty() || base64Data.empty() )
959 wxMemoryBuffer buf = wxBase64Decode( base64Data );
961 if( mimeType == wxS(
"image/svg+xml" ) )
983 for( std::unique_ptr<EDA_ITEM>& item : schImporter.
GetItems() )
987 for(
double i = angle; i > 0; i -= 90 )
993 schItem->
Rotate( kstart,
false );
997 schItem->
Rotate( kstart,
false );
1002 schItem->
Rotate( kstart,
false );
1018 createdItems.emplace_back( schItem );
1023 std::unique_ptr<SCH_BITMAP> bitmap = std::make_unique<SCH_BITMAP>();
1026 wxImage::SetDefaultLoadFlags( wxImage::GetDefaultLoadFlags()
1027 & ~wxImage::Load_Verbose );
1031 VECTOR2D kcenter = kstart + ksize / 2;
1035 bitmap->SetPosition( kcenter );
1037 for(
double i = angle; i > 0; i -= 90 )
1038 bitmap->Rotate( kstart,
false );
1041 bitmap->MirrorHorizontally( kstart.
x );
1043 createdItems.push_back( std::move( bitmap ) );
1047 if( type == wxS(
"WIRE" ) )
1049 wxString wireId = line.at( 1 );
1050 parentedLines[wireId].push_back( line );
1052 else if( type == wxS(
"COMPONENT" ) )
1054 wxString compId = line.at( 1 );
1055 parentedLines[compId].push_back( line );
1057 else if( type == wxS(
"ATTR" ) )
1059 wxString compId = line.at( 2 );
1060 parentedLines[compId].push_back( line );
1064 for(
auto& [parentId, lines] : parentedLines )
1066 std::optional<EASYEDAPRO::SCH_COMPONENT> component;
1067 std::optional<EASYEDAPRO::SCH_WIRE> wire;
1068 std::map<wxString, EASYEDAPRO::SCH_ATTR> attributes;
1070 for(
const nlohmann::json& line : lines )
1072 if( line.at( 0 ) ==
"COMPONENT" )
1076 else if( line.at( 0 ) ==
"WIRE" )
1080 else if( line.at( 0 ) ==
"ATTR" )
1083 attributes.emplace( attr.
key, attr );
1089 auto deviceAttr =
get_opt( attributes,
"Device" );
1090 auto symbolAttr =
get_opt( attributes,
"Symbol" );
1096 aProject.at(
"devices" ).at( deviceAttr->value ).at(
"attributes" ) );
1099 std::map<wxString, wxString> mergedAttrValues;
1101 for(
const auto& [key, value] : prjCompAttrs )
1102 mergedAttrValues[key] = value;
1104 for(
const auto& [key, attr] : attributes )
1105 mergedAttrValues[key] = attr.value;
1109 if( symbolAttr && !symbolAttr->value.IsEmpty() )
1110 symbolId = symbolAttr->value;
1112 symbolId = prjCompAttrs.at(
"Symbol" );
1114 auto it = aSymbolMap.find( symbolId );
1115 if( it == aSymbolMap.end() )
1117 wxLogTrace(
traceEasyEdaIo,
"Symbol of '%s' with uuid '%s' not found.", component->name, symbolId );
1124 wxString unitName = component->name;
1129 auto schSym = std::make_unique<SCH_SYMBOL>( newLibSymbol, libId,
1133 schSym->SetFootprintFieldText( newLibSymbol.
GetFootprint() );
1135 for(
double i = component->rotation; i > 0; i -= 90 )
1136 schSym->Rotate(
VECTOR2I(),
true );
1138 if( component->mirror )
1139 schSym->MirrorHorizontally( 0 );
1141 schSym->SetPosition(
ScalePos( component->position ) );
1147 auto globalNetNameAttr =
get_opt( attributes,
"Global Net Name" );
1148 wxString globalNetNameFromProject =
get_def( prjCompAttrs,
"Global Net Name", wxEmptyString );
1149 wxString globalNetName;
1154 if( globalNetNameAttr && !globalNetNameAttr->value.IsEmpty() )
1156 globalNetName = globalNetNameAttr->value;
1159 mergedAttrValues, schSym.get() );
1161 else if( !globalNetNameFromProject.IsEmpty() )
1163 globalNetName = globalNetNameFromProject;
1172 for(
SCH_PIN*
pin : schSym->GetAllLibPins() )
1173 pin->SetName( globalNetName );
1175 schSym->SetRef( &aSchematic->
CurrentSheet(), wxS(
"#PWR?" ) );
1180 auto nameAttr =
get_opt( attributes,
"Name" );
1184 if( nameAttr && !nameAttr->value.IsEmpty() )
1185 netName = nameAttr->value;
1187 netName = prjCompAttrs.at(
"Name" );
1189 std::unique_ptr<SCH_GLOBALLABEL> label = std::make_unique<SCH_GLOBALLABEL>(
1190 ScalePos( component->position ), netName );
1192 std::vector<SCH_PIN*> pins = schSym->GetPins( &aSchematic->
CurrentSheet() );
1194 if( pins.size() > 0 )
1196 switch( pins[0]->GetType() )
1212 BOX2I bbox = schSym->GetBodyAndPinsBoundingBox();
1213 bbox.
Offset( -schSym->GetPosition() );
1220 if( bboxCenter.
x >= 0 )
1227 if( bboxCenter.
y >= 0 )
1233 label->SetSpinStyle( spin );
1237 nlohmann::json style = fontStyles[nameAttr->fontStyle];
1239 if( !style.is_null() && style.at( 5 ).is_number() )
1241 double size = style.at( 5 ).get<
double>() * 0.62;
1246 createdItems.push_back( std::move( label ) );
1253 mergedAttrValues,
true,
1254 [&](
const wxString& attrKey,
const wxString& value )
1256 SCH_FIELD*
text = schSym->FindFieldCaseInsensitive( attrKey );
1261 text->SetText( value );
1262 text->SetVisible(
false );
1265 auto nameAttr =
get_opt( attributes,
"Name" );
1266 auto valueAttr =
get_opt( attributes,
"Value" );
1268 if( valueAttr && valueAttr->value.empty() )
1269 valueAttr->value =
get_def( prjCompAttrs,
"Value", wxString() );
1271 if( nameAttr && nameAttr->value.empty() )
1272 nameAttr->value =
get_def( prjCompAttrs,
"Name", wxString() );
1274 std::optional<EASYEDAPRO::SCH_ATTR> targetValueAttr;
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;
1285 if( targetValueAttr )
1288 *targetValueAttr,
false,
true, mergedAttrValues, schSym.get() );
1291 if(
auto descrAttr =
get_opt( attributes,
"Description" ) )
1294 *descrAttr,
false,
true, mergedAttrValues, schSym.get() );
1297 if(
auto designatorAttr =
get_opt( attributes,
"Designator" ) )
1300 *designatorAttr,
false,
true, mergedAttrValues, schSym.get() );
1302 schSym->SetRef( &aSchematic->
CurrentSheet(), designatorAttr->value );
1305 for(
auto& [attrKey, attr] : attributes )
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" ) )
1316 if( attr.value.IsEmpty() )
1319 SCH_FIELD*
text = schSym->FindFieldCaseInsensitive( attrKey );
1324 text->SetPosition( schSym->GetPosition() );
1333 wxString pinKey = parentId + pinInfo.
pin.
id;
1334 auto pinLines =
get_opt( parentedLines, pinKey );
1339 for(
const nlohmann::json& pinLine : *pinLines )
1341 if( pinLine.at( 0 ) !=
"ATTR" )
1346 if( attr.
key != wxS(
"NO_CONNECT" ) )
1349 for(
SCH_PIN* schPin : schSym->GetPinsByNumber( pinInfo.
number ) )
1351 VECTOR2I pos = schSym->GetPinPhysicalPosition( schPin->GetLibPin() );
1353 std::unique_ptr<SCH_NO_CONNECT> noConn =
1354 std::make_unique<SCH_NO_CONNECT>( pos );
1356 createdItems.push_back( std::move( noConn ) );
1361 createdItems.push_back( std::move( schSym ) );
1365 std::vector<SHAPE_LINE_CHAIN> wireLines;
1369 for(
const std::vector<double>& ptArr : wire->geometry )
1373 for(
size_t i = 1; i < ptArr.size(); i += 2 )
1376 if(
chain.PointCount() < 2 )
1379 wireLines.push_back(
chain );
1381 for(
int segId = 0; segId <
chain.SegmentCount(); segId++ )
1383 const SEG& seg =
chain.CSegment( segId );
1385 std::unique_ptr<SCH_LINE> schLine =
1387 schLine->SetEndPoint( seg.
B );
1389 createdItems.push_back( std::move( schLine ) );
1394 auto netAttr =
get_opt( attributes,
"NET" );
1398 if( !netAttr->valVisible || netAttr->value.IsEmpty() )
1408 SEG::ecoord dist_sq = ( nearestPt - kpos ).SquaredEuclideanNorm();
1410 if( dist_sq < min_dist_sq )
1412 min_dist_sq = dist_sq;
1413 nearestPos = nearestPt;
1417 std::unique_ptr<SCH_LABEL> label = std::make_unique<SCH_LABEL>();
1422 for(
double i = netAttr->rotation; i > 0; i -= 90 )
1423 label->Rotate90(
true );
1425 label->SetPosition( nearestPos );
1426 label->SetText( netAttr->value );
1430 createdItems.push_back( std::move( label ) );
1438 for( std::unique_ptr<SCH_ITEM>& ptr : createdItems )
1443 sheetBBox.
Merge( ptr->GetBoundingBox() );
1452 offset.
x =
KiROUND( offset.
x / alignGrid ) * alignGrid;
1453 offset.
y =
KiROUND( offset.
y / alignGrid ) * alignGrid;
1460 for( std::unique_ptr<SCH_ITEM>& ptr : createdItems )
1462 ptr->Move( offset );
1463 screen->
Append( ptr.release() );