Salome HOME
Dump Python extension
[modules/geom.git] / src / GEOM_I / GEOM_Object_i.cc
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 #include <Standard_OStream.hxx>
21
22 #include <GEOM_Object_i.hh>
23 //#include <GEOM_Gen_i.hh>
24 #include <GEOM_ISubShape.hxx>
25 #include <GEOMImpl_Types.hxx>
26
27 #include "utilities.h"
28 #include <fstream>
29 #include <strstream>
30
31 #include <OpUtil.hxx>
32 #include <Utils_ExceptHandlers.hxx>
33 #include <TDF_Tool.hxx>
34 #include <TDF_Label.hxx>
35 #include <TCollection_AsciiString.hxx>
36
37 #include <BRepTools_ShapeSet.hxx>
38 #include <BRepTools.hxx>
39 #include <TopAbs.hxx>
40
41 #ifdef WNT
42 #pragma warning( disable:4786 )
43 #endif
44
45 //=============================================================================
46 /*!
47  *   constructor:
48  */
49 //=============================================================================
50
51 GEOM_Object_i::GEOM_Object_i (PortableServer::POA_ptr thePOA, GEOM::GEOM_Gen_ptr theEngine,
52                               Handle(GEOM_Object) theImpl)
53 : SALOME::GenericObj_i( thePOA ), _engine(theEngine), _impl(theImpl)
54 {
55 }
56
57 //=============================================================================
58 /*!
59  *  destructor
60  */
61 //=============================================================================
62
63 GEOM_Object_i::~GEOM_Object_i()
64 {
65   GEOM_Engine::GetEngine()->RemoveObject(_impl);
66 }
67
68
69 //=============================================================================
70 /*!
71  *  GetEntry
72  */
73 //=============================================================================
74 char* GEOM_Object_i::GetEntry()
75 {
76   const TDF_Label& aLabel = _impl->GetEntry();
77   TCollection_AsciiString anEntry;
78   TDF_Tool::Entry(aLabel, anEntry);
79   const char* anEntstr = anEntry.ToCString();
80   return CORBA::string_dup(anEntstr);
81 }
82
83 //=============================================================================
84 /*!
85  *  GetStudyID
86  */
87 //=============================================================================
88 CORBA::Long GEOM_Object_i::GetStudyID()
89 {
90    return _impl->GetDocID();
91 }
92
93
94 //=============================================================================
95 /*!
96  *  GetType
97  */
98 //=============================================================================
99 CORBA::Long GEOM_Object_i::GetType()
100 {
101   return _impl->GetType();
102 }
103
104 //=============================================================================
105 /*!
106  *  GetShapeType
107  */
108 //=============================================================================
109 GEOM::shape_type GEOM_Object_i::GetShapeType()
110 {
111   TopoDS_Shape _geom = _impl->GetValue();
112   if(_geom.IsNull()) return GEOM::SHAPE;
113   return (GEOM::shape_type)_geom.ShapeType();
114 }
115
116 //=============================================================================
117 /*!
118  *  SetName
119  */
120 //=============================================================================
121 void GEOM_Object_i::SetName(const char* theName)
122 {
123   _impl->SetName(theName);
124 }
125
126
127 //=============================================================================
128 /*!
129  *  GetName
130  */
131 //=============================================================================
132 char* GEOM_Object_i::GetName()
133 {
134   char* aName = _impl->GetName();
135   if (aName)
136     return aName; // this is already copy of pointer (see implementation of _impl)
137   return strdup("");
138 }
139
140 //=============================================================================
141 /*!
142  *  SetColor
143  */
144 //=============================================================================
145 void GEOM_Object_i::SetColor(const SALOMEDS::Color& theColor)
146 {
147   _impl->SetColor(theColor);
148 }
149
150
151 //=============================================================================
152 /*!
153  *  GetColor
154  */
155 //=============================================================================
156 SALOMEDS::Color GEOM_Object_i::GetColor()
157 {
158   return _impl->GetColor();
159 }
160
161
162 //=============================================================================
163 /*!
164  *  SetAutoColor
165  */
166 //=============================================================================
167 void GEOM_Object_i::SetAutoColor(CORBA::Boolean theAutoColor)
168 {
169   _impl->SetAutoColor(theAutoColor);
170 }
171
172
173 //=============================================================================
174 /*!
175  *  GetAutoColor
176  */
177 //=============================================================================
178 CORBA::Boolean GEOM_Object_i::GetAutoColor()
179 {
180   return _impl->GetAutoColor();
181 }
182
183
184 //=============================================================================
185 /*!
186  *  SetStudyEntry
187  */
188 //=============================================================================
189 void GEOM_Object_i::SetStudyEntry(const char* theEntry)
190 {
191   _impl->SetAuxData(theEntry);
192 }
193
194
195 //=============================================================================
196 /*!
197  *  GetStudyEntry
198  */
199 //=============================================================================
200 char* GEOM_Object_i::GetStudyEntry()
201 {
202   TCollection_AsciiString anEntry = _impl->GetAuxData();
203   if(!anEntry.IsEmpty()) return CORBA::string_dup(anEntry.ToCString());
204   return CORBA::string_dup("");
205 }
206
207
208 //=============================================================================
209 /*!
210  *  GetDependency
211  */
212 //=============================================================================
213 GEOM::ListOfGO* GEOM_Object_i::GetDependency()
214 {
215   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
216   aList->length(0);
217
218   Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetAllDependency();
219   if (aSeq.IsNull()) return aList._retn();
220   int aLength = aSeq->Length();
221   if (aLength == 0) return aList._retn();
222
223   aList->length(aLength);
224
225   TCollection_AsciiString anEntry;
226
227   for (int i = 1; i<=aLength; i++) {
228     Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
229     if (anObj.IsNull()) continue;
230     TDF_Tool::Entry(anObj->GetEntry(), anEntry);
231     GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
232     aList[i-1] = obj;
233   }
234
235   return aList._retn();
236 }
237
238 //=============================================================================
239 /*!
240  * GetLastDependency
241  */
242 //=============================================================================
243 GEOM::ListOfGO* GEOM_Object_i::GetLastDependency()
244 {
245   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
246   aList->length(0);
247
248   Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetLastDependency();
249   if (aSeq.IsNull()) return aList._retn();
250   int aLength = aSeq->Length();
251   if (aLength == 0) return aList._retn();
252
253   aList->length(aLength);
254
255   TCollection_AsciiString anEntry;
256
257   for (int i = 1; i<=aLength; i++) {
258      Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
259      if (anObj.IsNull()) continue;
260      TDF_Tool::Entry(anObj->GetEntry(), anEntry);
261      GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
262      aList[i-1] = obj;
263   }
264
265   return aList._retn();
266 }
267
268 //=================================================================================
269 // function : GetShapeStream
270 // Transfer resulting shape to client as sequence of bytes
271 //client can extract shape from stream using BrepTools::Read function
272 //=================================================================================
273 SALOMEDS::TMPFile* GEOM_Object_i::GetShapeStream()
274 {
275   TopoDS_Shape aShape = _impl->GetValue();
276
277   if(aShape.IsNull()) return NULL;
278
279   ostrstream streamShape;
280   //Write TopoDS_Shape in ASCII format to the stream
281   BRepTools::Write(aShape, streamShape);
282   //Returns the number of bytes that have been stored in the stream's buffer.
283   int size = streamShape.pcount();
284   char* buf = new char [size];
285   //Get pointer on internal character array in ostrstream
286   char* valueOfStream = streamShape.str();
287   //Create copy of ostrstream content
288   memcpy(buf, valueOfStream, size);
289   //Allow automatic deletion of ostrstream content
290   streamShape.rdbuf()->freeze(0);
291
292   CORBA::Octet* OctetBuf =  (CORBA::Octet*)buf;
293   SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
294   return SeqFile._retn();
295
296 }
297
298
299 //=======================================================================
300 //function : getShape
301 //purpose  : return the TopoDS_Shape when client and servant are colocated, be careful
302 //=======================================================================
303 CORBA::LongLong GEOM_Object_i::getShape() {
304   _geom = _impl->GetValue();
305   return ((CORBA::LongLong)(&_geom));
306 }
307
308 //=============================================================================
309 /*!
310  *  GetSubShapeIndices
311  */
312 //=============================================================================
313 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
314 {
315   GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
316
317   if(!_impl->IsMainShape()) {
318     Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last)  one)
319     if(aFunction.IsNull()) return anIndices._retn();
320     GEOM_ISubShape ISS(aFunction);
321     Handle(TColStd_HArray1OfInteger) anArray = ISS.GetIndices();
322     if(anArray.IsNull() || anArray->Length() < 1) return anIndices._retn();
323     anIndices->length(anArray->Length());
324     for(int i=1; i<=anArray->Length(); i++) anIndices[i-1] = anArray->Value(i);
325   }
326   else {
327     anIndices->length(0);
328   }
329
330   return anIndices._retn();
331 }
332
333
334 //=============================================================================
335 /*!
336  *  GetMainShape
337  */
338 //=============================================================================
339 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
340 {
341   GEOM::GEOM_Object_var obj;
342   if(!_impl->IsMainShape()) {
343     Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last)  one)
344     if(aFunction.IsNull()) return obj._retn();
345     GEOM_ISubShape ISS(aFunction);
346
347     aFunction = ISS.GetMainShape();
348     if(aFunction.IsNull()) return obj._retn();
349     TDF_Label aLabel  = aFunction->GetOwnerEntry();
350     if(aLabel.IsNull()) return obj._retn();
351     TCollection_AsciiString anEntry;
352     TDF_Tool::Entry(aLabel, anEntry);
353     return GEOM::GEOM_Object::_duplicate(_engine->GetObject(_impl->GetDocID(), anEntry.ToCString()));
354   }
355
356   return obj._retn();
357 }
358
359 bool GEOM_Object_i::IsShape()
360 {
361   return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;
362 }
363
364 void GEOM_Object_i::SetParameters(const char* theParameters)
365 {
366   _impl->SetParameters((char*)theParameters);
367 }
368
369 char* GEOM_Object_i::GetParameters()
370 {
371   return CORBA::string_dup(_impl->GetParameters().ToCString());
372 }
373