Salome HOME
a3145b83c590771bbf95f3beb98959ed969af51f
[modules/geom.git] / src / GEOMImpl / GEOMImpl_BoxDriver.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
23 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_BoxDriver.hxx>
26 #include <GEOMImpl_IBox.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
29
30 #include <BRepPrimAPI_MakeBox.hxx>
31 #include <BRep_Tool.hxx>
32 #include <gp_Pnt.hxx>
33 #include <TopoDS.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <TopoDS_Vertex.hxx>
36 #include <TopAbs.hxx>
37
38 #include <StdFail_NotDone.hxx>
39
40 #include <Precision.hxx>
41
42 //=======================================================================
43 //function : GetID
44 //purpose  :
45 //=======================================================================
46 const Standard_GUID& GEOMImpl_BoxDriver::GetID()
47 {
48   static Standard_GUID aBoxDriver("FF1BBB13-5D14-4df2-980B-3A668264EA16");
49   return aBoxDriver;
50 }
51
52
53 //=======================================================================
54 //function : GEOMImpl_BoxDriver
55 //purpose  :
56 //=======================================================================
57 GEOMImpl_BoxDriver::GEOMImpl_BoxDriver()
58 {
59 }
60
61 //=======================================================================
62 //function : Execute
63 //purpose  :
64 //=======================================================================
65 Standard_Integer GEOMImpl_BoxDriver::Execute(TFunction_Logbook& log) const
66 {
67   if (Label().IsNull()) return 0;
68   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
69
70   GEOMImpl_IBox aBI (aFunction);
71   Standard_Integer aType = aFunction->GetType();
72
73   TopoDS_Shape aShape;
74
75   if (aType == BOX_DX_DY_DZ) {
76     BRepPrimAPI_MakeBox MB (aBI.GetDX(), aBI.GetDY(), aBI.GetDZ());
77     MB.Build();
78
79     if (!MB.IsDone()) {
80       StdFail_NotDone::Raise("Box with the given dimensions can not be computed");
81     }
82     aShape = MB.Shape();
83   }
84   else if (aType == BOX_TWO_PNT) {
85     Handle(GEOM_Function) aRefPoint1 = aBI.GetRef1();
86     Handle(GEOM_Function) aRefPoint2 = aBI.GetRef2();
87     TopoDS_Shape aShape1 = aRefPoint1->GetValue();
88     TopoDS_Shape aShape2 = aRefPoint2->GetValue();
89     if (aShape1.ShapeType() == TopAbs_VERTEX &&
90         aShape2.ShapeType() == TopAbs_VERTEX) {
91       gp_Pnt P1 = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
92       gp_Pnt P2 = BRep_Tool::Pnt(TopoDS::Vertex(aShape2));
93
94       if (P1.X() - P2.X() < Precision::Confusion() || 
95           P1.Y() - P2.Y() < Precision::Confusion() || 
96           P1.Z() - P2.Z() < Precision::Confusion() ) {
97         StdFail_NotDone::Raise("Box can not be created, the points belong both to one of the OXY, OYZ or OZX planes");
98         return 0;
99       }
100
101       BRepPrimAPI_MakeBox MB (P1,P2);
102       MB.Build();
103
104       if (!MB.IsDone()) {
105         StdFail_NotDone::Raise("Box can not be computed from the given point");
106       }
107       aShape = MB.Shape();
108     }
109   }
110   else {
111   }
112
113   if (aShape.IsNull()) return 0;
114
115   aFunction->SetValue(aShape);
116
117   log.SetTouched(Label());
118
119   return 1;
120 }
121
122
123 //=======================================================================
124 //function :  GEOMImpl_BoxDriver_Type_
125 //purpose  :
126 //=======================================================================
127 Standard_EXPORT Handle_Standard_Type& GEOMImpl_BoxDriver_Type_()
128 {
129
130   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
131   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
132   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
133   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
134   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
135   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
136
137
138   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
139   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_BoxDriver",
140                                                          sizeof(GEOMImpl_BoxDriver),
141                                                          1,
142                                                          (Standard_Address)_Ancestors,
143                                                          (Standard_Address)NULL);
144
145   return _aType;
146 }
147
148 //=======================================================================
149 //function : DownCast
150 //purpose  :
151 //=======================================================================
152 const Handle(GEOMImpl_BoxDriver) Handle(GEOMImpl_BoxDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
153 {
154   Handle(GEOMImpl_BoxDriver) _anOtherObject;
155
156   if (!AnObject.IsNull()) {
157      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_BoxDriver))) {
158        _anOtherObject = Handle(GEOMImpl_BoxDriver)((Handle(GEOMImpl_BoxDriver)&)AnObject);
159      }
160   }
161
162   return _anOtherObject ;
163 }