Salome HOME
32599b5a382e2e04c55b67bf3712bf3229da994b
[modules/geom.git] / src / GEOMImpl / GEOMImpl_BooleanDriver.cxx
1
2 using namespace std;
3 #include "GEOMImpl_BooleanDriver.hxx"
4 #include "GEOMImpl_IBoolean.hxx"
5 #include "GEOMImpl_Types.hxx"
6 #include "GEOM_Function.hxx"
7
8 #include <BRepAlgo.hxx>
9 #include <BRepAlgoAPI_Common.hxx>
10 #include <BRepAlgoAPI_Cut.hxx>
11 #include <BRepAlgoAPI_Fuse.hxx>
12 #include <BRepAlgoAPI_Section.hxx>
13 #include <TopoDS_Shape.hxx>
14
15 #include <Standard_ConstructionError.hxx>
16 #include <StdFail_NotDone.hxx>
17
18 //=======================================================================
19 //function : GetID
20 //purpose  :
21 //=======================================================================
22 const Standard_GUID& GEOMImpl_BooleanDriver::GetID()
23 {
24   static Standard_GUID aBooleanDriver("FF1BBB21-5D14-4df2-980B-3A668264EA16");
25   return aBooleanDriver;
26 }
27
28
29 //=======================================================================
30 //function : GEOMImpl_BooleanDriver
31 //purpose  :
32 //=======================================================================
33 GEOMImpl_BooleanDriver::GEOMImpl_BooleanDriver()
34 {
35 }
36
37 //=======================================================================
38 //function : Execute
39 //purpose  :
40 //=======================================================================
41 Standard_Integer GEOMImpl_BooleanDriver::Execute(TFunction_Logbook& log) const
42 {
43   if (Label().IsNull()) return 0;
44   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
45
46   GEOMImpl_IBoolean aCI (aFunction);
47   Standard_Integer aType = aFunction->GetType();
48
49   TopoDS_Shape aShape;
50
51   Handle(GEOM_Function) aRefShape1 = aCI.GetShape1();
52   Handle(GEOM_Function) aRefShape2 = aCI.GetShape2();
53   TopoDS_Shape aShape1 = aRefShape1->GetValue();
54   TopoDS_Shape aShape2 = aRefShape2->GetValue();
55   if (!aShape1.IsNull() && !aShape2.IsNull()) {
56     if (aType == BOOLEAN_COMMON) {
57       BRepAlgoAPI_Common BO (aShape1, aShape2);
58 //      BO.Build();
59       if (!BO.IsDone()) {
60         StdFail_NotDone::Raise("Requested boolean operation can not be performed on the given shapes");
61       }
62       aShape = BO.Shape();
63     } else if (aType == BOOLEAN_CUT) {
64       BRepAlgoAPI_Cut BO (aShape1, aShape2);
65       if (!BO.IsDone()) {
66         StdFail_NotDone::Raise("Requested boolean operation can not be performed on the given shapes");
67       }
68       aShape = BO.Shape();
69     } else if (aType == BOOLEAN_FUSE) {
70       BRepAlgoAPI_Fuse BO (aShape1, aShape2);
71       if (!BO.IsDone()) {
72         StdFail_NotDone::Raise("Requested boolean operation can not be performed on the given shapes");
73       }
74       aShape = BO.Shape();
75     } else if (aType == BOOLEAN_SECTION) {
76       BRepAlgoAPI_Section BO (aShape1, aShape2);
77       if (!BO.IsDone()) {
78         StdFail_NotDone::Raise("Requested boolean operation can not be performed on the given shapes");
79       }
80       aShape = BO.Shape();
81     } else {
82     }
83   }
84
85   if (aShape.IsNull()) return 0;
86   if (!BRepAlgo::IsValid(aShape)) {
87     Standard_ConstructionError::Raise("Boolean aborted : non valid shape result");
88   }
89
90   aFunction->SetValue(aShape);
91
92   log.SetTouched(Label());
93
94   return 1;
95 }
96
97
98 //=======================================================================
99 //function :  GEOMImpl_BooleanDriver_Type_
100 //purpose  :
101 //=======================================================================
102 Standard_EXPORT Handle_Standard_Type& GEOMImpl_BooleanDriver_Type_()
103 {
104
105   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
106   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
107   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
108   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
109   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
110   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
111
112
113   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
114   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_BooleanDriver",
115                                                          sizeof(GEOMImpl_BooleanDriver),
116                                                          1,
117                                                          (Standard_Address)_Ancestors,
118                                                          (Standard_Address)NULL);
119
120   return _aType;
121 }
122
123 //=======================================================================
124 //function : DownCast
125 //purpose  :
126 //=======================================================================
127 const Handle(GEOMImpl_BooleanDriver) Handle(GEOMImpl_BooleanDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
128 {
129   Handle(GEOMImpl_BooleanDriver) _anOtherObject;
130
131   if (!AnObject.IsNull()) {
132      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_BooleanDriver))) {
133        _anOtherObject = Handle(GEOMImpl_BooleanDriver)((Handle(GEOMImpl_BooleanDriver)&)AnObject);
134      }
135   }
136
137   return _anOtherObject ;
138 }