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