Salome HOME
Mantis issue 0021071: Regression in GetInPlace. A fix by PKV on Partition error.
[modules/geom.git] / src / GEOM_I / GEOM_Object_i.cc
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <Standard_OStream.hxx>
24
25 #include <GEOM_Object_i.hh>
26 #include <GEOM_ISubShape.hxx>
27 #include <GEOMImpl_Types.hxx>
28
29 #include "utilities.h"
30 #include <fstream>
31 #include <sstream>
32
33 #include <OpUtil.hxx>
34 #include <Utils_ExceptHandlers.hxx>
35 #include <TDF_Tool.hxx>
36 #include <TDF_Label.hxx>
37 #include <TCollection_AsciiString.hxx>
38
39 #include <BRepTools_ShapeSet.hxx>
40 #include <BRepTools.hxx>
41 #include <TopAbs.hxx>
42 #include <TopoDS_Iterator.hxx>
43
44 #ifdef WNT
45 #pragma warning( disable:4786 )
46 #endif
47
48 //=============================================================================
49 /*!
50  *   constructor:
51  */
52 //=============================================================================
53
54 GEOM_Object_i::GEOM_Object_i (PortableServer::POA_ptr thePOA, GEOM::GEOM_Gen_ptr theEngine,
55                               Handle(GEOM_Object) theImpl)
56 : SALOME::GenericObj_i( thePOA ), _engine(theEngine), _impl(theImpl)
57 {
58 }
59
60 //=============================================================================
61 /*!
62  *  destructor
63  */
64 //=============================================================================
65
66 GEOM_Object_i::~GEOM_Object_i()
67 {
68   MESSAGE("GEOM_Object_i::~GEOM_Object_i");
69   GEOM_Engine::GetEngine()->RemoveObject(_impl);
70 }
71
72
73 //=============================================================================
74 /*!
75  *  GetEntry
76  */
77 //=============================================================================
78 char* GEOM_Object_i::GetEntry()
79 {
80   const TDF_Label& aLabel = _impl->GetEntry();
81   TCollection_AsciiString anEntry;
82   TDF_Tool::Entry(aLabel, anEntry);
83   const char* anEntstr = anEntry.ToCString();
84   return CORBA::string_dup(anEntstr);
85 }
86
87 //=============================================================================
88 /*!
89  *  GetStudyID
90  */
91 //=============================================================================
92 CORBA::Long GEOM_Object_i::GetStudyID()
93 {
94    return _impl->GetDocID();
95 }
96
97
98 //=============================================================================
99 /*!
100  *  GetType
101  */
102 //=============================================================================
103 CORBA::Long GEOM_Object_i::GetType()
104 {
105   return _impl->GetType();
106 }
107
108 //=============================================================================
109 /*!
110  *  GetShapeType
111  */
112 //=============================================================================
113 GEOM::shape_type GEOM_Object_i::GetShapeType()
114 {
115   TopoDS_Shape _geom = _impl->GetValue();
116   if(_geom.IsNull()) return GEOM::SHAPE;
117   return (GEOM::shape_type)_geom.ShapeType();
118 }
119
120 //=============================================================================
121 /*!
122  *  GetTopologyType
123  */
124 //=============================================================================
125 GEOM::shape_type GEOM_Object_i::GetTopologyType()
126 {
127   TopoDS_Shape shape = _impl->GetValue();
128   if(shape.IsNull()) return GEOM::SHAPE;
129
130   if ( shape.ShapeType() == TopAbs_COMPOUND || shape.ShapeType() == TopAbs_COMPSOLID ) {
131     TopoDS_Shape shape_i;
132     TopoDS_Iterator It (shape, Standard_True, Standard_False);
133     for (; It.More(); It.Next()) {
134       if ( !shape_i.IsNull() ) return (GEOM::shape_type)shape.ShapeType();
135       shape_i = It.Value();
136     }
137     if ( !shape_i.IsNull() )
138       return (GEOM::shape_type) shape_i.ShapeType();
139   }
140
141   return (GEOM::shape_type)shape.ShapeType();
142 }
143
144 //=============================================================================
145 /*!
146  *  SetName
147  */
148 //=============================================================================
149 void GEOM_Object_i::SetName(const char* theName)
150 {
151   _impl->SetName(theName);
152 }
153
154
155 //=============================================================================
156 /*!
157  *  GetName
158  */
159 //=============================================================================
160 char* GEOM_Object_i::GetName()
161 {
162   char* aName = _impl->GetName();
163   if (aName)
164     return aName; // this is already copy of pointer (see implementation of _impl)
165   return strdup("");
166 }
167
168 //=============================================================================
169 /*!
170  *  SetColor
171  */
172 //=============================================================================
173 void GEOM_Object_i::SetColor(const SALOMEDS::Color& theColor)
174 {
175   _impl->SetColor(theColor);
176 }
177
178
179 //=============================================================================
180 /*!
181  *  GetColor
182  */
183 //=============================================================================
184 SALOMEDS::Color GEOM_Object_i::GetColor()
185 {
186   return _impl->GetColor();
187 }
188
189
190 //=============================================================================
191 /*!
192  *  SetAutoColor
193  */
194 //=============================================================================
195 void GEOM_Object_i::SetAutoColor(CORBA::Boolean theAutoColor)
196 {
197   _impl->SetAutoColor(theAutoColor);
198 }
199
200
201 //=============================================================================
202 /*!
203  *  GetAutoColor
204  */
205 //=============================================================================
206 CORBA::Boolean GEOM_Object_i::GetAutoColor()
207 {
208   return _impl->GetAutoColor();
209 }
210
211
212 //=============================================================================
213 /*!
214  *  SetMarkerStd
215  */
216 //=============================================================================
217 void GEOM_Object_i::SetMarkerStd(GEOM::marker_type theType, GEOM::marker_size theSize)
218 {
219   if ( theType == GEOM::MT_NONE || theSize == GEOM::MS_NONE ) {
220     _impl->UnsetMarker();
221   }
222   else {
223     Aspect_TypeOfMarker aType = (Aspect_TypeOfMarker)( (int)theType-1 );
224     double aSize = ((int)theSize+1)*0.5;
225     _impl->SetMarkerStd( aType, aSize );
226   }
227 }
228
229
230 //=============================================================================
231 /*!
232  *  SetMarkerTexture
233  */
234 //=============================================================================
235 void GEOM_Object_i::SetMarkerTexture(CORBA::Long theTextureId)
236 {
237   _impl->SetMarkerTexture( theTextureId );
238 }
239
240
241 //=============================================================================
242 /*!
243  *  GetMarkerType
244  */
245 //=============================================================================
246 GEOM::marker_type GEOM_Object_i::GetMarkerType()
247 {
248   return (GEOM::marker_type)( (int)_impl->GetMarkerType()+1 );
249 }
250
251
252 //=============================================================================
253 /*!
254  *  GetMarkerSize
255  */
256 //=============================================================================
257 GEOM::marker_size GEOM_Object_i::GetMarkerSize()
258 {
259   int aSize = (int)( _impl->GetMarkerSize()/0.5 ) - 1;
260   return aSize < GEOM::MS_10 || aSize > GEOM::MS_70 ? GEOM::MS_NONE : (GEOM::marker_size)aSize;
261 }
262
263
264 //=============================================================================
265 /*!
266  *  GetMarkerTexture
267  */
268 //=============================================================================
269 CORBA::Long GEOM_Object_i::GetMarkerTexture()
270 {
271   return _impl->GetMarkerTexture();
272 }
273
274
275 //=============================================================================
276 /*!
277  *  SetStudyEntry
278  */
279 //=============================================================================
280 void GEOM_Object_i::SetStudyEntry(const char* theEntry)
281 {
282   _impl->SetAuxData(theEntry);
283 }
284
285
286 //=============================================================================
287 /*!
288  *  GetStudyEntry
289  */
290 //=============================================================================
291 char* GEOM_Object_i::GetStudyEntry()
292 {
293   TCollection_AsciiString anEntry = _impl->GetAuxData();
294   if(!anEntry.IsEmpty()) return CORBA::string_dup(anEntry.ToCString());
295   return CORBA::string_dup("");
296 }
297
298
299 //=============================================================================
300 /*!
301  *  GetDependency
302  */
303 //=============================================================================
304 GEOM::ListOfGO* GEOM_Object_i::GetDependency()
305 {
306   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
307   aList->length(0);
308
309   Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetAllDependency();
310   if (aSeq.IsNull()) return aList._retn();
311   int aLength = aSeq->Length();
312   if (aLength == 0) return aList._retn();
313
314   aList->length(aLength);
315
316   TCollection_AsciiString anEntry;
317
318   for (int i = 1; i<=aLength; i++) {
319     Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
320     if (anObj.IsNull()) continue;
321     TDF_Tool::Entry(anObj->GetEntry(), anEntry);
322     GEOM::GEOM_Object_var obj = _engine->GetObject(anObj->GetDocID(), anEntry.ToCString());
323     aList[i-1] = obj;
324   }
325
326   return aList._retn();
327 }
328
329 //=============================================================================
330 /*!
331  * GetLastDependency
332  */
333 //=============================================================================
334 GEOM::ListOfGO* GEOM_Object_i::GetLastDependency()
335 {
336   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
337   aList->length(0);
338
339   Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetLastDependency();
340   if (aSeq.IsNull()) return aList._retn();
341   int aLength = aSeq->Length();
342   if (aLength == 0) return aList._retn();
343
344   aList->length(aLength);
345
346   TCollection_AsciiString anEntry;
347
348   for (int i = 1; i<=aLength; i++) {
349      Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
350      if (anObj.IsNull()) continue;
351      TDF_Tool::Entry(anObj->GetEntry(), anEntry);
352      GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
353      aList[i-1] = obj;
354   }
355
356   return aList._retn();
357 }
358
359 //=================================================================================
360 // function : GetShapeStream
361 // Transfer resulting shape to client as sequence of bytes
362 //client can extract shape from stream using BrepTools::Read function
363 //=================================================================================
364 SALOMEDS::TMPFile* GEOM_Object_i::GetShapeStream()
365 {
366   TopoDS_Shape aShape = _impl->GetValue();
367
368   if(aShape.IsNull()) return NULL;
369
370   std::ostringstream streamShape;
371   //Write TopoDS_Shape in ASCII format to the stream
372   BRepTools::Write(aShape, streamShape);
373   //Returns the number of bytes that have been stored in the stream's buffer.
374   int size = streamShape.str().size();
375   //Allocate octect buffer of required size
376   CORBA::Octet* OctetBuf = SALOMEDS::TMPFile::allocbuf(size);
377   //Copy ostrstream content to the octect buffer
378   memcpy(OctetBuf, streamShape.str().c_str(), size);
379   //Create and return TMPFile
380   SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
381   return SeqFile._retn();
382 }
383
384
385 //=======================================================================
386 //function : getShape
387 //purpose  : return the TopoDS_Shape when client and servant are colocated, be careful
388 //=======================================================================
389 CORBA::LongLong GEOM_Object_i::getShape() {
390   _geom = _impl->GetValue();
391   return ((CORBA::LongLong)(&_geom));
392 }
393
394 //=============================================================================
395 /*!
396  *  GetSubShapeIndices
397  */
398 //=============================================================================
399 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
400 {
401   GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
402
403   if(!_impl->IsMainShape()) {
404     Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last)  one)
405     if(aFunction.IsNull()) return anIndices._retn();
406     GEOM_ISubShape ISS(aFunction);
407     Handle(TColStd_HArray1OfInteger) anArray = ISS.GetIndices();
408     if(anArray.IsNull() || anArray->Length() < 1) return anIndices._retn();
409     anIndices->length(anArray->Length());
410     for(int i=1; i<=anArray->Length(); i++) anIndices[i-1] = anArray->Value(i);
411   }
412   else {
413     anIndices->length(0);
414   }
415
416   return anIndices._retn();
417 }
418
419
420 //=============================================================================
421 /*!
422  *  GetMainShape
423  */
424 //=============================================================================
425 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
426 {
427   GEOM::GEOM_Object_var obj;
428   if(!_impl->IsMainShape()) {
429     Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last)  one)
430     if(aFunction.IsNull()) return obj._retn();
431     GEOM_ISubShape ISS(aFunction);
432
433     aFunction = ISS.GetMainShape();
434     if(aFunction.IsNull()) return obj._retn();
435     TDF_Label aLabel  = aFunction->GetOwnerEntry();
436     if(aLabel.IsNull()) return obj._retn();
437     TCollection_AsciiString anEntry;
438     TDF_Tool::Entry(aLabel, anEntry);
439     return _engine->GetObject(_impl->GetDocID(), anEntry.ToCString());
440   }
441
442   return obj._retn();
443 }
444
445 bool GEOM_Object_i::IsShape()
446 {
447   return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;
448 }
449
450 void GEOM_Object_i::SetParameters(const char* theParameters)
451 {
452   _impl->SetParameters((char*)theParameters);
453 }
454
455 char* GEOM_Object_i::GetParameters()
456 {
457   return CORBA::string_dup(_impl->GetParameters().ToCString());
458 }
459