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