altium-format 0.1.7

Core altium-cli library for reading and writing Altium Designer files.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
//! Base primitive trait and SchRecord dispatch enum.

use crate::error::Result;
use crate::traits::{FromParams, ToParams};
use crate::tree::TreeRecord;
use crate::types::{
    CoordPoint, CoordRect, ParameterCollection, coord_to_dxp_frac, dxp_frac_to_coord,
};

/// Base trait for all schematic primitives.
pub trait SchPrimitive: Sized {
    /// Record type ID for this primitive.
    const RECORD_ID: i32;

    /// Import primitive data from parameters.
    fn import_from_params(params: &ParameterCollection) -> Result<Self>;

    /// Export primitive data to parameters.
    fn export_to_params(&self) -> ParameterCollection;

    /// Get the owner index (index of parent primitive in the list).
    fn owner_index(&self) -> i32;

    /// Get the location (if applicable).
    fn location(&self) -> Option<CoordPoint> {
        None
    }

    /// Get the record type name for diagnostics.
    fn record_type_name(&self) -> &'static str;

    /// Get a property value by name (for generic queries).
    fn get_property(&self, _name: &str) -> Option<String> {
        None
    }

    /// Calculate the bounding rectangle.
    fn calculate_bounds(&self) -> CoordRect;
}

/// Common fields shared by all schematic primitives.
#[derive(Debug, Clone)]
pub struct SchPrimitiveBase {
    /// Index of owner primitive in the component's primitive list.
    pub owner_index: i32,
    /// Whether the primitive is not accessible.
    pub is_not_accessible: bool,
    /// Owner part ID (for multi-part symbols).
    pub owner_part_id: Option<i32>,
    /// Owner display mode (for symbols with multiple display modes).
    pub owner_part_display_mode: Option<i32>,
    /// Whether the primitive is graphically locked.
    pub graphically_locked: bool,
}

impl Default for SchPrimitiveBase {
    fn default() -> Self {
        Self {
            owner_index: -1, // -1 indicates no owner
            is_not_accessible: false,
            owner_part_id: None, // None serializes as 1 for pins; overridden to Some(-1) for components
            owner_part_display_mode: None,
            graphically_locked: false,
        }
    }
}

impl SchPrimitiveBase {
    /// Import base fields from parameters.
    pub fn import_from_params(params: &ParameterCollection) -> Self {
        SchPrimitiveBase {
            owner_index: params
                .get("OWNERINDEX")
                .map(|v| v.as_int_or(0))
                .unwrap_or(0),
            is_not_accessible: params
                .get("ISNOTACCESIBLE")
                .map(|v| v.as_bool_or(false))
                .unwrap_or(false),
            owner_part_id: params.get("OWNERPARTID").map(|v| v.as_int_or(0)),
            owner_part_display_mode: params.get("OWNERPARTDISPLAYMODE").map(|v| v.as_int_or(0)),
            graphically_locked: params
                .get("GRAPHICALLYLOCKED")
                .map(|v| v.as_bool_or(false))
                .unwrap_or(false),
        }
    }

    /// Export base fields to parameters.
    pub fn export_to_params(&self, params: &mut ParameterCollection) {
        params.add_int("OWNERINDEX", self.owner_index);
        params.add_bool("ISNOTACCESIBLE", self.is_not_accessible);
        if let Some(part_id) = self.owner_part_id {
            params.add_int("OWNERPARTID", part_id);
        }
        if let Some(display_mode) = self.owner_part_display_mode {
            params.add_int("OWNERPARTDISPLAYMODE", display_mode);
        }
        params.add_bool("GRAPHICALLYLOCKED", self.graphically_locked);
    }

    /// Get the owner index.
    pub fn owner_index(&self) -> i32 {
        self.owner_index
    }

    /// Set the owner index.
    pub fn set_owner_index(&mut self, index: i32) {
        self.owner_index = index;
    }
}

// Trait implementations for derive macro support
impl FromParams for SchPrimitiveBase {
    fn from_params(params: &ParameterCollection) -> Result<Self> {
        Ok(Self::import_from_params(params))
    }
}

impl ToParams for SchPrimitiveBase {
    fn append_to_params(&self, params: &mut ParameterCollection) {
        self.export_to_params(params);
    }
}

