Salome HOME
42971688602e8cdf6fdb5fc0fb61264fd73500c1
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_SObject_i.cxx
1 //  SALOME SALOMEDS : data structure of SALOME and sources of Salome data server 
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SALOMEDS_SObject_i.cxx
25 //  Author : Yves FRICAUD
26 //  Module : SALOME
27 //  $Header$
28
29 using namespace std;
30 #include "utilities.h"
31 #include "SALOMEDS_SObject_i.hxx"
32 //SALOMEDS Headers
33 #include "SALOMEDS_SComponent_i.hxx"
34 #include "SALOMEDS_GenericAttribute_i.hxx"
35 #include "SALOMEDS_IORAttribute.hxx"
36 #include <TDF_AttributeIterator.hxx>
37
38 //============================================================================
39 /*! Function : constructor
40  *  Purpose  : 
41  */
42 //============================================================================
43 SALOMEDS_SObject_i::SALOMEDS_SObject_i(const TDF_Label lab, CORBA::ORB_ptr orb)
44   :_lab(lab)
45 {
46   _orb = CORBA::ORB::_duplicate(orb);
47   _value = NULL;
48   _type = NULL;
49   _name = NULL;
50   _liste_ba_type.resize(0);
51 }
52   
53
54 //============================================================================
55 /*! Function : destructor
56  *  Purpose  : 
57  */
58 //============================================================================
59 SALOMEDS_SObject_i::~SALOMEDS_SObject_i()
60 {
61   CORBA::string_free(_value);
62   CORBA::string_free(_type);
63   CORBA::string_free(_name);
64 }
65   
66   
67 //============================================================================
68 /*! Function :
69  *  Purpose  : 
70  */
71 //============================================================================
72 char* SALOMEDS_SObject_i::GetID()
73 {
74   TCollection_AsciiString anEntry;
75   TDF_Tool::Entry (_lab,anEntry);
76   return CORBA::string_dup(anEntry.ToCString());
77 }
78   
79 //============================================================================
80 /*! Function :
81  *  Purpose  : 
82  */
83 //============================================================================
84 SALOMEDS::SComponent_ptr SALOMEDS_SObject_i::GetFatherComponent()
85 {
86   TDF_Label LF = _lab;
87   while (!SALOMEDS_SComponent_i::IsA(LF) && !LF.IsRoot()) {
88     LF = LF.Father();
89   }
90   SALOMEDS_SComponent_i *  so_servant = new SALOMEDS_SComponent_i (LF,_orb);
91   SALOMEDS::SComponent_var so;
92   so= SALOMEDS::SComponent::_narrow(so_servant->SComponent::_this()); 
93   return so;
94 }
95   
96 //============================================================================
97 /*! Function :
98  *  Purpose  : 
99  */
100 //============================================================================
101 SALOMEDS::SObject_ptr SALOMEDS_SObject_i::GetFather()
102 {
103   TDF_Label LF = _lab.Father();
104
105   SALOMEDS_SObject_i *  so_servant = new SALOMEDS_SObject_i (LF,_orb);
106   SALOMEDS::SObject_var so = SALOMEDS::SObject::_narrow(so_servant->_this()); 
107   return so;
108 }
109
110 //============================================================================
111 /*! Function :
112  *  Purpose  : 
113  */
114 //============================================================================
115 SALOMEDS::Study_ptr SALOMEDS_SObject_i::GetStudy()
116 {
117   TDF_Label Root = _lab.Root();
118   Handle(SALOMEDS_IORAttribute) Att;
119   char* IOR;
120   if (Root.FindAttribute(SALOMEDS_IORAttribute::GetID(),Att)){
121     TCollection_AsciiString ch(Att->Get());
122     IOR = CORBA::string_dup(ch.ToCString());
123     CORBA::Object_var obj = _orb->string_to_object(IOR);
124     SALOMEDS::Study_var Study = SALOMEDS::Study::_narrow(obj) ;
125     ASSERT(!CORBA::is_nil(Study));
126     return SALOMEDS::Study::_duplicate(Study); //return Study = abort...
127   }
128   MESSAGE("Problem GetStudy");
129   return SALOMEDS::Study::_nil();
130 }
131
132 //============================================================================
133 /*! Function : FindAttribute
134  *  Purpose  : Find attribute of given type on this SObject
135  */
136 //============================================================================
137 CORBA::Boolean SALOMEDS_SObject_i::FindAttribute (SALOMEDS::GenericAttribute_out anAttribute, 
138                                                   const char* aTypeOfAttribute)
139 {
140   Handle(TDF_Attribute) anAttr;
141   if (_lab.FindAttribute(SALOMEDS_GenericAttribute_i::GetGUID(aTypeOfAttribute), anAttr)) {
142     anAttribute = SALOMEDS::GenericAttribute::_duplicate(SALOMEDS_GenericAttribute_i::CreateAttribute(_orb, anAttr));
143     return Standard_True;
144   }
145   return Standard_False;
146 }
147
148 //============================================================================
149 /*! Function : GetAllAttributes
150  *  Purpose  : Returns list of all attributes for this sobject
151  */
152 //============================================================================
153
154 SALOMEDS::ListOfAttributes* SALOMEDS_SObject_i::GetAllAttributes()
155 {
156   Standard_Integer NumAttr = _lab.NbAttributes();
157   SALOMEDS::ListOfAttributes_var SeqOfAttr = new SALOMEDS::ListOfAttributes;
158   //SeqOfAttr->length(NumAttr);
159   if (NumAttr != 0) {
160     Standard_Integer i = 0;
161     for(TDF_AttributeIterator iter(_lab);iter.More();iter.Next()) {
162       Handle(TDF_Attribute) anAttr = iter.Value();
163       SALOMEDS::GenericAttribute_var anAttribute = SALOMEDS_GenericAttribute_i::CreateAttribute(_orb, anAttr);
164       if (!CORBA::is_nil(anAttribute)) {
165         SeqOfAttr->length(++i);
166         SeqOfAttr[i - 1] = anAttribute;
167       }
168     }
169   }
170   return SeqOfAttr._retn();
171 }
172
173
174 //============================================================================
175 /*! Function : ReferencedObject
176  *  Purpose  : 
177  */
178 //============================================================================
179 CORBA::Boolean SALOMEDS_SObject_i::ReferencedObject(SALOMEDS::SObject_out obj)
180 {
181   Handle(TDF_Reference) Ref;
182   if (!_lab.FindAttribute(TDF_Reference::GetID(),Ref))
183     return false;
184   
185   SALOMEDS_SObject_i *  so_servant = new SALOMEDS_SObject_i (Ref->Get(),_orb);
186   obj  = SALOMEDS::SObject::_narrow(so_servant->_this()); 
187   return true;
188 }
189
190 //============================================================================
191 /*! Function :
192  *  Purpose  : 
193  */
194 //============================================================================
195 CORBA::Boolean SALOMEDS_SObject_i::FindSubObject(long atag, SALOMEDS::SObject_out obj)
196 {
197   TDF_Label L = _lab.FindChild(atag,false);
198   if (L.IsNull()) return false;
199   
200   SALOMEDS_SObject_i *  so_servant = new SALOMEDS_SObject_i (L,_orb);
201   obj  = SALOMEDS::SObject::_narrow(so_servant->_this()); 
202   return true;
203     
204 }  
205
206 //============================================================================
207 /*! Function :
208  *  Purpose  : 
209  */
210 //============================================================================
211 char* SALOMEDS_SObject_i::Name()
212 {
213   return CORBA::string_dup(_name);
214 }
215   
216 //============================================================================
217 /*! Function :
218  *  Purpose  : 
219  */
220 //============================================================================
221 void  SALOMEDS_SObject_i::Name(const char* name)
222 {
223   _name = CORBA::string_dup(name);
224 }
225   
226 //============================================================================
227 /*! Function :
228  *  Purpose  : 
229  */
230 //============================================================================
231 CORBA::Short SALOMEDS_SObject_i::Tag()
232 {
233   return _lab.Tag();
234 }
235
236 //============================================================================
237 /*! Function :
238  *  Purpose  : 
239  */
240 //============================================================================
241 CORBA::Short SALOMEDS_SObject_i::Depth()
242 {
243   return _lab.Depth();
244 }
245
246 //============================================================================
247 /*! Function :
248  *  Purpose  : 
249  */
250 //============================================================================
251 CORBA::Object_ptr SALOMEDS_SObject_i::GetObject()
252 {
253   CORBA::Object_ptr obj = CORBA::Object::_nil();
254   try {
255     Handle(SALOMEDS_IORAttribute) Att;
256     if (_lab.FindAttribute(SALOMEDS_IORAttribute::GetID(),Att)) {
257       TCollection_AsciiString ch(Att->Get());
258       char* IOR = CORBA::string_dup(ch.ToCString());
259       obj = _orb->string_to_object(IOR);
260     }
261   } catch(...) {}
262   return obj;
263 }
264
265 //============================================================================
266 /*! Function :
267  *  Purpose  : 
268  */
269 //============================================================================
270 char* SALOMEDS_SObject_i::GetName() {
271   CORBA::String_var aStr = CORBA::string_dup( "" );
272   Handle(TDataStd_Name) aName;
273   if (_lab.FindAttribute(TDataStd_Name::GetID(), aName)) {
274     aStr = CORBA::string_dup(TCollection_AsciiString(aName->Get()).ToCString());
275   }
276   return aStr._retn();
277 }
278
279 //============================================================================
280 /*! Function :
281  *  Purpose  : 
282  */
283 //============================================================================
284 char* SALOMEDS_SObject_i::GetComment() {
285   CORBA::String_var aStr = CORBA::string_dup( "" );
286   Handle(TDataStd_Comment) aComment;
287   if (_lab.FindAttribute(TDataStd_Comment::GetID(), aComment)) {
288     aStr = CORBA::string_dup(TCollection_AsciiString(aComment->Get()).ToCString());
289   }
290   return aStr._retn();
291 }
292
293 //============================================================================
294 /*! Function :
295  *  Purpose  : 
296  */
297 //============================================================================
298 char* SALOMEDS_SObject_i::GetIOR() {
299   CORBA::String_var aStr = CORBA::string_dup( "" );
300   Handle(SALOMEDS_IORAttribute) anIOR;
301   if (_lab.FindAttribute(SALOMEDS_IORAttribute::GetID(), anIOR)) {
302     aStr = CORBA::string_dup(TCollection_AsciiString(anIOR->Get()).ToCString());
303   }
304   return aStr._retn();
305 }