Salome HOME
Update mail address
[modules/geom.git] / src / GEOMImpl / GEOMImpl_BooleanDriver.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/ or email : webmaster.salome@opencascade.com
19 //
20
21 #include <Standard_Stream.hxx>
22
23 #include <GEOMImpl_BooleanDriver.hxx>
24 #include <GEOMImpl_IBoolean.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
27
28 #include <BRepAlgo.hxx>
29 #include <BRepAlgoAPI_Common.hxx>
30 #include <BRepAlgoAPI_Cut.hxx>
31 #include <BRepAlgoAPI_Fuse.hxx>
32 #include <BRepAlgoAPI_Section.hxx>
33 #include <TopoDS_Shape.hxx>
34
35 #include <Standard_ConstructionError.hxx>
36 #include <StdFail_NotDone.hxx>
37
38 //=======================================================================
39 //function : GetID
40 //purpose  :
41 //=======================================================================
42 const Standard_GUID& GEOMImpl_BooleanDriver::GetID()
43 {
44   static Standard_GUID aBooleanDriver("FF1BBB21-5D14-4df2-980B-3A668264EA16");
45   return aBooleanDriver;
46 }
47
48
49 //=======================================================================
50 //function : GEOMImpl_BooleanDriver
51 //purpose  :
52 //=======================================================================
53 GEOMImpl_BooleanDriver::GEOMImpl_BooleanDriver()
54 {
55 }
56
57 //=======================================================================
58 //function : Execute
59 //purpose  :
60 //=======================================================================
61 Standard_Integer GEOMImpl_BooleanDriver::Execute(TFunction_Logbook& log) const
62 {
63   if (Label().IsNull()) return 0;
64   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
65
66   GEOMImpl_IBoolean aCI (aFunction);
67   Standard_Integer aType = aFunction->GetType();
68
69   TopoDS_Shape aShape;
70
71   Handle(GEOM_Function) aRefShape1 = aCI.GetShape1();
72   Handle(GEOM_Function) aRefShape2 = aCI.GetShape2();
73   TopoDS_Shape aShape1 = aRefShape1->GetValue();
74   TopoDS_Shape aShape2 = aRefShape2->GetValue();
75   if (!aShape1.IsNull() && !aShape2.IsNull()) {
76     if (aType == BOOLEAN_COMMON) {
77       BRepAlgoAPI_Common BO (aShape1, aShape2);
78 //      BO.Build();
79       if (!BO.IsDone()) {
80         StdFail_NotDone::Raise("Requested boolean operation can not be performed on the given shapes");
81       }
82       aShape = BO.Shape();
83     } else if (aType == BOOLEAN_CUT) {
84       BRepAlgoAPI_Cut BO (aShape1, aShape2);
85       if (!BO.IsDone()) {
86         StdFail_NotDone::Raise("Requested boolean operation can not be performed on the given shapes");
87       }
88       aShape = BO.Shape();
89     } else if (aType == BOOLEAN_FUSE) {
90       BRepAlgoAPI_Fuse BO (aShape1, aShape2);
91       if (!BO.IsDone()) {
92         StdFail_NotDone::Raise("Requested boolean operation can not be performed on the given shapes");
93       }
94       aShape = BO.Shape();
95     } else if (aType == BOOLEAN_SECTION) {
96       BRepAlgoAPI_Section BO (aShape1, aShape2);
97       if (!BO.IsDone()) {
98         StdFail_NotDone::Raise("Requested boolean operation can not be performed on the given shapes");
99       }
100       aShape = BO.Shape();
101     } else {
102     }
103   }
104
105   if (aShape.IsNull()) return 0;
106   if (!BRepAlgo::IsValid(aShape)) {
107     Standard_ConstructionError::Raise("Boolean aborted : non valid shape result");
108   }
109
110   aFunction->SetValue(aShape);
111
112   log.SetTouched(Label());
113
114   return 1;
115 }
116
117
118 //=======================================================================
119 //function :  GEOMImpl_BooleanDriver_Type_
120 //purpose  :
121 //=======================================================================
122 Standard_EXPORT Handle_Standard_Type& GEOMImpl_BooleanDriver_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_BooleanDriver",
135                                                          sizeof(GEOMImpl_BooleanDriver),
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_BooleanDriver) Handle(GEOMImpl_BooleanDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
148 {
149   Handle(GEOMImpl_BooleanDriver) _anOtherObject;
150
151   if (!AnObject.IsNull()) {
152      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_BooleanDriver))) {
153        _anOtherObject = Handle(GEOMImpl_BooleanDriver)((Handle(GEOMImpl_BooleanDriver)&)AnObject);
154      }
155   }
156
157   return _anOtherObject ;
158 }