1 // Copyright (C) 2007-2010 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");
102 if (aShapeBase.ShapeType() == TopAbs_VERTEX) {
103 aCenterMass = BRep_Tool::Pnt(TopoDS::Vertex(aShapeBase));
104 } else if (aShapeBase.ShapeType() == TopAbs_EDGE || aShapeBase.ShapeType() == TopAbs_WIRE) {
105 BRepGProp::LinearProperties(aShapeBase, aSystem);
106 aCenterMass = aSystem.CentreOfMass();
107 } else if (aShapeBase.ShapeType() == TopAbs_FACE || aShapeBase.ShapeType() == TopAbs_SHELL) {
108 BRepGProp::SurfaceProperties(aShapeBase, aSystem);
109 aCenterMass = aSystem.CentreOfMass();
111 BRepGProp::VolumeProperties(aShapeBase, aSystem);
112 aCenterMass = aSystem.CentreOfMass();
115 aShape = BRepBuilderAPI_MakeVertex(aCenterMass).Shape();
117 else if (aType == VERTEX_BY_INDEX)
119 Handle(GEOM_Function) aRefBase = aCI.GetBase();
120 TopoDS_Shape aShapeBase = aRefBase->GetValue();
121 if (aShapeBase.IsNull()) {
122 Standard_NullObject::Raise("Shape for centre of mass calculation is null");
125 int index = aCI.GetIndex();
128 if (aShapeBase.ShapeType() == TopAbs_VERTEX) {
130 Standard_NullObject::Raise("Vertex index is out of range");
132 aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aShapeBase));
133 } else if (aShapeBase.ShapeType() == TopAbs_EDGE) {
134 TopoDS_Vertex aV1, aV2;
135 TopoDS_Edge anEdgeE = TopoDS::Edge(aShapeBase);
137 TopExp::Vertices(anEdgeE, aV1, aV2);
138 gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
139 gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
141 if (index < 0 || index > 1)
142 Standard_NullObject::Raise("Vertex index is out of range");
144 if ( ( anEdgeE.Orientation() == TopAbs_FORWARD && index == 0 ) ||
145 ( anEdgeE.Orientation() == TopAbs_REVERSED && index == 1 ) )
149 } else if (aShapeBase.ShapeType() == TopAbs_WIRE) {
150 TopTools_IndexedMapOfShape anEdgeShapes;
151 TopTools_IndexedMapOfShape aVertexShapes;
152 TopoDS_Vertex aV1, aV2;
153 TopoDS_Wire aWire = TopoDS::Wire(aShapeBase);
154 TopExp_Explorer exp (aWire, TopAbs_EDGE);
155 for (; exp.More(); exp.Next()) {
156 anEdgeShapes.Add(exp.Current());
157 TopoDS_Edge E = TopoDS::Edge(exp.Current());
158 TopExp::Vertices(E, aV1, aV2);
159 if ( aVertexShapes.Extent() == 0)
160 aVertexShapes.Add(aV1);
161 if ( !aV1.IsSame( aVertexShapes(aVertexShapes.Extent()) ) )
162 aVertexShapes.Add(aV1);
163 if ( !aV2.IsSame( aVertexShapes(aVertexShapes.Extent()) ) )
164 aVertexShapes.Add(aV2);
167 if (index < 0 || index > aVertexShapes.Extent())
168 Standard_NullObject::Raise("Vertex index is out of range");
170 if (aWire.Orientation() == TopAbs_FORWARD)
171 aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aVertexShapes(index+1)));
173 aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aVertexShapes(aVertexShapes.Extent() - index)));
175 Standard_NullObject::Raise("Shape for vertex calculation is not an edge or wire");
178 aShape = BRepBuilderAPI_MakeVertex(aVertex).Shape();
180 else if (aType == VECTOR_FACE_NORMALE)
183 Handle(GEOM_Function) aRefBase = aCI.GetBase();
184 TopoDS_Shape aShapeBase = aRefBase->GetValue();
185 if (aShapeBase.IsNull()) {
186 Standard_NullObject::Raise("Face for normale calculation is null");
188 if (aShapeBase.ShapeType() != TopAbs_FACE) {
189 Standard_NullObject::Raise("Shape for normale calculation is not a face");
191 TopoDS_Face aFace = TopoDS::Face(aShapeBase);
196 Handle(GEOM_Function) aPntFunc = aCI.GetPoint();
197 if (!aPntFunc.IsNull())
199 TopoDS_Shape anOptPnt = aPntFunc->GetValue();
200 if (anOptPnt.IsNull())
201 Standard_NullObject::Raise("Invalid shape given for point argument");
202 p1 = BRep_Tool::Pnt(TopoDS::Vertex(anOptPnt));
206 gp_Ax3 aPos = GEOMImpl_IMeasureOperations::GetPosition(aFace);
207 p1 = aPos.Location();
210 // Point parameters on surface
211 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
212 Handle(ShapeAnalysis_Surface) aSurfAna = new ShapeAnalysis_Surface (aSurf);
213 gp_Pnt2d pUV = aSurfAna->ValueOfUV(p1, Precision::Confusion());
217 BRepAdaptor_Surface SF (aFace);
218 SF.D1(pUV.X(), pUV.Y(), p1, Vec1, Vec2);
219 if (Vec1.Magnitude() < Precision::Confusion()) {
222 SF.D1(pUV.X(), pUV.Y()-0.1, tmpP, Vec1, tmpV);
224 else if (Vec2.Magnitude() < Precision::Confusion()) {
227 SF.D1(pUV.X()-0.1, pUV.Y(), tmpP, tmpV, Vec2);
230 gp_Vec V = Vec1.Crossed(Vec2);
231 Standard_Real mod = V.Magnitude();
232 if (mod < Precision::Confusion())
233 Standard_NullObject::Raise("Normal vector of a face has null magnitude");
235 // Set length of normal vector to average radius of curvature
236 Standard_Real radius = 0.0;
237 GeomLProp_SLProps aProperties (aSurf, pUV.X(), pUV.Y(), 2, Precision::Confusion());
238 if (aProperties.IsCurvatureDefined()) {
239 Standard_Real radius1 = Abs(aProperties.MinCurvature());
240 Standard_Real radius2 = Abs(aProperties.MaxCurvature());
241 if (Abs(radius1) > Precision::Confusion()) {
242 radius = 1.0 / radius1;
243 if (Abs(radius2) > Precision::Confusion()) {
244 radius = (radius + 1.0 / radius2) / 2.0;
248 if (Abs(radius2) > Precision::Confusion()) {
249 radius = 1.0 / radius2;
254 // Set length of normal vector to average dimension of the face
255 // (only if average radius of curvature is not appropriate)
256 if (radius < Precision::Confusion()) {
258 Standard_Real Xmin, Xmax, Ymin, Ymax, Zmin, Zmax;
259 BRepBndLib::Add(aFace, B);
260 B.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
261 radius = ((Xmax - Xmin) + (Ymax - Ymin) + (Zmax - Zmin)) / 3.0;
264 if (radius < Precision::Confusion())
269 // consider the face orientation
270 if (aFace.Orientation() == TopAbs_REVERSED ||
271 aFace.Orientation() == TopAbs_INTERNAL) {
276 gp_Pnt p2 = p1.Translated(V);
277 BRepBuilderAPI_MakeEdge aBuilder (p1, p2);
278 if (!aBuilder.IsDone())
279 Standard_NullObject::Raise("Vector construction failed");
280 aShape = aBuilder.Shape();
285 if (aShape.IsNull()) return 0;
287 aFunction->SetValue(aShape);
289 log.SetTouched(Label());
295 //=======================================================================
296 //function : GEOMImpl_MeasureDriver_Type_
298 //=======================================================================
299 Standard_EXPORT Handle_Standard_Type& GEOMImpl_MeasureDriver_Type_()
302 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
303 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
304 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
305 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
306 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
307 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
310 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
311 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_MeasureDriver",
312 sizeof(GEOMImpl_MeasureDriver),
314 (Standard_Address)_Ancestors,
315 (Standard_Address)NULL);
320 //=======================================================================
321 //function : DownCast
323 //=======================================================================
324 const Handle(GEOMImpl_MeasureDriver) Handle(GEOMImpl_MeasureDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
326 Handle(GEOMImpl_MeasureDriver) _anOtherObject;
328 if (!AnObject.IsNull()) {
329 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_MeasureDriver))) {
330 _anOtherObject = Handle(GEOMImpl_MeasureDriver)((Handle(GEOMImpl_MeasureDriver)&)AnObject);
334 return _anOtherObject ;