KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_io_easyedapro_v3_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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
26
27#include <memory>
28
29
30#include <json_common.h>
32#include <core/map_helpers.h>
33#include <string_utils.h>
34
35#include <wx/log.h>
36#include <wx/base64.h>
37#include <wx/mstream.h>
38
39#include <footprint.h>
40#include <board.h>
41#include <pcb_group.h>
42#include <pcb_shape.h>
43#include <pcb_text.h>
44#include <pcb_track.h>
45#include <pcb_reference_image.h>
46#include <geometry/shape_arc.h>
47#include <geometry/shape_rect.h>
48#include <zone.h>
49#include <pad.h>
50#include <fix_board_shape.h>
53#include <netclass.h>
55
56#include <font/font.h>
57#include <core/mirror.h>
59#include <trace_helpers.h>
60
61using namespace EASYEDAPRO;
62
63static const wxString safeSpacingNames[] = { wxS( "Track" ),
64 wxS( "SMD Pad" ),
65 wxS( "TH Pad" ),
66 wxS( "SMD Test Point" ),
67 wxS( "TH Test Point" ),
68 wxS( "Via" ),
69 wxS( "Fill Region / Teardrop" ),
70 wxS( "Copper / Plane Zone" ),
71 wxS( "Slot Region" ),
72 wxS( "Line" ),
73 wxS( "Text / Image" ),
74 wxS( "Board Outline" ),
75 wxS( "Hole" ) };
76
77// Empty entries are source categories that the current item conversion flattens into another
78// KiCad type. Their values are retained as comments in the generated .kicad_dru file.
79static const wxString safeSpacingConditions[] = { wxS( "A.Type == 'Track'" ),
80 wxS( "A.Type == 'Pad' && A.Pad_Type == 'SMD'" ),
81 wxS( "A.Type == 'Pad' && A.Pad_Type == 'Through-hole'" ),
82 wxEmptyString,
83 wxEmptyString,
84 wxS( "A.Type == 'Via'" ),
85 wxEmptyString,
86 wxEmptyString,
87 wxEmptyString,
88 wxEmptyString,
89 wxEmptyString,
90 wxEmptyString,
91 wxEmptyString };
92
93
94static wxString safeSpacingToMm( double aMil )
95{
96 return wxString::FromUTF8( FormatDouble2Str( pcbIUScale.IUTomm( PCB_IO_EASYEDAPRO_PARSER::ScaleSize( aMil ) ) ) );
97}
98
99
104
105
106wxString PCB_IO_EASYEDAPRO_V3_PARSER::GenerateSafeSpacingRules( const nlohmann::json& aSafeSpacing )
107{
108 if( !aSafeSpacing.is_array() || aSafeSpacing.empty() )
109 return wxEmptyString;
110
111 wxString rules = wxS( "(version 1)\n\n"
112 "# EasyEDA Pro safeSpacing values. Values are mils even when the\n"
113 "# source rule context reports unit \"mm\". Entries without a\n"
114 "# generated rule are source categories that KiCad's imported item\n"
115 "# model cannot distinguish; they remain here for lossless reference.\n" );
116
117 for( size_t row = 0; row < sizeof( safeSpacingNames ) / sizeof( safeSpacingNames[0] ) && row < aSafeSpacing.size();
118 ++row )
119 {
120 const nlohmann::json& values = aSafeSpacing.at( row );
121
122 if( !values.is_array() )
123 continue;
124
125 for( size_t col = 0; col <= row && col < values.size(); ++col )
126 {
127 if( !values.at( col ).is_number() )
128 continue;
129
130 double value = values.at( col );
131 wxString mil = wxString::FromUTF8( FormatDouble2Str( value ) );
132 wxString mm = safeSpacingToMm( value );
133
134 rules += wxString::Format( wxS( "# %s ↔ %s: %s mil (%s mm)\n" ), safeSpacingNames[row],
135 safeSpacingNames[col], mil, mm );
136
137 if( value <= 0 || safeSpacingConditions[row].empty() || safeSpacingConditions[col].empty()
138 || ( row == 0 && col == 0 ) )
139 {
140 continue;
141 }
142 wxString conditionA = safeSpacingConditions[row];
143 wxString conditionB = safeSpacingConditions[col];
144 conditionB.Replace( wxS( "A." ), wxS( "B." ) );
145
146 rules += wxString::Format( wxS( "\n(rule \"EasyEDA safe spacing: %s ↔ %s\"\n"
147 " (condition \"(%s) && (%s)\")\n"
148 " (constraint clearance (min %smm)))\n" ),
149 safeSpacingNames[row], safeSpacingNames[col], conditionA, conditionB, mm );
150 }
151 }
152
153 constexpr size_t boardOutline = 11;
154
155 if( boardOutline < aSafeSpacing.size() && aSafeSpacing.at( boardOutline ).is_array() )
156 {
157 const nlohmann::json& values = aSafeSpacing.at( boardOutline );
158
159 for( size_t index : { 0u, 1u, 2u, 5u } )
160 {
161 if( index >= values.size() || !values.at( index ).is_number() || values.at( index ).get<double>() <= 0 )
162 {
163 continue;
164 }
165
166 wxString mm = safeSpacingToMm( values.at( index ) );
167
168 rules += wxString::Format( wxS( "\n(rule \"EasyEDA safe spacing: %s ↔ Board Outline\"\n"
169 " (condition \"(%s)\")\n"
170 " (constraint edge_clearance (min %smm)))\n" ),
172 }
173 }
174
175 return rules;
176}
177
178
179static const int SHAPE_JOIN_DISTANCE = pcbIUScale.mmToIU( 1.5 );
180
181
183 m_board( aBoard ),
184 m_v2Parser( aBoard, aProgressReporter )
185{
186}
187
188
189static void V3AlignText( EDA_TEXT* text, int align );
190static int V3AlignToOriginCode( const wxString& aAlign );
191
192
193std::unique_ptr<PAD> PCB_IO_EASYEDAPRO_V3_PARSER::createV3PAD( FOOTPRINT* aFootprint, const V3_ROW& aRow )
194{
195 int layer = V3GetInt( aRow.inner, "layerId", 1 );
196 PCB_LAYER_ID klayer = m_v2Parser.LayerToKi( layer );
197
198 wxString padNumber = V3GetString( aRow.inner, "num" );
199
201 center.x = V3GetDouble( aRow.inner, "centerX" );
202 center.y = V3GetDouble( aRow.inner, "centerY" );
203
204 double orientation = V3GetDouble( aRow.inner, "padAngle" );
205
206 nlohmann::json holeObj = aRow.inner.value( "hole", nlohmann::json() );
207 nlohmann::json padDef = aRow.inner.value( "defaultPad", nlohmann::json() );
208
209 std::unique_ptr<PAD> pad = std::make_unique<PAD>( aFootprint );
210
211 pad->SetNumber( padNumber );
213 pad->SetOrientationDegrees( orientation );
214 pad->SetPadstackMode( PADSTACK::MODE::NORMAL );
215
216 // Process hole
217 bool hasHole = false;
218
219 if( holeObj.is_object() )
220 {
221 wxString holeType = V3GetString( holeObj, "holeType", wxS( "ROUND" ) );
222
223 if( holeType == wxS( "RECT" ) )
224 holeType = wxS( "SLOT" );
225
226 if( holeType != wxS( "ROUND" ) && holeType != wxS( "SLOT" ) )
227 holeType = wxS( "ROUND" );
228
229 VECTOR2D drill;
230 drill.x = V3GetDouble( holeObj, "width" );
231 drill.y = V3GetDouble( holeObj, "height" );
232
233 if( drill.x > 0 || drill.y > 0 )
234 {
235 hasHole = true;
236
237 double drill_dir = V3GetDouble( aRow.inner, "relativeAngle" );
238
239 double deg = EDA_ANGLE( drill_dir, DEGREES_T ).Normalize90().AsDegrees();
240
241 if( std::abs( deg ) >= 45 )
242 std::swap( drill.x, drill.y );
243
244 if( holeType == wxS( "SLOT" ) )
245 pad->SetDrillShape( PAD_DRILL_SHAPE::OBLONG );
246
247 pad->SetDrillSize( PCB_IO_EASYEDAPRO_PARSER::ScaleSize( drill ) );
248 pad->SetLayerSet( PAD::PTHMask() );
249 pad->SetAttribute( PAD_ATTRIB::PTH );
250 }
251 }
252
253 if( !hasHole )
254 {
255 if( klayer == F_Cu )
256 pad->SetLayerSet( PAD::SMDMask() );
257 else if( klayer == B_Cu )
258 pad->SetLayerSet( PAD::SMDMask().FlipStandardLayers() );
259
260 pad->SetAttribute( PAD_ATTRIB::SMD );
261 }
262
263 // Process pad shape
264 if( padDef.is_object() )
265 {
266 wxString padType = V3GetString( padDef, "padType", wxS( "RECT" ) );
267
268 if( padType == wxS( "RECT" ) )
269 {
270 VECTOR2D size;
271 size.x = V3GetDouble( padDef, "width", 1.0 );
272 size.y = V3GetDouble( padDef, "height", 1.0 );
273 double radius = V3GetDouble( padDef, "radius" );
274 double radiusRatio = std::clamp( radius, 0.0, 100.0 ) / 100 / 2;
275
277
278 if( radiusRatio == 0 )
280 else
281 {
283 pad->SetRoundRectRadiusRatio( PADSTACK::ALL_LAYERS, radiusRatio );
284 }
285 }
286 else if( padType == wxS( "ELLIPSE" ) )
287 {
288 VECTOR2D size;
289 size.x = V3GetDouble( padDef, "width", 1.0 );
290 size.y = V3GetDouble( padDef, "height", 1.0 );
291
293 pad->SetShape( PADSTACK::ALL_LAYERS, size.x == size.y ? PAD_SHAPE::CIRCLE : PAD_SHAPE::OVAL );
294 }
295 else if( padType == wxS( "OVAL" ) )
296 {
297 VECTOR2D size;
298 size.x = V3GetDouble( padDef, "width", 1.0 );
299 size.y = V3GetDouble( padDef, "height", 1.0 );
300
303 }
304 else if( padType == wxS( "POLY" ) || padType == wxS( "POLYGON" ) )
305 {
307 pad->SetAnchorPadShape( PADSTACK::ALL_LAYERS, PAD_SHAPE::CIRCLE );
308 pad->SetSize( PADSTACK::ALL_LAYERS, { 1, 1 } );
309
310 nlohmann::json polyData = padDef.value( "path", nlohmann::json() );
311
312 std::vector<std::unique_ptr<PCB_SHAPE>> results = m_v2Parser.ParsePoly( aFootprint, polyData, true, false );
313
314 for( auto& shape : results )
315 {
316 shape->SetLayer( klayer );
317 shape->SetWidth( 0 );
318 shape->Move( -pad->GetPosition() );
319 pad->AddPrimitive( PADSTACK::ALL_LAYERS, shape.release() );
320 }
321 }
322 }
323 else
324 {
327 }
328
329 if( hasHole )
330 PCB_IO_EASYEDAPRO_PARSER::SizeHoleOnlyPad( *pad, aRow.inner.value( "plated", true ) );
331
332 pad->SetThermalSpokeAngle( ANGLE_90 );
333
334 return pad;
335}
336
337
339{
340 int layer = V3GetInt( aRow.inner, "layerId", 3 );
341 PCB_LAYER_ID klayer = m_v2Parser.LayerToKi( layer );
342
344 location.x = V3GetDouble( aRow.inner, "x" );
345 location.y = V3GetDouble( aRow.inner, "y" );
346
347 wxString string = V3GetString( aRow.inner, "text" );
348 wxString font = V3GetString( aRow.inner, "fontFamily", wxS( "default" ) );
349
350 double height = V3GetDouble( aRow.inner, "fontSize", 45.0 );
351 double strokew = V3GetDouble( aRow.inner, "strokeWidth", 6.0 );
352
353 wxString originStr = V3GetString( aRow.inner, "origin", wxS( "LEFT_BOTTOM" ) );
354 int align = V3AlignToOriginCode( originStr );
355 double angle = V3GetDouble( aRow.inner, "angle" );
356 int inverted = V3GetBool( aRow.inner, "reverse" ) ? 1 : 0;
357 int mirror = V3GetBool( aRow.inner, "mirror" ) ? 1 : 0;
358
359 PCB_TEXT* text = new PCB_TEXT( aContainer );
360
361 text->SetText( string );
362 text->SetLayer( klayer );
364 text->SetIsKnockout( inverted );
365 text->SetTextThickness( PCB_IO_EASYEDAPRO_PARSER::ScaleSize( strokew ) );
366 text->SetTextSize( VECTOR2D( PCB_IO_EASYEDAPRO_PARSER::ScaleSize( height * 0.6 ),
367 PCB_IO_EASYEDAPRO_PARSER::ScaleSize( height * 0.7 ) ) );
368
369 if( font != wxS( "default" ) )
370 text->SetFont( KIFONT::FONT::GetFont( font ) );
371
372 V3AlignText( text, align );
373
374 if( IsBackLayer( klayer ) ^ !!mirror )
375 {
376 text->SetMirrored( true );
377 text->SetTextAngleDegrees( -angle );
378 }
379 else
380 {
381 text->SetTextAngleDegrees( angle );
382 }
383
384 return text;
385}
386
387
388std::unique_ptr<FOOTPRINT>
389PCB_IO_EASYEDAPRO_V3_PARSER::ParseFootprint( const std::map<wxString, EASYEDAPRO::BLOB>& aBlobMap,
390 const V3_DOC_RAW& aDoc )
391{
392 std::unique_ptr<FOOTPRINT> footprintPtr = std::make_unique<FOOTPRINT>( m_board );
393 FOOTPRINT* footprint = footprintPtr.get();
394
395 const VECTOR2I defaultTextSize( pcbIUScale.mmToIU( 1.0 ), pcbIUScale.mmToIU( 1.0 ) );
396 const int defaultTextThickness( pcbIUScale.mmToIU( 0.15 ) );
397 VECTOR2I canvasOrigin( 0, 0 );
398
399 for( PCB_FIELD* field : footprint->GetFields() )
400 {
401 field->SetTextSize( defaultTextSize );
402 field->SetTextThickness( defaultTextThickness );
403 }
404
405 for( const V3_ROW& row : aDoc.rows )
406 {
407 if( row.type == wxS( "CANVAS" ) )
408 {
410 VECTOR2D( V3GetDouble( row.inner, "originX" ), V3GetDouble( row.inner, "originY" ) ) );
411 }
412 else if( row.type == wxS( "POLY" ) )
413 {
414 int layer = V3GetInt( row.inner, "layerId", 1 );
415 PCB_LAYER_ID klayer = m_v2Parser.LayerToKi( layer );
416 double thickness = V3GetDouble( row.inner, "width" );
417
418 nlohmann::json polyData = row.inner.value( "path", nlohmann::json::array() );
419
420 std::vector<std::unique_ptr<PCB_SHAPE>> results = m_v2Parser.ParsePoly( footprint, polyData, false, false );
421
422 for( auto& shape : results )
423 {
424 shape->SetLayer( klayer );
425 shape->SetWidth( PCB_IO_EASYEDAPRO_PARSER::ScaleSize( thickness ) );
426 footprint->Add( shape.release(), ADD_MODE::APPEND );
427 }
428 }
429 else if( row.type == wxS( "PAD" ) )
430 {
431 std::unique_ptr<PAD> pad = createV3PAD( footprint, row );
432 footprint->Add( pad.release(), ADD_MODE::APPEND );
433 }
434 else if( row.type == wxS( "FILL" ) )
435 {
436 int layer = V3GetInt( row.inner, "layerId", 1 );
437 PCB_LAYER_ID klayer = m_v2Parser.LayerToKi( layer );
438
439 nlohmann::json polyDataList = row.inner.value( "path", nlohmann::json::array() );
440
441 if( !polyDataList.is_null() && !polyDataList.empty() )
442 {
443 if( !polyDataList.at( 0 ).is_array() )
444 polyDataList = nlohmann::json::array( { polyDataList } );
445
446 std::vector<SHAPE_LINE_CHAIN> contours;
447
448 for( nlohmann::json& polyData : polyDataList )
449 {
450 SHAPE_LINE_CHAIN contour = m_v2Parser.ParseContour( polyData, false );
451 contour.SetClosed( true );
452 contours.push_back( contour );
453 }
454
455 SHAPE_POLY_SET polySet;
456
457 for( SHAPE_LINE_CHAIN& contour : contours )
458 polySet.AddOutline( contour );
459
460 polySet.RebuildHolesFromContours();
461
462 std::unique_ptr<PCB_GROUP> group;
463
464 if( polySet.OutlineCount() > 1 )
465 group = std::make_unique<PCB_GROUP>( footprint );
466
467 for( const SHAPE_POLY_SET::POLYGON& poly : polySet.CPolygons() )
468 {
469 std::unique_ptr<PCB_SHAPE> shape = std::make_unique<PCB_SHAPE>( footprint, SHAPE_T::POLY );
470
471 shape->SetFilled( true );
472 shape->SetPolyShape( poly );
473 shape->SetLayer( klayer );
474 shape->SetWidth( 0 );
475
476 if( group )
477 group->AddItem( shape.get() );
478
479 footprint->Add( shape.release(), ADD_MODE::APPEND );
480 }
481
482 if( group )
483 footprint->Add( group.release(), ADD_MODE::APPEND );
484 }
485 }
486 else if( row.type == wxS( "ATTR" ) )
487 {
488 wxString key = V3GetString( row.inner, "key" );
489 wxString value = V3JsonToString( row.inner.value( "value", nlohmann::json() ) );
490
491 if( key == wxS( "Designator" ) )
492 footprint->GetField( FIELD_T::REFERENCE )->SetText( value );
493 }
494 else if( row.type == wxS( "REGION" ) )
495 {
496 int layer = V3GetInt( row.inner, "layerId", 1 );
497 PCB_LAYER_ID klayer = m_v2Parser.LayerToKi( layer );
498
499 nlohmann::json polyDataList = row.inner.value( "path", nlohmann::json::array() );
500 nlohmann::json prohibitTypes = row.inner.value( "prohibitType", nlohmann::json::array() );
501
502 std::set<int> flags;
503
504 if( prohibitTypes.is_array() )
505 {
506 for( const auto& pt : prohibitTypes )
507 {
508 wxString ptStr;
509
510 if( pt.is_string() )
511 ptStr = pt.get<std::string>();
512
513 if( ptStr == wxS( "COMPONENT" ) )
514 flags.insert( 2 );
515 else if( ptStr == wxS( "TRACK" ) )
516 flags.insert( 5 );
517 else if( ptStr == wxS( "COPPER" ) )
518 flags.insert( 6 );
519 else if( ptStr == wxS( "VIA" ) )
520 flags.insert( 7 );
521 else if( ptStr == wxS( "FILL" ) || ptStr == wxS( "PLANE" ) )
522 flags.insert( 8 );
523 }
524 }
525
526 for( nlohmann::json& polyData : polyDataList )
527 {
528 SHAPE_POLY_SET polySet;
529
530 std::vector<std::unique_ptr<PCB_SHAPE>> results =
531 m_v2Parser.ParsePoly( nullptr, polyData, true, false );
532
533 for( auto& shape : results )
534 {
535 shape->SetFilled( true );
536 shape->TransformShapeToPolygon( polySet, klayer, 0, ARC_HIGH_DEF, ERROR_INSIDE, true );
537 }
538
539 polySet.Simplify();
540
541 std::unique_ptr<ZONE> zone = std::make_unique<ZONE>( footprint );
542
543 zone->SetIsRuleArea( true );
544 zone->SetDoNotAllowFootprints( !!flags.count( 2 ) );
545 zone->SetDoNotAllowZoneFills( !!flags.count( 7 ) || !!flags.count( 6 ) || !!flags.count( 8 ) );
546 zone->SetDoNotAllowPads( !!flags.count( 7 ) );
547 zone->SetDoNotAllowTracks( !!flags.count( 7 ) || !!flags.count( 5 ) );
548 zone->SetDoNotAllowVias( !!flags.count( 7 ) );
549
550 zone->SetLayer( klayer );
551 zone->Outline()->Append( polySet );
552
553 footprint->Add( zone.release(), ADD_MODE::APPEND );
554 }
555 }
556 else if( row.type == wxS( "IMAGE" ) )
557 {
558 int layer = V3GetInt( row.inner, "layerId", 1 );
559 PCB_LAYER_ID klayer = m_v2Parser.LayerToKi( layer );
560
561 VECTOR2D start;
562 start.x = V3GetDouble( row.inner, "startX" );
563 start.y = V3GetDouble( row.inner, "startY" );
564
565 VECTOR2D size;
566 size.x = V3GetDouble( row.inner, "width" );
567 size.y = V3GetDouble( row.inner, "height" );
568
569 double angle = V3GetDouble( row.inner, "angle" );
570 bool mirror = V3GetBool( row.inner, "mirror" );
571 nlohmann::json polyDataList = row.inner.value( "path", nlohmann::json::array() );
572
573 BOX2I bbox;
574 std::vector<SHAPE_LINE_CHAIN> contours;
575 for( nlohmann::json& polyData : polyDataList )
576 {
577 SHAPE_LINE_CHAIN contour = m_v2Parser.ParseContour( polyData, false );
578 contour.SetClosed( true );
579
580 contours.push_back( contour );
581
582 bbox.Merge( contour.BBox() );
583 }
584
585 if( bbox.GetSize().x == 0 || bbox.GetSize().y == 0 )
586 continue;
587
590
591 SHAPE_POLY_SET polySet;
592
593 for( SHAPE_LINE_CHAIN& contour : contours )
594 {
595 for( int i = 0; i < contour.PointCount(); i++ )
596 {
597 VECTOR2I pt = contour.CPoint( i );
598 contour.SetPoint( i, VECTOR2I( pt.x * scale.x, pt.y * scale.y ) );
599 }
600
601 polySet.AddOutline( contour );
602 }
603
604 polySet.RebuildHolesFromContours();
605
606 std::unique_ptr<PCB_GROUP> group;
607
608 if( polySet.OutlineCount() > 1 )
609 group = std::make_unique<PCB_GROUP>( footprint );
610
611 BOX2I polyBBox = polySet.BBox();
612
613 for( const SHAPE_POLY_SET::POLYGON& poly : polySet.CPolygons() )
614 {
615 std::unique_ptr<PCB_SHAPE> shape = std::make_unique<PCB_SHAPE>( footprint, SHAPE_T::POLY );
616
617 shape->SetFilled( true );
618 shape->SetPolyShape( poly );
619 shape->SetLayer( klayer );
620 shape->SetWidth( 0 );
621
622 shape->Move( PCB_IO_EASYEDAPRO_PARSER::ScalePos( start ) - polyBBox.GetOrigin() );
623 shape->Rotate( PCB_IO_EASYEDAPRO_PARSER::ScalePos( start ), EDA_ANGLE( angle, DEGREES_T ) );
624
625 if( IsBackLayer( klayer ) ^ mirror )
626 {
628 shape->Mirror( PCB_IO_EASYEDAPRO_PARSER::ScalePos( start ), flipDirection );
629 }
630
631 if( group )
632 group->AddItem( shape.get() );
633
634 footprint->Add( shape.release(), ADD_MODE::APPEND );
635 }
636
637 if( group )
638 footprint->Add( group.release(), ADD_MODE::APPEND );
639 }
640 else if( row.type == wxS( "OBJ" ) )
641 {
642 int layer = V3GetInt( row.inner, "layerId", 1 );
643 PCB_LAYER_ID klayer = m_v2Parser.LayerToKi( layer );
644
645 VECTOR2D start;
646 start.x = V3GetDouble( row.inner, "startX" );
647 start.y = V3GetDouble( row.inner, "startY" );
648
649 VECTOR2D size;
650 size.x = V3GetDouble( row.inner, "width" );
651 size.y = V3GetDouble( row.inner, "height" );
652
653 double angle = V3GetDouble( row.inner, "angle" );
654 bool mirror = V3GetBool( row.inner, "mirror" );
655 wxString imageUrl = V3GetString( row.inner, "path" );
656
657 if( imageUrl.empty() )
658 continue;
659
660 wxString mimeType, base64Data;
661
662 if( imageUrl.BeforeFirst( ':' ) == wxS( "blob" ) )
663 {
664 wxString objectId = imageUrl.AfterLast( ':' );
665
666 if( auto blob = get_opt( aBlobMap, objectId ) )
667 {
668 wxString blobUrl = blob->url;
669
670 if( blobUrl.BeforeFirst( ':' ) == wxS( "data" ) )
671 {
672 wxArrayString paramsArr = wxSplit( blobUrl.AfterFirst( ':' ).BeforeFirst( ',' ), ';', '\0' );
673
674 base64Data = blobUrl.AfterFirst( ',' );
675
676 if( paramsArr.size() > 0 )
677 mimeType = paramsArr[0];
678 }
679 }
680 }
681
684
685 if( mimeType.empty() || base64Data.empty() )
686 continue;
687
688 wxMemoryBuffer buf = wxBase64Decode( base64Data );
689
690 if( mimeType == wxS( "image/svg+xml" ) )
691 {
692 // Not yet supported by EasyEDA
693 }
694 else
695 {
696 VECTOR2D kcenter = kstart + ksize / 2;
697
698 std::unique_ptr<PCB_REFERENCE_IMAGE> bitmap =
699 std::make_unique<PCB_REFERENCE_IMAGE>( footprint, kcenter, klayer );
700 REFERENCE_IMAGE& refImage = bitmap->GetReferenceImage();
701
702 wxImage::SetDefaultLoadFlags( wxImage::GetDefaultLoadFlags() & ~wxImage::Load_Verbose );
703
704 if( refImage.ReadImageFile( buf ) )
705 {
706 double scaleFactor = PCB_IO_EASYEDAPRO_PARSER::ScaleSize( size.x ) / refImage.GetSize().x;
707 refImage.SetImageScale( scaleFactor );
708
709 // TODO: support non-90-deg angles
710 bitmap->Rotate( kstart, EDA_ANGLE( angle, DEGREES_T ) );
711
712 if( mirror )
713 {
714 int x = bitmap->GetPosition().x;
715 MIRROR( x, KiROUND( kstart.x ) );
716 bitmap->SetX( x );
717
719 }
720
721 footprint->Add( bitmap.release(), ADD_MODE::APPEND );
722 }
723 }
724 }
725 else if( row.type == wxS( "STRING" ) )
726 {
727 footprint->Add( createV3Text( footprint, row ), ADD_MODE::APPEND );
728 }
729 }
730
731 // 3D models are applied by the caller from the component's Device ATTR (board import)
732 // or a matching library device (FootprintLoad). Do not pick an arbitrary device here:
733 // footprints are often shared by devices with different models.
734
735 // Heal board outlines
736 std::vector<PCB_SHAPE*> edgeShapes;
737
738 for( BOARD_ITEM* item : footprint->GraphicalItems() )
739 {
740 if( item->IsOnLayer( Edge_Cuts ) && item->Type() == PCB_SHAPE_T )
741 edgeShapes.push_back( static_cast<PCB_SHAPE*>( item ) );
742 }
743
745
746 // Build courtyard if missing
747 if( !footprint->IsOnLayer( F_CrtYd ) )
748 {
749 BOX2I bbox = footprint->GetLayerBoundingBox( { F_Cu, F_Fab, F_Paste, F_Mask, Edge_Cuts } );
750 bbox.Inflate( pcbIUScale.mmToIU( 0.25 ) );
751
752 std::unique_ptr<PCB_SHAPE> shape = std::make_unique<PCB_SHAPE>( footprint, SHAPE_T::RECTANGLE );
753
754 shape->SetWidth( pcbIUScale.mmToIU( DEFAULT_COURTYARD_WIDTH ) );
755 shape->SetLayer( F_CrtYd );
756 shape->SetStart( bbox.GetOrigin() );
757 shape->SetEnd( bbox.GetEnd() );
758
759 footprint->Add( shape.release(), ADD_MODE::APPEND );
760 }
761
762 // Add F_Fab reference text if missing
763 bool hasFabRef = false;
764
765 for( BOARD_ITEM* item : footprint->GraphicalItems() )
766 {
767 if( item->Type() == PCB_TEXT_T && item->IsOnLayer( F_Fab ) )
768 {
769 if( static_cast<PCB_TEXT*>( item )->GetText() == wxT( "${REFERENCE}" ) )
770 {
771 hasFabRef = true;
772 break;
773 }
774 }
775 }
776
777 if( !hasFabRef )
778 {
779 int c_refTextSize = pcbIUScale.mmToIU( 0.5 );
780 int c_refTextThickness = pcbIUScale.mmToIU( 0.1 );
781 std::unique_ptr<PCB_TEXT> refText = std::make_unique<PCB_TEXT>( footprint );
782
783 refText->SetLayer( F_Fab );
784 refText->SetTextSize( VECTOR2I( c_refTextSize, c_refTextSize ) );
785 refText->SetTextThickness( c_refTextThickness );
786 refText->SetText( wxT( "${REFERENCE}" ) );
787
788 footprint->Add( refText.release(), ADD_MODE::APPEND );
789 }
790
791 footprint->MoveAnchorPosition( -canvasOrigin );
792
793 return footprintPtr;
794}
795
796
797static void V3AlignText( EDA_TEXT* text, int align )
798{
799 switch( align )
800 {
801 case 1:
802 text->SetVertJustify( GR_TEXT_V_ALIGN_TOP );
803 text->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
804 break;
805 case 2:
806 text->SetVertJustify( GR_TEXT_V_ALIGN_CENTER );
807 text->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
808 break;
809 case 3:
810 text->SetVertJustify( GR_TEXT_V_ALIGN_BOTTOM );
811 text->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
812 break;
813 case 4:
814 text->SetVertJustify( GR_TEXT_V_ALIGN_TOP );
815 text->SetHorizJustify( GR_TEXT_H_ALIGN_CENTER );
816 break;
817 case 5:
818 text->SetVertJustify( GR_TEXT_V_ALIGN_CENTER );
819 text->SetHorizJustify( GR_TEXT_H_ALIGN_CENTER );
820 break;
821 case 6:
822 text->SetVertJustify( GR_TEXT_V_ALIGN_BOTTOM );
823 text->SetHorizJustify( GR_TEXT_H_ALIGN_CENTER );
824 break;
825 case 7:
826 text->SetVertJustify( GR_TEXT_V_ALIGN_TOP );
827 text->SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT );
828 break;
829 case 8:
830 text->SetVertJustify( GR_TEXT_V_ALIGN_CENTER );
831 text->SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT );
832 break;
833 case 9:
834 text->SetVertJustify( GR_TEXT_V_ALIGN_BOTTOM );
835 text->SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT );
836 break;
837 }
838}
839
840
841static int V3AlignToOriginCode( const wxString& aAlign )
842{
843 if( aAlign == wxS( "LEFT_TOP" ) )
844 return 1;
845 if( aAlign == wxS( "LEFT_CENTER" ) || aAlign == wxS( "LEFT_MIDDLE" ) )
846 return 2;
847 if( aAlign == wxS( "LEFT_BOTTOM" ) )
848 return 3;
849 if( aAlign == wxS( "CENTER_TOP" ) )
850 return 4;
851 if( aAlign == wxS( "CENTER_MIDDLE" ) || aAlign == wxS( "CENTER_CENTER" ) )
852 return 5;
853 if( aAlign == wxS( "CENTER_BOTTOM" ) )
854 return 6;
855 if( aAlign == wxS( "RIGHT_TOP" ) )
856 return 7;
857 if( aAlign == wxS( "RIGHT_CENTER" ) || aAlign == wxS( "RIGHT_MIDDLE" ) )
858 return 8;
859 if( aAlign == wxS( "RIGHT_BOTTOM" ) )
860 return 9;
861
862 return 3; // default: LEFT_BOTTOM
863}
864
865
866void PCB_IO_EASYEDAPRO_V3_PARSER::ParseBoard( BOARD* aBoard, const nlohmann::json& aProject,
867 std::map<wxString, std::unique_ptr<FOOTPRINT>>& aFootprintMap,
868 const std::map<wxString, EASYEDAPRO::BLOB>& aBlobMap,
869 const std::multimap<wxString, EASYEDAPRO::POURED>& aPouredMap,
870 const V3_DOC_RAW& aDoc, const wxString& aFpLibName )
871{
872 // Structures to collect component-related rows for second-pass placement
873 struct COMP_DATA
874 {
875 int layer = 1;
876 VECTOR2D pos;
877 double angle = 0;
878 std::map<wxString, wxString> attrs;
879 };
880
881 struct ATTR_DATA
882 {
883 wxString parentId;
884 int layer = 3;
885 double x = 0;
886 double y = 0;
887 bool hasPos = false;
888 wxString key;
889 wxString value;
890 bool keyVisible = false;
891 bool valVisible = false;
892 wxString fontName;
893 double fontSize = 45.0;
894 double strokeWidth = 6.0;
895 wxString origin;
896 double angle = 0;
897 int inverted = 0;
898 int mirror = 0;
899 };
900
901 struct PAD_NET_DATA
902 {
903 wxString compId;
904 wxString padNumber;
905 wxString padNet;
906 };
907
908 std::map<wxString, COMP_DATA> componentMap;
909 std::multimap<wxString, ATTR_DATA> attrMap;
910 std::multimap<wxString, PAD_NET_DATA> padNetMap;
911
912 std::multimap<wxString, EASYEDAPRO::POURED> boardPouredMap = aPouredMap;
913 std::map<wxString, ZONE*> poursToFill;
915
916 m_safeSpacing = nlohmann::json();
917
918 for( const V3_ROW& row : aDoc.rows )
919 {
920 if( row.type != wxS( "RULE" ) || V3GetString( row.inner, "ruleState" ) != wxS( "DEFAULT" ) )
921 continue;
922
923 nlohmann::json idArr = V3ParseIdArray( row.id );
924
925 if( !idArr.is_array() || idArr.size() < 3 || V3JsonToString( idArr[1] ) != wxS( "SAFE" ) )
926 continue;
927
928 nlohmann::json safeSpacing =
929 row.inner.value( "ruleContext", nlohmann::json() ).value( "safeSpacing", nlohmann::json::array() );
930
931 if( safeSpacing.is_array() && !safeSpacing.empty() )
932 {
934 safeSpacing[0].value( "content", nlohmann::json::array() ) );
935 break;
936 }
937 }
938
939 auto zoneClearance =
940 [&]() -> int
941 {
942 if( m_safeSpacing.is_array() && m_safeSpacing.size() > 7 && m_safeSpacing.at( 7 ).is_array()
943 && !m_safeSpacing.at( 7 ).empty() && m_safeSpacing.at( 7 ).at( 0 ).is_number() )
944 {
945 const int clearance = PCB_IO_EASYEDAPRO_PARSER::ScaleSize( m_safeSpacing.at( 7 ).at( 0 ).get<double>() );
946
947 if( clearance > 0 )
948 return clearance;
949 }
950
951 return bds.m_MinClearance;
952 };
953
954 const int zoneMinThickness = bds.GetDefaultZoneSettings().m_ZoneMinThickness;
955
956
957 for( const V3_ROW& row : aDoc.rows )
958 {
959 if( row.type == wxS( "LAYER" ) )
960 {
961 int layer = V3GetInt( row.inner, "layerId", -1 );
962
963 if( layer < 0 )
964 {
965 nlohmann::json id = V3ParseIdArray( row.id );
966
967 if( id.is_array() && id.size() > 1 )
968 {
969 wxString layerId = V3JsonToString( id.at( 1 ) );
970 long parsedLayer;
971
972 if( layerId.ToLong( &parsedLayer ) )
973 layer = parsedLayer;
974 }
975 }
976
977 if( layer < 0 )
978 continue;
979
980 PCB_LAYER_ID klayer = m_v2Parser.LayerToKi( layer );
981
982 bool use = V3GetBool( row.inner, "use", true );
983 wxString layerName = V3GetString( row.inner, "layerName" );
984
985 if( use )
986 {
987 LSET blayers = aBoard->GetEnabledLayers();
988 blayers.set( klayer );
989 aBoard->SetEnabledLayers( blayers );
990
991 if( !layerName.IsEmpty() )
992 aBoard->SetLayerName( klayer, layerName );
993 }
994 }
995 else if( row.type == wxS( "NET" ) )
996 {
997 nlohmann::json idArr = V3ParseIdArray( row.id );
998 wxString netname;
999
1000 if( idArr.is_array() && idArr.size() > 1 )
1001 netname = V3JsonToString( idArr[1] );
1002
1003 if( !netname.IsEmpty() )
1004 {
1005 aBoard->FindOrAddNet( netname, aBoard->GetNetCount() + 1, ADD_MODE::APPEND );
1006 }
1007 }
1008 else if( row.type == wxS( "RULE" ) )
1009 {
1010 nlohmann::json idArr = V3ParseIdArray( row.id );
1011
1012 if( !idArr.is_array() || idArr.size() < 3 )
1013 continue;
1014
1015 wxString ruleType = V3JsonToString( idArr[1] );
1016 bool isDefault = V3GetString( row.inner, "ruleState" ) == wxS( "DEFAULT" );
1017
1018 nlohmann::json context = row.inner.value( "ruleContext", nlohmann::json() );
1019
1020 if( ruleType == wxS( "TRACK" ) && isDefault )
1021 {
1022 nlohmann::json track = context.value( "track", nlohmann::json() );
1023 nlohmann::json content = track.value( "content", nlohmann::json::array() );
1024
1025 if( content.is_array() && !content.empty() )
1026 {
1027 double minVal = V3GetDouble( content[0], "stroMin" );
1029 aBoard->m_LegacyDesignSettingsLoaded = true;
1030 }
1031 }
1032 else if( ruleType == wxS( "SAFE" ) && isDefault )
1033 {
1034 nlohmann::json safeSpacing = context.value( "safeSpacing", nlohmann::json::array() );
1035
1036 if( safeSpacing.is_array() && !safeSpacing.empty() )
1037 {
1038 nlohmann::json content = PCB_IO_EASYEDAPRO_PARSER::NormalizeSafeSpacing(
1039 safeSpacing[0].value( "content", nlohmann::json::array() ) );
1040
1041 if( m_safeSpacing.is_null() )
1042 m_safeSpacing = content;
1043
1044 // EasyEDA Pro labels this array "mm", but its values are always mils. The
1045 // lower-triangular matrix starts with the track-to-track clearance, which maps
1046 // to KiCad's global minimum clearance; the remaining object-specific values
1047 // cannot be represented by that single board setting.
1048 if( content.is_array() && !content.empty() && content.at( 0 ).is_array() && !content.at( 0 ).empty()
1049 && content.at( 0 ).at( 0 ).is_number() )
1050 {
1051 double trackClearance = content.at( 0 ).at( 0 );
1052
1053 if( trackClearance > 0 )
1054 {
1055 int clearance = PCB_IO_EASYEDAPRO_PARSER::ScaleSize( trackClearance );
1058 aBoard->m_LegacyDesignSettingsLoaded = true;
1059 aBoard->m_LegacyNetclassesLoaded = true;
1060 }
1061 }
1062 double minEdgeClearance = 0;
1063
1064 if( content.size() > 11 && content.at( 11 ).is_array() )
1065 {
1066 for( const nlohmann::json& value : content.at( 11 ) )
1067 {
1068 if( value.is_number() && value.get<double>() > 0
1069 && ( minEdgeClearance == 0 || value.get<double>() < minEdgeClearance ) )
1070 {
1071 minEdgeClearance = value;
1072 }
1073 }
1074 }
1075
1076 if( minEdgeClearance > 0 )
1077 {
1079 aBoard->m_LegacyDesignSettingsLoaded = true;
1080 }
1081 }
1082 }
1083 }
1084 else if( row.type == wxS( "VIA" ) )
1085 {
1087 center.x = V3GetDouble( row.inner, "centerX" );
1088 center.y = V3GetDouble( row.inner, "centerY" );
1089
1090 double drill = V3GetDouble( row.inner, "holeDiameter" );
1091 double dia = V3GetDouble( row.inner, "viaDiameter" );
1092 wxString netname = V3GetString( row.inner, "netName" );
1093
1094 std::unique_ptr<PCB_VIA> via = std::make_unique<PCB_VIA>( aBoard );
1095
1096 via->SetPadstackMode( PADSTACK::MODE::NORMAL );
1098 via->SetDrill( PCB_IO_EASYEDAPRO_PARSER::ScaleSize( drill ) );
1100 via->SetNet( aBoard->FindNet( netname ) );
1101
1102 aBoard->Add( via.release(), ADD_MODE::APPEND );
1103 }
1104 else if( row.type == wxS( "LINE" ) )
1105 {
1106 int layer = V3GetInt( row.inner, "layerId", 1 );
1107 PCB_LAYER_ID klayer = m_v2Parser.LayerToKi( layer );
1108
1109 VECTOR2D start;
1110 start.x = V3GetDouble( row.inner, "startX" );
1111 start.y = V3GetDouble( row.inner, "startY" );
1112
1113 VECTOR2D end;
1114 end.x = V3GetDouble( row.inner, "endX" );
1115 end.y = V3GetDouble( row.inner, "endY" );
1116
1117 double width = V3GetDouble( row.inner, "width" );
1118 wxString netname = V3GetString( row.inner, "netName" );
1119
1120 std::unique_ptr<PCB_TRACK> track = std::make_unique<PCB_TRACK>( aBoard );
1121
1122 track->SetLayer( klayer );
1123 track->SetStart( PCB_IO_EASYEDAPRO_PARSER::ScalePos( start ) );
1124 track->SetEnd( PCB_IO_EASYEDAPRO_PARSER::ScalePos( end ) );
1125 track->SetWidth( PCB_IO_EASYEDAPRO_PARSER::ScaleSize( width ) );
1126 track->SetNet( aBoard->FindNet( netname ) );
1127
1128 aBoard->Add( track.release(), ADD_MODE::APPEND );
1129 }
1130 else if( row.type == wxS( "ARC" ) )
1131 {
1132 int layer = V3GetInt( row.inner, "layerId", 1 );
1133 PCB_LAYER_ID klayer = m_v2Parser.LayerToKi( layer );
1134
1135 VECTOR2D start;
1136 start.x = V3GetDouble( row.inner, "startX" );
1137 start.y = V3GetDouble( row.inner, "startY" );
1138
1139 VECTOR2D end;
1140 end.x = V3GetDouble( row.inner, "endX" );
1141 end.y = V3GetDouble( row.inner, "endY" );
1142
1143 double angle = V3GetDouble( row.inner, "angle" );
1144 double width = V3GetDouble( row.inner, "width" );
1145 wxString netname = V3GetString( row.inner, "netName" );
1146
1147 VECTOR2D delta = end - start;
1148 VECTOR2D mid = ( start + delta / 2 );
1149
1150 double ha = angle / 2;
1151 double hd = delta.EuclideanNorm() / 2;
1152 double cdist = hd / tan( DEG2RAD( ha ) );
1153 VECTOR2D center = mid + delta.Perpendicular().Resize( cdist );
1154
1155 SHAPE_ARC sarc;
1158 PCB_IO_EASYEDAPRO_PARSER::ScalePos( center ), angle >= 0, width );
1159
1160 std::unique_ptr<PCB_ARC> arc = std::make_unique<PCB_ARC>( aBoard, &sarc );
1161 arc->SetWidth( PCB_IO_EASYEDAPRO_PARSER::ScaleSize( width ) );
1162 arc->SetLayer( klayer );
1163 arc->SetNet( aBoard->FindNet( netname ) );
1164
1165 aBoard->Add( arc.release(), ADD_MODE::APPEND );
1166 }
1167 else if( row.type == wxS( "POLY" ) )
1168 {
1169 int layer = V3GetInt( row.inner, "layerId", 1 );
1170 PCB_LAYER_ID klayer = m_v2Parser.LayerToKi( layer );
1171
1172 double thickness = V3GetDouble( row.inner, "width" );
1173 nlohmann::json polyData = row.inner.value( "path", nlohmann::json::array() );
1174
1175 std::vector<std::unique_ptr<PCB_SHAPE>> results = m_v2Parser.ParsePoly( aBoard, polyData, false, false );
1176
1177 for( auto& shape : results )
1178 {
1179 shape->SetLayer( klayer );
1180 shape->SetWidth( PCB_IO_EASYEDAPRO_PARSER::ScaleSize( thickness ) );
1181
1182 aBoard->Add( shape.release(), ADD_MODE::APPEND );
1183 }
1184 }
1185 else if( row.type == wxS( "FILL" ) )
1186 {
1187 int layer = V3GetInt( row.inner, "layerId", 1 );
1188 PCB_LAYER_ID klayer = m_v2Parser.LayerToKi( layer );
1189
1190 wxString netname = V3GetString( row.inner, "netName" );
1191
1192 nlohmann::json polyDataList = row.inner.value( "path", nlohmann::json::array() );
1193
1194 if( polyDataList.is_null() || polyDataList.empty() )
1195 continue;
1196
1197 if( !polyDataList.at( 0 ).is_array() )
1198 polyDataList = nlohmann::json::array( { polyDataList } );
1199
1200 std::vector<SHAPE_LINE_CHAIN> contours;
1201
1202 for( nlohmann::json& polyData : polyDataList )
1203 {
1204 SHAPE_LINE_CHAIN contour = m_v2Parser.ParseContour( polyData, true );
1205 contour.SetClosed( true );
1206 contours.push_back( contour );
1207 }
1208
1209 SHAPE_POLY_SET zoneFillPoly;
1210
1211 for( SHAPE_LINE_CHAIN& contour : contours )
1212 zoneFillPoly.AddOutline( contour );
1213
1214 zoneFillPoly.RebuildHolesFromContours();
1215 zoneFillPoly.ClearArcs();
1216 zoneFillPoly.Fracture();
1217
1218 std::unique_ptr<ZONE> zone = std::make_unique<ZONE>( aBoard );
1219
1220 zone->SetNet( aBoard->FindNet( netname ) );
1221 zone->SetLayer( klayer );
1222 zone->Outline()->Append( SHAPE_RECT( zoneFillPoly.BBox() ).Outline() );
1223 zone->SetFilledPolysList( klayer, zoneFillPoly );
1224 zone->SetAssignedPriority( 500 );
1225 zone->SetIsFilled( true );
1226 zone->SetNeedRefill( false );
1227
1228 zone->SetLocalClearance( zoneClearance() );
1229 zone->SetMinThickness( zoneMinThickness );
1230
1231 aBoard->Add( zone.release(), ADD_MODE::APPEND );
1232 }
1233 else if( row.type == wxS( "POUR" ) )
1234 {
1235 int layer = V3GetInt( row.inner, "layerId", 1 );
1236 PCB_LAYER_ID klayer = m_v2Parser.LayerToKi( layer );
1237
1238 wxString netname = V3GetString( row.inner, "netName" );
1239 int fillOrder = V3GetInt( row.inner, "order" );
1240
1241 nlohmann::json polyDataList = row.inner.value( "path", nlohmann::json::array() );
1242
1243 std::unique_ptr<ZONE> zone = std::make_unique<ZONE>( aBoard );
1244
1245 zone->SetNet( aBoard->FindNet( netname ) );
1246 zone->SetLayer( klayer );
1247 zone->SetAssignedPriority( 500 - fillOrder );
1248 zone->SetLocalClearance( zoneClearance() );
1249 zone->SetMinThickness( zoneMinThickness );
1250
1251 for( nlohmann::json& polyData : polyDataList )
1252 {
1253 SHAPE_LINE_CHAIN contour = m_v2Parser.ParseContour( polyData, false );
1254 contour.SetClosed( true );
1255 zone->Outline()->Append( contour );
1256 }
1257
1258 wxASSERT( zone->Outline()->OutlineCount() == 1 );
1259
1260 poursToFill.emplace( row.id, zone.get() );
1261
1262 aBoard->Add( zone.release(), ADD_MODE::APPEND );
1263 }
1264 else if( row.type == wxS( "TEARDROP" ) )
1265 {
1266 int layer = V3GetInt( row.inner, "layerId", 1 );
1267 PCB_LAYER_ID klayer = m_v2Parser.LayerToKi( layer );
1268 wxString netname = V3GetString( row.inner, "netName" );
1269
1270 nlohmann::json polyData = row.inner.value( "path", nlohmann::json::array() );
1271
1272 SHAPE_LINE_CHAIN contour = m_v2Parser.ParseContour( polyData, false );
1273 contour.SetClosed( true );
1274
1275 std::unique_ptr<ZONE> zone = std::make_unique<ZONE>( aBoard );
1276
1277 zone->SetNet( aBoard->FindNet( netname ) );
1278 zone->SetLayer( klayer );
1279 zone->Outline()->Append( contour );
1280 SHAPE_POLY_SET teardropFill( contour );
1281 teardropFill.ClearArcs();
1282 zone->SetFilledPolysList( klayer, teardropFill );
1283 zone->SetNeedRefill( false );
1284 zone->SetIsFilled( true );
1285 zone->SetAssignedPriority( 600 );
1286 zone->SetLocalClearance( 0 );
1287 zone->SetMinThickness( 0 );
1288 zone->SetTeardropAreaType( TEARDROP_TYPE::TD_VIAPAD );
1289
1290 aBoard->Add( zone.release(), ADD_MODE::APPEND );
1291 }
1292 else if( row.type == wxS( "POURED" ) )
1293 {
1294 nlohmann::json idArr = V3ParseIdArray( row.id );
1295 wxString parentId;
1296
1297 if( idArr.is_array() && idArr.size() > 1 )
1298 parentId = V3JsonToString( idArr[1] );
1299
1300 nlohmann::json fills = row.inner.value( "pourFill", nlohmann::json::array() );
1301
1302 if( !fills.is_array() )
1303 continue;
1304
1305 for( const nlohmann::json& fill : fills )
1306 {
1307 EASYEDAPRO::POURED poured;
1308 poured.pouredId = V3JsonToString( fill.value( "id", nlohmann::json( row.id ) ) );
1309 poured.parentId = parentId;
1310 poured.unki = V3GetInt( fill, "strokeWidth" );
1311 poured.isPoly = V3GetBool( fill, "fill" );
1312 poured.polyData = fill.value( "path", nlohmann::json::array() );
1313
1314 boardPouredMap.emplace( poured.parentId, poured );
1315 }
1316 }
1317 else if( row.type == wxS( "REGION" ) )
1318 {
1319 int layer = V3GetInt( row.inner, "layerId", 1 );
1320 PCB_LAYER_ID klayer = m_v2Parser.LayerToKi( layer );
1321
1322 nlohmann::json polyDataList = row.inner.value( "path", nlohmann::json::array() );
1323 nlohmann::json prohibitTypes = row.inner.value( "prohibitType", nlohmann::json::array() );
1324
1325 std::set<int> flags;
1326
1327 if( prohibitTypes.is_array() )
1328 {
1329 for( const auto& pt : prohibitTypes )
1330 {
1331 wxString ptStr;
1332
1333 if( pt.is_string() )
1334 ptStr = pt.get<std::string>();
1335
1336 if( ptStr == wxS( "COMPONENT" ) )
1337 flags.insert( 2 );
1338 else if( ptStr == wxS( "TRACK" ) )
1339 flags.insert( 5 );
1340 else if( ptStr == wxS( "COPPER" ) )
1341 flags.insert( 6 );
1342 else if( ptStr == wxS( "VIA" ) )
1343 flags.insert( 7 );
1344 else if( ptStr == wxS( "FILL" ) || ptStr == wxS( "PLANE" ) )
1345 flags.insert( 8 );
1346 }
1347 }
1348
1349 for( nlohmann::json& polyData : polyDataList )
1350 {
1351 SHAPE_LINE_CHAIN contour = m_v2Parser.ParseContour( polyData, false );
1352 contour.SetClosed( true );
1353
1354 std::unique_ptr<ZONE> zone = std::make_unique<ZONE>( aBoard );
1355
1356 zone->SetIsRuleArea( true );
1357 zone->SetDoNotAllowFootprints( !!flags.count( 2 ) );
1358 zone->SetDoNotAllowZoneFills( !!flags.count( 7 ) || !!flags.count( 6 ) || !!flags.count( 8 ) );
1359 zone->SetDoNotAllowPads( !!flags.count( 7 ) );
1360 zone->SetDoNotAllowTracks( !!flags.count( 7 ) || !!flags.count( 5 ) );
1361 zone->SetDoNotAllowVias( !!flags.count( 7 ) );
1362
1363 zone->SetLayer( klayer );
1364 zone->Outline()->Append( contour );
1365
1366 aBoard->Add( zone.release(), ADD_MODE::APPEND );
1367 }
1368 }
1369 else if( row.type == wxS( "STRING" ) )
1370 {
1371 aBoard->Add( createV3Text( aBoard, row ), ADD_MODE::APPEND );
1372 }
1373 else if( row.type == wxS( "COMPONENT" ) )
1374 {
1375 COMP_DATA comp;
1376 comp.layer = V3GetInt( row.inner, "layerId", 1 );
1377 comp.pos.x = V3GetDouble( row.inner, "x" );
1378 comp.pos.y = V3GetDouble( row.inner, "y" );
1379 comp.angle = V3GetDouble( row.inner, "angle" );
1380
1381 nlohmann::json attrsObj = row.inner.value( "attrs", nlohmann::json::object() );
1382
1383 if( attrsObj.is_object() )
1384 {
1385 for( auto& [k, v] : attrsObj.items() )
1386 comp.attrs[wxString( k )] = V3JsonToString( v );
1387 }
1388
1389 componentMap[row.id] = comp;
1390 }
1391 else if( row.type == wxS( "ATTR" ) )
1392 {
1393 ATTR_DATA attr;
1394 attr.parentId = V3GetString( row.inner, "parentId" );
1395 attr.layer = V3GetInt( row.inner, "layerId", 3 );
1396 attr.hasPos = !V3IsNullOrMissing( row.inner, "x" );
1397
1398 if( attr.hasPos )
1399 {
1400 attr.x = V3GetDouble( row.inner, "x" );
1401 attr.y = V3GetDouble( row.inner, "y" );
1402 }
1403
1404 attr.key = V3GetString( row.inner, "key" );
1405 attr.value = V3JsonToString( row.inner.value( "value", nlohmann::json() ) );
1406 attr.keyVisible = V3GetBool( row.inner, "keyVisible" );
1407 attr.valVisible = V3GetBool( row.inner, "valueVisible" );
1408 attr.fontName = V3GetString( row.inner, "fontFamily", wxS( "default" ) );
1409 attr.fontSize = V3GetDouble( row.inner, "fontSize", 45.0 );
1410 attr.strokeWidth = V3GetDouble( row.inner, "strokeWidth", 6.0 );
1411 attr.origin = V3GetString( row.inner, "origin", wxS( "LEFT_BOTTOM" ) );
1412 attr.angle = V3GetDouble( row.inner, "angle" );
1413 attr.inverted = V3GetBool( row.inner, "reverse" ) ? 1 : 0;
1414 attr.mirror = V3GetBool( row.inner, "mirror" ) ? 1 : 0;
1415
1416 attrMap.emplace( attr.parentId, attr );
1417 }
1418 else if( row.type == wxS( "PAD_NET" ) )
1419 {
1420 nlohmann::json idArr = V3ParseIdArray( row.id );
1421
1422 PAD_NET_DATA pn;
1423 pn.compId = idArr.is_array() && idArr.size() > 1 ? V3JsonToString( idArr[1] ) : wxString();
1424 pn.padNumber = idArr.is_array() && idArr.size() > 2 ? V3JsonToString( idArr[2] ) : wxString();
1425 pn.padNet = V3GetString( row.inner, "padNet" );
1426
1427 if( !pn.compId.IsEmpty() )
1428 padNetMap.emplace( pn.compId, pn );
1429 }
1430 else if( row.type == wxS( "PAD" ) )
1431 {
1432 wxString netname = V3GetString( row.inner, "netName" );
1433
1434 std::unique_ptr<FOOTPRINT> footprint = std::make_unique<FOOTPRINT>( aBoard );
1435 std::unique_ptr<PAD> pad = createV3PAD( footprint.get(), row );
1436
1437 pad->SetNet( aBoard->FindNet( netname ) );
1438
1439 VECTOR2I pos = pad->GetPosition();
1440 EDA_ANGLE orient = pad->GetOrientation();
1441
1442 pad->SetPosition( VECTOR2I() );
1443 pad->SetOrientation( ANGLE_0 );
1444
1445 footprint->Add( pad.release(), ADD_MODE::APPEND );
1446 footprint->SetPosition( pos );
1447 footprint->SetOrientation( orient );
1448
1449 wxString fpName = wxS( "Pad_" ) + row.id;
1450 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( wxEmptyString, fpName );
1451
1452 footprint->SetFPID( fpID );
1453 footprint->Reference().SetVisible( true );
1454 footprint->Value().SetVisible( true );
1455 footprint->AutoPositionFields();
1456
1457 aBoard->Add( footprint.release(), ADD_MODE::APPEND );
1458 }
1459 else if( row.type == wxS( "IMAGE" ) )
1460 {
1461 int layer = V3GetInt( row.inner, "layerId", 1 );
1462 PCB_LAYER_ID klayer = m_v2Parser.LayerToKi( layer );
1463
1464 VECTOR2D start;
1465 start.x = V3GetDouble( row.inner, "startX" );
1466 start.y = V3GetDouble( row.inner, "startY" );
1467
1468 VECTOR2D size;
1469 size.x = V3GetDouble( row.inner, "width" );
1470 size.y = V3GetDouble( row.inner, "height" );
1471
1472 double angle = V3GetDouble( row.inner, "angle" );
1473 bool mirror = V3GetBool( row.inner, "mirror" );
1474 nlohmann::json polyDataList = row.inner.value( "path", nlohmann::json::array() );
1475
1476 BOX2I bbox;
1477 std::vector<SHAPE_LINE_CHAIN> contours;
1478 for( nlohmann::json& polyData : polyDataList )
1479 {
1480 SHAPE_LINE_CHAIN contour = m_v2Parser.ParseContour( polyData, false );
1481 contour.SetClosed( true );
1482
1483 contours.push_back( contour );
1484
1485 bbox.Merge( contour.BBox() );
1486 }
1487
1488 if( bbox.GetSize().x == 0 || bbox.GetSize().y == 0 )
1489 continue;
1490
1493
1494 SHAPE_POLY_SET polySet;
1495
1496 for( SHAPE_LINE_CHAIN& contour : contours )
1497 {
1498 for( int i = 0; i < contour.PointCount(); i++ )
1499 {
1500 VECTOR2I pt = contour.CPoint( i );
1501 contour.SetPoint( i, VECTOR2I( pt.x * scale.x, pt.y * scale.y ) );
1502 }
1503
1504 polySet.AddOutline( contour );
1505 }
1506
1507 polySet.RebuildHolesFromContours();
1508
1509 std::unique_ptr<PCB_GROUP> group;
1510
1511 if( polySet.OutlineCount() > 1 )
1512 group = std::make_unique<PCB_GROUP>( aBoard );
1513
1514 BOX2I polyBBox = polySet.BBox();
1515
1516 for( const SHAPE_POLY_SET::POLYGON& poly : polySet.CPolygons() )
1517 {
1518 std::unique_ptr<PCB_SHAPE> shape = std::make_unique<PCB_SHAPE>( aBoard, SHAPE_T::POLY );
1519
1520 shape->SetFilled( true );
1521 shape->SetPolyShape( poly );
1522 shape->SetLayer( klayer );
1523 shape->SetWidth( 0 );
1524
1525 shape->Move( PCB_IO_EASYEDAPRO_PARSER::ScalePos( start ) - polyBBox.GetOrigin() );
1526 shape->Rotate( PCB_IO_EASYEDAPRO_PARSER::ScalePos( start ), EDA_ANGLE( angle, DEGREES_T ) );
1527
1528 if( IsBackLayer( klayer ) ^ mirror )
1529 {
1531 shape->Mirror( PCB_IO_EASYEDAPRO_PARSER::ScalePos( start ), flipDirection );
1532 }
1533
1534 if( group )
1535 group->AddItem( shape.get() );
1536
1537 aBoard->Add( shape.release(), ADD_MODE::APPEND );
1538 }
1539
1540 if( group )
1541 aBoard->Add( group.release(), ADD_MODE::APPEND );
1542 }
1543 else if( row.type == wxS( "OBJ" ) )
1544 {
1545 int layer = V3GetInt( row.inner, "layerId", 1 );
1546 PCB_LAYER_ID klayer = m_v2Parser.LayerToKi( layer );
1547
1548 VECTOR2D start;
1549 start.x = V3GetDouble( row.inner, "startX" );
1550 start.y = V3GetDouble( row.inner, "startY" );
1551
1552 VECTOR2D size;
1553 size.x = V3GetDouble( row.inner, "width" );
1554 size.y = V3GetDouble( row.inner, "height" );
1555
1556 double angle = V3GetDouble( row.inner, "angle" );
1557 bool mirror = V3GetBool( row.inner, "mirror" );
1558 wxString imageUrl = V3GetString( row.inner, "path" );
1559
1560 if( imageUrl.empty() )
1561 continue;
1562
1563 wxString mimeType, base64Data;
1564
1565 if( imageUrl.BeforeFirst( ':' ) == wxS( "blob" ) )
1566 {
1567 wxString objectId = imageUrl.AfterLast( ':' );
1568
1569 if( auto blob = get_opt( aBlobMap, objectId ) )
1570 {
1571 wxString blobUrl = blob->url;
1572
1573 if( blobUrl.BeforeFirst( ':' ) == wxS( "data" ) )
1574 {
1575 wxArrayString paramsArr = wxSplit( blobUrl.AfterFirst( ':' ).BeforeFirst( ',' ), ';', '\0' );
1576
1577 base64Data = blobUrl.AfterFirst( ',' );
1578
1579 if( paramsArr.size() > 0 )
1580 mimeType = paramsArr[0];
1581 }
1582 }
1583 }
1584
1587
1588 if( mimeType.empty() || base64Data.empty() )
1589 continue;
1590
1591 wxMemoryBuffer buf = wxBase64Decode( base64Data );
1592
1593 if( mimeType == wxS( "image/svg+xml" ) )
1594 {
1595 // Not yet supported by EasyEDA
1596 }
1597 else
1598 {
1599 VECTOR2D kcenter = kstart + ksize / 2;
1600
1601 std::unique_ptr<PCB_REFERENCE_IMAGE> bitmap =
1602 std::make_unique<PCB_REFERENCE_IMAGE>( aBoard, kcenter, klayer );
1603 REFERENCE_IMAGE& refImage = bitmap->GetReferenceImage();
1604
1605 wxImage::SetDefaultLoadFlags( wxImage::GetDefaultLoadFlags() & ~wxImage::Load_Verbose );
1606
1607 if( refImage.ReadImageFile( buf ) )
1608 {
1609 double scaleFactor = PCB_IO_EASYEDAPRO_PARSER::ScaleSize( size.x ) / refImage.GetSize().x;
1610 refImage.SetImageScale( scaleFactor );
1611
1612 // TODO: support non-90-deg angles
1613 bitmap->Rotate( kstart, EDA_ANGLE( angle, DEGREES_T ) );
1614
1615 if( mirror )
1616 {
1617 int x = bitmap->GetPosition().x;
1618 MIRROR( x, KiROUND( kstart.x ) );
1619 bitmap->SetX( x );
1620
1622 }
1623
1624 aBoard->Add( bitmap.release(), ADD_MODE::APPEND );
1625 }
1626 }
1627 }
1628 } // end first pass
1629
1630 // Second pass: place components
1631 for( auto const& [compId, comp] : componentMap )
1632 {
1633 wxString deviceId;
1634 wxString fpIdOverride;
1635 wxString fpDesignator;
1636
1637 // Collect attrs for this component
1638 auto attrRange = attrMap.equal_range( compId );
1639
1640 for( auto it = attrRange.first; it != attrRange.second; ++it )
1641 {
1642 const ATTR_DATA& attr = it->second;
1643
1644 if( attr.key == wxS( "Device" ) )
1645 deviceId = attr.value;
1646 else if( attr.key == wxS( "Footprint" ) )
1647 fpIdOverride = attr.value;
1648 else if( attr.key == wxS( "Designator" ) )
1649 fpDesignator = attr.value;
1650 }
1651
1652 // BOM / identity / 3D fields come from the component's Device ATTR.
1653 EASYEDAPRO::V3_DEVICE_DATA deviceData = GetV3DeviceData( aProject, deviceId );
1654 const std::map<wxString, wxString>& deviceAttrs = deviceData.attributes;
1655
1656 wxString fpId = fpIdOverride;
1657
1658 if( fpId.empty() )
1659 fpId = get_def( deviceAttrs, wxS( "Footprint" ), wxEmptyString );
1660
1661 if( fpId.empty() )
1662 {
1663 wxLogTrace( traceEasyEdaIo, wxT( "EasyEDA Pro v3 component '%s' (%s): no footprint mapping, skipping." ),
1664 compId, fpDesignator );
1665 continue;
1666 }
1667
1668 auto it = aFootprintMap.find( fpId );
1669
1670 if( it == aFootprintMap.end() )
1671 {
1672 wxLogTrace( traceEasyEdaIo, "Footprint of '%s' with uuid '%s' not found.", fpDesignator, fpId );
1673 continue;
1674 }
1675
1676 std::unique_ptr<FOOTPRINT>& footprintOrig = it->second;
1677 std::unique_ptr<FOOTPRINT> footprint( static_cast<FOOTPRINT*>( footprintOrig->Duplicate( false ) ) );
1678
1679 footprint->SetParent( aBoard );
1680
1681 // Footprint LIB_ID uses package geometry name (e.g. C0402), matching schematic
1682 // Footprint fields. Device/BOM identity stays in fields, not the FPID.
1683 wxString libItemName = footprint->GetFPID().GetLibItemName();
1684
1685 if( libItemName.empty() )
1686 libItemName = fpId;
1687
1688 footprint->SetFPID( ToKiCadLibID( aFpLibName, libItemName ) );
1689
1690 wxString modelUuid = get_def( deviceAttrs, wxS( "3D Model" ), wxEmptyString );
1691 wxString modelTitle = get_def( deviceAttrs, wxS( "3D Model Title" ), modelUuid ).Trim();
1692 wxString modelTransform = get_def( deviceAttrs, wxS( "3D Model Transform" ), wxEmptyString );
1693
1694 m_v2Parser.FillFootprintModelInfo( footprint.get(), modelUuid, modelTitle, modelTransform );
1695
1696 wxString valueText = ResolveV3DeviceValueText( deviceAttrs );
1697
1698 if( !valueText.empty() )
1699 footprint->SetValue( valueText );
1700
1701 wxString description = deviceData.description;
1702
1703 if( description.empty() )
1704 description = get_def( deviceAttrs, wxS( "Description" ), wxEmptyString );
1705
1706 if( !description.empty() )
1707 {
1708 footprint->GetField( FIELD_T::DESCRIPTION )
1709 ->SetText( NormalizeEasyEDAText( ResolveDeviceFieldVariables( description, deviceAttrs ) ) );
1710 }
1711
1712 ForEachImportedDeviceField( deviceAttrs, false,
1713 [&]( const wxString& attrKey, const wxString& value )
1714 {
1715 PCB_FIELD* field = nullptr;
1716
1717 if( attrKey == wxS( "Datasheet" ) )
1718 field = footprint->GetField( FIELD_T::DATASHEET );
1719 else
1720 field = footprint->GetField( attrKey );
1721
1722 if( !field )
1723 {
1724 field = new PCB_FIELD( footprint.get(), FIELD_T::USER, attrKey );
1725 footprint->Add( field, ADD_MODE::APPEND );
1726 }
1727
1728 field->SetText( value );
1729 field->SetVisible( false );
1730 } );
1731
1732 // Apply position, rotation, flip
1733 PCB_LAYER_ID klayer = m_v2Parser.LayerToKi( comp.layer );
1734
1735 if( klayer == B_Cu )
1736 footprint->Flip( footprint->GetPosition(), FLIP_DIRECTION::TOP_BOTTOM );
1737
1738 footprint->SetOrientationDegrees( comp.angle );
1739 footprint->SetPosition( PCB_IO_EASYEDAPRO_PARSER::ScalePos( comp.pos ) );
1740
1741 // Apply attributes (designator, etc.)
1742 for( auto attrIt = attrRange.first; attrIt != attrRange.second; ++attrIt )
1743 {
1744 const ATTR_DATA& attr = attrIt->second;
1745
1746 if( attr.key == wxS( "Designator" ) )
1747 {
1748 PCB_FIELD* field = footprint->GetField( FIELD_T::REFERENCE );
1749
1750 if( attr.fontName != wxS( "default" ) )
1751 field->SetFont( KIFONT::FONT::GetFont( attr.fontName ) );
1752
1753 if( attr.valVisible && attr.keyVisible )
1754 field->SetText( attr.key + ':' + attr.value );
1755 else if( attr.keyVisible )
1756 field->SetText( attr.key );
1757 else
1758 field->SetText( attr.value );
1759
1760 field->SetVisible( attr.keyVisible || attr.valVisible );
1761
1762 PCB_LAYER_ID attrLayer = m_v2Parser.LayerToKi( attr.layer );
1763 field->SetLayer( attrLayer );
1764
1765 if( attr.hasPos )
1766 {
1767 field->SetPosition( PCB_IO_EASYEDAPRO_PARSER::ScalePos( VECTOR2D( attr.x, attr.y ) ) );
1768 }
1769
1770 field->SetKeepUpright( false );
1771 field->SetTextAngleDegrees( footprint->IsFlipped() ? -attr.angle : attr.angle );
1772 field->SetIsKnockout( attr.inverted );
1773 field->SetTextThickness( PCB_IO_EASYEDAPRO_PARSER::ScaleSize( attr.strokeWidth ) );
1774 field->SetTextSize( VECTOR2D( PCB_IO_EASYEDAPRO_PARSER::ScaleSize( attr.fontSize * 0.55 ),
1775 PCB_IO_EASYEDAPRO_PARSER::ScaleSize( attr.fontSize * 0.6 ) ) );
1776
1777 int alignCode = V3AlignToOriginCode( attr.origin );
1778 V3AlignText( field, alignCode );
1779 }
1780 }
1781
1782 // Apply pad nets
1783 auto padNetRange = padNetMap.equal_range( compId );
1784
1785 for( auto pnIt = padNetRange.first; pnIt != padNetRange.second; ++pnIt )
1786 {
1787 const PAD_NET_DATA& pn = pnIt->second;
1788
1789 for( PAD* pad : footprint->Pads() )
1790 {
1791 if( pad->GetNumber() == pn.padNumber )
1792 pad->SetNet( aBoard->FindNet( pn.padNet ) );
1793 }
1794 }
1795
1796 aBoard->Add( footprint.release(), ADD_MODE::APPEND );
1797 }
1798
1799 // Set zone fills from POURED data
1801 {
1802 for( auto& [uuid, zone] : poursToFill )
1803 {
1804 SHAPE_POLY_SET fillPolySet;
1805 SHAPE_POLY_SET thermalSpokes;
1806
1807 auto range = boardPouredMap.equal_range( uuid );
1808
1809 for( auto& pIt = range.first; pIt != range.second; ++pIt )
1810 {
1811 const EASYEDAPRO::POURED& poured = pIt->second;
1812
1813 SHAPE_POLY_SET thisPoly;
1814
1815 for( int dataId = 0; dataId < static_cast<int>( poured.polyData.size() ); dataId++ )
1816 {
1817 const nlohmann::json& fillData = poured.polyData[dataId];
1818 const double ptScale = 10;
1819
1820 SHAPE_LINE_CHAIN contour = m_v2Parser.ParseContour( fillData, false, ARC_HIGH_DEF / ptScale );
1821
1822 // Scale the fill
1823 for( int i = 0; i < contour.PointCount(); i++ )
1824 contour.SetPoint( i, contour.GetPoint( i ) * ptScale );
1825
1826 if( poured.isPoly )
1827 {
1828 contour.SetClosed( true );
1829
1830 // The contour can be self-intersecting
1831 SHAPE_POLY_SET simple( contour );
1832 simple.Simplify();
1833
1834 if( dataId == 0 )
1835 {
1836 thisPoly.Append( simple );
1837 }
1838 else
1839 {
1840 thisPoly.BooleanSubtract( simple );
1841 }
1842 }
1843 else
1844 {
1845 const int thermalWidth = pcbIUScale.mmToIU( 0.2 );
1846
1847 for( int segId = 0; segId < contour.SegmentCount(); segId++ )
1848 {
1849 const SEG& seg = contour.CSegment( segId );
1850
1851 TransformOvalToPolygon( thermalSpokes, seg.A, seg.B, thermalWidth, ARC_HIGH_DEF,
1852 ERROR_INSIDE );
1853 }
1854 }
1855 }
1856
1857 fillPolySet.Append( thisPoly );
1858 }
1859
1860 if( !fillPolySet.IsEmpty() )
1861 {
1862 fillPolySet.Simplify();
1863
1864 const int strokeWidth = pcbIUScale.MilsToIU( 8 );
1865
1866 fillPolySet.Inflate( strokeWidth / 2, CORNER_STRATEGY::ROUND_ALL_CORNERS, ARC_HIGH_DEF, false );
1867
1868 fillPolySet.BooleanAdd( thermalSpokes );
1869 fillPolySet.Fracture();
1870
1871 zone->SetFilledPolysList( zone->GetFirstLayer(), fillPolySet );
1872 zone->SetNeedRefill( false );
1873 zone->SetIsFilled( true );
1874 }
1875 }
1876 }
1877
1878 // Heal board outlines
1879 std::vector<PCB_SHAPE*> shapes;
1880
1881 for( BOARD_ITEM* item : aBoard->Drawings() )
1882 {
1883 if( !item->IsOnLayer( Edge_Cuts ) )
1884 continue;
1885
1886 if( item->Type() == PCB_SHAPE_T )
1887 shapes.push_back( static_cast<PCB_SHAPE*>( item ) );
1888 }
1889
1891
1892 // Center the board
1893 BOX2I outlineBbox = aBoard->ComputeBoundingBox( true );
1894 PAGE_INFO pageInfo = aBoard->GetPageSettings();
1895
1896 VECTOR2D pageCenter( pcbIUScale.MilsToIU( pageInfo.GetWidthMils() / 2 ),
1897 pcbIUScale.MilsToIU( pageInfo.GetHeightMils() / 2 ) );
1898
1899 VECTOR2D offset = pageCenter - outlineBbox.GetCenter();
1900
1901 int alignGrid = pcbIUScale.mmToIU( 10 );
1902 offset.x = KiROUND( offset.x / alignGrid ) * alignGrid;
1903 offset.y = KiROUND( offset.y / alignGrid ) * alignGrid;
1904
1905 aBoard->Move( offset );
1906 bds.SetAuxOrigin( offset );
1907}
int index
@ ERROR_INSIDE
constexpr int ARC_HIGH_DEF
Definition base_units.h:144
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
#define DEFAULT_COURTYARD_WIDTH
BOX2< VECTOR2I > BOX2I
Definition box2.h:917
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:985
BASE_SET & set(size_t pos)
Definition base_set.h:126
void Mirror(FLIP_DIRECTION aFlipDirection)
Mirror image vertically (i.e.
Container for design settings for a BOARD object.
std::shared_ptr< NET_SETTINGS > m_NetSettings
void SetAuxOrigin(const VECTOR2I &aOrigin)
ZONE_SETTINGS & GetDefaultZoneSettings()
Abstract interface for BOARD_ITEMs capable of storing other items inside.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:99
virtual void SetIsKnockout(bool aKnockout)
Definition board_item.h:438
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition board_item.h:397
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
bool m_LegacyDesignSettingsLoaded
True if the legacy board design settings were loaded from a file.
Definition board.h:565
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition board.cpp:1548
NETINFO_ITEM * FindOrAddNet(const wxString &aNetName, int aNetCode=-1, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false)
Return the net with the given name, adding it to the board if there is none.
Definition board.cpp:2852
const PAGE_INFO & GetPageSettings() const
Definition board.h:1029
NETINFO_ITEM * FindNet(int aNetcode) const
Search for a net with the given netcode.
Definition board.cpp:2833
bool SetLayerName(PCB_LAYER_ID aLayer, const wxString &aLayerName)
Changes the name of the layer given by aLayer.
Definition board.cpp:978
void Move(const VECTOR2I &aMoveVector) override
Move this object.
Definition board.cpp:844
bool m_LegacyNetclassesLoaded
True if netclasses were loaded from the file.
Definition board.h:569
const LSET & GetEnabledLayers() const
A proxy function that calls the corresponding function in m_BoardSettings.
Definition board.cpp:1202
BOX2I ComputeBoundingBox(bool aBoardEdgesOnly=false, bool aPhysicalLayersOnly=false) const
Calculate the bounding box containing all board items (or board edge segments).
Definition board.cpp:2572
unsigned GetNetCount() const
Definition board.h:1276
void SetEnabledLayers(const LSET &aLayerMask)
A proxy function that calls the correspondent function in m_BoardSettings.
Definition board.cpp:1222
BOARD_DESIGN_SETTINGS & GetDesignSettings()
Definition board.cpp:1318
const DRAWINGS & Drawings() const
Definition board.h:467
constexpr BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition box2.h:556
constexpr const Vec GetEnd() const
Definition box2.h:215
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 const Vec & GetOrigin() const
Definition box2.h:213
constexpr const SizeVec & GetSize() const
Definition box2.h:209
EDA_ANGLE Normalize90()
Definition eda_angle.h:260
double AsDegrees() const
Definition eda_angle.h:115
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 SetTextAngleDegrees(double aOrientation)
Definition eda_text.h:181
void SetKeepUpright(bool aKeepUpright)
Definition eda_text.cpp:387
virtual void SetText(const wxString &aText)
Definition eda_text.cpp:237
void SetFont(KIFONT::FONT *aFont)
Definition eda_text.cpp:472
const BOX2I GetLayerBoundingBox(const LSET &aLayers) const
Return the bounding box of the footprint on a given set of layers.
void MoveAnchorPosition(const VECTOR2I &aMoveVector)
Move the reference point of the footprint.
PCB_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this footprint.
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Adds an item to the container.
bool IsOnLayer(PCB_LAYER_ID aLayer) const override
Test to see if this object is on the given layer.
void GetFields(std::vector< PCB_FIELD * > &aVector, bool aVisibleOnly) const
Populate a std::vector with PCB_TEXTs.
DRAWINGS & GraphicalItems()
Definition footprint.h:358
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 logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
void SetClearance(int aClearance)
Definition netclass.h:144
std::shared_ptr< NETCLASS > GetDefaultNetclass() const
Gets the default netclass for the project.
@ NORMAL
Shape is the same on all layers.
Definition padstack.h:175
static constexpr PCB_LAYER_ID ALL_LAYERS
! The layer identifier to use for the single defintion on normal padstacks
Definition padstack.h:184
Definition pad.h:61
static LSET PTHMask()
layer set for a through hole pad
Definition pad.cpp:620
static LSET SMDMask()
layer set for a SMD pad on Front layer
Definition pad.cpp:627
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition page_info.h:75
double GetHeightMils() const
Definition page_info.h:142
double GetWidthMils() const
Definition page_info.h:137
static nlohmann::json NormalizeSafeSpacing(const nlohmann::json &aTable)
Map legacy eight- and ten-row safe-spacing matrices to the v3 category layout.
static void SizeHoleOnlyPad(PAD &aPad, bool aPlated)
A hole with no copper around it takes the drill size, and stays unplated if the source says it is.
static VECTOR2< T > ScalePos(VECTOR2< T > aValue)
void ParseBoard(BOARD *aBoard, const nlohmann::json &aProject, std::map< wxString, std::unique_ptr< FOOTPRINT > > &aFootprintMap, const std::map< wxString, EASYEDAPRO::BLOB > &aBlobMap, const std::multimap< wxString, EASYEDAPRO::POURED > &aPouredMap, const EASYEDAPRO::V3_DOC_RAW &aDoc, const wxString &aFpLibName)
PCB_IO_EASYEDAPRO_V3_PARSER(BOARD *aBoard, PROGRESS_REPORTER *aProgressReporter)
std::unique_ptr< PAD > createV3PAD(FOOTPRINT *aFootprint, const EASYEDAPRO::V3_ROW &aRow)
std::unique_ptr< FOOTPRINT > ParseFootprint(const std::map< wxString, EASYEDAPRO::BLOB > &aBlobMap, const EASYEDAPRO::V3_DOC_RAW &aDoc)
PCB_TEXT * createV3Text(BOARD_ITEM_CONTAINER *aContainer, const EASYEDAPRO::V3_ROW &aRow)
void SetTextThickness(int aWidth) override
The TextThickness is that set by the user.
Definition pcb_text.cpp:514
void SetTextSize(VECTOR2I aNewSize, bool aEnforceMinTextSize=true) override
Definition pcb_text.cpp:486
void SetPosition(const VECTOR2I &aPos) override
Definition pcb_text.h:103
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...
BITMAP_BASE & MutableImage() const
Only use this if you really need to modify the underlying image.
bool ReadImageFile(const wxString &aFullFilename)
Read and store an image file.
VECTOR2I GetSize() const
void SetImageScale(double aScale)
Set the image "zoom" value.
Definition seg.h:38
VECTOR2I A
Definition seg.h:45
VECTOR2I B
Definition seg.h:46
SHAPE_ARC & ConstructFromStartEndCenter(const VECTOR2I &aStart, const VECTOR2I &aEnd, const VECTOR2I &aCenter, bool aClockwise=false, double aWidth=0)
Constructs this arc from the given start, end and center.
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
virtual const VECTOR2I GetPoint(int aIndex) const override
void SetPoint(int aIndex, const VECTOR2I &aPos)
Move a point to a specific location.
void SetClosed(bool aClosed)
Mark the line chain as closed (i.e.
int PointCount() const
Return the number of points (vertices) in this line chain.
int SegmentCount() const
Return the number of segments in this line chain.
const SEG CSegment(int aIndex) const
Return a constant copy of the aIndex segment in the line chain.
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
Represent a set of closed polygons.
void BooleanAdd(const SHAPE_POLY_SET &b)
Perform boolean polyset union.
void ClearArcs()
Removes all arc references from all the outlines and holes in the polyset.
int AddOutline(const SHAPE_LINE_CHAIN &aOutline)
Adds a new outline to the set and returns its index.
bool IsEmpty() const
Return true if the set is empty (no polygons at all)
void Inflate(int aAmount, CORNER_STRATEGY aCornerStrategy, int aMaxError, bool aSimplify=false)
Perform outline inflation/deflation.
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Appends a vertex at the end of the given outline/hole (default: the last outline)
void Simplify()
Simplify the polyset (merges overlapping polys, eliminates degeneracy/self-intersections)
std::vector< SHAPE_LINE_CHAIN > POLYGON
represents a single polygon outline with holes.
void RebuildHolesFromContours()
Extract all contours from this polygon set, then recreate polygons with holes.
int OutlineCount() const
Return the number of outlines in the set.
void Fracture(bool aSimplify=true)
Convert a set of polygons with holes to a single outline with "slits"/"fractures" connecting the oute...
void BooleanSubtract(const SHAPE_POLY_SET &b)
Perform boolean polyset difference.
const std::vector< POLYGON > & CPolygons() const
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
const SHAPE_LINE_CHAIN Outline() const
void TransformOvalToPolygon(SHAPE_POLY_SET &aBuffer, const VECTOR2I &aStart, const VECTOR2I &aEnd, int aWidth, int aError, ERROR_LOC aErrorLoc, int aMinSegCount=0)
Convert a oblong shape to a polygon, using multiple segments.
@ ROUND_ALL_CORNERS
All angles are rounded.
static bool empty(const wxTextEntryBase *aCtrl)
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:472
static constexpr EDA_ANGLE ANGLE_90
Definition eda_angle.h:474
@ DEGREES_T
Definition eda_angle.h:30
@ RECTANGLE
Use RECTANGLE instead of RECT to avoid collision in a Windows header.
Definition eda_shape.h:58
void ConnectBoardShapes(std::vector< PCB_SHAPE * > &aShapeList, int aChainingEpsilon)
Connects shapes to each other, making continious contours (adjacent shapes will have a common vertex)...
const wxChar *const traceEasyEdaIo
bool IsBackLayer(PCB_LAYER_ID aLayerId)
Layer classification: check if it's a back layer.
Definition layer_ids.h:870
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ F_CrtYd
Definition layer_ids.h:112
@ Edge_Cuts
Definition layer_ids.h:108
@ F_Paste
Definition layer_ids.h:100
@ B_Cu
Definition layer_ids.h:61
@ F_Mask
Definition layer_ids.h:93
@ F_Fab
Definition layer_ids.h:115
@ F_Cu
Definition layer_ids.h:60
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
constexpr void MIRROR(T &aPoint, const T &aMirrorRef)
Updates aPoint with the mirror of aPoint relative to the aMirrorRef.
Definition mirror.h:44
FLIP_DIRECTION
Definition mirror.h:26
@ LEFT_RIGHT
Flip left to right (around the Y axis)
Definition mirror.h:27
@ TOP_BOTTOM
Flip top to bottom (around the X axis)
Definition mirror.h:28
LIB_ID ToKiCadLibID(const wxString &aLibName, const wxString &aLibReference)
bool V3GetBool(const nlohmann::json &aObj, const char *aKey, bool aDefault)
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.
wxString NormalizeEasyEDAText(wxString aText)
Replace EasyEDA temperature glyph (℃) with °C.
V3_DEVICE_DATA GetV3DeviceData(const nlohmann::json &aProject, const wxString &aDeviceUuid)
static const bool IMPORT_POURED
nlohmann::json V3ParseIdArray(const wxString &aId)
bool V3IsNullOrMissing(const nlohmann::json &aObj, const char *aKey)
double V3GetDouble(const nlohmann::json &aObj, const char *aKey, double aDefault)
wxString V3JsonToString(const nlohmann::json &aValue, const wxString &aDefault)
wxString ResolveV3DeviceValueText(const std::map< wxString, wxString > &aDeviceAttributes)
Preferred Value text: Value attribute, else Name, with variables resolved.
int V3GetInt(const nlohmann::json &aObj, const char *aKey, int aDefault)
wxString V3GetString(const nlohmann::json &aObj, const char *aKey, const wxString &aDefault)
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:461
@ SMD
Smd pad, appears on the solder paste layer (default)
Definition padstack.h:100
@ PTH
Plated through hole pad.
Definition padstack.h:99
@ ROUNDRECT
Definition padstack.h:58
@ RECTANGLE
Definition padstack.h:55
Class to handle a set of BOARD_ITEMs.
static const int SHAPE_JOIN_DISTANCE
static wxString safeSpacingToMm(double aMil)
static int V3AlignToOriginCode(const wxString &aAlign)
static void V3AlignText(EDA_TEXT *text, int align)
static const wxString safeSpacingNames[]
static const wxString safeSpacingConditions[]
const int scale
std::string FormatDouble2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 This function is intended in...
nlohmann::json polyData
std::map< wxString, wxString > attributes
Raw parsed document from an EasyEDA Pro v3 .epru stream.
std::vector< V3_ROW > rows
One parsed row from an EasyEDA Pro v3 .epru document stream.
@ 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".
@ DATASHEET
name of datasheet
@ REFERENCE
Field Reference of part, i.e. "IC21".
static EDA_ORIENTATION orientation(double d)
static EDA_ANGLE deg(double d)
KIBIS_COMPONENT * comp
VECTOR2I center
int radius
VECTOR2I end
int clearance
VECTOR2I location
int delta
@ GR_TEXT_H_ALIGN_CENTER
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
@ GR_TEXT_V_ALIGN_BOTTOM
@ GR_TEXT_V_ALIGN_CENTER
@ GR_TEXT_V_ALIGN_TOP
wxLogTrace helper definitions.
double DEG2RAD(double deg)
Definition trigo.h:178
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:80
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition typeinfo.h:84
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
VECTOR2< double > VECTOR2D
Definition vector2d.h:707