Back to index

salome-geom  6.5.0
GEOM_Object_i.cc
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include <Standard_OStream.hxx>
00024 
00025 #include <GEOM_Object_i.hh>
00026 #include <GEOM_ISubShape.hxx>
00027 #include <GEOMImpl_Types.hxx>
00028 
00029 #include "utilities.h"
00030 #include <fstream>
00031 #include <sstream>
00032 
00033 #include <OpUtil.hxx>
00034 #include <Utils_ExceptHandlers.hxx>
00035 #include <TDF_Tool.hxx>
00036 #include <TDF_Label.hxx>
00037 #include <TCollection_AsciiString.hxx>
00038 
00039 #include <BRepTools_ShapeSet.hxx>
00040 #include <BRepTools.hxx>
00041 #include <TopAbs.hxx>
00042 #include <TopoDS_Iterator.hxx>
00043 
00044 #ifdef WNT
00045 #pragma warning( disable:4786 )
00046 #endif
00047 
00048 //=============================================================================
00052 //=============================================================================
00053 
00054 GEOM_Object_i::GEOM_Object_i (PortableServer::POA_ptr thePOA, GEOM::GEOM_Gen_ptr theEngine,
00055                            Handle(GEOM_Object) theImpl)
00056 : SALOME::GenericObj_i( thePOA ), _engine(theEngine), _impl(theImpl)
00057 {
00058 }
00059 
00060 //=============================================================================
00064 //=============================================================================
00065 
00066 GEOM_Object_i::~GEOM_Object_i()
00067 {
00068   MESSAGE("GEOM_Object_i::~GEOM_Object_i");
00069   GEOM_Engine::GetEngine()->RemoveObject(_impl);
00070 }
00071 
00072 
00073 //=============================================================================
00077 //=============================================================================
00078 char* GEOM_Object_i::GetEntry()
00079 {
00080   const TDF_Label& aLabel = _impl->GetEntry();
00081   TCollection_AsciiString anEntry;
00082   TDF_Tool::Entry(aLabel, anEntry);
00083   const char* anEntstr = anEntry.ToCString();
00084   return CORBA::string_dup(anEntstr);
00085 }
00086 
00087 //=============================================================================
00091 //=============================================================================
00092 CORBA::Long GEOM_Object_i::GetStudyID()
00093 {
00094    return _impl->GetDocID();
00095 }
00096 
00097 
00098 //=============================================================================
00102 //=============================================================================
00103 CORBA::Long GEOM_Object_i::GetType()
00104 {
00105   return _impl->GetType();
00106 }
00107 
00108 //=============================================================================
00112 //=============================================================================
00113 GEOM::shape_type GEOM_Object_i::GetShapeType()
00114 {
00115   TopoDS_Shape _geom = _impl->GetValue();
00116   if(_geom.IsNull()) return GEOM::SHAPE;
00117   return (GEOM::shape_type)_geom.ShapeType();
00118 }
00119 
00120 //=============================================================================
00124 //=============================================================================
00125 GEOM::shape_type GEOM_Object_i::GetTopologyType()
00126 {
00127   TopoDS_Shape shape = _impl->GetValue();
00128   if(shape.IsNull()) return GEOM::SHAPE;
00129 
00130   if ( shape.ShapeType() == TopAbs_COMPOUND || shape.ShapeType() == TopAbs_COMPSOLID ) {
00131     TopoDS_Shape shape_i;
00132     TopoDS_Iterator It (shape, Standard_True, Standard_False);
00133     for (; It.More(); It.Next()) {
00134       if ( !shape_i.IsNull() ) return (GEOM::shape_type)shape.ShapeType();
00135       shape_i = It.Value();
00136     }
00137     if ( !shape_i.IsNull() )
00138       return (GEOM::shape_type) shape_i.ShapeType();
00139   }
00140 
00141   return (GEOM::shape_type)shape.ShapeType();
00142 }
00143 
00144 static GEOM::shape_type getMinMaxShapeType( const TopoDS_Shape& shape, bool ismin )
00145 {
00146   if ( shape.IsNull() )
00147     return GEOM::SHAPE;
00148 
00149   GEOM::shape_type ret = (GEOM::shape_type)shape.ShapeType();
00150 
00151   if ( shape.ShapeType() == TopAbs_COMPOUND || shape.ShapeType() == TopAbs_COMPSOLID ) {
00152     TopoDS_Iterator it(shape, Standard_True, Standard_False);
00153     for (; it.More(); it.Next()) {
00154       TopoDS_Shape sub_shape = it.Value();
00155       if ( sub_shape.IsNull() ) continue;
00156       GEOM::shape_type stype = (GEOM::shape_type)getMinMaxShapeType( sub_shape, ismin );
00157       if ( stype == GEOM::SHAPE ) continue;
00158       if ( ismin && stype > ret )
00159        ret = stype;
00160       else if ( !ismin && ( ret < GEOM::SOLID || stype < ret ) )
00161        ret = stype;
00162     }
00163   }
00164 
00165   return ret;
00166 }
00167 
00168 //=============================================================================
00172 //=============================================================================
00173 GEOM::shape_type GEOM_Object_i::GetMinShapeType()
00174 {
00175   return getMinMaxShapeType( _impl->GetValue(), true );
00176 }
00177 
00178 //=============================================================================
00182 //=============================================================================
00183 GEOM::shape_type GEOM_Object_i::GetMaxShapeType()
00184 {
00185   return getMinMaxShapeType( _impl->GetValue(), false );
00186 }
00187 
00188 //=============================================================================
00192 //=============================================================================
00193 void GEOM_Object_i::SetName(const char* theName)
00194 {
00195   _impl->SetName(theName);
00196 }
00197 
00198 
00199 //=============================================================================
00203 //=============================================================================
00204 char* GEOM_Object_i::GetName()
00205 {
00206   char* aName = _impl->GetName();
00207   if (aName)
00208     return aName; // this is already copy of pointer (see implementation of _impl)
00209   return strdup("");
00210 }
00211 
00212 //=============================================================================
00216 //=============================================================================
00217 void GEOM_Object_i::SetColor(const SALOMEDS::Color& theColor)
00218 {
00219   ::GEOM_Object::Color aColor;
00220   aColor.R = theColor.R;
00221   aColor.G = theColor.G;
00222   aColor.B = theColor.B;
00223   _impl->SetColor(aColor);
00224 }
00225 
00226 
00227 //=============================================================================
00231 //=============================================================================
00232 SALOMEDS::Color GEOM_Object_i::GetColor()
00233 {
00234   SALOMEDS::Color aColor;
00235   aColor.R = _impl->GetColor().R;
00236   aColor.G = _impl->GetColor().G;
00237   aColor.B = _impl->GetColor().B;
00238   return aColor;
00239 }
00240 
00241 
00242 //=============================================================================
00246 //=============================================================================
00247 void GEOM_Object_i::SetAutoColor(CORBA::Boolean theAutoColor)
00248 {
00249   _impl->SetAutoColor(theAutoColor);
00250 }
00251 
00252 
00253 //=============================================================================
00257 //=============================================================================
00258 CORBA::Boolean GEOM_Object_i::GetAutoColor()
00259 {
00260   return _impl->GetAutoColor();
00261 }
00262 
00263 
00264 //=============================================================================
00268 //=============================================================================
00269 void GEOM_Object_i::SetMarkerStd(GEOM::marker_type theType, GEOM::marker_size theSize)
00270 {
00271   if ( theType == GEOM::MT_NONE || theSize == GEOM::MS_NONE ) {
00272     _impl->UnsetMarker();
00273   }
00274   else {
00275     Aspect_TypeOfMarker aType = (Aspect_TypeOfMarker)( (int)theType-1 );
00276     double aSize = ((int)theSize+1)*0.5;
00277     _impl->SetMarkerStd( aType, aSize );
00278   }
00279 }
00280 
00281 
00282 //=============================================================================
00286 //=============================================================================
00287 void GEOM_Object_i::SetMarkerTexture(CORBA::Long theTextureId)
00288 {
00289   _impl->SetMarkerTexture( theTextureId );
00290 }
00291 
00292 
00293 //=============================================================================
00297 //=============================================================================
00298 GEOM::marker_type GEOM_Object_i::GetMarkerType()
00299 {
00300   return (GEOM::marker_type)( (int)_impl->GetMarkerType()+1 );
00301 }
00302 
00303 
00304 //=============================================================================
00308 //=============================================================================
00309 GEOM::marker_size GEOM_Object_i::GetMarkerSize()
00310 {
00311   int aSize = (int)( _impl->GetMarkerSize()/0.5 ) - 1;
00312   return aSize < GEOM::MS_10 || aSize > GEOM::MS_70 ? GEOM::MS_NONE : (GEOM::marker_size)aSize;
00313 }
00314 
00315 
00316 //=============================================================================
00320 //=============================================================================
00321 CORBA::Long GEOM_Object_i::GetMarkerTexture()
00322 {
00323   return _impl->GetMarkerTexture();
00324 }
00325 
00326 
00327 //=============================================================================
00331 //=============================================================================
00332 void GEOM_Object_i::SetStudyEntry(const char* theEntry)
00333 {
00334   _impl->SetAuxData(theEntry);
00335 }
00336 
00337 
00338 //=============================================================================
00342 //=============================================================================
00343 char* GEOM_Object_i::GetStudyEntry()
00344 {
00345   TCollection_AsciiString anEntry = _impl->GetAuxData();
00346   if(!anEntry.IsEmpty()) return CORBA::string_dup(anEntry.ToCString());
00347   return CORBA::string_dup("");
00348 }
00349 
00350 
00351 //=============================================================================
00355 //=============================================================================
00356 GEOM::ListOfGO* GEOM_Object_i::GetDependency()
00357 {
00358   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
00359   aList->length(0);
00360 
00361   Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetAllDependency();
00362   if (aSeq.IsNull()) return aList._retn();
00363   int aLength = aSeq->Length();
00364   if (aLength == 0) return aList._retn();
00365 
00366   aList->length(aLength);
00367 
00368   TCollection_AsciiString anEntry;
00369 
00370   for (int i = 1; i<=aLength; i++) {
00371     Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
00372     if (anObj.IsNull()) continue;
00373     TDF_Tool::Entry(anObj->GetEntry(), anEntry);
00374     GEOM::GEOM_Object_var obj = _engine->GetObject(anObj->GetDocID(), anEntry.ToCString());
00375     aList[i-1] = obj;
00376   }
00377 
00378   return aList._retn();
00379 }
00380 
00381 //=============================================================================
00385 //=============================================================================
00386 GEOM::ListOfGO* GEOM_Object_i::GetLastDependency()
00387 {
00388   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
00389   aList->length(0);
00390 
00391   Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetLastDependency();
00392   if (aSeq.IsNull()) return aList._retn();
00393   int aLength = aSeq->Length();
00394   if (aLength == 0) return aList._retn();
00395 
00396   aList->length(aLength);
00397 
00398   TCollection_AsciiString anEntry;
00399 
00400   for (int i = 1; i<=aLength; i++) {
00401      Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
00402      if (anObj.IsNull()) continue;
00403      TDF_Tool::Entry(anObj->GetEntry(), anEntry);
00404      GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
00405      aList[i-1] = obj;
00406   }
00407 
00408   return aList._retn();
00409 }
00410 
00411 //=================================================================================
00412 // function : GetShapeStream
00413 // Transfer resulting shape to client as sequence of bytes
00414 //client can extract shape from stream using BrepTools::Read function
00415 //=================================================================================
00416 SALOMEDS::TMPFile* GEOM_Object_i::GetShapeStream()
00417 {
00418   TopoDS_Shape aShape = _impl->GetValue();
00419 
00420   if(aShape.IsNull()) return NULL;
00421 
00422   std::ostringstream streamShape;
00423   //Write TopoDS_Shape in ASCII format to the stream
00424   BRepTools::Write(aShape, streamShape);
00425   //Returns the number of bytes that have been stored in the stream's buffer.
00426   int size = streamShape.str().size();
00427   //Allocate octect buffer of required size
00428   CORBA::Octet* OctetBuf = SALOMEDS::TMPFile::allocbuf(size);
00429   //Copy ostrstream content to the octect buffer
00430   memcpy(OctetBuf, streamShape.str().c_str(), size);
00431   //Create and return TMPFile
00432   SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
00433   return SeqFile._retn();
00434 }
00435 
00436 
00437 //=======================================================================
00438 //function : getShape
00439 //purpose  : return the TopoDS_Shape when client and servant are colocated, be careful
00440 //=======================================================================
00441 CORBA::LongLong GEOM_Object_i::getShape() {
00442   _geom = _impl->GetValue();
00443   return ((CORBA::LongLong)(&_geom));
00444 }
00445 
00446 //=============================================================================
00450 //=============================================================================
00451 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
00452 {
00453   GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
00454 
00455   if(!_impl->IsMainShape()) {
00456     Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get Sub-shape function (always the first (and last)  one)
00457     if(aFunction.IsNull()) return anIndices._retn();
00458     GEOM_ISubShape ISS(aFunction);
00459     Handle(TColStd_HArray1OfInteger) anArray = ISS.GetIndices();
00460     if(anArray.IsNull() || anArray->Length() < 1) return anIndices._retn();
00461     anIndices->length(anArray->Length());
00462     for(int i=1; i<=anArray->Length(); i++) anIndices[i-1] = anArray->Value(i);
00463   }
00464   else {
00465     anIndices->length(0);
00466   }
00467 
00468   return anIndices._retn();
00469 }
00470 
00471 
00472 //=============================================================================
00476 //=============================================================================
00477 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
00478 {
00479   GEOM::GEOM_Object_var obj;
00480   if(!_impl->IsMainShape()) {
00481     Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get Sub-shape function (always the first (and last)  one)
00482     if(aFunction.IsNull()) return obj._retn();
00483     GEOM_ISubShape ISS(aFunction);
00484 
00485     aFunction = ISS.GetMainShape();
00486     if(aFunction.IsNull()) return obj._retn();
00487     TDF_Label aLabel  = aFunction->GetOwnerEntry();
00488     if(aLabel.IsNull()) return obj._retn();
00489     TCollection_AsciiString anEntry;
00490     TDF_Tool::Entry(aLabel, anEntry);
00491     return _engine->GetObject(_impl->GetDocID(), anEntry.ToCString());
00492   }
00493 
00494   return obj._retn();
00495 }
00496 
00497 bool GEOM_Object_i::IsShape()
00498 {
00499   return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;
00500 }
00501 
00502 bool GEOM_Object_i::IsSame(GEOM::GEOM_Object_ptr other)
00503 {
00504   TopoDS_Shape thisShape  = _impl->GetValue();
00505   TopoDS_Shape otherShape;
00506   if ( !CORBA::is_nil( other ) ) {
00507     Handle(GEOM_Object) otherObject = GEOM_Engine::GetEngine()->GetObject( other->GetStudyID(), other->GetEntry(), false );
00508     if ( !otherObject.IsNull() )
00509       otherShape = otherObject->GetValue();
00510   }
00511   return thisShape.IsSame( otherShape );
00512 }
00513 
00514 void GEOM_Object_i::SetParameters(const char* theParameters)
00515 {
00516   _impl->SetParameters((char*)theParameters);
00517 }
00518 
00519 char* GEOM_Object_i::GetParameters()
00520 {
00521   return CORBA::string_dup(_impl->GetParameters().ToCString());
00522 }
00523