Salome HOME
Bug 0020057: EDF GEOM: Impossible to explode an object to faces. Infinite loop.
[modules/geom.git] / src / GEOMImpl / GEOMImpl_MeasureDriver.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
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.
8 // 
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.
13 //
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
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21 #include <Standard_Stream.hxx>
22
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>
28
29 #include <BRep_Tool.hxx>
30 #include <BRepGProp.hxx>
31 #include <BRepBuilderAPI_MakeVertex.hxx>
32 #include <BRepBuilderAPI_MakeEdge.hxx>
33
34 #include <TopAbs.hxx>
35 #include <TopoDS.hxx>
36 #include <TopoDS_Shape.hxx>
37
38 #include <GProp_GProps.hxx>
39 #include <GeomLProp_SLProps.hxx>
40 #include <Geom_Surface.hxx>
41
42 #include <Bnd_Box.hxx>
43 #include <BRepBndLib.hxx>
44 #include <BRepAdaptor_Surface.hxx>
45 #include <ShapeAnalysis_Surface.hxx>
46
47 #include <gp_Pnt.hxx>
48 #include <Precision.hxx>
49 #include <Standard_NullObject.hxx>
50
51 //=======================================================================
52 //function : GetID
53 //purpose  :
54 //======================================================================= 
55 const Standard_GUID& GEOMImpl_MeasureDriver::GetID()
56 {
57   static Standard_GUID aMeasureDriver("FF1BBB65-5D14-4df2-980B-3A668264EA16");
58   return aMeasureDriver; 
59 }
60
61
62 //=======================================================================
63 //function : GEOMImpl_MeasureDriver
64 //purpose  : 
65 //=======================================================================
66 GEOMImpl_MeasureDriver::GEOMImpl_MeasureDriver() 
67 {
68 }
69
70 //=======================================================================
71 //function : Execute
72 //purpose  :
73 //======================================================================= 
74 Standard_Integer GEOMImpl_MeasureDriver::Execute(TFunction_Logbook& log) const
75 {
76   if (Label().IsNull()) return 0;    
77   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
78
79   GEOMImpl_IMeasure aCI (aFunction);
80   Standard_Integer aType = aFunction->GetType();
81
82   TopoDS_Shape aShape;
83
84   if (aType == CDG_MEASURE)
85   {
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");
90     }
91
92     GProp_GProps aSystem;
93     gp_Pnt aCenterMass;
94
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();
103     } else {
104       BRepGProp::VolumeProperties(aShapeBase, aSystem);
105       aCenterMass = aSystem.CentreOfMass();
106     }
107
108     aShape = BRepBuilderAPI_MakeVertex(aCenterMass).Shape();
109   }
110   else if (aType == VECTOR_FACE_NORMALE)
111   {
112     // Face
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");
117     }
118     if (aShapeBase.ShapeType() != TopAbs_FACE) {
119       Standard_NullObject::Raise("Shape for normale calculation is not a face");
120     }
121     TopoDS_Face aFace = TopoDS::Face(aShapeBase);
122
123     // Point
124     gp_Pnt p1 (0,0,0);
125
126     Handle(GEOM_Function) aPntFunc = aCI.GetPoint();
127     if (!aPntFunc.IsNull())
128     {
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));
133     }
134     else
135     {
136       gp_Ax3 aPos = GEOMImpl_IMeasureOperations::GetPosition(aFace);
137       p1 = aPos.Location();
138     }
139
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());
144
145     // Normal direction
146     gp_Vec Vec1,Vec2;
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");
153
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;
164         }
165       }
166       else {
167         if (Abs(radius2) > Precision::Confusion()) {
168           radius = 1.0 / radius2;
169         }
170       }
171     }
172
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()) {
176         Bnd_Box B;
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;
181     }
182
183     if (radius < Precision::Confusion())
184       radius = 1.0;
185
186     V *= radius / mod;
187
188     // consider the face orientation
189     if (aFace.Orientation() == TopAbs_REVERSED ||
190         aFace.Orientation() == TopAbs_INTERNAL) {
191       V = - V;
192     }
193
194     // Edge
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();
200   }
201   else {
202   }
203
204   if (aShape.IsNull()) return 0;
205
206   aFunction->SetValue(aShape);
207
208   log.SetTouched(Label()); 
209
210   return 1;
211 }
212
213
214 //=======================================================================
215 //function :  GEOMImpl_MeasureDriver_Type_
216 //purpose  :
217 //======================================================================= 
218 Standard_EXPORT Handle_Standard_Type& GEOMImpl_MeasureDriver_Type_()
219 {
220
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);
227  
228
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),
232                                                          1,
233                                                          (Standard_Address)_Ancestors,
234                                                          (Standard_Address)NULL);
235
236   return _aType;
237 }
238
239 //=======================================================================
240 //function : DownCast
241 //purpose  :
242 //======================================================================= 
243 const Handle(GEOMImpl_MeasureDriver) Handle(GEOMImpl_MeasureDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
244 {
245   Handle(GEOMImpl_MeasureDriver) _anOtherObject;
246
247   if (!AnObject.IsNull()) {
248      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_MeasureDriver))) {
249        _anOtherObject = Handle(GEOMImpl_MeasureDriver)((Handle(GEOMImpl_MeasureDriver)&)AnObject);
250      }
251   }
252
253   return _anOtherObject ;
254 }