1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include <Standard_Stream.hxx>
25 #include <GEOMImpl_MeasureDriver.hxx>
26 #include <GEOMImpl_IMeasure.hxx>
27 #include <GEOMImpl_IMeasureOperations.hxx>
28 #include <GEOMImpl_Types.hxx>
29 #include <GEOM_Function.hxx>
31 #include <BRep_Tool.hxx>
32 #include <BRepGProp.hxx>
33 #include <BRepBuilderAPI_MakeVertex.hxx>
34 #include <BRepBuilderAPI_MakeEdge.hxx>
38 #include <TopoDS_Shape.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Wire.hxx>
42 #include <TopExp_Explorer.hxx>
43 #include <TopTools_IndexedMapOfShape.hxx>
45 #include <GProp_GProps.hxx>
46 #include <GeomLProp_SLProps.hxx>
47 #include <Geom_Surface.hxx>
49 #include <Bnd_Box.hxx>
50 #include <BRepBndLib.hxx>
51 #include <BRepAdaptor_Surface.hxx>
52 #include <ShapeAnalysis_Surface.hxx>
55 #include <Precision.hxx>
56 #include <Standard_NullObject.hxx>
58 //=======================================================================
61 //=======================================================================
62 const Standard_GUID& GEOMImpl_MeasureDriver::GetID()
64 static Standard_GUID aMeasureDriver("FF1BBB65-5D14-4df2-980B-3A668264EA16");
65 return aMeasureDriver;
69 //=======================================================================
70 //function : GEOMImpl_MeasureDriver
72 //=======================================================================
73 GEOMImpl_MeasureDriver::GEOMImpl_MeasureDriver()
77 //=======================================================================
80 //=======================================================================
81 Standard_Integer GEOMImpl_MeasureDriver::Execute(TFunction_Logbook& log) const
83 if (Label().IsNull()) return 0;
84 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
86 GEOMImpl_IMeasure aCI (aFunction);
87 Standard_Integer aType = aFunction->GetType();
91 if (aType == CDG_MEASURE)
93 Handle(GEOM_Function) aRefBase = aCI.GetBase();
94 TopoDS_Shape aShapeBase = aRefBase->GetValue();
95 if (aShapeBase.IsNull()) {
96 Standard_NullObject::Raise("Shape for centre of mass calculation is null");
99 gp_Ax3 aPos = GEOMImpl_IMeasureOperations::GetPosition(aShapeBase);
100 gp_Pnt aCenterMass = aPos.Location();
101 aShape = BRepBuilderAPI_MakeVertex(aCenterMass).Shape();
103 else if (aType == VERTEX_BY_INDEX)
105 Handle(GEOM_Function) aRefBase = aCI.GetBase();
106 TopoDS_Shape aShapeBase = aRefBase->GetValue();
107 if (aShapeBase.IsNull()) {
108 Standard_NullObject::Raise("Shape for centre of mass calculation is null");
111 int index = aCI.GetIndex();
114 if (aShapeBase.ShapeType() == TopAbs_VERTEX) {
116 Standard_NullObject::Raise("Vertex index is out of range");
118 aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aShapeBase));
119 } else if (aShapeBase.ShapeType() == TopAbs_EDGE) {
120 TopoDS_Vertex aV1, aV2;
121 TopoDS_Edge anEdgeE = TopoDS::Edge(aShapeBase);
123 TopExp::Vertices(anEdgeE, aV1, aV2);
124 gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
125 gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
127 if (index < 0 || index > 1)
128 Standard_NullObject::Raise("Vertex index is out of range");
130 if ( ( anEdgeE.Orientation() == TopAbs_FORWARD && index == 0 ) ||
131 ( anEdgeE.Orientation() == TopAbs_REVERSED && index == 1 ) )
135 } else if (aShapeBase.ShapeType() == TopAbs_WIRE) {
136 TopTools_IndexedMapOfShape anEdgeShapes;
137 TopTools_IndexedMapOfShape aVertexShapes;
138 TopoDS_Vertex aV1, aV2;
139 TopoDS_Wire aWire = TopoDS::Wire(aShapeBase);
140 TopExp_Explorer exp (aWire, TopAbs_EDGE);
141 for (; exp.More(); exp.Next()) {
142 anEdgeShapes.Add(exp.Current());
143 TopoDS_Edge E = TopoDS::Edge(exp.Current());
144 TopExp::Vertices(E, aV1, aV2);
145 if ( aVertexShapes.Extent() == 0)
146 aVertexShapes.Add(aV1);
147 if ( !aV1.IsSame( aVertexShapes(aVertexShapes.Extent()) ) )
148 aVertexShapes.Add(aV1);
149 if ( !aV2.IsSame( aVertexShapes(aVertexShapes.Extent()) ) )
150 aVertexShapes.Add(aV2);
153 if (index < 0 || index > aVertexShapes.Extent())
154 Standard_NullObject::Raise("Vertex index is out of range");
156 if (aWire.Orientation() == TopAbs_FORWARD)
157 aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aVertexShapes(index+1)));
159 aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aVertexShapes(aVertexShapes.Extent() - index)));
161 Standard_NullObject::Raise("Shape for vertex calculation is not an edge or wire");
164 aShape = BRepBuilderAPI_MakeVertex(aVertex).Shape();
166 else if (aType == VECTOR_FACE_NORMALE)
169 Handle(GEOM_Function) aRefBase = aCI.GetBase();
170 TopoDS_Shape aShapeBase = aRefBase->GetValue();
171 if (aShapeBase.IsNull()) {
172 Standard_NullObject::Raise("Face for normale calculation is null");
174 if (aShapeBase.ShapeType() != TopAbs_FACE) {
175 Standard_NullObject::Raise("Shape for normale calculation is not a face");
177 TopoDS_Face aFace = TopoDS::Face(aShapeBase);
182 Handle(GEOM_Function) aPntFunc = aCI.GetPoint();
183 if (!aPntFunc.IsNull())
185 TopoDS_Shape anOptPnt = aPntFunc->GetValue();
186 if (anOptPnt.IsNull())
187 Standard_NullObject::Raise("Invalid shape given for point argument");
188 p1 = BRep_Tool::Pnt(TopoDS::Vertex(anOptPnt));
192 gp_Ax3 aPos = GEOMImpl_IMeasureOperations::GetPosition(aFace);
193 p1 = aPos.Location();
196 // Point parameters on surface
197 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
198 Handle(ShapeAnalysis_Surface) aSurfAna = new ShapeAnalysis_Surface (aSurf);
199 gp_Pnt2d pUV = aSurfAna->ValueOfUV(p1, Precision::Confusion());
203 BRepAdaptor_Surface SF (aFace);
204 SF.D1(pUV.X(), pUV.Y(), p1, Vec1, Vec2);
205 if (Vec1.Magnitude() < Precision::Confusion()) {
208 SF.D1(pUV.X(), pUV.Y()-0.1, tmpP, Vec1, tmpV);
210 else if (Vec2.Magnitude() < Precision::Confusion()) {
213 SF.D1(pUV.X()-0.1, pUV.Y(), tmpP, tmpV, Vec2);
216 gp_Vec V = Vec1.Crossed(Vec2);
217 Standard_Real mod = V.Magnitude();
218 if (mod < Precision::Confusion())
219 Standard_NullObject::Raise("Normal vector of a face has null magnitude");
221 // Set length of normal vector to average radius of curvature
222 Standard_Real radius = 0.0;
223 GeomLProp_SLProps aProperties (aSurf, pUV.X(), pUV.Y(), 2, Precision::Confusion());
224 if (aProperties.IsCurvatureDefined()) {
225 Standard_Real radius1 = Abs(aProperties.MinCurvature());
226 Standard_Real radius2 = Abs(aProperties.MaxCurvature());
227 if (Abs(radius1) > Precision::Confusion()) {
228 radius = 1.0 / radius1;
229 if (Abs(radius2) > Precision::Confusion()) {
230 radius = (radius + 1.0 / radius2) / 2.0;
234 if (Abs(radius2) > Precision::Confusion()) {
235 radius = 1.0 / radius2;
240 // Set length of normal vector to average dimension of the face
241 // (only if average radius of curvature is not appropriate)
242 if (radius < Precision::Confusion()) {
244 Standard_Real Xmin, Xmax, Ymin, Ymax, Zmin, Zmax;
245 BRepBndLib::Add(aFace, B);
246 B.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
247 radius = ((Xmax - Xmin) + (Ymax - Ymin) + (Zmax - Zmin)) / 3.0;
250 if (radius < Precision::Confusion())
255 // consider the face orientation
256 if (aFace.Orientation() == TopAbs_REVERSED ||
257 aFace.Orientation() == TopAbs_INTERNAL) {
262 gp_Pnt p2 = p1.Translated(V);
263 BRepBuilderAPI_MakeEdge aBuilder (p1, p2);
264 if (!aBuilder.IsDone())
265 Standard_NullObject::Raise("Vector construction failed");
266 aShape = aBuilder.Shape();
271 if (aShape.IsNull()) return 0;
273 aFunction->SetValue(aShape);
275 log.SetTouched(Label());
281 //=======================================================================
282 //function : GEOMImpl_MeasureDriver_Type_
284 //=======================================================================
285 Standard_EXPORT Handle_Standard_Type& GEOMImpl_MeasureDriver_Type_()
288 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
289 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
290 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
291 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
292 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
293 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
296 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
297 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_MeasureDriver",
298 sizeof(GEOMImpl_MeasureDriver),
300 (Standard_Address)_Ancestors,
301 (Standard_Address)NULL);
306 //=======================================================================
307 //function : DownCast
309 //=======================================================================
310 const Handle(GEOMImpl_MeasureDriver) Handle(GEOMImpl_MeasureDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
312 Handle(GEOMImpl_MeasureDriver) _anOtherObject;
314 if (!AnObject.IsNull()) {
315 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_MeasureDriver))) {
316 _anOtherObject = Handle(GEOMImpl_MeasureDriver)((Handle(GEOMImpl_MeasureDriver)&)AnObject);
320 return _anOtherObject ;