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