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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
//! Schematic document (SchDoc) commands.
//!
//! High-level operations for exploring and analyzing Altium schematic documents.
use clap::Subcommand;
use serde::Serialize;
use std::path::PathBuf;
use crate::output::{self, TextFormat};
use altium_format::ops::{schdoc, schdoc_edit};
#[derive(Subcommand)]
pub enum SchDocCommands {
/// Complete design overview with component categories, power architecture, and interfaces
Overview {
/// Path to SchDoc file
path: PathBuf,
},
/// Generate bill of materials grouped by component type
Bom {
/// Path to SchDoc file
path: PathBuf,
},
/// Extract net connectivity map
Netlist {
/// Path to SchDoc file
path: PathBuf,
/// Filter by net name (supports wildcards)
#[arg(short, long)]
filter: Option<String>,
/// Minimum connections to include (default: 1)
#[arg(short, long, default_value = "1")]
min_connections: usize,
},
/// Power distribution analysis showing power rails and consumers
PowerMap {
/// Path to SchDoc file
path: PathBuf,
},
/// Block diagram showing major ICs as functional blocks
Blocks {
/// Path to SchDoc file
path: PathBuf,
/// Include passive components (capacitors, resistors, etc.)
#[arg(long)]
all: bool,
},
/// Signal flow tracing from inputs to outputs
SignalFlow {
/// Path to SchDoc file
path: PathBuf,
/// Signal name to trace
signal: String,
},
/// Multi-file hierarchical design analysis
Project {
/// Paths to SchDoc files
paths: Vec<PathBuf>,
},
/// Document info and sheet metadata
Info {
/// Path to SchDoc file
path: PathBuf,
},
/// Detailed record statistics
Stats {
/// Path to SchDoc file
path: PathBuf,
},
/// List all components
Components {
/// Path to SchDoc file
path: PathBuf,
/// Show child primitive counts
#[arg(short, long)]
verbose: bool,
},
/// Show detailed component information
Component {
/// Path to SchDoc file
path: PathBuf,
/// Component designator (e.g., U1) or index
designator: String,
/// Show all child primitives
#[arg(long)]
children: bool,
},
/// List all wires
Wires {
/// Path to SchDoc file
path: PathBuf,
/// Limit number of wires shown
#[arg(short, long)]
limit: Option<usize>,
},
/// List all net labels
Nets {
/// Path to SchDoc file
path: PathBuf,
/// Group by net name
#[arg(short, long)]
group: bool,
},
/// List all ports
Ports {
/// Path to SchDoc file
path: PathBuf,
},
/// List all power objects
Power {
/// Path to SchDoc file
path: PathBuf,
/// Group by net name
#[arg(short, long)]
group: bool,
},
/// List pins (optionally filtered by component)
Pins {
/// Path to SchDoc file
path: PathBuf,
/// Filter by component designator
#[arg(short, long)]
component: Option<String>,
},
/// List all junctions
Junctions {
/// Path to SchDoc file
path: PathBuf,
},
/// Show record hierarchy tree
Hierarchy {
/// Path to SchDoc file
path: PathBuf,
/// Maximum depth to display
#[arg(short, long)]
depth: Option<usize>,
/// Start from specific component designator
#[arg(short, long)]
from: Option<String>,
},
/// Export as JSON for LLM processing
Json {
/// Path to SchDoc file
path: PathBuf,
/// Include full component details (pins, parameters)
#[arg(long)]
full: bool,
/// Pretty-print JSON output
#[arg(long)]
pretty: bool,
},
/// Create new schematic document
Create {
/// Path to new SchDoc file
path: PathBuf,
/// Optional template file
#[arg(long)]
template: Option<PathBuf>,
},
/// Add component from library
AddComponent {
/// Path to SchDoc file
path: PathBuf,
/// Library path
#[arg(short, long)]
library: PathBuf,
/// Component name
#[arg(short, long)]
component: String,
/// X position
#[arg(short, long)]
x: String,
/// Y position
#[arg(short, long)]
y: String,
/// Designator
#[arg(short, long)]
designator: Option<String>,
},
/// Move component to new location
MoveComponent {
/// Path to SchDoc file
path: PathBuf,
/// Component designator
designator: String,
/// X position
x: String,
/// Y position
y: String,
},
/// Delete component by designator
DeleteComponent {
/// Path to SchDoc file
path: PathBuf,
/// Component designator
designator: String,
},
/// Add wire path
AddWire {
/// Path to SchDoc file
path: PathBuf,
/// Vertices as comma-separated values
vertices: String,
},
/// Delete wire by index
DeleteWire {
/// Path to SchDoc file
path: PathBuf,
/// Wire index
index: usize,
},
/// Add net label
AddNetLabel {
/// Path to SchDoc file
path: PathBuf,
/// Net label text
name: String,
/// X position
x: String,
/// Y position
y: String,
},
/// Add power port
AddPower {
/// Path to SchDoc file
path: PathBuf,
/// Power net name
name: String,
/// X position
x: String,
/// Y position
y: String,
/// Style (bar, arrow, wave, ground, etc.)
style: String,
/// Orientation (up, down, left, right)
orientation: String,
},
/// Add junction at location
AddJunction {
/// Path to SchDoc file
path: PathBuf,
/// X position
x: String,
/// Y position
y: String,
},
/// Auto-add junctions where wires cross
AddMissingJunctions {
/// Path to SchDoc file
path: PathBuf,
},
/// Add port
AddPort {
/// Path to SchDoc file
path: PathBuf,
/// Port name
name: String,
/// X position
x: String,
/// Y position
y: String,
/// I/O type (input, output, bidirectional, unspecified)
io_type: String,
},
/// Route wire between two points
RouteWire {
/// Path to SchDoc file
path: PathBuf,
/// From point or pin
from: String,
/// To point or pin
to: String,
},
/// Connect two component pins with wire
ConnectPins {
/// Path to SchDoc file
path: PathBuf,
/// From component designator
from_comp: String,
/// From pin name
from_pin: String,
/// To component designator
to_comp: String,
/// To pin name
to_pin: String,
},
/// Auto-wire a pin with a net label or power port
SmartWire {
/// Path to SchDoc file
path: PathBuf,
/// Component designator (e.g., U1)
component: String,
/// Pin designator or name
pin: String,
/// Net name for the label or power port
net: String,
/// Create a power port instead of a net label
#[arg(long)]
power: Option<String>,
/// Wire stub length in mils (default: 200)
#[arg(long, default_value = "200")]
wire_length: f64,
},
/// Batch auto-wire pins from a mapping string
SmartWireBatch {
/// Path to SchDoc file
path: PathBuf,
/// Pin mappings: "COMP.PIN=NET,COMP.PIN=NET:power_style,..."
mappings: String,
/// Wire stub length in mils (default: 200)
#[arg(long, default_value = "200")]
wire_length: f64,
},
/// Validate schematic connectivity
Validate {
/// Path to SchDoc file
path: PathBuf,
},
/// Suggest placement location for component
SuggestPlacement {
/// Path to SchDoc file
path: PathBuf,
/// Library path
#[arg(short, long)]
library: PathBuf,
/// Component name
#[arg(short, long)]
component: String,
},
/// Find unconnected pins
FindUnconnected {
/// Path to SchDoc file
path: PathBuf,
},
/// Find missing junctions where wires cross
FindMissingJunctions {
/// Path to SchDoc file
path: PathBuf,
},
/// Show netlist with connectivity details
ShowNetlist {
/// Path to SchDoc file
path: PathBuf,
/// Filter by net name
#[arg(short, long)]
filter: Option<String>,
/// Output as JSON
#[arg(long)]
json: bool,
},
/// Search component library
SearchLibrary {
/// Library path
library: PathBuf,
/// Search pattern
pattern: String,
},
}
pub fn run(cmd: &SchDocCommands, format: &str) -> Result<(), Box<dyn std::error::Error>> {
match cmd {
SchDocCommands::Overview { path } => {
let result = schdoc::cmd_overview(path)?;
output::print(&TextWrapper(result), format)?;
}
SchDocCommands::Bom { path } => {
let result = schdoc::cmd_bom(path)?;
output::print(&TextWrapper(result), format)?;
}
SchDocCommands::Netlist {
path,
filter,
min_connections,
} => {
let result = schdoc::cmd_netlist(path, filter.clone(), *min_connections)?;
output::print(&TextWrapper(result), format)?;
}
SchDocCommands::PowerMap { path } => {
let result = schdoc::cmd_power_map(path)?;
output::print(&TextWrapper(result), format)?;
}
SchDocCommands::Blocks { path, all } => {
let result = schdoc::cmd_blocks(path, *all)?;
output::print(&TextWrapper(result), format)?;
}
SchDocCommands::SignalFlow { path, signal } => {
let result = schdoc::cmd_signal_flow(path, signal)?;
output::print(&TextWrapper(result), format)?;
}
SchDocCommands::Project { paths } => {
let result = schdoc::cmd_project(paths)?;
output::print(&TextWrapper(result), format)?;
}
SchDocCommands::Info { path } => {
let result = schdoc::cmd_info(path)?;
output::print(&TextWrapper(result), format)?;
}
SchDocCommands::Stats { path } => {
let result = schdoc::cmd_stats(path)?;
output::print(&TextWrapper(result), format)?;
}
SchDocCommands::Components { path, verbose } => {
let result = schdoc::cmd_components(path, *verbose)?;
output::print(&TextWrapper(result), format)?;
}
SchDocCommands::Component {
path,
designator,
children,
} => {
let result = schdoc::cmd_component(path, designator, *children)?;
output::print(&TextWrapper(result), format)?;
}
SchDocCommands::Wires { path, limit } => {
let result = schdoc::cmd_wires(path, *limit)?;
output::print(&TextWrapper(result), format)?;
}
SchDocCommands::Nets { path, group } => {
let result = schdoc::cmd_nets(path, *group)?;
output::print(&TextWrapper(result), format)?;
}
SchDocCommands::Ports { path } => {
let result = schdoc::cmd_ports(path)?;
output::print(&TextWrapper(result), format)?;
}
SchDocCommands::Power { path, group } => {
let result = schdoc::cmd_power(path, *group)?;
output::print(&TextWrapper(result), format)?;
}
SchDocCommands::Pins { path, component } => {
let result = schdoc::cmd_pins(path, component.clone(), false)?;
output::print(&TextWrapper(result), format)?;
}
SchDocCommands::Junctions { path } => {
let result = schdoc::cmd_junctions(path)?;
output::print(&TextWrapper(result), format)?;
}
SchDocCommands::Hierarchy { path, depth, from } => {
let result = schdoc::cmd_hierarchy(path, *depth, from.clone())?;
output::print(&TextWrapper(result), format)?;
}
SchDocCommands::Json { path, full, pretty } => {
// cmd_json prints directly
schdoc::cmd_json(path, *full, *pretty).map_err(|e| e.to_string())?;
}
SchDocCommands::Create { path, template } => {
schdoc::cmd_create(path, template.clone())?;
}
SchDocCommands::AddComponent {
path,
library,
component,
x,
y,
designator,
} => {
schdoc_edit::cmd_add_component(path, library, component, x, y, designator.as_deref(), 0, None)?;
}
SchDocCommands::MoveComponent { path, designator, x, y } => {
schdoc_edit::cmd_move_component(path, designator, x, y, None)?;
}
SchDocCommands::DeleteComponent { path, designator } => {
schdoc_edit::cmd_delete_component(path, designator, None)?;
}
SchDocCommands::AddWire { path, vertices } => {
schdoc_edit::cmd_add_wire(path, vertices, None)?;
}
SchDocCommands::DeleteWire { path, index } => {
schdoc_edit::cmd_delete_wire(path, *index, None)?;
}
SchDocCommands::AddNetLabel { path, name, x, y } => {
schdoc_edit::cmd_add_net_label(path, name, x, y, None)?;
}
SchDocCommands::AddPower {
path,
name,
x,
y,
style,
orientation,
} => {
schdoc_edit::cmd_add_power(path, name, x, y, style, orientation, None)?;
}
SchDocCommands::AddJunction { path, x, y } => {
schdoc_edit::cmd_add_junction(path, x, y, None)?;
}
SchDocCommands::AddMissingJunctions { path } => {
schdoc_edit::cmd_add_missing_junctions(path, None)?;
}
SchDocCommands::AddPort {
path,
name,
x,
y,
io_type,
} => {
schdoc_edit::cmd_add_port(path, name, x, y, io_type, None)?;
}
SchDocCommands::RouteWire { path, from, to } => {
schdoc_edit::cmd_route_wire(path, from, to, None)?;
}
SchDocCommands::ConnectPins {
path,
from_comp,
from_pin,
to_comp,
to_pin,
} => {
schdoc_edit::cmd_connect_pins(path, from_comp, from_pin, to_comp, to_pin, None)?;
}
SchDocCommands::SmartWire {
path,
component,
pin,
net,
power,
wire_length,
} => {
schdoc_edit::cmd_smart_wire(
path,
component,
pin,
net,
power.as_deref(),
*wire_length,
None,
)?;
}
SchDocCommands::SmartWireBatch {
path,
mappings,
wire_length,
} => {
schdoc_edit::cmd_smart_wire_batch(path, mappings, *wire_length, None)?;
}
SchDocCommands::Validate { path } => {
let result = schdoc_edit::cmd_validate(path)?;
output::print(&TextWrapper(result), format)?;
}
SchDocCommands::SuggestPlacement {
path,
library,
component,
} => {
schdoc_edit::cmd_suggest_placement(path, library, component, None, format == "json")?;
}
SchDocCommands::FindUnconnected { path } => {
let result = schdoc_edit::cmd_find_unconnected(path)?;
output::print(&TextWrapper(result), format)?;
}
SchDocCommands::FindMissingJunctions { path } => {
let result = schdoc_edit::cmd_find_missing_junctions(path)?;
output::print(&TextWrapper(result), format)?;
}
SchDocCommands::ShowNetlist { path, filter, json } => {
schdoc_edit::cmd_show_netlist(path, filter.as_deref(), *json).map_err(|e| -> Box<dyn std::error::Error> { e.into() })?;
}
SchDocCommands::SearchLibrary {
library,
pattern,
} => {
let result = schdoc_edit::cmd_search_library(library, pattern)?;
output::print(&TextWrapper(result), format)?;
}
}
Ok(())
}
// Wrapper to add TextFormat impl for library types
#[derive(Serialize)]
#[serde(transparent)]
struct TextWrapper<T>(T);
impl<T: Serialize> TextFormat for TextWrapper<T> {
fn format_text(&self) -> String {
// Use serde_json to get a Value, then format it nicely
if let Ok(value) = serde_json::to_value(&self.0) {
format_value(&value, 0)
} else {
"Error formatting output".to_string()
}
}
}
fn format_value(value: &serde_json::Value, indent: usize) -> String {
let prefix = " ".repeat(indent);
match value {
serde_json::Value::Object(map) => {
let mut out = String::new();
for (key, val) in map {
match val {
serde_json::Value::String(s) => {
out.push_str(&format!("{}{}: {}\n", prefix, key, s));
}
serde_json::Value::Number(n) => {
out.push_str(&format!("{}{}: {}\n", prefix, key, n));
}
serde_json::Value::Bool(b) => {
out.push_str(&format!("{}{}: {}\n", prefix, key, b));
}
serde_json::Value::Null => {
out.push_str(&format!("{}{}: null\n", prefix, key));
}
serde_json::Value::Array(arr) => {
if arr.is_empty() {
out.push_str(&format!("{}{}: []\n", prefix, key));
} else {
out.push_str(&format!("{}{}:\n", prefix, key));
for item in arr {
out.push_str(&format_value(item, indent + 1));
out.push('\n');
}
}
}
serde_json::Value::Object(_) => {
out.push_str(&format!("{}{}:\n", prefix, key));
out.push_str(&format_value(val, indent + 1));
}
}
}
out
}
serde_json::Value::Array(arr) => {
let mut out = String::new();
for (i, item) in arr.iter().enumerate() {
out.push_str(&format!("{}[{}]\n", prefix, i));
out.push_str(&format_value(item, indent + 1));
}
out
}
serde_json::Value::String(s) => format!("{}{}\n", prefix, s),
serde_json::Value::Number(n) => format!("{}{}\n", prefix, n),
serde_json::Value::Bool(b) => format!("{}{}\n", prefix, b),
serde_json::Value::Null => format!("{}null\n", prefix),
}
}