1 // Copyright (C) 2007-2013 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 "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>
49 #pragma warning( disable:4786 )
52 //=============================================================================
56 //=============================================================================
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)
64 //=============================================================================
68 //=============================================================================
70 GEOM_Object_i::~GEOM_Object_i()
72 MESSAGE("GEOM_Object_i::~GEOM_Object_i");
73 GEOM_Engine::GetEngine()->RemoveObject(_impl);
77 //=============================================================================
81 //=============================================================================
82 char* GEOM_Object_i::GetEntry()
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);
91 //=============================================================================
95 //=============================================================================
96 CORBA::Long GEOM_Object_i::GetStudyID()
98 return _impl->GetDocID();
102 //=============================================================================
106 //=============================================================================
107 CORBA::Long GEOM_Object_i::GetType()
109 return _impl->GetType();
112 //=============================================================================
116 //=============================================================================
117 GEOM::shape_type GEOM_Object_i::GetShapeType()
119 TopoDS_Shape _geom = _impl->GetValue();
120 if(_geom.IsNull()) return GEOM::SHAPE;
121 return (GEOM::shape_type)_geom.ShapeType();
124 //=============================================================================
128 //=============================================================================
129 GEOM::shape_type GEOM_Object_i::GetTopologyType()
131 TopoDS_Shape shape = _impl->GetValue();
132 if(shape.IsNull()) return GEOM::SHAPE;
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();
141 if ( !shape_i.IsNull() )
142 return (GEOM::shape_type) shape_i.ShapeType();
145 return (GEOM::shape_type)shape.ShapeType();
148 static GEOM::shape_type getMinMaxShapeType( const TopoDS_Shape& shape, bool ismin )
150 if ( shape.IsNull() )
153 GEOM::shape_type ret = (GEOM::shape_type)shape.ShapeType();
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 )
164 else if ( !ismin && ( ret < GEOM::SOLID || stype < ret ) )
172 //=============================================================================
176 //=============================================================================
177 GEOM::shape_type GEOM_Object_i::GetMinShapeType()
179 return getMinMaxShapeType( _impl->GetValue(), true );
182 //=============================================================================
186 //=============================================================================
187 GEOM::shape_type GEOM_Object_i::GetMaxShapeType()
189 return getMinMaxShapeType( _impl->GetValue(), false );
192 //=============================================================================
196 //=============================================================================
197 void GEOM_Object_i::SetName(const char* theName)
199 _impl->SetName(theName);
203 //=============================================================================
207 //=============================================================================
208 char* GEOM_Object_i::GetName()
210 TCollection_AsciiString aName = _impl->GetName();
211 return CORBA::string_dup( aName.ToCString() );
214 //=============================================================================
218 //=============================================================================
219 void GEOM_Object_i::SetColor(const SALOMEDS::Color& theColor)
221 ::GEOM_Object::Color aColor;
222 aColor.R = theColor.R;
223 aColor.G = theColor.G;
224 aColor.B = theColor.B;
225 _impl->SetColor(aColor);
229 //=============================================================================
233 //=============================================================================
234 SALOMEDS::Color GEOM_Object_i::GetColor()
236 SALOMEDS::Color aColor;
237 aColor.R = _impl->GetColor().R;
238 aColor.G = _impl->GetColor().G;
239 aColor.B = _impl->GetColor().B;
244 //=============================================================================
248 //=============================================================================
249 void GEOM_Object_i::SetAutoColor(CORBA::Boolean theAutoColor)
251 _impl->SetAutoColor(theAutoColor);
255 //=============================================================================
259 //=============================================================================
260 CORBA::Boolean GEOM_Object_i::GetAutoColor()
262 return _impl->GetAutoColor();
266 //=============================================================================
270 //=============================================================================
271 void GEOM_Object_i::SetMarkerStd(GEOM::marker_type theType, GEOM::marker_size theSize)
273 if ( theType == GEOM::MT_NONE || theSize == GEOM::MS_NONE ) {
274 _impl->UnsetMarker();
277 Aspect_TypeOfMarker aType = (Aspect_TypeOfMarker)( (int)theType-1 );
278 double aSize = ((int)theSize+1)*0.5;
279 _impl->SetMarkerStd( aType, aSize );
284 //=============================================================================
288 //=============================================================================
289 void GEOM_Object_i::SetMarkerTexture(CORBA::Long theTextureId)
291 _impl->SetMarkerTexture( theTextureId );
295 //=============================================================================
299 //=============================================================================
300 GEOM::marker_type GEOM_Object_i::GetMarkerType()
302 return (GEOM::marker_type)( (int)_impl->GetMarkerType()+1 );
306 //=============================================================================
310 //=============================================================================
311 GEOM::marker_size GEOM_Object_i::GetMarkerSize()
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;
318 //=============================================================================
322 //=============================================================================
323 CORBA::Long GEOM_Object_i::GetMarkerTexture()
325 return _impl->GetMarkerTexture();
329 //=============================================================================
333 //=============================================================================
334 void GEOM_Object_i::SetStudyEntry(const char* theEntry)
336 _impl->SetAuxData(theEntry);
340 //=============================================================================
344 //=============================================================================
345 char* GEOM_Object_i::GetStudyEntry()
347 TCollection_AsciiString anEntry = _impl->GetAuxData();
348 if(!anEntry.IsEmpty()) return CORBA::string_dup(anEntry.ToCString());
349 return CORBA::string_dup("");
353 //=============================================================================
357 //=============================================================================
358 GEOM::ListOfGO* GEOM_Object_i::GetDependency()
360 GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
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();
368 aList->length(aLength);
370 TCollection_AsciiString anEntry;
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());
380 return aList._retn();
383 //=============================================================================
387 //=============================================================================
388 GEOM::ListOfGO* GEOM_Object_i::GetLastDependency()
390 GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
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();
398 aList->length(aLength);
400 TCollection_AsciiString anEntry;
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()));
410 return aList._retn();
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()
420 TopoDS_Shape aShape = _impl->GetValue();
422 if(aShape.IsNull()) return NULL;
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();
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));
448 //=============================================================================
452 //=============================================================================
453 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
455 GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
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);
467 anIndices->length(0);
470 return anIndices._retn();
474 //=============================================================================
478 //=============================================================================
479 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
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);
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());
499 bool GEOM_Object_i::IsShape()
501 return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;
504 bool GEOM_Object_i::IsSame(GEOM::GEOM_Object_ptr other)
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();
513 return thisShape.IsSame( otherShape );
516 void GEOM_Object_i::SetParameters(const char* theParameters)
518 _impl->SetParameters((char*)theParameters);
521 char* GEOM_Object_i::GetParameters()
523 return CORBA::string_dup(_impl->GetParameters().ToCString());
526 GEOM::CreationInformation* GEOM_Object_i::GetCreationInformation()
528 GEOM::CreationInformation_var info = new GEOM::CreationInformation;
530 Handle(GEOM_BaseDriver) driver =
531 Handle(GEOM_BaseDriver)::DownCast( _impl->GetCreationDriver() );
532 if ( !driver.IsNull() )
534 std::vector<GEOM_Param> params;
535 std::string operationName;
539 if ( driver->GetCreationInformation( operationName, params ))
541 info->operationName = operationName.c_str();
542 info->params.length( params.size() );
543 for ( size_t i = 0; i < params.size(); ++i )
545 info->params[i].name = params[i].name.c_str();
546 info->params[i].value = params[i].value.c_str();
553 cout << "Ecxeption in GEOM_Object_i::GetCreationInformation()" << endl;