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