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