Salome HOME
Mantis issue 0021465: EDF 2067 GEOM: Extrusion along a path leads to a self-intersect...
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ArchimedeDriver.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21
22 #include "GEOMImpl_ArchimedeDriver.hxx"
23 #include "GEOMImpl_IArchimede.hxx"
24 #include "GEOMImpl_Types.hxx"
25
26 #include "Archimede_VolumeSection.hxx"
27
28 #include <Basics_OCCTVersion.hxx>
29
30 #include <stdio.h>
31
32 #include <BRepBuilderAPI_MakeFace.hxx>
33
34 #include <TopoDS_Shape.hxx>
35 #include <TopoDS_Face.hxx>
36
37 #include <Geom_Plane.hxx>
38 #include <Geom_Geometry.hxx>
39 #include <Geom_RectangularTrimmedSurface.hxx>
40
41 #include <gp_Pnt.hxx>
42 #include <gp_Dir.hxx>
43
44 #include <StdFail_NotDone.hxx>
45
46
47 //=======================================================================
48 //function : GetID
49 //purpose  :
50 //=======================================================================
51 const Standard_GUID& GEOMImpl_ArchimedeDriver::GetID()
52 {
53   static Standard_GUID aArchimedeDriver("FF1BBB59-5D14-4df2-980B-3A668264EA16");
54   return aArchimedeDriver;
55 }
56
57
58 //=======================================================================
59 //function : GEOMImpl_ArchimedeDriver
60 //purpose  :
61 //=======================================================================
62
63 GEOMImpl_ArchimedeDriver::GEOMImpl_ArchimedeDriver()
64 {
65 }
66
67 //=======================================================================
68 //function : Execute
69 //purpose  :
70 //=======================================================================
71 Standard_Integer GEOMImpl_ArchimedeDriver::Execute(TFunction_Logbook& log) const
72 {
73   if (Label().IsNull()) return 0;
74   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
75
76   Standard_Integer aType = aFunction->GetType();
77   if (aType != ARCHIMEDE_TYPE) return 0;
78
79   GEOMImpl_IArchimede IA (aFunction);
80
81   Handle(GEOM_Function) aShapeFunction = IA.GetBasicShape();
82   if (aShapeFunction.IsNull()) return 0;
83   TopoDS_Shape shape = aShapeFunction->GetValue();
84   if(shape.IsNull()) return 0;
85
86   Standard_Real aWaterDensity = IA.GetDensity();
87   Standard_Real aWeight = IA.GetWeight();
88   Standard_Real aMeshingDeflection = IA.GetDeflection();
89
90   double cste = -1;
91   if (aWaterDensity != 0.)
92     cste = aWeight/aWaterDensity;
93   else
94     return 0;
95
96   gp_Dir direct (0.0, 0.0, 1.0);
97   gp_Pnt PosPlan (0.0, 0.0, 0.0);
98   Geom_Plane PP (PosPlan, direct);
99   Handle(Geom_Plane) P = Handle(Geom_Plane)::DownCast(PP.Copy());
100
101   gp_Dir Zdirection (0.0, 0.0, 1.0);
102   VolumeSection VOL (shape, aMeshingDeflection);
103   VOL.SetPlane(P);
104   Handle(Geom_RectangularTrimmedSurface) SurfaceTrimmee;
105
106   if (Zdirection.IsEqual(direct, Precision::Angular()) == Standard_False) {
107     VOL.MakeRotation(direct);
108   }
109
110   VOL.CenterOfGravity();
111   SurfaceTrimmee = VOL.TrimSurf();
112   Standard_Real Cote = VOL.Archimede(cste, aMeshingDeflection);
113
114   if (Cote == -1) {
115     double Zmin,Zmax;
116     VOL.getZ(Zmin,Zmax);
117     double volume = VOL.CalculateVolume(Zmax) * aWaterDensity;
118
119     char msg[100] = "";
120     sprintf(msg, "shape sinks to the bottom : Weigth max = %.1f", volume);
121
122     StdFail_NotDone::Raise(msg);
123   }
124
125   SurfaceTrimmee = VOL.AjustePlan(SurfaceTrimmee,Cote,PosPlan);
126   if (Zdirection.IsEqual(direct,Precision::Angular()) == Standard_False) {
127     SurfaceTrimmee = VOL.InvMakeRotation(direct,SurfaceTrimmee);
128   }
129
130   Standard_Real u1,u2,v1,v2;
131   SurfaceTrimmee->Bounds(u1,u2,v1,v2);
132 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
133   TopoDS_Face tirant = BRepBuilderAPI_MakeFace(SurfaceTrimmee, u1, u2, v1, v2, Precision::Confusion());
134 #else
135   TopoDS_Face tirant = BRepBuilderAPI_MakeFace(SurfaceTrimmee, u1, u2, v1, v2);
136 #endif
137
138   if (tirant.IsNull()) {
139     StdFail_NotDone::Raise("Failed to build secant face");
140   }
141
142   aFunction->SetValue(tirant);
143
144   log.SetTouched(Label());
145
146   return 1;
147 }
148
149
150 //=======================================================================
151 //function :  GEOMImpl_ArchimedeDriver_Type_
152 //purpose  :
153 //=======================================================================
154 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ArchimedeDriver_Type_()
155 {
156
157   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
158   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
159   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
160   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
161   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
162   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
163
164
165   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
166   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ArchimedeDriver",
167                                                          sizeof(GEOMImpl_ArchimedeDriver),
168                                                          1,
169                                                          (Standard_Address)_Ancestors,
170                                                          (Standard_Address)NULL);
171
172   return _aType;
173 }
174
175 //=======================================================================
176 //function : DownCast
177 //purpose  :
178 //=======================================================================
179
180 const Handle(GEOMImpl_ArchimedeDriver) Handle(GEOMImpl_ArchimedeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
181 {
182   Handle(GEOMImpl_ArchimedeDriver) _anOtherObject;
183
184   if (!AnObject.IsNull()) {
185      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ArchimedeDriver))) {
186        _anOtherObject = Handle(GEOMImpl_ArchimedeDriver)((Handle(GEOMImpl_ArchimedeDriver)&)AnObject);
187      }
188   }
189
190   return _anOtherObject;
191 }