Back to index

salome-geom  6.5.0
GEOM_SubShapeDriver.cxx
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_Stream.hxx>
00024 
00025 #include <GEOM_SubShapeDriver.hxx>
00026 #include <GEOM_ISubShape.hxx>
00027 #include <GEOM_Function.hxx>
00028 #include <GEOM_Object.hxx>
00029 
00030 #include <BRep_Builder.hxx>
00031 
00032 #include <TopExp.hxx>
00033 #include <TopoDS_Shape.hxx>
00034 #include <TopoDS_Compound.hxx>
00035 #include <TopTools_MapOfShape.hxx>
00036 #include <TopTools_IndexedMapOfShape.hxx>
00037 
00038 #include <Standard_NullObject.hxx>
00039 
00040 //=======================================================================
00041 //function : GEOM_SubShapeDriver
00042 //purpose  :
00043 //=======================================================================
00044 GEOM_SubShapeDriver::GEOM_SubShapeDriver()
00045 {
00046 }
00047 
00048 //=======================================================================
00049 //function : Execute
00050 //purpose  :
00051 //=======================================================================
00052 Standard_Integer GEOM_SubShapeDriver::Execute(TFunction_Logbook& log) const
00053 {
00054   if (Label().IsNull()) return 0;
00055   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
00056 
00057   GEOM_ISubShape aCI (aFunction);
00058 
00059   TDF_Label aLabel = aCI.GetMainShape()->GetOwnerEntry();
00060   if (aLabel.IsRoot()) return 0;
00061   Handle(GEOM_Object) anObj = GEOM_Object::GetObject(aLabel);
00062   if (anObj.IsNull()) return 0;
00063   TopoDS_Shape aMainShape = anObj->GetValue();
00064   if (aMainShape.IsNull()) return 0;
00065 
00066   Handle(TColStd_HArray1OfInteger) anIndices = aCI.GetIndices();
00067   if (anIndices.IsNull() || anIndices->Length() <= 0) return 0;
00068 
00069   BRep_Builder B;
00070   TopoDS_Compound aCompound;
00071   TopoDS_Shape aShape;
00072 
00073   if (anIndices->Length() == 1 && anIndices->Value(1) == -1) { //The empty sub-shape
00074     B.MakeCompound(aCompound);
00075     aShape = aCompound;
00076   }
00077   else {
00078     TopTools_IndexedMapOfShape aMapOfShapes;
00079     TopExp::MapShapes(aMainShape, aMapOfShapes);
00080 
00081     if (anIndices->Length() > 1) {
00082       B.MakeCompound(aCompound);
00083 
00084       for (int i = anIndices->Lower(); i <= anIndices->Upper(); i++) {
00085         if (aMapOfShapes.Extent() < anIndices->Value(i))
00086           Standard_NullObject::Raise("GEOM_SubShapeDriver::Execute: Index is out of range");
00087         TopoDS_Shape aSubShape = aMapOfShapes.FindKey(anIndices->Value(i));
00088         if (aSubShape.IsNull()) continue;
00089         B.Add(aCompound,aSubShape);
00090       }
00091 
00092       aShape = aCompound;
00093     }
00094     else {
00095       int i = anIndices->Lower();
00096       if (aMapOfShapes.Extent() < anIndices->Value(i))
00097         Standard_NullObject::Raise("GEOM_SubShapeDriver::Execute: Index is out of range");
00098       aShape = aMapOfShapes.FindKey(anIndices->Value(i));
00099     }
00100   }
00101 
00102   if (aShape.IsNull()) return 0;
00103 
00104   aFunction->SetValue(aShape);
00105 
00106   log.SetTouched(Label());
00107 
00108   return 1;
00109 }
00110 
00111 //=======================================================================
00112 //function :  GEOM_SubShapeDriver_Type_
00113 //purpose  :
00114 //=======================================================================
00115 Standard_EXPORT Handle_Standard_Type& GEOM_SubShapeDriver_Type_()
00116 {
00117 
00118   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
00119   if (aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
00120   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
00121   if (aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
00122   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
00123   if (aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
00124 
00125 
00126   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
00127   static Handle_Standard_Type _aType = new Standard_Type("GEOM_SubShapeDriver",
00128                                                          sizeof(GEOM_SubShapeDriver),
00129                                                          1,
00130                                                          (Standard_Address)_Ancestors,
00131                                                          (Standard_Address)NULL);
00132 
00133   return _aType;
00134 }
00135 
00136 //=======================================================================
00137 //function : DownCast
00138 //purpose  :
00139 //=======================================================================
00140 const Handle(GEOM_SubShapeDriver) Handle(GEOM_SubShapeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
00141 {
00142   Handle(GEOM_SubShapeDriver) _anOtherObject;
00143 
00144   if (!AnObject.IsNull()) {
00145      if (AnObject->IsKind(STANDARD_TYPE(GEOM_SubShapeDriver))) {
00146        _anOtherObject = Handle(GEOM_SubShapeDriver)((Handle(GEOM_SubShapeDriver)&)AnObject);
00147      }
00148   }
00149 
00150   return _anOtherObject;
00151 }