1 // Copyright (C) 2007-2008 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
22 #include <Standard_Stream.hxx>
24 #include <GEOMImpl_MeasureDriver.hxx>
25 #include <GEOMImpl_IMeasure.hxx>
26 #include <GEOMImpl_IMeasureOperations.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
30 #include <BRep_Tool.hxx>
31 #include <BRepGProp.hxx>
32 #include <BRepBuilderAPI_MakeVertex.hxx>
33 #include <BRepBuilderAPI_MakeEdge.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <TopoDS_Wire.hxx>
41 #include <TopExp_Explorer.hxx>
42 #include <TopTools_IndexedMapOfShape.hxx>
44 #include <GProp_GProps.hxx>
45 #include <GeomLProp_SLProps.hxx>
46 #include <Geom_Surface.hxx>
48 #include <Bnd_Box.hxx>
49 #include <BRepBndLib.hxx>
50 #include <BRepAdaptor_Surface.hxx>
51 #include <ShapeAnalysis_Surface.hxx>
54 #include <Precision.hxx>
55 #include <Standard_NullObject.hxx>
57 //=======================================================================
60 //=======================================================================
61 const Standard_GUID& GEOMImpl_MeasureDriver::GetID()
63 static Standard_GUID aMeasureDriver("FF1BBB65-5D14-4df2-980B-3A668264EA16");
64 return aMeasureDriver;
68 //=======================================================================
69 //function : GEOMImpl_MeasureDriver
71 //=======================================================================
72 GEOMImpl_MeasureDriver::GEOMImpl_MeasureDriver()
76 //=======================================================================
79 //=======================================================================
80 Standard_Integer GEOMImpl_MeasureDriver::Execute(TFunction_Logbook& log) const
82 if (Label().IsNull()) return 0;
83 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
85 GEOMImpl_IMeasure aCI (aFunction);
86 Standard_Integer aType = aFunction->GetType();
90 if (aType == CDG_MEASURE)
92 Handle(GEOM_Function) aRefBase = aCI.GetBase();
93 TopoDS_Shape aShapeBase = aRefBase->GetValue();
94 if (aShapeBase.IsNull()) {
95 Standard_NullObject::Raise("Shape for centre of mass calculation is null");
101 if (aShapeBase.ShapeType() == TopAbs_VERTEX) {
102 aCenterMass = BRep_Tool::Pnt(TopoDS::Vertex(aShapeBase));
103 } else if (aShapeBase.ShapeType() == TopAbs_EDGE || aShapeBase.ShapeType() == TopAbs_WIRE) {
104 BRepGProp::LinearProperties(aShapeBase, aSystem);
105 aCenterMass = aSystem.CentreOfMass();
106 } else if (aShapeBase.ShapeType() == TopAbs_FACE || aShapeBase.ShapeType() == TopAbs_SHELL) {
107 BRepGProp::SurfaceProperties(aShapeBase, aSystem);
108 aCenterMass = aSystem.CentreOfMass();
110 BRepGProp::VolumeProperties(aShapeBase, aSystem);
111 aCenterMass = aSystem.CentreOfMass();
114 aShape = BRepBuilderAPI_MakeVertex(aCenterMass).Shape();
116 else if (aType == VERTEX_BY_INDEX)
118 Handle(GEOM_Function) aRefBase = aCI.GetBase();
119 TopoDS_Shape aShapeBase = aRefBase->GetValue();
120 if (aShapeBase.IsNull()) {
121 Standard_NullObject::Raise("Shape for centre of mass calculation is null");
124 int index = aCI.GetIndex();
127 if (aShapeBase.ShapeType() == TopAbs_VERTEX) {
129 Standard_NullObject::Raise("Vertex index is out of range");
131 aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aShapeBase));
132 } else if (aShapeBase.ShapeType() == TopAbs_EDGE) {
133 TopoDS_Vertex aV1, aV2;
134 TopoDS_Edge anEdgeE = TopoDS::Edge(aShapeBase);
136 TopExp::Vertices(anEdgeE, aV1, aV2);
137 gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
138 gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
140 if (index < 0 || index > 1)
141 Standard_NullObject::Raise("Vertex index is out of range");
143 if ( anEdgeE.Orientation() == TopAbs_FORWARD && index == 0 ||
144 anEdgeE.Orientation() == TopAbs_REVERSED && index == 1 )
148 } else if (aShapeBase.ShapeType() == TopAbs_WIRE) {
149 TopTools_IndexedMapOfShape anEdgeShapes;
150 TopTools_IndexedMapOfShape aVertexShapes;
151 TopoDS_Vertex aV1, aV2;
152 TopoDS_Wire aWire = TopoDS::Wire(aShapeBase);
153 TopExp_Explorer exp (aWire, TopAbs_EDGE);
154 for (; exp.More(); exp.Next()) {
155 anEdgeShapes.Add(exp.Current());
156 TopoDS_Edge E = TopoDS::Edge(exp.Current());
157 TopExp::Vertices(E, aV1, aV2);
158 if ( aVertexShapes.Extent() == 0)
159 aVertexShapes.Add(aV1);
160 if ( !aV1.IsSame( aVertexShapes(aVertexShapes.Extent()) ) )
161 aVertexShapes.Add(aV1);
162 if ( !aV2.IsSame( aVertexShapes(aVertexShapes.Extent()) ) )
163 aVertexShapes.Add(aV2);
166 if (index < 0 || index > aVertexShapes.Extent())
167 Standard_NullObject::Raise("Vertex index is out of range");
169 if (aWire.Orientation() == TopAbs_FORWARD)
170 aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aVertexShapes(index+1)));
172 aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aVertexShapes(aVertexShapes.Extent() - index)));
174 Standard_NullObject::Raise("Shape for vertex calculation is not an edge or wire");
177 aShape = BRepBuilderAPI_MakeVertex(aVertex).Shape();
179 else if (aType == VECTOR_FACE_NORMALE)
182 Handle(GEOM_Function) aRefBase = aCI.GetBase();
183 TopoDS_Shape aShapeBase = aRefBase->GetValue();
184 if (aShapeBase.IsNull()) {
185 Standard_NullObject::Raise("Face for normale calculation is null");
187 if (aShapeBase.ShapeType() != TopAbs_FACE) {
188 Standard_NullObject::Raise("Shape for normale calculation is not a face");
190 TopoDS_Face aFace = TopoDS::Face(aShapeBase);
195 Handle(GEOM_Function) aPntFunc = aCI.GetPoint();
196 if (!aPntFunc.IsNull())
198 TopoDS_Shape anOptPnt = aPntFunc->GetValue();
199 if (anOptPnt.IsNull())
200 Standard_NullObject::Raise("Invalid shape given for point argument");
201 p1 = BRep_Tool::Pnt(TopoDS::Vertex(anOptPnt));
205 gp_Ax3 aPos = GEOMImpl_IMeasureOperations::GetPosition(aFace);
206 p1 = aPos.Location();
209 // Point parameters on surface
210 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
211 Handle(ShapeAnalysis_Surface) aSurfAna = new ShapeAnalysis_Surface (aSurf);
212 gp_Pnt2d pUV = aSurfAna->ValueOfUV(p1, Precision::Confusion());
216 BRepAdaptor_Surface SF (aFace);
217 SF.D1(pUV.X(), pUV.Y(), p1, Vec1, Vec2);
218 gp_Vec V = Vec1.Crossed(Vec2);
219 Standard_Real mod = V.Magnitude();
220 if (mod < Precision::Confusion())
221 Standard_NullObject::Raise("Normal vector of a face has null magnitude");
223 // Set length of normal vector to average radius of curvature
224 Standard_Real radius = 0.0;
225 GeomLProp_SLProps aProperties (aSurf, pUV.X(), pUV.Y(), 2, Precision::Confusion());
226 if (aProperties.IsCurvatureDefined()) {
227 Standard_Real radius1 = Abs(aProperties.MinCurvature());
228 Standard_Real radius2 = Abs(aProperties.MaxCurvature());
229 if (Abs(radius1) > Precision::Confusion()) {
230 radius = 1.0 / radius1;
231 if (Abs(radius2) > Precision::Confusion()) {
232 radius = (radius + 1.0 / radius2) / 2.0;
236 if (Abs(radius2) > Precision::Confusion()) {
237 radius = 1.0 / radius2;
242 // Set length of normal vector to average dimension of the face
243 // (only if average radius of curvature is not appropriate)
244 if (radius < Precision::Confusion()) {
246 Standard_Real Xmin, Xmax, Ymin, Ymax, Zmin, Zmax;
247 BRepBndLib::Add(aFace, B);
248 B.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
249 radius = ((Xmax - Xmin) + (Ymax - Ymin) + (Zmax - Zmin)) / 3.0;
252 if (radius < Precision::Confusion())
257 // consider the face orientation
258 if (aFace.Orientation() == TopAbs_REVERSED ||
259 aFace.Orientation() == TopAbs_INTERNAL) {
264 gp_Pnt p2 = p1.Translated(V);
265 BRepBuilderAPI_MakeEdge aBuilder (p1, p2);
266 if (!aBuilder.IsDone())
267 Standard_NullObject::Raise("Vector construction failed");
268 aShape = aBuilder.Shape();
273 if (aShape.IsNull()) return 0;
275 aFunction->SetValue(aShape);
277 log.SetTouched(Label());
283 //=======================================================================
284 //function : GEOMImpl_MeasureDriver_Type_
286 //=======================================================================
287 Standard_EXPORT Handle_Standard_Type& GEOMImpl_MeasureDriver_Type_()
290 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
291 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
292 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
293 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
294 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
295 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
298 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
299 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_MeasureDriver",
300 sizeof(GEOMImpl_MeasureDriver),
302 (Standard_Address)_Ancestors,
303 (Standard_Address)NULL);
308 //=======================================================================
309 //function : DownCast
311 //=======================================================================
312 const Handle(GEOMImpl_MeasureDriver) Handle(GEOMImpl_MeasureDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
314 Handle(GEOMImpl_MeasureDriver) _anOtherObject;
316 if (!AnObject.IsNull()) {
317 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_MeasureDriver))) {
318 _anOtherObject = Handle(GEOMImpl_MeasureDriver)((Handle(GEOMImpl_MeasureDriver)&)AnObject);
322 return _anOtherObject ;