Salome HOME
updated copyright message
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_SObject.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SALOME SALOMEDS : data structure of SALOME and sources of Salome data server 
24 //  File   : SALOMEDSImpl_SObject.cxx
25 //  Author : Sergey RUIN
26 //  Module : SALOME
27 //
28 #include "SALOMEDSImpl_SObject.hxx"
29 #include "SALOMEDSImpl_Attributes.hxx"
30 #include "SALOMEDSImpl_SComponent.hxx"
31 #include "SALOMEDSImpl_Study.hxx"
32
33 #include <map>
34 #include <string.h>
35
36 //============================================================================
37 /*! Function : empty constructor
38  *  Purpose  : 
39  */
40 //============================================================================
41 SALOMEDSImpl_SObject::SALOMEDSImpl_SObject()
42 {
43 }
44
45 //============================================================================
46 /*! Function : copy constructor
47  *  Purpose  : 
48  */
49 //============================================================================
50 SALOMEDSImpl_SObject::SALOMEDSImpl_SObject(const SALOMEDSImpl_SObject& theSObject)
51 {
52   _lab   = theSObject._lab;
53   _value = theSObject._value;
54   _type  = theSObject._type;
55   _name  = theSObject._name;
56  
57 }
58
59
60 //============================================================================
61 /*! Function : constructor
62  *  Purpose  : 
63  */
64 //============================================================================
65 SALOMEDSImpl_SObject::SALOMEDSImpl_SObject(const DF_Label& theLabel)
66   :_lab(theLabel)
67 {
68   _value = "";
69   _type = "";
70   _name = "";
71 }
72
73 //============================================================================
74 /*! Function : Destructor
75  *  Purpose  : 
76  */
77 //============================================================================    
78 SALOMEDSImpl_SObject::~SALOMEDSImpl_SObject() 
79 {
80 }
81
82 //============================================================================
83 /*! Function : GetID
84  *  Purpose  : 
85  */
86 //============================================================================
87 std::string SALOMEDSImpl_SObject::GetID() const
88 {
89   return _lab.Entry();
90 }
91
92 //============================================================================
93 /*! Function : GetFatherComponent
94  *  Purpose  :
95  */
96 //============================================================================
97 SALOMEDSImpl_SComponent SALOMEDSImpl_SObject::GetFatherComponent() const
98 {
99   SALOMEDSImpl_SComponent sco;
100   DF_Label LF = _lab;
101   while (!SALOMEDSImpl_SComponent::IsA(LF) && !LF.IsRoot()) {
102     LF = LF.Father();
103   }
104
105   if(LF.IsRoot()) return sco;
106
107   return SALOMEDSImpl_Study::SComponent(LF);
108 }
109
110 //============================================================================
111 /*! Function : GetFather
112  *  Purpose  :
113  */
114 //============================================================================
115 SALOMEDSImpl_SObject SALOMEDSImpl_SObject::GetFather() const
116 {
117   return SALOMEDSImpl_Study::SObject(_lab.Father());
118 }
119
120 //============================================================================
121 /*! Function : GetLastChild
122  *  Purpose  :
123  */
124 //============================================================================
125 int SALOMEDSImpl_SObject::GetLastChildTag() const
126 {
127   return _lab.LastChildTag();
128 }
129
130 //============================================================================
131 /*! Function : FindAttribute
132  *  Purpose  : Find attribute of given type on this SObject
133  */
134 //============================================================================
135 bool SALOMEDSImpl_SObject::FindAttribute(DF_Attribute*& theAttribute, 
136                                          const std::string& theTypeOfAttribute) const
137 {
138   if(_lab.IsNull()) return false;
139   std::string aGUID = GetGUID(theTypeOfAttribute);
140   if ((theAttribute = _lab.FindAttribute(aGUID))) return true;
141   return false;
142 }
143
144
145
146 //============================================================================
147 /*! Function : GetAllAttributes
148  *  Purpose  : Returns list of all attributes for this sobject
149  */
150 //============================================================================
151 std::vector<DF_Attribute*> SALOMEDSImpl_SObject::GetAllAttributes() const
152 {
153   std::vector<DF_Attribute*> va1, va = _lab.GetAttributes();
154   for(size_t i = 0, len = va.size(); i<len; i++) {
155     SALOMEDSImpl_GenericAttribute* ga = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(va[i]); 
156     if(ga && ga->Type() != std::string("AttributeReference"))
157         va1.push_back(va[i]);
158   }
159
160   return va1;
161 }
162
163
164 //============================================================================
165 /*! Function : ReferencedObject
166  *  Purpose  : 
167  */
168 //============================================================================
169 bool SALOMEDSImpl_SObject::ReferencedObject(SALOMEDSImpl_SObject& theObject) const
170 {
171   SALOMEDSImpl_AttributeReference* Ref;
172   if (!(Ref=(SALOMEDSImpl_AttributeReference*)_lab.FindAttribute(SALOMEDSImpl_AttributeReference::GetID())))
173     return false;
174   
175   theObject =  SALOMEDSImpl_Study::SObject(Ref->Get());
176   return true;
177 }
178
179 //============================================================================
180 /*! Function : FindSubObject
181  *  Purpose  : 
182  */
183 //============================================================================
184 bool SALOMEDSImpl_SObject::FindSubObject(int theTag, SALOMEDSImpl_SObject& theObject)
185 {
186   DF_Label L = _lab.FindChild(theTag, false);
187   if (L.IsNull()) return false;
188   
189   theObject = SALOMEDSImpl_Study::SObject(L);
190   return true;
191     
192 }  
193
194   
195 //============================================================================
196 /*! Function : GetName
197  *  Purpose  : 
198  */
199 //============================================================================
200 std::string SALOMEDSImpl_SObject::GetName() const
201 {
202   std::string aStr = "";
203   SALOMEDSImpl_AttributeName* aName;
204   if ((aName=(SALOMEDSImpl_AttributeName*)_lab.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
205     aStr =aName->Value();
206   }
207   return aStr;
208 }
209
210 //============================================================================
211 /*! Function : GetComment
212  *  Purpose  : 
213  */
214 //============================================================================
215 std::string SALOMEDSImpl_SObject::GetComment() const
216 {
217   std::string aStr = "";
218   SALOMEDSImpl_AttributeComment* aComment;
219   if ((aComment=(SALOMEDSImpl_AttributeComment*)_lab.FindAttribute(SALOMEDSImpl_AttributeComment::GetID()))) {
220     aStr = aComment->Value();
221   }
222   return aStr;
223 }
224
225 //============================================================================
226 /*! Function : GetIOR
227  *  Purpose  : 
228  */
229 //============================================================================
230 std::string SALOMEDSImpl_SObject::GetIOR() const 
231 {
232   std::string aStr = "";
233   SALOMEDSImpl_AttributeIOR* anIOR;
234   if ((anIOR=(SALOMEDSImpl_AttributeIOR*)_lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
235     aStr = dynamic_cast<SALOMEDSImpl_AttributeIOR*>(anIOR)->Value();
236   }
237   return aStr;
238 }
239
240
241 std::string SALOMEDSImpl_SObject::GetGUID(const std::string& theType) 
242 {
243   __AttributeTypeToGUIDForSObject
244
245   if (strncmp(theType.c_str(), "AttributeTreeNodeGUID",21) == 0) {
246     return theType.substr(21, theType.size()); 
247   }
248   if (strncmp(theType.c_str(), "AttributeUserID",15) == 0) {
249     return theType.substr(15, theType.size()); 
250   }
251   return "";
252 }
253
254 //============================================================================
255 /*! Function :  SALOMEDSImpl_SComponent
256  *  Purpose  : 
257  */
258 //============================================================================
259 SALOMEDSImpl_SObject::operator SALOMEDSImpl_SComponent() const
260 {
261   SALOMEDSImpl_SComponent sco;
262   sco._lab = _lab;
263   sco._name = _name;
264   sco._type = _type;
265   sco._value = _value;
266   return sco;
267 }
268
269 //============================================================================
270 /*! Function :  GetPersistentCopy
271  *  Purpose  : 
272  */
273 //============================================================================
274 SALOMEDSImpl_SObject* SALOMEDSImpl_SObject::GetPersistentCopy() const
275 {
276   SALOMEDSImpl_SObject* so = new SALOMEDSImpl_SObject;
277   so->_lab = _lab;
278   so->_name = _name;
279   so->_type = _type;
280   so->_value = _value; 
281   return so;
282 }
283
284 //============================================================================
285 /*! Function :  IsComponent
286  *  Purpose  : 
287  */
288 //============================================================================
289 bool SALOMEDSImpl_SObject::IsComponent() const
290 {
291     return SALOMEDSImpl_SComponent::IsA(_lab);
292 }
293
294 void SALOMEDSImpl_SObject::SetAttrString(const std::string& name, const std::string& value)
295 {
296   if(name=="AttributeName")SALOMEDSImpl_AttributeName::Set(GetLabel(), value);
297   else if(name=="AttributeIOR")SALOMEDSImpl_AttributeIOR::Set(GetLabel(), value);
298   else if(name=="AttributeString")SALOMEDSImpl_AttributeString::Set(GetLabel(), value);
299   else if(name=="AttributePixMap")SALOMEDSImpl_AttributePixMap::Set(GetLabel(), value);
300 }