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