Skip to main content

virtuoso_cli/client/
schematic_ops.rs

1use crate::client::bridge::escape_skill_string;
2
3#[derive(Default)]
4pub struct SchematicOps;
5
6impl SchematicOps {
7    pub fn new() -> Self {
8        Self
9    }
10
11    pub fn create_instance(
12        &self,
13        lib: &str,
14        cell: &str,
15        view: &str,
16        name: &str,
17        origin: (i64, i64),
18    ) -> String {
19        let lib = escape_skill_string(lib);
20        let cell = escape_skill_string(cell);
21        let view = escape_skill_string(view);
22        let name = escape_skill_string(name);
23        let (x, y) = origin;
24        format!(
25            r#"let((cv master inst) cv = RB_SCH_CV master = dbOpenCellViewByType("{lib}" "{cell}" "{view}" nil "r") inst = dbCreateInst(cv master "{name}" list({x} {y}) "R0" 1) inst)"#
26        )
27    }
28
29    pub fn create_wire(&self, points: &[(i64, i64)], layer: &str, net_name: &str) -> String {
30        let layer = escape_skill_string(layer);
31        let net_name = escape_skill_string(net_name);
32        let pts: String = points
33            .iter()
34            .map(|(x, y)| format!("list({x} {y})"))
35            .collect::<Vec<_>>()
36            .join(" ");
37        format!(
38            r#"let((cv) cv = RB_SCH_CV dbCreateWire(cv dbMakeNet(cv "{net_name}") dbFindLayerByName(cv "{layer}") list({pts})))"#
39        )
40    }
41
42    pub fn create_wire_between_terms(
43        &self,
44        inst1: &str,
45        _term1: &str,
46        inst2: &str,
47        _term2: &str,
48        net_name: &str,
49    ) -> String {
50        let inst1 = escape_skill_string(inst1);
51        let inst2 = escape_skill_string(inst2);
52        let net_name = escape_skill_string(net_name);
53        format!(
54            r#"let((cv net) cv = RB_SCH_CV net = dbMakeNet(cv "{net_name}") dbCreateWire(net dbFindTermByName(cv "{inst1}") dbFindTermByName(cv "{inst2}")))"#
55        )
56    }
57
58    pub fn create_wire_label(&self, net_name: &str, origin: (i64, i64)) -> String {
59        let net_name = escape_skill_string(net_name);
60        let (x, y) = origin;
61        format!(
62            r#"let((cv net) cv = RB_SCH_CV net = dbFindNetByName(cv "{net_name}") when(net dbCreateLabel(cv net "{net_name}" list({x} {y}) "centerCenter" "R0" "stick" 0.0625)))"#
63        )
64    }
65
66    pub fn create_pin(&self, net_name: &str, _pin_type: &str, origin: (i64, i64)) -> String {
67        let net_name = escape_skill_string(net_name);
68        let (x, y) = origin;
69        format!(
70            r#"let((cv net pinInst) cv = RB_SCH_CV net = dbMakeNet(cv "{net_name}") pinInst = dbCreateInst(cv dbOpenCellViewByType("basic" "ipin" "symbol" nil "r") "PIN_{net_name}" list({x} {y}) "R0" 1) dbCreatePin(net pinInst))"#
71        )
72    }
73
74    pub fn check(&self) -> String {
75        r#"let((cv) cv = RB_SCH_CV schCheck(cv))"#.into()
76    }
77
78    pub fn open_cellview(&self, lib: &str, cell: &str, view: &str) -> String {
79        let lib = escape_skill_string(lib);
80        let cell = escape_skill_string(cell);
81        let view = escape_skill_string(view);
82        // dbOpenCellViewByType with viewType="schematic" mode="a":
83        //   creates cellview if absent, opens for editing (non-interactive)
84        // Store in RB_SCH_CV global for use by subsequent commands
85        format!(r#"RB_SCH_CV = dbOpenCellViewByType("{lib}" "{cell}" "{view}" "schematic" "a")"#)
86    }
87
88    pub fn save(&self) -> String {
89        r#"let((cv) cv = RB_SCH_CV dbSave(cv))"#.into()
90    }
91
92    pub fn set_instance_param(&self, inst_name: &str, param: &str, value: &str) -> String {
93        let inst_name = escape_skill_string(inst_name);
94        let param = escape_skill_string(param);
95        let value = escape_skill_string(value);
96        format!(
97            r#"let((cv inst) cv = RB_SCH_CV inst = car(setof(i cv~>instances i~>name == "{inst_name}")) when(inst dbReplaceProp(inst "{param}" "string" "{value}")))"#
98        )
99    }
100
101    // ── Read operations ──────────────────────────────────────────────
102
103    /// List all instances in the open cellview. Returns JSON array via sprintf.
104    pub fn list_instances(&self) -> String {
105        r#"let((cv out sep lib cell) cv = RB_SCH_CV out = "[" sep = "" foreach(inst cv~>instances lib = if(inst~>master inst~>master~>libName "?") cell = if(inst~>master inst~>master~>cellName "?") out = strcat(out sep sprintf(nil "{\"name\":\"%s\",\"master\":\"%s/%s\",\"x\":%g,\"y\":%g}" inst~>name lib cell car(inst~>xy) cadr(inst~>xy))) sep = ",") strcat(out "]"))"#.into()
106    }
107
108    /// List all nets in the open cellview. Returns JSON array.
109    pub fn list_nets(&self) -> String {
110        r#"let((cv out sep) cv = RB_SCH_CV out = "[" sep = "" foreach(net cv~>nets out = strcat(out sep sprintf(nil "\"%s\"" net~>name)) sep = ",") strcat(out "]"))"#.into()
111    }
112
113    /// List all pins (terminals) in the open cellview. Returns JSON array.
114    pub fn list_pins(&self) -> String {
115        r#"let((cv out sep) cv = RB_SCH_CV out = "[" sep = "" foreach(term cv~>terminals out = strcat(out sep sprintf(nil "{\"name\":\"%s\",\"direction\":\"%s\"}" term~>name term~>direction)) sep = ",") strcat(out "]"))"#.into()
116    }
117
118    /// Get parameters of a specific instance. Returns JSON object.
119    pub fn get_instance_params(&self, inst_name: &str) -> String {
120        let inst_name = escape_skill_string(inst_name);
121        format!(
122            r#"let((cv inst out sep v) cv = RB_SCH_CV inst = car(setof(i cv~>instances strcmp(i~>name "{inst_name}")==0)) if(inst then out = "{{" sep = "" foreach(prop inst~>prop when(prop~>name != nil v = prop~>value when(v out = strcat(out sep sprintf(nil "\"%s\":\"%s\"" prop~>name if(stringp(v) v sprintf(nil "%L" v)))) sep = ","))) strcat(out "}}") else "null"))"#
123        )
124    }
125
126    /// Assign net name to instance terminal.
127    /// Creates a named net and connects it to the instTerm via let-scoped locals.
128    pub fn assign_net(&self, inst_name: &str, term_name: &str, net_name: &str) -> String {
129        let inst_name = escape_skill_string(inst_name);
130        let term_name = escape_skill_string(term_name);
131        let net_name = escape_skill_string(net_name);
132        format!(
133            r#"let((inst iterm net) inst = car(setof(i RB_SCH_CV~>instances strcmp(i~>name "{inst_name}")==0)) iterm = car(setof(x inst~>instTerms strcmp(x~>name "{term_name}")==0)) net = dbMakeNet(RB_SCH_CV "{net_name}") schCreateWire(RB_SCH_CV net "draw" "full" list(list(0 0) list(0 0))))"#
134        )
135    }
136}