Salome HOME
Copyright update 2021
[modules/geom.git] / src / GEOM_I / GEOM_Object_i.cc
1 // Copyright (C) 2007-2021  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, or (at your option) any later version.
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 <TDataStd_ListIteratorOfListOfExtendedString.hxx>
40 #include <TopAbs.hxx>
41 #include <TopoDS_Iterator.hxx>
42
43 #include <fstream>
44 #include <sstream>
45
46 #include <Standard_Failure.hxx>
47 #include <Standard_ErrorHandler.hxx>
48
49 #ifdef _DEBUG_
50 #include <typeinfo>
51 #endif
52
53 #ifdef WIN32
54 #pragma warning( disable:4786 )
55 #endif
56
57 //=============================================================================
58 /*!
59  *   constructor:
60  */
61 //=============================================================================
62
63 GEOM_Object_i::GEOM_Object_i (PortableServer::POA_ptr thePOA, GEOM::GEOM_Gen_ptr theEngine,
64                               Handle(::GEOM_Object) theImpl) :
65   SALOME::GenericObj_i( thePOA ),
66   GEOM_BaseObject_i( thePOA, theEngine, theImpl ),
67   _impl( theImpl )
68 {
69 }
70
71 //=============================================================================
72 /*!
73  *  destructor
74  */
75 //=============================================================================
76
77 GEOM_Object_i::~GEOM_Object_i()
78 {
79   //MESSAGE("GEOM_Object_i::~GEOM_Object_i");
80 }
81
82 //=============================================================================
83 /*!
84  *  GetShapeType
85  */
86 //=============================================================================
87 GEOM::shape_type GEOM_Object_i::GetShapeType()
88 {
89   TopoDS_Shape _geom = _impl->GetValue();
90   if(_geom.IsNull()) return GEOM::SHAPE;
91   return (GEOM::shape_type)_geom.ShapeType();
92 }
93
94 //=============================================================================
95 /*!
96  *  GetTopologyType
97  */
98 //=============================================================================
99 GEOM::shape_type GEOM_Object_i::GetTopologyType()
100 {
101   TopoDS_Shape shape = _impl->GetValue();
102   if(shape.IsNull()) return GEOM::SHAPE;
103
104   if ( shape.ShapeType() == TopAbs_COMPOUND || shape.ShapeType() == TopAbs_COMPSOLID ) {
105     TopoDS_Shape shape_i;
106     TopoDS_Iterator It (shape, Standard_True, Standard_False);
107     for (; It.More(); It.Next()) {
108       if ( !shape_i.IsNull() ) return (GEOM::shape_type)shape.ShapeType();
109       shape_i = It.Value();
110     }
111     if ( !shape_i.IsNull() )
112       return (GEOM::shape_type) shape_i.ShapeType();
113   }
114
115   return (GEOM::shape_type)shape.ShapeType();
116 }
117
118 static GEOM::shape_type getMinMaxShapeType( const TopoDS_Shape& shape, bool ismin )
119 {
120   if ( shape.IsNull() )
121     return GEOM::SHAPE;
122
123   GEOM::shape_type ret = (GEOM::shape_type)shape.ShapeType();
124
125   if ( shape.ShapeType() == TopAbs_COMPOUND || shape.ShapeType() == TopAbs_COMPSOLID ) {
126     TopoDS_Iterator it(shape, Standard_True, Standard_False);
127     for (; it.More(); it.Next()) {
128       TopoDS_Shape sub_shape = it.Value();
129       if ( sub_shape.IsNull() ) continue;
130       GEOM::shape_type stype = (GEOM::shape_type)getMinMaxShapeType( sub_shape, ismin );
131       if ( stype == GEOM::SHAPE ) continue;
132       if ( ismin && stype > ret )
133         ret = stype;
134       else if ( !ismin && ( ret < GEOM::SOLID || stype < ret ) )
135         ret = stype;
136     }
137   }
138
139   return ret;
140 }
141
142 //=============================================================================
143 /*!
144  *  GetMinShapeType
145  */
146 //=============================================================================
147 GEOM::shape_type GEOM_Object_i::GetMinShapeType()
148 {
149   return getMinMaxShapeType( _impl->GetValue(), true );
150 }
151
152 //=============================================================================
153 /*!
154  *  GetMaxShapeType
155  */
156 //=============================================================================
157 GEOM::shape_type GEOM_Object_i::GetMaxShapeType()
158 {
159   return getMinMaxShapeType( _impl->GetValue(), false );
160 }
161
162 //================================================================================
163 /*!
164  * GetSubShapeName
165  */
166 //================================================================================
167
168 char* GEOM_Object_i::GetSubShapeName(CORBA::Long subID)
169 {
170   CORBA::String_var name("");
171
172   Handle(::GEOM_Function) aMainFun = _impl->GetLastFunction();
173   if ( aMainFun.IsNull() ) return name._retn();
174
175   const TDataStd_ListOfExtendedString& aListEntries = aMainFun->GetSubShapeReferences();
176   TDataStd_ListIteratorOfListOfExtendedString anIt( aListEntries );
177   for (; anIt.More(); anIt.Next())
178   {
179     TCollection_AsciiString anEntry = anIt.Value();
180     Handle(::GEOM_BaseObject) anObj =
181       GEOM_Engine::GetEngine()->GetObject( anEntry.ToCString(), false );
182     if ( anObj.IsNull() ) continue;
183
184     TCollection_AsciiString aSubName = anObj->GetName();
185     if ( aSubName.IsEmpty() ) continue;
186
187     Handle(::GEOM_Function) aFun = anObj->GetLastFunction();
188     if ( aFun.IsNull() ) continue;
189   
190     GEOM_ISubShape ISS( aFun );
191     Handle(TColStd_HArray1OfInteger) subIDs = ISS.GetIndices();
192     if ( subIDs.IsNull() || subIDs->Length() != 1 ) continue;
193
194     if ( subIDs->Value( subIDs->Lower() ) == subID )
195     {
196       name = aSubName.ToCString();
197       break;
198     }
199   }
200   return name._retn();
201 }
202
203 //=============================================================================
204 /*!
205  *  SetColor
206  */
207 //=============================================================================
208 void GEOM_Object_i::SetColor(const SALOMEDS::Color& theColor)
209 {
210   ::GEOM_Object::Color aColor;
211   aColor.R = theColor.R;
212   aColor.G = theColor.G;
213   aColor.B = theColor.B;
214   _impl->SetColor(aColor);
215 }
216
217
218 //=============================================================================
219 /*!
220  *  GetColor
221  */
222 //=============================================================================
223 SALOMEDS::Color GEOM_Object_i::GetColor()
224 {
225   SALOMEDS::Color aColor;
226   aColor.R = _impl->GetColor().R;
227   aColor.G = _impl->GetColor().G;
228   aColor.B = _impl->GetColor().B;
229   return aColor;
230 }
231
232
233 //=============================================================================
234 /*!
235  *  SetAutoColor
236  */
237 //=============================================================================
238 void GEOM_Object_i::SetAutoColor(CORBA::Boolean theAutoColor)
239 {
240   _impl->SetAutoColor(theAutoColor);
241 }
242
243
244 //=============================================================================
245 /*!
246  *  GetAutoColor
247  */
248 //=============================================================================
249 CORBA::Boolean GEOM_Object_i::GetAutoColor()
250 {
251   return _impl->GetAutoColor();
252 }
253
254
255 //=============================================================================
256 /*!
257  *  SetMarkerStd
258  */
259 //=============================================================================
260 void GEOM_Object_i::SetMarkerStd(GEOM::marker_type theType, GEOM::marker_size theSize)
261 {
262   if ( theType == GEOM::MT_NONE || theSize == GEOM::MS_NONE ) {
263     _impl->UnsetMarker();
264   }
265   else {
266     Aspect_TypeOfMarker aType = (Aspect_TypeOfMarker)( (int)theType-1 );
267     double aSize = ((int)theSize+1)*0.5;
268     _impl->SetMarkerStd( aType, aSize );
269   }
270 }
271
272
273 //=============================================================================
274 /*!
275  *  SetMarkerTexture
276  */
277 //=============================================================================
278 void GEOM_Object_i::SetMarkerTexture(CORBA::Long theTextureId)
279 {
280   _impl->SetMarkerTexture( theTextureId );
281 }
282
283
284 //=============================================================================
285 /*!
286  *  GetMarkerType
287  */
288 //=============================================================================
289 GEOM::marker_type GEOM_Object_i::GetMarkerType()
290 {
291   return (GEOM::marker_type)( (int)_impl->GetMarkerType()+1 );
292 }
293
294
295 //=============================================================================
296 /*!
297  *  GetMarkerSize
298  */
299 //=============================================================================
300 GEOM::marker_size GEOM_Object_i::GetMarkerSize()
301 {
302   int aSize = (int)( _impl->GetMarkerSize()/0.5 ) - 1;
303   return aSize < GEOM::MS_10 || aSize > GEOM::MS_70 ? GEOM::MS_NONE : (GEOM::marker_size)aSize;
304 }
305
306
307 //=============================================================================
308 /*!
309  *  GetMarkerTexture
310  */
311 //=============================================================================
312 CORBA::Long GEOM_Object_i::GetMarkerTexture()
313 {
314   return _impl->GetMarkerTexture();
315 }
316
317 //=================================================================================
318 // function : GetShapeStream
319 // Transfer resulting shape to client as sequence of bytes
320 //client can extract shape from stream using BrepTools::Read function
321 //=================================================================================
322 SALOMEDS::TMPFile* GEOM_Object_i::GetShapeStream()
323 {
324   TopoDS_Shape aShape = _impl->GetValue();
325
326   if(aShape.IsNull()) return NULL;
327
328   std::ostringstream streamShape;
329   //Write TopoDS_Shape in ASCII format to the stream
330   BRepTools::Write(aShape, streamShape);
331   //Returns the number of bytes that have been stored in the stream's buffer.
332   int size = streamShape.str().size();
333   //Allocate octect buffer of required size
334   CORBA::Octet* OctetBuf = SALOMEDS::TMPFile::allocbuf(size);
335   //Copy ostrstream content to the octect buffer
336   memcpy(OctetBuf, streamShape.str().c_str(), size);
337   //Create and return TMPFile
338   SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
339   return SeqFile._retn();
340 }
341
342
343 //=======================================================================
344 //function : getShape
345 //purpose  : return the TopoDS_Shape when client and servant are colocated, be careful
346 //=======================================================================
347 CORBA::LongLong GEOM_Object_i::getShape() {
348   _geom = _impl->GetValue();
349   return ((CORBA::LongLong)(&_geom));
350 }
351
352 //=============================================================================
353 /*!
354  *  GetSubShapeIndices
355  */
356 //=============================================================================
357 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
358 {
359   GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
360
361   if(!_impl->IsMainShape()) {
362     Handle(::GEOM_Function) aFunction = _impl->GetLastFunction(); //Get Sub-shape function (always the first (and last)  one)
363     if(aFunction.IsNull()) return anIndices._retn();
364     GEOM_ISubShape ISS(aFunction);
365     Handle(TColStd_HArray1OfInteger) anArray = ISS.GetIndices();
366     if(anArray.IsNull() || anArray->Length() < 1) return anIndices._retn();
367     anIndices->length(anArray->Length());
368     for(int i=1; i<=anArray->Length(); i++) anIndices[i-1] = anArray->Value(i);
369   }
370   else {
371     anIndices->length(0);
372   }
373
374   return anIndices._retn();
375 }
376
377
378 //=============================================================================
379 /*!
380  *  GetMainShape
381  */
382 //=============================================================================
383 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
384 {
385   GEOM::GEOM_Object_var obj;
386   if(!_impl->IsMainShape()) {
387     Handle(::GEOM_Function) aFunction = _impl->GetFunction(1); //Get Sub-shape function (always the first (and last)  one)
388     if(aFunction.IsNull()) return obj._retn();
389     GEOM_ISubShape ISS(aFunction);
390
391     aFunction = ISS.GetMainShape();
392     if(aFunction.IsNull()) return obj._retn();
393     TDF_Label aLabel  = aFunction->GetOwnerEntry();
394     if(aLabel.IsNull()) return obj._retn();
395     TCollection_AsciiString anEntry;
396     TDF_Tool::Entry(aLabel, anEntry);
397     return GEOM::GEOM_Object::_narrow
398       ( _engine->GetObject( anEntry.ToCString()) );
399   }
400
401   return obj._retn();
402 }
403
404 bool GEOM_Object_i::IsShape()
405 {
406   return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;
407 }
408
409 bool GEOM_Object_i::IsSame(GEOM::GEOM_BaseObject_ptr other)
410 {
411   bool result = false;
412
413   GEOM::GEOM_Object_var shapePtr = GEOM::GEOM_Object::_narrow( other );
414   if ( !CORBA::is_nil( shapePtr ) ) {
415     CORBA::String_var entry = shapePtr->GetEntry();
416     Handle(::GEOM_Object) otherObject = Handle(::GEOM_Object)::DownCast
417       ( GEOM_Engine::GetEngine()->GetObject( entry, false ));
418     if ( !otherObject.IsNull() ) {
419       TopoDS_Shape thisShape  = _impl->GetValue();
420       TopoDS_Shape otherShape = otherObject->GetValue();
421       result = !thisShape.IsNull() && !otherShape.IsNull() && thisShape.IsSame( otherShape );
422     }
423   }
424   return result;
425 }