Salome HOME
New implementation of SALOMEDSImpl package based on DF data structure instead of...
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_SObject.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //  SALOME SALOMEDS : data structure of SALOME and sources of Salome data server 
21 //  File   : SALOMEDSImpl_SObject.cxx
22 //  Author : Sergey RUIN
23 //  Module : SALOME
24
25
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
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 : Desctructor
75  *  Purpose  : 
76  */
77 //============================================================================    
78 SALOMEDSImpl_SObject::~SALOMEDSImpl_SObject() 
79 {
80 }
81
82 //============================================================================
83 /*! Function : GetID
84  *  Purpose  : 
85  */
86 //============================================================================
87 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 GetStudy()->GetSComponent(LF);
108 }
109   
110 //============================================================================
111 /*! Function : GetFather
112  *  Purpose  : 
113  */
114 //============================================================================
115 SALOMEDSImpl_SObject SALOMEDSImpl_SObject::GetFather() const
116 {
117   return GetStudy()->GetSObject(_lab.Father());    
118 }
119
120
121 //============================================================================
122 /*! Function : GetStudy
123  *  Purpose  : 
124  */
125 //============================================================================
126 SALOMEDSImpl_Study* SALOMEDSImpl_SObject::GetStudy() const
127 {
128   return SALOMEDSImpl_Study::GetStudy(_lab);
129 }
130
131 //============================================================================
132 /*! Function : FindAttribute
133  *  Purpose  : Find attribute of given type on this SObject
134  */
135 //============================================================================
136 bool SALOMEDSImpl_SObject::FindAttribute(DF_Attribute*& theAttribute, 
137                                          const string& theTypeOfAttribute) const
138 {
139   if(_lab.IsNull()) return false;
140   std::string aGUID = GetGUID(theTypeOfAttribute);
141   if ((theAttribute = _lab.FindAttribute(aGUID))) return true;
142   return false;
143 }
144
145
146
147 //============================================================================
148 /*! Function : GetAllAttributes
149  *  Purpose  : Returns list of all attributes for this sobject
150  */
151 //============================================================================
152 vector<DF_Attribute*> SALOMEDSImpl_SObject::GetAllAttributes() const
153 {
154   vector<DF_Attribute*> va1, va = _lab.GetAttributes();
155   for(int i = 0, len = va.size(); i<len; i++) {
156     SALOMEDSImpl_GenericAttribute* ga = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(va[i]); 
157     if(ga && ga->Type() != string("AttributeReference"))
158         va1.push_back(va[i]);
159   }
160
161   return va1;
162 }
163
164
165 //============================================================================
166 /*! Function : ReferencedObject
167  *  Purpose  : 
168  */
169 //============================================================================
170 bool SALOMEDSImpl_SObject::ReferencedObject(SALOMEDSImpl_SObject& theObject) const
171 {
172   SALOMEDSImpl_AttributeReference* Ref;
173   if (!(Ref=(SALOMEDSImpl_AttributeReference*)_lab.FindAttribute(SALOMEDSImpl_AttributeReference::GetID())))
174     return false;
175   
176   theObject =  GetStudy()->GetSObject(Ref->Get());
177   return true;
178 }
179
180 //============================================================================
181 /*! Function : FindSubObject
182  *  Purpose  : 
183  */
184 //============================================================================
185 bool SALOMEDSImpl_SObject::FindSubObject(int theTag, SALOMEDSImpl_SObject& theObject)
186 {
187   DF_Label L = _lab.FindChild(theTag, false);
188   if (L.IsNull()) return false;
189   
190   theObject = GetStudy()->GetSObject(L);
191   return true;
192     
193 }  
194
195   
196 //============================================================================
197 /*! Function : GetName
198  *  Purpose  : 
199  */
200 //============================================================================
201 string SALOMEDSImpl_SObject::GetName() const
202 {
203   string aStr = "";
204   SALOMEDSImpl_AttributeName* aName;
205   if ((aName=(SALOMEDSImpl_AttributeName*)_lab.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
206     aStr =aName->Value();
207   }
208   return aStr;
209 }
210
211 //============================================================================
212 /*! Function : GetComment
213  *  Purpose  : 
214  */
215 //============================================================================
216 string SALOMEDSImpl_SObject::GetComment() const
217 {
218   string aStr = "";
219   SALOMEDSImpl_AttributeComment* aComment;
220   if ((aComment=(SALOMEDSImpl_AttributeComment*)_lab.FindAttribute(SALOMEDSImpl_AttributeComment::GetID()))) {
221     aStr = aComment->Value();
222   }
223   return aStr;
224 }
225
226 //============================================================================
227 /*! Function : GetIOR
228  *  Purpose  : 
229  */
230 //============================================================================
231 string SALOMEDSImpl_SObject::GetIOR() const 
232 {
233   string aStr = "";
234   SALOMEDSImpl_AttributeIOR* anIOR;
235   if ((anIOR=(SALOMEDSImpl_AttributeIOR*)_lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
236     aStr = dynamic_cast<SALOMEDSImpl_AttributeIOR*>(anIOR)->Value();
237   }
238   return aStr;
239 }
240
241
242 std::string SALOMEDSImpl_SObject::GetGUID(const string& theType) 
243 {
244   __AttributeTypeToGUIDForSObject
245
246   if (strncmp(theType.c_str(), "AttributeTreeNodeGUID",21) == 0) {
247     return theType.substr(21, theType.size()); 
248   }
249   return "";
250 }
251
252 //============================================================================
253 /*! Function :  SALOMEDSImpl_SComponent
254  *  Purpose  : 
255  */
256 //============================================================================
257 SALOMEDSImpl_SObject::operator SALOMEDSImpl_SComponent() const
258 {
259   SALOMEDSImpl_SComponent sco;
260   sco._lab = _lab;
261   sco._name = _name;
262   sco._type = _type;
263   sco._value = _value;
264   return sco;
265 }
266
267 //============================================================================
268 /*! Function :  GetPersistentCopy
269  *  Purpose  : 
270  */
271 //============================================================================
272 SALOMEDSImpl_SObject* SALOMEDSImpl_SObject::GetPersistentCopy() const
273 {
274   SALOMEDSImpl_SObject* so = new SALOMEDSImpl_SObject;
275   so->_lab = _lab;
276   so->_name = _name;
277   so->_type = _type;
278   so->_value = _value; 
279   return so;
280 }
281
282 //============================================================================
283 /*! Function :  IsComponent
284  *  Purpose  : 
285  */
286 //============================================================================
287 bool SALOMEDSImpl_SObject::IsComponent() const
288 {
289     return SALOMEDSImpl_SComponent::IsA(_lab);
290 }
291