Back to index

salome-geom  6.5.0
GEOMImpl_MeasureDriver.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 <GEOMImpl_MeasureDriver.hxx>
00026 #include <GEOMImpl_IMeasure.hxx>
00027 #include <GEOMImpl_IMeasureOperations.hxx>
00028 #include <GEOMImpl_Types.hxx>
00029 #include <GEOM_Function.hxx>
00030 
00031 #include <BRep_Tool.hxx>
00032 #include <BRepGProp.hxx>
00033 #include <BRepBuilderAPI_MakeVertex.hxx>
00034 #include <BRepBuilderAPI_MakeEdge.hxx>
00035 
00036 #include <TopAbs.hxx>
00037 #include <TopoDS.hxx>
00038 #include <TopoDS_Shape.hxx>
00039 #include <TopoDS_Edge.hxx>
00040 #include <TopoDS_Wire.hxx>
00041 #include <TopExp.hxx>
00042 #include <TopExp_Explorer.hxx>
00043 #include <TopTools_IndexedMapOfShape.hxx>
00044 
00045 #include <GProp_GProps.hxx>
00046 #include <GeomLProp_SLProps.hxx>
00047 #include <Geom_Surface.hxx>
00048 
00049 #include <Bnd_Box.hxx>
00050 #include <BRepBndLib.hxx>
00051 #include <BRepAdaptor_Surface.hxx>
00052 #include <ShapeAnalysis_Surface.hxx>
00053 
00054 #include <gp_Pnt.hxx>
00055 #include <Precision.hxx>
00056 #include <Standard_NullObject.hxx>
00057 
00058 //=======================================================================
00059 //function : GetID
00060 //purpose  :
00061 //======================================================================= 
00062 const Standard_GUID& GEOMImpl_MeasureDriver::GetID()
00063 {
00064   static Standard_GUID aMeasureDriver("FF1BBB65-5D14-4df2-980B-3A668264EA16");
00065   return aMeasureDriver; 
00066 }
00067 
00068 
00069 //=======================================================================
00070 //function : GEOMImpl_MeasureDriver
00071 //purpose  : 
00072 //=======================================================================
00073 GEOMImpl_MeasureDriver::GEOMImpl_MeasureDriver() 
00074 {
00075 }
00076 
00077 //=======================================================================
00078 //function : Execute
00079 //purpose  :
00080 //======================================================================= 
00081 Standard_Integer GEOMImpl_MeasureDriver::Execute(TFunction_Logbook& log) const
00082 {
00083   if (Label().IsNull()) return 0;    
00084   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
00085 
00086   GEOMImpl_IMeasure aCI (aFunction);
00087   Standard_Integer aType = aFunction->GetType();
00088 
00089   TopoDS_Shape aShape;
00090 
00091   if (aType == CDG_MEASURE)
00092   {
00093     Handle(GEOM_Function) aRefBase = aCI.GetBase();
00094     TopoDS_Shape aShapeBase = aRefBase->GetValue();
00095     if (aShapeBase.IsNull()) {
00096       Standard_NullObject::Raise("Shape for centre of mass calculation is null");
00097     }
00098 
00099     gp_Ax3 aPos = GEOMImpl_IMeasureOperations::GetPosition(aShapeBase);
00100     gp_Pnt aCenterMass = aPos.Location();
00101     aShape = BRepBuilderAPI_MakeVertex(aCenterMass).Shape();
00102   }
00103   else if (aType == VERTEX_BY_INDEX)
00104   {
00105     Handle(GEOM_Function) aRefBase = aCI.GetBase();
00106     TopoDS_Shape aShapeBase = aRefBase->GetValue();
00107     if (aShapeBase.IsNull()) {
00108       Standard_NullObject::Raise("Shape for centre of mass calculation is null");
00109     }
00110 
00111     int index = aCI.GetIndex();
00112     gp_Pnt aVertex;
00113 
00114     if (aShapeBase.ShapeType() == TopAbs_VERTEX) {
00115       if ( index != 1 )
00116         Standard_NullObject::Raise("Vertex index is out of range");
00117       else
00118         aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aShapeBase));
00119     } else if (aShapeBase.ShapeType() == TopAbs_EDGE) {
00120       TopoDS_Vertex aV1, aV2;
00121       TopoDS_Edge anEdgeE = TopoDS::Edge(aShapeBase);
00122       
00123       TopExp::Vertices(anEdgeE, aV1, aV2);
00124       gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
00125       gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
00126 
00127       if (index < 0 || index > 1)
00128         Standard_NullObject::Raise("Vertex index is out of range");
00129 
00130       if ( ( anEdgeE.Orientation() == TopAbs_FORWARD && index == 0 ) ||
00131            ( anEdgeE.Orientation() == TopAbs_REVERSED && index == 1 ) )
00132         aVertex = aP1;
00133       else
00134       aVertex = aP2;
00135     } else if (aShapeBase.ShapeType() == TopAbs_WIRE) {
00136       TopTools_IndexedMapOfShape anEdgeShapes;
00137       TopTools_IndexedMapOfShape aVertexShapes;
00138       TopoDS_Vertex aV1, aV2;
00139       TopoDS_Wire aWire = TopoDS::Wire(aShapeBase);
00140       TopExp_Explorer exp (aWire, TopAbs_EDGE);
00141       for (; exp.More(); exp.Next()) {
00142         anEdgeShapes.Add(exp.Current());
00143         TopoDS_Edge E = TopoDS::Edge(exp.Current());
00144         TopExp::Vertices(E, aV1, aV2);
00145         if ( aVertexShapes.Extent() == 0)
00146           aVertexShapes.Add(aV1);
00147         if ( !aV1.IsSame( aVertexShapes(aVertexShapes.Extent()) ) )
00148           aVertexShapes.Add(aV1);
00149         if ( !aV2.IsSame( aVertexShapes(aVertexShapes.Extent()) ) )
00150           aVertexShapes.Add(aV2);
00151       }
00152 
00153       if (index < 0 || index > aVertexShapes.Extent())
00154         Standard_NullObject::Raise("Vertex index is out of range");
00155 
00156       if (aWire.Orientation() == TopAbs_FORWARD)
00157         aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aVertexShapes(index+1)));
00158       else
00159         aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aVertexShapes(aVertexShapes.Extent() - index)));
00160     } else {
00161       Standard_NullObject::Raise("Shape for vertex calculation is not an edge or wire");
00162     }
00163 
00164     aShape = BRepBuilderAPI_MakeVertex(aVertex).Shape();
00165   }
00166   else if (aType == VECTOR_FACE_NORMALE)
00167   {
00168     // Face
00169     Handle(GEOM_Function) aRefBase = aCI.GetBase();
00170     TopoDS_Shape aShapeBase = aRefBase->GetValue();
00171     if (aShapeBase.IsNull()) {
00172       Standard_NullObject::Raise("Face for normale calculation is null");
00173     }
00174     if (aShapeBase.ShapeType() != TopAbs_FACE) {
00175       Standard_NullObject::Raise("Shape for normale calculation is not a face");
00176     }
00177     TopoDS_Face aFace = TopoDS::Face(aShapeBase);
00178 
00179     // Point
00180     gp_Pnt p1 (0,0,0);
00181 
00182     Handle(GEOM_Function) aPntFunc = aCI.GetPoint();
00183     if (!aPntFunc.IsNull())
00184     {
00185       TopoDS_Shape anOptPnt = aPntFunc->GetValue();
00186       if (anOptPnt.IsNull())
00187         Standard_NullObject::Raise("Invalid shape given for point argument");
00188       p1 = BRep_Tool::Pnt(TopoDS::Vertex(anOptPnt));
00189     }
00190     else
00191     {
00192       gp_Ax3 aPos = GEOMImpl_IMeasureOperations::GetPosition(aFace);
00193       p1 = aPos.Location();
00194     }
00195 
00196     // Point parameters on surface
00197     Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
00198     Handle(ShapeAnalysis_Surface) aSurfAna = new ShapeAnalysis_Surface (aSurf);
00199     gp_Pnt2d pUV = aSurfAna->ValueOfUV(p1, Precision::Confusion());
00200 
00201     // Normal direction
00202     gp_Vec Vec1,Vec2;
00203     BRepAdaptor_Surface SF (aFace);
00204     SF.D1(pUV.X(), pUV.Y(), p1, Vec1, Vec2);
00205     if (Vec1.Magnitude() < Precision::Confusion()) {
00206       gp_Vec tmpV;
00207       gp_Pnt tmpP;
00208       SF.D1(pUV.X(), pUV.Y()-0.1, tmpP, Vec1, tmpV);
00209     }
00210     else if (Vec2.Magnitude() < Precision::Confusion()) {
00211       gp_Vec tmpV;
00212       gp_Pnt tmpP;
00213       SF.D1(pUV.X()-0.1, pUV.Y(), tmpP, tmpV, Vec2);
00214     }
00215 
00216     gp_Vec V = Vec1.Crossed(Vec2);
00217     Standard_Real mod = V.Magnitude();
00218     if (mod < Precision::Confusion())
00219       Standard_NullObject::Raise("Normal vector of a face has null magnitude");
00220 
00221     // Set length of normal vector to average radius of curvature
00222     Standard_Real radius = 0.0;
00223     GeomLProp_SLProps aProperties (aSurf, pUV.X(), pUV.Y(), 2, Precision::Confusion());
00224     if (aProperties.IsCurvatureDefined()) {
00225       Standard_Real radius1 = Abs(aProperties.MinCurvature());
00226       Standard_Real radius2 = Abs(aProperties.MaxCurvature());
00227       if (Abs(radius1) > Precision::Confusion()) {
00228         radius = 1.0 / radius1;
00229         if (Abs(radius2) > Precision::Confusion()) {
00230           radius = (radius + 1.0 / radius2) / 2.0;
00231         }
00232       }
00233       else {
00234         if (Abs(radius2) > Precision::Confusion()) {
00235           radius = 1.0 / radius2;
00236         }
00237       }
00238     }
00239 
00240     // Set length of normal vector to average dimension of the face
00241     // (only if average radius of curvature is not appropriate)
00242     if (radius < Precision::Confusion()) {
00243         Bnd_Box B;
00244         Standard_Real Xmin, Xmax, Ymin, Ymax, Zmin, Zmax;
00245         BRepBndLib::Add(aFace, B);
00246         B.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
00247         radius = ((Xmax - Xmin) + (Ymax - Ymin) + (Zmax - Zmin)) / 3.0;
00248     }
00249 
00250     if (radius < Precision::Confusion())
00251       radius = 1.0;
00252 
00253     V *= radius / mod;
00254 
00255     // consider the face orientation
00256     if (aFace.Orientation() == TopAbs_REVERSED ||
00257         aFace.Orientation() == TopAbs_INTERNAL) {
00258       V = - V;
00259     }
00260 
00261     // Edge
00262     gp_Pnt p2 = p1.Translated(V);
00263     BRepBuilderAPI_MakeEdge aBuilder (p1, p2);
00264     if (!aBuilder.IsDone())
00265       Standard_NullObject::Raise("Vector construction failed");
00266     aShape = aBuilder.Shape();
00267   }
00268   else {
00269   }
00270 
00271   if (aShape.IsNull()) return 0;
00272 
00273   aFunction->SetValue(aShape);
00274 
00275   log.SetTouched(Label()); 
00276 
00277   return 1;
00278 }
00279 
00280 
00281 //=======================================================================
00282 //function :  GEOMImpl_MeasureDriver_Type_
00283 //purpose  :
00284 //======================================================================= 
00285 Standard_EXPORT Handle_Standard_Type& GEOMImpl_MeasureDriver_Type_()
00286 {
00287 
00288   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
00289   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
00290   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
00291   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
00292   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
00293   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
00294  
00295 
00296   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
00297   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_MeasureDriver",
00298                                                          sizeof(GEOMImpl_MeasureDriver),
00299                                                          1,
00300                                                          (Standard_Address)_Ancestors,
00301                                                          (Standard_Address)NULL);
00302 
00303   return _aType;
00304 }
00305 
00306 //=======================================================================
00307 //function : DownCast
00308 //purpose  :
00309 //======================================================================= 
00310 const Handle(GEOMImpl_MeasureDriver) Handle(GEOMImpl_MeasureDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
00311 {
00312   Handle(GEOMImpl_MeasureDriver) _anOtherObject;
00313 
00314   if (!AnObject.IsNull()) {
00315      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_MeasureDriver))) {
00316        _anOtherObject = Handle(GEOMImpl_MeasureDriver)((Handle(GEOMImpl_MeasureDriver)&)AnObject);
00317      }
00318   }
00319 
00320   return _anOtherObject ;
00321 }