Salome HOME
merge from branch BR_V5_DEV
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_SObject.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, 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.
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 //  SALOME SALOMEDS : data structure of SALOME and sources of Salome data server 
23 //  File   : SALOMEDSImpl_SObject.cxx
24 //  Author : Sergey RUIN
25 //  Module : SALOME
26 //
27 #include "SALOMEDSImpl_SObject.hxx"
28 #include "SALOMEDSImpl_Attributes.hxx"
29 #include "SALOMEDSImpl_SComponent.hxx"
30 #include "SALOMEDSImpl_Study.hxx"
31
32 using namespace std;
33
34 #include <map>
35 #include <string.h>
36
37 //============================================================================
38 /*! Function : empty constructor
39  *  Purpose  : 
40  */
41 //============================================================================
42 SALOMEDSImpl_SObject::SALOMEDSImpl_SObject()
43 {
44 }
45
46 //============================================================================
47 /*! Function : copy constructor
48  *  Purpose  : 
49  */
50 //============================================================================
51 SALOMEDSImpl_SObject::SALOMEDSImpl_SObject(const SALOMEDSImpl_SObject& theSObject)
52 {
53   _lab   = theSObject._lab;
54   _value = theSObject._value;
55   _type  = theSObject._type;
56   _name  = theSObject._name;
57  
58 }
59
60
61 //============================================================================
62 /*! Function : constructor
63  *  Purpose  : 
64  */
65 //============================================================================
66 SALOMEDSImpl_SObject::SALOMEDSImpl_SObject(const DF_Label& theLabel)
67   :_lab(theLabel)
68 {
69   _value = "";
70   _type = "";
71   _name = "";
72 }
73
74 //============================================================================
75 /*! Function : Desctructor
76  *  Purpose  : 
77  */
78 //============================================================================    
79 SALOMEDSImpl_SObject::~SALOMEDSImpl_SObject() 
80 {
81 }
82
83 //============================================================================
84 /*! Function : GetID
85  *  Purpose  : 
86  */
87 //============================================================================
88 string SALOMEDSImpl_SObject::GetID() const
89 {
90   return _lab.Entry();
91 }
92   
93 //============================================================================
94 /*! Function : GetFatherComponent
95  *  Purpose  : 
96  */
97 //============================================================================
98 SALOMEDSImpl_SComponent SALOMEDSImpl_SObject::GetFatherComponent() const
99 {
100   SALOMEDSImpl_SComponent sco;    
101   DF_Label LF = _lab;
102   while (!SALOMEDSImpl_SComponent::IsA(LF) && !LF.IsRoot()) {
103     LF = LF.Father();
104   }
105   
106   if(LF.IsRoot()) return sco;
107   
108   return GetStudy()->GetSComponent(LF);
109 }
110   
111 //============================================================================
112 /*! Function : GetFather
113  *  Purpose  : 
114  */
115 //============================================================================
116 SALOMEDSImpl_SObject SALOMEDSImpl_SObject::GetFather() const
117 {
118   return GetStudy()->GetSObject(_lab.Father());    
119 }
120
121
122 //============================================================================
123 /*! Function : GetStudy
124  *  Purpose  : 
125  */
126 //============================================================================
127 SALOMEDSImpl_Study* SALOMEDSImpl_SObject::GetStudy() const
128 {
129   return SALOMEDSImpl_Study::GetStudy(_lab);
130 }
131
132 //============================================================================
133 /*! Function : FindAttribute
134  *  Purpose  : Find attribute of given type on this SObject
135  */
136 //============================================================================
137 bool SALOMEDSImpl_SObject::FindAttribute(DF_Attribute*& theAttribute, 
138                                          const string& theTypeOfAttribute) const
139 {
140   if(_lab.IsNull()) return false;
141   std::string aGUID = GetGUID(theTypeOfAttribute);
142   if ((theAttribute = _lab.FindAttribute(aGUID))) return true;
143   return false;
144 }
145
146
147
148 //============================================================================
149 /*! Function : GetAllAttributes
150  *  Purpose  : Returns list of all attributes for this sobject
151  */
152 //============================================================================
153 vector<DF_Attribute*> SALOMEDSImpl_SObject::GetAllAttributes() const
154 {
155   vector<DF_Attribute*> va1, va = _lab.GetAttributes();
156   for(int i = 0, len = va.size(); i<len; i++) {
157     SALOMEDSImpl_GenericAttribute* ga = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(va[i]); 
158     if(ga && ga->Type() != string("AttributeReference"))
159         va1.push_back(va[i]);
160   }
161
162   return va1;
163 }
164
165
166 //============================================================================
167 /*! Function : ReferencedObject
168  *  Purpose  : 
169  */
170 //============================================================================
171 bool SALOMEDSImpl_SObject::ReferencedObject(SALOMEDSImpl_SObject& theObject) const
172 {
173   SALOMEDSImpl_AttributeReference* Ref;
174   if (!(Ref=(SALOMEDSImpl_AttributeReference*)_lab.FindAttribute(SALOMEDSImpl_AttributeReference::GetID())))
175     return false;
176   
177   theObject =  GetStudy()->GetSObject(Ref->Get());
178   return true;
179 }
180
181 //============================================================================
182 /*! Function : FindSubObject
183  *  Purpose  : 
184  */
185 //============================================================================
186 bool SALOMEDSImpl_SObject::FindSubObject(int theTag, SALOMEDSImpl_SObject& theObject)
187 {
188   DF_Label L = _lab.FindChild(theTag, false);
189   if (L.IsNull()) return false;
190   
191   theObject = GetStudy()->GetSObject(L);
192   return true;
193     
194 }  
195
196   
197 //============================================================================
198 /*! Function : GetName
199  *  Purpose  : 
200  */
201 //============================================================================
202 string SALOMEDSImpl_SObject::GetName() const
203 {
204   string aStr = "";
205   SALOMEDSImpl_AttributeName* aName;
206   if ((aName=(SALOMEDSImpl_AttributeName*)_lab.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
207     aStr =aName->Value();
208   }
209   return aStr;
210 }
211
212 //============================================================================
213 /*! Function : GetComment
214  *  Purpose  : 
215  */
216 //============================================================================
217 string SALOMEDSImpl_SObject::GetComment() const
218 {
219   string aStr = "";
220   SALOMEDSImpl_AttributeComment* aComment;
221   if ((aComment=(SALOMEDSImpl_AttributeComment*)_lab.FindAttribute(SALOMEDSImpl_AttributeComment::GetID()))) {
222     aStr = aComment->Value();
223   }
224   return aStr;
225 }
226
227 //============================================================================
228 /*! Function : GetIOR
229  *  Purpose  : 
230  */
231 //============================================================================
232 string SALOMEDSImpl_SObject::GetIOR() const 
233 {
234   string aStr = "";
235   SALOMEDSImpl_AttributeIOR* anIOR;
236   if ((anIOR=(SALOMEDSImpl_AttributeIOR*)_lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
237     aStr = dynamic_cast<SALOMEDSImpl_AttributeIOR*>(anIOR)->Value();
238   }
239   return aStr;
240 }
241
242
243 std::string SALOMEDSImpl_SObject::GetGUID(const string& theType) 
244 {
245   __AttributeTypeToGUIDForSObject
246
247   if (strncmp(theType.c_str(), "AttributeTreeNodeGUID",21) == 0) {
248     return theType.substr(21, theType.size()); 
249   }
250   return "";
251 }
252
253 //============================================================================
254 /*! Function :  SALOMEDSImpl_SComponent
255  *  Purpose  : 
256  */
257 //============================================================================
258 SALOMEDSImpl_SObject::operator SALOMEDSImpl_SComponent() const
259 {
260   SALOMEDSImpl_SComponent sco;
261   sco._lab = _lab;
262   sco._name = _name;
263   sco._type = _type;
264   sco._value = _value;
265   return sco;
266 }
267
268 //============================================================================
269 /*! Function :  GetPersistentCopy
270  *  Purpose  : 
271  */
272 //============================================================================
273 SALOMEDSImpl_SObject* SALOMEDSImpl_SObject::GetPersistentCopy() const
274 {
275   SALOMEDSImpl_SObject* so = new SALOMEDSImpl_SObject;
276   so->_lab = _lab;
277   so->_name = _name;
278   so->_type = _type;
279   so->_value = _value; 
280   return so;
281 }
282
283 //============================================================================
284 /*! Function :  IsComponent
285  *  Purpose  : 
286  */
287 //============================================================================
288 bool SALOMEDSImpl_SObject::IsComponent() const
289 {
290     return SALOMEDSImpl_SComponent::IsA(_lab);
291 }
292