1 // Copyright (C) 2007-2011 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.
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 <Standard_OStream.hxx>
25 #include <GEOM_Object_i.hh>
26 #include <GEOM_ISubShape.hxx>
27 #include <GEOMImpl_Types.hxx>
29 #include "utilities.h"
34 #include <Utils_ExceptHandlers.hxx>
35 #include <TDF_Tool.hxx>
36 #include <TDF_Label.hxx>
37 #include <TCollection_AsciiString.hxx>
39 #include <BRepTools_ShapeSet.hxx>
40 #include <BRepTools.hxx>
42 #include <TopoDS_Iterator.hxx>
45 #pragma warning( disable:4786 )
48 //=============================================================================
52 //=============================================================================
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)
60 //=============================================================================
64 //=============================================================================
66 GEOM_Object_i::~GEOM_Object_i()
68 MESSAGE("GEOM_Object_i::~GEOM_Object_i");
69 GEOM_Engine::GetEngine()->RemoveObject(_impl);
73 //=============================================================================
77 //=============================================================================
78 char* GEOM_Object_i::GetEntry()
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);
87 //=============================================================================
91 //=============================================================================
92 CORBA::Long GEOM_Object_i::GetStudyID()
94 return _impl->GetDocID();
98 //=============================================================================
102 //=============================================================================
103 CORBA::Long GEOM_Object_i::GetType()
105 return _impl->GetType();
108 //=============================================================================
112 //=============================================================================
113 GEOM::shape_type GEOM_Object_i::GetShapeType()
115 TopoDS_Shape _geom = _impl->GetValue();
116 if(_geom.IsNull()) return GEOM::SHAPE;
117 return (GEOM::shape_type)_geom.ShapeType();
120 //=============================================================================
124 //=============================================================================
125 GEOM::shape_type GEOM_Object_i::GetTopologyType()
127 TopoDS_Shape shape = _impl->GetValue();
128 if(shape.IsNull()) return GEOM::SHAPE;
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();
137 if ( !shape_i.IsNull() )
138 return (GEOM::shape_type) shape_i.ShapeType();
141 return (GEOM::shape_type)shape.ShapeType();
144 static GEOM::shape_type getMinMaxShapeType( const TopoDS_Shape& shape, bool ismin )
146 if ( shape.IsNull() )
149 GEOM::shape_type ret = (GEOM::shape_type)shape.ShapeType();
151 if ( shape.ShapeType() == TopAbs_COMPOUND || shape.ShapeType() == TopAbs_COMPSOLID ) {
152 TopoDS_Iterator it(shape, Standard_True, Standard_False);
153 for (; it.More(); it.Next()) {
154 TopoDS_Shape sub_shape = it.Value();
155 if ( sub_shape.IsNull() ) continue;
156 GEOM::shape_type stype = (GEOM::shape_type)getMinMaxShapeType( sub_shape, ismin );
157 if ( stype == GEOM::SHAPE ) continue;
158 if ( ismin && stype > ret )
160 else if ( !ismin && ( ret < GEOM::SOLID || stype < ret ) )
168 //=============================================================================
172 //=============================================================================
173 GEOM::shape_type GEOM_Object_i::GetMinShapeType()
175 return getMinMaxShapeType( _impl->GetValue(), true );
178 //=============================================================================
182 //=============================================================================
183 GEOM::shape_type GEOM_Object_i::GetMaxShapeType()
185 return getMinMaxShapeType( _impl->GetValue(), false );
188 //=============================================================================
192 //=============================================================================
193 void GEOM_Object_i::SetName(const char* theName)
195 _impl->SetName(theName);
199 //=============================================================================
203 //=============================================================================
204 char* GEOM_Object_i::GetName()
206 char* aName = _impl->GetName();
208 return aName; // this is already copy of pointer (see implementation of _impl)
212 //=============================================================================
216 //=============================================================================
217 void GEOM_Object_i::SetColor(const SALOMEDS::Color& theColor)
219 ::GEOM_Object::Color aColor;
220 aColor.R = theColor.R;
221 aColor.G = theColor.G;
222 aColor.B = theColor.B;
223 _impl->SetColor(aColor);
227 //=============================================================================
231 //=============================================================================
232 SALOMEDS::Color GEOM_Object_i::GetColor()
234 SALOMEDS::Color aColor;
235 aColor.R = _impl->GetColor().R;
236 aColor.G = _impl->GetColor().G;
237 aColor.B = _impl->GetColor().B;
242 //=============================================================================
246 //=============================================================================
247 void GEOM_Object_i::SetAutoColor(CORBA::Boolean theAutoColor)
249 _impl->SetAutoColor(theAutoColor);
253 //=============================================================================
257 //=============================================================================
258 CORBA::Boolean GEOM_Object_i::GetAutoColor()
260 return _impl->GetAutoColor();
264 //=============================================================================
268 //=============================================================================
269 void GEOM_Object_i::SetMarkerStd(GEOM::marker_type theType, GEOM::marker_size theSize)
271 if ( theType == GEOM::MT_NONE || theSize == GEOM::MS_NONE ) {
272 _impl->UnsetMarker();
275 Aspect_TypeOfMarker aType = (Aspect_TypeOfMarker)( (int)theType-1 );
276 double aSize = ((int)theSize+1)*0.5;
277 _impl->SetMarkerStd( aType, aSize );
282 //=============================================================================
286 //=============================================================================
287 void GEOM_Object_i::SetMarkerTexture(CORBA::Long theTextureId)
289 _impl->SetMarkerTexture( theTextureId );
293 //=============================================================================
297 //=============================================================================
298 GEOM::marker_type GEOM_Object_i::GetMarkerType()
300 return (GEOM::marker_type)( (int)_impl->GetMarkerType()+1 );
304 //=============================================================================
308 //=============================================================================
309 GEOM::marker_size GEOM_Object_i::GetMarkerSize()
311 int aSize = (int)( _impl->GetMarkerSize()/0.5 ) - 1;
312 return aSize < GEOM::MS_10 || aSize > GEOM::MS_70 ? GEOM::MS_NONE : (GEOM::marker_size)aSize;
316 //=============================================================================
320 //=============================================================================
321 CORBA::Long GEOM_Object_i::GetMarkerTexture()
323 return _impl->GetMarkerTexture();
327 //=============================================================================
331 //=============================================================================
332 void GEOM_Object_i::SetStudyEntry(const char* theEntry)
334 _impl->SetAuxData(theEntry);
338 //=============================================================================
342 //=============================================================================
343 char* GEOM_Object_i::GetStudyEntry()
345 TCollection_AsciiString anEntry = _impl->GetAuxData();
346 if(!anEntry.IsEmpty()) return CORBA::string_dup(anEntry.ToCString());
347 return CORBA::string_dup("");
351 //=============================================================================
355 //=============================================================================
356 GEOM::ListOfGO* GEOM_Object_i::GetDependency()
358 GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
361 Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetAllDependency();
362 if (aSeq.IsNull()) return aList._retn();
363 int aLength = aSeq->Length();
364 if (aLength == 0) return aList._retn();
366 aList->length(aLength);
368 TCollection_AsciiString anEntry;
370 for (int i = 1; i<=aLength; i++) {
371 Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
372 if (anObj.IsNull()) continue;
373 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
374 GEOM::GEOM_Object_var obj = _engine->GetObject(anObj->GetDocID(), anEntry.ToCString());
378 return aList._retn();
381 //=============================================================================
385 //=============================================================================
386 GEOM::ListOfGO* GEOM_Object_i::GetLastDependency()
388 GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
391 Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetLastDependency();
392 if (aSeq.IsNull()) return aList._retn();
393 int aLength = aSeq->Length();
394 if (aLength == 0) return aList._retn();
396 aList->length(aLength);
398 TCollection_AsciiString anEntry;
400 for (int i = 1; i<=aLength; i++) {
401 Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
402 if (anObj.IsNull()) continue;
403 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
404 GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
408 return aList._retn();
411 //=================================================================================
412 // function : GetShapeStream
413 // Transfer resulting shape to client as sequence of bytes
414 //client can extract shape from stream using BrepTools::Read function
415 //=================================================================================
416 SALOMEDS::TMPFile* GEOM_Object_i::GetShapeStream()
418 TopoDS_Shape aShape = _impl->GetValue();
420 if(aShape.IsNull()) return NULL;
422 std::ostringstream streamShape;
423 //Write TopoDS_Shape in ASCII format to the stream
424 BRepTools::Write(aShape, streamShape);
425 //Returns the number of bytes that have been stored in the stream's buffer.
426 int size = streamShape.str().size();
427 //Allocate octect buffer of required size
428 CORBA::Octet* OctetBuf = SALOMEDS::TMPFile::allocbuf(size);
429 //Copy ostrstream content to the octect buffer
430 memcpy(OctetBuf, streamShape.str().c_str(), size);
431 //Create and return TMPFile
432 SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
433 return SeqFile._retn();
437 //=======================================================================
438 //function : getShape
439 //purpose : return the TopoDS_Shape when client and servant are colocated, be careful
440 //=======================================================================
441 CORBA::LongLong GEOM_Object_i::getShape() {
442 _geom = _impl->GetValue();
443 return ((CORBA::LongLong)(&_geom));
446 //=============================================================================
450 //=============================================================================
451 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
453 GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
455 if(!_impl->IsMainShape()) {
456 Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last) one)
457 if(aFunction.IsNull()) return anIndices._retn();
458 GEOM_ISubShape ISS(aFunction);
459 Handle(TColStd_HArray1OfInteger) anArray = ISS.GetIndices();
460 if(anArray.IsNull() || anArray->Length() < 1) return anIndices._retn();
461 anIndices->length(anArray->Length());
462 for(int i=1; i<=anArray->Length(); i++) anIndices[i-1] = anArray->Value(i);
465 anIndices->length(0);
468 return anIndices._retn();
472 //=============================================================================
476 //=============================================================================
477 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
479 GEOM::GEOM_Object_var obj;
480 if(!_impl->IsMainShape()) {
481 Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last) one)
482 if(aFunction.IsNull()) return obj._retn();
483 GEOM_ISubShape ISS(aFunction);
485 aFunction = ISS.GetMainShape();
486 if(aFunction.IsNull()) return obj._retn();
487 TDF_Label aLabel = aFunction->GetOwnerEntry();
488 if(aLabel.IsNull()) return obj._retn();
489 TCollection_AsciiString anEntry;
490 TDF_Tool::Entry(aLabel, anEntry);
491 return _engine->GetObject(_impl->GetDocID(), anEntry.ToCString());
497 bool GEOM_Object_i::IsShape()
499 return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;
502 bool GEOM_Object_i::IsSame(GEOM::GEOM_Object_ptr other)
504 TopoDS_Shape thisShape = _impl->GetValue();
505 TopoDS_Shape otherShape;
506 if ( !CORBA::is_nil( other ) ) {
507 Handle(GEOM_Object) otherObject = GEOM_Engine::GetEngine()->GetObject( other->GetStudyID(), other->GetEntry(), false );
508 if ( !otherObject.IsNull() )
509 otherShape = otherObject->GetValue();
511 return thisShape.IsSame( otherShape );
514 void GEOM_Object_i::SetParameters(const char* theParameters)
516 _impl->SetParameters((char*)theParameters);
519 char* GEOM_Object_i::GetParameters()
521 return CORBA::string_dup(_impl->GetParameters().ToCString());