1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include <Standard_Stream.hxx>
23 #include <GEOMImpl_MeasureDriver.hxx>
24 #include <GEOMImpl_IMeasure.hxx>
25 #include <GEOMImpl_IMeasureOperations.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOM_Function.hxx>
29 #include <BRep_Tool.hxx>
30 #include <BRepGProp.hxx>
31 #include <BRepBuilderAPI_MakeVertex.hxx>
32 #include <BRepBuilderAPI_MakeEdge.hxx>
36 #include <TopoDS_Shape.hxx>
38 #include <GProp_GProps.hxx>
39 #include <GeomLProp_SLProps.hxx>
40 #include <Geom_Surface.hxx>
42 #include <Bnd_Box.hxx>
43 #include <BRepBndLib.hxx>
44 #include <BRepAdaptor_Surface.hxx>
45 #include <ShapeAnalysis_Surface.hxx>
48 #include <Precision.hxx>
49 #include <Standard_NullObject.hxx>
51 //=======================================================================
54 //=======================================================================
55 const Standard_GUID& GEOMImpl_MeasureDriver::GetID()
57 static Standard_GUID aMeasureDriver("FF1BBB65-5D14-4df2-980B-3A668264EA16");
58 return aMeasureDriver;
62 //=======================================================================
63 //function : GEOMImpl_MeasureDriver
65 //=======================================================================
66 GEOMImpl_MeasureDriver::GEOMImpl_MeasureDriver()
70 //=======================================================================
73 //=======================================================================
74 Standard_Integer GEOMImpl_MeasureDriver::Execute(TFunction_Logbook& log) const
76 if (Label().IsNull()) return 0;
77 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
79 GEOMImpl_IMeasure aCI (aFunction);
80 Standard_Integer aType = aFunction->GetType();
84 if (aType == CDG_MEASURE)
86 Handle(GEOM_Function) aRefBase = aCI.GetBase();
87 TopoDS_Shape aShapeBase = aRefBase->GetValue();
88 if (aShapeBase.IsNull()) {
89 Standard_NullObject::Raise("Shape for centre of mass calculation is null");
95 if (aShapeBase.ShapeType() == TopAbs_VERTEX) {
96 aCenterMass = BRep_Tool::Pnt(TopoDS::Vertex(aShapeBase));
97 } else if (aShapeBase.ShapeType() == TopAbs_EDGE || aShapeBase.ShapeType() == TopAbs_WIRE) {
98 BRepGProp::LinearProperties(aShapeBase, aSystem);
99 aCenterMass = aSystem.CentreOfMass();
100 } else if (aShapeBase.ShapeType() == TopAbs_FACE || aShapeBase.ShapeType() == TopAbs_SHELL) {
101 BRepGProp::SurfaceProperties(aShapeBase, aSystem);
102 aCenterMass = aSystem.CentreOfMass();
104 BRepGProp::VolumeProperties(aShapeBase, aSystem);
105 aCenterMass = aSystem.CentreOfMass();
108 aShape = BRepBuilderAPI_MakeVertex(aCenterMass).Shape();
110 else if (aType == VECTOR_FACE_NORMALE)
113 Handle(GEOM_Function) aRefBase = aCI.GetBase();
114 TopoDS_Shape aShapeBase = aRefBase->GetValue();
115 if (aShapeBase.IsNull()) {
116 Standard_NullObject::Raise("Face for normale calculation is null");
118 if (aShapeBase.ShapeType() != TopAbs_FACE) {
119 Standard_NullObject::Raise("Shape for normale calculation is not a face");
121 TopoDS_Face aFace = TopoDS::Face(aShapeBase);
126 Handle(GEOM_Function) aPntFunc = aCI.GetPoint();
127 if (!aPntFunc.IsNull())
129 TopoDS_Shape anOptPnt = aPntFunc->GetValue();
130 if (anOptPnt.IsNull())
131 Standard_NullObject::Raise("Invalid shape given for point argument");
132 p1 = BRep_Tool::Pnt(TopoDS::Vertex(anOptPnt));
136 gp_Ax3 aPos = GEOMImpl_IMeasureOperations::GetPosition(aFace);
137 p1 = aPos.Location();
140 // Point parameters on surface
141 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
142 Handle(ShapeAnalysis_Surface) aSurfAna = new ShapeAnalysis_Surface (aSurf);
143 gp_Pnt2d pUV = aSurfAna->ValueOfUV(p1, Precision::Confusion());
147 BRepAdaptor_Surface SF (aFace);
148 SF.D1(pUV.X(), pUV.Y(), p1, Vec1, Vec2);
149 gp_Vec V = Vec1.Crossed(Vec2);
150 Standard_Real mod = V.Magnitude();
151 if (mod < Precision::Confusion())
152 Standard_NullObject::Raise("Normal vector of a face has null magnitude");
154 // Set length of normal vector to average radius of curvature
155 Standard_Real radius = 0.0;
156 GeomLProp_SLProps aProperties (aSurf, pUV.X(), pUV.Y(), 2, Precision::Confusion());
157 if (aProperties.IsCurvatureDefined()) {
158 Standard_Real radius1 = Abs(aProperties.MinCurvature());
159 Standard_Real radius2 = Abs(aProperties.MaxCurvature());
160 if (Abs(radius1) > Precision::Confusion()) {
161 radius = 1.0 / radius1;
162 if (Abs(radius2) > Precision::Confusion()) {
163 radius = (radius + 1.0 / radius2) / 2.0;
167 if (Abs(radius2) > Precision::Confusion()) {
168 radius = 1.0 / radius2;
173 // Set length of normal vector to average dimension of the face
174 // (only if average radius of curvature is not appropriate)
175 if (radius < Precision::Confusion()) {
177 Standard_Real Xmin, Xmax, Ymin, Ymax, Zmin, Zmax;
178 BRepBndLib::Add(aFace, B);
179 B.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
180 radius = ((Xmax - Xmin) + (Ymax - Ymin) + (Zmax - Zmin)) / 3.0;
183 if (radius < Precision::Confusion())
188 // consider the face orientation
189 if (aFace.Orientation() == TopAbs_REVERSED ||
190 aFace.Orientation() == TopAbs_INTERNAL) {
195 gp_Pnt p2 = p1.Translated(V);
196 BRepBuilderAPI_MakeEdge aBuilder (p1, p2);
197 if (!aBuilder.IsDone())
198 Standard_NullObject::Raise("Vector construction failed");
199 aShape = aBuilder.Shape();
204 if (aShape.IsNull()) return 0;
206 aFunction->SetValue(aShape);
208 log.SetTouched(Label());
214 //=======================================================================
215 //function : GEOMImpl_MeasureDriver_Type_
217 //=======================================================================
218 Standard_EXPORT Handle_Standard_Type& GEOMImpl_MeasureDriver_Type_()
221 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
222 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
223 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
224 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
225 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
226 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
229 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
230 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_MeasureDriver",
231 sizeof(GEOMImpl_MeasureDriver),
233 (Standard_Address)_Ancestors,
234 (Standard_Address)NULL);
239 //=======================================================================
240 //function : DownCast
242 //=======================================================================
243 const Handle(GEOMImpl_MeasureDriver) Handle(GEOMImpl_MeasureDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
245 Handle(GEOMImpl_MeasureDriver) _anOtherObject;
247 if (!AnObject.IsNull()) {
248 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_MeasureDriver))) {
249 _anOtherObject = Handle(GEOMImpl_MeasureDriver)((Handle(GEOMImpl_MeasureDriver)&)AnObject);
253 return _anOtherObject ;