Salome HOME
b3ebedfd835ff3da3390d89bf99dabd65b33d95d
[modules/geom.git] / src / GEOMImpl / GEOMImpl_BoxDriver.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 <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 #include<cmath>
43
44 //=======================================================================
45 //function : GetID
46 //purpose  :
47 //=======================================================================
48 const Standard_GUID& GEOMImpl_BoxDriver::GetID()
49 {
50   static Standard_GUID aBoxDriver("FF1BBB13-5D14-4df2-980B-3A668264EA16");
51   return aBoxDriver;
52 }
53
54
55 //=======================================================================
56 //function : GEOMImpl_BoxDriver
57 //purpose  :
58 //=======================================================================
59 GEOMImpl_BoxDriver::GEOMImpl_BoxDriver()
60 {
61 }
62
63 //=======================================================================
64 //function : Execute
65 //purpose  :
66 //=======================================================================
67 Standard_Integer GEOMImpl_BoxDriver::Execute(Handle(TFunction_Logbook)& log) const
68 {
69   if (Label().IsNull()) return 0;
70   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
71
72   GEOMImpl_IBox aBI (aFunction);
73   Standard_Integer aType = aFunction->GetType();
74
75   TopoDS_Shape aShape;
76
77   if (aType == BOX_DX_DY_DZ) {
78     BRepPrimAPI_MakeBox MB (aBI.GetDX(), aBI.GetDY(), aBI.GetDZ());
79     MB.Build();
80
81     if (!MB.IsDone()) {
82       StdFail_NotDone::Raise("Box with the given dimensions can not be computed");
83     }
84     aShape = MB.Shape();
85   }
86   else if (aType == BOX_TWO_PNT) {
87     Handle(GEOM_Function) aRefPoint1 = aBI.GetRef1();
88     Handle(GEOM_Function) aRefPoint2 = aBI.GetRef2();
89     TopoDS_Shape aShape1 = aRefPoint1->GetValue();
90     TopoDS_Shape aShape2 = aRefPoint2->GetValue();
91     if (aShape1.ShapeType() == TopAbs_VERTEX &&
92         aShape2.ShapeType() == TopAbs_VERTEX) {
93       gp_Pnt P1 = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
94       gp_Pnt P2 = BRep_Tool::Pnt(TopoDS::Vertex(aShape2));
95
96       if (std::abs(P1.X() - P2.X()) < Precision::Confusion() || 
97           std::abs(P1.Y() - P2.Y()) < Precision::Confusion() || 
98           std::abs(P1.Z() - P2.Z()) < Precision::Confusion() ) {
99         StdFail_NotDone::Raise("Box can not be created, the points belong both to one of the OXY, OYZ or OZX planes");
100         return 0;
101       }
102
103       BRepPrimAPI_MakeBox MB (P1,P2);
104       MB.Build();
105
106       if (!MB.IsDone()) {
107         StdFail_NotDone::Raise("Box can not be computed from the given point");
108       }
109       aShape = MB.Shape();
110     }
111   }
112   else {
113   }
114
115   if (aShape.IsNull()) return 0;
116
117   aFunction->SetValue(aShape);
118
119   log->SetTouched(Label());
120
121   return 1;
122 }
123
124 //================================================================================
125 /*!
126  * \brief Returns a name of creation operation and names and values of creation parameters
127  */
128 //================================================================================
129
130 bool GEOMImpl_BoxDriver::GetCreationInformation(std::string&             theOperationName,
131                                                 std::vector<GEOM_Param>& theParams)
132 {
133   if (Label().IsNull()) return 0;
134   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
135
136   GEOMImpl_IBox aBI (aFunction);
137   Standard_Integer aType = aFunction->GetType();
138
139   theOperationName = "BOX";
140
141   if (aType == BOX_DX_DY_DZ)
142   {
143     AddParam( theParams, "Dx", aBI.GetDX() );
144     AddParam( theParams, "Dy", aBI.GetDY() );
145     AddParam( theParams, "Dz", aBI.GetDZ() );
146   }
147   else if (aType == BOX_TWO_PNT) 
148   {
149     AddParam( theParams, "Point 1", aBI.GetRef1() );
150     AddParam( theParams, "Point 2", aBI.GetRef2() );
151   }
152   else {
153     return false;
154   }
155   
156   return true;
157 }
158
159 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_BoxDriver,GEOM_BaseDriver)