1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "GEOM_Object_i.hh"
25 #include "GEOM_ISubShape.hxx"
26 #include "GEOMImpl_Types.hxx"
27 #include "GEOM_BaseDriver.hxx"
29 #include <utilities.h>
31 #include <Utils_ExceptHandlers.hxx>
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>
41 #include <TopoDS_Iterator.hxx>
46 #include <Standard_Failure.hxx>
47 #include <Standard_ErrorHandler.hxx>
54 #pragma warning( disable:4786 )
57 //=============================================================================
61 //=============================================================================
63 GEOM_Object_i::GEOM_Object_i (PortableServer::POA_ptr thePOA, GEOM::GEOM_Gen_ptr theEngine,
64 HANDLE_NAMESPACE(GEOM_Object) theImpl) :
65 SALOME::GenericObj_i( thePOA ),
66 GEOM_BaseObject_i( thePOA, theEngine, theImpl ),
71 //=============================================================================
75 //=============================================================================
77 GEOM_Object_i::~GEOM_Object_i()
79 MESSAGE("GEOM_Object_i::~GEOM_Object_i");
82 //=============================================================================
86 //=============================================================================
87 GEOM::shape_type GEOM_Object_i::GetShapeType()
89 TopoDS_Shape _geom = _impl->GetValue();
90 if(_geom.IsNull()) return GEOM::SHAPE;
91 return (GEOM::shape_type)_geom.ShapeType();
94 //=============================================================================
98 //=============================================================================
99 GEOM::shape_type GEOM_Object_i::GetTopologyType()
101 TopoDS_Shape shape = _impl->GetValue();
102 if(shape.IsNull()) return GEOM::SHAPE;
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();
111 if ( !shape_i.IsNull() )
112 return (GEOM::shape_type) shape_i.ShapeType();
115 return (GEOM::shape_type)shape.ShapeType();
118 static GEOM::shape_type getMinMaxShapeType( const TopoDS_Shape& shape, bool ismin )
120 if ( shape.IsNull() )
123 GEOM::shape_type ret = (GEOM::shape_type)shape.ShapeType();
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 )
134 else if ( !ismin && ( ret < GEOM::SOLID || stype < ret ) )
142 //=============================================================================
146 //=============================================================================
147 GEOM::shape_type GEOM_Object_i::GetMinShapeType()
149 return getMinMaxShapeType( _impl->GetValue(), true );
152 //=============================================================================
156 //=============================================================================
157 GEOM::shape_type GEOM_Object_i::GetMaxShapeType()
159 return getMinMaxShapeType( _impl->GetValue(), false );
162 //================================================================================
166 //================================================================================
168 char* GEOM_Object_i::GetSubShapeName(CORBA::Long subID)
170 CORBA::String_var name("");
172 HANDLE_NAMESPACE(GEOM_Function) aMainFun = _impl->GetLastFunction();
173 if ( aMainFun.IsNull() ) return name._retn();
175 const TDataStd_ListOfExtendedString& aListEntries = aMainFun->GetSubShapeReferences();
176 TDataStd_ListIteratorOfListOfExtendedString anIt( aListEntries );
177 for (; anIt.More(); anIt.Next())
179 TCollection_AsciiString anEntry = anIt.Value();
180 HANDLE_NAMESPACE(GEOM_BaseObject) anObj =
181 GEOM_Engine::GetEngine()->GetObject( _impl->GetDocID(), anEntry.ToCString(), false );
182 if ( anObj.IsNull() ) continue;
184 TCollection_AsciiString aSubName = anObj->GetName();
185 if ( aSubName.IsEmpty() ) continue;
187 HANDLE_NAMESPACE(GEOM_Function) aFun = anObj->GetLastFunction();
188 if ( aFun.IsNull() ) continue;
190 GEOM_ISubShape ISS( aFun );
191 Handle(TColStd_HArray1OfInteger) subIDs = ISS.GetIndices();
192 if ( subIDs.IsNull() || subIDs->Length() != 1 ) continue;
194 if ( subIDs->Value( subIDs->Lower() ) == subID )
196 name = aSubName.ToCString();
203 //=============================================================================
207 //=============================================================================
208 void GEOM_Object_i::SetColor(const SALOMEDS::Color& theColor)
210 ::GEOM_Object::Color aColor;
211 aColor.R = theColor.R;
212 aColor.G = theColor.G;
213 aColor.B = theColor.B;
214 _impl->SetColor(aColor);
218 //=============================================================================
222 //=============================================================================
223 SALOMEDS::Color GEOM_Object_i::GetColor()
225 SALOMEDS::Color aColor;
226 aColor.R = _impl->GetColor().R;
227 aColor.G = _impl->GetColor().G;
228 aColor.B = _impl->GetColor().B;
233 //=============================================================================
237 //=============================================================================
238 void GEOM_Object_i::SetAutoColor(CORBA::Boolean theAutoColor)
240 _impl->SetAutoColor(theAutoColor);
244 //=============================================================================
248 //=============================================================================
249 CORBA::Boolean GEOM_Object_i::GetAutoColor()
251 return _impl->GetAutoColor();
255 //=============================================================================
259 //=============================================================================
260 void GEOM_Object_i::SetMarkerStd(GEOM::marker_type theType, GEOM::marker_size theSize)
262 if ( theType == GEOM::MT_NONE || theSize == GEOM::MS_NONE ) {
263 _impl->UnsetMarker();
266 Aspect_TypeOfMarker aType = (Aspect_TypeOfMarker)( (int)theType-1 );
267 double aSize = ((int)theSize+1)*0.5;
268 _impl->SetMarkerStd( aType, aSize );
273 //=============================================================================
277 //=============================================================================
278 void GEOM_Object_i::SetMarkerTexture(CORBA::Long theTextureId)
280 _impl->SetMarkerTexture( theTextureId );
284 //=============================================================================
288 //=============================================================================
289 GEOM::marker_type GEOM_Object_i::GetMarkerType()
291 return (GEOM::marker_type)( (int)_impl->GetMarkerType()+1 );
295 //=============================================================================
299 //=============================================================================
300 GEOM::marker_size GEOM_Object_i::GetMarkerSize()
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;
307 //=============================================================================
311 //=============================================================================
312 CORBA::Long GEOM_Object_i::GetMarkerTexture()
314 return _impl->GetMarkerTexture();
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()
324 TopoDS_Shape aShape = _impl->GetValue();
326 if(aShape.IsNull()) return NULL;
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();
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));
352 //=============================================================================
356 //=============================================================================
357 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
359 GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
361 if(!_impl->IsMainShape()) {
362 HANDLE_NAMESPACE(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);
371 anIndices->length(0);
374 return anIndices._retn();
378 //=============================================================================
382 //=============================================================================
383 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
385 GEOM::GEOM_Object_var obj;
386 if(!_impl->IsMainShape()) {
387 HANDLE_NAMESPACE(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);
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(_impl->GetDocID(), anEntry.ToCString()) );
404 bool GEOM_Object_i::IsShape()
406 return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;
409 bool GEOM_Object_i::IsSame(GEOM::GEOM_BaseObject_ptr other)
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_NAMESPACE(GEOM_Object) otherObject = HANDLE_NAMESPACE(GEOM_Object)::DownCast
417 ( GEOM_Engine::GetEngine()->GetObject( shapePtr->GetStudyID(), 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 );