Skip to main content

cimstructs/
VisibilityLayer.rs

1/// Layers are typically used for grouping diagram objects according to themes and scales. Themes are used to display or hide certain information (e.g., lakes, borders), while scales are used for hiding or displaying information depending on the current zoom level (hide text when it is too small to be read, or when it exceeds the screen size). This is also called de-cluttering. CIM based graphics exchange supports an m:n relationship between diagram objects and layers. The importing system shall convert an m:n case into an appropriate 1:n representation if the importing system does not support m:n.
2#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
3pub struct VisibilityLayer {
4    #[serde(flatten)]
5    pub base: super::IdentifiedObject,
6    /// A visibility layer can contain one or more diagram objects.
7    pub visible_objects: Vec<super::base::MridRef>,
8    /// The drawing order for this layer. The higher the number, the later the layer and the objects within it are rendered.
9    #[serde(skip_serializing_if = "Option::is_none")]
10    pub drawing_order: Option<i64>,
11}
12impl crate::base::CimElement for VisibilityLayer {
13    fn mrid(&self) -> &str { &self.base.id }
14    fn type_name(&self) -> &'static str { "VisibilityLayer" }
15    fn as_any(&self) -> &dyn std::any::Any { self }
16    fn to_json_value(&self) -> serde_json::Value {
17        serde_json::to_value(self).unwrap_or(serde_json::Value::Null)
18    }
19    fn to_block(&self) -> crate::base::RdfBlock {
20        let mut block = self.base.to_block();
21        block.type_name = "VisibilityLayer".to_string();
22        if !self.visible_objects.is_empty() {
23            block.fields.insert("VisibilityLayer.VisibleObjects".into(), crate::base::FieldValue::ResourceList(self.visible_objects.iter().map(|r| r.mrid.clone()).collect()));
24        }
25        if let Some(v) = self.drawing_order {
26            block.fields.insert("VisibilityLayer.drawingOrder".into(), crate::base::FieldValue::Text(v.to_string()));
27        }
28        block
29    }
30}
31
32impl VisibilityLayer {
33    pub fn from_block(b: &crate::base::RdfBlock) -> Self {
34        let mut obj = Self::default();
35        obj.base.id.clone_from(&b.mrid);
36        for (key, val) in &b.fields {
37            match key.as_str() {
38                "VisibilityLayer.VisibleObjects" => {
39                    match val {
40                        crate::base::FieldValue::Resource(sv) => obj.visible_objects.push(crate::base::MridRef { mrid: sv.clone() }),
41                        crate::base::FieldValue::ResourceList(svs) => {
42                            for sv in svs { obj.visible_objects.push(crate::base::MridRef { mrid: sv.clone() }); }
43                        }
44                        _ => {}
45                    }
46                }
47                "VisibilityLayer.drawingOrder" => {
48                    match val {
49                        crate::base::FieldValue::Text(sv) => { if let Ok(v) = sv.trim().parse() { obj.drawing_order = Some(v); } }
50                        crate::base::FieldValue::TextList(svs) => {
51                            if let Some(sv) = svs.last() { if let Ok(v) = sv.trim().parse() { obj.drawing_order = Some(v); } }
52                        }
53                        _ => {}
54                    }
55                }
56                "IdentifiedObject.description" => {
57                    match val {
58                        crate::base::FieldValue::Text(sv) => { obj.base.description = sv.clone(); }
59                        crate::base::FieldValue::TextList(svs) => {
60                            if let Some(sv) = svs.last() { obj.base.description = sv.clone(); }
61                        }
62                        _ => {}
63                    }
64                }
65                "IdentifiedObject.energyIdentCodeEic" => {
66                    match val {
67                        crate::base::FieldValue::Text(sv) => { obj.base.energy_ident_code_eic = sv.clone(); }
68                        crate::base::FieldValue::TextList(svs) => {
69                            if let Some(sv) = svs.last() { obj.base.energy_ident_code_eic = sv.clone(); }
70                        }
71                        _ => {}
72                    }
73                }
74                "IdentifiedObject.mRID" => {
75                    match val {
76                        crate::base::FieldValue::Text(sv) => { obj.base.m_rid = sv.clone(); }
77                        crate::base::FieldValue::TextList(svs) => {
78                            if let Some(sv) = svs.last() { obj.base.m_rid = sv.clone(); }
79                        }
80                        _ => {}
81                    }
82                }
83                "IdentifiedObject.name" => {
84                    match val {
85                        crate::base::FieldValue::Text(sv) => { obj.base.name = sv.clone(); }
86                        crate::base::FieldValue::TextList(svs) => {
87                            if let Some(sv) = svs.last() { obj.base.name = sv.clone(); }
88                        }
89                        _ => {}
90                    }
91                }
92                "IdentifiedObject.shortName" => {
93                    match val {
94                        crate::base::FieldValue::Text(sv) => { obj.base.short_name = sv.clone(); }
95                        crate::base::FieldValue::TextList(svs) => {
96                            if let Some(sv) = svs.last() { obj.base.short_name = sv.clone(); }
97                        }
98                        _ => {}
99                    }
100                }
101                _ => {}
102            }
103        }
104        obj
105    }
106}