Salome HOME
SMH: Merged GEOM (NEWGUI, HEAD, POLYWORK)
[modules/geom.git] / src / GEOM_I / GEOM_Object_i.cc
1 using namespace std;
2
3 #include "GEOM_Object_i.hh"
4 //#include "GEOM_Gen_i.hh"
5 #include "GEOM_ISubShape.hxx"
6 #include "GEOMImpl_Types.hxx"
7
8 #include "utilities.h"
9 #include "OpUtil.hxx"
10 #include "Utils_ExceptHandlers.hxx"
11 #include <TDF_Tool.hxx>
12 #include <TDF_Label.hxx>
13 #include <TCollection_AsciiString.hxx>
14
15 #include <fstream.h>
16 #include <strstream>
17
18 #include <BRepTools_ShapeSet.hxx>
19 #include <BRepTools.hxx>
20 #include <TopAbs.hxx>
21
22 //=============================================================================
23 /*!
24  *   constructor:
25  */
26 //=============================================================================
27
28 GEOM_Object_i::GEOM_Object_i (PortableServer::POA_ptr thePOA, GEOM::GEOM_Gen_ptr theEngine,
29                               Handle(GEOM_Object) theImpl)
30 :SALOME::GenericObj_i( thePOA ), _engine(theEngine), _impl(theImpl)
31 {
32   thePOA->activate_object(this);
33 }
34
35 //=============================================================================
36 /*!
37  *  destructor
38  */
39 //=============================================================================
40
41 GEOM_Object_i::~GEOM_Object_i()
42 {}
43
44
45 //=============================================================================
46 /*!
47  *  GetEntry
48  */
49 //=============================================================================
50 char* GEOM_Object_i::GetEntry()
51 {
52   const TDF_Label& aLabel = _impl->GetEntry();
53   TCollection_AsciiString anEntry;
54   TDF_Tool::Entry(aLabel, anEntry);
55   return CORBA::string_dup(anEntry.ToCString());
56 }
57
58 //=============================================================================
59 /*!
60  *  GetStudyID
61  */
62 //=============================================================================
63 CORBA::Long GEOM_Object_i::GetStudyID()
64 {
65    return _impl->GetDocID();
66 }
67
68
69 //=============================================================================
70 /*!
71  *  GetType
72  */
73 //=============================================================================
74 CORBA::Long GEOM_Object_i::GetType()
75 {
76   return _impl->GetType();
77 }
78
79 //=============================================================================
80 /*!
81  *  GetShapeType
82  */
83 //=============================================================================
84 GEOM::shape_type GEOM_Object_i::GetShapeType()
85 {
86   TopoDS_Shape _geom = _impl->GetValue();
87   if(_geom.IsNull()) return GEOM::SHAPE;
88   return (GEOM::shape_type)_geom.ShapeType();
89 }
90
91 //=============================================================================
92 /*!
93  *  SetName
94  */
95 //=============================================================================
96 void GEOM_Object_i::SetName(const char* theName)
97 {
98   _impl->SetName(theName);
99 }
100
101
102 //=============================================================================
103 /*!
104  *  GetName
105  */
106 //=============================================================================
107 char* GEOM_Object_i::GetName()
108 {
109   char* aName = _impl->GetName();
110   if(aName) return strdup(aName);
111   return strdup("");
112 }
113
114 //=============================================================================
115 /*!
116  *  SetStudyEntry
117  */
118 //=============================================================================
119 void GEOM_Object_i::SetStudyEntry(const char* theEntry)
120 {
121   _impl->SetAuxData(theEntry);
122 }
123
124
125 //=============================================================================
126 /*!
127  *  GetStudyEntry
128  */
129 //=============================================================================
130 char* GEOM_Object_i::GetStudyEntry()
131 {
132   TCollection_AsciiString anEntry = _impl->GetAuxData();
133   if(!anEntry.IsEmpty()) return strdup(anEntry.ToCString());
134   return strdup("");
135 }
136
137
138 //=============================================================================
139 /*!
140  *  GetDependency
141  */
142 //=============================================================================
143 GEOM::ListOfGO* GEOM_Object_i::GetDependency()
144 {
145   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
146   aList->length(0);
147
148   Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetAllDependency();
149   if (aSeq.IsNull()) return aList._retn();
150   int aLength = aSeq->Length();
151   if (aLength == 0) return aList._retn();
152
153   aList->length(aLength);
154
155   TCollection_AsciiString anEntry;
156
157   for (int i = 1; i<=aLength; i++) {
158     Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
159     if (anObj.IsNull()) continue;
160     TDF_Tool::Entry(anObj->GetEntry(), anEntry);
161     GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
162     aList[i-1] = obj;
163   }
164
165   return aList._retn();
166 }
167
168 //=============================================================================
169 /*!
170  * GetLastDependency
171  */
172 //=============================================================================
173 GEOM::ListOfGO* GEOM_Object_i::GetLastDependency()
174 {
175   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
176   aList->length(0);
177
178   Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetLastDependency();
179   if (aSeq.IsNull()) return aList._retn();
180   int aLength = aSeq->Length();
181   if (aLength == 0) return aList._retn();
182
183   aList->length(aLength);
184
185   TCollection_AsciiString anEntry;
186
187   for (int i = 1; i<=aLength; i++) {
188      Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
189      if (anObj.IsNull()) continue;
190      TDF_Tool::Entry(anObj->GetEntry(), anEntry);
191      GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
192      aList[i-1] = obj;
193   }
194
195   return aList._retn();
196 }
197
198 //=================================================================================
199 // function : GetShapeStream
200 // Transfer resulting shape to client as sequence of bytes
201 //client can extract shape from stream using BrepTools::Read function
202 //=================================================================================
203 SALOMEDS::TMPFile* GEOM_Object_i::GetShapeStream()
204 {
205   TopoDS_Shape aShape = _impl->GetValue();
206
207   if(aShape.IsNull()) return NULL;
208
209   ostrstream streamShape;
210   //Write TopoDS_Shape in ASCII format to the stream
211   BRepTools::Write(aShape, streamShape);
212   //Returns the number of bytes that have been stored in the stream's buffer.
213   int size = streamShape.pcount();
214   char* buf = new char [size];
215   //Get pointer on internal character array in ostrstream
216   char* valueOfStream = streamShape.str();
217   //Create copy of ostrstream content
218   memcpy(buf, valueOfStream, size);
219   //Allow automatic deletion of ostrstream content
220   streamShape.rdbuf()->freeze(0);
221
222   CORBA::Octet* OctetBuf =  (CORBA::Octet*)buf;
223   SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
224   return SeqFile._retn();
225
226 }
227
228
229 //=======================================================================
230 //function : getShape
231 //purpose  : return the TopoDS_Shape when client and servant are colocated, be careful
232 //=======================================================================
233 long GEOM_Object_i::getShape() {
234   _geom = _impl->GetValue();
235   return((long)(&_geom));
236 }
237
238 //=============================================================================
239 /*!
240  *  GetSubShapeIndices
241  */
242 //=============================================================================
243 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
244 {
245   GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
246
247   if(!_impl->IsMainShape()) {
248     Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last)  one)
249     if(aFunction.IsNull()) return anIndices._retn();
250     GEOM_ISubShape ISS(aFunction);
251     Handle(TColStd_HArray1OfInteger) anArray = ISS.GetIndices();
252     if(anArray.IsNull() || anArray->Length() < 1) return anIndices._retn();
253     anIndices->length(anArray->Length());
254     for(int i=1; i<=anArray->Length(); i++) anIndices[i-1] = anArray->Value(i);
255   }
256   else {
257     anIndices->length(0);
258   }
259
260   return anIndices._retn();
261 }
262
263
264 //=============================================================================
265 /*!
266  *  GetMainShape
267  */
268 //=============================================================================
269 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
270 {
271   GEOM::GEOM_Object_var obj;
272   if(!_impl->IsMainShape()) {
273     Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last)  one)
274     if(aFunction.IsNull()) return obj._retn();
275     GEOM_ISubShape ISS(aFunction);
276
277     aFunction = ISS.GetMainShape();
278     if(aFunction.IsNull()) return obj._retn();
279     TDF_Label aLabel  = aFunction->GetOwnerEntry();
280     if(aLabel.IsNull()) return obj._retn();
281     TCollection_AsciiString anEntry;
282     TDF_Tool::Entry(aLabel, anEntry);
283     return GEOM::GEOM_Object::_duplicate(_engine->GetObject(_impl->GetDocID(), anEntry.ToCString()));
284   }
285
286   return obj._retn();
287 }
288
289 bool GEOM_Object_i::IsShape()
290 {
291   return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;
292 }