/// Common fields for graphical schematic objects (extends SchPrimitiveBase).
#[derive(Debug, Clone, Default)]
pub struct SchGraphicalBase {
    /// Base primitive fields.
    pub base: SchPrimitiveBase,
    /// Location of the object.
    pub location_x: i32,
    pub location_y: i32,
    /// Color (Win32 COLORREF).
    pub color: i32,
    /// Area/fill color (Win32 COLORREF).
    pub area_color: i32,
}

impl SchGraphicalBase {
    /// Standard colors from Altium schematics (Win32 COLORREF format: 0xBBGGRR).
    pub const COLOR_BLUE: i32 = 128; // 0x000080 - Dark blue for components, junctions, ports
    pub const COLOR_RED: i32 = 8388608; // 0x800000 - Dark red for wires, text, labels
    pub const COLOR_LIGHT_CYAN: i32 = 11599871; // 0xB0FFFF - Light cyan for component fill

    /// Create a new SchGraphicalBase with default colors for graphical objects (blue).
    ///
    /// Use this for components, junctions, ports, and other graphical primitives.
    pub fn new_graphical() -> Self {
        Self {
            base: SchPrimitiveBase {
                owner_index: -1,
                is_not_accessible: false,
                owner_part_id: Some(-1),
                owner_part_display_mode: None,
                graphically_locked: false,
            },
            location_x: 0,
            location_y: 0,
            color: Self::COLOR_BLUE,
            area_color: Self::COLOR_LIGHT_CYAN,
        }
    }

    /// Create a new SchGraphicalBase with default colors for wires and text (red).
    ///
    /// Use this for wires, labels, and text objects.
    pub fn new_wire_or_text() -> Self {
        Self {
            base: SchPrimitiveBase {
                owner_index: -1,
                is_not_accessible: false,
                owner_part_id: Some(-1),
                owner_part_display_mode: None,
                graphically_locked: false,
            },
            location_x: 0,
            location_y: 0,
            color: Self::COLOR_RED,
            area_color: 0,
        }
    }

    /// Import graphical fields from parameters.
    pub fn import_from_params(params: &ParameterCollection) -> Self {
        let base = SchPrimitiveBase::import_from_params(params);

        let loc_x = params
            .get("LOCATION.X")
            .map(|v| v.as_int_or(0))
            .unwrap_or(0);
        let loc_x_frac = params
            .get("LOCATION.X_FRAC")
            .map(|v| v.as_int_or(0))
            .unwrap_or(0);
        let loc_y = params
            .get("LOCATION.Y")
            .map(|v| v.as_int_or(0))
            .unwrap_or(0);
        let loc_y_frac = params
            .get("LOCATION.Y_FRAC")
            .map(|v| v.as_int_or(0))
            .unwrap_or(0);

        SchGraphicalBase {
            base,
            location_x: dxp_frac_to_coord(loc_x, loc_x_frac),
            location_y: dxp_frac_to_coord(loc_y, loc_y_frac),
            color: params.get("COLOR").map(|v| v.as_int_or(0)).unwrap_or(0),
            area_color: params.get("AREACOLOR").map(|v| v.as_int_or(0)).unwrap_or(0),
        }
    }

    /// Export graphical fields to parameters.
    pub fn export_to_params(&self, params: &mut ParameterCollection) {
        self.base.export_to_params(params);

        let (x, x_frac) = coord_to_dxp_frac(self.location_x);
        let (y, y_frac) = coord_to_dxp_frac(self.location_y);
        params.add_int("LOCATION.X", x);
        params.add_int("LOCATION.X_FRAC", x_frac);
        params.add_int("LOCATION.Y", y);
        params.add_int("LOCATION.Y_FRAC", y_frac);
        params.add_int("COLOR", self.color);
        params.add_int("AREACOLOR", self.area_color);
    }

    /// Get the owner index (delegates to base).
    pub fn owner_index(&self) -> i32 {
        self.base.owner_index
    }

    /// Set the owner index (delegates to base).
    pub fn set_owner_index(&mut self, index: i32) {
        self.base.owner_index = index;
    }
}

// Trait implementations for derive macro support
impl FromParams for SchGraphicalBase {
    fn from_params(params: &ParameterCollection) -> Result<Self> {
        Ok(Self::import_from_params(params))
    }
}

