144 std::map<wxString, wxString> paramMap,
145 std::map<wxString, std::unique_ptr<FOOTPRINT>>& aFootprintMap,
146 wxArrayString aShapes )
149 const wxString easyedaModelDir = wxS(
"EASYEDA_MODELS" );
150 wxString kicadModelPrefix = wxS(
"${KIPRJMOD}/" ) + easyedaModelDir + wxS(
"/" );
152 BOARD* board = aParent ? aParent :
dynamic_cast<BOARD*
>( aContainer );
155 auto getOrAddNetItem =
161 if( aNetName.empty() )
183 for( wxString shape : aShapes )
185 wxArrayString arr = wxSplit( shape,
'~',
'\0' );
191 return aIndex < arr.size() ? arr[aIndex] : wxString();
194 wxString elType = arr[0];
195 if( elType == wxS(
"LIB" ) )
197 shape.Replace( wxS(
"#@$" ),
"\n" );
198 wxArrayString parts = wxSplit( shape,
'\n',
'\0' );
200 if( parts.size() < 1 )
203 wxArrayString paramsRoot = wxSplit( parts[0],
'~',
'\0' );
205 if( paramsRoot.size() < 4 )
210 wxString packageName = wxString::Format( wxS(
"Unknown_%s_%s" ), paramsRoot[1], paramsRoot[2] );
212 wxArrayString paramParts = wxSplit( paramsRoot[3],
'`',
'\0' );
216 if( !paramsRoot[4].IsEmpty() )
221 if( !paramsRoot[7].IsEmpty() )
222 layer =
Convert( paramsRoot[7] );
224 std::map<wxString, wxString> innerParamMap;
226 for(
int i = 1; i < (int) paramParts.size(); i += 2 )
228 wxString key = paramParts[i - 1];
229 wxString value = paramParts[i];
231 if( key == wxS(
"package" ) )
234 innerParamMap[key] = value;
241 aFootprintMap, parts );
248 fp->Move(
RelPos( fpOrigin ) );
252 else if( elType == wxS(
"TRACK" ) )
256 wxString netname = field( 3 );
257 wxArrayString data = wxSplit( field( 4 ),
' ',
'\0' );
259 for(
int i = 3; i < (int) data.size(); i += 2 )
269 auto track = std::make_unique<PCB_TRACK>( aContainer );
271 track->SetLayer( layer );
272 track->SetWidth( width );
273 track->SetStart( start );
274 track->SetEnd(
end );
275 track->SetNet( getOrAddNetItem( netname ) );
283 seg->SetLayer( layer );
284 seg->SetWidth( width );
285 seg->SetStart( start );
292 else if( elType == wxS(
"CIRCLE" ) )
294 auto circleShape = std::make_unique<PCB_SHAPE>( aContainer,
SHAPE_T::CIRCLE );
296 circleShape->SetWidth( width );
299 circleShape->SetLayer( layer );
307 circleShape->SetCenter(
center );
311 circleShape->SetNet( getOrAddNetItem( field( 8 ) ) );
315 else if( elType == wxS(
"RECT" ) )
319 const wxString widthField = field( 8 );
320 double width = widthField.IsEmpty() ? 0.0 :
ConvertSize( widthField );
321 rectShape->SetWidth( width );
324 rectShape->SetLayer( layer );
326 bool filled = field( 9 ) != wxS(
"none" );
327 rectShape->SetFilled( filled );
330 rectStart.
x =
RelPosX( field( 1 ) );
331 rectStart.
y =
RelPosY( field( 2 ) );
337 rectShape->SetStart( rectStart );
338 rectShape->SetEnd( rectStart + rectSize );
341 rectShape->SetNet( getOrAddNetItem( field( 11 ) ) );
345 else if( elType == wxS(
"ARC" ) )
347 auto arcShape = std::make_unique<PCB_SHAPE>( aContainer,
SHAPE_T::ARC );
350 arcShape->SetWidth( arcWidth );
353 arcShape->SetLayer( layer );
356 arcShape->SetNet( getOrAddNetItem( field( 3 ) ) );
364 wxString data = field( 4 );
365 auto readNumber = [&]( wxString& aOut )
367 wxUniChar ch = data[pos];
369 while( ch ==
' ' || ch ==
',' )
372 while( isdigit( ch ) || ch ==
'.' || ch ==
'-' )
377 if( pos == (
int) data.size() )
386 wxUniChar sym = data[pos++];
396 else if( sym ==
'A' )
398 wxString radX, radY,
unknown, farFlag, cwFlag, endX, endY;
402 readNumber( farFlag );
403 readNumber( cwFlag );
407 isFar = farFlag == wxS(
"1" );
408 cw = cwFlag == wxS(
"1" );
412 }
while( pos < (
int) data.size() );
416 double d =
delta.EuclideanNorm();
417 double h = sqrt( std::max( 0.0, rad.
x * rad.
x - d * d / 4 ) );
423 VECTOR2D arcCenter = arcStart +
delta / 2 +
delta.Perpendicular().Resize( ( isFar ^
cw ) ? h : -h );
426 std::swap( arcStart, arcEnd );
428 arcShape->SetStart(
RelPos( arcStart ) );
429 arcShape->SetEnd(
RelPos( arcEnd ) );
430 arcShape->SetCenter(
RelPos( arcCenter ) );
434 else if( elType == wxS(
"DIMENSION" ) )
438 double lineWidth =
ConvertSize( arr.size() > 7 ? field( 7 ) : wxString( wxS(
"0.4" ) ) );
439 wxString shapeData = field( 2 ).Trim();
445 std::unique_ptr<PCB_GROUP>
group = std::make_unique<PCB_GROUP>( aContainer );
446 group->SetName( wxS(
"Dimension" ) );
450 for(
int segId = 0; segId <
chain.SegmentCount(); segId++ )
455 dimSeg->SetLayer( layer );
456 dimSeg->SetWidth( lineWidth );
457 dimSeg->SetStart( seg.
A );
458 dimSeg->SetEnd( seg.
B );
460 group->AddItem( dimSeg.get() );
468 else if( elType == wxS(
"SOLIDREGION" ) )
470 wxString layer = field( 1 );
479 validPolySet &= contour.PointCount() >= 3;
485 if( layer == wxS(
"11" ) )
489 auto cutoutShape = std::make_unique<PCB_SHAPE>( aContainer,
SHAPE_T::POLY );
492 cutoutShape->SetFilled(
false );
493 cutoutShape->SetWidth(
pcbIUScale.mmToIU( 0.1 ) );
494 cutoutShape->SetPolyShape( poly );
501 std::unique_ptr<ZONE> zone = std::make_unique<ZONE>( aContainer );
504 zone->SetLayer( klayer );
507 zone->SetNet( getOrAddNetItem( field( 2 ) ) );
510 zone->Outline()->AddPolygon( poly );
512 if( zone->GetNumCorners() == 0 )
515 if( field( 4 ).Lower() == wxS(
"cutout" ) )
517 zone->SetIsRuleArea(
true );
518 zone->SetDoNotAllowZoneFills(
true );
519 zone->SetDoNotAllowTracks(
false );
520 zone->SetDoNotAllowVias(
false );
521 zone->SetDoNotAllowPads(
false );
522 zone->SetDoNotAllowFootprints(
false );
526 zone->SetFilledPolysList( klayer, polySet );
528 zone->SetIsFilled(
true );
529 zone->SetNeedRefill(
false );
532 zone->SetMinThickness( 0 );
533 zone->SetLocalClearance( 0 );
534 zone->SetAssignedPriority( 100 );
539 else if( elType == wxS(
"COPPERAREA" ) )
541 if( arr.size() < 11 )
544 std::unique_ptr<ZONE> zone = std::make_unique<ZONE>( aContainer );
547 zone->SetLayer( layer );
549 wxString netname = field( 3 );
552 zone->SetNet( getOrAddNetItem( netname ) );
554 zone->SetLocalClearance(
ConvertSize( field( 5 ) ) );
555 zone->SetThermalReliefGap( zone->GetLocalClearance().value() );
557 wxString fillStyle = field( 5 );
559 if( fillStyle == wxS(
"none" ) )
567 zone->Outline()->AddPolygon( poly );
569 wxString thermal = field( 8 );
571 if( thermal == wxS(
"direct" ) )
574 wxString keepIsland = field( 9 );
576 if( keepIsland == wxS(
"yes" ) )
579 wxString fillData = field( 10 );
583 for(
const nlohmann::json& polyData : nlohmann::json::parse( fillData ) )
585 for(
const nlohmann::json& contourData : polyData )
592 for(
int i = 0; i < contourPolySet.
HoleCount( 0 ); i++ )
595 fillPolySet.
Append( currentOutline );
601 zone->SetFilledPolysList( layer, fillPolySet );
602 zone->SetIsFilled(
true );
603 zone->SetNeedRefill(
false );
605 catch( nlohmann::json::exception& )
609 int fillOrder = wxAtoi( field( 13 ) );
610 zone->SetAssignedPriority( 100 - fillOrder );
613 wxString improveFabrication = field( 17 );
614 if( improveFabrication == wxS(
"none" ) )
616 zone->SetMinThickness( 0 );
621 int minThickness = std::max(
pcbIUScale.mmToIU( 0.03 ),
623 zone->SetMinThickness( minThickness );
626 if( arr.size() > 18 )
628 zone->SetThermalReliefSpokeWidth( std::max(
int(
ConvertSize( field( 18 ) ) ),
629 zone->GetMinThickness() ) );
633 zone->SetThermalReliefSpokeWidth( zone->GetMinThickness() );
638 else if( elType == wxS(
"SVGNODE" ) )
640 nlohmann::json nodeData = nlohmann::json::parse( field( 1 ) );
642 int nodeType = nodeData.at(
"nodeType" );
643 wxString layer = nodeData.at(
"layerid" );
648 std::map<wxString, wxString> attributes = nodeData.at(
"attrs" );
650 if( layer == wxS(
"19" ) )
655 auto ec_eType =
get_opt( attributes,
"c_etype" );
656 auto ec_rotation =
get_opt( attributes,
"c_rotation" );
657 auto ec_origin =
get_opt( attributes,
"c_origin" );
658 auto ec_width =
get_opt( attributes,
"c_width" );
659 auto ec_height =
get_opt( attributes,
"c_height" );
660 auto ez =
get_opt( attributes,
"z" );
661 auto etitle =
get_opt( attributes,
"title" );
662 auto euuid =
get_opt( attributes,
"uuid" );
663 auto etransform =
get_opt( attributes,
"transform" );
665 if( !ec_eType || *ec_eType != wxS(
"outline3D" ) || !etitle )
668 wxString modelTitle = *etitle;
678 footprint->
Add( idField );
690 if( ec_width && ec_height )
695 double rounding = 0.001;
696 fitXmm =
KiROUND( fitXmm / rounding ) * rounding;
697 fitYmm =
KiROUND( fitYmm / rounding ) * rounding;
702 sizeField->
SetText( wxString::FromCDouble( fitXmm ) + wxS(
" " )
703 + wxString::FromCDouble( fitYmm ) );
704 footprint->
Add( sizeField );
709 wxArrayString orParts = wxSplit( *ec_origin,
',',
'\0' );
711 if( orParts.size() == 2 )
714 pos.
x =
Convert( orParts[0].Trim() );
715 pos.
y =
Convert( orParts[1].Trim() );
733 wxArrayString rotParts = wxSplit( *ec_rotation,
',',
'\0' );
735 if( rotParts.size() == 3 )
737 kmodelRotation.
x = -
Convert( rotParts[0].Trim() );
738 kmodelRotation.
y = -
Convert( rotParts[1].Trim() );
739 kmodelRotation.
z = -
Convert( rotParts[2].Trim() )
746 kmodelRotation.
z = 180 - kmodelRotation.
z;
751 model.m_Filename = kicadModelPrefix
754 model.m_Offset = kmodelOffset;
755 model.m_Rotation = kmodelRotation;
760 if(
auto dataStr =
get_opt( attributes,
"d" ) )
764 if( dataStr->size() >= 8000 )
771 std::unique_ptr<PCB_GROUP>
group;
774 group = std::make_unique<PCB_GROUP>( aContainer );
778 auto svgShape = std::make_unique<PCB_SHAPE>( aContainer,
SHAPE_T::POLY );
780 svgShape->SetFilled(
true );
781 svgShape->SetPolyShape( poly );
782 svgShape->SetLayer( klayer );
783 svgShape->SetWidth( 0 );
786 group->AddItem( svgShape.get() );
801 else if( elType == wxS(
"TEXT" ) )
804 wxString textType = field( 1 );
806 if( footprint && textType == wxS(
"P" ) )
810 else if( footprint && textType == wxS(
"N" ) )
814 else if( field( 12 ) == wxS(
"none" ) )
828 text->SetPosition( start );
831 text->SetTextThickness( thickness );
833 double rot =
Convert( field( 5 ) );
834 text->SetTextAngleDegrees( rot );
840 text->SetLayer( layer );
843 text->SetMirrored(
true );
848 wxString textStr = field( 10 );
849 textStr.Replace( wxS(
"\\n" ), wxS(
"\n" ) );
854 wxString font = field( 14 );
855 if( !font.IsEmpty() )
860 else if( elType == wxS(
"VIA" ) )
868 std::unique_ptr<PAD>
pad = std::make_unique<PAD>( footprint );
883 std::unique_ptr<PCB_VIA>
via = std::make_unique<PCB_VIA>( aContainer );
888 via->SetNet( getOrAddNetItem( field( 4 ) ) );
889 via->SetDrill( kdrill );
894 else if( elType == wxS(
"HOLE" ) )
900 wxString holeUuid = field( 4 );
904 padContainer = footprint;
908 auto newFootprint = std::make_unique<FOOTPRINT>(
dynamic_cast<BOARD*
>( aContainer ) );
910 wxString
name = wxS(
"Hole_" ) + holeUuid;
912 newFootprint->SetFPID(
LIB_ID( wxEmptyString,
name ) );
913 newFootprint->SetPosition(
center );
914 newFootprint->SetLocked(
true );
916 newFootprint->Reference().SetText(
name );
917 newFootprint->Reference().SetVisible(
false );
919 newFootprint->Value().SetText(
name );
920 newFootprint->Value().SetVisible(
false );
923 padContainer = newFootprint.get();
927 std::unique_ptr<PAD>
pad = std::make_unique<PAD>( padContainer );
940 else if( elType == wxS(
"PAD" ) )
946 wxString padUuid = field( 12 );
950 padContainer = footprint;
954 auto newFootprint = std::make_unique<FOOTPRINT>(
dynamic_cast<BOARD*
>( aContainer ) );
956 wxString
name = wxS(
"Pad_" ) + padUuid;
958 newFootprint->SetFPID(
LIB_ID( wxEmptyString,
name ) );
959 newFootprint->SetPosition(
center );
960 newFootprint->SetLocked(
true );
962 newFootprint->Reference().SetText(
name );
963 newFootprint->Reference().SetVisible(
false );
965 newFootprint->Value().SetText(
name );
966 newFootprint->Value().SetVisible(
false );
969 padContainer = newFootprint.get();
973 std::unique_ptr<PAD>
pad = std::make_unique<PAD>( padContainer );
975 pad->SetNet( getOrAddNetItem( field( 7 ) ) );
976 pad->SetNumber( field( 8 ) );
980 pad->SetOrientationDegrees(
Convert( field( 11 ) ) );
983 wxString elayer = field( 6 );
986 bool plated = field( 15 ) == wxS(
"Y" );
994 else if( klayer ==
B_Cu )
1000 else if( elayer == wxS(
"11" ) )
1007 pad->SetLayer( klayer );
1008 pad->SetLayerSet(
LSET( { klayer } ) );
1012 const wxString& padType = field( 1 );
1014 if( padType == wxS(
"ELLIPSE" ) )
1018 else if( padType == wxS(
"RECT" ) )
1022 else if( padType == wxS(
"OVAL" ) )
1029 else if( padType == wxS(
"POLYGON" ) )
1035 wxArrayString data = wxSplit( field( 10 ),
' ',
'\0' );
1039 for(
int i = 1; i < (int) data.size(); i += 2 )
1047 chain.SetClosed(
true );
1050 chain.Rotate( -
pad->GetOrientation() );
1054 wxString holeDia = field( 9 );
1056 if( !holeDia.IsEmpty() )
1061 wxString holeLength = field( 13 );
1062 if( !holeLength.IsEmpty() )
1069 if( size.
x < size.
y )
1081 wxString pasteExp = field( 17 );
1083 if( !pasteExp.IsEmpty() )
1086 pad->SetLocalSolderPasteMargin( pasteExpansion );
1089 wxString maskExp = field( 18 );
1091 if( !maskExp.IsEmpty() )
1094 pad->SetLocalSolderMaskMargin( maskExpansion );
1097 bool badPad =
false;
1099 pad->Padstack().ForEachUniqueLayer(
1104 if( padSize.
x <= 0 || padSize.
y <= 0 )