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