Salome HOME
merge from branch BR_V5_DEV
[modules/yacs.git] / src / SALOMEDS / SALOMEDS_SObject_i.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 //  File   : SALOMEDS_SObject_i.cxx
23 //  Author : Sergey RUIN
24 //  Module : SALOME
25 //
26 #include "utilities.h"
27 #include "SALOMEDS_SObject_i.hxx"
28 #include "SALOMEDS_SComponent_i.hxx"
29 #include "SALOMEDS_GenericAttribute_i.hxx"
30 #include "SALOMEDS_StudyManager_i.hxx"
31 #include "SALOMEDS.hxx"
32 #include "SALOMEDSImpl_GenericAttribute.hxx"
33 #include "SALOMEDSImpl_SComponent.hxx"
34 #include "SALOMEDSImpl_Study.hxx"
35 #include "SALOMEDSImpl_AttributeIOR.hxx"
36 #include "Basics_Utils.hxx"
37
38 #include <map>
39
40 #ifdef WIN32
41 #include <process.h>
42 #else
43 #include <sys/types.h>
44 #include <unistd.h>
45 #endif
46
47 using namespace std;
48
49 SALOMEDS::SObject_ptr SALOMEDS_SObject_i::New(const SALOMEDSImpl_SObject& theImpl, CORBA::ORB_ptr theORB)
50 {
51   SALOMEDS_SObject_i* so_servant = new SALOMEDS_SObject_i(theImpl, theORB);
52   SALOMEDS::SObject_var so  = SALOMEDS::SObject::_narrow(so_servant->_this());
53
54   return so._retn();
55 }
56
57
58 //============================================================================
59 /*! Function : constructor
60  *  Purpose  :
61  */
62 //============================================================================
63 SALOMEDS_SObject_i::SALOMEDS_SObject_i(const SALOMEDSImpl_SObject& impl, CORBA::ORB_ptr orb)
64 {
65   if(!impl.IsNull()) {
66      if(impl.IsComponent()) {
67          SALOMEDSImpl_SComponent sco = impl;
68          _impl = sco.GetPersistentCopy();       
69      }
70      else {
71          _impl = impl.GetPersistentCopy();
72      }
73   }
74   _orb = CORBA::ORB::_duplicate(orb);
75    //SALOME::GenericObj_i::myPOA = SALOMEDS_StudyManager_i::GetPOA(GetStudy());
76 }
77
78
79 //============================================================================
80 /*! Function : destructor
81  *  Purpose  :
82  */
83 //============================================================================
84 SALOMEDS_SObject_i::~SALOMEDS_SObject_i()
85 {
86    if(_impl) delete _impl;    
87 }
88
89
90 //============================================================================
91 /*! Function :GetID
92  *  Purpose  :
93  */
94 //============================================================================
95 char* SALOMEDS_SObject_i::GetID()
96 {
97   SALOMEDS::Locker lock;
98   return CORBA::string_dup(_impl->GetID().c_str());
99 }
100
101 //============================================================================
102 /*! Function : GetFatherComponent
103  *  Purpose  :
104  */
105 //============================================================================
106 SALOMEDS::SComponent_ptr SALOMEDS_SObject_i::GetFatherComponent()
107 {
108   SALOMEDS::Locker lock;
109   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (_impl->GetFatherComponent(), _orb);
110   return sco._retn();
111 }
112
113 //============================================================================
114 /*! Function : GetFather
115  *  Purpose  :
116  */
117 //============================================================================
118 SALOMEDS::SObject_ptr SALOMEDS_SObject_i::GetFather()
119 {
120   SALOMEDS::Locker lock;
121   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (_impl->GetFather(), _orb);
122   return so._retn();
123 }
124
125 //============================================================================
126 /*! Function :
127  *  Purpose  :
128  */
129 //============================================================================
130 SALOMEDS::Study_ptr SALOMEDS_SObject_i::GetStudy()
131 {
132   SALOMEDS::Locker lock;
133   SALOMEDSImpl_Study* aStudy = _impl->GetStudy();
134   if(!aStudy) {
135     MESSAGE("Problem GetStudy");
136     return SALOMEDS::Study::_nil();
137   }
138
139   string IOR = aStudy->GetTransientReference();
140   CORBA::Object_var obj = _orb->string_to_object(IOR.c_str());
141   SALOMEDS::Study_var Study = SALOMEDS::Study::_narrow(obj) ;
142   ASSERT(!CORBA::is_nil(Study));
143   return SALOMEDS::Study::_duplicate(Study);
144 }
145
146 //============================================================================
147 /*! Function : FindAttribute
148  *  Purpose  : Find attribute of given type on this SObject
149  */
150 //============================================================================
151 CORBA::Boolean SALOMEDS_SObject_i::FindAttribute (SALOMEDS::GenericAttribute_out anAttribute,
152                                                   const char* aTypeOfAttribute)
153 {
154   SALOMEDS::Locker lock;
155   DF_Attribute* anAttr = NULL;
156   if(_impl->FindAttribute(anAttr, (char*)aTypeOfAttribute)) {
157     anAttribute = SALOMEDS::GenericAttribute::_duplicate(SALOMEDS_GenericAttribute_i::CreateAttribute(anAttr, _orb));
158     return true;
159   }
160
161   return false;
162 }
163
164 //============================================================================
165 /*! Function : GetAllAttributes
166  *  Purpose  : Returns list of all attributes for this sobject
167  */
168 //============================================================================
169
170 SALOMEDS::ListOfAttributes* SALOMEDS_SObject_i::GetAllAttributes()
171 {
172   SALOMEDS::Locker lock;
173   vector<DF_Attribute*> aSeq = _impl->GetAllAttributes();
174   SALOMEDS::ListOfAttributes_var SeqOfAttr = new SALOMEDS::ListOfAttributes;
175   int length = aSeq.size();
176
177   SeqOfAttr->length(length);
178
179   if (length != 0) {
180     for(int i = 0; i < length; i++) {
181       SALOMEDSImpl_GenericAttribute* anAttr = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(aSeq[i]);
182       SALOMEDS::GenericAttribute_var anAttribute;
183       anAttribute = SALOMEDS::GenericAttribute::_duplicate(SALOMEDS_GenericAttribute_i::CreateAttribute(anAttr, _orb));
184       if (!CORBA::is_nil(anAttribute)) {
185         SeqOfAttr[i] = anAttribute;
186       }
187     }
188   }
189   return SeqOfAttr._retn();
190 }
191
192
193 //============================================================================
194 /*! Function : ReferencedObject
195  *  Purpose  :
196  */
197 //============================================================================
198 CORBA::Boolean SALOMEDS_SObject_i::ReferencedObject(SALOMEDS::SObject_out obj)
199 {
200   SALOMEDS::Locker lock;
201   SALOMEDSImpl_SObject aRefObj;
202   if(!_impl->ReferencedObject(aRefObj)) return false;
203
204   obj = SALOMEDS_SObject_i::New (aRefObj, _orb);
205   return true;
206 }
207
208 //============================================================================
209 /*! Function : FindSubObject
210  *  Purpose  :
211  */
212 //============================================================================
213 CORBA::Boolean SALOMEDS_SObject_i::FindSubObject(CORBA::Long atag, SALOMEDS::SObject_out obj)
214 {
215   SALOMEDS::Locker lock;
216   SALOMEDSImpl_SObject aSubObj;
217   if(!_impl->FindSubObject(atag, aSubObj)) return false;
218
219   obj = SALOMEDS_SObject_i::New (aSubObj, _orb);
220   return true;
221
222 }
223
224 //============================================================================
225 /*! Function : Name
226  *  Purpose  : gets a name
227  */
228 //============================================================================
229 char* SALOMEDS_SObject_i::Name()
230 {
231   SALOMEDS::Locker lock;
232   return CORBA::string_dup(_impl->Name().c_str());
233 }
234
235 //============================================================================
236 /*! Function : Name
237  *  Purpose  : sets a name
238  */
239 //============================================================================
240 void  SALOMEDS_SObject_i::Name(const char* name)
241 {
242   SALOMEDS::Locker lock;
243   string aName((char*)name);
244   _impl->Name(aName);
245 }
246
247 //============================================================================
248 /*! Function : Tag
249  *  Purpose  :
250  */
251 //============================================================================
252 CORBA::Short SALOMEDS_SObject_i::Tag()
253 {
254   SALOMEDS::Locker lock;
255   return _impl->Tag();
256 }
257
258 //============================================================================
259 /*! Function : Depth
260  *  Purpose  :
261  */
262 //============================================================================
263 CORBA::Short SALOMEDS_SObject_i::Depth()
264 {
265   SALOMEDS::Locker lock;
266   return _impl->Depth();
267 }
268
269 //============================================================================
270 /*! Function : GetObject
271  *  Purpose  :
272  */
273 //============================================================================
274 CORBA::Object_ptr SALOMEDS_SObject_i::GetObject()
275 {
276   SALOMEDS::Locker lock;
277   CORBA::Object_ptr obj = CORBA::Object::_nil();
278   try {
279     string IOR = _impl->GetIOR();
280     char* c_ior = CORBA::string_dup(IOR.c_str());
281     obj = _orb->string_to_object(c_ior);
282     CORBA::string_free(c_ior);
283   } catch(...) {}
284   return obj;
285 }
286
287 //============================================================================
288 /*! Function : GetName
289  *  Purpose  :
290  */
291 //============================================================================
292 char* SALOMEDS_SObject_i::GetName()
293 {
294   SALOMEDS::Locker lock;
295   CORBA::String_var aStr = CORBA::string_dup(_impl->GetName().c_str());
296   return aStr._retn();
297 }
298
299 //============================================================================
300 /*! Function : GetComment
301  *  Purpose  :
302  */
303 //============================================================================
304 char* SALOMEDS_SObject_i::GetComment()
305 {
306   SALOMEDS::Locker lock;
307   CORBA::String_var aStr = CORBA::string_dup(_impl->GetComment().c_str());
308   return aStr._retn();
309 }
310
311 //============================================================================
312 /*! Function : GetIOR
313  *  Purpose  :
314  */
315 //============================================================================
316 char* SALOMEDS_SObject_i::GetIOR()
317 {
318   SALOMEDS::Locker lock;
319   CORBA::String_var aStr = CORBA::string_dup(_impl->GetIOR().c_str());
320   return aStr._retn();
321 }
322
323 //===========================================================================
324 //   PRIVATE FUNCTIONS
325 //===========================================================================
326 CORBA::LongLong SALOMEDS_SObject_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
327 {
328 #ifdef WIN32
329   long pid = (long)_getpid();
330 #else
331   long pid = (long)getpid();
332 #endif
333   isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
334   return reinterpret_cast<CORBA::LongLong>(_impl);
335 }