Skip to main content

cimstructs/
Diagram.rs

1/// The diagram being exchanged. The coordinate system is a standard Cartesian coordinate system and the orientation attribute defines the orientation. The initial view related attributes can be used to specify an initial view with the x,y coordinates of the diagonal points.
2#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
3pub struct Diagram {
4    #[serde(flatten)]
5    pub base: super::IdentifiedObject,
6    /// A Diagram may have a DiagramStyle.
7    #[serde(skip_serializing_if = "Option::is_none")]
8    pub diagram_style: Option<super::base::MridRef>,
9    /// Coordinate system orientation of the diagram. A positive orientation gives standard “right-hand” orientation, with negative orientation indicating a “left-hand” orientation. For 2D diagrams, a positive orientation will result in X values increasing from left to right and Y values increasing from bottom to top. A negative orientation gives the “left-hand” orientation (favoured by computer graphics displays) with X values increasing from left to right and Y values increasing from top to bottom.
10    #[serde(skip_serializing_if = "Option::is_none")]
11    pub orientation: Option<super::base::UriRef>,
12    /// X coordinate of the first corner of the initial view.
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub x1initial_view: Option<f64>,
15    /// X coordinate of the second corner of the initial view.
16    #[serde(skip_serializing_if = "Option::is_none")]
17    pub x2initial_view: Option<f64>,
18    /// Y coordinate of the first corner of the initial view.
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub y1initial_view: Option<f64>,
21    /// Y coordinate of the second corner of the initial view.
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub y2initial_view: Option<f64>,
24}
25impl crate::base::CimElement for Diagram {
26    fn mrid(&self) -> &str { &self.base.id }
27    fn type_name(&self) -> &'static str { "Diagram" }
28    fn as_any(&self) -> &dyn std::any::Any { self }
29    fn to_json_value(&self) -> serde_json::Value {
30        serde_json::to_value(self).unwrap_or(serde_json::Value::Null)
31    }
32    fn to_block(&self) -> crate::base::RdfBlock {
33        let mut block = self.base.to_block();
34        block.type_name = "Diagram".to_string();
35        if let Some(ref v) = self.diagram_style {
36            block.fields.insert("Diagram.DiagramStyle".into(), crate::base::FieldValue::Resource(v.mrid.clone()));
37        }
38        if let Some(ref v) = self.orientation {
39            block.fields.insert("Diagram.orientation".into(), crate::base::FieldValue::Resource(v.uri.clone()));
40        }
41        if let Some(v) = self.x1initial_view {
42            block.fields.insert("Diagram.x1InitialView".into(), crate::base::FieldValue::Text(v.to_string()));
43        }
44        if let Some(v) = self.x2initial_view {
45            block.fields.insert("Diagram.x2InitialView".into(), crate::base::FieldValue::Text(v.to_string()));
46        }
47        if let Some(v) = self.y1initial_view {
48            block.fields.insert("Diagram.y1InitialView".into(), crate::base::FieldValue::Text(v.to_string()));
49        }
50        if let Some(v) = self.y2initial_view {
51            block.fields.insert("Diagram.y2InitialView".into(), crate::base::FieldValue::Text(v.to_string()));
52        }
53        block
54    }
55}
56
57impl Diagram {
58    pub fn from_block(b: &crate::base::RdfBlock) -> Self {
59        let mut obj = Self::default();
60        obj.base.id.clone_from(&b.mrid);
61        for (key, val) in &b.fields {
62            match key.as_str() {
63                "Diagram.DiagramStyle" => {
64                    if let crate::base::FieldValue::Resource(sv) = val {
65                        obj.diagram_style = Some(crate::base::MridRef { mrid: sv.clone() });
66                    }
67                }
68                "Diagram.orientation" => {
69                    if let crate::base::FieldValue::Resource(sv) = val {
70                        obj.orientation = Some(crate::base::UriRef { uri: sv.clone() });
71                    }
72                }
73                "Diagram.x1InitialView" => {
74                    match val {
75                        crate::base::FieldValue::Text(sv) => { if let Ok(v) = sv.trim().parse() { obj.x1initial_view = Some(v); } }
76                        crate::base::FieldValue::TextList(svs) => {
77                            if let Some(sv) = svs.last() { if let Ok(v) = sv.trim().parse() { obj.x1initial_view = Some(v); } }
78                        }
79                        _ => {}
80                    }
81                }
82                "Diagram.x2InitialView" => {
83                    match val {
84                        crate::base::FieldValue::Text(sv) => { if let Ok(v) = sv.trim().parse() { obj.x2initial_view = Some(v); } }
85                        crate::base::FieldValue::TextList(svs) => {
86                            if let Some(sv) = svs.last() { if let Ok(v) = sv.trim().parse() { obj.x2initial_view = Some(v); } }
87                        }
88                        _ => {}
89                    }
90                }
91                "Diagram.y1InitialView" => {
92                    match val {
93                        crate::base::FieldValue::Text(sv) => { if let Ok(v) = sv.trim().parse() { obj.y1initial_view = Some(v); } }
94                        crate::base::FieldValue::TextList(svs) => {
95                            if let Some(sv) = svs.last() { if let Ok(v) = sv.trim().parse() { obj.y1initial_view = Some(v); } }
96                        }
97                        _ => {}
98                    }
99                }
100                "Diagram.y2InitialView" => {
101                    match val {
102                        crate::base::FieldValue::Text(sv) => { if let Ok(v) = sv.trim().parse() { obj.y2initial_view = Some(v); } }
103                        crate::base::FieldValue::TextList(svs) => {
104                            if let Some(sv) = svs.last() { if let Ok(v) = sv.trim().parse() { obj.y2initial_view = Some(v); } }
105                        }
106                        _ => {}
107                    }
108                }
109                "IdentifiedObject.description" => {
110                    match val {
111                        crate::base::FieldValue::Text(sv) => { obj.base.description = sv.clone(); }
112                        crate::base::FieldValue::TextList(svs) => {
113                            if let Some(sv) = svs.last() { obj.base.description = sv.clone(); }
114                        }
115                        _ => {}
116                    }
117                }
118                "IdentifiedObject.energyIdentCodeEic" => {
119                    match val {
120                        crate::base::FieldValue::Text(sv) => { obj.base.energy_ident_code_eic = sv.clone(); }
121                        crate::base::FieldValue::TextList(svs) => {
122                            if let Some(sv) = svs.last() { obj.base.energy_ident_code_eic = sv.clone(); }
123                        }
124                        _ => {}
125                    }
126                }
127                "IdentifiedObject.mRID" => {
128                    match val {
129                        crate::base::FieldValue::Text(sv) => { obj.base.m_rid = sv.clone(); }
130                        crate::base::FieldValue::TextList(svs) => {
131                            if let Some(sv) = svs.last() { obj.base.m_rid = sv.clone(); }
132                        }
133                        _ => {}
134                    }
135                }
136                "IdentifiedObject.name" => {
137                    match val {
138                        crate::base::FieldValue::Text(sv) => { obj.base.name = sv.clone(); }
139                        crate::base::FieldValue::TextList(svs) => {
140                            if let Some(sv) = svs.last() { obj.base.name = sv.clone(); }
141                        }
142                        _ => {}
143                    }
144                }
145                "IdentifiedObject.shortName" => {
146                    match val {
147                        crate::base::FieldValue::Text(sv) => { obj.base.short_name = sv.clone(); }
148                        crate::base::FieldValue::TextList(svs) => {
149                            if let Some(sv) = svs.last() { obj.base.short_name = sv.clone(); }
150                        }
151                        _ => {}
152                    }
153                }
154                _ => {}
155            }
156        }
157        obj
158    }
159}