1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <Standard_Stream.hxx>
22 #include <GEOMImpl_IBooleanOperations.hxx>
24 #include <GEOM_Function.hxx>
25 #include <GEOM_PythonDump.hxx>
27 #include <GEOMImpl_Types.hxx>
29 #include <GEOMImpl_BooleanDriver.hxx>
30 #include <GEOMImpl_IBoolean.hxx>
32 #include <GEOMImpl_PartitionDriver.hxx>
33 #include <GEOMImpl_IPartition.hxx>
35 #include <TDF_Tool.hxx>
37 #include "utilities.h"
39 #include <Standard_Failure.hxx>
40 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
42 //=============================================================================
46 //=============================================================================
47 GEOMImpl_IBooleanOperations::GEOMImpl_IBooleanOperations (GEOM_Engine* theEngine, int theDocID)
48 : GEOM_IOperations(theEngine, theDocID)
50 MESSAGE("GEOMImpl_IBooleanOperations::GEOMImpl_IBooleanOperations");
53 //=============================================================================
57 //=============================================================================
58 GEOMImpl_IBooleanOperations::~GEOMImpl_IBooleanOperations()
60 MESSAGE("GEOMImpl_IBooleanOperations::~GEOMImpl_IBooleanOperations");
64 //=============================================================================
68 //=============================================================================
69 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeBoolean (Handle(GEOM_Object) theShape1,
70 Handle(GEOM_Object) theShape2,
71 Standard_Integer theOp)
75 if (theShape1.IsNull() || theShape2.IsNull()) return NULL;
77 //Add a new Boolean object
78 Handle(GEOM_Object) aBool = GetEngine()->AddObject(GetDocID(), GEOM_BOOLEAN);
80 //Add a new Boolean function
81 Handle(GEOM_Function) aFunction;
83 aFunction = aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_COMMON);
84 } else if (theOp == 2) {
85 aFunction = aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_CUT);
86 } else if (theOp == 3) {
87 aFunction = aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_FUSE);
88 } else if (theOp == 4) {
89 aFunction = aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_SECTION);
92 if (aFunction.IsNull()) return NULL;
94 //Check if the function is set correctly
95 if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
97 GEOMImpl_IBoolean aCI (aFunction);
99 Handle(GEOM_Function) aRef1 = theShape1->GetLastFunction();
100 Handle(GEOM_Function) aRef2 = theShape2->GetLastFunction();
102 if (aRef1.IsNull() || aRef2.IsNull()) return NULL;
104 aCI.SetShape1(aRef1);
105 aCI.SetShape2(aRef2);
107 //Compute the Boolean value
109 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
112 if (!GetSolver()->ComputeFunction(aFunction)) {
113 SetErrorCode("Boolean driver failed");
117 catch (Standard_Failure) {
118 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
119 SetErrorCode(aFail->GetMessageString());
123 //Make a Python command
124 GEOM::TPythonDump pd (aFunction);
126 if (theOp == 1) pd << " = geompy.MakeCommon(";
127 else if (theOp == 2) pd << " = geompy.MakeCut(";
128 else if (theOp == 3) pd << " = geompy.MakeFuse(";
129 else if (theOp == 4) pd << " = geompy.MakeSection(";
131 pd << theShape1 << ", " << theShape2 << ")";
137 //=============================================================================
141 //=============================================================================
142 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakePartition
143 (const Handle(TColStd_HSequenceOfTransient)& theShapes,
144 const Handle(TColStd_HSequenceOfTransient)& theTools,
145 const Handle(TColStd_HSequenceOfTransient)& theKeepIns,
146 const Handle(TColStd_HSequenceOfTransient)& theRemoveIns,
147 const Standard_Integer theLimit,
148 const Standard_Boolean theRemoveWebs,
149 const Handle(TColStd_HArray1OfInteger)& theMaterials)
153 //Add a new Partition object
154 Handle(GEOM_Object) aPartition = GetEngine()->AddObject(GetDocID(), GEOM_PARTITION);
156 //Add a new Partition function
157 Handle(GEOM_Function) aFunction =
158 aPartition->AddFunction(GEOMImpl_PartitionDriver::GetID(), PARTITION_PARTITION);
159 if (aFunction.IsNull()) return NULL;
161 //Check if the function is set correctly
162 if (aFunction->GetDriverGUID() != GEOMImpl_PartitionDriver::GetID()) return NULL;
164 GEOMImpl_IPartition aCI (aFunction);
166 // int aLen = theShapes.size();
167 // aCI.SetLength(aLen);
169 Handle(TColStd_HSequenceOfTransient) aShapesSeq = new TColStd_HSequenceOfTransient;
170 Handle(TColStd_HSequenceOfTransient) aToolsSeq = new TColStd_HSequenceOfTransient;
171 Handle(TColStd_HSequenceOfTransient) aKeepInsSeq = new TColStd_HSequenceOfTransient;
172 Handle(TColStd_HSequenceOfTransient) aRemInsSeq = new TColStd_HSequenceOfTransient;
174 Standard_Integer ind, aLen;
175 TCollection_AsciiString anEntry;
176 TCollection_AsciiString aShapesDescr, aToolsDescr, aKeepInsDescr, aRemoveInsDescr;
179 aLen = theShapes->Length();
180 for (ind = 1; ind <= aLen; ind++) {
181 Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(theShapes->Value(ind));
182 Handle(GEOM_Function) aRefSh = anObj->GetLastFunction();
183 if (aRefSh.IsNull()) {
184 SetErrorCode("NULL shape for Partition");
187 aShapesSeq->Append(aRefSh);
189 // For Python command
190 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
191 if (ind > 1) aShapesDescr += ", ";
192 aShapesDescr += anEntry;
194 aCI.SetShapes(aShapesSeq);
197 aLen = theTools->Length();
198 for (ind = 1; ind <= aLen; ind++) {
199 Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(theTools->Value(ind));
200 Handle(GEOM_Function) aRefSh = anObj->GetLastFunction();
201 if (aRefSh.IsNull()) {
202 SetErrorCode("NULL tool shape for Partition");
205 aToolsSeq->Append(aRefSh);
207 // For Python command
208 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
209 if (ind > 1) aToolsDescr += ", ";
210 aToolsDescr += anEntry;
212 aCI.SetTools(aToolsSeq);
215 aLen = theKeepIns->Length();
216 for (ind = 1; ind <= aLen; ind++) {
217 Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(theKeepIns->Value(ind));
218 Handle(GEOM_Function) aRefSh = anObj->GetLastFunction();
219 if (aRefSh.IsNull()) {
220 SetErrorCode("NULL <keep inside> shape for Partition");
223 aKeepInsSeq->Append(aRefSh);
225 // For Python command
226 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
227 if (ind > 1) aKeepInsDescr += ", ";
228 aKeepInsDescr += anEntry;
230 aCI.SetKeepIns(aKeepInsSeq);
233 aLen = theRemoveIns->Length();
234 for (ind = 1; ind <= aLen; ind++) {
235 Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(theRemoveIns->Value(ind));
236 Handle(GEOM_Function) aRefSh = anObj->GetLastFunction();
237 if (aRefSh.IsNull()) {
238 SetErrorCode("NULL <remove inside> shape for Partition");
241 aRemInsSeq->Append(aRefSh);
243 // For Python command
244 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
245 if (ind > 1) aRemoveInsDescr += ", ";
246 aRemoveInsDescr += anEntry;
248 aCI.SetRemoveIns(aRemInsSeq);
251 aCI.SetLimit(theLimit);
255 if (theMaterials.IsNull()) {
256 Handle(TColStd_HArray1OfInteger) aMaterials =
257 new TColStd_HArray1OfInteger (1, aShapesSeq->Length());
259 aCI.SetMaterials(aMaterials);
261 aCI.SetMaterials(theMaterials);
265 //Compute the Partition
267 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
270 if (!GetSolver()->ComputeFunction(aFunction)) {
271 SetErrorCode("Partition driver failed");
275 catch (Standard_Failure) {
276 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
277 SetErrorCode(aFail->GetMessageString());
281 //Make a Python command
282 GEOM::TPythonDump pd (aFunction);
283 pd << aPartition << " = geompy.MakePartition([";
285 pd << aShapesDescr.ToCString() << "], [" << aToolsDescr.ToCString() << "], [";
286 // Keep Ins, Remove Ins
287 pd << aKeepInsDescr.ToCString() << "], [" << aRemoveInsDescr.ToCString() << "], ";
288 // Limit, Remove Webs
289 pd << theLimit << ", " << (int)theRemoveWebs << ", [";
291 if (!theMaterials.IsNull() && theMaterials->Length() > 0) {
292 int i = theMaterials->Lower();
293 pd << theMaterials->Value(i);
295 for (; i <= theMaterials->Upper(); i++) {
296 pd << ", " << theMaterials->Value(i);
305 //=============================================================================
309 //=============================================================================
310 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeHalfPartition
311 (Handle(GEOM_Object) theShape, Handle(GEOM_Object) thePlane)
315 if (theShape.IsNull() || thePlane.IsNull()) return NULL;
317 //Add a new Boolean object
318 Handle(GEOM_Object) aPart = GetEngine()->AddObject(GetDocID(), GEOM_BOOLEAN);
320 //Add a new Partition function
321 Handle(GEOM_Function) aFunction =
322 aPart->AddFunction(GEOMImpl_PartitionDriver::GetID(), PARTITION_HALF);
323 if (aFunction.IsNull()) return NULL;
325 //Check if the function is set correctly
326 if (aFunction->GetDriverGUID() != GEOMImpl_PartitionDriver::GetID()) return NULL;
328 GEOMImpl_IPartition aCI (aFunction);
330 Handle(GEOM_Function) aRef1 = theShape->GetLastFunction();
331 Handle(GEOM_Function) aRef2 = thePlane->GetLastFunction();
333 if (aRef1.IsNull() || aRef2.IsNull()) return NULL;
338 //Compute the Partition value
340 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
343 if (!GetSolver()->ComputeFunction(aFunction)) {
344 SetErrorCode("Partition driver failed");
348 catch (Standard_Failure) {
349 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
350 SetErrorCode(aFail->GetMessageString());
354 //Make a Python command
355 GEOM::TPythonDump(aFunction) << aPart << " = geompy.MakeHalfPartition("
356 << theShape << ", " << thePlane << ")";