impl ToParams for SchGraphicalBase {
    fn append_to_params(&self, params: &mut ParameterCollection) {
        self.export_to_params(params);
    }
}

/// Dispatch enum containing all schematic record types.
#[derive(Debug, Clone)]
pub enum SchRecord {
    Component(super::SchComponent),
    Pin(super::SchPin),
    Symbol(super::SchSymbol),
    Label(super::SchLabel),
    Bezier(super::SchBezier),
    Polyline(super::SchPolyline),
    Polygon(super::SchPolygon),
    Ellipse(super::SchEllipse),
    Pie(super::SchPie),
    EllipticalArc(super::SchEllipticalArc),
    Arc(super::SchArc),
    Line(super::SchLine),
    Rectangle(super::SchRectangle),
    PowerObject(super::SchPowerObject),
    Port(super::SchPort),
    NoErc(super::SchNoErc),
    NetLabel(super::SchNetLabel),
    Bus(super::SchBus),
    Wire(super::SchWire),
    TextFrame(super::SchTextFrame),
    TextFrameVariant(super::SchTextFrameVariant),
    Junction(super::SchJunction),
    Image(super::SchImage),
    SheetHeader(super::SchSheetHeader),
    Designator(super::SchDesignator),
    BusEntry(super::SchBusEntry),
    Parameter(super::SchParameter),
    WarningSign(super::SchWarningSign),
    ImplementationList(super::SchImplementationList),
    Implementation(super::SchImplementation),
    MapDefinerList(super::SchMapDefinerList),
    MapDefiner(super::SchMapDefiner),
    ImplementationParameters(super::SchImplementationParameters),
    /// Unknown record type - stores raw parameters.
    Unknown {
        record_id: i32,
        params: ParameterCollection,
    },
}

impl SchRecord {
    /// Create a record from parameters by dispatching on RECORD type.
    pub fn from_params(params: &ParameterCollection) -> Result<Self> {
        let record_id = params.get("RECORD").map(|v| v.as_int_or(-1)).unwrap_or(-1);

        let record = match record_id {
            1 => SchRecord::Component(super::SchComponent::import_from_params(params)?),
            2 => SchRecord::Pin(super::SchPin::import_from_params(params)?),
            3 => SchRecord::Symbol(super::SchSymbol::import_from_params(params)?),
            4 => SchRecord::Label(super::SchLabel::import_from_params(params)?),
            5 => SchRecord::Bezier(super::SchBezier::import_from_params(params)?),
            6 => SchRecord::Polyline(super::SchPolyline::import_from_params(params)?),
            7 => SchRecord::Polygon(super::SchPolygon::import_from_params(params)?),
            8 => SchRecord::Ellipse(super::SchEllipse::import_from_params(params)?),
            9 => SchRecord::Pie(super::SchPie::import_from_params(params)?),
            11 => SchRecord::EllipticalArc(super::SchEllipticalArc::import_from_params(params)?),
            12 => SchRecord::Arc(super::SchArc::import_from_params(params)?),
            13 => SchRecord::Line(super::SchLine::import_from_params(params)?),
            14 => SchRecord::Rectangle(super::SchRectangle::import_from_params(params)?),
            17 => SchRecord::PowerObject(super::SchPowerObject::import_from_params(params)?),
            18 => SchRecord::Port(super::SchPort::import_from_params(params)?),
            22 => SchRecord::NoErc(super::SchNoErc::import_from_params(params)?),
            25 => SchRecord::NetLabel(super::SchNetLabel::import_from_params(params)?),
            26 => SchRecord::Bus(super::SchBus::import_from_params(params)?),
            27 => SchRecord::Wire(super::SchWire::import_from_params(params)?),
            28 => SchRecord::TextFrame(super::SchTextFrame::import_from_params(params)?),
            29 => SchRecord::Junction(super::SchJunction::import_from_params(params)?),
            30 => SchRecord::Image(super::SchImage::import_from_params(params)?),
            31 => SchRecord::SheetHeader(super::SchSheetHeader::import_from_params(params)?),
            34 => SchRecord::Designator(super::SchDesignator::import_from_params(params)?),
            37 => SchRecord::BusEntry(super::SchBusEntry::import_from_params(params)?),
            41 => SchRecord::Parameter(super::SchParameter::import_from_params(params)?),
            43 => SchRecord::WarningSign(super::SchWarningSign::import_from_params(params)?),
            44 => SchRecord::ImplementationList(super::SchImplementationList::import_from_params(
                params,
            )?),
            45 => SchRecord::Implementation(super::SchImplementation::import_from_params(params)?),
            46 => SchRecord::MapDefinerList(super::SchMapDefinerList::import_from_params(params)?),
            47 => SchRecord::MapDefiner(super::SchMapDefiner::import_from_params(params)?),
            48 => SchRecord::ImplementationParameters(
                super::SchImplementationParameters::import_from_params(params)?,
            ),
            209 => {
                SchRecord::TextFrameVariant(super::SchTextFrameVariant::import_from_params(params)?)
            }
            _ => SchRecord::Unknown {
                record_id,
                params: params.clone(),
            },
        };

        Ok(record)
    }

