KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_pads_sch_binary_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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <boost/test/unit_test.hpp>
22
27
28#include <ki_exception.h>
29#include <algorithm>
30#include <cmath>
31#include <filesystem>
32#include <fstream>
33#include <iterator>
34#include <limits>
35#include <map>
36#include <optional>
37#include <set>
38#include <sstream>
39#include <string>
40#include <type_traits>
41#include <variant>
42#include <vector>
43#include <wx/filename.h>
44
45using namespace PADS_SCH_BINARY;
46
47namespace
48{
49
50static std::vector<uint8_t> loadMinimalV13()
51{
52 std::ifstream file( KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/binary/minimal_v13.sch", std::ios::binary );
53 BOOST_REQUIRE( file );
54 return { std::istreambuf_iterator<char>( file ), std::istreambuf_iterator<char>() };
55}
56
57
58static std::vector<uint8_t> loadBinaryFixture( const std::string& aName )
59{
60 std::ifstream file( KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/binary/" + aName, std::ios::binary );
61 BOOST_REQUIRE( file );
62 return { std::istreambuf_iterator<char>( file ), std::istreambuf_iterator<char>() };
63}
64
65
66static uint32_t readU32( const std::vector<uint8_t>& aBytes, size_t aOffset )
67{
68 return static_cast<uint32_t>( aBytes[aOffset] ) | ( static_cast<uint32_t>( aBytes[aOffset + 1] ) << 8 )
69 | ( static_cast<uint32_t>( aBytes[aOffset + 2] ) << 16 )
70 | ( static_cast<uint32_t>( aBytes[aOffset + 3] ) << 24 );
71}
72
73
74static uint16_t readU16( const std::vector<uint8_t>& aBytes, size_t aOffset )
75{
76 return static_cast<uint16_t>( aBytes[aOffset] ) | ( static_cast<uint16_t>( aBytes[aOffset + 1] ) << 8 );
77}
78
79
80static void writeU16( std::vector<uint8_t>& aBytes, size_t aOffset, uint16_t aValue )
81{
82 aBytes[aOffset] = static_cast<uint8_t>( aValue );
83 aBytes[aOffset + 1] = static_cast<uint8_t>( aValue >> 8 );
84}
85
86
87static void writeU32( std::vector<uint8_t>& aBytes, size_t aOffset, uint32_t aValue )
88{
89 writeU16( aBytes, aOffset, static_cast<uint16_t>( aValue ) );
90 writeU16( aBytes, aOffset + 2, static_cast<uint16_t>( aValue >> 16 ) );
91}
92
93
94static size_t outerControllerOffset( const std::vector<uint8_t>& aBytes, size_t aController )
95{
96 size_t offset = 0x254;
97
98 for( size_t controller = 1; controller < aController; ++controller )
99 offset += readU32( aBytes, 0x20 + controller * 28 + 12 );
100
101 return offset;
102}
103
104
105static size_t sheetControllerOffset( const std::vector<uint8_t>& aBytes, size_t aController )
106{
107 const size_t sheetOffset = readU32( aBytes, outerControllerOffset( aBytes, 3 ) );
108 size_t offset = sheetOffset + 20 + 24 * 28;
109
110 for( size_t controller = 1; controller < aController; ++controller )
111 offset += readU32( aBytes, sheetOffset + 20 + ( controller - 1 ) * 28 + 16 );
112
113 return offset;
114}
115
116
117static uint32_t sheetControllerCount( const std::vector<uint8_t>& aBytes, size_t aController )
118{
119 const size_t sheetOffset = readU32( aBytes, outerControllerOffset( aBytes, 3 ) );
120 return readU32( aBytes, sheetOffset + 20 + ( aController - 1 ) * 28 + 12 );
121}
122
123
124template <typename Item>
125static const Item& itemNamed( const std::vector<Item>& aItems, const wxString& aName )
126{
127 auto item = std::ranges::find_if( aItems,
128 [&]( const Item& aItem )
129 {
130 return aItem.name.text == aName;
131 } );
132 BOOST_REQUIRE( item != aItems.end() );
133 return *item;
134}
135
136
137static wxString propertyValue( const std::vector<SOURCE_PROPERTY>& aProperties, const wxString& aName )
138{
139 auto property = std::ranges::find_if( aProperties,
140 [&]( const SOURCE_PROPERTY& aProperty )
141 {
142 return aProperty.name.text == aName;
143 } );
144 BOOST_REQUIRE( property != aProperties.end() );
145 return property->value.text;
146}
147
148
149static std::string canonicalModel( const PADS_SCH_MODEL& aModel )
150{
151 std::ostringstream out;
152 out << aModel.version << '|' << aModel.source.file << '|' << aModel.source.absoluteOffset << '|'
154
155 auto provenance = [&]( const SOURCE_PROVENANCE& aSource )
156 {
157 out << ':' << aSource.version << ',' << aSource.objectClass << ',' << aSource.controller << ','
158 << aSource.recordIndex << ',' << aSource.absoluteOffset << ',' << aSource.length << ',' << aSource.sheet;
159 };
160
161 for( const MODEL_SHEET& sheet : aModel.sheets )
162 {
163 out << "|sheet," << sheet.id.Value() << ',' << sheet.index << ',' << sheet.name.text;
164 provenance( sheet.source );
165 }
166
167 out << "|definitions=" << aModel.definitions.size() << "|parts=" << aModel.partTypes.size()
168 << "|placements=" << aModel.placements.size() << "|nets=" << aModel.nets.size()
169 << "|buses=" << aModel.buses.size() << "|labels=" << aModel.labels.size()
170 << "|junctions=" << aModel.junctions.size() << "|texts=" << aModel.texts.size()
171 << "|graphics=" << aModel.graphics.size() << "|diagnostics=" << aModel.diagnostics.size();
172 return out.str();
173}
174
175
176enum class CANONICAL_KIND
177{
178 SETTINGS,
179 SHEET,
181 GRAPHIC,
182 PIN,
183 FIELD,
184 PART_TYPE,
185 GATE,
186 GATE_PIN_MAPPING,
187 PLACEMENT,
188 NET,
189 CONNECTION,
190 BUS,
191 BUS_ENTRY,
192 BUS_ALIAS,
194 LABEL,
195 JUNCTION,
196 TEXT
197};
198
199using CANONICAL_VALUE = std::variant<int64_t, bool, std::string, std::vector<std::string>>;
200
201struct CANONICAL_PROPERTY
202{
203 CANONICAL_VALUE value;
204 PROPERTY_DISPOSITION disposition = PROPERTY_DISPOSITION::EXACT;
205 bool operator==( const CANONICAL_PROPERTY& ) const = default;
206 bool operator<( const CANONICAL_PROPERTY& aOther ) const
207 {
208 return std::tie( value, disposition ) < std::tie( aOther.value, aOther.disposition );
209 }
210};
211
212struct CANONICAL_POINT
213{
214 int64_t xHalfMils = 0;
215 int64_t yHalfMils = 0;
216 bool operator==( const CANONICAL_POINT& ) const = default;
217 bool operator<( const CANONICAL_POINT& aOther ) const
218 {
219 return std::tie( xHalfMils, yHalfMils ) < std::tie( aOther.xHalfMils, aOther.yHalfMils );
220 }
221};
222
223struct CANONICAL_GEOMETRY
224{
225 std::vector<CANONICAL_POINT> points;
226 std::optional<int64_t> angleTenths;
227 bool operator==( const CANONICAL_GEOMETRY& ) const = default;
228};
229
230struct CANONICAL_SEMANTIC_RECORD
231{
232 int sheet = -1;
233 CANONICAL_KIND kind = CANONICAL_KIND::SETTINGS;
234 std::string stableKey;
235 std::map<std::string, CANONICAL_PROPERTY> properties;
236 CANONICAL_GEOMETRY geometry;
237 bool operator==( const CANONICAL_SEMANTIC_RECORD& ) const = default;
238 bool operator<( const CANONICAL_SEMANTIC_RECORD& aOther ) const
239 {
240 return std::tie( sheet, kind, stableKey, properties, geometry.points, geometry.angleTenths )
241 < std::tie( aOther.sheet, aOther.kind, aOther.stableKey, aOther.properties, aOther.geometry.points,
242 aOther.geometry.angleTenths );
243 }
244};
245
246using SNAPSHOT_ALLOWLIST = std::set<std::pair<std::string, PROPERTY_DISPOSITION>>;
247
248static CANONICAL_POINT point( const SOURCE_POINT& aPoint )
249{
250 return { aPoint.x, aPoint.y };
251}
252static CANONICAL_POINT point( const PADS_SCH::POINT& aPoint )
253{
254 return { static_cast<int64_t>( std::llround( aPoint.x * 2.0 ) ),
255 static_cast<int64_t>( std::llround( aPoint.y * 2.0 ) ) };
256}
257
258static void addSourceProperties( CANONICAL_SEMANTIC_RECORD& aRecord, const std::vector<SOURCE_PROPERTY>& aProperties,
259 const std::string& aPrefix = {} )
260{
261 for( const SOURCE_PROPERTY& property : aProperties )
262 aRecord.properties[aPrefix + property.name.text.ToStdString()] = { property.value.text.ToStdString(),
263 property.disposition };
264}
265
266static CANONICAL_PROPERTY unknownEnum( int64_t aValue )
267{
268 return { "unknown:" + std::to_string( aValue ), PROPERTY_DISPOSITION::UNSUPPORTED };
269}
270
271static CANONICAL_PROPERTY canonicalGraphicType( MODEL_GRAPHIC_KIND aKind )
272{
273 switch( aKind )
274 {
275 case MODEL_GRAPHIC_KIND::LINE: return { "line" };
276 case MODEL_GRAPHIC_KIND::POLYLINE: return { "polyline" };
277 case MODEL_GRAPHIC_KIND::RECTANGLE: return { "rectangle" };
278 case MODEL_GRAPHIC_KIND::CIRCLE: return { "circle" };
279 case MODEL_GRAPHIC_KIND::ARC: return { "arc" };
280 case MODEL_GRAPHIC_KIND::TEXT: return { "text" };
281 }
282
283 return unknownEnum( static_cast<int64_t>( aKind ) );
284}
285
286static CANONICAL_PROPERTY canonicalGraphicType( PADS_SCH::GRAPHIC_TYPE aKind )
287{
288 switch( aKind )
289 {
290 case PADS_SCH::GRAPHIC_TYPE::LINE: return { "line" };
291 case PADS_SCH::GRAPHIC_TYPE::POLYLINE: return { "polyline" };
292 case PADS_SCH::GRAPHIC_TYPE::RECTANGLE: return { "rectangle" };
293 case PADS_SCH::GRAPHIC_TYPE::CIRCLE: return { "circle" };
294 case PADS_SCH::GRAPHIC_TYPE::ARC: return { "arc" };
295 }
296
297 return unknownEnum( static_cast<int64_t>( aKind ) );
298}
299
300static CANONICAL_PROPERTY canonicalGraphicType( const PADS_SCH::SYMBOL_GRAPHIC& aGraphic )
301{
302 if( std::ranges::any_of( aGraphic.points,
303 []( const PADS_SCH::GRAPHIC_POINT& aPoint )
304 {
305 return aPoint.arc.has_value();
306 } ) )
307 {
308 return { "arc" };
309 }
310
311 if( aGraphic.type == PADS_SCH::GRAPHIC_TYPE::POLYLINE && aGraphic.points.size() == 2 )
312 return { "line" };
313
314 return canonicalGraphicType( aGraphic.type );
315}
316
317static CANONICAL_PROPERTY canonicalLineStyle( MODEL_LINE_STYLE aStyle )
318{
319 switch( aStyle )
320 {
322 case MODEL_LINE_STYLE::SOLID: return { "solid" };
323 case MODEL_LINE_STYLE::DASH: return { "dash" };
324 case MODEL_LINE_STYLE::DOT: return { "dot" };
325 case MODEL_LINE_STYLE::DASH_DOT: return { "dash_dot" };
326 }
327
328 return unknownEnum( static_cast<int64_t>( aStyle ) );
329}
330
331static CANONICAL_PROPERTY canonicalLineStyle( int aStyle )
332{
333 switch( static_cast<int8_t>( aStyle & 0xFF ) )
334 {
335 case 1:
336 case -1: return { "solid" };
337 case 0:
338 case -2: return { "dash" };
339 case -3: return { "dot" };
340 case -4: return { "dash_dot" };
341 case -5: return { "dash_dot_dot" };
342 default: return unknownEnum( aStyle );
343 }
344}
345
346static CANONICAL_PROPERTY canonicalFill( MODEL_FILL_STYLE aFill )
347{
348 switch( aFill )
349 {
350 case MODEL_FILL_STYLE::NONE: return { "none" };
351 case MODEL_FILL_STYLE::FILLED: return { "filled" };
352 case MODEL_FILL_STYLE::HATCHED: return { "hatched" };
353 }
354
355 return unknownEnum( static_cast<int64_t>( aFill ) );
356}
357
358static CANONICAL_PROPERTY canonicalFill( bool aFilled )
359{
360 return { aFilled ? "filled" : "none" };
361}
362
363static CANONICAL_PROPERTY canonicalPinType( uint32_t aType )
364{
365 switch( aType )
366 {
367 case 0: return { "passive" };
368 case 1: return { "input" };
369 case 2: return { "output" };
370 case 3: return { "bidirectional" };
371 case 4: return { "tristate" };
372 case 5: return { "open_collector" };
373 case 6: return { "open_emitter" };
374 case 7: return { "power" };
375 case 8: return { "unspecified" };
376 default: return unknownEnum( aType );
377 }
378}
379
380static CANONICAL_PROPERTY canonicalPinType( PADS_SCH::PIN_TYPE aType )
381{
382 switch( aType )
383 {
384 case PADS_SCH::PIN_TYPE::PASSIVE: return { "passive" };
385 case PADS_SCH::PIN_TYPE::INPUT: return { "input" };
386 case PADS_SCH::PIN_TYPE::OUTPUT: return { "output" };
387 case PADS_SCH::PIN_TYPE::BIDIRECTIONAL: return { "bidirectional" };
388 case PADS_SCH::PIN_TYPE::TRISTATE: return { "tristate" };
389 case PADS_SCH::PIN_TYPE::OPEN_COLLECTOR: return { "open_collector" };
390 case PADS_SCH::PIN_TYPE::OPEN_EMITTER: return { "open_emitter" };
391 case PADS_SCH::PIN_TYPE::POWER: return { "power" };
392 case PADS_SCH::PIN_TYPE::UNSPECIFIED: return { "unspecified" };
393 }
394
395 return unknownEnum( static_cast<int64_t>( aType ) );
396}
397
398static CANONICAL_PROPERTY canonicalPinType( char aType )
399{
400 switch( aType )
401 {
402 case 'U': return { "passive" };
403 case 'L': return { "input" };
404 case 'S': return { "output" };
405 case 'B': return { "bidirectional" };
406 case 'T': return { "tristate" };
407 case 'C': return { "open_collector" };
408 case 'E': return { "open_emitter" };
409 case 'P':
410 case 'G': return { "power" };
411 default: return { "unspecified" };
412 }
413}
414
415static CANONICAL_PROPERTY canonicalPinStyle( uint32_t aStyle )
416{
417 switch( aStyle )
418 {
419 case 0: return { "normal" };
420 case 1: return { "inverted" };
421 case 2: return { "clock" };
422 case 3: return { "inverted_clock" };
423 default: return unknownEnum( aStyle );
424 }
425}
426
427static CANONICAL_PROPERTY canonicalPinStyle( bool aInverted, bool aClock )
428{
429 return canonicalPinStyle( ( aInverted ? 1U : 0U ) | ( aClock ? 2U : 0U ) );
430}
431
432static CANONICAL_PROPERTY canonicalJustification( MODEL_JUSTIFICATION aJustification )
433{
434 switch( aJustification )
435 {
436 case MODEL_JUSTIFICATION::LEFT: return { "left" };
437 case MODEL_JUSTIFICATION::CENTER: return { "center" };
438 case MODEL_JUSTIFICATION::RIGHT: return { "right" };
439 }
440
441 return unknownEnum( static_cast<int64_t>( aJustification ) );
442}
443
444static CANONICAL_PROPERTY canonicalVerticalJustification( MODEL_JUSTIFICATION aJustification )
445{
446 switch( aJustification )
447 {
448 case MODEL_JUSTIFICATION::LEFT: return { "top" };
449 case MODEL_JUSTIFICATION::CENTER: return { "center" };
450 case MODEL_JUSTIFICATION::RIGHT: return { "bottom" };
451 }
452
453 return unknownEnum( static_cast<int64_t>( aJustification ) );
454}
455
456// PADS renders justification as a bitfield where center wins, see TextOptionMatrix
457static CANONICAL_PROPERTY canonicalHorizontalJustification( int aJustification )
458{
459 if( aJustification & 0x4 )
460 return { "center" };
461
462 if( aJustification & 0x1 )
463 return { "right" };
464
465 return { "left" };
466}
467
468static CANONICAL_PROPERTY canonicalVerticalJustification( int aJustification )
469{
470 if( aJustification < 0 )
471 return unknownEnum( aJustification );
472
473 if( aJustification & 0x8 )
474 return { "center" };
475
476 if( aJustification & 0x2 )
477 return { "top" };
478
479 return { "bottom" };
480}
481
482static CANONICAL_PROPERTY canonicalLabelKind( MODEL_LABEL_KIND aKind )
483{
484 switch( aKind )
485 {
486 case MODEL_LABEL_KIND::LOCAL: return { "local" };
487 case MODEL_LABEL_KIND::GLOBAL: return { "global" };
488 case MODEL_LABEL_KIND::HIERARCHICAL: return { "hierarchical" };
489 case MODEL_LABEL_KIND::GROUND: return { "ground" };
490 case MODEL_LABEL_KIND::POWER: return { "power" };
491 case MODEL_LABEL_KIND::BUS: return { "bus" };
492 case MODEL_LABEL_KIND::UNSUPPORTED: return unknownEnum( static_cast<int64_t>( aKind ) );
493 }
494
495 return unknownEnum( static_cast<int64_t>( aKind ) );
496}
497
498static CANONICAL_PROPERTY canonicalEndpointKind( MODEL_ENDPOINT_KIND aKind )
499{
500 switch( aKind )
501 {
503 case MODEL_ENDPOINT_KIND::POINT: return { "point" };
504 case MODEL_ENDPOINT_KIND::PIN: return { "pin" };
505 }
506
507 return unknownEnum( static_cast<int64_t>( aKind ) );
508}
509
510static int64_t canonicalAngle( int64_t aAngleTenths )
511{
512 return NormalizeAngle( static_cast<int>( aAngleTenths ) );
513}
514
515static std::vector<CANONICAL_SEMANTIC_RECORD> normalizeBinaryModel( const PADS_SCH_MODEL& aModel )
516{
517 std::vector<CANONICAL_SEMANTIC_RECORD> out;
518 auto add = [&]( CANONICAL_KIND aKind, int aSheet, std::string aKey = {} ) -> CANONICAL_SEMANTIC_RECORD&
519 {
520 out.push_back( { aSheet, aKind, std::move( aKey ) } );
521 return out.back();
522 };
523 auto addOwned = [&]( CANONICAL_KIND aKind, const SHEET_REFERENCE& aSheet,
524 std::string aKey = {} ) -> CANONICAL_SEMANTIC_RECORD&
525 {
526 auto sheet = std::ranges::find_if( aModel.sheets,
527 [&]( const MODEL_SHEET& aCandidate )
528 {
529 return aCandidate.id == aSheet.id;
530 } );
531 auto& record =
532 add( aKind, sheet == aModel.sheets.end() ? -1 : static_cast<int>( sheet->index ), std::move( aKey ) );
533 record.properties["owner_sheet"] = sheet == aModel.sheets.end()
534 ? unknownEnum( aSheet.id.Value() )
535 : CANONICAL_PROPERTY{ sheet->name.text.ToStdString() };
536 return record;
537 };
538 auto graphic = [&]( const MODEL_GRAPHIC& aGraphic, int aSheet )
539 {
540 auto& r = add( CANONICAL_KIND::GRAPHIC, aSheet );
541 r.properties["type"] = canonicalGraphicType( aGraphic.kind );
542 r.properties["line_style"] = canonicalLineStyle( aGraphic.lineStyle );
543 r.properties["stroke_half_mils"] = { aGraphic.strokeWidth };
544 r.properties["fill"] = canonicalFill( aGraphic.fill );
545 r.properties["text"] = { aGraphic.text.text.ToStdString() };
546 r.properties["font"] = { aGraphic.presentation.font.text.ToStdString() };
547 r.properties["visible"] = { aGraphic.presentation.visible };
548 r.properties["height_half_mils"] = { aGraphic.presentation.height };
549 r.properties["width_half_mils"] = { aGraphic.presentation.width };
550
551 if( aGraphic.kind == MODEL_GRAPHIC_KIND::ARC )
552 {
553 r.properties["arc_center_x_half_mils"] = { aGraphic.arcCenter.x };
554 r.properties["arc_center_y_half_mils"] = { aGraphic.arcCenter.y };
555 r.properties["arc_bounds_x1_half_mils"] = { aGraphic.arcBoundsStart.x };
556 r.properties["arc_bounds_y1_half_mils"] = { aGraphic.arcBoundsStart.y };
557 r.properties["arc_bounds_x2_half_mils"] = { aGraphic.arcBoundsEnd.x };
558 r.properties["arc_bounds_y2_half_mils"] = { aGraphic.arcBoundsEnd.y };
559 r.properties["arc_sweep_tenths"] = { int64_t( aGraphic.arcSweepAngle ) };
560 r.properties["arc_clockwise"] = { aGraphic.arcClockwise };
561 }
562
563 for( const SOURCE_POINT& p : aGraphic.points )
564 r.geometry.points.push_back( point( p ) );
565 r.geometry.angleTenths = canonicalAngle( aGraphic.angle );
566 addSourceProperties( r, aGraphic.properties );
567 };
568 auto field = [&]( const MODEL_FIELD& aField, int aSheet )
569 {
570 auto& r = add( CANONICAL_KIND::FIELD, aSheet, aField.name.text.ToStdString() );
571 r.properties["value"] = { aField.value.text.ToStdString() };
572 r.properties["visible"] = { aField.visible };
573 r.properties["font"] = { aField.presentation.font.text.ToStdString() };
574 r.geometry.points.push_back( point( aField.position ) );
575 r.geometry.angleTenths = canonicalAngle( aField.angle );
576 addSourceProperties( r, aField.properties );
577 };
578 auto& settings = add( CANONICAL_KIND::SETTINGS, -1 );
579 settings.properties["coordinate_units_per_mil"] = { aModel.settings.coordinateUnitsPerMil };
580 settings.properties["line_width_half_mils"] = { aModel.settings.defaultLineWidth };
581 settings.properties["bus_width_half_mils"] = { aModel.settings.defaultBusWidth };
582 settings.geometry.points.push_back( point( aModel.settings.pageSize ) );
583 addSourceProperties( settings, aModel.settings.properties );
584 for( const MODEL_SHEET& s : aModel.sheets )
585 {
586 auto& r = add( CANONICAL_KIND::SHEET, static_cast<int>( s.index ), s.name.text.ToStdString() );
587 r.properties["title"] = { s.title.text.ToStdString() };
588 r.properties["line_width_half_mils"] = { s.defaultLineWidth };
589 r.properties["bus_width_half_mils"] = { s.defaultBusWidth };
590 r.geometry.points.push_back( point( s.pageSize ) );
591 addSourceProperties( r, s.properties );
592 for( const auto& g : s.border )
593 graphic( g, static_cast<int>( s.index ) );
594 for( const auto& f : s.titleBlockFields )
595 field( f, static_cast<int>( s.index ) );
596 }
597 for( const auto& d : aModel.definitions )
598 {
599 auto& definition = add( CANONICAL_KIND::DEFINITION, d.source.sheet, d.name.text.ToStdString() );
600 addSourceProperties( definition, d.properties );
601 for( const auto& g : d.graphics )
602 graphic( g, d.source.sheet );
603 for( const auto& p : d.pins )
604 {
605 auto& r = add( CANONICAL_KIND::PIN, d.source.sheet, p.number.text.ToStdString() );
606 r.properties["name"] = { p.name.text.ToStdString() };
607 r.properties["type"] = canonicalPinType( p.electricalType );
608 r.properties["style"] = canonicalPinStyle( p.graphicStyle );
609 r.properties["length_half_mils"] = { p.length };
610 r.properties["font"] = { p.presentation.font.text.ToStdString() };
611 r.properties["visible"] = { p.presentation.visible };
612 r.geometry.points.push_back( point( p.position ) );
613 r.geometry.angleTenths = canonicalAngle( p.angle );
614 addSourceProperties( r, p.properties );
615 }
616 for( const auto& f : d.fields )
617 field( f, d.source.sheet );
618 }
619 for( const auto& p : aModel.partTypes )
620 {
621 auto& part = add( CANONICAL_KIND::PART_TYPE, p.source.sheet, p.name.text.ToStdString() );
622 addSourceProperties( part, p.properties );
623 for( const auto& g : p.gates )
624 {
625 auto& r = add( CANONICAL_KIND::GATE, g.source.sheet, std::to_string( g.unit ) );
626 auto definition = std::ranges::find_if( aModel.definitions,
627 [&]( const MODEL_SYMBOL_DEFINITION& aDefinition )
628 {
629 return aDefinition.id == g.definition.id;
630 } );
631 r.properties["definition"] = definition == aModel.definitions.end()
632 ? unknownEnum( g.definition.id.Value() )
633 : CANONICAL_PROPERTY{ definition->name.text.ToStdString() };
634 addSourceProperties( r, g.properties );
635 for( size_t i = 0; i < g.pins.size(); ++i )
636 {
637 auto& m = add( CANONICAL_KIND::GATE_PIN_MAPPING, g.source.sheet,
638 std::to_string( g.unit ) + ":" + std::to_string( i ) );
639 const MODEL_PIN_DEFINITION* pin = nullptr;
640
641 if( definition != aModel.definitions.end() )
642 {
643 auto resolved = std::ranges::find_if( definition->pins,
644 [&]( const MODEL_PIN_DEFINITION& aPin )
645 {
646 return aPin.id == g.pins[i].id;
647 } );
648
649 if( resolved != definition->pins.end() )
650 pin = &*resolved;
651 }
652
653 if( pin )
654 {
655 m.properties["pin_number"] = { pin->number.text.ToStdString() };
656 m.properties["pin_name"] = { pin->name.text.ToStdString() };
657 }
658 else
659 {
660 m.properties["pin_number"] = unknownEnum( g.pins[i].id.Value() );
661 m.properties["pin_name"] = unknownEnum( g.pins[i].id.Value() );
662 }
663 }
664 }
665 for( const auto& f : p.fields )
666 field( f, p.source.sheet );
667 }
668 for( const auto& p : aModel.placements )
669 {
670 auto& r = addOwned( CANONICAL_KIND::PLACEMENT, p.sheet, p.reference.text.ToStdString() );
671 r.properties["unit"] = { int64_t( p.unit ) };
672 r.properties["mirrored"] = { p.mirrored };
673 r.geometry.points.push_back( point( p.position ) );
674 r.geometry.angleTenths = canonicalAngle( p.angle );
675 addSourceProperties( r, p.properties );
676 int ownerSheet = r.sheet;
677 for( const auto& f : p.fields )
678 field( f, ownerSheet );
679 }
680 for( const auto& n : aModel.nets )
681 {
682 auto& net = addOwned( CANONICAL_KIND::NET, n.sheet, n.name.text.ToStdString() );
683 addSourceProperties( net, n.properties );
684 for( const auto& c : n.connections )
685 {
686 auto& r = addOwned( CANONICAL_KIND::CONNECTION, n.sheet );
687 r.properties["endpoint_count"] = { int64_t( c.endpoints.size() ) };
688 for( size_t i = 0; i < c.endpoints.size(); ++i )
689 {
690 const auto prefix = "endpoint_" + std::to_string( i ) + "_";
691 r.properties[prefix + "kind"] = canonicalEndpointKind( c.endpoints[i].kind );
692 addSourceProperties( r, c.endpoints[i].properties, prefix );
693
694 if( c.endpoints[i].placement )
695 {
696 auto placement = std::ranges::find_if( aModel.placements,
697 [&]( const MODEL_PLACEMENT& aPlacement )
698 {
699 return aPlacement.id == c.endpoints[i].placement->id;
700 } );
701 r.properties[prefix + "placement"] =
702 placement == aModel.placements.end()
703 ? unknownEnum( c.endpoints[i].placement->id.Value() )
704 : CANONICAL_PROPERTY{ placement->reference.text.ToStdString() };
705 }
706
707 if( c.endpoints[i].pin )
708 {
709 auto definition = std::ranges::find_if( aModel.definitions,
710 [&]( const MODEL_SYMBOL_DEFINITION& aDefinition )
711 {
712 return std::ranges::any_of(
713 aDefinition.pins,
714 [&]( const MODEL_PIN_DEFINITION& aPin )
715 {
716 return aPin.id == c.endpoints[i].pin->id;
717 } );
718 } );
719 r.properties[prefix + "pin_number"] =
720 definition == aModel.definitions.end()
721 ? unknownEnum( c.endpoints[i].pin->id.Value() )
722 : CANONICAL_PROPERTY{ std::ranges::find_if( definition->pins,
723 [&]( const MODEL_PIN_DEFINITION& aPin )
724 {
725 return aPin.id
726 == c.endpoints[i].pin->id;
727 } )
728 ->number.text.ToStdString() };
729 }
730 }
731 for( const auto& p : c.vertices )
732 r.geometry.points.push_back( point( p ) );
733 addSourceProperties( r, c.properties );
734 }
735 }
736 for( const auto& b : aModel.buses )
737 {
738 auto netName = [&]( NET_ID aId ) -> CANONICAL_PROPERTY
739 {
740 auto net = std::ranges::find_if( aModel.nets,
741 [&]( const MODEL_NET& aNet )
742 {
743 return aNet.id == aId;
744 } );
745
746 if( net == aModel.nets.end() )
747 return unknownEnum( aId.Value() );
748
749 return { net->name.text.ToStdString() };
750 };
751 auto& r = addOwned( CANONICAL_KIND::BUS, b.sheet, b.name.text.ToStdString() );
752 std::vector<std::string> aliases;
753 std::vector<std::string> members;
754
755 for( const SOURCE_STRING& alias : b.aliases )
756 aliases.push_back( alias.text.ToStdString() );
757
758 for( const NET_REFERENCE& member : b.memberNets )
759 members.push_back( std::get<std::string>( netName( member.id ).value ) );
760
761 r.properties["aliases"] = { aliases };
762 r.properties["member_nets"] = { members };
763 for( const auto& p : b.vertices )
764 r.geometry.points.push_back( point( p ) );
765 addSourceProperties( r, b.properties );
766 for( size_t i = 0; i < b.entries.size(); ++i )
767 {
768 auto& e = addOwned( CANONICAL_KIND::BUS_ENTRY, b.sheet, std::to_string( i ) );
769 e.properties["member_index"] = { int64_t( i ) };
770 e.properties["member_net"] = netName( b.entries[i].memberNet.id );
771 e.geometry.points.push_back( point( b.entries[i].position ) );
772 addSourceProperties( e, b.entries[i].properties );
773 }
774 for( const auto& a : b.aliases )
775 addOwned( CANONICAL_KIND::BUS_ALIAS, b.sheet, a.text.ToStdString() );
776 for( size_t i = 0; i < b.memberNets.size(); ++i )
777 {
778 auto& member = addOwned( CANONICAL_KIND::BUS_MEMBER, b.sheet, std::to_string( i ) );
779 member.properties["member_net"] = netName( b.memberNets[i].id );
780 }
781 }
782 for( const auto& l : aModel.labels )
783 {
784 auto& r = addOwned( CANONICAL_KIND::LABEL, l.sheet, l.text.text.ToStdString() );
785 r.properties["kind"] = canonicalLabelKind( l.kind );
786 std::vector<std::string> linkedSheets;
787
788 for( const SHEET_REFERENCE& linked : l.linkedSheets )
789 {
790 auto sheet = std::ranges::find( aModel.sheets, linked.id, &MODEL_SHEET::id );
791
792 if( sheet != aModel.sheets.end() )
793 linkedSheets.push_back( std::to_string( sheet->index ) );
794 }
795
796 std::ranges::sort( linkedSheets );
797 r.properties["linked_sheets"] = { linkedSheets };
798 r.geometry.points.push_back( point( l.position ) );
799 r.geometry.angleTenths = canonicalAngle( l.angle );
800 addSourceProperties( r, l.properties );
801 }
802 for( const auto& j : aModel.junctions )
803 {
804 auto& r = addOwned( CANONICAL_KIND::JUNCTION, j.sheet );
805 r.geometry.points.push_back( point( j.position ) );
806 addSourceProperties( r, j.properties );
807 }
808 for( const auto& t : aModel.texts )
809 {
810 auto& r = addOwned( CANONICAL_KIND::TEXT, t.sheet, t.text.text.ToStdString() );
811 r.properties["visible"] = { t.presentation.visible };
812 r.properties["font"] = { t.presentation.font.text.ToStdString() };
813 r.properties["bold"] = { t.presentation.bold };
814 r.properties["italic"] = { t.presentation.italic };
815 r.properties["underline"] = { t.presentation.underline };
816 r.properties["horizontal_justification"] = canonicalJustification( t.presentation.horizontalJustification );
817 r.properties["vertical_justification"] = canonicalVerticalJustification( t.presentation.verticalJustification );
818 r.geometry.points.push_back( point( t.position ) );
819 r.geometry.angleTenths = canonicalAngle( t.angle );
820 addSourceProperties( r, t.properties );
821 }
822 for( const MODEL_PAGE_GRAPHIC& pageGraphic : aModel.graphics )
823 {
824 auto& record = addOwned( CANONICAL_KIND::GRAPHIC, pageGraphic.sheet );
825 int sheet = record.sheet;
826 auto owner = record.properties.at( "owner_sheet" );
827 out.pop_back();
828 graphic( pageGraphic.graphic, sheet );
829 out.back().properties["owner_sheet"] = owner;
830 }
831 for( const MODEL_WORKSHEET& worksheet : aModel.worksheets )
832 {
833 auto& record = addOwned( CANONICAL_KIND::GRAPHIC, worksheet.sheet );
834 int sheet = record.sheet;
835 auto owner = record.properties.at( "owner_sheet" );
836 out.pop_back();
837
838 for( const MODEL_GRAPHIC& worksheetGraphic : worksheet.graphics )
839 {
840 graphic( worksheetGraphic, sheet );
841 out.back().properties["owner_sheet"] = owner;
842 }
843 }
844 return out;
845}
846
847// Binary resolves the default font in system-font mode; ASCII names its face with a style prefix
848static std::string canonicalAsciiFont( const PADS_SCH::PARAMETERS& aParameters, const std::string& aFont )
849{
850 if( aParameters.font_mode != "System" || aFont != "Default Font" || aParameters.default_font.empty() )
851 return aFont;
852
853 const std::string& face = aParameters.default_font;
854 return face.starts_with( "Regular " ) ? face.substr( 8 ) : face;
855}
856
857
858static std::vector<CANONICAL_SEMANTIC_RECORD> normalizeAsciiModel( const PADS_SCH::PADS_SCH_PARSER& aParser )
859{
860 std::vector<CANONICAL_SEMANTIC_RECORD> out;
861 auto add = [&]( CANONICAL_KIND k, int s, std::string key = {} ) -> CANONICAL_SEMANTIC_RECORD&
862 {
863 out.push_back( { s, k, std::move( key ) } );
864 return out.back();
865 };
866 const auto& p = aParser.GetParameters();
867 auto addOwned = [&]( CANONICAL_KIND aKind, int aSheet, std::string aKey = {} ) -> CANONICAL_SEMANTIC_RECORD&
868 {
869 auto sheet = std::ranges::find_if( aParser.GetSheetHeaders(),
870 [&]( const PADS_SCH::SHEET_HEADER& aCandidate )
871 {
872 return aCandidate.sheet_num - 1 == aSheet;
873 } );
874 auto& record = add( aKind, aSheet, std::move( aKey ) );
875 record.properties["owner_sheet"] =
876 sheet == aParser.GetSheetHeaders().end()
877 ? CANONICAL_PROPERTY{ "unknown:" + std::to_string( aSheet ), PROPERTY_DISPOSITION::UNSUPPORTED }
878 : CANONICAL_PROPERTY{ sheet->sheet_name };
879 return record;
880 };
881 auto& settings = add( CANONICAL_KIND::SETTINGS, -1 );
882 settings.properties["coordinate_units_per_mil"] = { int64_t( 2 ) };
883
884 if( !p.font_mode.empty() )
885 {
886 settings.properties["font_mode_raw"] =
887 p.font_mode == "System"
888 ? CANONICAL_PROPERTY{ std::string( "1" ) }
889 : CANONICAL_PROPERTY{ "unknown:" + p.font_mode, PROPERTY_DISPOSITION::UNSUPPORTED };
890 }
891
892 settings.properties["line_width_half_mils"] = { int64_t( std::llround( p.line_width * 2 ) ) };
893 settings.properties["bus_width_half_mils"] = { int64_t( p.bus_width * 2 ) };
894 settings.geometry.points.push_back( point( PADS_SCH::POINT{ p.sheet_size.width, p.sheet_size.height } ) );
895 auto addGraphic = [&]( const PADS_SCH::SYMBOL_GRAPHIC& aGraphic, int aSheet )
896 {
897 auto& r = add( CANONICAL_KIND::GRAPHIC, aSheet );
898 r.properties["type"] = canonicalGraphicType( aGraphic.type );
899 r.properties["line_style"] = canonicalLineStyle( aGraphic.line_style );
900 r.properties["stroke_half_mils"] = { int64_t( std::llround( aGraphic.line_width * 2 ) ) };
901 r.properties["fill"] = canonicalFill( aGraphic.filled );
902 r.properties["text"] = { std::string() };
903 r.properties["font"] = { std::string() };
904 r.properties["visible"] = { true };
905 r.properties["height_half_mils"] = { int64_t( 0 ) };
906 r.properties["width_half_mils"] = { int64_t( 0 ) };
907
908 for( const auto& q : aGraphic.points )
909 r.geometry.points.push_back( point( q.coord ) );
910
911 auto arcPoint = std::ranges::find_if( aGraphic.points,
912 []( const PADS_SCH::GRAPHIC_POINT& aPoint )
913 {
914 return aPoint.arc.has_value();
915 } );
916
917 if( arcPoint != aGraphic.points.end() )
918 {
919 const PADS_SCH::ARC_DATA& arc = *arcPoint->arc;
920 r.properties["arc_center_x_half_mils"] = { int64_t( std::llround( ( arc.bbox_x1 + arc.bbox_x2 ) ) ) };
921 r.properties["arc_center_y_half_mils"] = { int64_t( std::llround( ( arc.bbox_y1 + arc.bbox_y2 ) ) ) };
922 r.properties["arc_bounds_x1_half_mils"] = { int64_t( std::llround( arc.bbox_x1 * 2 ) ) };
923 r.properties["arc_bounds_y1_half_mils"] = { int64_t( std::llround( arc.bbox_y1 * 2 ) ) };
924 r.properties["arc_bounds_x2_half_mils"] = { int64_t( std::llround( arc.bbox_x2 * 2 ) ) };
925 r.properties["arc_bounds_y2_half_mils"] = { int64_t( std::llround( arc.bbox_y2 * 2 ) ) };
926 r.properties["arc_sweep_tenths"] = { int64_t( std::llround( std::abs( arc.bulge ) ) ) };
927 r.properties["arc_clockwise"] = { arc.angle < 0 };
928 }
929 };
930 for( const auto& s : aParser.GetSheetHeaders() )
931 {
932 auto& r = add( CANONICAL_KIND::SHEET, s.sheet_num - 1, s.sheet_name );
933 r.properties["title"] = { std::string() };
934 r.properties["line_width_half_mils"] = { int64_t( std::llround( p.line_width * 2 ) ) };
935 r.properties["bus_width_half_mils"] = { int64_t( p.bus_width * 2 ) };
936 r.geometry.points.push_back( point( PADS_SCH::POINT{ p.sheet_size.width, p.sheet_size.height } ) );
937 }
938 for( const auto& d : aParser.GetSymbolDefs() )
939 {
940 add( CANONICAL_KIND::DEFINITION, -1, d.name );
941 for( const auto& g : d.graphics )
942 addGraphic( g, -1 );
943 std::vector<PADS_SCH::SYMBOL_PIN> pins = d.pins;
944
945 for( const auto& [partName, part] : aParser.GetPartTypes() )
946 {
947 for( const PADS_SCH::GATE_DEF& gate : part.gates )
948 {
949 if( gate.decal_names.empty() || gate.decal_names.front() != d.name || gate.pins.size() != pins.size() )
950 continue;
951
952 for( size_t i = 0; i < pins.size(); ++i )
953 {
954 pins[i].number = gate.pins[i].pin_id;
955 pins[i].name = gate.pins[i].pin_name;
956 switch( gate.pins[i].pin_type )
957 {
958 case 'L': pins[i].type = PADS_SCH::PIN_TYPE::INPUT; break;
959 case 'S': pins[i].type = PADS_SCH::PIN_TYPE::OUTPUT; break;
960 case 'B': pins[i].type = PADS_SCH::PIN_TYPE::BIDIRECTIONAL; break;
961 case 'T': pins[i].type = PADS_SCH::PIN_TYPE::TRISTATE; break;
962 case 'C': pins[i].type = PADS_SCH::PIN_TYPE::OPEN_COLLECTOR; break;
963 case 'E': pins[i].type = PADS_SCH::PIN_TYPE::OPEN_EMITTER; break;
964 case 'P':
965 case 'G': pins[i].type = PADS_SCH::PIN_TYPE::POWER; break;
966 case 'U': pins[i].type = PADS_SCH::PIN_TYPE::PASSIVE; break;
967 default: pins[i].type = PADS_SCH::PIN_TYPE::UNSPECIFIED; break;
968 }
969 }
970
971 break;
972 }
973 }
974
975 for( const auto& pin : pins )
976 {
977 auto& r = add( CANONICAL_KIND::PIN, -1, pin.number );
978 r.properties["name"] = { pin.name };
979 r.properties["type"] = canonicalPinType( pin.type );
980 r.properties["style"] = canonicalPinStyle( pin.inverted, pin.clock );
981 r.properties["length_half_mils"] = { int64_t( std::llround( pin.length * 2 ) ) };
982 r.geometry.points.push_back( point( pin.position ) );
983 r.geometry.angleTenths = canonicalAngle( std::llround( pin.rotation * 10 ) );
984 }
985 for( const auto& t : d.texts )
986 {
987 auto& r = add( CANONICAL_KIND::GRAPHIC, -1 );
988 r.properties["type"] = { "text" };
989 r.properties["line_style"] = { "solid" };
990 r.properties["stroke_half_mils"] = { int64_t( 0 ) };
991 r.properties["fill"] = { "none" };
992 r.properties["text"] = { t.content };
993 r.properties["visible"] = { t.visible };
994 r.properties["font"] = { canonicalAsciiFont( p, t.font_name ) };
995 r.properties["height_half_mils"] = { int64_t( std::llround( t.size * 2 ) ) };
996 r.properties["width_half_mils"] = { int64_t( t.width_factor * 2 ) };
997 r.geometry.points.push_back( point( t.position ) );
998 r.geometry.angleTenths = canonicalAngle( std::llround( t.rotation * 10 ) );
999 }
1000 }
1001 for( const auto& [name, part] : aParser.GetPartTypes() )
1002 {
1003 add( CANONICAL_KIND::PART_TYPE, -1, name );
1004 for( size_t i = 0; i < part.gates.size(); ++i )
1005 {
1006 auto& gate = add( CANONICAL_KIND::GATE, -1, std::to_string( i + 1 ) );
1007 gate.properties["definition"] = { part.gates[i].decal_names.empty() ? std::string()
1008 : part.gates[i].decal_names.front() };
1009 for( size_t j = 0; j < part.gates[i].pins.size(); ++j )
1010 {
1011 auto& r = add( CANONICAL_KIND::GATE_PIN_MAPPING, -1,
1012 std::to_string( i + 1 ) + ":" + std::to_string( j ) );
1013 r.properties["pin_number"] = { part.gates[i].pins[j].pin_id };
1014 r.properties["pin_name"] = { part.gates[i].pins[j].pin_name };
1015 }
1016 }
1017 }
1018 for( const auto& x : aParser.GetPartPlacements() )
1019 {
1020 auto& r = addOwned( CANONICAL_KIND::PLACEMENT, x.sheet_number - 1, x.reference );
1021 r.properties["unit"] = { int64_t( x.gate_number ) };
1022 r.properties["mirrored"] = { x.mirror_flags != 0 };
1023 r.geometry.points.push_back( point( x.position ) );
1024 r.geometry.angleTenths = canonicalAngle( std::llround( x.rotation * 10 ) );
1025 int ownerSheet = r.sheet;
1026 for( const auto& f : x.attributes )
1027 {
1028 auto& q = add( CANONICAL_KIND::FIELD, ownerSheet, f.name );
1029 q.properties["value"] = { f.value };
1030 q.properties["visible"] = { f.visible };
1031 q.properties["font"] = { canonicalAsciiFont( p, f.font_name ) };
1032 q.geometry.points.push_back( point( f.position ) );
1033 q.geometry.angleTenths = canonicalAngle( std::llround( f.rotation * 10 ) );
1034 }
1035 }
1036 for( const auto& n : aParser.GetSignals() )
1037 {
1038 if( n.flags1 == 5 && n.flags2 == 3 )
1039 continue;
1040
1041 int netSheet = n.wires.empty() ? -1 : n.wires.front().sheet_number - 1;
1042 addOwned( CANONICAL_KIND::NET, netSheet, n.name );
1043 for( const auto& c : n.wires )
1044 {
1045 auto& r = addOwned( CANONICAL_KIND::CONNECTION, c.sheet_number - 1 );
1046 r.properties["endpoint_count"] = { int64_t( 2 ) };
1047
1048 const std::array<std::string, 2> endpoints = { c.endpoint_a, c.endpoint_b };
1049
1050 for( size_t endpoint = 0; endpoint < endpoints.size(); ++endpoint )
1051 {
1052 const std::string prefix = "endpoint_" + std::to_string( endpoint ) + "_";
1053 const std::string& token = endpoints[endpoint];
1054
1055 if( token.starts_with( "@@@" ) )
1056 {
1057 r.properties[prefix + "kind"] = canonicalEndpointKind( MODEL_ENDPOINT_KIND::POINT );
1058 }
1059 else
1060 {
1061 const size_t separator = token.rfind( '.' );
1062 r.properties[prefix + "kind"] = canonicalEndpointKind( MODEL_ENDPOINT_KIND::PIN );
1063 r.properties[prefix + "placement"] = { token.substr( 0, separator ) };
1064 r.properties[prefix + "pin_number"] = { token.substr( separator + 1 ) };
1065 }
1066 }
1067
1068 for( const auto& q : c.vertices )
1069 r.geometry.points.push_back( point( q ) );
1070 }
1071 }
1072 for( const auto& b : aParser.GetBuses() )
1073 {
1074 auto& r = addOwned( CANONICAL_KIND::BUS, b.sheet_number - 1, b.name );
1075 r.properties["aliases"] = { b.aliases };
1076 r.properties["member_nets"] = { b.member_nets };
1077 for( const auto& q : b.path )
1078 r.geometry.points.push_back( point( q ) );
1079 for( size_t i = 0; i < b.entries.size(); ++i )
1080 {
1081 auto& e = addOwned( CANONICAL_KIND::BUS_ENTRY, b.sheet_number - 1, std::to_string( i ) );
1082 e.properties["member_index"] = { int64_t( i ) };
1083 e.properties["member_net"] = { b.entries[i].member_net };
1084 e.geometry.points.push_back( point( b.entries[i].position ) );
1085 }
1086 for( const auto& a : b.aliases )
1087 addOwned( CANONICAL_KIND::BUS_ALIAS, b.sheet_number - 1, a );
1088 for( size_t i = 0; i < b.member_nets.size(); ++i )
1089 {
1090 auto& member = addOwned( CANONICAL_KIND::BUS_MEMBER, b.sheet_number - 1, std::to_string( i ) );
1091 member.properties["member_net"] = { b.member_nets[i] };
1092
1093 if( std::ranges::none_of( aParser.GetSignals(),
1094 [&]( const PADS_SCH::SCH_SIGNAL& aSignal )
1095 {
1096 return aSignal.name == b.member_nets[i];
1097 } ) )
1098 {
1099 addOwned( CANONICAL_KIND::NET, b.sheet_number - 1, b.member_nets[i] );
1100 }
1101 }
1102 }
1103 for( const auto& l : aParser.GetOffPageConnectors() )
1104 {
1105 if( l.symbol_lib.starts_with( "@@@B" ) )
1106 continue;
1107 auto& r = addOwned( CANONICAL_KIND::LABEL, l.source_sheet - 1, l.signal_name );
1108 r.properties["kind"] = { l.symbol_lib == "@TERM" ? "local"
1109 : l.symbol_lib.starts_with( "$OSR" ) ? "global"
1110 : l.symbol_lib.starts_with( "$GND" ) ? "ground"
1111 : l.symbol_lib.starts_with( "$PWR" ) ? "power"
1112 : "unsupported" };
1113 std::vector<std::string> linkedSheets;
1114
1115 if( l.symbol_lib.starts_with( "$OSR" ) )
1116 {
1117 for( const auto& peer : aParser.GetOffPageConnectors() )
1118 {
1119 if( peer.signal_name != l.signal_name || peer.source_sheet == l.source_sheet
1120 || !peer.symbol_lib.starts_with( "$OSR" ) )
1121 {
1122 continue;
1123 }
1124
1125 auto sheet = std::ranges::find_if( aParser.GetSheetHeaders(),
1126 [&]( const PADS_SCH::SHEET_HEADER& aHeader )
1127 {
1128 return aHeader.sheet_num == peer.source_sheet;
1129 } );
1130
1131 if( sheet != aParser.GetSheetHeaders().end() )
1132 linkedSheets.push_back( std::to_string( sheet->sheet_num - 1 ) );
1133 }
1134 }
1135
1136 std::ranges::sort( linkedSheets );
1137 r.properties["linked_sheets"] = { linkedSheets };
1138 r.geometry.points.push_back( point( l.position ) );
1139 r.geometry.angleTenths = canonicalAngle( l.rotation * 10 );
1140 }
1141 for( const auto& j : aParser.GetTiedDots() )
1142 addOwned( CANONICAL_KIND::JUNCTION, j.sheet_number - 1 ).geometry.points.push_back( point( j.position ) );
1143 for( const auto& t : aParser.GetTextItems() )
1144 {
1145 auto& r = addOwned( CANONICAL_KIND::TEXT, t.sheet_number - 1, t.content );
1146 r.properties["visible"] = { true };
1147 r.properties["bold"] = { false };
1148 r.properties["italic"] = { false };
1149 r.properties["underline"] = { false };
1150 r.geometry.points.push_back( point( t.position ) );
1151 r.geometry.angleTenths = canonicalAngle( t.rotation * 10 );
1152 }
1153 for( const auto& lines : aParser.GetLinesItems() )
1154 {
1155 for( const auto& g : lines.primitives )
1156 {
1157 auto& owner = addOwned( CANONICAL_KIND::GRAPHIC, lines.sheet_number - 1 );
1158 auto property = owner.properties.at( "owner_sheet" );
1159 out.pop_back();
1160 addGraphic( g, lines.sheet_number - 1 );
1161 out.back().properties["owner_sheet"] = property;
1162 out.back().properties["page_graphic_group"] = { lines.name };
1163 out.back().properties["type"] = canonicalGraphicType( g );
1164
1165 for( CANONICAL_POINT& graphicPoint : out.back().geometry.points )
1166 {
1167 graphicPoint.xHalfMils += std::llround( lines.origin.x * 2 );
1168 graphicPoint.yHalfMils += std::llround( lines.origin.y * 2 );
1169 }
1170
1171 for( const std::string& name :
1172 { "arc_center_x_half_mils", "arc_bounds_x1_half_mils", "arc_bounds_x2_half_mils" } )
1173 {
1174 auto property = out.back().properties.find( name );
1175
1176 if( property != out.back().properties.end() )
1177 std::get<int64_t>( property->second.value ) += std::llround( lines.origin.x * 2 );
1178 }
1179
1180 for( const std::string& name :
1181 { "arc_center_y_half_mils", "arc_bounds_y1_half_mils", "arc_bounds_y2_half_mils" } )
1182 {
1183 auto property = out.back().properties.find( name );
1184
1185 if( property != out.back().properties.end() )
1186 std::get<int64_t>( property->second.value ) += std::llround( lines.origin.y * 2 );
1187 }
1188
1189 if( g.type == PADS_SCH::GRAPHIC_TYPE::RECTANGLE )
1190 {
1191 out.back().properties["type"] = canonicalGraphicType( MODEL_GRAPHIC_KIND::POLYLINE );
1192
1193 if( out.back().geometry.points.size() == 2 )
1194 {
1195 const CANONICAL_POINT first = out.back().geometry.points.front();
1196 const CANONICAL_POINT last = out.back().geometry.points.back();
1197 out.back().geometry.points = { { last.xHalfMils, last.yHalfMils },
1198 { first.xHalfMils, last.yHalfMils },
1199 { first.xHalfMils, first.yHalfMils },
1200 { last.xHalfMils, first.yHalfMils },
1201 { last.xHalfMils, last.yHalfMils } };
1202 }
1203 }
1204
1205 out.back().geometry.angleTenths = 0;
1206 }
1207
1208 for( const auto& t : lines.texts )
1209 {
1210 auto& r = addOwned( CANONICAL_KIND::GRAPHIC, lines.sheet_number - 1 );
1211 r.properties["type"] = canonicalGraphicType( MODEL_GRAPHIC_KIND::TEXT );
1212 r.properties["line_style"] = canonicalLineStyle( MODEL_LINE_STYLE::DEFAULT );
1213 r.properties["stroke_half_mils"] = { int64_t( 0 ) };
1214 r.properties["fill"] = canonicalFill( MODEL_FILL_STYLE::NONE );
1215 r.properties["text"] = { t.content };
1216 r.properties["page_graphic_group"] = { lines.name };
1217 r.properties["visible"] = { true };
1218 r.properties["height_half_mils"] = { int64_t( t.height * 2 ) };
1219 r.properties["width_half_mils"] = { int64_t( t.width_factor * 2 ) };
1220 r.properties["font"] = { canonicalAsciiFont( p, t.font_name ) };
1221 r.geometry.points.push_back( point( t.position ) );
1222 r.geometry.points.back().xHalfMils += std::llround( lines.origin.x * 2 );
1223 r.geometry.points.back().yHalfMils += std::llround( lines.origin.y * 2 );
1224 r.geometry.angleTenths = canonicalAngle( t.rotation * 10 );
1225 }
1226 }
1227 for( const auto& label : aParser.GetNetNameLabels() )
1228 {
1229 auto& r = addOwned( CANONICAL_KIND::LABEL, -1, label.net_name );
1230 r.properties["kind"] = { "local" };
1231 r.properties["visible"] = { true };
1232 r.properties["font"] = { canonicalAsciiFont( p, label.font_name ) };
1233 r.properties["horizontal_justification"] = canonicalHorizontalJustification( label.justification );
1234 r.properties["vertical_justification"] = canonicalVerticalJustification( label.justification );
1235 r.geometry.points.push_back( point( PADS_SCH::POINT{ double( label.x_offset ), double( label.y_offset ) } ) );
1236 r.geometry.angleTenths = canonicalAngle( label.rotation * 10 );
1237 }
1238 return out;
1239}
1240
1241static bool snapshotsMatch( std::vector<CANONICAL_SEMANTIC_RECORD> aExpected,
1242 std::vector<CANONICAL_SEMANTIC_RECORD> aActual,
1243 const SNAPSHOT_ALLOWLIST& aAllowedDifferences = {} )
1244{
1245 auto strip = [&]( auto& records )
1246 {
1247 for( auto& record : records )
1248 std::erase_if( record.properties,
1249 [&]( const auto& p )
1250 {
1251 return aAllowedDifferences.contains( { p.first, p.second.disposition } );
1252 } );
1253 std::sort( records.begin(), records.end() );
1254 };
1255 strip( aExpected );
1256 strip( aActual );
1257 return aExpected == aActual;
1258}
1259
1260
1261static bool task10SourcePropertiesPresent( const std::vector<CANONICAL_SEMANTIC_RECORD>& aRecords )
1262{
1263 auto has =
1264 []( const CANONICAL_SEMANTIC_RECORD& aRecord, const std::string& aName, PROPERTY_DISPOSITION aDisposition )
1265 {
1266 auto property = aRecord.properties.find( aName );
1267 return property != aRecord.properties.end() && property->second.disposition == aDisposition;
1268 };
1269
1270 for( const CANONICAL_SEMANTIC_RECORD& record : aRecords )
1271 {
1272 if( record.kind == CANONICAL_KIND::NET
1273 && ( !has( record, "global_net_record", PROPERTY_DISPOSITION::EXACT )
1274 || !has( record, "preserved_net_identity", PROPERTY_DISPOSITION::PRESERVED )
1275 || !has( record, "preserved_net_relationship", PROPERTY_DISPOSITION::PRESERVED ) ) )
1276 {
1277 return false;
1278 }
1279
1280 if( record.kind == CANONICAL_KIND::CONNECTION
1281 && ( !has( record, "endpoint_0_raw_endpoint_handle", PROPERTY_DISPOSITION::EXACT )
1282 || !has( record, "endpoint_1_raw_endpoint_handle", PROPERTY_DISPOSITION::EXACT )
1283 || !has( record, "endpoint_0_raw_endpoint_relationship", PROPERTY_DISPOSITION::PRESERVED )
1284 || !has( record, "endpoint_1_raw_endpoint_relationship", PROPERTY_DISPOSITION::PRESERVED )
1285 || !has( record, "raw_connection_marker", PROPERTY_DISPOSITION::EXACT ) ) )
1286 {
1287 return false;
1288 }
1289
1290 if( record.kind == CANONICAL_KIND::LABEL && !has( record, "offpage_variant", PROPERTY_DISPOSITION::EXACT ) )
1291 {
1292 return false;
1293 }
1294
1295 if( record.kind == CANONICAL_KIND::JUNCTION
1296 && !has( record, "connection_record", PROPERTY_DISPOSITION::EXACT ) )
1297 {
1298 return false;
1299 }
1300
1301 if( record.kind == CANONICAL_KIND::BUS
1302 && ( !has( record, "preserved_net_identity", PROPERTY_DISPOSITION::PRESERVED )
1303 || !has( record, "preserved_net_relationship", PROPERTY_DISPOSITION::PRESERVED ) ) )
1304 {
1305 return false;
1306 }
1307 }
1308
1309 return true;
1310}
1311
1312} // namespace
1313
1314
1315BOOST_AUTO_TEST_SUITE( PadsSchBinaryParser )
1316
1317
1318BOOST_AUTO_TEST_CASE( EmbeddedOleImages )
1319{
1321 std::vector<uint8_t> bytes = loadBinaryFixture( "ole_images.sch" );
1322 PADS_SCH_MODEL model = parser.Parse( bytes, wxS( "ole_images.sch" ) );
1323
1324 BOOST_REQUIRE_EQUAL( model.images.size(), 2 );
1325 BOOST_CHECK_EQUAL( model.images[0].id.Value(), 0 );
1326 BOOST_CHECK_EQUAL( model.images[0].sheet.id.Value(), 1 );
1328 BOOST_CHECK_EQUAL( model.images[0].streamName, wxS( "\\x01Ole10Native" ) );
1329 BOOST_CHECK( ( model.images[0].extent == std::array<int32_t, 4>{ 30, 30, 350, 210 } ) );
1330 BOOST_CHECK( ( model.images[0].databaseBox == std::array<int32_t, 4>{ -15766, -10366, -13296, -11755 } ) );
1331 BOOST_CHECK_EQUAL( model.images[0].position.x, 5876 );
1332 BOOST_CHECK_EQUAL( model.images[0].position.y, 19758 );
1333 BOOST_CHECK_EQUAL( model.images[0].size.x, 9880 );
1334 BOOST_CHECK_EQUAL( model.images[0].size.y, 5556 );
1335 BOOST_CHECK_EQUAL( model.images[0].flags, 1 );
1336 BOOST_REQUIRE_GE( model.images[0].data.size(), 2 );
1337 BOOST_CHECK_EQUAL( model.images[0].data[0], 'B' );
1338 BOOST_CHECK_EQUAL( model.images[0].data[1], 'M' );
1339 BOOST_CHECK_EQUAL( model.images[0].source.objectClass, wxS( "embedded OLE image" ) );
1340 BOOST_CHECK_EQUAL( model.images[0].source.recordIndex, 0 );
1341 BOOST_CHECK_GT( model.images[0].source.absoluteOffset, 0 );
1342 BOOST_CHECK_GT( model.images[0].source.length, 512 );
1343
1344 BOOST_CHECK_EQUAL( model.images[1].id.Value(), 1 );
1345 BOOST_CHECK_EQUAL( model.images[1].sheet.id.Value(), 1 );
1347 BOOST_CHECK_EQUAL( model.images[1].streamName, wxS( "\\x01Ole10Native" ) );
1348 BOOST_CHECK( ( model.images[1].extent == std::array<int32_t, 4>{ 30, 30, 170, 84 } ) );
1349 BOOST_CHECK( ( model.images[1].databaseBox == std::array<int32_t, 4>{ -15766, -10366, -14686, -10782 } ) );
1350 BOOST_CHECK_EQUAL( model.images[1].position.x, 3096 );
1351 BOOST_CHECK_EQUAL( model.images[1].position.y, 21704 );
1352 BOOST_CHECK_EQUAL( model.images[1].size.x, 4320 );
1353 BOOST_CHECK_EQUAL( model.images[1].size.y, 1664 );
1354 BOOST_CHECK_EQUAL( model.images[1].flags, 1 );
1355 BOOST_REQUIRE_GE( model.images[1].data.size(), 4 );
1356 BOOST_CHECK_EQUAL( model.images[1].data[0], 0xD7 );
1357 BOOST_CHECK_EQUAL( model.images[1].data[1], 0xCD );
1358 BOOST_CHECK_EQUAL( model.images[1].data[2], 0xC6 );
1359 BOOST_CHECK_EQUAL( model.images[1].data[3], 0x9A );
1360
1361 PADS_SCH_SDB sdb;
1362 sdb.Load( bytes );
1363 BOOST_REQUIRE_EQUAL( sdb.OleItems().size(), 2u );
1364 std::vector<uint8_t> zeroWidth = bytes;
1365 writeU32( zeroWidth, sdb.OleItems()[0].boxOffset + 8, static_cast<uint32_t>( sdb.OleItems()[0].left ) );
1366 PADS_SCH_MODEL zeroWidthModel = parser.Parse( zeroWidth, wxS( "ole-zero-width.sch" ) );
1367 BOOST_REQUIRE_EQUAL( zeroWidthModel.images.size(), 2 );
1368 BOOST_CHECK( zeroWidthModel.images[0].type == MODEL_EMBEDDED_IMAGE_TYPE::UNSUPPORTED );
1369 BOOST_CHECK( std::ranges::any_of( zeroWidthModel.diagnostics,
1370 []( const PARSER_DIAGNOSTIC& aDiagnostic )
1371 {
1372 return aDiagnostic.source.objectClass
1373 == wxS( "embedded OLE image database box" )
1374 && aDiagnostic.message.Contains( wxS( "zero-size" ) );
1375 } ) );
1376}
1377
1378
1379BOOST_AUTO_TEST_CASE( GlobalsAndSheets )
1380{
1382 PADS_SCH_MODEL minimal = parser.Parse( loadBinaryFixture( "minimal_v13.sch" ), wxS( "minimal_v13.sch" ) );
1383
1384 BOOST_CHECK_EQUAL( minimal.version, 0x000D );
1385 BOOST_CHECK_EQUAL( minimal.subversion, 0 );
1386 BOOST_CHECK_EQUAL( minimal.settings.codePage, 1252 );
1388 BOOST_CHECK_EQUAL( minimal.settings.pageSize.x, 34000 );
1389 BOOST_CHECK_EQUAL( minimal.settings.pageSize.y, 22000 );
1392 BOOST_CHECK_EQUAL( minimal.settings.source.objectClass, wxS( "design settings" ) );
1395 BOOST_CHECK_EQUAL( minimal.settings.source.length, 400 );
1396 BOOST_CHECK_EQUAL( minimal.settings.source.version, 0x000D );
1397 BOOST_REQUIRE_EQUAL( minimal.sheets.size(), 1 );
1398 BOOST_CHECK_EQUAL( minimal.sheets[0].id.Value(), 1 );
1399 BOOST_CHECK_EQUAL( minimal.sheets[0].index, 0 );
1400 BOOST_CHECK_EQUAL( minimal.sheets[0].name.text, wxS( "$$$NONE" ) );
1401 BOOST_CHECK( !minimal.sheets[0].parent );
1402 BOOST_CHECK_EQUAL( minimal.sheets[0].pageSize.x, 34000 );
1403 BOOST_CHECK_EQUAL( minimal.sheets[0].defaultLineWidth, 20 );
1404 BOOST_REQUIRE_EQUAL( minimal.sheets[0].titleBlockFields.size(), 14 );
1405 BOOST_CHECK_EQUAL( minimal.sheets[0].titleBlockFields.front().name.text, wxS( "Drawn By" ) );
1406 BOOST_CHECK_EQUAL( minimal.sheets[0].titleBlockFields.back().name.text, wxS( "Scale" ) );
1407
1408 const std::array<wxString, 14> titleNames = { wxS( "Drawn By" ), wxS( "Checked By" ), wxS( "QC By" ),
1409 wxS( "Released By" ), wxS( "Drawn Date" ), wxS( "Checked Date" ),
1410 wxS( "QC Date" ), wxS( "Release Date" ), wxS( "Company Name" ),
1411 wxS( "Title" ), wxS( "Code" ), wxS( "Drawing Number" ),
1412 wxS( "Revision" ), wxS( "Scale" ) };
1413 const std::array<size_t, 14> titleOffsets = { 0x254, 0x264, 0x276, 0x283, 0x296, 0x2A8, 0x2BC,
1414 0x2CB, 0x2DF, 0x2F3, 0x300, 0x30C, 0x322, 0x332 };
1415 const std::array<size_t, 14> titleLengths = { 16, 18, 13, 19, 18, 20, 15, 20, 20, 13, 12, 22, 16, 13 };
1416
1417 for( size_t i = 0; i < titleNames.size(); ++i )
1418 {
1419 const MODEL_FIELD& field = minimal.sheets[0].titleBlockFields[i];
1420 const std::string expectedName = titleNames[i].ToStdString();
1421 BOOST_CHECK_EQUAL( field.name.text, titleNames[i] );
1422 BOOST_CHECK_EQUAL( field.value.text, wxString() );
1423 BOOST_CHECK_EQUAL_COLLECTIONS( field.name.raw.begin(), field.name.raw.end(), expectedName.begin(),
1424 expectedName.end() );
1425 BOOST_CHECK( field.value.raw.empty() );
1426 BOOST_CHECK_EQUAL( field.source.absoluteOffset, titleOffsets[i] );
1427 BOOST_CHECK_EQUAL( field.source.length, titleLengths[i] );
1429 BOOST_CHECK_EQUAL( field.source.version, 0x000D );
1430 BOOST_CHECK_EQUAL( field.name.source.absoluteOffset, titleOffsets[i] + 6 );
1431 BOOST_CHECK_EQUAL( field.name.source.length, expectedName.size() );
1433 BOOST_CHECK_EQUAL( field.name.source.version, 0x000D );
1434 BOOST_CHECK_EQUAL( field.value.source.absoluteOffset, titleOffsets[i] + titleLengths[i] - 1 );
1437 BOOST_CHECK_EQUAL( field.value.source.version, 0x000D );
1438 }
1439
1440 BOOST_CHECK_EQUAL( minimal.sheets[0].title.text, wxString() );
1441 PADS_SCH::PADS_SCH_PARSER minimalAscii;
1442 BOOST_REQUIRE( minimalAscii.Parse( KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/binary/minimal_v13.txt" ) );
1443 BOOST_REQUIRE_EQUAL( minimalAscii.GetParameters().fields.size(), titleNames.size() );
1444
1445 for( const MODEL_FIELD& field : minimal.sheets[0].titleBlockFields )
1446 {
1447 auto asciiField = minimalAscii.GetParameters().fields.find( field.name.text.ToStdString() );
1448 BOOST_REQUIRE( asciiField != minimalAscii.GetParameters().fields.end() );
1449 BOOST_CHECK_EQUAL( asciiField->second, field.value.text.ToStdString() );
1450 }
1451
1452 PADS_SCH_MODEL multisheet =
1453 parser.Parse( loadBinaryFixture( "multisheet_connectivity.sch" ), wxS( "multisheet_connectivity.sch" ) );
1454 BOOST_REQUIRE_EQUAL( multisheet.sheets.size(), 2 );
1455 BOOST_CHECK_EQUAL( multisheet.sheets[0].id.Value(), 1 );
1456 BOOST_CHECK_EQUAL( multisheet.sheets[1].id.Value(), 2 );
1457 BOOST_CHECK_EQUAL( multisheet.sheets[0].name.text, wxS( "[1]DUP/SAFE:*" ) );
1458 BOOST_CHECK_EQUAL( multisheet.sheets[1].name.text, wxS( "[2]DUP/SAFE:*" ) );
1459 BOOST_CHECK_LT( multisheet.sheets[0].source.absoluteOffset, multisheet.sheets[1].source.absoluteOffset );
1460 BOOST_CHECK( !multisheet.sheets[0].parent );
1461 BOOST_CHECK( !multisheet.sheets[1].parent );
1462
1463 for( const MODEL_SHEET& sheet : multisheet.sheets )
1464 {
1465 BOOST_REQUIRE_EQUAL( sheet.titleBlockFields.size(), 14 );
1466
1467 for( const MODEL_FIELD& field : sheet.titleBlockFields )
1468 {
1469 BOOST_CHECK_EQUAL( field.source.sheet, -1 );
1470 BOOST_CHECK_EQUAL( field.name.source.sheet, -1 );
1471 BOOST_CHECK_EQUAL( field.value.source.sheet, -1 );
1473 }
1474
1475 BOOST_CHECK_EQUAL( sheet.title.source.sheet, -1 );
1476 auto title = std::ranges::find_if( sheet.titleBlockFields,
1477 []( const MODEL_FIELD& aField )
1478 {
1479 return aField.name.text == wxS( "Title" );
1480 } );
1481 BOOST_REQUIRE( title != sheet.titleBlockFields.end() );
1482 BOOST_CHECK( sheet.title == title->value );
1483 }
1484}
1485
1486
1487BOOST_AUTO_TEST_CASE( VariableTitleFields )
1488{
1489 std::vector<uint8_t> bytes = loadBinaryFixture( "minimal_v13.sch" );
1490 const size_t poolOffset = outerControllerOffset( bytes, 1 );
1491 const size_t poolBytes = readU32( bytes, 0x20 + 1 * 28 + 12 );
1492 const std::array<std::pair<std::string, std::string>, 5> expected = {
1493 std::pair{ "Drawn By", "Alice" }, std::pair{ "Checked By", "Bob" }, std::pair{ "Title", "Variable Title" },
1494 std::pair{ "Revision", "C" }, std::pair{ "Scale", "2:1" }
1495 };
1496 std::vector<uint8_t> pool;
1497 std::array<size_t, expected.size()> offsets;
1498 std::array<size_t, expected.size()> lengths;
1499
1500 for( size_t i = 0; i < expected.size(); ++i )
1501 {
1502 offsets[i] = poolOffset + pool.size();
1503 const std::string slot = "Field\n" + expected[i].first + '\x01' + expected[i].second + '\0';
1504 lengths[i] = slot.size();
1505 pool.insert( pool.end(), slot.begin(), slot.end() );
1506 }
1507
1508 const size_t firstNonFieldOffset = poolOffset + pool.size();
1509 const std::string firstNonField( "Font Default\0", 13 );
1510 pool.insert( pool.end(), firstNonField.begin(), firstNonField.end() );
1511 BOOST_REQUIRE_LE( pool.size(), poolBytes );
1512 pool.resize( poolBytes, 0 );
1513 std::copy( pool.begin(), pool.end(), bytes.begin() + poolOffset );
1514
1516 PADS_SCH_MODEL model = parser.Parse( bytes, wxS( "variable-title.sch" ) );
1517 BOOST_REQUIRE_EQUAL( model.sheets.size(), 1 );
1518 BOOST_REQUIRE_EQUAL( model.sheets[0].titleBlockFields.size(), expected.size() );
1519
1520 for( size_t i = 0; i < expected.size(); ++i )
1521 {
1522 const MODEL_FIELD& field = model.sheets[0].titleBlockFields[i];
1523 BOOST_CHECK_EQUAL( field.name.text, wxString::FromUTF8( expected[i].first ) );
1524 BOOST_CHECK_EQUAL( field.value.text, wxString::FromUTF8( expected[i].second ) );
1525 BOOST_CHECK_EQUAL_COLLECTIONS( field.name.raw.begin(), field.name.raw.end(), expected[i].first.begin(),
1526 expected[i].first.end() );
1527 BOOST_CHECK_EQUAL_COLLECTIONS( field.value.raw.begin(), field.value.raw.end(), expected[i].second.begin(),
1528 expected[i].second.end() );
1529 BOOST_CHECK_EQUAL( field.source.absoluteOffset, offsets[i] );
1530 BOOST_CHECK_EQUAL( field.source.length, lengths[i] );
1533 BOOST_CHECK_EQUAL( field.source.version, 0x000D );
1534 BOOST_CHECK_EQUAL( field.source.sheet, -1 );
1535 BOOST_CHECK_EQUAL( field.source.objectClass, wxS( "title field" ) );
1536 BOOST_CHECK_EQUAL( field.name.source.absoluteOffset, offsets[i] + 6 );
1537 BOOST_CHECK_EQUAL( field.name.source.length, expected[i].first.size() );
1539 BOOST_CHECK_EQUAL( field.name.source.version, 0x000D );
1540 BOOST_CHECK_EQUAL( field.name.source.sheet, -1 );
1541 BOOST_CHECK_EQUAL( field.value.source.absoluteOffset, offsets[i] + 6 + expected[i].first.size() + 1 );
1542 BOOST_CHECK_EQUAL( field.value.source.length, expected[i].second.size() );
1544 BOOST_CHECK_EQUAL( field.value.source.version, 0x000D );
1545 BOOST_CHECK_EQUAL( field.value.source.sheet, -1 );
1546 }
1547
1548 BOOST_CHECK_EQUAL( model.sheets[0].title.text, wxS( "Variable Title" ) );
1549 BOOST_CHECK_EQUAL_COLLECTIONS( bytes.begin() + firstNonFieldOffset,
1550 bytes.begin() + firstNonFieldOffset + firstNonField.size(), firstNonField.begin(),
1551 firstNonField.end() );
1552 BOOST_CHECK_EQUAL( model.sheets[0].titleBlockFields.back().source.absoluteOffset
1553 + model.sheets[0].titleBlockFields.back().source.length,
1554 firstNonFieldOffset );
1555}
1556
1557
1559{
1561 PADS_SCH_MODEL model = parser.Parse( loadBinaryFixture( "text_encoding.sch" ), wxS( "text_encoding.sch" ) );
1562
1563 BOOST_REQUIRE_EQUAL( model.texts.size(), 1 );
1564 const MODEL_TEXT& text = model.texts.front();
1565 BOOST_CHECK_EQUAL( text.sheet.id.Value(), 1 );
1566 BOOST_CHECK_EQUAL( text.text.text, wxS( "ascii-text cafe=" ) );
1567 BOOST_CHECK_EQUAL( text.text.raw.size(), 16 );
1569 BOOST_CHECK_EQUAL( text.text.codePage, 1252 );
1570 BOOST_CHECK_EQUAL( text.position.x, 20200 );
1571 BOOST_CHECK_EQUAL( text.position.y, 14000 );
1572 BOOST_CHECK_EQUAL( text.angle, 0 );
1573 BOOST_CHECK_EQUAL( text.presentation.height, 97 );
1574 BOOST_CHECK_EQUAL( text.presentation.width, 10 );
1575 BOOST_CHECK( text.presentation.horizontalJustification == MODEL_JUSTIFICATION::LEFT );
1576 BOOST_CHECK( text.presentation.verticalJustification == MODEL_JUSTIFICATION::RIGHT );
1577 BOOST_CHECK( !text.presentation.bold );
1578 BOOST_CHECK( !text.presentation.italic );
1579 BOOST_CHECK( !text.presentation.underline );
1580 BOOST_CHECK( text.presentation.visible );
1581 BOOST_REQUIRE_EQUAL( text.properties.size(), 1 );
1582 BOOST_CHECK_EQUAL( text.properties[0].name.text, wxS( "controller_1_relationship_word_28" ) );
1583 BOOST_CHECK_EQUAL( text.properties[0].value.text, wxS( "16" ) );
1584 BOOST_CHECK_EQUAL( text.properties[0].value.raw.size(), 2 );
1585 BOOST_CHECK_EQUAL( text.properties[0].source.absoluteOffset, text.source.absoluteOffset + 28 );
1586 BOOST_CHECK_EQUAL( text.properties[0].source.length, 2 );
1587 BOOST_CHECK( text.properties[0].disposition == PROPERTY_DISPOSITION::PRESERVED );
1588 BOOST_CHECK_EQUAL( text.source.objectClass, wxS( "free text" ) );
1589 BOOST_CHECK_EQUAL( text.source.controller, 1 );
1590 BOOST_CHECK_EQUAL( text.text.source.controller, 2 );
1591 BOOST_CHECK_EQUAL( text.source.sheet, 0 );
1592
1593 std::vector<uint8_t> cp1252Bytes = loadBinaryFixture( "text_encoding.sch" );
1594 cp1252Bytes[text.text.source.absoluteOffset + text.text.source.length - 1] = 0xE9;
1595 PADS_SCH_MODEL cp1252 = parser.Parse( cp1252Bytes, wxS( "text_encoding.sch" ) );
1596 BOOST_REQUIRE_EQUAL( cp1252.texts.size(), 1 );
1597 BOOST_CHECK_EQUAL( cp1252.texts[0].text.raw.back(), 0xE9 );
1598 BOOST_CHECK_EQUAL( cp1252.texts[0].text.text.Last().GetValue(), 0x00E9 );
1599 BOOST_CHECK( cp1252.texts[0].text.encoding == STRING_ENCODING_STATUS::CODE_PAGE );
1600 BOOST_CHECK_EQUAL( cp1252.texts[0].text.codePage, 1252 );
1601 BOOST_CHECK_EQUAL( cp1252.texts[0].text.source.absoluteOffset, text.text.source.absoluteOffset );
1602 BOOST_CHECK_EQUAL( cp1252.texts[0].text.source.length, text.text.source.length );
1603 BOOST_CHECK_EQUAL( cp1252.texts[0].text.source.controller, 2 );
1604 BOOST_CHECK_EQUAL( cp1252.texts[0].text.source.version, 0x000D );
1605
1606 std::vector<uint8_t> invalidOffset = loadBinaryFixture( "text_encoding.sch" );
1607 writeU32( invalidOffset, text.source.absoluteOffset + 8, std::numeric_limits<uint32_t>::max() );
1608 BOOST_CHECK_EXCEPTION( parser.Parse( invalidOffset, wxS( "invalid-offset.sch" ) ), IO_ERROR,
1609 [&]( const IO_ERROR& aError )
1610 {
1611 return aError.What().Contains( wxString::Format( wxS( "offset 0x%zX" ),
1612 text.source.absoluteOffset + 8 ) )
1613 && aError.What().Contains( wxS( "string offset leaves controller 2" ) );
1614 } );
1615
1616 std::vector<uint8_t> invalidLength = loadBinaryFixture( "text_encoding.sch" );
1617 writeU16( invalidLength, text.source.absoluteOffset + 20, std::numeric_limits<uint16_t>::max() );
1618 BOOST_CHECK_EXCEPTION( parser.Parse( invalidLength, wxS( "invalid-length.sch" ) ), IO_ERROR,
1619 [&]( const IO_ERROR& aError )
1620 {
1621 return aError.What().Contains( wxString::Format( wxS( "offset 0x%zX" ),
1622 text.source.absoluteOffset + 20 ) )
1623 && aError.What().Contains( wxS( "string length leaves controller 2" ) );
1624 } );
1625
1626 std::vector<uint8_t> missingNul = loadBinaryFixture( "text_encoding.sch" );
1627 const size_t terminatorOffset = text.text.source.absoluteOffset + text.text.source.length;
1628 missingNul[terminatorOffset] = 'X';
1629 BOOST_CHECK_EXCEPTION( parser.Parse( missingNul, wxS( "missing-nul.sch" ) ), IO_ERROR,
1630 [&]( const IO_ERROR& aError )
1631 {
1632 return aError.What().Contains(
1633 wxString::Format( wxS( "offset 0x%zX" ), terminatorOffset ) )
1634 && aError.What().Contains( wxS( "not NUL terminated" ) )
1635 && aError.What().Contains( wxS( "controller 2" ) );
1636 } );
1637
1639 BOOST_REQUIRE( ascii.Parse( KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/binary/text_encoding.txt" ) );
1640 std::vector<CANONICAL_SEMANTIC_RECORD> expected = normalizeAsciiModel( ascii );
1641 std::vector<CANONICAL_SEMANTIC_RECORD> actual = normalizeBinaryModel( model );
1642 auto retainTask7Properties = []( std::vector<CANONICAL_SEMANTIC_RECORD>& aRecords )
1643 {
1644 std::erase_if( aRecords,
1645 []( const CANONICAL_SEMANTIC_RECORD& aRecord )
1646 {
1647 return aRecord.kind != CANONICAL_KIND::SETTINGS && aRecord.kind != CANONICAL_KIND::SHEET
1648 && ( aRecord.kind != CANONICAL_KIND::TEXT
1649 || aRecord.stableKey != "ascii-text cafe=" );
1650 } );
1651
1652 for( CANONICAL_SEMANTIC_RECORD& record : aRecords )
1653 {
1654 if( record.kind != CANONICAL_KIND::TEXT )
1655 continue;
1656
1657 std::erase_if( record.properties,
1658 []( const auto& aProperty )
1659 {
1660 return aProperty.first != "owner_sheet" && aProperty.first != "visible"
1661 && aProperty.first != "bold" && aProperty.first != "italic"
1662 && aProperty.first != "underline";
1663 } );
1664 }
1665 };
1666 retainTask7Properties( expected );
1667 retainTask7Properties( actual );
1668 BOOST_CHECK( snapshotsMatch( expected, actual ) );
1669
1670 std::vector<uint8_t> changedRelationship = loadBinaryFixture( "text_encoding.sch" );
1671 writeU16( changedRelationship, text.source.absoluteOffset + 28, 0xBEEF );
1672 PADS_SCH_MODEL changed = parser.Parse( changedRelationship, wxS( "text_encoding.sch" ) );
1673 BOOST_REQUIRE_EQUAL( changed.texts.size(), 1 );
1674 BOOST_CHECK_EQUAL( changed.texts[0].properties[0].value.text, wxS( "48879" ) );
1675 BOOST_CHECK( changed.texts[0].properties[0].disposition == PROPERTY_DISPOSITION::PRESERVED );
1676 BOOST_CHECK_EQUAL( changed.texts[0].presentation.bold, text.presentation.bold );
1677 BOOST_CHECK_EQUAL( changed.texts[0].presentation.italic, text.presentation.italic );
1678 BOOST_CHECK_EQUAL( changed.texts[0].presentation.underline, text.presentation.underline );
1679 BOOST_CHECK_EQUAL( changed.texts[0].presentation.visible, text.presentation.visible );
1680 BOOST_CHECK_EQUAL( changed.diagnostics.size(), model.diagnostics.size() );
1681
1682 struct JUSTIFICATION_CASE
1683 {
1684 uint16_t raw;
1685 MODEL_JUSTIFICATION horizontal;
1686 MODEL_JUSTIFICATION vertical;
1687 };
1688
1689 for( const JUSTIFICATION_CASE& expected :
1690 { JUSTIFICATION_CASE{ 5, MODEL_JUSTIFICATION::CENTER, MODEL_JUSTIFICATION::RIGHT },
1697 {
1698 std::vector<uint8_t> changedJustification = loadBinaryFixture( "text_encoding.sch" );
1699 writeU16( changedJustification, text.source.absoluteOffset + 18, expected.raw );
1700 PADS_SCH_MODEL justified = parser.Parse( changedJustification, wxS( "text_encoding.sch" ) );
1701 BOOST_REQUIRE_EQUAL( justified.texts.size(), 1 );
1702 BOOST_CHECK( justified.texts[0].presentation.horizontalJustification == expected.horizontal );
1703 BOOST_CHECK( justified.texts[0].presentation.verticalJustification == expected.vertical );
1704 BOOST_CHECK_EQUAL( justified.diagnostics.size(), model.diagnostics.size() );
1705 }
1706
1707 struct FONT_CASE
1708 {
1709 uint32_t style;
1710 bool bold;
1711 bool italic;
1712 };
1713
1714 for( const FONT_CASE& expected : { FONT_CASE{ 0, false, false }, FONT_CASE{ 1, false, true },
1715 FONT_CASE{ 2, true, false }, FONT_CASE{ 3, true, true } } )
1716 {
1717 std::vector<uint8_t> changedFont = loadBinaryFixture( "text_encoding.sch" );
1718 writeU16( changedFont, text.source.absoluteOffset, 0 );
1719 writeU32( changedFont, outerControllerOffset( changedFont, 19 ), expected.style );
1720 PADS_SCH_MODEL styled = parser.Parse( changedFont, wxS( "text_encoding.sch" ) );
1721 BOOST_REQUIRE_EQUAL( styled.texts.size(), 1 );
1722 BOOST_CHECK_EQUAL( styled.texts[0].presentation.bold, expected.bold );
1723 BOOST_CHECK_EQUAL( styled.texts[0].presentation.italic, expected.italic );
1724 BOOST_CHECK_EQUAL( styled.texts[0].presentation.width, 10 );
1725 }
1726
1727 std::vector<uint8_t> hiddenText = loadBinaryFixture( "text_encoding.sch" );
1728 hiddenText[text.source.absoluteOffset + 31] = 1;
1729 PADS_SCH_MODEL hidden = parser.Parse( hiddenText, wxS( "text_encoding.sch" ) );
1730 BOOST_REQUIRE_EQUAL( hidden.texts.size(), 1 );
1731 BOOST_CHECK( !hidden.texts[0].presentation.visible );
1732 BOOST_CHECK_EQUAL( hidden.texts[0].presentation.width, 10 );
1733}
1734
1735
1736BOOST_AUTO_TEST_CASE( TextOptionMatrix )
1737{
1739 PADS_SCH_MODEL model = parser.Parse( loadBinaryFixture( "text_options.sch" ), wxS( "text_options.sch" ) );
1740
1741 BOOST_REQUIRE_EQUAL( model.texts.size(), 26 );
1742
1743 auto textByName = [&]( const wxString& aName ) -> const MODEL_TEXT&
1744 {
1745 auto found = std::ranges::find_if( model.texts,
1746 [&]( const MODEL_TEXT& aText )
1747 {
1748 return aText.text.text == aName;
1749 } );
1750 BOOST_REQUIRE( found != model.texts.end() );
1751 return *found;
1752 };
1753
1754 // Measured from the PADS render of this fixture; vertical LEFT is top and RIGHT is bottom
1758 const std::array<MODEL_JUSTIFICATION, 16> horizontal = { L, R, L, R, C, C, C, C, L, R, L, R, C, C, C, C };
1759 const std::array<MODEL_JUSTIFICATION, 16> vertical = { R, R, L, L, R, R, L, L, C, C, C, C, C, C, C, C };
1760
1761 for( size_t ii = 0; ii < horizontal.size(); ++ii )
1762 {
1763 const MODEL_TEXT& text = textByName( wxString::Format( wxS( "JUST_%02zu" ), ii ) );
1764 BOOST_CHECK( text.presentation.horizontalJustification == horizontal[ii] );
1765 BOOST_CHECK( text.presentation.verticalJustification == vertical[ii] );
1766 }
1767
1768 for( const auto& [name, angle] : { std::pair{ wxS( "ANGLE_000" ), 0 }, std::pair{ wxS( "ANGLE_090" ), 900 },
1769 std::pair{ wxS( "ANGLE_180" ), 1800 }, std::pair{ wxS( "ANGLE_270" ), 2700 } } )
1770 {
1771 BOOST_CHECK_EQUAL( textByName( name ).angle, angle );
1772 }
1773
1774 BOOST_CHECK( !textByName( wxS( "STYLE_REGULAR" ) ).presentation.bold );
1775 BOOST_CHECK( !textByName( wxS( "STYLE_REGULAR" ) ).presentation.italic );
1776 BOOST_CHECK( textByName( wxS( "STYLE_BOLD" ) ).presentation.bold );
1777 BOOST_CHECK( textByName( wxS( "STYLE_ITALIC" ) ).presentation.italic );
1778 BOOST_CHECK( textByName( wxS( "STYLE_BOLD_ITALIC" ) ).presentation.bold );
1779 BOOST_CHECK( !textByName( wxS( "STYLE_BOLD_ITALIC" ) ).presentation.italic );
1780
1781 BOOST_CHECK( textByName( wxS( "VISIBLE_0" ) ).presentation.visible );
1782 BOOST_CHECK( !textByName( wxS( "VISIBLE_1" ) ).presentation.visible );
1783 BOOST_CHECK_EQUAL( textByName( wxS( "VISIBLE_0" ) ).presentation.width, 10 );
1784 BOOST_CHECK_EQUAL( textByName( wxS( "VISIBLE_1" ) ).presentation.width, 10 );
1785}
1786
1787
1788BOOST_AUTO_TEST_CASE( VisibilityAndUnderlineOptionMatrix )
1789{
1790 PADS_SCH_MODEL model = PADS_SCH_BINARY_PARSER().Parse( loadBinaryFixture( "visibility_options.sch" ),
1791 wxS( "visibility_options.sch" ) );
1792
1793 auto underlined = std::ranges::find_if( model.texts,
1794 []( const MODEL_TEXT& aText )
1795 {
1796 return aText.text.text == wxS( "STYLE_UNDERLINE" );
1797 } );
1798 BOOST_REQUIRE( underlined != model.texts.end() );
1799 BOOST_CHECK( underlined->presentation.underline );
1800 BOOST_CHECK( !underlined->presentation.bold );
1801 BOOST_CHECK( !underlined->presentation.italic );
1802 BOOST_CHECK_EQUAL( underlined->presentation.font.text, wxS( "Verdana" ) );
1803
1804 BOOST_REQUIRE_EQUAL( model.placements.size(), 32 );
1805
1806 for( uint8_t flags = 0; flags < 32; ++flags )
1807 {
1808 const wxString reference = wxString::Format( wxS( "R%u" ), flags + 1 );
1809 auto placement = std::ranges::find( model.placements, reference,
1810 []( const MODEL_PLACEMENT& aPlacement )
1811 {
1812 return aPlacement.reference.text;
1813 } );
1814 BOOST_REQUIRE_MESSAGE( placement != model.placements.end(), reference );
1815 BOOST_CHECK_EQUAL( placement->itemVisibilityFlags, flags );
1816 BOOST_CHECK_EQUAL( placement->referenceVisible, ( flags & 0x01 ) == 0 );
1817 BOOST_CHECK_EQUAL( placement->partTypeVisible, ( flags & 0x02 ) == 0 );
1818 BOOST_CHECK_EQUAL( placement->pinNamesVisible, ( flags & 0x08 ) == 0 );
1819 BOOST_CHECK_EQUAL( placement->pinNumbersVisible, ( flags & 0x10 ) == 0 );
1820 }
1821}
1822
1823
1824BOOST_AUTO_TEST_CASE( TextEncodingWarnings )
1825{
1826 SOURCE_PROVENANCE source{ wxS( "text_encoding.sch" ), 0x000D, wxS( "free text" ), 2, 0, 0x6250, 2, 0 };
1827 std::vector<PARSER_DIAGNOSTIC> diagnostics;
1828 SOURCE_STRING unknown = PADS_SCH_BINARY_PARSER::DecodeString( { 0x41, 0xE9 }, 932, source, diagnostics );
1829 SOURCE_STRING invalid = PADS_SCH_BINARY_PARSER::DecodeString( { 0xC3, 0x28 }, 65001, source, diagnostics );
1830
1832 BOOST_CHECK_EQUAL( unknown.raw[1], 0xE9 );
1833 BOOST_CHECK_EQUAL( unknown.text[1].GetValue(), 0xFFFD );
1835 BOOST_REQUIRE_EQUAL( diagnostics.size(), 2 );
1836 BOOST_CHECK_EQUAL( diagnostics[0].source.absoluteOffset, 0x6250 );
1837 BOOST_CHECK( diagnostics[0].message.Contains( wxS( "code page 932" ) ) );
1838 BOOST_CHECK( diagnostics[1].message.Contains( wxS( "invalid UTF-8" ) ) );
1839}
1840
1841
1842BOOST_AUTO_TEST_CASE( GlobalRecordCorruption )
1843{
1845 std::vector<uint8_t> reordered = loadBinaryFixture( "multisheet_connectivity.sch" );
1846 size_t sheetIndex = outerControllerOffset( reordered, 3 );
1847 writeU16( reordered, sheetIndex + 8, 2 );
1848 writeU16( reordered, sheetIndex + 48 + 8, 1 );
1849 PADS_SCH_MODEL reorderedModel = parser.Parse( reordered, wxS( "reordered.sch" ) );
1850 BOOST_REQUIRE_EQUAL( reorderedModel.sheets.size(), 2u );
1851 BOOST_CHECK_EQUAL( reorderedModel.sheets[0].id.Value(), 2u );
1852 BOOST_CHECK_EQUAL( reorderedModel.sheets[0].index, 1u );
1853 BOOST_CHECK_EQUAL( reorderedModel.sheets[0].source.recordIndex, 0u );
1854 BOOST_CHECK_EQUAL( reorderedModel.sheets[1].id.Value(), 1u );
1855 BOOST_CHECK_EQUAL( reorderedModel.sheets[1].index, 0u );
1856 BOOST_CHECK_EQUAL( reorderedModel.sheets[1].source.recordIndex, 1u );
1857
1858 std::vector<uint8_t> duplicate = loadBinaryFixture( "multisheet_connectivity.sch" );
1859 sheetIndex = outerControllerOffset( duplicate, 3 );
1860 writeU16( duplicate, sheetIndex + 48 + 8, 1 );
1861 BOOST_CHECK_EXCEPTION( parser.Parse( duplicate, wxS( "duplicate.sch" ) ), IO_ERROR,
1862 []( const IO_ERROR& aError )
1863 {
1864 return aError.What().Contains( wxS( "duplicate sheet ID 1" ) )
1865 && aError.What().Contains( wxS( "controller 3" ) );
1866 } );
1867
1868 std::vector<uint8_t> wrongClassBytes = loadMinimalV13();
1869 sheetIndex = outerControllerOffset( wrongClassBytes, 3 );
1870 writeU32( wrongClassBytes, sheetIndex, static_cast<uint32_t>( outerControllerOffset( wrongClassBytes, 5 ) ) );
1871 BOOST_CHECK_EXCEPTION( parser.Parse( wrongClassBytes, wxS( "wrong-class.sch" ) ), IO_ERROR,
1872 []( const IO_ERROR& aError )
1873 {
1874 return aError.What().Contains( wxS( "references the wrong SDB object class" ) )
1875 && aError.What().Contains( wxS( "controller 3" ) );
1876 } );
1877
1878 PADS_SCH_MODEL wrongClass = parser.Parse( loadMinimalV13(), wxS( "wrong-class.sch" ) );
1879 SOURCE_PROVENANCE refSource{ wxS( "wrong-class.sch" ), 0x000D, wxS( "free text" ), 1, 0, 0x100, 32, 0 };
1880 wrongClass.texts.push_back( { refSource, { SHEET_ID( 99 ), refSource } } );
1881 BOOST_CHECK_EXCEPTION( wrongClass.ValidateOrThrow(), IO_ERROR,
1882 []( const IO_ERROR& aError )
1883 {
1884 return aError.What().Contains( wxS( "unresolved text sheet reference" ) );
1885 } );
1886
1887 PADS_SCH_MODEL cycle = parser.Parse( loadMinimalV13(), wxS( "cycle.sch" ) );
1888 cycle.sheets[0].parent = SHEET_REFERENCE{ cycle.sheets[0].id, cycle.sheets[0].source };
1889 BOOST_CHECK_EXCEPTION( cycle.ValidateOrThrow(), IO_ERROR,
1890 []( const IO_ERROR& aError )
1891 {
1892 return aError.What().Contains( wxS( "cyclic sheet hierarchy" ) );
1893 } );
1894
1895 PADS_SCH_MODEL longHierarchy;
1896 longHierarchy.version = 0x000D;
1897 constexpr size_t hierarchySize = 4096;
1898 longHierarchy.sheets.reserve( hierarchySize );
1899
1900 for( size_t i = 0; i < hierarchySize; ++i )
1901 {
1902 SOURCE_PROVENANCE source{ wxS( "long-hierarchy.sch" ), 0x000D, wxS( "sheet" ), 3, i, 0x100 + i * 48, 48,
1903 static_cast<int>( i ) };
1904 MODEL_SHEET sheet;
1905 sheet.id = SHEET_ID( i + 1 );
1906 sheet.index = i;
1907 sheet.source = source;
1908
1909 if( i > 0 )
1910 sheet.parent = SHEET_REFERENCE{ SHEET_ID( i ), source };
1911
1912 longHierarchy.sheets.push_back( std::move( sheet ) );
1913 }
1914
1915 BOOST_CHECK_NO_THROW( longHierarchy.ValidateOrThrow() );
1916 longHierarchy.sheets[0].parent = SHEET_REFERENCE{ longHierarchy.sheets.back().id, longHierarchy.sheets[0].source };
1917 BOOST_CHECK_EXCEPTION( longHierarchy.ValidateOrThrow(), IO_ERROR,
1918 []( const IO_ERROR& aError )
1919 {
1920 return aError.What().Contains( wxS( "cyclic sheet hierarchy" ) )
1921 && aError.What().Contains( wxS( "controller 3" ) );
1922 } );
1923}
1924
1925
1926BOOST_AUTO_TEST_CASE( ModelContract )
1927{
1928 static_assert( !std::is_same_v<SHEET_ID, PLACEMENT_ID> );
1929 static_assert( !std::is_convertible_v<SHEET_ID, PLACEMENT_ID> );
1930
1931 const std::vector<uint8_t> bytes = loadMinimalV13();
1933 PADS_SCH_MODEL first = parser.Parse( bytes, wxS( "minimal_v13.sch" ) );
1934 PADS_SCH_MODEL second = parser.Parse( bytes, wxS( "minimal_v13.sch" ) );
1935
1936 BOOST_CHECK_EQUAL( first.version, 0x000D );
1937 BOOST_REQUIRE_EQUAL( first.sheets.size(), 1 );
1938 BOOST_CHECK_EQUAL( first.sheets[0].index, 0 );
1939 BOOST_CHECK_EQUAL( first.sheets[0].id.Value(), 1 );
1940 BOOST_CHECK_EQUAL( first.sheets[0].source.file, wxS( "minimal_v13.sch" ) );
1941 BOOST_CHECK_GT( first.sheets[0].source.length, 0 );
1942 BOOST_CHECK_EQUAL( first.sheets[0].source.version, 0x000D );
1943 BOOST_CHECK_EQUAL( first.sheets[0].source.objectClass, wxS( "sheet" ) );
1944 BOOST_CHECK_EQUAL( first.sheets[0].source.recordIndex, 0 );
1945 BOOST_CHECK_EQUAL( first.sheets[0].source.absoluteOffset, second.sheets[0].source.absoluteOffset );
1946 BOOST_CHECK_EQUAL( first.sheets[0].source.length, second.sheets[0].source.length );
1947 BOOST_CHECK_EQUAL( first.sheets[0].id.Value(), second.sheets[0].id.Value() );
1948 BOOST_CHECK_EQUAL( canonicalModel( first ), canonicalModel( second ) );
1949 BOOST_CHECK( first == second );
1950
1951 BOOST_CHECK_EQUAL( NormalizeAngle( -900 ), 2700 );
1952 BOOST_CHECK_EQUAL( NormalizeAngle( 4500 ), 900 );
1953 BOOST_CHECK_EQUAL( NormalizeCoordinate( -1234 ), -1234 );
1954
1955 SOURCE_STRING sourceString{
1956 { 0x41, 0xC3, 0xA9 }, wxString::FromUTF8( "A\xC3\xA9" ), STRING_ENCODING_STATUS::UTF8, first.sheets[0].source
1957 };
1958 BOOST_CHECK_EQUAL( sourceString.raw.size(), 3 );
1959 BOOST_CHECK_EQUAL( sourceString.text, wxString::FromUTF8( "A\xC3\xA9" ) );
1960 BOOST_CHECK( sourceString.encoding == STRING_ENCODING_STATUS::UTF8 );
1961
1962 first.diagnostics.push_back(
1963 { RPT_SEVERITY_WARNING, first.sheets[0].source, wxS( "unknown but structurally valid enum" ) } );
1964 BOOST_CHECK_EQUAL( first.diagnostics.back().source.absoluteOffset, first.sheets[0].source.absoluteOffset );
1965
1966 PADS_SCH_MODEL unresolved = second;
1967 unresolved.sheets[0].parent = SHEET_REFERENCE{ SHEET_ID( 99 ), unresolved.sheets[0].source };
1968 BOOST_CHECK_EXCEPTION( unresolved.ValidateOrThrow(), IO_ERROR,
1969 []( const IO_ERROR& aError )
1970 {
1971 return aError.What().Contains( wxS( "unresolved" ) );
1972 } );
1973
1974 PADS_SCH_MODEL cyclic = second;
1975 cyclic.sheets[0].parent = SHEET_REFERENCE{ cyclic.sheets[0].id, cyclic.sheets[0].source };
1976 BOOST_CHECK_EXCEPTION( cyclic.ValidateOrThrow(), IO_ERROR,
1977 []( const IO_ERROR& aError )
1978 {
1979 return aError.What().Contains( wxS( "cyclic" ) )
1980 && aError.What().Contains( wxS( "offset" ) );
1981 } );
1982
1983 SOURCE_PROVENANCE textSource{ wxS( "strings.sch" ), 0x000D, wxS( "text" ), 4, 7, 0x1234, 2, 0 };
1984 std::vector<PARSER_DIAGNOSTIC> diagnostics;
1985 SOURCE_STRING unknown = PADS_SCH_BINARY_PARSER::DecodeString( { 0x41 }, 99999, textSource, diagnostics );
1986 SOURCE_STRING invalid = PADS_SCH_BINARY_PARSER::DecodeString( { 0xC3, 0x28 }, 65001, textSource, diagnostics );
1987 PADS_SCH_BINARY_PARSER::RecordUnknownEnum( wxS( "line style" ), 17, textSource, diagnostics );
1990 BOOST_REQUIRE_EQUAL( diagnostics.size(), 3 );
1991 BOOST_CHECK_EQUAL( diagnostics[0].source.absoluteOffset, 0x1234 );
1992 BOOST_CHECK_EQUAL( diagnostics[1].source.recordIndex, 7 );
1993 BOOST_CHECK( diagnostics[2].message.Contains( wxS( "line style 17" ) ) );
1994}
1995
1996
1997BOOST_AUTO_TEST_CASE( ConnectionEndpointValidation )
1998{
1999 SOURCE_PROVENANCE source{ wxS( "model.sch" ), 0x000D, wxS( "connection" ), 8, 3, 0x200, 16, 0 };
2001 model.version = 0x000D;
2002 model.source = source;
2003 model.sheets.push_back( { SHEET_ID( 0 ), 0, source } );
2004 model.definitions.push_back( { DEFINITION_ID( 1 ), source } );
2005 model.definitions[0].pins.push_back( { PIN_ID( 2 ), source } );
2006 model.partTypes.push_back( { PART_TYPE_ID( 3 ), source } );
2007 model.partTypes[0].gates.push_back(
2008 { GATE_ID( 4 ), source, { DEFINITION_ID( 1 ), source }, 1, { { PIN_ID( 2 ), source } } } );
2009 model.placements.push_back( { PLACEMENT_ID( 5 ),
2010 source,
2011 { SHEET_ID( 0 ), source },
2012 { PART_TYPE_ID( 3 ), source },
2013 GATE_REFERENCE{ GATE_ID( 4 ), source },
2014 { DEFINITION_ID( 1 ), source },
2015 { { PIN_ID( 2 ), source } } } );
2016 model.nets.push_back( { NET_ID( 6 ), source, { SHEET_ID( 0 ), source } } );
2017 model.nets[0].connections.push_back( { source } );
2018 model.nets[0].connections[0].endpoints.push_back( { MODEL_ENDPOINT_KIND::PIN, source,
2020 PIN_REFERENCE{ PIN_ID( 2 ), source } } );
2021 BOOST_CHECK_NO_THROW( model.ValidateOrThrow() );
2022
2023 PADS_SCH_MODEL wrongPin = model;
2024 wrongPin.nets[0].connections[0].endpoints[0].pin = PIN_REFERENCE{ PIN_ID( 99 ), source };
2025 BOOST_CHECK_THROW( wrongPin.ValidateOrThrow(), IO_ERROR );
2026
2027 PADS_SCH_MODEL wrongUnit = model;
2028 wrongUnit.placements[0].unit = 2;
2029 BOOST_CHECK_THROW( wrongUnit.ValidateOrThrow(), IO_ERROR );
2030
2032 empty.nets[0].connections[0].endpoints[0] = { MODEL_ENDPOINT_KIND::INVALID, source };
2033 BOOST_CHECK_THROW( empty.ValidateOrThrow(), IO_ERROR );
2034
2035 PADS_SCH_MODEL mixed = model;
2036 mixed.nets[0].connections[0].endpoints[0].kind = MODEL_ENDPOINT_KIND::POINT;
2037 BOOST_CHECK_THROW( mixed.ValidateOrThrow(), IO_ERROR );
2038
2039 PADS_SCH_MODEL point = model;
2040 point.nets[0].connections[0].endpoints[0] = { MODEL_ENDPOINT_KIND::POINT, source };
2041 BOOST_CHECK_NO_THROW( point.ValidateOrThrow() );
2042
2043 SOURCE_PROVENANCE topologySource{ wxS( "model.sch" ), 0x000D, wxS( "endpoint" ), 9, 4, 0x280, 16, 1 };
2044 PADS_SCH_MODEL crossSheet = model;
2045 crossSheet.sheets.push_back( { SHEET_ID( 1 ), 1, topologySource } );
2046 crossSheet.nets[0].sheet = { SHEET_ID( 1 ), topologySource };
2047 crossSheet.nets[0].connections[0].endpoints[0].source = topologySource;
2048 const wxString endpointError =
2049 FormatParserError( topologySource, wxS( "connection endpoint placement sheet does not match net sheet" ) );
2050 BOOST_CHECK_EXCEPTION( crossSheet.ValidateOrThrow(), IO_ERROR,
2051 [&]( const IO_ERROR& aError )
2052 {
2053 return aError.What().Contains( endpointError );
2054 } );
2055
2056 PADS_SCH_MODEL busModel = model;
2057 busModel.nets[0].connections.clear();
2058 busModel.buses.push_back( { BUS_ID( 7 ), source, { SHEET_ID( 0 ), source } } );
2059 busModel.buses[0].memberNets.push_back( { NET_ID( 6 ), source } );
2060 busModel.buses[0].entries.push_back( { topologySource, {}, { NET_ID( 6 ), topologySource } } );
2061 BOOST_CHECK_NO_THROW( busModel.ValidateOrThrow() );
2062
2063 PADS_SCH_MODEL missingBusMember = busModel;
2064 missingBusMember.buses[0].memberNets.clear();
2065 const wxString membershipError =
2066 FormatParserError( topologySource, wxS( "bus-entry net is absent from bus member nets" ) );
2067 BOOST_CHECK_EXCEPTION( missingBusMember.ValidateOrThrow(), IO_ERROR,
2068 [&]( const IO_ERROR& aError )
2069 {
2070 return aError.What().Contains( membershipError );
2071 } );
2072
2073 PADS_SCH_MODEL crossSheetBus = busModel;
2074 crossSheetBus.sheets.push_back( { SHEET_ID( 1 ), 1, topologySource } );
2075 crossSheetBus.buses[0].sheet = { SHEET_ID( 1 ), topologySource };
2076 const wxString busSheetError = FormatParserError( source, wxS( "bus member-net sheet does not match bus sheet" ) );
2077 BOOST_CHECK_EXCEPTION( crossSheetBus.ValidateOrThrow(), IO_ERROR,
2078 [&]( const IO_ERROR& aError )
2079 {
2080 return aError.What().Contains( busSheetError );
2081 } );
2082}
2083
2084
2085BOOST_AUTO_TEST_CASE( TypedPageGraphicOwnership )
2086{
2087 SOURCE_PROVENANCE source{ wxS( "graphics.sch" ), 0x000D, wxS( "page graphic" ), 5, 6, 0x420, 24, 0 };
2089 model.source = source;
2090 model.sheets.push_back( { SHEET_ID( 4 ), 0, source } );
2091 model.graphics.push_back( MODEL_PAGE_GRAPHIC{ source, { SHEET_ID( 4 ), source }, { source } } );
2092 BOOST_CHECK_NO_THROW( model.ValidateOrThrow() );
2093
2094 model.graphics[0].sheet.id = SHEET_ID( 99 );
2095 auto records = normalizeBinaryModel( model );
2096 auto record = std::ranges::find_if( records,
2097 []( const CANONICAL_SEMANTIC_RECORD& aRecord )
2098 {
2099 return aRecord.kind == CANONICAL_KIND::GRAPHIC;
2100 } );
2101 BOOST_REQUIRE( record != records.end() );
2102 BOOST_CHECK( record->properties.at( "owner_sheet" ).disposition == PROPERTY_DISPOSITION::UNSUPPORTED );
2103 const wxString error = FormatParserError( source, wxS( "unresolved page-graphic sheet reference" ) );
2104 BOOST_CHECK_EXCEPTION( model.ValidateOrThrow(), IO_ERROR,
2105 [&]( const IO_ERROR& aError )
2106 {
2107 return aError.What().Contains( error );
2108 } );
2109}
2110
2111
2112BOOST_AUTO_TEST_CASE( TypedIdDiagnosticProvenance )
2113{
2114 SOURCE_PROVENANCE firstSource{ wxS( "ids.sch" ), 0x000D, wxS( "sheet" ), 3, 1, 0x100, 48, 0 };
2115 SOURCE_PROVENANCE secondSource{ wxS( "ids.sch" ), 0x000D, wxS( "sheet" ), 3, 2, 0x200, 48, 1 };
2117 duplicate.source = { wxS( "ids.sch" ), 0x000D, wxS( "model" ), -1, 0, 0, 0x300, -1 };
2118 duplicate.sheets.push_back( { SHEET_ID( 7 ), 0, firstSource } );
2119 duplicate.sheets.push_back( { SHEET_ID( 7 ), 1, secondSource } );
2120
2121 const wxString duplicateDetail =
2122 wxS( "duplicate sheet ID 7; first at v0x000D sheet controller 3 record 1 sheet 0 offset 0x100" );
2123 const wxString duplicateError = FormatParserError( secondSource, duplicateDetail );
2124 BOOST_CHECK_EXCEPTION( duplicate.ValidateOrThrow(), IO_ERROR,
2125 [&]( const IO_ERROR& aError )
2126 {
2127 return aError.What().Contains( duplicateError );
2128 } );
2129
2130 PADS_SCH_MODEL invalid;
2131 invalid.source = duplicate.source;
2132 invalid.sheets.push_back( { SHEET_ID(), 0, secondSource } );
2133 const wxString invalidError = FormatParserError( secondSource, wxS( "invalid sheet ID" ) );
2134 BOOST_CHECK_EXCEPTION( invalid.ValidateOrThrow(), IO_ERROR,
2135 [&]( const IO_ERROR& aError )
2136 {
2137 return aError.What().Contains( invalidError );
2138 } );
2139}
2140
2141
2142BOOST_AUTO_TEST_CASE( SourceStringEncoding )
2143{
2144 SOURCE_PROVENANCE source{ wxS( "encoding.sch" ), 0x000D, wxS( "text" ), 4, 8, 0x300, 4, 0 };
2145 std::vector<PARSER_DIAGNOSTIC> diagnostics;
2146
2147 SOURCE_STRING utf8 = PADS_SCH_BINARY_PARSER::DecodeString( { 0x41, 0xC3, 0xA9 }, 65001, source, diagnostics );
2148 BOOST_CHECK_EQUAL( utf8.text, wxString::FromUTF8( "A\xC3\xA9" ) );
2149 BOOST_CHECK_EQUAL( utf8.codePage, 65001 );
2150 BOOST_CHECK_EQUAL( utf8.codePageName, wxS( "UTF-8" ) );
2152 BOOST_CHECK( utf8.source == source );
2153
2154 SOURCE_STRING cp1252 =
2155 PADS_SCH_BINARY_PARSER::DecodeString( { 0x80, 0x93, 0xE9 }, 1252, source, diagnostics, wxS( "CP1252" ) );
2156 BOOST_REQUIRE_EQUAL( cp1252.text.length(), 3 );
2157 BOOST_CHECK_EQUAL( cp1252.text[0].GetValue(), 0x20AC );
2158 BOOST_CHECK_EQUAL( cp1252.text[1].GetValue(), 0x201C );
2159 BOOST_CHECK_EQUAL( cp1252.text[2].GetValue(), 0x00E9 );
2160 BOOST_CHECK_EQUAL( cp1252.codePage, 1252 );
2161 BOOST_CHECK_EQUAL( cp1252.codePageName, wxS( "CP1252" ) );
2163
2165 PADS_SCH_BINARY_PARSER::DecodeString( { 0x41, 0xFE }, 99999, source, diagnostics, wxS( "vendor-foo" ) );
2166 BOOST_CHECK_EQUAL( unknown.raw[1], 0xFE );
2167 BOOST_REQUIRE_EQUAL( unknown.text.length(), 2 );
2168 BOOST_CHECK_EQUAL( unknown.text[0].GetValue(), 0x41 );
2169 BOOST_CHECK_EQUAL( unknown.text[1].GetValue(), 0xFFFD );
2170 BOOST_CHECK_EQUAL( unknown.codePage, 99999 );
2171 BOOST_CHECK_EQUAL( unknown.codePageName, wxS( "vendor-foo" ) );
2173
2174 SOURCE_STRING invalidUtf8 =
2175 PADS_SCH_BINARY_PARSER::DecodeString( { 0x41, 0xC3, 0x28, 0xFF }, 65001, source, diagnostics );
2176 BOOST_REQUIRE_EQUAL( invalidUtf8.text.length(), 4 );
2177 BOOST_CHECK_EQUAL( invalidUtf8.text[0].GetValue(), 0x41 );
2178 BOOST_CHECK_EQUAL( invalidUtf8.text[1].GetValue(), 0xFFFD );
2179 BOOST_CHECK_EQUAL( invalidUtf8.text[2].GetValue(), 0x28 );
2180 BOOST_CHECK_EQUAL( invalidUtf8.text[3].GetValue(), 0xFFFD );
2182 BOOST_REQUIRE_EQUAL( diagnostics.size(), 2 );
2183 BOOST_CHECK_EQUAL( diagnostics[0].source.absoluteOffset, 0x300 );
2184 BOOST_CHECK( diagnostics[0].message.Contains( wxS( "99999" ) ) );
2185 BOOST_CHECK_EQUAL( diagnostics[1].source.recordIndex, 8 );
2186 BOOST_CHECK( diagnostics[1].message.Contains( wxS( "UTF-8" ) ) );
2187
2188 SOURCE_STRING emptyUnknown = PADS_SCH_BINARY_PARSER::DecodeString( {}, 932, source, diagnostics );
2190 BOOST_REQUIRE_EQUAL( diagnostics.size(), 3 );
2191 BOOST_CHECK( diagnostics[2].message.Contains( wxS( "932" ) ) );
2192}
2193
2194
2195BOOST_AUTO_TEST_CASE( SemanticSnapshotAdapterContract )
2196{
2197 SOURCE_PROVENANCE source{ wxS( "snapshot.sch" ), 0x000D, wxS( "sheet" ), 3, 0, 0x250, 48, 0 };
2198 PADS_SCH_MODEL binary;
2199 binary.sheets.push_back( { SHEET_ID( 1 ),
2200 0,
2201 source,
2202 { { 'M', 'a', 'i', 'n' }, wxS( "Main" ), STRING_ENCODING_STATUS::UTF8, source } } );
2203 binary.sheets.push_back( { SHEET_ID( 2 ),
2204 1,
2205 source,
2206 { { 'M', 'a', 'i', 'n' }, wxS( "Main" ), STRING_ENCODING_STATUS::UTF8, source } } );
2207 BOOST_CHECK_NE( binary.sheets[0].id.Value(), binary.sheets[1].id.Value() );
2208 BOOST_CHECK( normalizeBinaryModel( binary ) == normalizeBinaryModel( binary ) );
2209 binary.sheets[0].properties.push_back( { { {}, wxS( "value" ), STRING_ENCODING_STATUS::UTF8, source },
2210 { {}, wxS( "10k" ), STRING_ENCODING_STATUS::UTF8, source },
2212 source } );
2213 binary.sheets[0].properties.push_back( { { {}, wxS( "font" ), STRING_ENCODING_STATUS::UTF8, source },
2214 { {}, wxS( "PADS stroke" ), STRING_ENCODING_STATUS::UTF8, source },
2216 source } );
2217 binary.sheets[0].properties.push_back( { { {}, wxS( "color" ), STRING_ENCODING_STATUS::UTF8, source },
2218 { {}, wxS( "green" ), STRING_ENCODING_STATUS::UTF8, source },
2220 source } );
2221 binary.sheets[1].properties.push_back( { { {}, wxS( "value" ), STRING_ENCODING_STATUS::UTF8, source },
2222 { {}, wxS( "22k" ), STRING_ENCODING_STATUS::UTF8, source },
2224 source } );
2225
2226 std::vector<CANONICAL_SEMANTIC_RECORD> expected = normalizeBinaryModel( binary );
2227 std::vector<CANONICAL_SEMANTIC_RECORD> actual = expected;
2228 std::reverse( actual.begin(), actual.end() );
2229
2230 for( CANONICAL_SEMANTIC_RECORD& item : actual )
2231 {
2232 if( item.properties.contains( "font" ) )
2233 item.properties.at( "font" ).value = std::string( "Arial" );
2234
2235 if( item.properties.contains( "color" ) )
2236 item.properties.at( "color" ).value = std::string( "red" );
2237 }
2238
2239 BOOST_CHECK( !snapshotsMatch( expected, actual ) );
2240 BOOST_CHECK( !snapshotsMatch( expected, actual, { { "font", PROPERTY_DISPOSITION::APPROXIMATE } } ) );
2241 BOOST_CHECK( snapshotsMatch(
2244
2245 actual.pop_back();
2246 BOOST_CHECK( !snapshotsMatch(
2249}
2250
2251
2252BOOST_AUTO_TEST_CASE( SnapshotAdaptersCoverSemanticVocabulary )
2253{
2254 SOURCE_PROVENANCE source{ wxS( "snapshot.sch" ), 0x000D, wxS( "synthetic" ), 1, 0, 0x250, 16, 0 };
2255 auto string = [&]( const wxString& aText )
2256 {
2258 result.text = aText;
2259 result.source = source;
2260 return result;
2261 };
2262
2264 model.settings.source = source;
2265 model.sheets.push_back( { SHEET_ID( 1 ), 0, source, string( wxS( "Main" ) ) } );
2266
2267 MODEL_SYMBOL_DEFINITION definition;
2268 definition.id = DEFINITION_ID( 2 );
2269 definition.source = source;
2270 definition.name = string( wxS( "SYM" ) );
2271 definition.graphics.push_back( { source } );
2272 definition.pins.push_back( { PIN_ID( 3 ), source, string( wxS( "1" ) ) } );
2273 definition.fields.push_back( { FIELD_ID( 10 ), source, string( wxS( "Value" ) ), string( wxS( "R" ) ) } );
2274 model.definitions.push_back( definition );
2275
2276 MODEL_PART_TYPE partType;
2277 partType.id = PART_TYPE_ID( 4 );
2278 partType.source = source;
2279 partType.name = string( wxS( "RES" ) );
2280 partType.gates.push_back(
2281 { GATE_ID( 5 ), source, { DEFINITION_ID( 2 ), source }, 1, { { PIN_ID( 3 ), source } } } );
2282 partType.fields.push_back( { FIELD_ID( 11 ), source, string( wxS( "Tolerance" ) ), string( wxS( "1%" ) ) } );
2283 model.partTypes.push_back( partType );
2284
2285 MODEL_PLACEMENT placement;
2286 placement.id = PLACEMENT_ID( 6 );
2287 placement.source = source;
2288 placement.reference = string( wxS( "R1" ) );
2289 placement.fields.push_back( { FIELD_ID( 12 ), source, string( wxS( "Value" ) ), string( wxS( "10k" ) ) } );
2290 model.placements.push_back( placement );
2291
2292 MODEL_NET net;
2293 net.id = NET_ID( 7 );
2294 net.source = source;
2295 net.name = string( wxS( "N" ) );
2296 net.connections.push_back( { source } );
2297 model.nets.push_back( net );
2298
2299 MODEL_BUS bus;
2300 bus.id = BUS_ID( 8 );
2301 bus.source = source;
2302 bus.name = string( wxS( "D[0..0]" ) );
2303 bus.entries.push_back( { source, {}, { NET_ID( 7 ), source } } );
2304 bus.aliases.push_back( string( wxS( "DATA" ) ) );
2305 bus.memberNets.push_back( { NET_ID( 7 ), source } );
2306 model.buses.push_back( bus );
2307 model.labels.push_back( { source, {}, MODEL_LABEL_KIND::LOCAL, string( wxS( "N" ) ) } );
2308 model.junctions.push_back( { source } );
2309 model.texts.push_back( { source, {}, string( wxS( "note" ) ) } );
2310 model.graphics.push_back( { source, { SHEET_ID( 1 ), source }, { source } } );
2311
2312 std::vector<CANONICAL_SEMANTIC_RECORD> binaryRecords = normalizeBinaryModel( model );
2313 std::set<CANONICAL_KIND> binaryKinds;
2314
2315 for( const CANONICAL_SEMANTIC_RECORD& item : binaryRecords )
2316 binaryKinds.insert( item.kind );
2317
2318 const std::set<CANONICAL_KIND> expectedKinds{
2319 CANONICAL_KIND::SETTINGS, CANONICAL_KIND::SHEET, CANONICAL_KIND::DEFINITION,
2320 CANONICAL_KIND::GRAPHIC, CANONICAL_KIND::PIN, CANONICAL_KIND::FIELD,
2321 CANONICAL_KIND::PART_TYPE, CANONICAL_KIND::GATE, CANONICAL_KIND::GATE_PIN_MAPPING,
2322 CANONICAL_KIND::PLACEMENT, CANONICAL_KIND::NET, CANONICAL_KIND::CONNECTION,
2323 CANONICAL_KIND::BUS, CANONICAL_KIND::BUS_ENTRY, CANONICAL_KIND::BUS_ALIAS,
2324 CANONICAL_KIND::BUS_MEMBER, CANONICAL_KIND::LABEL, CANONICAL_KIND::JUNCTION,
2325 CANONICAL_KIND::TEXT
2326 };
2327 BOOST_CHECK( binaryKinds == expectedKinds );
2328
2329 const std::string fixtureRoot = KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/binary/";
2330 PADS_SCH::PADS_SCH_PARSER asciiParser;
2331 BOOST_REQUIRE( asciiParser.Parse( fixtureRoot + "minimal_v13.txt" ) );
2332 std::vector<CANONICAL_SEMANTIC_RECORD> asciiRecords = normalizeAsciiModel( asciiParser );
2333 BOOST_CHECK( std::ranges::any_of( asciiRecords,
2334 []( const CANONICAL_SEMANTIC_RECORD& aItem )
2335 {
2336 return aItem.kind == CANONICAL_KIND::SETTINGS;
2337 } ) );
2338 BOOST_CHECK( std::ranges::any_of( asciiRecords,
2339 []( const CANONICAL_SEMANTIC_RECORD& aItem )
2340 {
2341 return aItem.kind == CANONICAL_KIND::SHEET;
2342 } ) );
2343
2344 PADS_SCH_BINARY_PARSER binaryParser;
2345 std::vector<CANONICAL_SEMANTIC_RECORD> minimalBinary =
2346 normalizeBinaryModel( binaryParser.Parse( loadMinimalV13(), wxS( "minimal_v13.sch" ) ) );
2347 auto countKind = []( const std::vector<CANONICAL_SEMANTIC_RECORD>& aItems, CANONICAL_KIND aKind )
2348 {
2349 return std::ranges::count_if( aItems,
2350 [&]( const CANONICAL_SEMANTIC_RECORD& aItem )
2351 {
2352 return aItem.kind == aKind;
2353 } );
2354 };
2355 BOOST_CHECK_EQUAL( countKind( minimalBinary, CANONICAL_KIND::SETTINGS ),
2356 countKind( asciiRecords, CANONICAL_KIND::SETTINGS ) );
2357 BOOST_CHECK_EQUAL( countKind( minimalBinary, CANONICAL_KIND::SHEET ),
2358 countKind( asciiRecords, CANONICAL_KIND::SHEET ) );
2359}
2360
2361
2362BOOST_AUTO_TEST_CASE( HandbuiltBusAdapterParity )
2363{
2364 const std::string fixture = KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/buses.txt";
2365 PADS_SCH::PADS_SCH_PARSER asciiParser;
2366 BOOST_REQUIRE( asciiParser.Parse( fixture ) );
2367
2368 SOURCE_PROVENANCE source{ wxS( "buses.sch" ), 0x000D, wxS( "synthetic" ), 1, 0, 0, 0, 0 };
2369 auto string = [&]( const wxString& aText )
2370 {
2372 result.text = aText;
2373 result.source = source;
2374 return result;
2375 };
2376
2377 PADS_SCH_MODEL binary;
2379 binary.settings.pageSize = { 34000, 22000, source };
2380 binary.settings.defaultLineWidth = 20;
2381 binary.settings.defaultBusWidth = 50;
2382
2383 MODEL_SHEET sheet{ SHEET_ID( 1 ), 0, source, string( wxS( "Main" ) ) };
2384 sheet.pageSize = binary.settings.pageSize;
2385 sheet.defaultLineWidth = 20;
2386 sheet.defaultBusWidth = 50;
2387 binary.sheets.push_back( sheet );
2388
2389 for( int i = 0; i < 3; ++i )
2390 {
2391 MODEL_NET net;
2392 net.id = NET_ID( i + 1 );
2393 net.source = source;
2394 net.sheet = { SHEET_ID( 1 ), source };
2395 net.name = string( wxString::Format( wxS( "DATA%d" ), i ) );
2396 binary.nets.push_back( net );
2397 }
2398
2399 MODEL_BUS bus;
2400 bus.id = BUS_ID( 2 );
2401 bus.source = source;
2402 bus.sheet = { SHEET_ID( 1 ), source };
2403 bus.name = string( wxS( "DATA[0..2]" ) );
2404 bus.vertices = { { 6000, 14000, source }, { 14000, 14000, source }, { 14000, 10000, source } };
2405 bus.aliases.push_back( string( wxS( "DATA[0..2]" ) ) );
2406
2407 for( int i = 0; i < 3; ++i )
2408 {
2409 bus.entries.push_back( { source, { 8000 + i * 2000, 14000, source }, { NET_ID( i + 1 ), source } } );
2410 bus.memberNets.push_back( { NET_ID( i + 1 ), source } );
2411 }
2412
2413 binary.buses.push_back( bus );
2414 BOOST_CHECK( snapshotsMatch( normalizeBinaryModel( binary ), normalizeAsciiModel( asciiParser ) ) );
2415
2416 std::vector<CANONICAL_SEMANTIC_RECORD> records = normalizeBinaryModel( binary );
2417 auto entry = std::ranges::find_if( records,
2418 []( const CANONICAL_SEMANTIC_RECORD& aRecord )
2419 {
2420 return aRecord.kind == CANONICAL_KIND::BUS_ENTRY;
2421 } );
2422 BOOST_REQUIRE( entry != records.end() );
2423 BOOST_CHECK( entry->properties.at( "member_net" ).value == CANONICAL_VALUE( std::string( "DATA0" ) ) );
2424 auto busRecord = std::ranges::find_if( records,
2425 []( const CANONICAL_SEMANTIC_RECORD& aRecord )
2426 {
2427 return aRecord.kind == CANONICAL_KIND::BUS;
2428 } );
2429 BOOST_REQUIRE( busRecord != records.end() );
2430 BOOST_CHECK( busRecord->properties.at( "member_nets" ).value
2431 == CANONICAL_VALUE( std::vector<std::string>{ "DATA0", "DATA1", "DATA2" } ) );
2432
2433 PADS_SCH_MODEL wrongMapping = binary;
2434 wrongMapping.buses[0].entries[0].memberNet.id = NET_ID( 2 );
2435 BOOST_CHECK( !snapshotsMatch( normalizeBinaryModel( wrongMapping ), normalizeAsciiModel( asciiParser ) ) );
2436
2437 PADS_SCH_MODEL missingMember = binary;
2438 missingMember.buses[0].memberNets.pop_back();
2439 BOOST_CHECK( !snapshotsMatch( normalizeBinaryModel( missingMember ), normalizeAsciiModel( asciiParser ) ) );
2440}
2441
2442
2443BOOST_AUTO_TEST_CASE( CanonicalEnumMappings )
2444{
2445 BOOST_CHECK( canonicalGraphicType( MODEL_GRAPHIC_KIND::RECTANGLE ).value
2446 == CANONICAL_VALUE( std::string( "rectangle" ) ) );
2447 BOOST_CHECK( canonicalGraphicType( MODEL_GRAPHIC_KIND::CIRCLE ).value
2448 == CANONICAL_VALUE( std::string( "circle" ) ) );
2449 BOOST_CHECK( canonicalGraphicType( MODEL_GRAPHIC_KIND::ARC ).value == CANONICAL_VALUE( std::string( "arc" ) ) );
2450 BOOST_CHECK( canonicalGraphicType( MODEL_GRAPHIC_KIND::POLYLINE ).value
2451 == CANONICAL_VALUE( std::string( "polyline" ) ) );
2452 BOOST_CHECK( canonicalGraphicType( PADS_SCH::GRAPHIC_TYPE::RECTANGLE ).value
2453 == CANONICAL_VALUE( std::string( "rectangle" ) ) );
2454 BOOST_CHECK( canonicalGraphicType( PADS_SCH::GRAPHIC_TYPE::CIRCLE ).value
2455 == CANONICAL_VALUE( std::string( "circle" ) ) );
2456 BOOST_CHECK( canonicalGraphicType( PADS_SCH::GRAPHIC_TYPE::ARC ).value == CANONICAL_VALUE( std::string( "arc" ) ) );
2457 BOOST_CHECK( canonicalGraphicType( PADS_SCH::GRAPHIC_TYPE::POLYLINE ).value
2458 == CANONICAL_VALUE( std::string( "polyline" ) ) );
2459 BOOST_CHECK( canonicalLineStyle( MODEL_LINE_STYLE::DEFAULT ).value == CANONICAL_VALUE( std::string( "solid" ) ) );
2460 BOOST_CHECK( canonicalLineStyle( 255 ).value == CANONICAL_VALUE( std::string( "solid" ) ) );
2461 BOOST_CHECK( canonicalLineStyle( 0 ).value == CANONICAL_VALUE( std::string( "dash" ) ) );
2462 BOOST_CHECK( canonicalLineStyle( 42 ).disposition == PROPERTY_DISPOSITION::UNSUPPORTED );
2463 BOOST_CHECK( canonicalFill( MODEL_FILL_STYLE::HATCHED ).value == CANONICAL_VALUE( std::string( "hatched" ) ) );
2464 BOOST_CHECK( canonicalFill( true ).value == CANONICAL_VALUE( std::string( "filled" ) ) );
2465 BOOST_CHECK( canonicalPinType( uint32_t( 3 ) ).value == CANONICAL_VALUE( std::string( "bidirectional" ) ) );
2466 BOOST_CHECK( canonicalPinType( PADS_SCH::PIN_TYPE::BIDIRECTIONAL ).value
2467 == CANONICAL_VALUE( std::string( "bidirectional" ) ) );
2468 BOOST_CHECK( canonicalPinStyle( 3 ).value == CANONICAL_VALUE( std::string( "inverted_clock" ) ) );
2469 BOOST_CHECK( canonicalPinStyle( true, true ).value == CANONICAL_VALUE( std::string( "inverted_clock" ) ) );
2470 BOOST_CHECK( canonicalJustification( MODEL_JUSTIFICATION::CENTER ).value
2471 == CANONICAL_VALUE( std::string( "center" ) ) );
2472 BOOST_CHECK( canonicalHorizontalJustification( 12 ).value == CANONICAL_VALUE( std::string( "center" ) ) );
2473 BOOST_CHECK( canonicalVerticalJustification( 12 ).value == CANONICAL_VALUE( std::string( "center" ) ) );
2474 BOOST_CHECK( canonicalLabelKind( MODEL_LABEL_KIND::HIERARCHICAL ).value
2475 == CANONICAL_VALUE( std::string( "hierarchical" ) ) );
2476 BOOST_CHECK( canonicalEndpointKind( MODEL_ENDPOINT_KIND::PIN ).value == CANONICAL_VALUE( std::string( "pin" ) ) );
2477
2479 BOOST_REQUIRE( parser.Parse( KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/binary/multigate.txt" ) );
2480 const PADS_SCH::SYMBOL_GRAPHIC* serializedGraphic = nullptr;
2481
2482 for( const PADS_SCH::SYMBOL_DEF& definition : parser.GetSymbolDefs() )
2483 {
2484 auto graphic = std::ranges::find_if( definition.graphics,
2485 []( const PADS_SCH::SYMBOL_GRAPHIC& aGraphic )
2486 {
2487 return aGraphic.line_style == 255;
2488 } );
2489
2490 if( graphic != definition.graphics.end() )
2491 {
2492 serializedGraphic = &*graphic;
2493 break;
2494 }
2495 }
2496
2497 BOOST_REQUIRE( serializedGraphic );
2498 BOOST_CHECK( canonicalLineStyle( serializedGraphic->line_style )
2499 == canonicalLineStyle( MODEL_LINE_STYLE::DEFAULT ) );
2500}
2501
2502
2503BOOST_AUTO_TEST_CASE( GatePinMappingsUseSemanticPinIdentity )
2504{
2505 SOURCE_PROVENANCE source{ wxS( "mapping.sch" ), 0x000D, wxS( "synthetic" ), 1, 0, 0, 0, 0 };
2506 auto string = [&]( const wxString& aText )
2507 {
2509 result.text = aText;
2510 result.source = source;
2511 return result;
2512 };
2513
2515 MODEL_SYMBOL_DEFINITION definition;
2516 definition.id = DEFINITION_ID( 1 );
2517 definition.source = source;
2518 definition.pins.push_back( { PIN_ID( 91 ), source, string( wxS( "A1" ) ), string( wxS( "CLK" ) ) } );
2519 model.definitions.push_back( definition );
2520
2521 MODEL_PART_TYPE part;
2522 part.source = source;
2523 part.gates.push_back( { GATE_ID( 2 ), source, { definition.id, source }, 1, { { PIN_ID( 91 ), source } } } );
2524 model.partTypes.push_back( part );
2525
2526 const auto records = normalizeBinaryModel( model );
2527 auto mapping = std::ranges::find_if( records,
2528 []( const CANONICAL_SEMANTIC_RECORD& aRecord )
2529 {
2530 return aRecord.kind == CANONICAL_KIND::GATE_PIN_MAPPING;
2531 } );
2532 BOOST_REQUIRE( mapping != records.end() );
2533 BOOST_CHECK( mapping->properties.at( "pin_number" ).value == CANONICAL_VALUE( std::string( "A1" ) ) );
2534 BOOST_CHECK( mapping->properties.at( "pin_name" ).value == CANONICAL_VALUE( std::string( "CLK" ) ) );
2535 BOOST_CHECK( !mapping->properties.contains( "pin_id" ) );
2536}
2537
2538
2539BOOST_AUTO_TEST_CASE( SymbolPrimitives )
2540{
2542 PADS_SCH_MODEL model = parser.Parse( loadBinaryFixture( "symbol_primitives.sch" ), wxS( "symbol_primitives.sch" ) );
2543 const MODEL_SYMBOL_DEFINITION& definition = itemNamed( model.definitions, wxS( "BATCHB_PRIMITIVES" ) );
2544
2545 BOOST_REQUIRE_EQUAL( definition.graphics.size(), 6 );
2546 BOOST_CHECK( definition.graphics[0].kind == MODEL_GRAPHIC_KIND::LINE );
2547 BOOST_REQUIRE_EQUAL( definition.graphics[0].points.size(), 2 );
2548 BOOST_CHECK_EQUAL( definition.graphics[0].points[0].x, 200 );
2549 BOOST_CHECK_EQUAL( definition.graphics[0].points[0].y, 200 );
2550 BOOST_CHECK_EQUAL( definition.graphics[0].points[1].x, 800 );
2551 BOOST_CHECK_EQUAL( definition.graphics[0].points[1].y, 200 );
2552 BOOST_CHECK( definition.graphics[1].kind == MODEL_GRAPHIC_KIND::POLYLINE );
2553 BOOST_CHECK( definition.graphics[2].kind == MODEL_GRAPHIC_KIND::CIRCLE );
2554 BOOST_CHECK( definition.graphics[3].kind == MODEL_GRAPHIC_KIND::ARC );
2555 BOOST_CHECK_EQUAL( definition.graphics[3].strokeWidth, 20 );
2556 BOOST_CHECK( definition.graphics[3].lineStyle == MODEL_LINE_STYLE::SOLID );
2557 BOOST_CHECK_EQUAL( definition.graphics[3].arcSweepAngle, 1800 );
2558 BOOST_CHECK( definition.graphics[3].arcClockwise );
2559 BOOST_CHECK_EQUAL( definition.graphics[3].arcCenter.x, 1300 );
2560 BOOST_CHECK_EQUAL( definition.graphics[3].arcCenter.y, 1100 );
2561 BOOST_CHECK_EQUAL( definition.graphics[3].arcBoundsStart.x, 1000 );
2562 BOOST_CHECK_EQUAL( definition.graphics[3].arcBoundsStart.y, 800 );
2563 BOOST_CHECK_EQUAL( definition.graphics[3].arcBoundsEnd.x, 1600 );
2564 BOOST_CHECK_EQUAL( definition.graphics[3].arcBoundsEnd.y, 1400 );
2565 BOOST_CHECK( definition.graphics[4].fill == MODEL_FILL_STYLE::FILLED );
2566 BOOST_CHECK( definition.graphics[5].kind == MODEL_GRAPHIC_KIND::TEXT );
2567 BOOST_CHECK_EQUAL( definition.graphics[5].text.text, wxS( "EMBEDDED_TEXT" ) );
2568 BOOST_CHECK_EQUAL( definition.graphics[5].presentation.font.text, wxS( "Verdana" ) );
2569 BOOST_REQUIRE_EQUAL( definition.graphics[5].points.size(), 1 );
2570 BOOST_CHECK_EQUAL( definition.graphics[5].points[0].x, 1000 );
2571 BOOST_CHECK_EQUAL( definition.graphics[5].points[0].y, 1600 );
2572 BOOST_CHECK_EQUAL( definition.graphics[5].presentation.height, 200 );
2573 BOOST_CHECK_EQUAL( definition.graphics[5].presentation.width, 20 );
2574 BOOST_CHECK_EQUAL( definition.graphics[5].angle, 0 );
2575 BOOST_REQUIRE_EQUAL( definition.pins.size(), 2 );
2576 BOOST_CHECK_EQUAL( definition.pins[0].position.x, 0 );
2577 BOOST_CHECK_EQUAL( definition.pins[0].position.y, 600 );
2578 BOOST_CHECK_EQUAL( definition.pins[1].position.x, 2400 );
2579 BOOST_CHECK_EQUAL( definition.pins[1].position.y, 600 );
2580
2581 std::vector<uint8_t> privateStroke = loadBinaryFixture( "symbol_primitives.sch" );
2582 const size_t privatePiece =
2583 sheetControllerOffset( privateStroke, 4 ) + definition.graphics[0].source.recordIndex * 6;
2584 writeU16( privateStroke, privatePiece + 4, 0xEF03 );
2585 PADS_SCH_MODEL preservedStroke = parser.Parse( privateStroke, wxS( "private-symbol-stroke.sch" ) );
2586 const MODEL_SYMBOL_DEFINITION& preservedDefinition =
2587 itemNamed( preservedStroke.definitions, wxS( "BATCHB_PRIMITIVES" ) );
2588 auto privateGraphic = std::ranges::find_if( preservedDefinition.graphics,
2589 [&]( const MODEL_GRAPHIC& aGraphic )
2590 {
2591 return aGraphic.source.controller == 4
2592 && aGraphic.source.recordIndex
2593 == definition.graphics[0].source.recordIndex;
2594 } );
2595 BOOST_REQUIRE( privateGraphic != preservedDefinition.graphics.end() );
2596 BOOST_CHECK_EQUAL( privateGraphic->strokeWidth, 0 );
2597 auto rawStroke = std::ranges::find_if( privateGraphic->properties,
2598 []( const SOURCE_PROPERTY& aProperty )
2599 {
2600 return aProperty.name.text == wxS( "unsupported_graphic_stroke_width" );
2601 } );
2602 BOOST_REQUIRE( rawStroke != privateGraphic->properties.end() );
2603 BOOST_CHECK_EQUAL( rawStroke->value.text, wxS( "3" ) );
2604 BOOST_CHECK( rawStroke->disposition == PROPERTY_DISPOSITION::UNSUPPORTED );
2605 BOOST_CHECK_EQUAL( rawStroke->source.absoluteOffset, privatePiece + 4 );
2606 BOOST_CHECK_EQUAL( rawStroke->source.length, 1 );
2607 BOOST_CHECK_EQUAL( propertyValue( privateGraphic->properties, wxS( "preserved_graphic_presentation" ) ),
2608 wxS( "239" ) );
2609}
2610
2611
2612BOOST_AUTO_TEST_CASE( TerminalOffsetPresentation )
2613{
2615 std::vector<uint8_t> fixture = loadBinaryFixture( "pin_styles.sch" );
2616 PADS_SCH_MODEL baseline = parser.Parse( fixture, wxS( "pin_styles.sch" ) );
2617 const MODEL_PIN_DEFINITION& baselinePin =
2618 itemNamed( baseline.definitions, wxS( "BATCHB_PIN_STYLES" ) ).pins[0];
2619 const size_t visibilityOffset = baselinePin.source.absoluteOffset + 24;
2620
2621 writeU16( fixture, visibilityOffset, 0x8040 );
2622 PADS_SCH_MODEL visibilityOnly = parser.Parse( fixture, wxS( "terminal-visibility-only.sch" ) );
2623 const MODEL_PIN_DEFINITION& visibilityOnlyPin =
2624 itemNamed( visibilityOnly.definitions, wxS( "BATCHB_PIN_STYLES" ) ).pins[0];
2625
2626 BOOST_CHECK_EQUAL( visibilityOnlyPin.visibilityFlags, 0x80 );
2627 BOOST_CHECK( std::ranges::none_of( visibilityOnly.diagnostics,
2628 [&]( const PARSER_DIAGNOSTIC& aDiagnostic )
2629 {
2630 return aDiagnostic.message.Contains(
2631 wxS( "terminal number-offset presentation" ) )
2632 && aDiagnostic.source.controller == 8
2633 && aDiagnostic.source.recordIndex
2634 == baselinePin.source.recordIndex;
2635 } ) );
2636
2637 writeU16( fixture, visibilityOffset, 0x0D40 );
2638 PADS_SCH_MODEL pairedPresentation = parser.Parse( fixture, wxS( "terminal-presentation-0d.sch" ) );
2639 const MODEL_PIN_DEFINITION& pairedPin =
2640 itemNamed( pairedPresentation.definitions, wxS( "BATCHB_PIN_STYLES" ) ).pins[0];
2641
2643 BOOST_CHECK_EQUAL( pairedPin.nameOffsetAngle, 900 );
2645 BOOST_CHECK_EQUAL( pairedPin.numberOffsetAngle, 0 );
2647 BOOST_CHECK( std::ranges::none_of( pairedPresentation.diagnostics,
2648 [&]( const PARSER_DIAGNOSTIC& aDiagnostic )
2649 {
2650 return ( aDiagnostic.message.Contains(
2651 wxS( "terminal name-offset presentation" ) )
2652 || aDiagnostic.message.Contains(
2653 wxS( "terminal number-offset presentation" ) ) )
2654 && aDiagnostic.source.controller == 8
2655 && aDiagnostic.source.recordIndex
2656 == baselinePin.source.recordIndex;
2657 } ) );
2658}
2659
2660
2661BOOST_AUTO_TEST_CASE( PartPinsAndGates )
2662{
2664 PADS_SCH_MODEL pins = parser.Parse( loadBinaryFixture( "pin_styles.sch" ), wxS( "pin_styles.sch" ) );
2665 const MODEL_PART_TYPE& pinPart = itemNamed( pins.partTypes, wxS( "BATCHB-PIN-STYLES" ) );
2666 BOOST_REQUIRE_EQUAL( pinPart.gates.size(), 1 );
2667 BOOST_REQUIRE_EQUAL( pinPart.gates[0].pins.size(), 7 );
2668
2669 const MODEL_SYMBOL_DEFINITION& pinDefinition = itemNamed( pins.definitions, wxS( "BATCHB_PIN_STYLES" ) );
2670 BOOST_REQUIRE_EQUAL( pinDefinition.pins.size(), 7 );
2671 BOOST_CHECK_EQUAL( pinDefinition.pins[0].number.text, wxS( "1" ) );
2672 BOOST_CHECK_EQUAL( pinDefinition.pins[0].name.text, wxS( "INPUT_PIN" ) );
2673 BOOST_CHECK_EQUAL( pinDefinition.pins[0].electricalType, 1 );
2674 BOOST_CHECK_EQUAL( pinDefinition.pins[1].electricalType, 2 );
2675 BOOST_CHECK_EQUAL( pinDefinition.pins[4].graphicStyle, 1 );
2676 BOOST_CHECK_EQUAL( pinDefinition.pins[5].graphicStyle, 2 );
2677 BOOST_CHECK( pinDefinition.pins[6].presentation.visible );
2678 BOOST_CHECK_EQUAL( pinDefinition.pins[4].side, 1 );
2679 BOOST_CHECK_EQUAL( pinDefinition.pins[4].angle, 0 );
2680 BOOST_CHECK_EQUAL( pinDefinition.pins[4].length, 280 );
2681 BOOST_CHECK_EQUAL( propertyValue( pinDefinition.pins[0].properties, wxS( "pin_name_height_half_mils" ) ),
2682 wxS( "200" ) );
2683 BOOST_CHECK_EQUAL( propertyValue( pinDefinition.pins[0].properties, wxS( "pin_number_height_half_mils" ) ),
2684 wxS( "200" ) );
2685 BOOST_CHECK_EQUAL( propertyValue( pinDefinition.pins[0].properties, wxS( "terminal_p_line_position_1" ) ),
2686 wxS( "-200,-100" ) );
2687 BOOST_CHECK_EQUAL( propertyValue( pinDefinition.pins[0].properties, wxS( "terminal_p_line_position_2" ) ),
2688 wxS( "500,-100" ) );
2689 BOOST_CHECK_EQUAL( pinDefinition.pins[0].namePresentation.width, 20 );
2690 BOOST_CHECK_EQUAL( pinDefinition.pins[0].numberPresentation.width, 20 );
2691 BOOST_CHECK_EQUAL( pinDefinition.pins[0].nameOffset.x, 200 );
2692 BOOST_CHECK_EQUAL( pinDefinition.pins[0].nameOffset.y, 20 );
2693 BOOST_CHECK_EQUAL( pinDefinition.pins[0].numberOffset.x, 200 );
2694 BOOST_CHECK_EQUAL( pinDefinition.pins[0].numberOffset.y, 20 );
2695 BOOST_CHECK( pinDefinition.pins[2].presentation.visible );
2696 BOOST_CHECK( pinDefinition.pins[2].namePresentation.visible );
2697 BOOST_CHECK( !pinDefinition.pins[6].namePresentation.visible );
2698 BOOST_CHECK( pinDefinition.pins[6].numberPresentation.visible );
2699 BOOST_REQUIRE_EQUAL( pinPart.signalPins.size(), 1 );
2700 BOOST_CHECK_EQUAL( pinPart.signalPins[0].number.text, wxS( "8" ) );
2701 BOOST_CHECK_EQUAL( pinPart.signalPins[0].name.text, wxS( "VCC_HIDDEN" ) );
2702
2703 PADS_SCH_MODEL multi = parser.Parse( loadBinaryFixture( "multigate.sch" ), wxS( "multigate.sch" ) );
2704 const MODEL_PART_TYPE& multiPart = itemNamed( multi.partTypes, wxS( "BATCHD-MULTIGATE" ) );
2705 BOOST_REQUIRE_EQUAL( multiPart.gates.size(), 2 );
2706 BOOST_CHECK_EQUAL( multiPart.gates[0].unit, 1 );
2707 BOOST_CHECK_EQUAL( multiPart.gates[0].pins.size(), 7 );
2708 BOOST_CHECK_EQUAL( multiPart.gates[1].unit, 2 );
2709 BOOST_CHECK_EQUAL( multiPart.gates[1].pins.size(), 2 );
2710 BOOST_CHECK_EQUAL( multiPart.gates[0].properties.front().name.text, wxS( "swap_group" ) );
2711 const MODEL_PART_TYPE& alternatePart = itemNamed( multi.partTypes, wxS( "RES-RESN1" ) );
2712 BOOST_REQUIRE_EQUAL( alternatePart.gates[0].alternateDefinitions.size(), 3 );
2713 BOOST_REQUIRE_EQUAL( multiPart.signalPins.size(), 1 );
2714 BOOST_CHECK_EQUAL( multiPart.signalPins[0].number.text, wxS( "15" ) );
2715
2716 PADS_SCH_MODEL connectorModel = parser.Parse( loadBinaryFixture( "connectors.sch" ), wxS( "connectors.sch" ) );
2717 const MODEL_PART_TYPE& connector = itemNamed( connectorModel.partTypes, wxS( "CON-26P-ED" ) );
2718 BOOST_REQUIRE_EQUAL( connector.gates.size(), 1 );
2719 BOOST_REQUIRE_EQUAL( connector.gates[0].decalGroupMembers.size(), 5 );
2720 BOOST_REQUIRE_EQUAL( connector.gates[0].connectorPins.size(), 26 );
2721
2722 for( const DEFINITION_REFERENCE& member : connector.gates[0].decalGroupMembers )
2723 {
2724 BOOST_CHECK( member.id.IsValid() );
2725 BOOST_CHECK_EQUAL( member.source.controller, 10 );
2726 }
2727
2728 for( size_t pin = 0; pin < connector.gates[0].connectorPins.size(); ++pin )
2729 {
2730 const MODEL_CONNECTOR_PIN& connectorPin = connector.gates[0].connectorPins[pin];
2731 BOOST_CHECK_EQUAL( connectorPin.number.text, wxString::Format( wxS( "%llu" ), pin + 1 ) );
2732 BOOST_CHECK_EQUAL( connectorPin.name.text, wxString() );
2733 BOOST_CHECK_EQUAL( connectorPin.electricalType, 0 );
2734 BOOST_CHECK_EQUAL( connectorPin.swapGroup, 0 );
2735 BOOST_CHECK_EQUAL( connectorPin.flags, 0 );
2736 BOOST_CHECK_EQUAL( connectorPin.source.controller, 11 );
2737 BOOST_CHECK_EQUAL( connectorPin.source.length, 24 );
2738 BOOST_CHECK_EQUAL( connectorPin.number.source.controller, 11 );
2739 BOOST_CHECK_EQUAL( connectorPin.number.source.absoluteOffset, connectorPin.source.absoluteOffset + 4 );
2740 BOOST_CHECK_EQUAL( connectorPin.number.source.length, 16 );
2741 }
2742}
2743
2744
2745BOOST_AUTO_TEST_CASE( DefinitionFields )
2746{
2747 const FIELD_ID controller7Record11214 = MakeFieldId( FIELD_ID_DOMAIN::DEFINITION, 11214, 0 );
2748 const FIELD_ID controller7Record12125 = MakeFieldId( FIELD_ID_DOMAIN::DEFINITION, 12125, 0 );
2749 BOOST_CHECK( controller7Record11214.IsValid() );
2750 BOOST_CHECK( controller7Record12125.IsValid() );
2751 BOOST_CHECK_NE( controller7Record11214.Value(), controller7Record12125.Value() );
2752
2754 PADS_SCH_MODEL model = parser.Parse( loadBinaryFixture( "fields.sch" ), wxS( "fields.sch" ) );
2755 const MODEL_SYMBOL_DEFINITION& definition = itemNamed( model.definitions, wxS( "RESZ-H" ) );
2756 BOOST_REQUIRE_EQUAL( definition.fields.size(), 4 );
2757 std::set<uint64_t> fieldIds;
2758
2759 for( size_t ordinal = 0; ordinal < definition.fields.size(); ++ordinal )
2760 {
2761 const MODEL_FIELD& field = definition.fields[ordinal];
2762 BOOST_CHECK( field.id.IsValid() );
2763 BOOST_CHECK_EQUAL( field.id.Value(),
2764 MakeFieldId( FIELD_ID_DOMAIN::DEFINITION, definition.id.Value(), ordinal ).Value() );
2765 BOOST_CHECK( fieldIds.insert( field.id.Value() ).second );
2766 }
2767 BOOST_CHECK_EQUAL( definition.fields[0].name.text, wxS( "REF-DES" ) );
2768 BOOST_CHECK_EQUAL( definition.fields[1].name.text, wxS( "PART-TYPE" ) );
2769 BOOST_CHECK_EQUAL( definition.fields[2].name.text, wxS( "VALUE" ) );
2770 BOOST_CHECK_EQUAL( definition.fields[3].name.text, wxS( "*" ) );
2771 BOOST_CHECK_EQUAL( definition.fields[0].presentation.font.text, wxS( "Verdana" ) );
2772 BOOST_CHECK_EQUAL( definition.fields[0].presentation.height, 200 );
2773 BOOST_CHECK_EQUAL( definition.fields[0].presentation.width, 20 );
2774 BOOST_CHECK_EQUAL( definition.fields[0].position.x, 600 );
2775 BOOST_CHECK_EQUAL( definition.fields[0].position.y, 200 );
2776 BOOST_CHECK_EQUAL( definition.fields[1].position.x, 620 );
2777 BOOST_CHECK_EQUAL( definition.fields[1].position.y, 400 );
2778 BOOST_CHECK( definition.fields[0].value.text.empty() );
2779 BOOST_CHECK_EQUAL( propertyValue( definition.fields[0].presentation.properties, wxS( "font_handle" ) ),
2780 wxS( "-1" ) );
2781 BOOST_CHECK_EQUAL( propertyValue( definition.fields[1].presentation.properties, wxS( "font_handle" ) ),
2782 wxS( "-1" ) );
2783 BOOST_CHECK_EQUAL( propertyValue( definition.fields[2].presentation.properties, wxS( "font_handle" ) ),
2784 wxS( "-4" ) );
2785 BOOST_CHECK_EQUAL( propertyValue( definition.fields[3].presentation.properties, wxS( "font_handle" ) ),
2786 wxS( "-4" ) );
2787 BOOST_CHECK( definition.fields[0].presentation.horizontalJustification == MODEL_JUSTIFICATION::CENTER );
2788 BOOST_CHECK( definition.fields[0].presentation.verticalJustification == MODEL_JUSTIFICATION::RIGHT );
2789 BOOST_CHECK( definition.fields[2].presentation.horizontalJustification == MODEL_JUSTIFICATION::CENTER );
2790 BOOST_CHECK( definition.fields[2].presentation.verticalJustification == MODEL_JUSTIFICATION::LEFT );
2791
2792 const MODEL_PART_TYPE& partType = itemNamed( model.partTypes, wxS( "RES-RESN1" ) );
2793 BOOST_REQUIRE_EQUAL( partType.fields.size(), 4 );
2794
2795 for( size_t i = 0; i < partType.fields.size(); ++i )
2796 {
2797 BOOST_CHECK_EQUAL( partType.fields[i].name.text, definition.fields[i].name.text );
2798 BOOST_CHECK_EQUAL( partType.fields[i].value.text, definition.fields[i].value.text );
2799 BOOST_CHECK( partType.fields[i].presentation == definition.fields[i].presentation );
2800 BOOST_CHECK( partType.fields[i].id.IsValid() );
2801 BOOST_CHECK_EQUAL( partType.fields[i].id.Value(),
2802 MakeFieldId( FIELD_ID_DOMAIN::PART_TYPE, partType.id.Value(), i ).Value() );
2803 BOOST_CHECK( fieldIds.insert( partType.fields[i].id.Value() ).second );
2804 }
2805
2806 PADS_SCH_MODEL repeated = parser.Parse( loadBinaryFixture( "fields.sch" ), wxS( "fields.sch" ) );
2807 BOOST_CHECK( repeated.definitions == model.definitions );
2808 BOOST_CHECK( repeated.partTypes == model.partTypes );
2809}
2810
2811
2812BOOST_AUTO_TEST_CASE( PlacementTransforms )
2813{
2816 parser.Parse( loadBinaryFixture( "placement_transform.sch" ), wxS( "placement_transform.sch" ) );
2817 BOOST_REQUIRE_EQUAL( model.placements.size(), 5 );
2818
2819 const std::array<wxString, 5> references = { wxS( "R1" ), wxS( "R2" ), wxS( "R3" ), wxS( "R4" ), wxS( "R5" ) };
2820 const std::array<SOURCE_POINT, 5> positions = { SOURCE_POINT{ -4800, 18200 }, SOURCE_POINT{ -200, 14800 },
2821 SOURCE_POINT{ 2200, 14000 }, SOURCE_POINT{ 3400, 13000 },
2822 SOURCE_POINT{ 6000, 10800 } };
2823 const std::array<int, 5> angles = { 0, 900, 0, 900, 900 };
2824 const std::array<bool, 5> mirrored = { false, false, true, true, true };
2825 const std::array<uint16_t, 5> mirrorFlags = { 0, 0, 3, 3, 2 };
2826 const std::array<std::pair<uint16_t, int>, 4> orthogonalAngles = {
2827 std::pair<uint16_t, int>{ 0, 0 }, { 900, 900 }, { 1800, 1800 }, { 2700, 2700 }
2828 };
2829
2830 for( const auto& [raw, normalized] : orthogonalAngles )
2831 {
2832 std::vector<uint8_t> transform = loadBinaryFixture( "placement_transform.sch" );
2833 writeU16( transform, sheetControllerOffset( transform, 15 ) + 0x24, raw );
2834 PADS_SCH_MODEL transformed = parser.Parse( transform, wxS( "placement_transform.sch" ) );
2835 BOOST_CHECK_EQUAL( transformed.placements[0].angle, normalized );
2836 BOOST_CHECK_EQUAL( propertyValue( transformed.placements[0].properties, wxS( "raw_angle" ) ),
2837 wxString::Format( wxS( "%u" ), raw ) );
2838 }
2839
2840 for( size_t i = 0; i < model.placements.size(); ++i )
2841 {
2842 const MODEL_PLACEMENT& placement = model.placements[i];
2843 BOOST_CHECK_EQUAL( placement.reference.text, references[i] );
2844 BOOST_CHECK_EQUAL( placement.position.x, positions[i].x );
2845 BOOST_CHECK_EQUAL( placement.position.y, positions[i].y );
2846 BOOST_CHECK_EQUAL( placement.angle, angles[i] );
2847 BOOST_CHECK_EQUAL( placement.mirrored, mirrored[i] );
2848 BOOST_CHECK_EQUAL( placement.mirrorFlags, mirrorFlags[i] );
2849 BOOST_CHECK_EQUAL( propertyValue( placement.properties, wxS( "raw_angle" ) ),
2850 wxString::Format( wxS( "%d" ), angles[i] ) );
2851 BOOST_CHECK_EQUAL( propertyValue( placement.properties, wxS( "raw_mirror" ) ),
2852 wxString::Format( wxS( "%u" ), mirrorFlags[i] ) );
2853 auto rawAngle = std::ranges::find_if( placement.properties,
2854 []( const SOURCE_PROPERTY& aProperty )
2855 {
2856 return aProperty.name.text == wxS( "raw_angle" );
2857 } );
2858 auto rawMirror = std::ranges::find_if( placement.properties,
2859 []( const SOURCE_PROPERTY& aProperty )
2860 {
2861 return aProperty.name.text == wxS( "raw_mirror" );
2862 } );
2863 BOOST_REQUIRE( rawAngle != placement.properties.end() );
2864 BOOST_REQUIRE( rawMirror != placement.properties.end() );
2865 BOOST_CHECK_EQUAL( rawAngle->source.absoluteOffset, placement.source.absoluteOffset + 0x24 );
2866 BOOST_CHECK_EQUAL( rawMirror->source.absoluteOffset, placement.source.absoluteOffset + 0x26 );
2867 BOOST_CHECK_EQUAL( rawAngle->source.length, 2 );
2868 BOOST_CHECK_EQUAL( rawMirror->source.length, 2 );
2869 BOOST_CHECK_EQUAL( placement.sheet.id.Value(), model.sheets[0].id.Value() );
2870 BOOST_REQUIRE( placement.gate.has_value() );
2871 BOOST_CHECK( placement.partType.id.IsValid() );
2872 BOOST_CHECK( placement.gate->id.IsValid() );
2873 BOOST_CHECK( placement.definition.id.IsValid() );
2874 BOOST_CHECK_EQUAL( placement.pins.size(), 2 );
2875 BOOST_CHECK_GE( placement.fields.size(), 3 );
2876 BOOST_CHECK_EQUAL( placement.source.controller, 15 );
2877 }
2878
2879 BOOST_CHECK_EQUAL( model.placements[0].pins[0].numberOffset.x, 34 );
2880 BOOST_CHECK_EQUAL( model.placements[0].pins[0].numberOffset.y, 16 );
2881 BOOST_CHECK_EQUAL( model.placements[0].pins[0].numberAngle, 0 );
2882 BOOST_CHECK_EQUAL( model.placements[0].pins[0].numberJustification, 0 );
2883 BOOST_CHECK_EQUAL( model.placements[0].pins[0].numberPresentationFlags, 0x0100 );
2884 BOOST_CHECK_EQUAL( model.placements[0].pins[1].numberOffset.x, 0 );
2885 BOOST_CHECK_EQUAL( model.placements[0].pins[1].numberOffset.y, 20 );
2886 BOOST_CHECK_EQUAL( model.placements[0].pins[1].numberPresentationFlags, 0 );
2887
2888 auto placementNamed = []( const PADS_SCH_MODEL& aModel, const wxString& aReference ) -> const MODEL_PLACEMENT&
2889 {
2890 auto placement = std::ranges::find_if( aModel.placements,
2891 [&]( const MODEL_PLACEMENT& aPlacement )
2892 {
2893 return aPlacement.reference.text == aReference;
2894 } );
2895 BOOST_REQUIRE( placement != aModel.placements.end() );
2896 return *placement;
2897 };
2898
2899 PADS_SCH_MODEL multi = parser.Parse( loadBinaryFixture( "multigate.sch" ), wxS( "multigate.sch" ) );
2900 const MODEL_PLACEMENT& gateA = placementNamed( multi, wxS( "U3-A" ) );
2901 const MODEL_PLACEMENT& gateB = placementNamed( multi, wxS( "U3-B" ) );
2902 BOOST_CHECK_EQUAL( gateA.unit, 1 );
2903 BOOST_CHECK_EQUAL( gateB.unit, 2 );
2904 BOOST_CHECK( gateA.definition.id != gateB.definition.id );
2905 BOOST_CHECK_EQUAL( gateA.pins.size(), 7 );
2906 BOOST_CHECK_EQUAL( gateB.pins.size(), 2 );
2907
2908 PADS_SCH_MODEL connectors = parser.Parse( loadBinaryFixture( "connectors.sch" ), wxS( "connectors.sch" ) );
2909 const MODEL_PLACEMENT& connector = placementNamed( connectors, wxS( "P1-1" ) );
2910 BOOST_CHECK_EQUAL( connector.unit, 1 );
2911 BOOST_CHECK_GE( connector.fields.size(), 4 );
2912 BOOST_CHECK_EQUAL( connector.fields[2].name.text, wxS( "*" ) );
2913 BOOST_CHECK( connector.fields[2].value.text.empty() );
2914 BOOST_CHECK_EQUAL( connector.fields[3].name.text, wxS( "*" ) );
2915 BOOST_CHECK( connector.fields[3].value.text.empty() );
2916}
2917
2918
2919BOOST_AUTO_TEST_CASE( PlacementInstanceFields )
2920{
2922 PADS_SCH_MODEL model = parser.Parse( loadBinaryFixture( "fields.sch" ), wxS( "fields.sch" ) );
2923 BOOST_REQUIRE_EQUAL( model.placements.size(), 1 );
2924 const MODEL_PLACEMENT& placement = model.placements[0];
2925 BOOST_REQUIRE_GE( placement.fields.size(), 3 );
2926
2927 BOOST_CHECK_EQUAL( placement.fields[0].name.text, wxS( "REF-DES" ) );
2928 BOOST_CHECK_EQUAL( placement.fields[0].value.text, wxS( "R1" ) );
2929 BOOST_CHECK_EQUAL( placement.fields[0].position.x, 600 );
2930 BOOST_CHECK_EQUAL( placement.fields[0].position.y, 200 );
2931 BOOST_CHECK_EQUAL( placement.fields[0].presentation.font.text, wxS( "Verdana" ) );
2932 BOOST_CHECK_EQUAL( placement.fields[1].name.text, wxS( "PART-TYPE" ) );
2933 BOOST_CHECK_EQUAL( placement.fields[1].value.text, wxS( "RES-RESN1" ) );
2934 BOOST_CHECK_EQUAL( placement.fields[1].position.x, 620 );
2935 BOOST_CHECK_EQUAL( placement.fields[1].position.y, 400 );
2936 BOOST_CHECK_EQUAL( placement.fields[1].angle, 900 );
2937 BOOST_CHECK_EQUAL( placement.fields[1].presentation.height, 388 );
2938 BOOST_CHECK_EQUAL( placement.fields[1].presentation.width, 20 );
2939 BOOST_CHECK_EQUAL( placement.fields[1].presentation.font.text, wxS( "Bold Verdana" ) );
2940 BOOST_CHECK_EQUAL( placement.fields[2].name.text, wxS( "userfield" ) );
2941 BOOST_CHECK_EQUAL( placement.fields[2].value.text, wxS( "override-value" ) );
2942 BOOST_CHECK_EQUAL( placement.fields[2].position.x, 600 );
2943 BOOST_CHECK_EQUAL( placement.fields[2].position.y, -400 );
2944 BOOST_CHECK_EQUAL( placement.fields[2].presentation.height, 388 );
2945 BOOST_CHECK_EQUAL( placement.fields[2].presentation.width, 20 );
2946 BOOST_CHECK_EQUAL( placement.fields[2].presentation.visible, false );
2947 BOOST_CHECK_EQUAL( placement.fields[2].presentation.font.text, wxS( "Bold Verdana" ) );
2948 BOOST_CHECK_EQUAL( placement.fields[2].source.controller, 17 );
2949 BOOST_CHECK_EQUAL( propertyValue( placement.fields[2].properties, wxS( "component_attribute_index" ) ),
2950 wxS( "10" ) );
2951 auto attributeIndex = std::ranges::find_if( placement.fields[2].properties,
2952 []( const SOURCE_PROPERTY& aProperty )
2953 {
2954 return aProperty.name.text == wxS( "component_attribute_index" );
2955 } );
2956 BOOST_REQUIRE( attributeIndex != placement.fields[2].properties.end() );
2957 BOOST_CHECK_EQUAL( attributeIndex->source.controller, 17 );
2958 BOOST_CHECK_EQUAL( attributeIndex->source.absoluteOffset, placement.fields[2].source.absoluteOffset + 16 );
2959 BOOST_CHECK_EQUAL( attributeIndex->source.length, 2 );
2960
2961 for( size_t i = 0; i < placement.fields.size(); ++i )
2962 {
2963 BOOST_CHECK_EQUAL( placement.fields[i].id.Value(),
2964 MakeFieldId( FIELD_ID_DOMAIN::PLACEMENT, placement.id.Value(), i ).Value() );
2965 }
2966}
2967
2968
2969BOOST_AUTO_TEST_CASE( PlacementHandleErrors )
2970{
2973 parser.Parse( loadBinaryFixture( "placement_transform.sch" ), wxS( "duplicate-placement.sch" ) );
2974 duplicate.placements[1].id = duplicate.placements[0].id;
2975 BOOST_CHECK_EXCEPTION( duplicate.ValidateOrThrow(), IO_ERROR,
2976 []( const IO_ERROR& aError )
2977 {
2978 return aError.What().Contains( wxS( "duplicate placement ID" ) )
2979 && aError.What().Contains( wxS( "controller 15" ) )
2980 && aError.What().Contains( wxS( "first at" ) );
2981 } );
2982
2983 std::vector<uint8_t> unresolvedPart = loadBinaryFixture( "placement_transform.sch" );
2984 writeU16( unresolvedPart, sheetControllerOffset( unresolvedPart, 15 ) + 0x42, 20 );
2985 BOOST_CHECK_EXCEPTION( parser.Parse( unresolvedPart, wxS( "placement-part.sch" ) ), IO_ERROR,
2986 []( const IO_ERROR& aError )
2987 {
2988 return aError.What().Contains( wxS( "targets definition object class" ) )
2989 && aError.What().Contains( wxS( "controller 15" ) );
2990 } );
2991
2992 std::vector<uint8_t> wrongGroup = loadBinaryFixture( "placement_transform.sch" );
2993 writeU32( wrongGroup, sheetControllerOffset( wrongGroup, 15 ) + 0x1C, 0xFFFFFFFF );
2994 BOOST_CHECK_EXCEPTION( parser.Parse( wrongGroup, wxS( "placement-group.sch" ) ), IO_ERROR,
2995 []( const IO_ERROR& aError )
2996 {
2997 return aError.What().Contains( wxS( "component-group handle" ) );
2998 } );
2999
3000 std::vector<uint8_t> wrongDecal = loadBinaryFixture( "placement_transform.sch" );
3001 writeU16( wrongDecal, sheetControllerOffset( wrongDecal, 15 ) + 0x44, 0 );
3002 BOOST_CHECK_EXCEPTION( parser.Parse( wrongDecal, wxS( "placement-decal.sch" ) ), IO_ERROR,
3003 []( const IO_ERROR& aError )
3004 {
3005 return aError.What().Contains( wxS( "different object classes" ) );
3006 } );
3007
3008 std::vector<uint8_t> wrongGate = loadBinaryFixture( "multigate.sch" );
3009 writeU16( wrongGate, sheetControllerOffset( wrongGate, 15 ) + 136 + 0x4A, 7 );
3010 BOOST_CHECK_EXCEPTION( parser.Parse( wrongGate, wxS( "placement-gate.sch" ) ), IO_ERROR,
3011 []( const IO_ERROR& aError )
3012 {
3013 return aError.What().Contains( wxS( "gate handle targets definition object class" ) );
3014 } );
3015
3016 std::vector<uint8_t> wrongPin = loadBinaryFixture( "placement_transform.sch" );
3017 writeU16( wrongPin, sheetControllerOffset( wrongPin, 16 ) + 4, 0xFFFF );
3018 BOOST_CHECK_EXCEPTION( parser.Parse( wrongPin, wxS( "placement-pin.sch" ) ), IO_ERROR,
3019 []( const IO_ERROR& aError )
3020 {
3021 return aError.What().Contains( wxS( "placed-pin handle" ) );
3022 } );
3023
3024 std::vector<uint8_t> wrongField = loadBinaryFixture( "fields.sch" );
3025 BOOST_REQUIRE_GT( readU32( wrongField, 0x20 + 7 * 28 + 8 ), 10 );
3026 BOOST_REQUIRE_LE( readU32( wrongField, 0x20 + 19 * 28 + 8 ), 10 );
3027 writeU16( wrongField, sheetControllerOffset( wrongField, 17 ), 10 );
3028 BOOST_CHECK_EXCEPTION( parser.Parse( wrongField, wxS( "placement-field.sch" ) ), IO_ERROR,
3029 []( const IO_ERROR& aError )
3030 {
3031 return aError.What().Contains( wxS( "placement font handle" ) );
3032 } );
3033
3034 std::vector<uint8_t> wrongAttribute = loadBinaryFixture( "fields.sch" );
3035 writeU16( wrongAttribute, sheetControllerOffset( wrongAttribute, 17 ) + 16, 0xFFFE );
3036 BOOST_CHECK_EXCEPTION( parser.Parse( wrongAttribute, wxS( "placement-field-attribute.sch" ) ), IO_ERROR,
3037 []( const IO_ERROR& aError )
3038 {
3039 return aError.What().Contains( wxS( "attribute index leaves component group" ) )
3040 && aError.What().Contains( wxS( "controller 17" ) );
3041 } );
3042
3043 PADS_SCH_MODEL wrongSheet =
3044 parser.Parse( loadBinaryFixture( "placement_transform.sch" ), wxS( "placement-sheet.sch" ) );
3045 const uint32_t wrongClassSheet = wrongSheet.definitions.back().id.Value();
3046 BOOST_REQUIRE( std::ranges::none_of( wrongSheet.sheets,
3047 [&]( const MODEL_SHEET& aSheet )
3048 {
3049 return aSheet.id.Value() == wrongClassSheet;
3050 } ) );
3051 wrongSheet.placements[0].sheet = { SHEET_ID( wrongClassSheet ), wrongSheet.placements[0].source };
3052 BOOST_CHECK_EXCEPTION( wrongSheet.ValidateOrThrow(), IO_ERROR,
3053 []( const IO_ERROR& aError )
3054 {
3055 return aError.What().Contains( wxS( "unresolved placement sheet reference" ) );
3056 } );
3057
3058 std::vector<uint8_t> unknownTransform = loadBinaryFixture( "placement_transform.sch" );
3059 writeU16( unknownTransform, sheetControllerOffset( unknownTransform, 15 ) + 0x24, 123 );
3060 writeU16( unknownTransform, sheetControllerOffset( unknownTransform, 15 ) + 0x26, 9 );
3061 PADS_SCH_MODEL unknown = parser.Parse( unknownTransform, wxS( "placement-transform-enum.sch" ) );
3062 BOOST_CHECK_EQUAL( propertyValue( unknown.placements[0].properties, wxS( "raw_angle" ) ), wxS( "123" ) );
3063 BOOST_CHECK_EQUAL( propertyValue( unknown.placements[0].properties, wxS( "raw_mirror" ) ), wxS( "9" ) );
3064 BOOST_CHECK_EQUAL( std::ranges::count_if( unknown.diagnostics,
3065 []( const PARSER_DIAGNOSTIC& aDiagnostic )
3066 {
3067 return aDiagnostic.message.Contains(
3068 wxS( "unknown placement transform" ) );
3069 } ),
3070 2 );
3071
3072 std::vector<uint8_t> boundaryTransform = loadBinaryFixture( "placement_transform.sch" );
3073 writeU16( boundaryTransform, sheetControllerOffset( boundaryTransform, 15 ) + 0x24, 3600 );
3074 writeU16( boundaryTransform, sheetControllerOffset( boundaryTransform, 15 ) + 0x26, 1 );
3075 PADS_SCH_MODEL boundary = parser.Parse( boundaryTransform, wxS( "placement-transform-boundary.sch" ) );
3076 BOOST_CHECK_EQUAL( boundary.placements[0].angle, 0 );
3077 BOOST_CHECK_EQUAL( boundary.placements[0].mirrored, true );
3078 BOOST_CHECK_EQUAL( propertyValue( boundary.placements[0].properties, wxS( "raw_angle" ) ), wxS( "3600" ) );
3079 BOOST_CHECK_EQUAL( propertyValue( boundary.placements[0].properties, wxS( "raw_mirror" ) ), wxS( "1" ) );
3080 BOOST_CHECK_EQUAL( std::ranges::count_if( boundary.diagnostics,
3081 []( const PARSER_DIAGNOSTIC& aDiagnostic )
3082 {
3083 return aDiagnostic.message.Contains(
3084 wxS( "unknown placement transform" ) );
3085 } ),
3086 1 );
3087}
3088
3089
3090BOOST_AUTO_TEST_CASE( PlacementTransformOptions )
3091{
3093 PADS_SCH_MODEL model = parser.Parse( loadBinaryFixture( "placement_transform_options.sch" ),
3094 wxS( "placement_transform_options.sch" ) );
3095
3096 BOOST_REQUIRE_EQUAL( model.placements.size(), 8 );
3097
3098 const std::array<int, 4> angles = { 0, 900, 1800, 2700 };
3099
3100 for( size_t i = 0; i < model.placements.size(); ++i )
3101 {
3102 const MODEL_PLACEMENT& placement = model.placements[i];
3103 const uint16_t expectedMirror = i / angles.size();
3104
3105 BOOST_CHECK_EQUAL( placement.reference.text, wxString::Format( wxS( "R%zu" ), i + 1 ) );
3106 BOOST_CHECK_EQUAL( placement.angle, angles[i % angles.size()] );
3107 BOOST_CHECK_EQUAL( placement.mirrorFlags, expectedMirror );
3108 BOOST_CHECK_EQUAL( placement.mirrored, expectedMirror != 0 );
3109 BOOST_CHECK_EQUAL( propertyValue( placement.properties, wxS( "raw_angle" ) ),
3110 wxString::Format( wxS( "%d" ), angles[i % angles.size()] ) );
3111 BOOST_CHECK_EQUAL( propertyValue( placement.properties, wxS( "raw_mirror" ) ),
3112 wxString::Format( wxS( "%u" ), expectedMirror ) );
3113 }
3114}
3115
3116
3117BOOST_AUTO_TEST_CASE( PlacementSemanticSnapshot )
3118{
3119 PADS_SCH_BINARY_PARSER binaryParser;
3120 const std::array<std::string, 7> fixtures = {
3121 "placement_transform", "placement_transform_options", "fields", "connectors",
3122 "multigate", "field_justification", "pin_justification"
3123 };
3124
3125 for( const std::string& fixture : fixtures )
3126 {
3127 PADS_SCH_MODEL binary =
3128 binaryParser.Parse( loadBinaryFixture( fixture + ".sch" ), wxString::FromUTF8( fixture + ".sch" ) );
3130 BOOST_REQUIRE( ascii.Parse( KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/binary/" + fixture + ".txt" ) );
3131 BOOST_REQUIRE_EQUAL( binary.placements.size(), ascii.GetPartPlacements().size() );
3132
3133 for( size_t placementIndex = 0; placementIndex < binary.placements.size(); ++placementIndex )
3134 {
3135 const MODEL_PLACEMENT& binaryPlacement = binary.placements[placementIndex];
3136 const PADS_SCH::PART_PLACEMENT& asciiPlacement = ascii.GetPartPlacements()[placementIndex];
3137 BOOST_CHECK_EQUAL( binaryPlacement.reference.text, wxString::FromUTF8( asciiPlacement.reference ) );
3138 BOOST_CHECK_EQUAL( binaryPlacement.position.x, asciiPlacement.position.x * 2 );
3139 BOOST_CHECK_EQUAL( binaryPlacement.position.y, asciiPlacement.position.y * 2 );
3140 BOOST_CHECK_EQUAL( binaryPlacement.angle, std::lround( asciiPlacement.rotation * 10 ) );
3141 BOOST_CHECK_EQUAL( binaryPlacement.mirrorFlags, asciiPlacement.mirror_flags );
3142 BOOST_CHECK_EQUAL( binaryPlacement.sheet.id.Value(), binary.sheets[0].id.Value() );
3143 auto part = std::ranges::find_if( binary.partTypes,
3144 [&]( const MODEL_PART_TYPE& aPart )
3145 {
3146 return aPart.id == binaryPlacement.partType.id;
3147 } );
3148 BOOST_REQUIRE( part != binary.partTypes.end() );
3149 BOOST_CHECK_EQUAL( part->name.text, wxString::FromUTF8( asciiPlacement.part_type ) );
3150 BOOST_REQUIRE( binaryPlacement.gate.has_value() );
3151 auto gate = std::ranges::find_if( part->gates,
3152 [&]( const MODEL_GATE& aGate )
3153 {
3154 return aGate.id == binaryPlacement.gate->id;
3155 } );
3156 BOOST_REQUIRE( gate != part->gates.end() );
3157 BOOST_CHECK_EQUAL( binaryPlacement.unit, static_cast<uint32_t>( asciiPlacement.gate_index + 1 ) );
3158 auto definition = std::ranges::find_if( binary.definitions,
3159 [&]( const MODEL_SYMBOL_DEFINITION& aDefinition )
3160 {
3161 return aDefinition.id == binaryPlacement.definition.id;
3162 } );
3163 BOOST_REQUIRE( definition != binary.definitions.end() );
3164 BOOST_CHECK_EQUAL( definition->name.text, wxString::FromUTF8( asciiPlacement.decal_name ) );
3165 BOOST_CHECK( !propertyValue( binaryPlacement.properties, wxS( "decal_handle" ) ).empty() );
3166 BOOST_REQUIRE_GE( binaryPlacement.fields.size(), asciiPlacement.attributes.size() );
3167
3168 if( !asciiPlacement.pin_overrides.empty() )
3169 {
3170 BOOST_REQUIRE_EQUAL( binaryPlacement.pins.size(), asciiPlacement.pin_overrides.size() );
3171
3172 for( size_t pinIndex = 0; pinIndex < binaryPlacement.pins.size(); ++pinIndex )
3173 {
3174 BOOST_CHECK_EQUAL( binaryPlacement.pins[pinIndex].numberAngle,
3175 asciiPlacement.pin_overrides[pinIndex].angle * 10 );
3176 BOOST_CHECK_EQUAL( binaryPlacement.pins[pinIndex].numberJustification,
3177 asciiPlacement.pin_overrides[pinIndex].justification );
3178 }
3179 }
3180
3181 for( size_t fieldIndex = 0; fieldIndex < asciiPlacement.attributes.size(); ++fieldIndex )
3182 {
3183 const MODEL_FIELD& binaryField = binaryPlacement.fields[fieldIndex];
3184 const PADS_SCH::PART_ATTRIBUTE& asciiField = asciiPlacement.attributes[fieldIndex];
3185 BOOST_CHECK_EQUAL( binaryField.name.text, wxString::FromUTF8( asciiField.name ) );
3186 const wxString asciiValue = fieldIndex == 0 ? wxString::FromUTF8( asciiPlacement.reference )
3187 : fieldIndex == 1 ? wxString::FromUTF8( asciiPlacement.part_type )
3188 : wxString::FromUTF8( asciiField.value );
3189 BOOST_CHECK_EQUAL( binaryField.value.text, asciiValue );
3190 BOOST_CHECK_EQUAL( binaryField.position.x, asciiField.position.x * 2 );
3191 BOOST_CHECK_EQUAL( binaryField.position.y, asciiField.position.y * 2 );
3192 BOOST_CHECK_EQUAL( binaryField.angle, std::lround( asciiField.rotation * 10 ) );
3193 BOOST_CHECK_EQUAL( binaryField.presentation.height, asciiField.height * 2 );
3194 BOOST_CHECK_EQUAL( binaryField.presentation.width, asciiField.width * 2 );
3195 const bool expectedVisible = fieldIndex == 0 ? binaryPlacement.referenceVisible
3196 : fieldIndex == 1 ? binaryPlacement.partTypeVisible
3197 : asciiField.visible;
3198 BOOST_CHECK_EQUAL( binaryField.presentation.visible, expectedVisible );
3199 // BOOST_CHECK_EQUAL( binaryField.presentation.font.text, wxString::FromUTF8( asciiField.font_name ) );
3200 BOOST_CHECK_EQUAL( binaryField.presentation.bold,
3201 wxString::FromUTF8( asciiField.font_name ).StartsWith( wxS( "Bold " ) ) );
3203 wxString::FromUTF8( asciiField.font_name ).StartsWith( wxS( "Italic " ) ) );
3204
3205 BOOST_CHECK( canonicalJustification( binaryField.presentation.horizontalJustification ).value
3206 == canonicalHorizontalJustification( asciiField.justification ).value );
3207 BOOST_CHECK( canonicalVerticalJustification( binaryField.presentation.verticalJustification ).value
3208 == canonicalVerticalJustification( asciiField.justification ).value );
3209
3210 if( fieldIndex >= 2 )
3211 {
3212 BOOST_CHECK_EQUAL( propertyValue( binaryField.properties, wxS( "display_flags" ) ),
3213 wxString::Format( wxS( "%d" ), asciiField.visibility ) );
3214 const wxString expectedAttributeIndex = fixture == "fields" || fixture == "field_justification"
3215 || fixture == "placement_transform_options"
3216 ? wxS( "10" )
3217 : wxS( "65535" );
3218 BOOST_CHECK_EQUAL( propertyValue( binaryField.properties, wxS( "component_attribute_index" ) ),
3219 expectedAttributeIndex );
3220 }
3221 }
3222 }
3223 }
3224}
3225
3226
3228{
3231 parser.Parse( loadBinaryFixture( "connectivity_topology.sch" ), wxS( "connectivity_topology.sch" ) );
3232
3233 BOOST_REQUIRE_EQUAL( model.nets.size(), 9 );
3234 BOOST_CHECK_EQUAL( itemNamed( model.nets, wxS( "CROSS_H" ) ).connections.size(), 1 );
3235 BOOST_CHECK_EQUAL( itemNamed( model.nets, wxS( "CROSS_V" ) ).connections.size(), 1 );
3236 BOOST_REQUIRE_EQUAL( itemNamed( model.nets, wxS( "T_NET" ) ).connections.size(), 3 );
3237
3238 const MODEL_CONNECTION& horizontal = itemNamed( model.nets, wxS( "CROSS_H" ) ).connections.front();
3239 BOOST_REQUIRE_EQUAL( horizontal.vertices.size(), 2 );
3240 BOOST_REQUIRE_EQUAL( horizontal.endpoints.size(), 2 );
3241 BOOST_CHECK_EQUAL( horizontal.vertices[0].x, 6000 );
3242 BOOST_CHECK_EQUAL( horizontal.vertices[0].y, 6000 );
3243 BOOST_CHECK_EQUAL( horizontal.vertices[1].x, 14000 );
3244 BOOST_CHECK_EQUAL( horizontal.vertices[1].y, 6000 );
3245 BOOST_CHECK( horizontal.endpoints[0].kind == MODEL_ENDPOINT_KIND::POINT );
3246 BOOST_CHECK( horizontal.endpoints[1].kind == MODEL_ENDPOINT_KIND::POINT );
3247
3248 PADS_SCH_MODEL minimal = parser.Parse( loadMinimalV13(), wxS( "minimal_v13.sch" ) );
3249 BOOST_REQUIRE_EQUAL( minimal.nets.size(), 1 );
3250 BOOST_REQUIRE_EQUAL( minimal.nets[0].connections.size(), 1 );
3251 BOOST_CHECK( minimal.nets[0].connections[0].endpoints[0].kind == MODEL_ENDPOINT_KIND::PIN );
3252 BOOST_REQUIRE( minimal.nets[0].connections[0].endpoints[0].placement );
3253 BOOST_REQUIRE( minimal.nets[0].connections[0].endpoints[0].pin );
3254 BOOST_CHECK_EQUAL( minimal.nets[0].connections[0].endpoints[0].placement->id.Value(),
3255 minimal.placements[0].id.Value() );
3256 BOOST_CHECK_EQUAL( minimal.nets[0].connections[0].endpoints[0].pin->id.Value(),
3257 minimal.placements[0].pins[0].id.Value() );
3258}
3259
3260
3261BOOST_AUTO_TEST_CASE( LabelsAndPower )
3262{
3265 parser.Parse( loadBinaryFixture( "connectivity_topology.sch" ), wxS( "connectivity_topology.sch" ) );
3266
3267 BOOST_REQUIRE_EQUAL( model.labels.size(), 16 );
3268 BOOST_CHECK_EQUAL( std::ranges::count( model.labels, MODEL_LABEL_KIND::LOCAL, &MODEL_LABEL::kind ), 13 );
3269 BOOST_CHECK_EQUAL( std::ranges::count( model.labels, MODEL_LABEL_KIND::GROUND, &MODEL_LABEL::kind ), 1 );
3270 BOOST_CHECK_EQUAL( std::ranges::count( model.labels, MODEL_LABEL_KIND::POWER, &MODEL_LABEL::kind ), 1 );
3271 BOOST_CHECK_EQUAL( std::ranges::count( model.labels, MODEL_LABEL_KIND::GLOBAL, &MODEL_LABEL::kind ), 1 );
3272
3273 auto global = std::ranges::find_if( model.labels,
3274 []( const MODEL_LABEL& aLabel )
3275 {
3276 return aLabel.kind == MODEL_LABEL_KIND::GLOBAL;
3277 } );
3278 BOOST_REQUIRE( global != model.labels.end() );
3279 BOOST_CHECK_EQUAL( global->text.text, wxS( "GLOBAL_LINK" ) );
3280 BOOST_CHECK_EQUAL( global->position.x, 16000 );
3281 BOOST_CHECK_EQUAL( global->position.y, 12000 );
3282 BOOST_CHECK_EQUAL( global->angle, 0 );
3283
3284 PADS_SCH_MODEL multisheet =
3285 parser.Parse( loadBinaryFixture( "multisheet_connectivity.sch" ), wxS( "multisheet_connectivity.sch" ) );
3286 BOOST_CHECK_EQUAL( std::ranges::count_if( multisheet.labels,
3287 []( const MODEL_LABEL& aLabel )
3288 {
3289 return aLabel.kind == MODEL_LABEL_KIND::GLOBAL
3290 && aLabel.text.text == wxS( "CROSS_SHEET" );
3291 } ),
3292 2 );
3293 std::vector<const MODEL_LABEL*> crossSheetLabels;
3294
3295 for( const MODEL_LABEL& label : multisheet.labels )
3296 {
3297 if( label.kind == MODEL_LABEL_KIND::GLOBAL && label.text.text == wxS( "CROSS_SHEET" ) )
3298 crossSheetLabels.push_back( &label );
3299 }
3300
3301 BOOST_REQUIRE_EQUAL( crossSheetLabels.size(), 2 );
3302 std::ranges::sort( crossSheetLabels,
3303 []( const MODEL_LABEL* aLeft, const MODEL_LABEL* aRight )
3304 {
3305 return aLeft->source.sheet < aRight->source.sheet;
3306 } );
3307 BOOST_REQUIRE_EQUAL( crossSheetLabels[0]->linkedSheets.size(), 1 );
3308 BOOST_REQUIRE_EQUAL( crossSheetLabels[1]->linkedSheets.size(), 1 );
3309 BOOST_CHECK_EQUAL( crossSheetLabels[0]->source.sheet, 0 );
3310 BOOST_CHECK_EQUAL( crossSheetLabels[1]->source.sheet, 1 );
3311 BOOST_CHECK_EQUAL( crossSheetLabels[0]->linkedSheets[0].id.Value(), multisheet.sheets[1].id.Value() );
3312 BOOST_CHECK_EQUAL( crossSheetLabels[1]->linkedSheets[0].id.Value(), multisheet.sheets[0].id.Value() );
3313}
3314
3315
3317{
3320 parser.Parse( loadBinaryFixture( "connectivity_topology.sch" ), wxS( "connectivity_topology.sch" ) );
3321
3322 BOOST_REQUIRE_EQUAL( model.buses.size(), 1 );
3323 const MODEL_BUS& bus = model.buses.front();
3324 BOOST_CHECK_EQUAL( bus.name.text, wxS( "BATCHC_BUS_ALIAS" ) );
3325 BOOST_REQUIRE_EQUAL( bus.vertices.size(), 3 );
3326 BOOST_CHECK_EQUAL( bus.vertices[0].x, 6000 );
3327 BOOST_CHECK_EQUAL( bus.vertices[0].y, 14000 );
3328 BOOST_CHECK_EQUAL( bus.vertices[2].x, 14000 );
3329 BOOST_CHECK_EQUAL( bus.vertices[2].y, 10000 );
3330 BOOST_REQUIRE_EQUAL( bus.entries.size(), 3 );
3331 BOOST_REQUIRE_EQUAL( bus.aliases.size(), 1 );
3332 BOOST_REQUIRE_EQUAL( bus.memberNets.size(), 3 );
3333 BOOST_CHECK_EQUAL( bus.aliases[0].text, wxS( "BATCHC_BUS_ALIAS" ) );
3334 BOOST_CHECK_EQUAL( bus.entries[0].position.x, 8000 );
3335 BOOST_CHECK_EQUAL( bus.entries[1].position.x, 10000 );
3336 BOOST_CHECK_EQUAL( bus.entries[2].position.x, 12000 );
3337 BOOST_CHECK_EQUAL( itemNamed( model.nets, wxS( "BUS0" ) ).id.Value(), bus.memberNets[0].id.Value() );
3338 BOOST_CHECK_EQUAL( itemNamed( model.nets, wxS( "BUS1" ) ).id.Value(), bus.memberNets[1].id.Value() );
3339 BOOST_CHECK_EQUAL( itemNamed( model.nets, wxS( "BUS2" ) ).id.Value(), bus.memberNets[2].id.Value() );
3340}
3341
3342
3344{
3346 PADS_SCH_MODEL model = parser.Parse( loadBinaryFixture( "page_graphics.sch" ), wxS( "page_graphics.sch" ) );
3347
3348 BOOST_REQUIRE_EQUAL( model.sheets.size(), 1 );
3349 BOOST_REQUIRE_EQUAL( model.worksheets.size(), 1u );
3350 BOOST_REQUIRE_EQUAL( model.worksheets[0].graphics.size(), 86u );
3351 BOOST_REQUIRE_EQUAL( model.graphics.size(), 6u );
3352 BOOST_CHECK_EQUAL( std::ranges::count_if( model.worksheets[0].graphics,
3353 []( const MODEL_GRAPHIC& aGraphic )
3354 {
3355 return aGraphic.kind == MODEL_GRAPHIC_KIND::LINE;
3356 } ),
3357 31 );
3358 BOOST_CHECK_GE( std::ranges::count_if( model.worksheets[0].graphics,
3359 []( const MODEL_GRAPHIC& aGraphic )
3360 {
3361 return aGraphic.kind == MODEL_GRAPHIC_KIND::POLYLINE;
3362 } ),
3363 1 );
3364 BOOST_CHECK_EQUAL( std::ranges::count_if( model.worksheets[0].graphics,
3365 []( const MODEL_GRAPHIC& aGraphic )
3366 {
3367 return aGraphic.kind == MODEL_GRAPHIC_KIND::TEXT;
3368 } ),
3369 52 );
3370 auto titleText = std::ranges::find_if( model.worksheets[0].graphics,
3371 []( const MODEL_GRAPHIC& aGraphic )
3372 {
3373 return aGraphic.kind == MODEL_GRAPHIC_KIND::TEXT
3374 && aGraphic.text.text == wxS( "TITLE:" );
3375 } );
3376 BOOST_REQUIRE( titleText != model.worksheets[0].graphics.end() );
3377 BOOST_CHECK_EQUAL( titleText->presentation.height, 200 );
3378 BOOST_CHECK_EQUAL( titleText->presentation.width, 20 );
3379 auto titleGroup = std::ranges::find_if( titleText->properties,
3380 []( const SOURCE_PROPERTY& aProperty )
3381 {
3382 return aProperty.name.text == wxS( "page_graphic_group" );
3383 } );
3384 BOOST_REQUIRE( titleGroup != titleText->properties.end() );
3385 const uint16_t titleTextCount =
3386 readU16( loadBinaryFixture( "page_graphics.sch" ), titleGroup->source.absoluteOffset + 64 );
3387 BOOST_REQUIRE_GT( titleTextCount, 1u );
3388
3389 std::vector<uint8_t> repeatedPredecessor = loadBinaryFixture( "page_graphics.sch" );
3390 const uint16_t lastTextRecord = readU16( repeatedPredecessor, titleGroup->source.absoluteOffset + 66 );
3391 writeU16( repeatedPredecessor, sheetControllerOffset( repeatedPredecessor, 1 ) + lastTextRecord * 32 + 24,
3392 lastTextRecord );
3393 BOOST_CHECK_EXCEPTION( parser.Parse( repeatedPredecessor, wxS( "page-text-cycle.sch" ) ), IO_ERROR,
3394 []( const IO_ERROR& aError )
3395 {
3396 return aError.What().Contains(
3397 wxS( "page-text predecessor repeats controller 1 record" ) );
3398 } );
3399
3400 std::vector<uint8_t> noncontiguousPredecessor = loadBinaryFixture( "page_graphics.sch" );
3401 const size_t textController = sheetControllerOffset( noncontiguousPredecessor, 1 );
3402 const uint16_t predecessor1 = readU16( noncontiguousPredecessor, textController + lastTextRecord * 32 + 24 );
3403 const uint16_t predecessor2 = readU16( noncontiguousPredecessor, textController + predecessor1 * 32 + 24 );
3404 const uint16_t predecessor3 = readU16( noncontiguousPredecessor, textController + predecessor2 * 32 + 24 );
3405 writeU16( noncontiguousPredecessor, textController + lastTextRecord * 32 + 24, predecessor2 );
3406 writeU16( noncontiguousPredecessor, textController + predecessor2 * 32 + 24, predecessor1 );
3407 writeU16( noncontiguousPredecessor, textController + predecessor1 * 32 + 24, predecessor3 );
3408 PADS_SCH_MODEL noncontiguous = parser.Parse( noncontiguousPredecessor, wxS( "page-text-noncontiguous.sch" ) );
3409 std::vector<size_t> recoveredRecords;
3410
3411 const MODEL_WORKSHEET& noncontiguousWorksheet = itemNamed( noncontiguous.worksheets, wxS( "SIZEB" ) );
3412
3413 for( const MODEL_GRAPHIC& graphic : noncontiguousWorksheet.graphics )
3414 {
3415 if( graphic.kind == MODEL_GRAPHIC_KIND::TEXT
3416 && propertyValue( graphic.properties, wxS( "page_graphic_group" ) ) == titleGroup->value.text )
3417 {
3418 recoveredRecords.push_back( graphic.source.recordIndex );
3419 }
3420 }
3421
3422 BOOST_REQUIRE_EQUAL( recoveredRecords.size(), titleTextCount );
3423 BOOST_CHECK_EQUAL( recoveredRecords[recoveredRecords.size() - 3], predecessor1 );
3424 BOOST_CHECK_EQUAL( recoveredRecords[recoveredRecords.size() - 2], predecessor2 );
3425 BOOST_CHECK_EQUAL( recoveredRecords.back(), lastTextRecord );
3426
3427 std::vector<const MODEL_PAGE_GRAPHIC*> custom;
3428
3429 for( const MODEL_PAGE_GRAPHIC& graphic : model.graphics )
3430 {
3431 if( propertyValue( graphic.graphic.properties, wxS( "page_graphic_group" ) ) == wxS( "BATCHB_PAGE_GRAPHICS" ) )
3432 {
3433 custom.push_back( &graphic );
3434 }
3435 }
3436
3437 BOOST_REQUIRE_EQUAL( custom.size(), 6 );
3438 auto customKind = [&]( MODEL_GRAPHIC_KIND aKind )
3439 {
3440 return std::ranges::find_if( custom,
3441 [&]( const MODEL_PAGE_GRAPHIC* aGraphic )
3442 {
3443 return aGraphic->graphic.kind == aKind;
3444 } );
3445 };
3446 auto line = customKind( MODEL_GRAPHIC_KIND::LINE );
3447 BOOST_REQUIRE( line != custom.end() );
3448 BOOST_REQUIRE_EQUAL( ( *line )->graphic.points.size(), 2 );
3449 BOOST_CHECK_EQUAL( ( *line )->graphic.points[0].x, 12000 );
3450 BOOST_CHECK_EQUAL( ( *line )->graphic.points[1].x, 13600 );
3451 BOOST_CHECK_EQUAL( ( *line )->graphic.strokeWidth, 20 );
3452 BOOST_CHECK( ( *line )->graphic.lineStyle == MODEL_LINE_STYLE::SOLID );
3453 std::vector<uint8_t> unprovedLineStyle = loadBinaryFixture( "page_graphics.sch" );
3454 unprovedLineStyle[( *line )->graphic.source.absoluteOffset + 1] = 2;
3455 PADS_SCH_MODEL unprovedStyleModel = parser.Parse( unprovedLineStyle, wxS( "unproved-line-style.sch" ) );
3456 auto unprovedStyleGraphic = std::ranges::find_if( unprovedStyleModel.graphics,
3457 [&]( const MODEL_PAGE_GRAPHIC& aGraphic )
3458 {
3459 return aGraphic.graphic.source.recordIndex
3460 == ( *line )->graphic.source.recordIndex;
3461 } );
3462 BOOST_REQUIRE( unprovedStyleGraphic != unprovedStyleModel.graphics.end() );
3463 BOOST_CHECK( unprovedStyleGraphic->graphic.lineStyle == MODEL_LINE_STYLE::DEFAULT );
3464 BOOST_CHECK_EQUAL( std::ranges::count_if( unprovedStyleModel.diagnostics,
3465 []( const PARSER_DIAGNOSTIC& aDiagnostic )
3466 {
3467 return aDiagnostic.message.Contains(
3468 wxS( "page graphic line style 2" ) );
3469 } ),
3470 1 );
3471 auto circle = customKind( MODEL_GRAPHIC_KIND::CIRCLE );
3472 BOOST_REQUIRE( circle != custom.end() );
3473 BOOST_CHECK_EQUAL( ( *circle )->graphic.points[0].x, 15200 );
3474 BOOST_CHECK_EQUAL( ( *circle )->graphic.points[1].y, 12800 );
3475 auto arc = customKind( MODEL_GRAPHIC_KIND::ARC );
3476 BOOST_REQUIRE( arc != custom.end() );
3477 BOOST_CHECK_EQUAL( ( *arc )->graphic.arcSweepAngle, 1800 );
3478 BOOST_CHECK( ( *arc )->graphic.arcClockwise );
3479 BOOST_CHECK_EQUAL( ( *arc )->graphic.arcCenter.x, 16800 );
3480 BOOST_CHECK_EQUAL( ( *arc )->graphic.arcCenter.y, 12400 );
3481 auto text = customKind( MODEL_GRAPHIC_KIND::TEXT );
3482 BOOST_REQUIRE( text != custom.end() );
3483 BOOST_CHECK_EQUAL( ( *text )->graphic.text.text, wxS( "EMBEDDED PAGE TEXT" ) );
3484 BOOST_CHECK_EQUAL( ( *text )->graphic.points[0].y, 13200 );
3485 BOOST_CHECK_EQUAL( ( *text )->graphic.presentation.font.text, wxS( "Verdana" ) );
3486 BOOST_CHECK_EQUAL( ( *text )->graphic.presentation.height, 300 );
3487 BOOST_CHECK_EQUAL( ( *text )->graphic.presentation.width, 20 );
3489 BOOST_REQUIRE( ascii.Parse( KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/binary/page_graphics.txt" ) );
3490 auto pageRecords = []( std::vector<CANONICAL_SEMANTIC_RECORD> aRecords )
3491 {
3492 std::erase_if( aRecords,
3493 []( const CANONICAL_SEMANTIC_RECORD& aRecord )
3494 {
3495 auto group = aRecord.properties.find( "page_graphic_group" );
3496 return aRecord.kind != CANONICAL_KIND::GRAPHIC || group == aRecord.properties.end()
3497 || group->second.value != CANONICAL_VALUE( std::string( "BATCHB_PAGE_GRAPHICS" ) );
3498 } );
3499 return aRecords;
3500 };
3501 auto expectedPage = pageRecords( normalizeAsciiModel( ascii ) );
3502 auto actualPage = pageRecords( normalizeBinaryModel( model ) );
3503 BOOST_CHECK( snapshotsMatch( expectedPage, actualPage,
3504 { { "preserved_graphic_presentation", PROPERTY_DISPOSITION::PRESERVED } } ) );
3505
3506 std::vector<uint8_t> privateStroke = loadBinaryFixture( "page_graphics.sch" );
3507 const size_t privatePiece = sheetControllerOffset( privateStroke, 4 ) + 81 * 6;
3508 writeU16( privateStroke, privatePiece + 4, 0xEF03 );
3509 PADS_SCH_MODEL preservedStroke = parser.Parse( privateStroke, wxS( "private-stroke.sch" ) );
3510 auto privateGraphic = std::ranges::find_if( preservedStroke.graphics,
3511 []( const MODEL_PAGE_GRAPHIC& aGraphic )
3512 {
3513 return aGraphic.graphic.source.controller == 4
3514 && aGraphic.graphic.source.recordIndex == 81;
3515 } );
3516 BOOST_REQUIRE( privateGraphic != preservedStroke.graphics.end() );
3517 BOOST_CHECK_EQUAL( privateGraphic->graphic.strokeWidth, 0 );
3518 auto rawStroke = std::ranges::find_if( privateGraphic->graphic.properties,
3519 []( const SOURCE_PROPERTY& aProperty )
3520 {
3521 return aProperty.name.text == wxS( "unsupported_graphic_stroke_width" );
3522 } );
3523 BOOST_REQUIRE( rawStroke != privateGraphic->graphic.properties.end() );
3524 BOOST_CHECK_EQUAL( rawStroke->value.text, wxS( "3" ) );
3525 BOOST_CHECK( rawStroke->disposition == PROPERTY_DISPOSITION::UNSUPPORTED );
3526 BOOST_CHECK_EQUAL( rawStroke->source.controller, 4 );
3527 BOOST_CHECK_EQUAL( rawStroke->source.recordIndex, 81 );
3528 BOOST_CHECK_EQUAL( rawStroke->source.absoluteOffset, privatePiece + 4 );
3529 BOOST_CHECK_EQUAL( rawStroke->source.length, 1 );
3530 BOOST_CHECK_EQUAL( propertyValue( privateGraphic->graphic.properties, wxS( "preserved_graphic_presentation" ) ),
3531 wxS( "239" ) );
3532 BOOST_CHECK_EQUAL( std::ranges::count_if( preservedStroke.diagnostics,
3533 [&]( const PARSER_DIAGNOSTIC& aDiagnostic )
3534 {
3535 return aDiagnostic.source == rawStroke->source && aDiagnostic.property
3536 && aDiagnostic.property->name == rawStroke->name.text
3537 && aDiagnostic.property->disposition == rawStroke->disposition;
3538 } ),
3539 1u );
3540}
3541
3542
3543BOOST_AUTO_TEST_CASE( NativeWorksheetRecognition )
3544{
3546 const PADS_SCH_MODEL minimal = parser.Parse( loadBinaryFixture( "minimal_v13.sch" ), wxS( "minimal_v13.sch" ) );
3547
3548 BOOST_REQUIRE_EQUAL( minimal.worksheets.size(), 1u );
3549 BOOST_CHECK_EQUAL( minimal.worksheets[0].name.text, wxS( "SIZEB" ) );
3550 BOOST_CHECK_EQUAL( minimal.worksheets[0].graphics.size(), 86u );
3551 BOOST_CHECK( minimal.graphics.empty() );
3552
3553 const PADS_SCH_MODEL page = parser.Parse( loadBinaryFixture( "page_graphics.sch" ), wxS( "page_graphics.sch" ) );
3554 BOOST_REQUIRE_EQUAL( page.worksheets.size(), 1u );
3555 BOOST_CHECK_EQUAL( page.worksheets[0].name.text, wxS( "SIZEB" ) );
3556 BOOST_CHECK_EQUAL( page.worksheets[0].graphics.size(), 86u );
3557 BOOST_REQUIRE_EQUAL( page.graphics.size(), 6u );
3558 BOOST_CHECK( std::ranges::all_of( page.graphics,
3559 []( const MODEL_PAGE_GRAPHIC& aGraphic )
3560 {
3561 return propertyValue( aGraphic.graphic.properties,
3562 wxS( "page_graphic_group" ) )
3563 == wxS( "BATCHB_PAGE_GRAPHICS" );
3564 } ) );
3565
3566 std::vector<uint8_t> distinct = loadBinaryFixture( "page_graphics.sch" );
3567 const std::string duplicateName = "BATCHB_SIZEB";
3568 auto duplicateRecord = std::search( distinct.begin(), distinct.end(), duplicateName.begin(), duplicateName.end() );
3569 BOOST_REQUIRE( duplicateRecord != distinct.end() );
3570 const size_t duplicateOffset = std::distance( distinct.begin(), duplicateRecord );
3571 const size_t firstVertex = readU32( distinct, duplicateOffset + 0x34 );
3572 const size_t vertexOffset = sheetControllerOffset( distinct, 5 ) + firstVertex * 8;
3573 writeU16( distinct, vertexOffset, readU16( distinct, vertexOffset ) + 2 );
3574
3575 const PADS_SCH_MODEL distinctPage = parser.Parse( distinct, wxS( "distinct-page-graphics.sch" ) );
3576 BOOST_REQUIRE_EQUAL( distinctPage.worksheets.size(), 1u );
3577 BOOST_CHECK_EQUAL( distinctPage.worksheets[0].name.text, wxS( "SIZEB" ) );
3578 BOOST_CHECK_EQUAL( distinctPage.graphics.size(), 92u );
3579 BOOST_CHECK_EQUAL( std::ranges::count_if( distinctPage.graphics,
3580 []( const MODEL_PAGE_GRAPHIC& aGraphic )
3581 {
3582 return propertyValue( aGraphic.graphic.properties,
3583 wxS( "page_graphic_group" ) )
3584 == wxS( "BATCHB_SIZEB" );
3585 } ),
3586 86u );
3587 BOOST_CHECK_EQUAL( std::ranges::count_if( distinctPage.diagnostics,
3588 []( const PARSER_DIAGNOSTIC& aDiagnostic )
3589 {
3590 return aDiagnostic.message.Contains(
3591 wxS( "distinct worksheet layout preserved" ) );
3592 } ),
3593 1u );
3594}
3595
3596
3597BOOST_AUTO_TEST_CASE( ConnectivityHandleErrors )
3598{
3600 std::vector<uint8_t> wrongEndpoint = loadBinaryFixture( "connectivity_topology.sch" );
3601 const size_t connection = sheetControllerOffset( wrongEndpoint, 21 );
3602 writeU16( wrongEndpoint, connection + 12, 0x1000 );
3603 BOOST_CHECK_EXCEPTION( parser.Parse( wrongEndpoint, wxS( "wrong-endpoint.sch" ) ), IO_ERROR,
3604 [&]( const IO_ERROR& aError )
3605 {
3606 return aError.What().Contains( wxS( "controller 21" ) )
3607 && aError.What().Contains( wxS( "wrong endpoint object class" ) );
3608 } );
3609
3610 std::vector<uint8_t> unresolvedEndpoint = loadBinaryFixture( "connectivity_topology.sch" );
3611 writeU16( unresolvedEndpoint, connection + 12, 0x2FFF );
3612 BOOST_CHECK_EXCEPTION( parser.Parse( unresolvedEndpoint, wxS( "unresolved-endpoint.sch" ) ), IO_ERROR,
3613 [&]( const IO_ERROR& aError )
3614 {
3615 return aError.What().Contains( wxS( "controller 21" ) )
3616 && aError.What().Contains( wxS( "unresolved off-page endpoint" ) );
3617 } );
3618
3619 std::vector<uint8_t> unresolvedPlacement = loadBinaryFixture( "minimal_v13.sch" );
3620 const size_t minimalConnection = sheetControllerOffset( unresolvedPlacement, 21 );
3621 BOOST_REQUIRE_EQUAL( readU16( unresolvedPlacement, minimalConnection + 12 ), 0u );
3622 writeU16( unresolvedPlacement, minimalConnection + 12, 0x0FFF );
3623 BOOST_CHECK_EXCEPTION( parser.Parse( unresolvedPlacement, wxS( "unresolved-placement.sch" ) ), IO_ERROR,
3624 []( const IO_ERROR& aError )
3625 {
3626 return aError.What().Contains( wxS( "controller 21" ) )
3627 && aError.What().Contains( wxS( "unresolved placement endpoint" ) );
3628 } );
3629
3630 std::vector<uint8_t> duplicateMembership = loadBinaryFixture( "connectivity_topology.sch" );
3631 const size_t globalNets = outerControllerOffset( duplicateMembership, 8 );
3632 writeU32( duplicateMembership, globalNets + 2 * 88 + 4, 1 );
3633 BOOST_CHECK_EXCEPTION( parser.Parse( duplicateMembership, wxS( "duplicate-membership.sch" ) ), IO_ERROR,
3634 []( const IO_ERROR& aError )
3635 {
3636 return aError.What().Contains( wxS( "controller 8" ) )
3637 && aError.What().Contains( wxS( "duplicate net sheet-membership" ) );
3638 } );
3639
3640 std::vector<uint8_t> wrongNetClass = loadBinaryFixture( "connectivity_topology.sch" );
3641 const size_t wrongNetConnection = sheetControllerOffset( wrongNetClass, 21 );
3642 writeU32( wrongNetClass, wrongNetConnection + 8, 0 );
3643 BOOST_CHECK_EXCEPTION( parser.Parse( wrongNetClass, wxS( "wrong-net-class.sch" ) ), IO_ERROR,
3644 []( const IO_ERROR& aError )
3645 {
3646 return aError.What().Contains( wxS( "controller 21" ) )
3647 && aError.What().Contains( wxS( "wrong or unresolved object class" ) );
3648 } );
3649
3650 std::vector<uint8_t> wrongBusClass = loadBinaryFixture( "connectivity_topology.sch" );
3651 const size_t wrongBus = sheetControllerOffset( wrongBusClass, 18 );
3652 writeU32( wrongBusClass, wrongBus + 8, 1 );
3653 BOOST_CHECK_EXCEPTION( parser.Parse( wrongBusClass, wxS( "wrong-bus-class.sch" ) ), IO_ERROR,
3654 []( const IO_ERROR& aError )
3655 {
3656 return aError.What().Contains( wxS( "controller 18" ) )
3657 && aError.What().Contains( wxS( "wrong or unresolved object class" ) );
3658 } );
3659
3660 std::vector<uint8_t> wrongSheet = loadBinaryFixture( "multisheet_connectivity.sch" );
3661 const size_t sheetMembership = outerControllerOffset( wrongSheet, 4 );
3662 writeU16( wrongSheet, sheetMembership + 2, 2 );
3663 BOOST_CHECK_EXCEPTION( parser.Parse( wrongSheet, wxS( "wrong-net-sheet.sch" ) ), IO_ERROR,
3664 []( const IO_ERROR& aError )
3665 {
3666 return aError.What().Contains( wxS( "controller 8" ) )
3667 && aError.What().Contains( wxS( "wrong sheet object class" ) );
3668 } );
3669
3670 std::vector<uint8_t> busCycle = loadBinaryFixture( "connectivity_topology.sch" );
3671 const size_t bus = sheetControllerOffset( busCycle, 18 );
3672 const size_t offpage = sheetControllerOffset( busCycle, 20 );
3673 const uint16_t tailHandle = readU16( busCycle, bus + 24 );
3674 BOOST_REQUIRE_EQUAL( tailHandle >> 12, 2 );
3675 const uint16_t tailRecord = tailHandle & 0x0FFF;
3676 writeU16( busCycle, offpage + tailRecord * 32 + 4, tailHandle );
3677 BOOST_CHECK_EXCEPTION( parser.Parse( busCycle, wxS( "bus-entry-cycle.sch" ) ), IO_ERROR,
3678 []( const IO_ERROR& aError )
3679 {
3680 return aError.What().Contains( wxS( "controller 18" ) )
3681 && aError.What().Contains( wxS( "cyclic bus-entry" ) );
3682 } );
3683
3684 std::vector<uint8_t> unresolvedLabelConnection = loadBinaryFixture( "connectivity_topology.sch" );
3685 const size_t label = sheetControllerOffset( unresolvedLabelConnection, 20 );
3686 writeU16( unresolvedLabelConnection, label + 8, 0xFFFF );
3687 BOOST_CHECK_EXCEPTION( parser.Parse( unresolvedLabelConnection, wxS( "unresolved-label-connection.sch" ) ),
3688 IO_ERROR,
3689 []( const IO_ERROR& aError )
3690 {
3691 return aError.What().Contains( wxS( "controller 20" ) )
3692 && aError.What().Contains( wxS( "off-page net handle leaves controller 21" ) );
3693 } );
3694
3695 std::vector<uint8_t> wrongJunctionOwner = loadBinaryFixture( "connectivity_topology.sch" );
3696 const size_t junction = sheetControllerOffset( wrongJunctionOwner, 19 );
3697 const uint16_t junctionOwner = readU16( wrongJunctionOwner, junction + 8 );
3698 BOOST_REQUIRE_GT( sheetControllerCount( wrongJunctionOwner, 21 ), 1 );
3699 writeU16( wrongJunctionOwner, junction + 8, junctionOwner == 0 ? 1 : 0 );
3700 const wxString junctionOwnerOffset =
3701 wxString::Format( wxS( "offset 0x%llX" ), static_cast<unsigned long long>( junction + 8 ) );
3702 BOOST_CHECK_EXCEPTION( parser.Parse( wrongJunctionOwner, wxS( "wrong-junction-owner.sch" ) ), IO_ERROR,
3703 [&]( const IO_ERROR& aError )
3704 {
3705 return aError.What().Contains( wxS( "controller 19" ) )
3706 && aError.What().Contains( wxS( "does not point back" ) )
3707 && aError.What().Contains( junctionOwnerOffset );
3708 } );
3709
3710 std::vector<uint8_t> crossNetJunction = loadBinaryFixture( "connectivity_topology.sch" );
3711 const size_t crossNetJunctions = sheetControllerOffset( crossNetJunction, 19 );
3712 const size_t crossNetConnections = sheetControllerOffset( crossNetJunction, 21 );
3713 const uint16_t tiedDotHandle = 0x3000;
3714 const uint16_t tiedDotOwner = readU16( crossNetJunction, crossNetJunctions + 8 );
3715 size_t sharedConnection = std::numeric_limits<size_t>::max();
3716 uint32_t differentNet = std::numeric_limits<uint32_t>::max();
3717 const uint32_t ownerNet = readU32( crossNetJunction, crossNetConnections + tiedDotOwner * 40 + 8 );
3718
3719 for( size_t record = 0; record < sheetControllerCount( crossNetJunction, 21 ); ++record )
3720 {
3721 const size_t offset = crossNetConnections + record * 40;
3722
3723 if( record != tiedDotOwner
3724 && ( readU16( crossNetJunction, offset + 12 ) == tiedDotHandle
3725 || readU16( crossNetJunction, offset + 14 ) == tiedDotHandle ) )
3726 {
3727 sharedConnection = record;
3728 }
3729
3730 if( readU32( crossNetJunction, offset + 8 ) != ownerNet )
3731 differentNet = readU32( crossNetJunction, offset + 8 );
3732 }
3733
3734 BOOST_REQUIRE_NE( sharedConnection, std::numeric_limits<size_t>::max() );
3735 BOOST_REQUIRE_NE( differentNet, std::numeric_limits<uint32_t>::max() );
3736 writeU32( crossNetJunction, crossNetConnections + sharedConnection * 40 + 8, differentNet );
3737 const wxString crossNetOffset =
3738 wxString::Format( wxS( "offset 0x%llX" ), static_cast<unsigned long long>( crossNetJunctions + 8 ) );
3739 BOOST_CHECK_EXCEPTION( parser.Parse( crossNetJunction, wxS( "cross-net-junction.sch" ) ), IO_ERROR,
3740 [&]( const IO_ERROR& aError )
3741 {
3742 return aError.What().Contains( wxS( "controller 19" ) )
3743 && aError.What().Contains( wxS( "shared across different nets" ) )
3744 && aError.What().Contains( crossNetOffset );
3745 } );
3746
3747 std::vector<uint8_t> wrongOffpageOwner = loadBinaryFixture( "connectivity_topology.sch" );
3748 const size_t offpageOwner = sheetControllerOffset( wrongOffpageOwner, 20 );
3749 const uint16_t connectionOwner = readU16( wrongOffpageOwner, offpageOwner + 8 );
3750 writeU16( wrongOffpageOwner, offpageOwner + 8, connectionOwner == 0 ? 1 : 0 );
3751 const wxString offpageOwnerOffset =
3752 wxString::Format( wxS( "offset 0x%llX" ), static_cast<unsigned long long>( offpageOwner + 8 ) );
3753 BOOST_CHECK_EXCEPTION( parser.Parse( wrongOffpageOwner, wxS( "wrong-offpage-owner.sch" ) ), IO_ERROR,
3754 [&]( const IO_ERROR& aError )
3755 {
3756 return aError.What().Contains( wxS( "controller 20" ) )
3757 && aError.What().Contains( wxS( "does not point back" ) )
3758 && aError.What().Contains( offpageOwnerOffset );
3759 } );
3760
3761 std::vector<uint8_t> crossNetOffpage = loadBinaryFixture( "connectivity_topology.sch" );
3762 const size_t crossNetOffpages = sheetControllerOffset( crossNetOffpage, 20 );
3763 const size_t crossNetOffpageConnections = sheetControllerOffset( crossNetOffpage, 21 );
3764 const size_t crossNetVertices = sheetControllerOffset( crossNetOffpage, 22 );
3765 const uint16_t offpageConnection = readU16( crossNetOffpage, crossNetOffpages + 8 );
3766 const uint32_t offpageNet = readU32( crossNetOffpage, crossNetOffpageConnections + offpageConnection * 40 + 8 );
3767 size_t foreignConnection = std::numeric_limits<size_t>::max();
3768
3769 for( size_t record = 0; record < sheetControllerCount( crossNetOffpage, 21 ); ++record )
3770 {
3771 if( readU32( crossNetOffpage, crossNetOffpageConnections + record * 40 + 8 ) != offpageNet )
3772 {
3773 foreignConnection = record;
3774 break;
3775 }
3776 }
3777
3778 BOOST_REQUIRE_NE( foreignConnection, std::numeric_limits<size_t>::max() );
3779 const size_t foreignOffset = crossNetOffpageConnections + foreignConnection * 40;
3780 const size_t firstVertex = crossNetVertices + readU32( crossNetOffpage, foreignOffset + 4 ) * 8;
3781 writeU16( crossNetOffpage, foreignOffset + 12, 0x2000 );
3782 writeU16( crossNetOffpage, firstVertex + 4, readU16( crossNetOffpage, crossNetOffpages + 22 ) );
3783 writeU16( crossNetOffpage, firstVertex + 6, readU16( crossNetOffpage, crossNetOffpages + 24 ) );
3784 BOOST_CHECK_EXCEPTION( parser.Parse( crossNetOffpage, wxS( "cross-net-offpage.sch" ) ), IO_ERROR,
3785 []( const IO_ERROR& aError )
3786 {
3787 return aError.What().Contains( wxS( "controller 20" ) )
3788 && aError.What().Contains( wxS( "shared across different nets" ) );
3789 } );
3790}
3791
3792
3793BOOST_AUTO_TEST_CASE( CorpusSemanticSnapshot )
3794{
3795 PADS_SCH_BINARY_PARSER binaryParser;
3796 std::vector<CANONICAL_SEMANTIC_RECORD> allActual;
3797
3798 for( const std::string& fixture :
3799 { "minimal_v13", "placement_transform", "fields", "connectors", "text_encoding", "page_graphics",
3800 "connectivity_topology", "multisheet_connectivity", "symbol_primitives", "pin_styles", "multigate" } )
3801 {
3802 PADS_SCH_MODEL binary =
3803 binaryParser.Parse( loadBinaryFixture( fixture + ".sch" ), wxString::FromUTF8( fixture + ".sch" ) );
3805 BOOST_REQUIRE( ascii.Parse( KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/binary/" + fixture + ".txt" ) );
3806
3807 auto task10 = []( std::vector<CANONICAL_SEMANTIC_RECORD> aRecords )
3808 {
3809 std::erase_if( aRecords,
3810 []( const CANONICAL_SEMANTIC_RECORD& aRecord )
3811 {
3812 return aRecord.kind != CANONICAL_KIND::NET && aRecord.kind != CANONICAL_KIND::CONNECTION
3813 && aRecord.kind != CANONICAL_KIND::BUS
3814 && aRecord.kind != CANONICAL_KIND::BUS_ENTRY
3815 && aRecord.kind != CANONICAL_KIND::BUS_ALIAS
3816 && aRecord.kind != CANONICAL_KIND::BUS_MEMBER
3817 && aRecord.kind != CANONICAL_KIND::LABEL
3818 && aRecord.kind != CANONICAL_KIND::JUNCTION
3819 && ( aRecord.kind != CANONICAL_KIND::GRAPHIC
3820 || !aRecord.properties.contains( "page_graphic_group" )
3821 || aRecord.properties.at( "page_graphic_group" ).value
3822 != CANONICAL_VALUE( std::string( "BATCHB_PAGE_GRAPHICS" ) ) );
3823 } );
3824
3825 return aRecords;
3826 };
3827
3828 const SNAPSHOT_ALLOWLIST sourceProperties = {
3829 { "global_net_record", PROPERTY_DISPOSITION::EXACT },
3830 { "preserved_net_identity", PROPERTY_DISPOSITION::PRESERVED },
3831 { "preserved_net_relationship", PROPERTY_DISPOSITION::PRESERVED },
3832 { "endpoint_0_raw_endpoint_handle", PROPERTY_DISPOSITION::EXACT },
3833 { "endpoint_1_raw_endpoint_handle", PROPERTY_DISPOSITION::EXACT },
3834 { "endpoint_0_raw_endpoint_relationship", PROPERTY_DISPOSITION::PRESERVED },
3835 { "endpoint_1_raw_endpoint_relationship", PROPERTY_DISPOSITION::PRESERVED },
3836 { "raw_connection_marker", PROPERTY_DISPOSITION::EXACT },
3837 { "offpage_variant", PROPERTY_DISPOSITION::EXACT },
3838 { "offpage_mirror_flags", PROPERTY_DISPOSITION::EXACT },
3839 { "connection_record", PROPERTY_DISPOSITION::EXACT },
3840 { "preserved_drawing_text_relationship", PROPERTY_DISPOSITION::UNSUPPORTED },
3841 { "preserved_graphic_presentation", PROPERTY_DISPOSITION::PRESERVED },
3842 };
3843
3844 BOOST_TEST_CONTEXT( fixture )
3845 {
3846 for( const MODEL_NET& net : binary.nets )
3847 {
3848 for( const MODEL_CONNECTION& connection : net.connections )
3849 {
3850 BOOST_REQUIRE_GE( connection.vertices.size(), 2 );
3851 BOOST_REQUIRE_EQUAL( connection.endpoints.size(), 2 );
3852 BOOST_CHECK_EQUAL( connection.endpoints[0].point.x, connection.vertices.front().x );
3853 BOOST_CHECK_EQUAL( connection.endpoints[0].point.y, connection.vertices.front().y );
3854 BOOST_CHECK_EQUAL( connection.endpoints[1].point.x, connection.vertices.back().x );
3855 BOOST_CHECK_EQUAL( connection.endpoints[1].point.y, connection.vertices.back().y );
3856 BOOST_CHECK( connection.vertices.front().x != connection.vertices.back().x
3857 || connection.vertices.front().y != connection.vertices.back().y );
3858 }
3859 }
3860
3861 auto expected = task10( normalizeAsciiModel( ascii ) );
3862 auto actual = task10( normalizeBinaryModel( binary ) );
3863 BOOST_CHECK( snapshotsMatch( expected, actual, sourceProperties ) );
3864 allActual.insert( allActual.end(), actual.begin(), actual.end() );
3865 }
3866 }
3867
3868 BOOST_CHECK( task10SourcePropertiesPresent( allActual ) );
3869 auto missingRawKind = allActual;
3870
3871 auto label = std::ranges::find( missingRawKind, CANONICAL_KIND::LABEL, &CANONICAL_SEMANTIC_RECORD::kind );
3872 BOOST_REQUIRE( label != missingRawKind.end() );
3873 label->properties.erase( "offpage_variant" );
3874
3875 BOOST_CHECK( !task10SourcePropertiesPresent( missingRawKind ) );
3876
3877 for( const std::string& endpoint :
3878 { "endpoint_0_raw_endpoint_handle", "endpoint_1_raw_endpoint_handle", "endpoint_0_raw_endpoint_relationship",
3879 "endpoint_1_raw_endpoint_relationship" } )
3880 {
3881 auto missingEndpoint = allActual;
3882 auto connection =
3883 std::ranges::find( missingEndpoint, CANONICAL_KIND::CONNECTION, &CANONICAL_SEMANTIC_RECORD::kind );
3884 BOOST_REQUIRE( connection != missingEndpoint.end() );
3885 connection->properties.erase( endpoint );
3886 BOOST_CHECK( !task10SourcePropertiesPresent( missingEndpoint ) );
3887 }
3888}
3889
3890
3891BOOST_AUTO_TEST_CASE( SymbolHandleErrors )
3892{
3894 std::vector<uint8_t> wrongClass = loadBinaryFixture( "symbol_primitives.sch" );
3895 size_t usedDecals = sheetControllerOffset( wrongClass, 7 );
3896 writeU32( wrongClass, usedDecals + 104 * 108 + 48, 8 );
3897 BOOST_CHECK_EXCEPTION( parser.Parse( wrongClass, wxS( "wrong-class.sch" ) ), IO_ERROR,
3898 []( const IO_ERROR& aError )
3899 {
3900 wxString message = aError.What();
3901 return message.Contains( wxS( "wrong object class" ) )
3902 && message.Contains( wxS( "controller 7" ) )
3903 && message.Contains( wxS( "record 104" ) );
3904 } );
3905
3906 std::vector<uint8_t> unresolved = loadBinaryFixture( "multigate.sch" );
3907 size_t gates = sheetControllerOffset( unresolved, 10 );
3908 writeU16( unresolved, gates + 12, 0xFFFF );
3909 BOOST_CHECK_EXCEPTION( parser.Parse( unresolved, wxS( "unresolved.sch" ) ), IO_ERROR,
3910 []( const IO_ERROR& aError )
3911 {
3912 return aError.What().Contains( wxS( "unresolved symbol definition reference" ) );
3913 } );
3914
3915 std::vector<uint8_t> badPinHandle = loadBinaryFixture( "symbol_primitives.sch" );
3916 size_t terminals = sheetControllerOffset( badPinHandle, 8 );
3917 writeU16( badPinHandle, terminals + 8 * 26, 0xFFFE );
3918 BOOST_CHECK_EXCEPTION( parser.Parse( badPinHandle, wxS( "bad-pin-handle.sch" ) ), IO_ERROR,
3919 []( const IO_ERROR& aError )
3920 {
3921 return aError.What().Contains( wxS( "pin-decal handle" ) )
3922 && aError.What().Contains( wxS( "controller 8" ) );
3923 } );
3924
3925 std::vector<uint8_t> gateCount = loadBinaryFixture( "multigate.sch" );
3926 size_t partTypes = sheetControllerOffset( gateCount, 9 );
3927 writeU16( gateCount, partTypes + 76 + 68, 3 );
3928 BOOST_CHECK_EXCEPTION( parser.Parse( gateCount, wxS( "gate-count.sch" ) ), IO_ERROR,
3929 []( const IO_ERROR& aError )
3930 {
3931 return aError.What().Contains( wxS( "gate count" ) )
3932 && aError.What().Contains( wxS( "controller 9" ) );
3933 } );
3934
3935 std::vector<uint8_t> fieldHandle = loadBinaryFixture( "symbol_primitives.sch" );
3936 size_t fieldDecals = sheetControllerOffset( fieldHandle, 7 );
3937 writeU32( fieldHandle, fieldDecals + 104 * 108 + 52, 132 );
3938 BOOST_CHECK_EXCEPTION( parser.Parse( fieldHandle, wxS( "field-handle.sch" ) ), IO_ERROR,
3939 []( const IO_ERROR& aError )
3940 {
3941 return aError.What().Contains( wxS( "field" ) )
3942 && aError.What().Contains( wxS( "controller 7" ) );
3943 } );
3944
3945 std::vector<uint8_t> alternateHandle = loadBinaryFixture( "multigate.sch" );
3946 size_t alternateGates = sheetControllerOffset( alternateHandle, 10 );
3947 writeU16( alternateHandle, alternateGates + 2, 0xFFFE );
3948 BOOST_CHECK_EXCEPTION( parser.Parse( alternateHandle, wxS( "alternate-handle.sch" ) ), IO_ERROR,
3949 []( const IO_ERROR& aError )
3950 {
3951 return aError.What().Contains( wxS( "alternate symbol definition" ) )
3952 && aError.What().Contains( wxS( "controller 10" ) );
3953 } );
3954
3955 std::vector<uint8_t> binaryCycle = loadBinaryFixture( "multigate.sch" );
3956 const size_t cyclePartTypes = sheetControllerOffset( binaryCycle, 9 );
3957 const size_t cycleGates = sheetControllerOffset( binaryCycle, 10 );
3958 const uint32_t firstGateRecord = readU32( binaryCycle, cyclePartTypes + 76 + 44 );
3959 const uint16_t firstDefinition = readU16( binaryCycle, cycleGates + firstGateRecord * 12 );
3960 const uint16_t secondDefinition = readU16( binaryCycle, cycleGates + ( firstGateRecord + 1 ) * 12 );
3961 writeU16( binaryCycle, cycleGates + firstGateRecord * 12 + 2, secondDefinition );
3962 writeU16( binaryCycle, cycleGates + ( firstGateRecord + 1 ) * 12 + 2, firstDefinition );
3963 BOOST_CHECK_EXCEPTION( parser.Parse( binaryCycle, wxS( "definition-cycle.sch" ) ), IO_ERROR,
3964 []( const IO_ERROR& aError )
3965 {
3966 return aError.What().Contains( wxS( "cyclic symbol definition reference" ) )
3967 && aError.What().Contains( wxS( "controller 10" ) );
3968 } );
3969
3970 std::vector<uint8_t> unknownSide = loadBinaryFixture( "pin_styles.sch" );
3971 const size_t unknownSideTerminals = sheetControllerOffset( unknownSide, 8 );
3972 writeU16( unknownSide, unknownSideTerminals + 22, 8 );
3973 PADS_SCH_MODEL unknownSideModel = parser.Parse( unknownSide, wxS( "unknown-side.sch" ) );
3974 BOOST_CHECK( std::ranges::any_of( unknownSideModel.diagnostics,
3975 []( const PARSER_DIAGNOSTIC& aDiagnostic )
3976 {
3977 return aDiagnostic.message.Contains( wxS( "terminal side" ) )
3978 && aDiagnostic.source.controller == 8
3979 && aDiagnostic.source.recordIndex == 0;
3980 } ) );
3981
3982 std::vector<uint8_t> unknownLength = loadBinaryFixture( "pin_styles.sch" );
3983 const PADS_SCH_MODEL unknownLengthBaseline = parser.Parse( unknownLength, wxS( "known-length.sch" ) );
3984 const size_t unknownLengthTerminalRecord = static_cast<size_t>(
3985 itemNamed( unknownLengthBaseline.definitions, wxS( "BATCHB_PIN_STYLES" ) ).pins[0].source.recordIndex );
3986 const size_t unknownLengthTerminals = sheetControllerOffset( unknownLength, 8 );
3987 const uint16_t pinDecalHandle = readU16( unknownLength, unknownLengthTerminals + unknownLengthTerminalRecord * 26 );
3988 const size_t unknownLengthDecals = sheetControllerOffset( unknownLength, 7 );
3989 const uint32_t pinDecalDefinition = readU32( unknownLength, unknownLengthDecals + pinDecalHandle * 108 + 48 );
3990 const size_t unknownLengthDefinitions = sheetControllerOffset( unknownLength, 3 );
3991 const uint32_t pinDecalVertex = readU32( unknownLength, unknownLengthDefinitions + pinDecalDefinition * 80 + 0x34 );
3992 const uint32_t pinDecalVertexEnd =
3993 readU32( unknownLength, unknownLengthDefinitions + ( pinDecalDefinition + 1 ) * 80 + 0x34 );
3994 const size_t unknownLengthVertices = sheetControllerOffset( unknownLength, 5 );
3995
3996 for( uint32_t vertex = pinDecalVertex; vertex < pinDecalVertexEnd; ++vertex )
3997 {
3998 const size_t vertexOffset = unknownLengthVertices + vertex * 6;
3999
4000 if( readU16( unknownLength, vertexOffset ) == 0 && readU16( unknownLength, vertexOffset + 2 ) == 0 )
4001 writeU16( unknownLength, vertexOffset, 1 );
4002 }
4003 PADS_SCH_MODEL unknownLengthModel = parser.Parse( unknownLength, wxS( "unknown-length.sch" ) );
4004 const MODEL_PIN_DEFINITION& unknownLengthPin =
4005 itemNamed( unknownLengthModel.definitions, wxS( "BATCHB_PIN_STYLES" ) ).pins[0];
4006 BOOST_CHECK_EQUAL( unknownLengthPin.length, 0 );
4007 BOOST_CHECK( std::ranges::any_of( unknownLengthPin.properties,
4008 []( const SOURCE_PROPERTY& aProperty )
4009 {
4010 return aProperty.name.text == wxS( "pin_length" )
4011 && aProperty.disposition == PROPERTY_DISPOSITION::UNSUPPORTED;
4012 } ) );
4013
4014 std::vector<uint8_t> pieceCount = loadBinaryFixture( "symbol_primitives.sch" );
4015 size_t definitions = sheetControllerOffset( pieceCount, 3 );
4016 pieceCount[definitions + 14 * 80 + 43] = 1;
4017 BOOST_CHECK_EXCEPTION( parser.Parse( pieceCount, wxS( "piece-count.sch" ) ), IO_ERROR,
4018 []( const IO_ERROR& aError )
4019 {
4020 return aError.What().Contains( wxS( "graphic-piece counts" ) )
4021 && aError.What().Contains( wxS( "controller 3" ) );
4022 } );
4023
4024 std::vector<uint8_t> ownership = loadBinaryFixture( "symbol_primitives.sch" );
4025 size_t ownershipDefinitions = sheetControllerOffset( ownership, 3 );
4026 const size_t vertexPrefix = ownershipDefinitions + 14 * 80 + 0x34;
4027 writeU32( ownership, vertexPrefix, readU32( ownership, vertexPrefix ) + 1 );
4028 BOOST_CHECK_EXCEPTION( parser.Parse( ownership, wxS( "piece-ownership.sch" ) ), IO_ERROR,
4029 []( const IO_ERROR& aError )
4030 {
4031 return aError.What().Contains( wxS( "ownership mismatch" ) )
4032 && aError.What().Contains( wxS( "controller 3" ) );
4033 } );
4034
4035 auto duplicateModel = [&]()
4036 {
4037 return parser.Parse( loadBinaryFixture( "multigate.sch" ), wxS( "duplicate-id.sch" ) );
4038 };
4039 PADS_SCH_MODEL duplicateDefinition = duplicateModel();
4040 duplicateDefinition.definitions[1].id = duplicateDefinition.definitions[0].id;
4041 BOOST_CHECK_EXCEPTION( duplicateDefinition.ValidateOrThrow(), IO_ERROR,
4042 []( const IO_ERROR& aError )
4043 {
4044 return aError.What().Contains( wxS( "duplicate definition ID" ) );
4045 } );
4046 PADS_SCH_MODEL duplicatePart = duplicateModel();
4047 duplicatePart.partTypes[1].id = duplicatePart.partTypes[0].id;
4048 BOOST_CHECK_EXCEPTION( duplicatePart.ValidateOrThrow(), IO_ERROR,
4049 []( const IO_ERROR& aError )
4050 {
4051 return aError.What().Contains( wxS( "duplicate part type ID" ) );
4052 } );
4053 PADS_SCH_MODEL duplicateGate = duplicateModel();
4054 duplicateGate.partTypes[1].gates[1].id = duplicateGate.partTypes[1].gates[0].id;
4055 BOOST_CHECK_EXCEPTION( duplicateGate.ValidateOrThrow(), IO_ERROR,
4056 []( const IO_ERROR& aError )
4057 {
4058 return aError.What().Contains( wxS( "duplicate gate ID" ) );
4059 } );
4060 PADS_SCH_MODEL duplicatePin = duplicateModel();
4061 const MODEL_SYMBOL_DEFINITION& pinOwner = itemNamed( duplicatePin.definitions, wxS( "BATCHB_PIN_STYLES" ) );
4062 size_t pinOwnerIndex = &pinOwner - duplicatePin.definitions.data();
4063 duplicatePin.definitions[pinOwnerIndex].pins[1].id = duplicatePin.definitions[pinOwnerIndex].pins[0].id;
4064 BOOST_CHECK_EXCEPTION( duplicatePin.ValidateOrThrow(), IO_ERROR,
4065 []( const IO_ERROR& aError )
4066 {
4067 return aError.What().Contains( wxS( "duplicate pin ID" ) );
4068 } );
4069
4070 PADS_SCH_MODEL duplicateField = duplicateModel();
4071 duplicateField.definitions[0].fields.push_back( duplicateField.definitions[1].fields[0] );
4072 duplicateField.definitions[0].fields.back().source = duplicateField.definitions[0].source;
4073 BOOST_CHECK_EXCEPTION( duplicateField.ValidateOrThrow(), IO_ERROR,
4074 []( const IO_ERROR& aError )
4075 {
4076 return aError.What().Contains( wxS( "duplicate field ID" ) )
4077 && aError.What().Contains( wxS( "first at" ) );
4078 } );
4079
4080 std::vector<uint8_t> duplicateBinaryField = loadBinaryFixture( "fields.sch" );
4081 const size_t duplicateFieldDecals = sheetControllerOffset( duplicateBinaryField, 7 );
4082 const uint32_t textCount = sheetControllerCount( duplicateBinaryField, 1 );
4083 std::vector<size_t> fieldRecords;
4084
4085 for( size_t record = 0; record < sheetControllerCount( duplicateBinaryField, 7 ); ++record )
4086 {
4087 if( readU32( duplicateBinaryField, duplicateFieldDecals + record * 108 + 52 ) < textCount )
4088 fieldRecords.push_back( record );
4089 }
4090
4091 BOOST_REQUIRE_GE( fieldRecords.size(), 2 );
4092 writeU32( duplicateBinaryField, duplicateFieldDecals + fieldRecords[1] * 108 + 52,
4093 readU32( duplicateBinaryField, duplicateFieldDecals + fieldRecords[0] * 108 + 52 ) );
4094 BOOST_CHECK_EXCEPTION( parser.Parse( duplicateBinaryField, wxS( "duplicate-field.sch" ) ), IO_ERROR,
4095 []( const IO_ERROR& aError )
4096 {
4097 return aError.What().Contains( wxS( "duplicate field ID" ) )
4098 && aError.What().Contains( wxS( "controller 7" ) )
4099 && aError.What().Contains( wxS( "first at" ) );
4100 } );
4101
4102 std::vector<uint8_t> connectorPinName = loadBinaryFixture( "connectors.sch" );
4103 const size_t connectorParts = sheetControllerOffset( connectorPinName, 9 );
4104 const size_t connectorPins = sheetControllerOffset( connectorPinName, 11 );
4105 const uint32_t connectorPinStart = readU32( connectorPinName, connectorParts + 76 + 48 );
4106 writeU32( connectorPinName, connectorPins + connectorPinStart * 24, 0xFFFFFFFE );
4107 BOOST_CHECK_EXCEPTION( parser.Parse( connectorPinName, wxS( "connector-pin-name.sch" ) ), IO_ERROR,
4108 []( const IO_ERROR& aError )
4109 {
4110 return aError.What().Contains( wxS( "pin-name offset leaves controller 14" ) )
4111 && aError.What().Contains( wxS( "controller 11" ) );
4112 } );
4113}
4114
4115
4116BOOST_AUTO_TEST_CASE( SymbolDefinitionSemanticSnapshot )
4117{
4118 PADS_SCH_BINARY_PARSER binaryParser;
4119 PADS_SCH_MODEL binary = binaryParser.Parse( loadBinaryFixture( "pin_styles.sch" ), wxS( "pin_styles.sch" ) );
4120 PADS_SCH::PADS_SCH_PARSER asciiParser;
4121 BOOST_REQUIRE( asciiParser.Parse( KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/binary/pin_styles.txt" ) );
4122
4123 const MODEL_SYMBOL_DEFINITION& binaryDefinition = itemNamed( binary.definitions, wxS( "BATCHB_PIN_STYLES" ) );
4124 auto asciiDefinition = std::ranges::find_if( asciiParser.GetSymbolDefs(),
4125 []( const PADS_SCH::SYMBOL_DEF& aDefinition )
4126 {
4127 return aDefinition.name == "BATCHB_PIN_STYLES";
4128 } );
4129 BOOST_REQUIRE( asciiDefinition != asciiParser.GetSymbolDefs().end() );
4130 BOOST_CHECK_EQUAL( binaryDefinition.graphics.size(), asciiDefinition->graphics.size() );
4131 BOOST_CHECK_EQUAL( binaryDefinition.pins.size(), asciiDefinition->pins.size() );
4132 BOOST_CHECK_EQUAL( binaryDefinition.pins[0].position.x,
4133 static_cast<int64_t>( std::llround( asciiDefinition->pins[0].position.x * 2 ) ) );
4134 BOOST_CHECK_EQUAL( binaryDefinition.pins[4].graphicStyle,
4135 ( asciiDefinition->pins[4].inverted ? 1U : 0U ) | ( asciiDefinition->pins[4].clock ? 2U : 0U ) );
4136
4137 BOOST_REQUIRE_EQUAL( binaryDefinition.graphics.size(), asciiDefinition->graphics.size() );
4138
4139 for( size_t i = 0; i < binaryDefinition.graphics.size(); ++i )
4140 {
4141 BOOST_CHECK( canonicalGraphicType( binaryDefinition.graphics[i].kind )
4142 == canonicalGraphicType( asciiDefinition->graphics[i].type ) );
4143 BOOST_CHECK_EQUAL( binaryDefinition.graphics[i].strokeWidth,
4144 std::llround( asciiDefinition->graphics[i].line_width * 2 ) );
4145 BOOST_CHECK( canonicalFill( binaryDefinition.graphics[i].fill )
4146 == canonicalFill( asciiDefinition->graphics[i].filled ) );
4147 BOOST_CHECK_EQUAL( binaryDefinition.graphics[i].points.size(), asciiDefinition->graphics[i].points.size() );
4148 }
4149
4150 BOOST_REQUIRE_EQUAL( binaryDefinition.pins.size(), asciiDefinition->pins.size() );
4151 const PADS_SCH::GATE_DEF& asciiGate = asciiParser.GetPartTypes().at( "BATCHB-PIN-STYLES" ).gates[0];
4152 BOOST_REQUIRE_EQUAL( asciiGate.pins.size(), binaryDefinition.pins.size() );
4153
4154 auto checkPinPresentation = []( const MODEL_PIN_DEFINITION& aBinaryPin, const PADS_SCH::SYMBOL_DEF& aAsciiSymbol,
4155 const PADS_SCH::SYMBOL_PIN& aAsciiPin )
4156 {
4157 BOOST_CHECK( canonicalPinStyle( aBinaryPin.graphicStyle )
4158 == canonicalPinStyle( aAsciiPin.inverted, aAsciiPin.clock ) );
4159 BOOST_CHECK_EQUAL( aBinaryPin.length, std::llround( aAsciiPin.length * 2 ) );
4160 BOOST_CHECK_EQUAL( aBinaryPin.position.x, std::llround( aAsciiPin.position.x * 2 ) );
4161 BOOST_CHECK_EQUAL( aBinaryPin.position.y, std::llround( aAsciiPin.position.y * 2 ) );
4162 BOOST_CHECK_EQUAL( aBinaryPin.side, aAsciiPin.side );
4163 BOOST_CHECK_EQUAL( aBinaryPin.angle, std::llround( aAsciiPin.rotation * 10 ) );
4164 BOOST_CHECK_EQUAL( aBinaryPin.numberPresentation.height, aAsciiSymbol.height * 2 );
4165 BOOST_CHECK_EQUAL( aBinaryPin.numberPresentation.width, aAsciiSymbol.width * 2 );
4166 BOOST_CHECK_EQUAL( aBinaryPin.namePresentation.height, aAsciiSymbol.h2 * 2 );
4167 BOOST_CHECK_EQUAL( aBinaryPin.namePresentation.width, aAsciiSymbol.w2 * 2 );
4168 BOOST_CHECK_EQUAL( aBinaryPin.numberOffset.x, std::llround( aAsciiPin.pn_offset.x * 2 ) );
4169 BOOST_CHECK_EQUAL( aBinaryPin.numberOffset.y, std::llround( aAsciiPin.pn_offset.y * 2 ) );
4170 BOOST_CHECK_EQUAL( aBinaryPin.nameOffset.x, std::llround( aAsciiPin.pl_offset.x * 2 ) );
4171 BOOST_CHECK_EQUAL( aBinaryPin.nameOffset.y, std::llround( aAsciiPin.pl_offset.y * 2 ) );
4172 BOOST_CHECK_EQUAL( aBinaryPin.numberAngle, aAsciiPin.pn_angle * 10 );
4173 BOOST_CHECK_EQUAL( aBinaryPin.nameAngle, aAsciiPin.pl_angle * 10 );
4174 BOOST_CHECK_EQUAL( aBinaryPin.numberJustification, aAsciiPin.pn_just );
4175 BOOST_CHECK_EQUAL( aBinaryPin.nameJustification, aAsciiPin.pl_just );
4176 BOOST_CHECK_EQUAL( aBinaryPin.nameOffsetAngle, aAsciiPin.p_angle1 * 10 );
4177 BOOST_CHECK_EQUAL( aBinaryPin.numberOffsetAngle, aAsciiPin.p_angle2 * 10 );
4178 BOOST_CHECK_EQUAL( aBinaryPin.nameOffsetJustification, aAsciiPin.p_just1 );
4179 BOOST_CHECK_EQUAL( aBinaryPin.numberOffsetJustification, aAsciiPin.p_just2 );
4180 BOOST_CHECK_EQUAL( aBinaryPin.visibilityFlags, aAsciiPin.p_flags );
4182 aAsciiSymbol.h2 != 0 && ( aAsciiPin.p_flags & 128 ) == 0 );
4183 BOOST_CHECK_EQUAL( aBinaryPin.numberPresentation.visible, aAsciiSymbol.height != 0 );
4184 };
4185
4186 for( size_t i = 0; i < binaryDefinition.pins.size(); ++i )
4187 {
4188 BOOST_CHECK_EQUAL( binaryDefinition.pins[i].number.text, wxString::FromUTF8( asciiGate.pins[i].pin_id ) );
4189 BOOST_CHECK_EQUAL( binaryDefinition.pins[i].name.text, wxString::FromUTF8( asciiGate.pins[i].pin_name ) );
4190 BOOST_CHECK( canonicalPinType( binaryDefinition.pins[i].electricalType )
4191 == canonicalPinType( asciiGate.pins[i].pin_type ) );
4192 checkPinPresentation( binaryDefinition.pins[i], *asciiDefinition, asciiDefinition->pins[i] );
4193 }
4194
4195 PADS_SCH_MODEL primitiveBinary =
4196 binaryParser.Parse( loadBinaryFixture( "symbol_primitives.sch" ), wxS( "symbol_primitives.sch" ) );
4197 PADS_SCH::PADS_SCH_PARSER primitiveAscii;
4199 primitiveAscii.Parse( KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/binary/symbol_primitives.txt" ) );
4200 const MODEL_SYMBOL_DEFINITION& primitiveDefinition =
4201 itemNamed( primitiveBinary.definitions, wxS( "BATCHB_PRIMITIVES" ) );
4202 const PADS_SCH::SYMBOL_DEF* asciiPrimitive = primitiveAscii.GetSymbolDef( "BATCHB_PRIMITIVES" );
4203 BOOST_REQUIRE( asciiPrimitive );
4204 BOOST_REQUIRE_EQUAL( primitiveDefinition.graphics.size(),
4205 asciiPrimitive->graphics.size() + asciiPrimitive->texts.size() );
4206
4207 for( size_t graphic = 0; graphic < asciiPrimitive->graphics.size(); ++graphic )
4208 {
4209 BOOST_CHECK( canonicalGraphicType( primitiveDefinition.graphics[graphic].kind )
4210 == canonicalGraphicType( asciiPrimitive->graphics[graphic] ) );
4211 BOOST_CHECK_EQUAL( primitiveDefinition.graphics[graphic].strokeWidth,
4212 std::llround( asciiPrimitive->graphics[graphic].line_width * 2 ) );
4213 BOOST_CHECK( primitiveDefinition.graphics[graphic].lineStyle == MODEL_LINE_STYLE::SOLID );
4214 BOOST_CHECK( canonicalFill( primitiveDefinition.graphics[graphic].fill )
4215 == canonicalFill( asciiPrimitive->graphics[graphic].filled ) );
4216 BOOST_REQUIRE_EQUAL( primitiveDefinition.graphics[graphic].points.size(),
4217 asciiPrimitive->graphics[graphic].points.size() );
4218
4219 for( size_t pointIndex = 0; pointIndex < asciiPrimitive->graphics[graphic].points.size(); ++pointIndex )
4220 {
4221 BOOST_CHECK_EQUAL( primitiveDefinition.graphics[graphic].points[pointIndex].x,
4222 std::llround( asciiPrimitive->graphics[graphic].points[pointIndex].coord.x * 2 ) );
4223 BOOST_CHECK_EQUAL( primitiveDefinition.graphics[graphic].points[pointIndex].y,
4224 std::llround( asciiPrimitive->graphics[graphic].points[pointIndex].coord.y * 2 ) );
4225 }
4226 }
4227
4228 const MODEL_GRAPHIC& binaryText = primitiveDefinition.graphics.back();
4229 BOOST_REQUIRE_EQUAL( asciiPrimitive->texts.size(), 1 );
4230 BOOST_CHECK_EQUAL( binaryText.text.text, wxString::FromUTF8( asciiPrimitive->texts[0].content ) );
4231 BOOST_CHECK_EQUAL( binaryText.points[0].x, std::llround( asciiPrimitive->texts[0].position.x * 2 ) );
4232 BOOST_CHECK_EQUAL( binaryText.points[0].y, std::llround( asciiPrimitive->texts[0].position.y * 2 ) );
4233 BOOST_CHECK_EQUAL( binaryText.presentation.height, std::llround( asciiPrimitive->texts[0].size * 2 ) );
4234 BOOST_CHECK_EQUAL( binaryText.presentation.width, asciiPrimitive->texts[0].width_factor * 2 );
4236 binaryText.presentation.font.text,
4237 wxString::FromUTF8( canonicalAsciiFont( primitiveAscii.GetParameters(), asciiPrimitive->font1 ) ) );
4238 BOOST_CHECK_EQUAL( binaryText.angle, std::llround( asciiPrimitive->texts[0].rotation * 10 ) );
4239 BOOST_CHECK_EQUAL( binaryText.presentation.visible, asciiPrimitive->texts[0].visible );
4240 BOOST_CHECK( canonicalJustification( binaryText.presentation.horizontalJustification ).value
4241 == canonicalHorizontalJustification( asciiPrimitive->texts[0].justification ).value );
4242 BOOST_CHECK( canonicalVerticalJustification( binaryText.presentation.verticalJustification ).value
4243 == canonicalVerticalJustification( asciiPrimitive->texts[0].justification ).value );
4244
4245 PADS_SCH_MODEL fieldsBinary = binaryParser.Parse( loadBinaryFixture( "fields.sch" ), wxS( "fields.sch" ) );
4246 PADS_SCH::PADS_SCH_PARSER fieldsAscii;
4247 BOOST_REQUIRE( fieldsAscii.Parse( KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/binary/fields.txt" ) );
4248 const MODEL_SYMBOL_DEFINITION& fieldsDefinition = itemNamed( fieldsBinary.definitions, wxS( "RESZ-H" ) );
4249 const PADS_SCH::SYMBOL_DEF* asciiFields = fieldsAscii.GetSymbolDef( "RESZ-H" );
4250 BOOST_REQUIRE( asciiFields );
4251 BOOST_REQUIRE_EQUAL( fieldsDefinition.fields.size(), asciiFields->attrs.size() );
4252
4253 for( size_t field = 0; field < asciiFields->attrs.size(); ++field )
4254 {
4255 BOOST_TEST_CONTEXT( "field " << field )
4256 {
4257 BOOST_CHECK_EQUAL( fieldsDefinition.fields[field].name.text,
4258 wxString::FromUTF8( asciiFields->attrs[field].attr_name ) );
4259 BOOST_CHECK( fieldsDefinition.fields[field].value.text.empty() );
4260 BOOST_CHECK_EQUAL( fieldsDefinition.fields[field].position.x,
4261 std::llround( asciiFields->attrs[field].position.x * 2 ) );
4262 BOOST_CHECK_EQUAL( fieldsDefinition.fields[field].position.y,
4263 std::llround( asciiFields->attrs[field].position.y * 2 ) );
4264 BOOST_CHECK_EQUAL( fieldsDefinition.fields[field].angle, asciiFields->attrs[field].angle * 10 );
4265 BOOST_CHECK_EQUAL( fieldsDefinition.fields[field].presentation.height,
4266 asciiFields->attrs[field].height * 2 );
4267 BOOST_CHECK_EQUAL( fieldsDefinition.fields[field].presentation.width, asciiFields->attrs[field].width * 2 );
4268 BOOST_CHECK_EQUAL( fieldsDefinition.fields[field].presentation.font.text,
4269 wxString::FromUTF8( canonicalAsciiFont( fieldsAscii.GetParameters(),
4270 asciiFields->attrs[field].font_name ) ) );
4272 canonicalJustification( fieldsDefinition.fields[field].presentation.horizontalJustification ).value
4273 == canonicalHorizontalJustification( asciiFields->attrs[field].justification ).value );
4275 canonicalVerticalJustification( fieldsDefinition.fields[field].presentation.verticalJustification )
4276 .value
4277 == canonicalVerticalJustification( asciiFields->attrs[field].justification ).value );
4278 }
4279 }
4280
4281 const MODEL_PART_TYPE& fieldsPart = itemNamed( fieldsBinary.partTypes, wxS( "RES-RESN1" ) );
4282 BOOST_REQUIRE_EQUAL( fieldsPart.fields.size(), fieldsDefinition.fields.size() );
4283
4284 for( size_t field = 0; field < fieldsPart.fields.size(); ++field )
4285 {
4286 BOOST_CHECK_EQUAL( fieldsPart.fields[field].name.text, fieldsDefinition.fields[field].name.text );
4287 BOOST_CHECK_EQUAL( fieldsPart.fields[field].value.text, fieldsDefinition.fields[field].value.text );
4288 BOOST_CHECK( fieldsPart.fields[field].presentation == fieldsDefinition.fields[field].presentation );
4289 }
4290
4291 PADS_SCH_MODEL multiBinary = binaryParser.Parse( loadBinaryFixture( "multigate.sch" ), wxS( "multigate.sch" ) );
4292 PADS_SCH::PADS_SCH_PARSER multiAscii;
4293 BOOST_REQUIRE( multiAscii.Parse( KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/binary/multigate.txt" ) );
4294 const MODEL_PART_TYPE& multiPart = itemNamed( multiBinary.partTypes, wxS( "BATCHD-MULTIGATE" ) );
4295 const PADS_SCH::PARTTYPE_DEF& asciiMulti = multiAscii.GetPartTypes().at( "BATCHD-MULTIGATE" );
4296 BOOST_REQUIRE_EQUAL( multiPart.gates.size(), asciiMulti.gates.size() );
4297 BOOST_REQUIRE_EQUAL( multiPart.signalPins.size(), asciiMulti.sigpins.size() );
4298
4299 auto definitionName = [&]( const PADS_SCH_MODEL& aModel, const DEFINITION_REFERENCE& aReference )
4300 {
4301 auto definition = std::ranges::find_if( aModel.definitions,
4302 [&]( const MODEL_SYMBOL_DEFINITION& aDefinition )
4303 {
4304 return aDefinition.id == aReference.id;
4305 } );
4306 BOOST_REQUIRE( definition != aModel.definitions.end() );
4307 return definition->name.text;
4308 };
4309
4310 auto pinDefinition = []( const PADS_SCH_MODEL& aModel,
4311 const PIN_REFERENCE& aReference ) -> const MODEL_PIN_DEFINITION&
4312 {
4313 for( const MODEL_SYMBOL_DEFINITION& definition : aModel.definitions )
4314 {
4315 auto pin = std::ranges::find_if( definition.pins,
4316 [&]( const MODEL_PIN_DEFINITION& aPin )
4317 {
4318 return aPin.id == aReference.id;
4319 } );
4320
4321 if( pin != definition.pins.end() )
4322 return *pin;
4323 }
4324
4325 BOOST_FAIL( "unresolved pin reference" );
4326 throw std::logic_error( "unreachable" );
4327 };
4328
4329 for( size_t gate = 0; gate < multiPart.gates.size(); ++gate )
4330 {
4331 BOOST_CHECK_EQUAL( definitionName( multiBinary, multiPart.gates[gate].definition ),
4332 wxString::FromUTF8( asciiMulti.gates[gate].decal_names.front() ) );
4333 BOOST_CHECK_EQUAL( propertyValue( multiPart.gates[gate].properties, wxS( "swap_group" ) ),
4334 wxString::Format( wxS( "%d" ), asciiMulti.gates[gate].swap_flag ) );
4335 BOOST_CHECK_EQUAL( multiPart.gates[gate].pins.size(), asciiMulti.gates[gate].pins.size() );
4336
4337 for( size_t pin = 0; pin < multiPart.gates[gate].pins.size(); ++pin )
4338 {
4339 const MODEL_PIN_DEFINITION& binaryPin = pinDefinition( multiBinary, multiPart.gates[gate].pins[pin] );
4340 BOOST_CHECK_EQUAL( binaryPin.number.text, wxString::FromUTF8( asciiMulti.gates[gate].pins[pin].pin_id ) );
4341 BOOST_CHECK_EQUAL( binaryPin.name.text, wxString::FromUTF8( asciiMulti.gates[gate].pins[pin].pin_name ) );
4342 BOOST_CHECK_EQUAL( propertyValue( binaryPin.properties, wxS( "swap_group" ) ),
4343 wxString::Format( wxS( "%d" ), asciiMulti.gates[gate].pins[pin].swap_group ) );
4344 }
4345 }
4346
4347 const MODEL_PART_TYPE& alternatePart = itemNamed( multiBinary.partTypes, wxS( "RES-RESN1" ) );
4348 const PADS_SCH::GATE_DEF& asciiAlternates = multiAscii.GetPartTypes().at( "RES-RESN1" ).gates[0];
4349 BOOST_REQUIRE_EQUAL( alternatePart.gates[0].alternateDefinitions.size() + 1, asciiAlternates.decal_names.size() );
4350 BOOST_CHECK_EQUAL( definitionName( multiBinary, alternatePart.gates[0].definition ),
4351 wxString::FromUTF8( asciiAlternates.decal_names[0] ) );
4352
4353 for( size_t alternate = 0; alternate < alternatePart.gates[0].alternateDefinitions.size(); ++alternate )
4354 {
4355 BOOST_CHECK_EQUAL( definitionName( multiBinary, alternatePart.gates[0].alternateDefinitions[alternate] ),
4356 wxString::FromUTF8( asciiAlternates.decal_names[alternate + 1] ) );
4357 }
4358
4359 for( size_t signal = 0; signal < multiPart.signalPins.size(); ++signal )
4360 {
4361 BOOST_CHECK_EQUAL( multiPart.signalPins[signal].number.text,
4362 wxString::FromUTF8( asciiMulti.sigpins[signal].pin_number ) );
4363 BOOST_CHECK_EQUAL( multiPart.signalPins[signal].name.text,
4364 wxString::FromUTF8( asciiMulti.sigpins[signal].net_name ) );
4365 }
4366
4367 PADS_SCH_MODEL connectorsBinary =
4368 binaryParser.Parse( loadBinaryFixture( "connectors.sch" ), wxS( "connectors.sch" ) );
4369 PADS_SCH::PADS_SCH_PARSER connectorsAscii;
4370 BOOST_REQUIRE( connectorsAscii.Parse( KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/binary/connectors.txt" ) );
4371 const MODEL_PART_TYPE& connectorPart = itemNamed( connectorsBinary.partTypes, wxS( "CON-26P-ED" ) );
4372 const PADS_SCH::GATE_DEF& asciiConnector = connectorsAscii.GetPartTypes().at( "CON-26P-ED" ).gates[0];
4373 BOOST_REQUIRE_EQUAL( connectorPart.gates[0].decalGroupMembers.size(), asciiConnector.decal_names.size() );
4374
4375 for( size_t member = 0; member < asciiConnector.decal_names.size(); ++member )
4376 {
4377 BOOST_CHECK_EQUAL( definitionName( connectorsBinary, connectorPart.gates[0].decalGroupMembers[member] ),
4378 wxString::FromUTF8( asciiConnector.decal_names[member] ) );
4379 }
4380
4381 const std::array<std::string, 11> pairedFixtures = { "minimal_v13",
4382 "placement_transform",
4383 "fields",
4384 "connectors",
4385 "text_encoding",
4386 "page_graphics",
4387 "connectivity_topology",
4388 "multisheet_connectivity",
4389 "symbol_primitives",
4390 "pin_styles",
4391 "multigate" };
4392
4393 for( const std::string& fixture : pairedFixtures )
4394 {
4395 PADS_SCH_MODEL fixtureBinary =
4396 binaryParser.Parse( loadBinaryFixture( fixture + ".sch" ), wxString::FromUTF8( fixture ) );
4397 PADS_SCH::PADS_SCH_PARSER fixtureAscii;
4399 fixtureAscii.Parse( KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/binary/" + fixture + ".txt" ) );
4400
4401 for( const MODEL_SYMBOL_DEFINITION& binarySymbol : fixtureBinary.definitions )
4402 {
4403 if( binarySymbol.pins.empty() )
4404 continue;
4405
4406 const PADS_SCH::SYMBOL_DEF* asciiSymbol = fixtureAscii.GetSymbolDef( binarySymbol.name.text.ToStdString() );
4407 BOOST_REQUIRE_MESSAGE( asciiSymbol, fixture + ": " + binarySymbol.name.text.ToStdString() );
4408 BOOST_REQUIRE_EQUAL( binarySymbol.pins.size(), asciiSymbol->pins.size() );
4409
4410 for( size_t pin = 0; pin < binarySymbol.pins.size(); ++pin )
4411 {
4412 BOOST_TEST_CONTEXT( fixture << ": " << binarySymbol.name.text << " pin " << pin << " flags "
4413 << binarySymbol.pins[pin].presentationFlags << "/"
4414 << binarySymbol.pins[pin].visibilityAndNumberPresentationFlags )
4415 {
4416 checkPinPresentation( binarySymbol.pins[pin], *asciiSymbol, asciiSymbol->pins[pin] );
4417 }
4418 }
4419 }
4420
4421 for( const MODEL_PART_TYPE& binaryPart : fixtureBinary.partTypes )
4422 {
4423 const auto asciiPart = fixtureAscii.GetPartTypes().find( binaryPart.name.text.ToStdString() );
4424 BOOST_REQUIRE_MESSAGE( asciiPart != fixtureAscii.GetPartTypes().end(),
4425 fixture + ": " + binaryPart.name.text.ToStdString() );
4426
4427 if( asciiPart->second.gates.empty() )
4428 continue;
4429
4430 BOOST_REQUIRE_EQUAL( binaryPart.gates.size(), asciiPart->second.gates.size() );
4431
4432 for( size_t gate = 0; gate < binaryPart.gates.size(); ++gate )
4433 {
4434 const MODEL_GATE& binaryGate = binaryPart.gates[gate];
4435 const PADS_SCH::GATE_DEF& asciiGateForPart = asciiPart->second.gates[gate];
4436
4437 if( !binaryGate.decalGroupMembers.empty() )
4438 {
4439 BOOST_REQUIRE_EQUAL( binaryGate.connectorPins.size(), asciiGateForPart.pins.size() );
4440
4441 for( size_t pin = 0; pin < binaryGate.connectorPins.size(); ++pin )
4442 {
4443 const MODEL_CONNECTOR_PIN& binaryPin = binaryGate.connectorPins[pin];
4444 BOOST_TEST_CONTEXT( fixture << ": " << binaryPart.name.text << " connector pin " << pin )
4445 {
4446 BOOST_CHECK_EQUAL( binaryPin.number.text,
4447 wxString::FromUTF8( asciiGateForPart.pins[pin].pin_id ) );
4448 BOOST_CHECK_EQUAL( binaryPin.name.text,
4449 wxString::FromUTF8( asciiGateForPart.pins[pin].pin_name ) );
4450 BOOST_CHECK( canonicalPinType( binaryPin.electricalType )
4451 == canonicalPinType( asciiGateForPart.pins[pin].pin_type ) );
4452 }
4453 }
4454
4455 continue;
4456 }
4457
4458 BOOST_REQUIRE_EQUAL( binaryGate.pins.size(), asciiGateForPart.pins.size() );
4459 BOOST_REQUIRE_EQUAL( binaryGate.logicalPins.size(), asciiGateForPart.pins.size() );
4460
4461 for( size_t pin = 0; pin < binaryGate.pins.size(); ++pin )
4462 {
4463 const MODEL_GATE_PIN& binaryPin = binaryGate.logicalPins[pin];
4464 BOOST_TEST_CONTEXT( fixture << ": " << binaryPart.name.text << " gate " << gate << " pin " << pin )
4465 {
4466 BOOST_CHECK_EQUAL( binaryPin.number.text,
4467 wxString::FromUTF8( asciiGateForPart.pins[pin].pin_id ) );
4468 BOOST_CHECK_EQUAL( binaryPin.name.text,
4469 wxString::FromUTF8( asciiGateForPart.pins[pin].pin_name ) );
4470 BOOST_CHECK( canonicalPinType( binaryPin.electricalType )
4471 == canonicalPinType( asciiGateForPart.pins[pin].pin_type ) );
4472 }
4473 }
4474 }
4475 }
4476 }
4477}
4478
4479
4480BOOST_AUTO_TEST_CASE( TerminalJustificationCodes )
4481{
4482 static constexpr std::array<const char*, 6> fixtures = { "terminal_justification_0_6",
4483 "terminal_justification_7_13",
4484 "terminal_justification_14_15",
4485 "terminal_justification_90_0_6",
4486 "terminal_justification_90_7_13",
4487 "terminal_justification_90_14_15" };
4488
4489 PADS_SCH_BINARY_PARSER binaryParser;
4490
4491 for( const char* fixture : fixtures )
4492 {
4493 const std::string binaryName = std::string( fixture ) + ".sch";
4494 const std::string asciiName = std::string( fixture ) + ".txt";
4495 PADS_SCH_MODEL binary = binaryParser.Parse( loadBinaryFixture( binaryName ), wxString::FromUTF8( binaryName ) );
4496 PADS_SCH::PADS_SCH_PARSER asciiParser;
4497 BOOST_REQUIRE( asciiParser.Parse( KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/binary/" + asciiName ) );
4498
4499 const MODEL_SYMBOL_DEFINITION& binaryDefinition = itemNamed( binary.definitions, wxS( "BATCHB_PIN_STYLES" ) );
4500 const PADS_SCH::SYMBOL_DEF* asciiDefinition = asciiParser.GetSymbolDef( "BATCHB_PIN_STYLES" );
4501 BOOST_REQUIRE( asciiDefinition );
4502 BOOST_REQUIRE_EQUAL( binaryDefinition.pins.size(), asciiDefinition->pins.size() );
4503
4504 for( size_t pin = 0; pin < binaryDefinition.pins.size(); ++pin )
4505 {
4506 BOOST_CHECK_EQUAL( binaryDefinition.pins[pin].numberAngle, asciiDefinition->pins[pin].pn_angle * 10 );
4507 BOOST_CHECK_EQUAL( binaryDefinition.pins[pin].nameAngle, asciiDefinition->pins[pin].pl_angle * 10 );
4508 BOOST_CHECK_EQUAL( binaryDefinition.pins[pin].numberJustification, asciiDefinition->pins[pin].pn_just );
4509 BOOST_CHECK_EQUAL( binaryDefinition.pins[pin].nameJustification, asciiDefinition->pins[pin].pl_just );
4510 }
4511 }
4512}
4513
4514// Free text shares the justification code space of every other PADS record; expectations follow the PADS
4515// render in TextOptionMatrix, where code 4 is center-bottom
4516BOOST_AUTO_TEST_CASE( FreeTextVerticalJustification )
4517{
4519 PADS_SCH_MODEL model = parser.Parse( loadBinaryFixture( "text_encoding.sch" ), wxS( "text_encoding.sch" ) );
4520
4521 BOOST_REQUIRE_EQUAL( model.texts.size(), 1 );
4522 const size_t justificationOffset = model.texts.front().source.absoluteOffset + 18;
4523
4524 struct VERTICAL_CASE
4525 {
4526 uint16_t code;
4527 MODEL_JUSTIFICATION vertical;
4528 };
4529
4530 for( const VERTICAL_CASE& expected : { VERTICAL_CASE{ 0, MODEL_JUSTIFICATION::RIGHT },
4538 { 12, MODEL_JUSTIFICATION::CENTER } } )
4539 {
4540 BOOST_TEST_CONTEXT( "justification " << expected.code )
4541 {
4542 std::vector<uint8_t> bytes = loadBinaryFixture( "text_encoding.sch" );
4543 writeU16( bytes, justificationOffset, expected.code );
4544
4545 PADS_SCH_MODEL patched = parser.Parse( bytes, wxS( "text_encoding.sch" ) );
4546 BOOST_REQUIRE_EQUAL( patched.texts.size(), 1 );
4547 BOOST_CHECK( patched.texts.front().presentation.verticalJustification == expected.vertical );
4548 }
4549 }
4550}
4551
4552
4553// Vendor designs carry 0xFF00 in the off-page mirror word, so the high byte must not warn
4554BOOST_AUTO_TEST_CASE( OffpageMirrorHighByte )
4555{
4557 std::vector<uint8_t> bytes = loadBinaryFixture( "text_encoding.sch" );
4558 PADS_SCH_MODEL model = parser.Parse( bytes, wxS( "text_encoding.sch" ) );
4559 BOOST_REQUIRE_EQUAL( model.labels.size(), 1 );
4560
4561 writeU16( bytes, model.labels.front().source.absoluteOffset + 28, 0xFF00 );
4562
4563 PADS_SCH_MODEL patched = parser.Parse( bytes, wxS( "text_encoding.sch" ) );
4564 BOOST_REQUIRE_EQUAL( patched.labels.size(), 1 );
4565 BOOST_CHECK_EQUAL( patched.labels.front().angle, model.labels.front().angle );
4566 BOOST_CHECK( std::ranges::none_of( patched.diagnostics,
4567 []( const PARSER_DIAGNOSTIC& aDiagnostic )
4568 {
4569 return aDiagnostic.property
4570 && aDiagnostic.property->name == wxS( "offpage_mirror_flags" );
4571 } ) );
4572}
4573
4574
const char * name
static std::optional< VECTOR2D > normalized(const VECTOR2D &aVec)
bool operator==(const wxAuiPaneInfo &aLhs, const wxAuiPaneInfo &aRhs)
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
Parser for PADS Logic ASCII schematic export files.
const std::vector< TEXT_ITEM > & GetTextItems() const
const std::vector< TIED_DOT > & GetTiedDots() const
const std::vector< OFF_PAGE_CONNECTOR > & GetOffPageConnectors() const
bool Parse(const std::string &aFileName)
const std::vector< SHEET_HEADER > & GetSheetHeaders() const
const std::vector< NETNAME_LABEL > & GetNetNameLabels() const
const std::vector< BUS_DEF > & GetBuses() const
const std::vector< SCH_SIGNAL > & GetSignals() const
const std::vector< LINES_ITEM > & GetLinesItems() const
const std::vector< SYMBOL_DEF > & GetSymbolDefs() const
const std::vector< PART_PLACEMENT > & GetPartPlacements() const
const std::map< std::string, PARTTYPE_DEF > & GetPartTypes() const
const SYMBOL_DEF * GetSymbolDef(const std::string &aName) const
const PARAMETERS & GetParameters() const
constexpr ValueType Value() const
PADS_SCH_MODEL Parse(const std::vector< uint8_t > &aBytes, const wxString &aSourceName={}) const
static SOURCE_STRING DecodeString(const std::vector< uint8_t > &aBytes, uint32_t aCodePage, const SOURCE_PROVENANCE &aSource, std::vector< PARSER_DIAGNOSTIC > &aDiagnostics, const wxString &aRecordedCodePageName={})
static void RecordUnknownEnum(const wxString &aEnumName, uint32_t aValue, const SOURCE_PROVENANCE &aSource, std::vector< PARSER_DIAGNOSTIC > &aDiagnostics)
void Load(std::vector< uint8_t > aBytes)
const std::vector< SCH_SDB_OLE_ITEM > & OleItems() const
std::string utf8(const wxString &aText)
static bool empty(const wxTextEntryBase *aCtrl)
static std::string ToStdString(const wxString &aStr)
bool operator<(const FOOTPRINT_INFO &lhs, const FOOTPRINT_INFO &rhs)
std::string source
std::string message
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
constexpr int64_t NormalizeCoordinate(int64_t aCoordinate)
CONTROLLER_REFERENCE< DEFINITION_ID > DEFINITION_REFERENCE
constexpr int NormalizeAngle(int aAngle)
CONTROLLER_ID< NET_ID_TAG > NET_ID
CONTROLLER_ID< DEFINITION_ID_TAG > DEFINITION_ID
CONTROLLER_REFERENCE< PLACEMENT_ID > PLACEMENT_REFERENCE
CONTROLLER_REFERENCE< SHEET_ID > SHEET_REFERENCE
CONTROLLER_ID< SHEET_ID_TAG > SHEET_ID
CONTROLLER_ID< PIN_ID_TAG > PIN_ID
CONTROLLER_ID< FIELD_ID_TAG, uint64_t > FIELD_ID
constexpr FIELD_ID MakeFieldId(FIELD_ID_DOMAIN aDomain, uint32_t aOwner, uint32_t aOrdinal)
CONTROLLER_ID< PART_TYPE_ID_TAG > PART_TYPE_ID
wxString FormatParserError(const SOURCE_PROVENANCE &aSource, const wxString &aMessage)
CONTROLLER_ID< PLACEMENT_ID_TAG > PLACEMENT_ID
CONTROLLER_ID< GATE_ID_TAG > GATE_ID
CONTROLLER_REFERENCE< NET_ID > NET_REFERENCE
CONTROLLER_REFERENCE< GATE_ID > GATE_REFERENCE
CONTROLLER_ID< BUS_ID_TAG > BUS_ID
@ SHEET
Joins names in one instance only.
Definition conn_keys.h:65
@ BUS_MEMBER
Synthetic claim of a bus member slot, set by BindBundle().
Definition conn_claims.h:47
STL namespace.
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:461
@ RPT_SEVERITY_WARNING
@ NET
This item represents a net.
std::vector< std::string > decal_names
std::vector< PARTTYPE_PIN > pins
std::map< std::string, std::string > fields
std::vector< GATE_DEF > gates
std::vector< SIGPIN > sigpins
std::string decal_name
CAEDECAL the placed gate draws, empty when unresolved.
std::vector< PART_ATTRIBUTE > attributes
std::vector< PIN_OVERRIDE > pin_overrides
std::vector< SYMBOL_GRAPHIC > graphics
std::vector< SYMBOL_PIN > pins
std::vector< CAEDECAL_ATTR > attrs
std::vector< SYMBOL_TEXT > texts
std::vector< GRAPHIC_POINT > points
std::vector< SOURCE_PROPERTY > properties
std::vector< MODEL_BUS_ENTRY > entries
std::vector< SOURCE_STRING > aliases
std::vector< NET_REFERENCE > memberNets
std::vector< SOURCE_POINT > vertices
std::vector< MODEL_CONNECTION_ENDPOINT > endpoints
std::vector< SOURCE_POINT > vertices
MODEL_TEXT_PRESENTATION presentation
std::vector< SOURCE_PROPERTY > properties
std::vector< DEFINITION_REFERENCE > decalGroupMembers
std::vector< MODEL_CONNECTOR_PIN > connectorPins
std::vector< PIN_REFERENCE > pins
std::vector< MODEL_GATE_PIN > logicalPins
std::vector< SOURCE_PROPERTY > properties
std::vector< SOURCE_POINT > points
std::vector< MODEL_CONNECTION > connections
std::vector< MODEL_SIGNAL_PIN > signalPins
std::vector< SOURCE_PROPERTY > properties
std::vector< PLACED_PIN_REFERENCE > pins
std::vector< SOURCE_PROPERTY > properties
std::optional< GATE_REFERENCE > gate
std::optional< SHEET_REFERENCE > parent
std::vector< SOURCE_PROPERTY > properties
std::vector< MODEL_FIELD > titleBlockFields
std::vector< MODEL_GRAPHIC > border
std::vector< MODEL_PIN_DEFINITION > pins
std::vector< MODEL_GRAPHIC > graphics
std::vector< MODEL_SYMBOL_DEFINITION > definitions
std::vector< MODEL_JUNCTION > junctions
std::vector< MODEL_PAGE_GRAPHIC > graphics
std::vector< MODEL_WORKSHEET > worksheets
std::vector< MODEL_LABEL > labels
std::vector< MODEL_EMBEDDED_IMAGE > images
std::vector< MODEL_PART_TYPE > partTypes
std::vector< MODEL_SHEET > sheets
std::vector< MODEL_PLACEMENT > placements
std::vector< PARSER_DIAGNOSTIC > diagnostics
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
static std::vector< uint8_t > bytes(const std::string &aText)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE_MESSAGE(dc.CacheTableInfo(table, { "part_id", "val" }), dc.GetLastError())
BOOST_REQUIRE(res.columns()==1)
BOOST_CHECK_EQUAL_COLLECTIONS(mixed.begin(), mixed.end(), expMixed.begin(), expMixed.end())
BOOST_AUTO_TEST_SUITE_END()
KIBIS_MODEL * model
KIBIS_PIN * pin
VECTOR3I expected(15, 30, 45)
BOOST_AUTO_TEST_CASE(EmbeddedOleImages)
BOOST_TEST_CONTEXT("Test Clearance")
SHAPE_CIRCLE circle(c.m_circle_center, c.m_circle_radius)
int actual
wxString result
Test unit parsing edge cases and error handling.