Salome HOME
0f8612f7b985d70e89cfc752acd2059a979aade4
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ArchimedeDriver.cxx
1 //  Copyright (C) 2007-2010  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
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(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 : Weigth 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);
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 //function :  GEOMImpl_ArchimedeDriver_Type_
147 //purpose  :
148 //=======================================================================
149 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ArchimedeDriver_Type_()
150 {
151
152   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
153   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
154   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
155   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
156   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
157   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
158
159
160   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
161   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ArchimedeDriver",
162                                                          sizeof(GEOMImpl_ArchimedeDriver),
163                                                          1,
164                                                          (Standard_Address)_Ancestors,
165                                                          (Standard_Address)NULL);
166
167   return _aType;
168 }
169
170 //=======================================================================
171 //function : DownCast
172 //purpose  :
173 //=======================================================================
174
175 const Handle(GEOMImpl_ArchimedeDriver) Handle(GEOMImpl_ArchimedeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
176 {
177   Handle(GEOMImpl_ArchimedeDriver) _anOtherObject;
178
179   if (!AnObject.IsNull()) {
180      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ArchimedeDriver))) {
181        _anOtherObject = Handle(GEOMImpl_ArchimedeDriver)((Handle(GEOMImpl_ArchimedeDriver)&)AnObject);
182      }
183   }
184
185   return _anOtherObject ;
186 }
187
188