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