]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOM_I/GEOM_Object_i.cc
Salome HOME
0022232: [CEA 837] Memory corruption in GEOM/SMESH that leads to segfault on debian64
[modules/geom.git] / src / GEOM_I / GEOM_Object_i.cc
1 // Copyright (C) 2007-2013  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 "GEOM_Object_i.hh"
24
25 #include "GEOM_ISubShape.hxx"
26 #include "GEOMImpl_Types.hxx"
27 #include "GEOM_BaseDriver.hxx"
28
29 #include <utilities.h>
30 #include <OpUtil.hxx>
31 #include <Utils_ExceptHandlers.hxx>
32
33 #include <BRepTools.hxx>
34 #include <BRepTools_ShapeSet.hxx>
35 #include <Standard_OStream.hxx>
36 #include <TCollection_AsciiString.hxx>
37 #include <TDF_Label.hxx>
38 #include <TDF_Tool.hxx>
39 #include <TopAbs.hxx>
40 #include <TopoDS_Iterator.hxx>
41
42 #include <fstream>
43 #include <sstream>
44
45 #include <Standard_Failure.hxx>
46 #include <Standard_ErrorHandler.hxx>
47
48 #ifdef WNT
49 #pragma warning( disable:4786 )
50 #endif
51
52 //=============================================================================
53 /*!
54  *   constructor:
55  */
56 //=============================================================================
57
58 GEOM_Object_i::GEOM_Object_i (PortableServer::POA_ptr thePOA, GEOM::GEOM_Gen_ptr theEngine,
59                               Handle(GEOM_Object) theImpl)
60 : SALOME::GenericObj_i( thePOA ), _engine(theEngine), _impl(theImpl)
61 {
62 }
63
64 //=============================================================================
65 /*!
66  *  destructor
67  */
68 //=============================================================================
69
70 GEOM_Object_i::~GEOM_Object_i()
71 {
72   MESSAGE("GEOM_Object_i::~GEOM_Object_i");
73   GEOM_Engine::GetEngine()->RemoveObject(_impl);
74 }
75
76
77 //=============================================================================
78 /*!
79  *  GetEntry
80  */
81 //=============================================================================
82 char* GEOM_Object_i::GetEntry()
83 {
84   const TDF_Label& aLabel = _impl->GetEntry();
85   TCollection_AsciiString anEntry;
86   TDF_Tool::Entry(aLabel, anEntry);
87   const char* anEntstr = anEntry.ToCString();
88   return CORBA::string_dup(anEntstr);
89 }
90
91 //=============================================================================
92 /*!
93  *  GetStudyID
94  */
95 //=============================================================================
96 CORBA::Long GEOM_Object_i::GetStudyID()
97 {
98    return _impl->GetDocID();
99 }
100
101
102 //=============================================================================
103 /*!
104  *  GetType
105  */
106 //=============================================================================
107 CORBA::Long GEOM_Object_i::GetType()
108 {
109   return _impl->GetType();
110 }
111
112 //=============================================================================
113 /*!
114  *  GetShapeType
115  */
116 //=============================================================================
117 GEOM::shape_type GEOM_Object_i::GetShapeType()
118 {
119   TopoDS_Shape _geom = _impl->GetValue();
120   if(_geom.IsNull()) return GEOM::SHAPE;
121   return (GEOM::shape_type)_geom.ShapeType();
122 }
123
124 //=============================================================================
125 /*!
126  *  GetTopologyType
127  */
128 //=============================================================================
129 GEOM::shape_type GEOM_Object_i::GetTopologyType()
130 {
131   TopoDS_Shape shape = _impl->GetValue();
132   if(shape.IsNull()) return GEOM::SHAPE;
133
134   if ( shape.ShapeType() == TopAbs_COMPOUND || shape.ShapeType() == TopAbs_COMPSOLID ) {
135     TopoDS_Shape shape_i;
136     TopoDS_Iterator It (shape, Standard_True, Standard_False);
137     for (; It.More(); It.Next()) {
138       if ( !shape_i.IsNull() ) return (GEOM::shape_type)shape.ShapeType();
139       shape_i = It.Value();
140     }
141     if ( !shape_i.IsNull() )
142       return (GEOM::shape_type) shape_i.ShapeType();
143   }
144
145   return (GEOM::shape_type)shape.ShapeType();
146 }
147
148 static GEOM::shape_type getMinMaxShapeType( const TopoDS_Shape& shape, bool ismin )
149 {
150   if ( shape.IsNull() )
151     return GEOM::SHAPE;
152
153   GEOM::shape_type ret = (GEOM::shape_type)shape.ShapeType();
154
155   if ( shape.ShapeType() == TopAbs_COMPOUND || shape.ShapeType() == TopAbs_COMPSOLID ) {
156     TopoDS_Iterator it(shape, Standard_True, Standard_False);
157     for (; it.More(); it.Next()) {
158       TopoDS_Shape sub_shape = it.Value();
159       if ( sub_shape.IsNull() ) continue;
160       GEOM::shape_type stype = (GEOM::shape_type)getMinMaxShapeType( sub_shape, ismin );
161       if ( stype == GEOM::SHAPE ) continue;
162       if ( ismin && stype > ret )
163         ret = stype;
164       else if ( !ismin && ( ret < GEOM::SOLID || stype < ret ) )
165         ret = stype;
166     }
167   }
168
169   return ret;
170 }
171
172 //=============================================================================
173 /*!
174  *  GetMinShapeType
175  */
176 //=============================================================================
177 GEOM::shape_type GEOM_Object_i::GetMinShapeType()
178 {
179   return getMinMaxShapeType( _impl->GetValue(), true );
180 }
181
182 //=============================================================================
183 /*!
184  *  GetMaxShapeType
185  */
186 //=============================================================================
187 GEOM::shape_type GEOM_Object_i::GetMaxShapeType()
188 {
189   return getMinMaxShapeType( _impl->GetValue(), false );
190 }
191
192 //=============================================================================
193 /*!
194  *  SetName
195  */
196 //=============================================================================
197 void GEOM_Object_i::SetName(const char* theName)
198 {
199   _impl->SetName(theName);
200 }
201
202
203 //=============================================================================
204 /*!
205  *  GetName
206  */
207 //=============================================================================
208 char* GEOM_Object_i::GetName()
209 {
210   TCollection_AsciiString aName = _impl->GetName();
211   return CORBA::string_dup( aName.ToCString() );
212 }
213
214 //=============================================================================
215 /*!
216  *  SetColor
217  */
218 //=============================================================================
219 void GEOM_Object_i::SetColor(const SALOMEDS::Color& theColor)
220 {
221   ::GEOM_Object::Color aColor;
222   aColor.R = theColor.R;
223   aColor.G = theColor.G;
224   aColor.B = theColor.B;
225   _impl->SetColor(aColor);
226 }
227
228
229 //=============================================================================
230 /*!
231  *  GetColor
232  */
233 //=============================================================================
234 SALOMEDS::Color GEOM_Object_i::GetColor()
235 {
236   SALOMEDS::Color aColor;
237   aColor.R = _impl->GetColor().R;
238   aColor.G = _impl->GetColor().G;
239   aColor.B = _impl->GetColor().B;
240   return aColor;
241 }
242
243
244 //=============================================================================
245 /*!
246  *  SetAutoColor
247  */
248 //=============================================================================
249 void GEOM_Object_i::SetAutoColor(CORBA::Boolean theAutoColor)
250 {
251   _impl->SetAutoColor(theAutoColor);
252 }
253
254
255 //=============================================================================
256 /*!
257  *  GetAutoColor
258  */
259 //=============================================================================
260 CORBA::Boolean GEOM_Object_i::GetAutoColor()
261 {
262   return _impl->GetAutoColor();
263 }
264
265
266 //=============================================================================
267 /*!
268  *  SetMarkerStd
269  */
270 //=============================================================================
271 void GEOM_Object_i::SetMarkerStd(GEOM::marker_type theType, GEOM::marker_size theSize)
272 {
273   if ( theType == GEOM::MT_NONE || theSize == GEOM::MS_NONE ) {
274     _impl->UnsetMarker();
275   }
276   else {
277     Aspect_TypeOfMarker aType = (Aspect_TypeOfMarker)( (int)theType-1 );
278     double aSize = ((int)theSize+1)*0.5;
279     _impl->SetMarkerStd( aType, aSize );
280   }
281 }
282
283
284 //=============================================================================
285 /*!
286  *  SetMarkerTexture
287  */
288 //=============================================================================
289 void GEOM_Object_i::SetMarkerTexture(CORBA::Long theTextureId)
290 {
291   _impl->SetMarkerTexture( theTextureId );
292 }
293
294
295 //=============================================================================
296 /*!
297  *  GetMarkerType
298  */
299 //=============================================================================
300 GEOM::marker_type GEOM_Object_i::GetMarkerType()
301 {
302   return (GEOM::marker_type)( (int)_impl->GetMarkerType()+1 );
303 }
304
305
306 //=============================================================================
307 /*!
308  *  GetMarkerSize
309  */
310 //=============================================================================
311 GEOM::marker_size GEOM_Object_i::GetMarkerSize()
312 {
313   int aSize = (int)( _impl->GetMarkerSize()/0.5 ) - 1;
314   return aSize < GEOM::MS_10 || aSize > GEOM::MS_70 ? GEOM::MS_NONE : (GEOM::marker_size)aSize;
315 }
316
317
318 //=============================================================================
319 /*!
320  *  GetMarkerTexture
321  */
322 //=============================================================================
323 CORBA::Long GEOM_Object_i::GetMarkerTexture()
324 {
325   return _impl->GetMarkerTexture();
326 }
327
328
329 //=============================================================================
330 /*!
331  *  SetStudyEntry
332  */
333 //=============================================================================
334 void GEOM_Object_i::SetStudyEntry(const char* theEntry)
335 {
336   _impl->SetAuxData(theEntry);
337 }
338
339
340 //=============================================================================
341 /*!
342  *  GetStudyEntry
343  */
344 //=============================================================================
345 char* GEOM_Object_i::GetStudyEntry()
346 {
347   TCollection_AsciiString anEntry = _impl->GetAuxData();
348   if(!anEntry.IsEmpty()) return CORBA::string_dup(anEntry.ToCString());
349   return CORBA::string_dup("");
350 }
351
352
353 //=============================================================================
354 /*!
355  *  GetDependency
356  */
357 //=============================================================================
358 GEOM::ListOfGO* GEOM_Object_i::GetDependency()
359 {
360   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
361   aList->length(0);
362
363   Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetAllDependency();
364   if (aSeq.IsNull()) return aList._retn();
365   int aLength = aSeq->Length();
366   if (aLength == 0) return aList._retn();
367
368   aList->length(aLength);
369
370   TCollection_AsciiString anEntry;
371
372   for (int i = 1; i<=aLength; i++) {
373     Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
374     if (anObj.IsNull()) continue;
375     TDF_Tool::Entry(anObj->GetEntry(), anEntry);
376     GEOM::GEOM_Object_var obj = _engine->GetObject(anObj->GetDocID(), anEntry.ToCString());
377     aList[i-1] = obj;
378   }
379
380   return aList._retn();
381 }
382
383 //=============================================================================
384 /*!
385  * GetLastDependency
386  */
387 //=============================================================================
388 GEOM::ListOfGO* GEOM_Object_i::GetLastDependency()
389 {
390   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
391   aList->length(0);
392
393   Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetLastDependency();
394   if (aSeq.IsNull()) return aList._retn();
395   int aLength = aSeq->Length();
396   if (aLength == 0) return aList._retn();
397
398   aList->length(aLength);
399
400   TCollection_AsciiString anEntry;
401
402   for (int i = 1; i<=aLength; i++) {
403      Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
404      if (anObj.IsNull()) continue;
405      TDF_Tool::Entry(anObj->GetEntry(), anEntry);
406      GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
407      aList[i-1] = obj;
408   }
409
410   return aList._retn();
411 }
412
413 //=================================================================================
414 // function : GetShapeStream
415 // Transfer resulting shape to client as sequence of bytes
416 //client can extract shape from stream using BrepTools::Read function
417 //=================================================================================
418 SALOMEDS::TMPFile* GEOM_Object_i::GetShapeStream()
419 {
420   TopoDS_Shape aShape = _impl->GetValue();
421
422   if(aShape.IsNull()) return NULL;
423
424   std::ostringstream streamShape;
425   //Write TopoDS_Shape in ASCII format to the stream
426   BRepTools::Write(aShape, streamShape);
427   //Returns the number of bytes that have been stored in the stream's buffer.
428   int size = streamShape.str().size();
429   //Allocate octect buffer of required size
430   CORBA::Octet* OctetBuf = SALOMEDS::TMPFile::allocbuf(size);
431   //Copy ostrstream content to the octect buffer
432   memcpy(OctetBuf, streamShape.str().c_str(), size);
433   //Create and return TMPFile
434   SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
435   return SeqFile._retn();
436 }
437
438
439 //=======================================================================
440 //function : getShape
441 //purpose  : return the TopoDS_Shape when client and servant are colocated, be careful
442 //=======================================================================
443 CORBA::LongLong GEOM_Object_i::getShape() {
444   _geom = _impl->GetValue();
445   return ((CORBA::LongLong)(&_geom));
446 }
447
448 //=============================================================================
449 /*!
450  *  GetSubShapeIndices
451  */
452 //=============================================================================
453 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
454 {
455   GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
456
457   if(!_impl->IsMainShape()) {
458     Handle(GEOM_Function) aFunction = _impl->GetLastFunction(); //Get Sub-shape function (always the first (and last)  one)
459     if(aFunction.IsNull()) return anIndices._retn();
460     GEOM_ISubShape ISS(aFunction);
461     Handle(TColStd_HArray1OfInteger) anArray = ISS.GetIndices();
462     if(anArray.IsNull() || anArray->Length() < 1) return anIndices._retn();
463     anIndices->length(anArray->Length());
464     for(int i=1; i<=anArray->Length(); i++) anIndices[i-1] = anArray->Value(i);
465   }
466   else {
467     anIndices->length(0);
468   }
469
470   return anIndices._retn();
471 }
472
473
474 //=============================================================================
475 /*!
476  *  GetMainShape
477  */
478 //=============================================================================
479 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
480 {
481   GEOM::GEOM_Object_var obj;
482   if(!_impl->IsMainShape()) {
483     Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get Sub-shape function (always the first (and last)  one)
484     if(aFunction.IsNull()) return obj._retn();
485     GEOM_ISubShape ISS(aFunction);
486
487     aFunction = ISS.GetMainShape();
488     if(aFunction.IsNull()) return obj._retn();
489     TDF_Label aLabel  = aFunction->GetOwnerEntry();
490     if(aLabel.IsNull()) return obj._retn();
491     TCollection_AsciiString anEntry;
492     TDF_Tool::Entry(aLabel, anEntry);
493     return _engine->GetObject(_impl->GetDocID(), anEntry.ToCString());
494   }
495
496   return obj._retn();
497 }
498
499 bool GEOM_Object_i::IsShape()
500 {
501   return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;
502 }
503
504 bool GEOM_Object_i::IsSame(GEOM::GEOM_Object_ptr other)
505 {
506   TopoDS_Shape thisShape  = _impl->GetValue();
507   TopoDS_Shape otherShape;
508   if ( !CORBA::is_nil( other ) ) {
509     Handle(GEOM_Object) otherObject = GEOM_Engine::GetEngine()->GetObject( other->GetStudyID(), other->GetEntry(), false );
510     if ( !otherObject.IsNull() )
511       otherShape = otherObject->GetValue();
512   }
513   return thisShape.IsSame( otherShape );
514 }
515
516 void GEOM_Object_i::SetParameters(const char* theParameters)
517 {
518   _impl->SetParameters((char*)theParameters);
519 }
520
521 char* GEOM_Object_i::GetParameters()
522 {
523   return CORBA::string_dup(_impl->GetParameters().ToCString());
524 }
525
526 GEOM::CreationInformation* GEOM_Object_i::GetCreationInformation()
527 {
528   GEOM::CreationInformation_var info = new GEOM::CreationInformation;
529
530   Handle(GEOM_BaseDriver) driver =
531     Handle(GEOM_BaseDriver)::DownCast( _impl->GetCreationDriver() );
532   if ( !driver.IsNull() )
533   {
534     std::vector<GEOM_Param> params;
535     std::string             operationName;
536     try
537     {
538       OCC_CATCH_SIGNALS;
539       if ( driver->GetCreationInformation( operationName, params ))
540       {
541         info->operationName = operationName.c_str();
542         info->params.length( params.size() );
543         for ( size_t i = 0; i < params.size(); ++i )
544         {
545           info->params[i].name  = params[i].name.c_str();
546           info->params[i].value = params[i].value.c_str();
547         }
548       }
549     }
550     catch(...)
551     {
552 #ifdef _DEBUG_
553       cout << "Ecxeption in GEOM_Object_i::GetCreationInformation()" << endl;
554 #endif
555     }
556   }
557   return info._retn();
558 }