2016-03-15 17:26:56 +05:00
|
|
|
// Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
|
2005-12-05 21:23:52 +05:00
|
|
|
//
|
2012-08-09 13:58:02 +06:00
|
|
|
// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
|
|
|
|
// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
|
2005-12-05 21:23:52 +05:00
|
|
|
//
|
2012-08-09 13:58:02 +06:00
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
2014-02-18 12:44:41 +06:00
|
|
|
// version 2.1 of the License, or (at your option) any later version.
|
2009-02-13 17:16:39 +05:00
|
|
|
//
|
2012-08-09 13:58:02 +06:00
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
2009-02-13 17:16:39 +05:00
|
|
|
//
|
2012-08-09 13:58:02 +06:00
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2009-02-13 17:16:39 +05:00
|
|
|
//
|
2012-08-09 13:58:02 +06:00
|
|
|
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
|
2013-04-01 18:25:01 +06:00
|
|
|
//
|
2012-08-09 13:58:02 +06:00
|
|
|
|
2005-08-11 10:43:57 +06:00
|
|
|
#include <Standard_Stream.hxx>
|
|
|
|
|
|
|
|
#include <GEOMImpl_MeasureDriver.hxx>
|
|
|
|
#include <GEOMImpl_IMeasure.hxx>
|
|
|
|
#include <GEOMImpl_Types.hxx>
|
2013-02-12 17:35:16 +06:00
|
|
|
|
2005-08-11 10:43:57 +06:00
|
|
|
#include <GEOM_Function.hxx>
|
2004-12-01 15:39:14 +05:00
|
|
|
|
2013-02-12 17:35:16 +06:00
|
|
|
#include <GEOMUtils.hxx>
|
|
|
|
|
2004-12-01 15:39:14 +05:00
|
|
|
#include <BRep_Tool.hxx>
|
2013-02-12 17:35:16 +06:00
|
|
|
#include <BRepTools.hxx>
|
2004-12-01 15:39:14 +05:00
|
|
|
#include <BRepGProp.hxx>
|
2013-02-12 17:35:16 +06:00
|
|
|
#include <BRepBuilderAPI_Copy.hxx>
|
2008-03-07 12:45:34 +05:00
|
|
|
#include <BRepBuilderAPI_MakeEdge.hxx>
|
2013-02-12 17:35:16 +06:00
|
|
|
#include <BRepBuilderAPI_MakeVertex.hxx>
|
|
|
|
#include <BRepPrimAPI_MakeBox.hxx>
|
2004-12-01 15:39:14 +05:00
|
|
|
|
|
|
|
#include <TopAbs.hxx>
|
|
|
|
#include <TopoDS.hxx>
|
|
|
|
#include <TopoDS_Shape.hxx>
|
2012-08-09 13:58:02 +06:00
|
|
|
#include <TopoDS_Edge.hxx>
|
|
|
|
#include <TopoDS_Wire.hxx>
|
|
|
|
#include <TopExp.hxx>
|
|
|
|
#include <TopExp_Explorer.hxx>
|
|
|
|
#include <TopTools_IndexedMapOfShape.hxx>
|
2004-12-01 15:39:14 +05:00
|
|
|
|
|
|
|
#include <GProp_GProps.hxx>
|
2008-03-07 12:45:34 +05:00
|
|
|
#include <GeomLProp_SLProps.hxx>
|
|
|
|
#include <Geom_Surface.hxx>
|
|
|
|
|
|
|
|
#include <Bnd_Box.hxx>
|
|
|
|
#include <BRepBndLib.hxx>
|
|
|
|
#include <BRepAdaptor_Surface.hxx>
|
|
|
|
#include <ShapeAnalysis_Surface.hxx>
|
2004-12-01 15:39:14 +05:00
|
|
|
|
|
|
|
#include <gp_Pnt.hxx>
|
|
|
|
#include <Precision.hxx>
|
|
|
|
#include <Standard_NullObject.hxx>
|
2013-02-12 17:35:16 +06:00
|
|
|
#include <StdFail_NotDone.hxx>
|
2004-12-01 15:39:14 +05:00
|
|
|
|
|
|
|
//=======================================================================
|
|
|
|
//function : GetID
|
|
|
|
//purpose :
|
|
|
|
//=======================================================================
|
|
|
|
const Standard_GUID& GEOMImpl_MeasureDriver::GetID()
|
|
|
|
{
|
|
|
|
static Standard_GUID aMeasureDriver("FF1BBB65-5D14-4df2-980B-3A668264EA16");
|
|
|
|
return aMeasureDriver;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//=======================================================================
|
|
|
|
//function : GEOMImpl_MeasureDriver
|
|
|
|
//purpose :
|
|
|
|
//=======================================================================
|
|
|
|
GEOMImpl_MeasureDriver::GEOMImpl_MeasureDriver()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//=======================================================================
|
|
|
|
//function : Execute
|
|
|
|
//purpose :
|
|
|
|
//=======================================================================
|
2016-02-24 18:45:13 +05:00
|
|
|
Standard_Integer GEOMImpl_MeasureDriver::Execute(LOGBOOK& log) const
|
2004-12-01 15:39:14 +05:00
|
|
|
{
|
|
|
|
if (Label().IsNull()) return 0;
|
|
|
|
Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
|
|
|
|
|
|
|
|
GEOMImpl_IMeasure aCI (aFunction);
|
|
|
|
Standard_Integer aType = aFunction->GetType();
|
|
|
|
|
|
|
|
TopoDS_Shape aShape;
|
|
|
|
|
2008-03-07 12:45:34 +05:00
|
|
|
if (aType == CDG_MEASURE)
|
|
|
|
{
|
2004-12-01 15:39:14 +05:00
|
|
|
Handle(GEOM_Function) aRefBase = aCI.GetBase();
|
|
|
|
TopoDS_Shape aShapeBase = aRefBase->GetValue();
|
2013-02-12 17:35:16 +06:00
|
|
|
if (aShapeBase.IsNull())
|
2004-12-01 15:39:14 +05:00
|
|
|
Standard_NullObject::Raise("Shape for centre of mass calculation is null");
|
|
|
|
|
2013-02-12 17:35:16 +06:00
|
|
|
gp_Ax3 aPos = GEOMUtils::GetPosition(aShapeBase);
|
2012-08-09 13:58:02 +06:00
|
|
|
gp_Pnt aCenterMass = aPos.Location();
|
|
|
|
aShape = BRepBuilderAPI_MakeVertex(aCenterMass).Shape();
|
|
|
|
}
|
2013-05-23 18:53:40 +06:00
|
|
|
else if (aType == BND_BOX_MEASURE || aType == BND_BOX_MEASURE_PRECISE)
|
2013-02-12 17:35:16 +06:00
|
|
|
{
|
|
|
|
Handle(GEOM_Function) aRefBase = aCI.GetBase();
|
|
|
|
TopoDS_Shape aShapeBase = aRefBase->GetValue();
|
|
|
|
if (aShapeBase.IsNull())
|
|
|
|
Standard_NullObject::Raise("Shape for bounding box calculation is null");
|
|
|
|
|
|
|
|
BRepBuilderAPI_Copy aCopyTool (aShapeBase);
|
|
|
|
if (!aCopyTool.IsDone())
|
|
|
|
Standard_NullObject::Raise("Shape for bounding box calculation is bad");
|
|
|
|
|
|
|
|
aShapeBase = aCopyTool.Shape();
|
|
|
|
|
|
|
|
// remove triangulation to obtain more exact boundaries
|
|
|
|
BRepTools::Clean(aShapeBase);
|
|
|
|
|
|
|
|
Bnd_Box B;
|
|
|
|
BRepBndLib::Add(aShapeBase, B);
|
|
|
|
|
2013-05-23 18:53:40 +06:00
|
|
|
if (aType == BND_BOX_MEASURE_PRECISE) {
|
|
|
|
if (!GEOMUtils::PreciseBoundingBox(aShapeBase, B)) {
|
|
|
|
Standard_NullObject::Raise("Bounding box cannot be precised");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-12 17:35:16 +06:00
|
|
|
Standard_Real Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
|
|
|
|
B.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
|
|
|
|
|
|
|
|
if (Xmax - Xmin < Precision::Confusion()) Xmax += Precision::Confusion();
|
|
|
|
if (Ymax - Ymin < Precision::Confusion()) Ymax += Precision::Confusion();
|
|
|
|
if (Zmax - Zmin < Precision::Confusion()) Zmax += Precision::Confusion();
|
|
|
|
|
|
|
|
gp_Pnt P1 (Xmin, Ymin, Zmin);
|
|
|
|
gp_Pnt P2 (Xmax, Ymax, Zmax);
|
|
|
|
|
|
|
|
BRepPrimAPI_MakeBox MB (P1, P2);
|
|
|
|
MB.Build();
|
|
|
|
if (!MB.IsDone()) StdFail_NotDone::Raise("Bounding box cannot be computed from the given shape");
|
|
|
|
aShape = MB.Shape();
|
|
|
|
}
|
2012-08-09 13:58:02 +06:00
|
|
|
else if (aType == VERTEX_BY_INDEX)
|
|
|
|
{
|
|
|
|
Handle(GEOM_Function) aRefBase = aCI.GetBase();
|
|
|
|
TopoDS_Shape aShapeBase = aRefBase->GetValue();
|
|
|
|
if (aShapeBase.IsNull()) {
|
|
|
|
Standard_NullObject::Raise("Shape for centre of mass calculation is null");
|
|
|
|
}
|
|
|
|
|
|
|
|
int index = aCI.GetIndex();
|
|
|
|
gp_Pnt aVertex;
|
2004-12-01 15:39:14 +05:00
|
|
|
|
|
|
|
if (aShapeBase.ShapeType() == TopAbs_VERTEX) {
|
2012-08-09 13:58:02 +06:00
|
|
|
if ( index != 1 )
|
|
|
|
Standard_NullObject::Raise("Vertex index is out of range");
|
|
|
|
else
|
|
|
|
aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aShapeBase));
|
|
|
|
} else if (aShapeBase.ShapeType() == TopAbs_EDGE) {
|
|
|
|
TopoDS_Vertex aV1, aV2;
|
|
|
|
TopoDS_Edge anEdgeE = TopoDS::Edge(aShapeBase);
|
|
|
|
|
|
|
|
TopExp::Vertices(anEdgeE, aV1, aV2);
|
|
|
|
gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
|
|
|
|
gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
|
|
|
|
|
|
|
|
if (index < 0 || index > 1)
|
|
|
|
Standard_NullObject::Raise("Vertex index is out of range");
|
|
|
|
|
|
|
|
if ( ( anEdgeE.Orientation() == TopAbs_FORWARD && index == 0 ) ||
|
|
|
|
( anEdgeE.Orientation() == TopAbs_REVERSED && index == 1 ) )
|
|
|
|
aVertex = aP1;
|
|
|
|
else
|
|
|
|
aVertex = aP2;
|
|
|
|
} else if (aShapeBase.ShapeType() == TopAbs_WIRE) {
|
|
|
|
TopTools_IndexedMapOfShape anEdgeShapes;
|
|
|
|
TopTools_IndexedMapOfShape aVertexShapes;
|
|
|
|
TopoDS_Vertex aV1, aV2;
|
|
|
|
TopoDS_Wire aWire = TopoDS::Wire(aShapeBase);
|
|
|
|
TopExp_Explorer exp (aWire, TopAbs_EDGE);
|
|
|
|
for (; exp.More(); exp.Next()) {
|
|
|
|
anEdgeShapes.Add(exp.Current());
|
|
|
|
TopoDS_Edge E = TopoDS::Edge(exp.Current());
|
|
|
|
TopExp::Vertices(E, aV1, aV2);
|
|
|
|
if ( aVertexShapes.Extent() == 0)
|
|
|
|
aVertexShapes.Add(aV1);
|
|
|
|
if ( !aV1.IsSame( aVertexShapes(aVertexShapes.Extent()) ) )
|
|
|
|
aVertexShapes.Add(aV1);
|
|
|
|
if ( !aV2.IsSame( aVertexShapes(aVertexShapes.Extent()) ) )
|
|
|
|
aVertexShapes.Add(aV2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index < 0 || index > aVertexShapes.Extent())
|
|
|
|
Standard_NullObject::Raise("Vertex index is out of range");
|
|
|
|
|
|
|
|
if (aWire.Orientation() == TopAbs_FORWARD)
|
|
|
|
aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aVertexShapes(index+1)));
|
|
|
|
else
|
|
|
|
aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aVertexShapes(aVertexShapes.Extent() - index)));
|
2004-12-01 15:39:14 +05:00
|
|
|
} else {
|
2012-08-09 13:58:02 +06:00
|
|
|
Standard_NullObject::Raise("Shape for vertex calculation is not an edge or wire");
|
2004-12-01 15:39:14 +05:00
|
|
|
}
|
|
|
|
|
2012-08-09 13:58:02 +06:00
|
|
|
aShape = BRepBuilderAPI_MakeVertex(aVertex).Shape();
|
2008-03-07 12:45:34 +05:00
|
|
|
}
|
|
|
|
else if (aType == VECTOR_FACE_NORMALE)
|
|
|
|
{
|
|
|
|
// Face
|
|
|
|
Handle(GEOM_Function) aRefBase = aCI.GetBase();
|
|
|
|
TopoDS_Shape aShapeBase = aRefBase->GetValue();
|
|
|
|
if (aShapeBase.IsNull()) {
|
|
|
|
Standard_NullObject::Raise("Face for normale calculation is null");
|
|
|
|
}
|
|
|
|
if (aShapeBase.ShapeType() != TopAbs_FACE) {
|
|
|
|
Standard_NullObject::Raise("Shape for normale calculation is not a face");
|
|
|
|
}
|
|
|
|
TopoDS_Face aFace = TopoDS::Face(aShapeBase);
|
|
|
|
|
|
|
|
// Point
|
|
|
|
gp_Pnt p1 (0,0,0);
|
|
|
|
|
|
|
|
Handle(GEOM_Function) aPntFunc = aCI.GetPoint();
|
|
|
|
if (!aPntFunc.IsNull())
|
|
|
|
{
|
|
|
|
TopoDS_Shape anOptPnt = aPntFunc->GetValue();
|
|
|
|
if (anOptPnt.IsNull())
|
|
|
|
Standard_NullObject::Raise("Invalid shape given for point argument");
|
|
|
|
p1 = BRep_Tool::Pnt(TopoDS::Vertex(anOptPnt));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-02-12 17:35:16 +06:00
|
|
|
gp_Ax3 aPos = GEOMUtils::GetPosition(aFace);
|
2008-03-07 12:45:34 +05:00
|
|
|
p1 = aPos.Location();
|
|
|
|
}
|
2004-12-01 15:39:14 +05:00
|
|
|
|
2008-03-07 12:45:34 +05:00
|
|
|
// Point parameters on surface
|
|
|
|
Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
|
|
|
|
Handle(ShapeAnalysis_Surface) aSurfAna = new ShapeAnalysis_Surface (aSurf);
|
|
|
|
gp_Pnt2d pUV = aSurfAna->ValueOfUV(p1, Precision::Confusion());
|
|
|
|
|
|
|
|
// Normal direction
|
|
|
|
gp_Vec Vec1,Vec2;
|
|
|
|
BRepAdaptor_Surface SF (aFace);
|
|
|
|
SF.D1(pUV.X(), pUV.Y(), p1, Vec1, Vec2);
|
2012-08-09 13:58:02 +06:00
|
|
|
if (Vec1.Magnitude() < Precision::Confusion()) {
|
|
|
|
gp_Vec tmpV;
|
|
|
|
gp_Pnt tmpP;
|
|
|
|
SF.D1(pUV.X(), pUV.Y()-0.1, tmpP, Vec1, tmpV);
|
|
|
|
}
|
|
|
|
else if (Vec2.Magnitude() < Precision::Confusion()) {
|
|
|
|
gp_Vec tmpV;
|
|
|
|
gp_Pnt tmpP;
|
|
|
|
SF.D1(pUV.X()-0.1, pUV.Y(), tmpP, tmpV, Vec2);
|
|
|
|
}
|
|
|
|
|
2008-03-07 12:45:34 +05:00
|
|
|
gp_Vec V = Vec1.Crossed(Vec2);
|
|
|
|
Standard_Real mod = V.Magnitude();
|
|
|
|
if (mod < Precision::Confusion())
|
|
|
|
Standard_NullObject::Raise("Normal vector of a face has null magnitude");
|
|
|
|
|
|
|
|
// Set length of normal vector to average radius of curvature
|
|
|
|
Standard_Real radius = 0.0;
|
|
|
|
GeomLProp_SLProps aProperties (aSurf, pUV.X(), pUV.Y(), 2, Precision::Confusion());
|
|
|
|
if (aProperties.IsCurvatureDefined()) {
|
|
|
|
Standard_Real radius1 = Abs(aProperties.MinCurvature());
|
|
|
|
Standard_Real radius2 = Abs(aProperties.MaxCurvature());
|
|
|
|
if (Abs(radius1) > Precision::Confusion()) {
|
2012-08-09 13:58:02 +06:00
|
|
|
radius = 1.0 / radius1;
|
2008-03-07 12:45:34 +05:00
|
|
|
if (Abs(radius2) > Precision::Confusion()) {
|
|
|
|
radius = (radius + 1.0 / radius2) / 2.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (Abs(radius2) > Precision::Confusion()) {
|
|
|
|
radius = 1.0 / radius2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set length of normal vector to average dimension of the face
|
|
|
|
// (only if average radius of curvature is not appropriate)
|
|
|
|
if (radius < Precision::Confusion()) {
|
|
|
|
Bnd_Box B;
|
|
|
|
Standard_Real Xmin, Xmax, Ymin, Ymax, Zmin, Zmax;
|
|
|
|
BRepBndLib::Add(aFace, B);
|
|
|
|
B.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
|
|
|
|
radius = ((Xmax - Xmin) + (Ymax - Ymin) + (Zmax - Zmin)) / 3.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (radius < Precision::Confusion())
|
|
|
|
radius = 1.0;
|
|
|
|
|
|
|
|
V *= radius / mod;
|
|
|
|
|
|
|
|
// consider the face orientation
|
|
|
|
if (aFace.Orientation() == TopAbs_REVERSED ||
|
|
|
|
aFace.Orientation() == TopAbs_INTERNAL) {
|
|
|
|
V = - V;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Edge
|
|
|
|
gp_Pnt p2 = p1.Translated(V);
|
|
|
|
BRepBuilderAPI_MakeEdge aBuilder (p1, p2);
|
|
|
|
if (!aBuilder.IsDone())
|
|
|
|
Standard_NullObject::Raise("Vector construction failed");
|
|
|
|
aShape = aBuilder.Shape();
|
|
|
|
}
|
|
|
|
else {
|
2004-12-01 15:39:14 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aShape.IsNull()) return 0;
|
|
|
|
|
|
|
|
aFunction->SetValue(aShape);
|
|
|
|
|
2016-02-24 18:45:13 +05:00
|
|
|
#if OCC_VERSION_MAJOR < 7
|
|
|
|
log.SetTouched(Label());
|
|
|
|
#else
|
|
|
|
log->SetTouched(Label());
|
|
|
|
#endif
|
2004-12-01 15:39:14 +05:00
|
|
|
|
2008-03-07 12:45:34 +05:00
|
|
|
return 1;
|
2004-12-01 15:39:14 +05:00
|
|
|
}
|
|
|
|
|
2013-06-17 18:44:32 +06:00
|
|
|
//================================================================================
|
|
|
|
/*!
|
|
|
|
* \brief Returns a name of creation operation and names and values of creation parameters
|
|
|
|
*/
|
|
|
|
//================================================================================
|
|
|
|
|
|
|
|
bool GEOMImpl_MeasureDriver::
|
|
|
|
GetCreationInformation(std::string& theOperationName,
|
|
|
|
std::vector<GEOM_Param>& theParams)
|
2004-12-01 15:39:14 +05:00
|
|
|
{
|
2013-06-17 18:44:32 +06:00
|
|
|
if (Label().IsNull()) return 0;
|
|
|
|
Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
|
|
|
|
|
|
|
|
GEOMImpl_IMeasure aCI( function );
|
|
|
|
Standard_Integer aType = function->GetType();
|
|
|
|
|
|
|
|
switch ( aType ) {
|
|
|
|
case CDG_MEASURE:
|
|
|
|
theOperationName = "MASS_CENTER";
|
|
|
|
AddParam( theParams, "Object", aCI.GetBase() );
|
|
|
|
break;
|
|
|
|
case BND_BOX_MEASURE:
|
|
|
|
case BND_BOX_MEASURE_PRECISE:
|
|
|
|
theOperationName = "BND_BOX";
|
|
|
|
AddParam( theParams, "Object", aCI.GetBase() );
|
|
|
|
break;
|
|
|
|
case VERTEX_BY_INDEX:
|
|
|
|
theOperationName = "GetVertexByIndex";
|
|
|
|
AddParam( theParams, "Object", aCI.GetBase() );
|
|
|
|
AddParam( theParams, "Index", aCI.GetIndex() );
|
|
|
|
break;
|
|
|
|
case VECTOR_FACE_NORMALE:
|
|
|
|
theOperationName = "NORMALE";
|
|
|
|
AddParam( theParams, "Face", aCI.GetBase() );
|
|
|
|
AddParam( theParams, "Point", aCI.GetPoint(), "face center" );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
2004-12-01 15:39:14 +05:00
|
|
|
}
|
|
|
|
|
2013-06-17 18:44:32 +06:00
|
|
|
return true;
|
2004-12-01 15:39:14 +05:00
|
|
|
}
|
2013-06-17 18:44:32 +06:00
|
|
|
|
2016-02-24 18:45:13 +05:00
|
|
|
OCCT_IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_MeasureDriver,GEOM_BaseDriver);
|