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