    /// Get the record type ID.
    pub fn record_id(&self) -> i32 {
        match self {
            SchRecord::Component(_) => 1,
            SchRecord::Pin(_) => 2,
            SchRecord::Symbol(_) => 3,
            SchRecord::Label(_) => 4,
            SchRecord::Bezier(_) => 5,
            SchRecord::Polyline(_) => 6,
            SchRecord::Polygon(_) => 7,
            SchRecord::Ellipse(_) => 8,
            SchRecord::Pie(_) => 9,
            SchRecord::EllipticalArc(_) => 11,
            SchRecord::Arc(_) => 12,
            SchRecord::Line(_) => 13,
            SchRecord::Rectangle(_) => 14,
            SchRecord::PowerObject(_) => 17,
            SchRecord::Port(_) => 18,
            SchRecord::NoErc(_) => 22,
            SchRecord::NetLabel(_) => 25,
            SchRecord::Bus(_) => 26,
            SchRecord::Wire(_) => 27,
            SchRecord::TextFrame(_) => 28,
            SchRecord::Junction(_) => 29,
            SchRecord::Image(_) => 30,
            SchRecord::SheetHeader(_) => 31,
            SchRecord::Designator(_) => 34,
            SchRecord::BusEntry(_) => 37,
            SchRecord::Parameter(_) => 41,
            SchRecord::WarningSign(_) => 43,
            SchRecord::ImplementationList(_) => 44,
            SchRecord::Implementation(_) => 45,
            SchRecord::MapDefinerList(_) => 46,
            SchRecord::MapDefiner(_) => 47,
            SchRecord::ImplementationParameters(_) => 48,
            SchRecord::TextFrameVariant(_) => 209,
            SchRecord::Unknown { record_id, .. } => *record_id,
        }
    }

    /// Get the owner index of this record.
    pub fn owner_index(&self) -> i32 {
        match self {
            SchRecord::Component(r) => r.owner_index(),
            SchRecord::Pin(r) => r.owner_index(),
            SchRecord::Symbol(r) => r.owner_index(),
            SchRecord::Label(r) => r.owner_index(),
            SchRecord::Bezier(r) => r.owner_index(),
            SchRecord::Polyline(r) => r.owner_index(),
            SchRecord::Polygon(r) => r.owner_index(),
            SchRecord::Ellipse(r) => r.owner_index(),
            SchRecord::Pie(r) => r.owner_index(),
            SchRecord::EllipticalArc(r) => r.owner_index(),
            SchRecord::Arc(r) => r.owner_index(),
            SchRecord::Line(r) => r.owner_index(),
            SchRecord::Rectangle(r) => r.owner_index(),
            SchRecord::PowerObject(r) => r.owner_index(),
            SchRecord::Port(r) => r.owner_index(),
            SchRecord::NoErc(r) => r.owner_index(),
            SchRecord::NetLabel(r) => r.owner_index(),
            SchRecord::Bus(r) => r.owner_index(),
            SchRecord::Wire(r) => r.owner_index(),
            SchRecord::TextFrame(r) => r.owner_index(),
            SchRecord::TextFrameVariant(r) => r.owner_index(),
            SchRecord::Junction(r) => r.owner_index(),
            SchRecord::Image(r) => r.owner_index(),
            SchRecord::SheetHeader(r) => r.owner_index(),
            SchRecord::Designator(r) => r.owner_index(),
            SchRecord::BusEntry(r) => r.owner_index(),
            SchRecord::Parameter(r) => r.owner_index(),
            SchRecord::WarningSign(r) => r.owner_index(),
            SchRecord::ImplementationList(r) => r.owner_index(),
            SchRecord::Implementation(r) => r.owner_index(),
            SchRecord::MapDefinerList(r) => r.owner_index(),
            SchRecord::MapDefiner(r) => r.owner_index(),
            SchRecord::ImplementationParameters(r) => r.owner_index(),
            SchRecord::Unknown { params, .. } => params
                .get("OWNERINDEX")
                .map(|v| v.as_int_or(0))
                .unwrap_or(0),
        }
    }

