Skip to main content

cimstructs/
DiagramObject.rs

1/// An object that defines one or more points in a given space. This object can be associated with anything that specializes IdentifiedObject. For single line diagrams such objects typically include such items as analog values, breakers, disconnectors, power transformers, and transmission lines.
2#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
3pub struct DiagramObject {
4    #[serde(flatten)]
5    pub base: super::IdentifiedObject,
6    /// A diagram object is part of a diagram.
7    #[serde(skip_serializing_if = "Option::is_none")]
8    pub diagram: Option<super::base::MridRef>,
9    /// A diagram object has a style associated that provides a reference for the style used in the originating system.
10    #[serde(skip_serializing_if = "Option::is_none")]
11    pub diagram_object_style: Option<super::base::MridRef>,
12    /// The domain object to which this diagram object is associated.
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub identified_object_: Option<super::base::MridRef>,
15    /// The drawing order of this element. The higher the number, the later the element is drawn in sequence. This is used to ensure that elements that overlap are rendered in the correct order.
16    #[serde(skip_serializing_if = "Option::is_none")]
17    pub drawing_order: Option<i64>,
18    /// Defines whether or not the diagram objects points define the boundaries of a polygon or the routing of a polyline. If this value is true then a receiving application should consider the first and last points to be connected.
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub is_polygon: Option<bool>,
21    /// The offset in the X direction. This is used for defining the offset from centre for rendering an icon (the default is that a single point specifies the centre of the icon). The offset is in per-unit with 0 indicating there is no offset from the horizontal centre of the icon. -0.5 indicates it is offset by 50% to the left and 0.5 indicates an offset of 50% to the right.
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub offset_x: Option<f64>,
24    /// The offset in the Y direction. This is used for defining the offset from centre for rendering an icon (the default is that a single point specifies the centre of the icon). The offset is in per-unit with 0 indicating there is no offset from the vertical centre of the icon. The offset direction is dependent on the orientation of the diagram, with -0.5 and 0.5 indicating an offset of +/- 50% on the vertical axis.
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub offset_y: Option<f64>,
27    /// Sets the angle of rotation of the diagram object. Zero degrees is pointing to the top of the diagram. Rotation is clockwise. DiagramObject.rotation=0 has the following meaning: The connection point of an element which has one terminal is pointing to the top side of the diagram. The connection point 'From side' of an element which has more than one terminal is pointing to the top side of the diagram. DiagramObject.rotation=90 has the following meaning: The connection point of an element which has one terminal is pointing to the right hand side of the diagram. The connection point 'From side' of an element which has more than one terminal is pointing to the right hand side of the diagram.
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub rotation: Option<f64>,
30}
31impl crate::base::CimElement for DiagramObject {
32    fn mrid(&self) -> &str { &self.base.id }
33    fn type_name(&self) -> &'static str { "DiagramObject" }
34    fn as_any(&self) -> &dyn std::any::Any { self }
35    fn to_json_value(&self) -> serde_json::Value {
36        serde_json::to_value(self).unwrap_or(serde_json::Value::Null)
37    }
38    fn to_block(&self) -> crate::base::RdfBlock {
39        let mut block = self.base.to_block();
40        block.type_name = "DiagramObject".to_string();
41        if let Some(ref v) = self.diagram {
42            block.fields.insert("DiagramObject.Diagram".into(), crate::base::FieldValue::Resource(v.mrid.clone()));
43        }
44        if let Some(ref v) = self.diagram_object_style {
45            block.fields.insert("DiagramObject.DiagramObjectStyle".into(), crate::base::FieldValue::Resource(v.mrid.clone()));
46        }
47        if let Some(ref v) = self.identified_object_ {
48            block.fields.insert("DiagramObject.IdentifiedObject".into(), crate::base::FieldValue::Resource(v.mrid.clone()));
49        }
50        if let Some(v) = self.drawing_order {
51            block.fields.insert("DiagramObject.drawingOrder".into(), crate::base::FieldValue::Text(v.to_string()));
52        }
53        if let Some(v) = self.is_polygon {
54            block.fields.insert("DiagramObject.isPolygon".into(), crate::base::FieldValue::Text(v.to_string()));
55        }
56        if let Some(v) = self.offset_x {
57            block.fields.insert("DiagramObject.offsetX".into(), crate::base::FieldValue::Text(v.to_string()));
58        }
59        if let Some(v) = self.offset_y {
60            block.fields.insert("DiagramObject.offsetY".into(), crate::base::FieldValue::Text(v.to_string()));
61        }
62        if let Some(v) = self.rotation {
63            block.fields.insert("DiagramObject.rotation".into(), crate::base::FieldValue::Text(v.to_string()));
64        }
65        block
66    }
67}
68
69impl DiagramObject {
70    pub fn from_block(b: &crate::base::RdfBlock) -> Self {
71        let mut obj = Self::default();
72        obj.base.id.clone_from(&b.mrid);
73        for (key, val) in &b.fields {
74            match key.as_str() {
75                "DiagramObject.Diagram" => {
76                    if let crate::base::FieldValue::Resource(sv) = val {
77                        obj.diagram = Some(crate::base::MridRef { mrid: sv.clone() });
78                    }
79                }
80                "DiagramObject.DiagramObjectStyle" => {
81                    if let crate::base::FieldValue::Resource(sv) = val {
82                        obj.diagram_object_style = Some(crate::base::MridRef { mrid: sv.clone() });
83                    }
84                }
85                "DiagramObject.IdentifiedObject" => {
86                    if let crate::base::FieldValue::Resource(sv) = val {
87                        obj.identified_object_ = Some(crate::base::MridRef { mrid: sv.clone() });
88                    }
89                }
90                "DiagramObject.drawingOrder" => {
91                    match val {
92                        crate::base::FieldValue::Text(sv) => { if let Ok(v) = sv.trim().parse() { obj.drawing_order = Some(v); } }
93                        crate::base::FieldValue::TextList(svs) => {
94                            if let Some(sv) = svs.last() { if let Ok(v) = sv.trim().parse() { obj.drawing_order = Some(v); } }
95                        }
96                        _ => {}
97                    }
98                }
99                "DiagramObject.isPolygon" => {
100                    match val {
101                        crate::base::FieldValue::Text(sv) => { obj.is_polygon = Some(sv.trim() == "true"); }
102                        crate::base::FieldValue::TextList(svs) => {
103                            if let Some(sv) = svs.last() { obj.is_polygon = Some(sv.trim() == "true"); }
104                        }
105                        _ => {}
106                    }
107                }
108                "DiagramObject.offsetX" => {
109                    match val {
110                        crate::base::FieldValue::Text(sv) => { if let Ok(v) = sv.trim().parse() { obj.offset_x = Some(v); } }
111                        crate::base::FieldValue::TextList(svs) => {
112                            if let Some(sv) = svs.last() { if let Ok(v) = sv.trim().parse() { obj.offset_x = Some(v); } }
113                        }
114                        _ => {}
115                    }
116                }
117                "DiagramObject.offsetY" => {
118                    match val {
119                        crate::base::FieldValue::Text(sv) => { if let Ok(v) = sv.trim().parse() { obj.offset_y = Some(v); } }
120                        crate::base::FieldValue::TextList(svs) => {
121                            if let Some(sv) = svs.last() { if let Ok(v) = sv.trim().parse() { obj.offset_y = Some(v); } }
122                        }
123                        _ => {}
124                    }
125                }
126                "DiagramObject.rotation" => {
127                    match val {
128                        crate::base::FieldValue::Text(sv) => { if let Ok(v) = sv.trim().parse() { obj.rotation = Some(v); } }
129                        crate::base::FieldValue::TextList(svs) => {
130                            if let Some(sv) = svs.last() { if let Ok(v) = sv.trim().parse() { obj.rotation = Some(v); } }
131                        }
132                        _ => {}
133                    }
134                }
135                "IdentifiedObject.description" => {
136                    match val {
137                        crate::base::FieldValue::Text(sv) => { obj.base.description = sv.clone(); }
138                        crate::base::FieldValue::TextList(svs) => {
139                            if let Some(sv) = svs.last() { obj.base.description = sv.clone(); }
140                        }
141                        _ => {}
142                    }
143                }
144                "IdentifiedObject.energyIdentCodeEic" => {
145                    match val {
146                        crate::base::FieldValue::Text(sv) => { obj.base.energy_ident_code_eic = sv.clone(); }
147                        crate::base::FieldValue::TextList(svs) => {
148                            if let Some(sv) = svs.last() { obj.base.energy_ident_code_eic = sv.clone(); }
149                        }
150                        _ => {}
151                    }
152                }
153                "IdentifiedObject.mRID" => {
154                    match val {
155                        crate::base::FieldValue::Text(sv) => { obj.base.m_rid = sv.clone(); }
156                        crate::base::FieldValue::TextList(svs) => {
157                            if let Some(sv) = svs.last() { obj.base.m_rid = sv.clone(); }
158                        }
159                        _ => {}
160                    }
161                }
162                "IdentifiedObject.name" => {
163                    match val {
164                        crate::base::FieldValue::Text(sv) => { obj.base.name = sv.clone(); }
165                        crate::base::FieldValue::TextList(svs) => {
166                            if let Some(sv) = svs.last() { obj.base.name = sv.clone(); }
167                        }
168                        _ => {}
169                    }
170                }
171                "IdentifiedObject.shortName" => {
172                    match val {
173                        crate::base::FieldValue::Text(sv) => { obj.base.short_name = sv.clone(); }
174                        crate::base::FieldValue::TextList(svs) => {
175                            if let Some(sv) = svs.last() { obj.base.short_name = sv.clone(); }
176                        }
177                        _ => {}
178                    }
179                }
180                _ => {}
181            }
182        }
183        obj
184    }
185}