Salome HOME
a5c99d9304d78aa62d7a7fc32da080ac725b4a25
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ArchimedeDriver.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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
23 #include "GEOMImpl_ArchimedeDriver.hxx"
24 #include "GEOMImpl_IArchimede.hxx"
25 #include "GEOMImpl_Types.hxx"
26
27 #include "Archimede_VolumeSection.hxx"
28
29 #include <Basics_OCCTVersion.hxx>
30
31 #include <stdio.h>
32
33 #include <BRepBuilderAPI_MakeFace.hxx>
34
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Face.hxx>
37
38 #include <Geom_Plane.hxx>
39 #include <Geom_Geometry.hxx>
40 #include <Geom_RectangularTrimmedSurface.hxx>
41
42 #include <gp_Pnt.hxx>
43 #include <gp_Dir.hxx>
44
45 #include <StdFail_NotDone.hxx>
46
47
48 //=======================================================================
49 //function : GetID
50 //purpose  :
51 //=======================================================================
52 const Standard_GUID& GEOMImpl_ArchimedeDriver::GetID()
53 {
54   static Standard_GUID aArchimedeDriver("FF1BBB59-5D14-4df2-980B-3A668264EA16");
55   return aArchimedeDriver;
56 }
57
58
59 //=======================================================================
60 //function : GEOMImpl_ArchimedeDriver
61 //purpose  :
62 //=======================================================================
63
64 GEOMImpl_ArchimedeDriver::GEOMImpl_ArchimedeDriver()
65 {
66 }
67
68 //=======================================================================
69 //function : Execute
70 //purpose  :
71 //=======================================================================
72 Standard_Integer GEOMImpl_ArchimedeDriver::Execute(TFunction_Logbook& log) const
73 {
74   if (Label().IsNull()) return 0;
75   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
76
77   Standard_Integer aType = aFunction->GetType();
78   if (aType != ARCHIMEDE_TYPE) return 0;
79
80   GEOMImpl_IArchimede IA (aFunction);
81
82   Handle(GEOM_Function) aShapeFunction = IA.GetBasicShape();
83   if (aShapeFunction.IsNull()) return 0;
84   TopoDS_Shape shape = aShapeFunction->GetValue();
85   if(shape.IsNull()) return 0;
86
87   Standard_Real aWaterDensity = IA.GetDensity();
88   Standard_Real aWeight = IA.GetWeight();
89   Standard_Real aMeshingDeflection = IA.GetDeflection();
90
91   double cste = -1;
92   if (aWaterDensity != 0.)
93     cste = aWeight/aWaterDensity;
94   else
95     return 0;
96
97   gp_Dir direct (0.0, 0.0, 1.0);
98   gp_Pnt PosPlan (0.0, 0.0, 0.0);
99   Geom_Plane PP (PosPlan, direct);
100   Handle(Geom_Plane) P = Handle(Geom_Plane)::DownCast(PP.Copy());
101
102   gp_Dir Zdirection (0.0, 0.0, 1.0);
103   VolumeSection VOL (shape, aMeshingDeflection);
104   VOL.SetPlane(P);
105   Handle(Geom_RectangularTrimmedSurface) SurfaceTrimmee;
106
107   if (Zdirection.IsEqual(direct, Precision::Angular()) == Standard_False) {
108     VOL.MakeRotation(direct);
109   }
110
111   VOL.CenterOfGravity();
112   SurfaceTrimmee = VOL.TrimSurf();
113   Standard_Real Cote = VOL.Archimede(cste, aMeshingDeflection);
114
115   if (Cote == -1) {
116     double Zmin,Zmax;
117     VOL.getZ(Zmin,Zmax);
118     double volume = VOL.CalculateVolume(Zmax) * aWaterDensity;
119
120     char msg[100] = "";
121     sprintf(msg, "shape sinks to the bottom : Weigth max = %.1f", volume);
122
123     StdFail_NotDone::Raise(msg);
124   }
125
126   SurfaceTrimmee = VOL.AjustePlan(SurfaceTrimmee,Cote,PosPlan);
127   if (Zdirection.IsEqual(direct,Precision::Angular()) == Standard_False) {
128     SurfaceTrimmee = VOL.InvMakeRotation(direct,SurfaceTrimmee);
129   }
130
131   Standard_Real u1,u2,v1,v2;
132   SurfaceTrimmee->Bounds(u1,u2,v1,v2);
133   TopoDS_Face tirant = BRepBuilderAPI_MakeFace(SurfaceTrimmee, u1, u2, v1, v2, Precision::Confusion());
134
135   if (tirant.IsNull()) {
136     StdFail_NotDone::Raise("Failed to build secant face");
137   }
138
139   aFunction->SetValue(tirant);
140
141   log.SetTouched(Label());
142
143   return 1;
144 }
145
146 //================================================================================
147 /*!
148  * \brief Returns a name of creation operation and names and values of creation parameters
149  */
150 //================================================================================
151
152 bool GEOMImpl_ArchimedeDriver::
153 GetCreationInformation(std::string&             theOperationName,
154                        std::vector<GEOM_Param>& theParams)
155 {
156   if (Label().IsNull()) return 0;
157   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
158
159   GEOMImpl_IArchimede IA (function);
160   Standard_Integer aType = function->GetType();
161   if (aType != ARCHIMEDE_TYPE) return 0;
162
163   theOperationName = "ARCHIMEDE";
164
165   AddParam( theParams, "Objects", IA.GetBasicShape() );
166   AddParam( theParams, "Weight", IA.GetWeight() );
167   AddParam( theParams, "Water Density", IA.GetDensity() );
168   AddParam( theParams, "Meshing Deflect.", IA.GetDeflection() );
169   
170   return true;
171 }
172
173 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_ArchimedeDriver,GEOM_BaseDriver);
174
175 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_ArchimedeDriver,GEOM_BaseDriver);