    /// Set the owner index for this record.
    pub fn set_owner_index(&mut self, index: i32) {
        match self {
            SchRecord::Component(r) => r.graphical.base.set_owner_index(index),
            SchRecord::Pin(r) => r.graphical.set_owner_index(index),
            SchRecord::Symbol(r) => r.graphical.set_owner_index(index),
            SchRecord::Label(r) => r.graphical.set_owner_index(index),
            SchRecord::Bezier(r) => r.graphical.set_owner_index(index),
            SchRecord::Polyline(r) => r.graphical.set_owner_index(index),
            SchRecord::Polygon(r) => r.graphical.set_owner_index(index),
            SchRecord::Ellipse(r) => r.graphical.set_owner_index(index),
            SchRecord::Pie(r) => r.graphical.set_owner_index(index),
            SchRecord::EllipticalArc(r) => r.graphical.set_owner_index(index),
            SchRecord::Arc(r) => r.graphical.set_owner_index(index),
            SchRecord::Line(r) => r.graphical.set_owner_index(index),
            SchRecord::Rectangle(r) => r.graphical.set_owner_index(index),
            SchRecord::PowerObject(r) => r.graphical.set_owner_index(index),
            SchRecord::Port(r) => r.graphical.set_owner_index(index),
            SchRecord::NoErc(r) => r.graphical.set_owner_index(index),
            SchRecord::NetLabel(r) => r.label.graphical.set_owner_index(index),
            SchRecord::Bus(r) => r.graphical.set_owner_index(index),
            SchRecord::Wire(r) => r.graphical.set_owner_index(index),
            SchRecord::TextFrame(r) => r.graphical.set_owner_index(index),
            SchRecord::TextFrameVariant(r) => r.graphical.set_owner_index(index),
            SchRecord::Junction(r) => r.graphical.set_owner_index(index),
            SchRecord::Image(r) => r.graphical.set_owner_index(index),
            SchRecord::SheetHeader(r) => r.base.set_owner_index(index),
            SchRecord::Designator(r) => r.param.label.graphical.set_owner_index(index),
            SchRecord::BusEntry(r) => r.graphical.set_owner_index(index),
            SchRecord::Parameter(r) => r.label.graphical.set_owner_index(index),
            SchRecord::WarningSign(r) => r.graphical.set_owner_index(index),
            SchRecord::ImplementationList(r) => r.base.set_owner_index(index),
            SchRecord::Implementation(r) => r.base.set_owner_index(index),
            SchRecord::MapDefinerList(r) => r.base.set_owner_index(index),
            SchRecord::MapDefiner(r) => r.base.set_owner_index(index),
            SchRecord::ImplementationParameters(r) => r.base.set_owner_index(index),
            SchRecord::Unknown { params, .. } => {
                params.add_int("OWNERINDEX", index);
            }
        }
    }

