1 // Copyright (C) 2007-2014 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>
40 #include <TopoDS_Iterator.hxx>
45 #include <Standard_Failure.hxx>
46 #include <Standard_ErrorHandler.hxx>
53 #pragma warning( disable:4786 )
56 //=============================================================================
60 //=============================================================================
62 GEOM_Object_i::GEOM_Object_i (PortableServer::POA_ptr thePOA, GEOM::GEOM_Gen_ptr theEngine,
63 Handle(GEOM_Object) theImpl) :
64 SALOME::GenericObj_i( thePOA ),
65 GEOM_BaseObject_i( thePOA, theEngine, theImpl ),
70 //=============================================================================
74 //=============================================================================
76 GEOM_Object_i::~GEOM_Object_i()
78 MESSAGE("GEOM_Object_i::~GEOM_Object_i");
81 //=============================================================================
85 //=============================================================================
86 GEOM::shape_type GEOM_Object_i::GetShapeType()
88 TopoDS_Shape _geom = _impl->GetValue();
89 if(_geom.IsNull()) return GEOM::SHAPE;
90 return (GEOM::shape_type)_geom.ShapeType();
93 //=============================================================================
97 //=============================================================================
98 GEOM::shape_type GEOM_Object_i::GetTopologyType()
100 TopoDS_Shape shape = _impl->GetValue();
101 if(shape.IsNull()) return GEOM::SHAPE;
103 if ( shape.ShapeType() == TopAbs_COMPOUND || shape.ShapeType() == TopAbs_COMPSOLID ) {
104 TopoDS_Shape shape_i;
105 TopoDS_Iterator It (shape, Standard_True, Standard_False);
106 for (; It.More(); It.Next()) {
107 if ( !shape_i.IsNull() ) return (GEOM::shape_type)shape.ShapeType();
108 shape_i = It.Value();
110 if ( !shape_i.IsNull() )
111 return (GEOM::shape_type) shape_i.ShapeType();
114 return (GEOM::shape_type)shape.ShapeType();
117 static GEOM::shape_type getMinMaxShapeType( const TopoDS_Shape& shape, bool ismin )
119 if ( shape.IsNull() )
122 GEOM::shape_type ret = (GEOM::shape_type)shape.ShapeType();
124 if ( shape.ShapeType() == TopAbs_COMPOUND || shape.ShapeType() == TopAbs_COMPSOLID ) {
125 TopoDS_Iterator it(shape, Standard_True, Standard_False);
126 for (; it.More(); it.Next()) {
127 TopoDS_Shape sub_shape = it.Value();
128 if ( sub_shape.IsNull() ) continue;
129 GEOM::shape_type stype = (GEOM::shape_type)getMinMaxShapeType( sub_shape, ismin );
130 if ( stype == GEOM::SHAPE ) continue;
131 if ( ismin && stype > ret )
133 else if ( !ismin && ( ret < GEOM::SOLID || stype < ret ) )
141 //=============================================================================
145 //=============================================================================
146 GEOM::shape_type GEOM_Object_i::GetMinShapeType()
148 return getMinMaxShapeType( _impl->GetValue(), true );
151 //=============================================================================
155 //=============================================================================
156 GEOM::shape_type GEOM_Object_i::GetMaxShapeType()
158 return getMinMaxShapeType( _impl->GetValue(), false );
161 //=============================================================================
165 //=============================================================================
166 void GEOM_Object_i::SetColor(const SALOMEDS::Color& theColor)
168 ::GEOM_Object::Color aColor;
169 aColor.R = theColor.R;
170 aColor.G = theColor.G;
171 aColor.B = theColor.B;
172 _impl->SetColor(aColor);
176 //=============================================================================
180 //=============================================================================
181 SALOMEDS::Color GEOM_Object_i::GetColor()
183 SALOMEDS::Color aColor;
184 aColor.R = _impl->GetColor().R;
185 aColor.G = _impl->GetColor().G;
186 aColor.B = _impl->GetColor().B;
191 //=============================================================================
195 //=============================================================================
196 void GEOM_Object_i::SetAutoColor(CORBA::Boolean theAutoColor)
198 _impl->SetAutoColor(theAutoColor);
202 //=============================================================================
206 //=============================================================================
207 CORBA::Boolean GEOM_Object_i::GetAutoColor()
209 return _impl->GetAutoColor();
213 //=============================================================================
217 //=============================================================================
218 void GEOM_Object_i::SetMarkerStd(GEOM::marker_type theType, GEOM::marker_size theSize)
220 if ( theType == GEOM::MT_NONE || theSize == GEOM::MS_NONE ) {
221 _impl->UnsetMarker();
224 Aspect_TypeOfMarker aType = (Aspect_TypeOfMarker)( (int)theType-1 );
225 double aSize = ((int)theSize+1)*0.5;
226 _impl->SetMarkerStd( aType, aSize );
231 //=============================================================================
235 //=============================================================================
236 void GEOM_Object_i::SetMarkerTexture(CORBA::Long theTextureId)
238 _impl->SetMarkerTexture( theTextureId );
242 //=============================================================================
246 //=============================================================================
247 GEOM::marker_type GEOM_Object_i::GetMarkerType()
249 return (GEOM::marker_type)( (int)_impl->GetMarkerType()+1 );
253 //=============================================================================
257 //=============================================================================
258 GEOM::marker_size GEOM_Object_i::GetMarkerSize()
260 int aSize = (int)( _impl->GetMarkerSize()/0.5 ) - 1;
261 return aSize < GEOM::MS_10 || aSize > GEOM::MS_70 ? GEOM::MS_NONE : (GEOM::marker_size)aSize;
265 //=============================================================================
269 //=============================================================================
270 CORBA::Long GEOM_Object_i::GetMarkerTexture()
272 return _impl->GetMarkerTexture();
275 //=================================================================================
276 // function : GetShapeStream
277 // Transfer resulting shape to client as sequence of bytes
278 //client can extract shape from stream using BrepTools::Read function
279 //=================================================================================
280 SALOMEDS::TMPFile* GEOM_Object_i::GetShapeStream()
282 TopoDS_Shape aShape = _impl->GetValue();
284 if(aShape.IsNull()) return NULL;
286 std::ostringstream streamShape;
287 //Write TopoDS_Shape in ASCII format to the stream
288 BRepTools::Write(aShape, streamShape);
289 //Returns the number of bytes that have been stored in the stream's buffer.
290 int size = streamShape.str().size();
291 //Allocate octect buffer of required size
292 CORBA::Octet* OctetBuf = SALOMEDS::TMPFile::allocbuf(size);
293 //Copy ostrstream content to the octect buffer
294 memcpy(OctetBuf, streamShape.str().c_str(), size);
295 //Create and return TMPFile
296 SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
297 return SeqFile._retn();
301 //=======================================================================
302 //function : getShape
303 //purpose : return the TopoDS_Shape when client and servant are colocated, be careful
304 //=======================================================================
305 CORBA::LongLong GEOM_Object_i::getShape() {
306 _geom = _impl->GetValue();
307 return ((CORBA::LongLong)(&_geom));
310 //=============================================================================
314 //=============================================================================
315 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
317 GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
319 if(!_impl->IsMainShape()) {
320 Handle(GEOM_Function) aFunction = _impl->GetLastFunction(); //Get Sub-shape function (always the first (and last) one)
321 if(aFunction.IsNull()) return anIndices._retn();
322 GEOM_ISubShape ISS(aFunction);
323 Handle(TColStd_HArray1OfInteger) anArray = ISS.GetIndices();
324 if(anArray.IsNull() || anArray->Length() < 1) return anIndices._retn();
325 anIndices->length(anArray->Length());
326 for(int i=1; i<=anArray->Length(); i++) anIndices[i-1] = anArray->Value(i);
329 anIndices->length(0);
332 return anIndices._retn();
336 //=============================================================================
340 //=============================================================================
341 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
343 GEOM::GEOM_Object_var obj;
344 if(!_impl->IsMainShape()) {
345 Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get Sub-shape function (always the first (and last) one)
346 if(aFunction.IsNull()) return obj._retn();
347 GEOM_ISubShape ISS(aFunction);
349 aFunction = ISS.GetMainShape();
350 if(aFunction.IsNull()) return obj._retn();
351 TDF_Label aLabel = aFunction->GetOwnerEntry();
352 if(aLabel.IsNull()) return obj._retn();
353 TCollection_AsciiString anEntry;
354 TDF_Tool::Entry(aLabel, anEntry);
355 return GEOM::GEOM_Object::_narrow
356 ( _engine->GetObject(_impl->GetDocID(), anEntry.ToCString()) );
362 bool GEOM_Object_i::IsShape()
364 return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;
367 bool GEOM_Object_i::IsSame(GEOM::GEOM_BaseObject_ptr other)
369 GEOM::GEOM_Object_ptr shapePtr = GEOM::GEOM_Object::_narrow( other );
370 if ( CORBA::is_nil( shapePtr ) )
372 TopoDS_Shape thisShape = _impl->GetValue();
373 TopoDS_Shape otherShape;
374 Handle(GEOM_Object) otherObject = Handle(GEOM_Object)::DownCast
375 ( GEOM_Engine::GetEngine()->GetObject( shapePtr->GetStudyID(), shapePtr->GetEntry(), false ));
376 if ( !otherObject.IsNull() )
377 otherShape = otherObject->GetValue();
378 return thisShape.IsSame( otherShape );