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>
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 ), _engine(theEngine), _impl(theImpl)
68 //=============================================================================
72 //=============================================================================
74 GEOM_Object_i::~GEOM_Object_i()
76 MESSAGE("GEOM_Object_i::~GEOM_Object_i");
77 GEOM_Engine::GetEngine()->RemoveObject(_impl);
81 //=============================================================================
85 //=============================================================================
86 char* GEOM_Object_i::GetEntry()
88 const TDF_Label& aLabel = _impl->GetEntry();
89 TCollection_AsciiString anEntry;
90 TDF_Tool::Entry(aLabel, anEntry);
91 const char* anEntstr = anEntry.ToCString();
92 return CORBA::string_dup(anEntstr);
95 //=============================================================================
99 //=============================================================================
100 CORBA::Long GEOM_Object_i::GetStudyID()
102 return _impl->GetDocID();
106 //=============================================================================
110 //=============================================================================
111 CORBA::Long GEOM_Object_i::GetType()
113 return _impl->GetType();
116 //=============================================================================
120 //=============================================================================
121 GEOM::shape_type GEOM_Object_i::GetShapeType()
123 TopoDS_Shape _geom = _impl->GetValue();
124 if(_geom.IsNull()) return GEOM::SHAPE;
125 return (GEOM::shape_type)_geom.ShapeType();
128 //=============================================================================
132 //=============================================================================
133 GEOM::shape_type GEOM_Object_i::GetTopologyType()
135 TopoDS_Shape shape = _impl->GetValue();
136 if(shape.IsNull()) return GEOM::SHAPE;
138 if ( shape.ShapeType() == TopAbs_COMPOUND || shape.ShapeType() == TopAbs_COMPSOLID ) {
139 TopoDS_Shape shape_i;
140 TopoDS_Iterator It (shape, Standard_True, Standard_False);
141 for (; It.More(); It.Next()) {
142 if ( !shape_i.IsNull() ) return (GEOM::shape_type)shape.ShapeType();
143 shape_i = It.Value();
145 if ( !shape_i.IsNull() )
146 return (GEOM::shape_type) shape_i.ShapeType();
149 return (GEOM::shape_type)shape.ShapeType();
152 static GEOM::shape_type getMinMaxShapeType( const TopoDS_Shape& shape, bool ismin )
154 if ( shape.IsNull() )
157 GEOM::shape_type ret = (GEOM::shape_type)shape.ShapeType();
159 if ( shape.ShapeType() == TopAbs_COMPOUND || shape.ShapeType() == TopAbs_COMPSOLID ) {
160 TopoDS_Iterator it(shape, Standard_True, Standard_False);
161 for (; it.More(); it.Next()) {
162 TopoDS_Shape sub_shape = it.Value();
163 if ( sub_shape.IsNull() ) continue;
164 GEOM::shape_type stype = (GEOM::shape_type)getMinMaxShapeType( sub_shape, ismin );
165 if ( stype == GEOM::SHAPE ) continue;
166 if ( ismin && stype > ret )
168 else if ( !ismin && ( ret < GEOM::SOLID || stype < ret ) )
176 //=============================================================================
180 //=============================================================================
181 GEOM::shape_type GEOM_Object_i::GetMinShapeType()
183 return getMinMaxShapeType( _impl->GetValue(), true );
186 //=============================================================================
190 //=============================================================================
191 GEOM::shape_type GEOM_Object_i::GetMaxShapeType()
193 return getMinMaxShapeType( _impl->GetValue(), false );
196 //=============================================================================
200 //=============================================================================
201 void GEOM_Object_i::SetName(const char* theName)
203 _impl->SetName(theName);
207 //=============================================================================
211 //=============================================================================
212 char* GEOM_Object_i::GetName()
214 TCollection_AsciiString aName = _impl->GetName();
215 return CORBA::string_dup( aName.ToCString() );
218 //=============================================================================
222 //=============================================================================
223 void GEOM_Object_i::SetColor(const SALOMEDS::Color& theColor)
225 ::GEOM_Object::Color aColor;
226 aColor.R = theColor.R;
227 aColor.G = theColor.G;
228 aColor.B = theColor.B;
229 _impl->SetColor(aColor);
233 //=============================================================================
237 //=============================================================================
238 SALOMEDS::Color GEOM_Object_i::GetColor()
240 SALOMEDS::Color aColor;
241 aColor.R = _impl->GetColor().R;
242 aColor.G = _impl->GetColor().G;
243 aColor.B = _impl->GetColor().B;
248 //=============================================================================
252 //=============================================================================
253 void GEOM_Object_i::SetAutoColor(CORBA::Boolean theAutoColor)
255 _impl->SetAutoColor(theAutoColor);
259 //=============================================================================
263 //=============================================================================
264 CORBA::Boolean GEOM_Object_i::GetAutoColor()
266 return _impl->GetAutoColor();
270 //=============================================================================
274 //=============================================================================
275 void GEOM_Object_i::SetMarkerStd(GEOM::marker_type theType, GEOM::marker_size theSize)
277 if ( theType == GEOM::MT_NONE || theSize == GEOM::MS_NONE ) {
278 _impl->UnsetMarker();
281 Aspect_TypeOfMarker aType = (Aspect_TypeOfMarker)( (int)theType-1 );
282 double aSize = ((int)theSize+1)*0.5;
283 _impl->SetMarkerStd( aType, aSize );
288 //=============================================================================
292 //=============================================================================
293 void GEOM_Object_i::SetMarkerTexture(CORBA::Long theTextureId)
295 _impl->SetMarkerTexture( theTextureId );
299 //=============================================================================
303 //=============================================================================
304 GEOM::marker_type GEOM_Object_i::GetMarkerType()
306 return (GEOM::marker_type)( (int)_impl->GetMarkerType()+1 );
310 //=============================================================================
314 //=============================================================================
315 GEOM::marker_size GEOM_Object_i::GetMarkerSize()
317 int aSize = (int)( _impl->GetMarkerSize()/0.5 ) - 1;
318 return aSize < GEOM::MS_10 || aSize > GEOM::MS_70 ? GEOM::MS_NONE : (GEOM::marker_size)aSize;
322 //=============================================================================
326 //=============================================================================
327 CORBA::Long GEOM_Object_i::GetMarkerTexture()
329 return _impl->GetMarkerTexture();
333 //=============================================================================
337 //=============================================================================
338 void GEOM_Object_i::SetStudyEntry(const char* theEntry)
340 _impl->SetAuxData(theEntry);
344 //=============================================================================
348 //=============================================================================
349 char* GEOM_Object_i::GetStudyEntry()
351 TCollection_AsciiString anEntry = _impl->GetAuxData();
352 if(!anEntry.IsEmpty()) return CORBA::string_dup(anEntry.ToCString());
353 return CORBA::string_dup("");
357 //=============================================================================
361 //=============================================================================
362 GEOM::ListOfGO* GEOM_Object_i::GetDependency()
364 GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
367 Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetAllDependency();
368 if (aSeq.IsNull()) return aList._retn();
369 int aLength = aSeq->Length();
370 if (aLength == 0) return aList._retn();
372 aList->length(aLength);
374 TCollection_AsciiString anEntry;
376 for (int i = 1; i<=aLength; i++) {
377 Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
378 if (anObj.IsNull()) continue;
379 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
380 GEOM::GEOM_Object_var obj = _engine->GetObject(anObj->GetDocID(), anEntry.ToCString());
384 return aList._retn();
387 //=============================================================================
391 //=============================================================================
392 GEOM::ListOfGO* GEOM_Object_i::GetLastDependency()
394 GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
397 Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetLastDependency();
398 if (aSeq.IsNull()) return aList._retn();
399 int aLength = aSeq->Length();
400 if (aLength == 0) return aList._retn();
402 aList->length(aLength);
404 TCollection_AsciiString anEntry;
406 for (int i = 1; i<=aLength; i++) {
407 Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
408 if (anObj.IsNull()) continue;
409 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
410 GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
414 return aList._retn();
417 //=================================================================================
418 // function : GetShapeStream
419 // Transfer resulting shape to client as sequence of bytes
420 //client can extract shape from stream using BrepTools::Read function
421 //=================================================================================
422 SALOMEDS::TMPFile* GEOM_Object_i::GetShapeStream()
424 TopoDS_Shape aShape = _impl->GetValue();
426 if(aShape.IsNull()) return NULL;
428 std::ostringstream streamShape;
429 //Write TopoDS_Shape in ASCII format to the stream
430 BRepTools::Write(aShape, streamShape);
431 //Returns the number of bytes that have been stored in the stream's buffer.
432 int size = streamShape.str().size();
433 //Allocate octect buffer of required size
434 CORBA::Octet* OctetBuf = SALOMEDS::TMPFile::allocbuf(size);
435 //Copy ostrstream content to the octect buffer
436 memcpy(OctetBuf, streamShape.str().c_str(), size);
437 //Create and return TMPFile
438 SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
439 return SeqFile._retn();
443 //=======================================================================
444 //function : getShape
445 //purpose : return the TopoDS_Shape when client and servant are colocated, be careful
446 //=======================================================================
447 CORBA::LongLong GEOM_Object_i::getShape() {
448 _geom = _impl->GetValue();
449 return ((CORBA::LongLong)(&_geom));
452 //=============================================================================
456 //=============================================================================
457 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
459 GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
461 if(!_impl->IsMainShape()) {
462 Handle(GEOM_Function) aFunction = _impl->GetLastFunction(); //Get Sub-shape function (always the first (and last) one)
463 if(aFunction.IsNull()) return anIndices._retn();
464 GEOM_ISubShape ISS(aFunction);
465 Handle(TColStd_HArray1OfInteger) anArray = ISS.GetIndices();
466 if(anArray.IsNull() || anArray->Length() < 1) return anIndices._retn();
467 anIndices->length(anArray->Length());
468 for(int i=1; i<=anArray->Length(); i++) anIndices[i-1] = anArray->Value(i);
471 anIndices->length(0);
474 return anIndices._retn();
478 //=============================================================================
482 //=============================================================================
483 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
485 GEOM::GEOM_Object_var obj;
486 if(!_impl->IsMainShape()) {
487 Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get Sub-shape function (always the first (and last) one)
488 if(aFunction.IsNull()) return obj._retn();
489 GEOM_ISubShape ISS(aFunction);
491 aFunction = ISS.GetMainShape();
492 if(aFunction.IsNull()) return obj._retn();
493 TDF_Label aLabel = aFunction->GetOwnerEntry();
494 if(aLabel.IsNull()) return obj._retn();
495 TCollection_AsciiString anEntry;
496 TDF_Tool::Entry(aLabel, anEntry);
497 return _engine->GetObject(_impl->GetDocID(), anEntry.ToCString());
503 bool GEOM_Object_i::IsShape()
505 return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;
508 bool GEOM_Object_i::IsSame(GEOM::GEOM_Object_ptr other)
510 TopoDS_Shape thisShape = _impl->GetValue();
511 TopoDS_Shape otherShape;
512 if ( !CORBA::is_nil( other ) ) {
513 Handle(GEOM_Object) otherObject = GEOM_Engine::GetEngine()->GetObject( other->GetStudyID(), other->GetEntry(), false );
514 if ( !otherObject.IsNull() )
515 otherShape = otherObject->GetValue();
517 return thisShape.IsSame( otherShape );
520 void GEOM_Object_i::SetParameters(const char* theParameters)
522 _impl->SetParameters((char*)theParameters);
525 char* GEOM_Object_i::GetParameters()
527 return CORBA::string_dup(_impl->GetParameters().ToCString());
530 GEOM::CreationInformation* GEOM_Object_i::GetCreationInformation()
532 GEOM::CreationInformation_var info = new GEOM::CreationInformation;
534 Handle(GEOM_BaseDriver) driver =
535 Handle(GEOM_BaseDriver)::DownCast( _impl->GetCreationDriver() );
536 if ( !driver.IsNull() )
538 std::vector<GEOM_Param> params;
539 std::string operationName;
543 if ( driver->GetCreationInformation( operationName, params ))
545 info->operationName = operationName.c_str();
546 info->params.length( params.size() );
547 for ( size_t i = 0; i < params.size(); ++i )
549 info->params[i].name = params[i].name.c_str();
550 info->params[i].value = params[i].value.c_str();
554 if ( operationName.empty() )
556 cout << endl << endl << endl << "Warning: " << endl << "Dear developer!!!" << endl
557 << " Consider implementing "
558 << typeid(*(driver.operator->())).name() << "::GetCreationInformation() " << endl
559 << " for the case of operation which has created '" << GetName() << "' object" << endl
560 << "PLEEEEEEEASE" << endl
561 << "\tPLEEEEEEEASE" << endl
562 << "\t\tPLEEEEEEEASE" << endl
563 << "\t\t\tPLEEEEEEEASE" << endl
564 << "\t\t\t\tPLEEEEEEEASE" << endl;
571 cout << "Ecxeption in GEOM_Object_i::GetCreationInformation()" << endl;