    /// Get location for all record types.
    pub fn location(&self) -> Option<CoordPoint> {
        match self {
            SchRecord::Component(r) => Some(CoordPoint::from_raw(
                r.graphical.location_x,
                r.graphical.location_y,
            )),
            SchRecord::Pin(r) => Some(CoordPoint::from_raw(
                r.graphical.location_x,
                r.graphical.location_y,
            )),
            SchRecord::Symbol(r) => Some(CoordPoint::from_raw(
                r.graphical.location_x,
                r.graphical.location_y,
            )),
            SchRecord::Label(r) => Some(CoordPoint::from_raw(
                r.graphical.location_x,
                r.graphical.location_y,
            )),
            SchRecord::Bezier(r) => Some(CoordPoint::from_raw(
                r.graphical.location_x,
                r.graphical.location_y,
            )),
            SchRecord::Polyline(r) => Some(CoordPoint::from_raw(
                r.graphical.location_x,
                r.graphical.location_y,
            )),
            SchRecord::Polygon(r) => Some(CoordPoint::from_raw(
                r.graphical.location_x,
                r.graphical.location_y,
            )),
            SchRecord::Ellipse(r) => Some(CoordPoint::from_raw(
                r.graphical.location_x,
                r.graphical.location_y,
            )),
            SchRecord::Pie(r) => Some(CoordPoint::from_raw(
                r.graphical.location_x,
                r.graphical.location_y,
            )),
            SchRecord::EllipticalArc(r) => Some(CoordPoint::from_raw(
                r.graphical.location_x,
                r.graphical.location_y,
            )),
            SchRecord::Arc(r) => Some(CoordPoint::from_raw(
                r.graphical.location_x,
                r.graphical.location_y,
            )),
            SchRecord::Line(r) => Some(CoordPoint::from_raw(
                r.graphical.location_x,
                r.graphical.location_y,
            )),
            SchRecord::Rectangle(r) => Some(CoordPoint::from_raw(
                r.graphical.location_x,
                r.graphical.location_y,
            )),
            SchRecord::PowerObject(r) => Some(CoordPoint::from_raw(
                r.graphical.location_x,
                r.graphical.location_y,
            )),
            SchRecord::Port(r) => Some(CoordPoint::from_raw(
                r.graphical.location_x,
                r.graphical.location_y,
            )),
            SchRecord::NoErc(r) => Some(CoordPoint::from_raw(
                r.graphical.location_x,
                r.graphical.location_y,
            )),
            SchRecord::NetLabel(r) => Some(CoordPoint::from_raw(
                r.label.graphical.location_x,
                r.label.graphical.location_y,
            )),
            SchRecord::Bus(r) => Some(CoordPoint::from_raw(
                r.graphical.location_x,
                r.graphical.location_y,
            )),
            SchRecord::Wire(r) => Some(CoordPoint::from_raw(
                r.graphical.location_x,
                r.graphical.location_y,
            )),
            SchRecord::TextFrame(r) => Some(CoordPoint::from_raw(
                r.graphical.location_x,
                r.graphical.location_y,
            )),
            SchRecord::TextFrameVariant(r) => Some(CoordPoint::from_raw(
                r.graphical.location_x,
                r.graphical.location_y,
            )),
            SchRecord::Junction(r) => Some(CoordPoint::from_raw(
                r.graphical.location_x,
                r.graphical.location_y,
            )),
            SchRecord::Image(r) => Some(CoordPoint::from_raw(
                r.graphical.location_x,
                r.graphical.location_y,
            )),
            SchRecord::SheetHeader(_) => None,
            SchRecord::Designator(r) => Some(CoordPoint::from_raw(
                r.param.label.graphical.location_x,
                r.param.label.graphical.location_y,
            )),
            SchRecord::BusEntry(r) => Some(CoordPoint::from_raw(
                r.graphical.location_x,
                r.graphical.location_y,
            )),
            SchRecord::Parameter(r) => Some(CoordPoint::from_raw(
                r.label.graphical.location_x,
                r.label.graphical.location_y,
            )),
            SchRecord::WarningSign(r) => Some(CoordPoint::from_raw(
                r.graphical.location_x,
                r.graphical.location_y,
            )),
            SchRecord::ImplementationList(_) => None,
            SchRecord::Implementation(_) => None,
            SchRecord::MapDefinerList(_) => None,
            SchRecord::MapDefiner(_) => None,
            SchRecord::ImplementationParameters(_) => None,
            SchRecord::Unknown { .. } => None,
        }
    }

    /// Get record type name for diagnostics.
    pub fn record_type_name(&self) -> &'static str {
        match self {
            SchRecord::Component(_) => "Component",
            SchRecord::Pin(_) => "Pin",
            SchRecord::Symbol(_) => "Symbol",
            SchRecord::Label(_) => "Label",
            SchRecord::Bezier(_) => "Bezier",
            SchRecord::Polyline(_) => "Polyline",
            SchRecord::Polygon(_) => "Polygon",
            SchRecord::Ellipse(_) => "Ellipse",
            SchRecord::Pie(_) => "Pie",
            SchRecord::EllipticalArc(_) => "EllipticalArc",
            SchRecord::Arc(_) => "Arc",
            SchRecord::Line(_) => "Line",
            SchRecord::Rectangle(_) => "Rectangle",
            SchRecord::PowerObject(_) => "PowerObject",
            SchRecord::Port(_) => "Port",
            SchRecord::NoErc(_) => "NoErc",
            SchRecord::NetLabel(_) => "NetLabel",
            SchRecord::Bus(_) => "Bus",
            SchRecord::Wire(_) => "Wire",
            SchRecord::TextFrame(_) => "TextFrame",
            SchRecord::TextFrameVariant(_) => "TextFrameVariant",
            SchRecord::Junction(_) => "Junction",
            SchRecord::Image(_) => "Image",
            SchRecord::SheetHeader(_) => "SheetHeader",
            SchRecord::Designator(_) => "Designator",
            SchRecord::BusEntry(_) => "BusEntry",
            SchRecord::Parameter(_) => "Parameter",
            SchRecord::WarningSign(_) => "WarningSign",
            SchRecord::ImplementationList(_) => "ImplementationList",
            SchRecord::Implementation(_) => "Implementation",
            SchRecord::MapDefinerList(_) => "MapDefinerList",
            SchRecord::MapDefiner(_) => "MapDefiner",
            SchRecord::ImplementationParameters(_) => "ImplementationParameters",
            SchRecord::Unknown { .. } => "Unknown",
        }
    }

    /// Get property value by name for generic queries.
    pub fn get_property(&self, name: &str) -> Option<String> {
        match self {
            SchRecord::Component(r) => match name {
                "LIBREFERENCE" => Some(r.lib_reference.clone()),
                "COMPONENTDESCRIPTION" => Some(r.component_description.clone()),
                "UNIQUEID" => Some(r.unique_id.clone()),
                _ => None,
            },
            SchRecord::Pin(r) => match name {
                "NAME" => Some(r.name.clone()),
                "DESIGNATOR" => Some(r.designator.clone()),
                "DESCRIPTION" => Some(r.description.clone()),
                _ => None,
            },
            SchRecord::Label(r) => match name {
                "TEXT" => Some(r.text.clone()),
                _ => None,
            },
            SchRecord::PowerObject(r) => match name {
                "TEXT" => Some(r.text.clone()),
                _ => None,
            },
            SchRecord::Port(r) => match name {
                "NAME" => Some(r.name.clone()),
                _ => None,
            },
            SchRecord::NetLabel(r) => match name {
                "TEXT" => Some(r.label.text.clone()),
                _ => None,
            },
            SchRecord::TextFrame(r) => match name {
                "TEXT" => Some(r.text.clone()),
                _ => None,
            },
            SchRecord::TextFrameVariant(r) => match name {
                "TEXT" => Some(r.text.clone()),
                _ => None,
            },
            SchRecord::Parameter(r) => match name {
                "NAME" => Some(r.name.clone()),
                "TEXT" => Some(r.label.text.clone()),
                _ => None,
            },
            SchRecord::Designator(r) => match name {
                "NAME" => Some(r.param.name.clone()),
                "TEXT" => Some(r.param.label.text.clone()),
                _ => None,
            },
            SchRecord::WarningSign(r) => match name {
                "NAME" => Some(r.name.clone()),
                _ => None,
            },
            SchRecord::Implementation(r) => match name {
                "MODELNAME" => Some(r.model_name.clone()),
                "MODELTYPE" => Some(r.model_type.clone()),
                _ => None,
            },
            _ => None,
        }
    }
}

/// TreeRecord implementation for SchRecord enables tree structure support.
impl TreeRecord for SchRecord {
    fn owner_index(&self) -> i32 {
        self.owner_index()
    }

    fn set_owner_index(&mut self, index: i32) {
        self.set_owner_index(index)
    }
}