1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include <Standard_Stream.hxx>
25 #include <GEOMImpl_IBooleanOperations.hxx>
27 #include <GEOM_Function.hxx>
28 #include <GEOM_PythonDump.hxx>
30 #include <GEOMImpl_Types.hxx>
32 #include <GEOMImpl_BooleanDriver.hxx>
33 #include <GEOMImpl_IBoolean.hxx>
35 #include <GEOMImpl_PartitionDriver.hxx>
36 #include <GEOMImpl_IPartition.hxx>
38 #include <Basics_OCCTVersion.hxx>
40 #include <TDF_Tool.hxx>
42 #include "utilities.h"
44 #include <Standard_Failure.hxx>
45 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
47 //=============================================================================
51 //=============================================================================
52 GEOMImpl_IBooleanOperations::GEOMImpl_IBooleanOperations (GEOM_Engine* theEngine, int theDocID)
53 : GEOM_IOperations(theEngine, theDocID)
55 MESSAGE("GEOMImpl_IBooleanOperations::GEOMImpl_IBooleanOperations");
58 //=============================================================================
62 //=============================================================================
63 GEOMImpl_IBooleanOperations::~GEOMImpl_IBooleanOperations()
65 MESSAGE("GEOMImpl_IBooleanOperations::~GEOMImpl_IBooleanOperations");
69 //=============================================================================
73 //=============================================================================
74 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeBoolean
75 (Handle(GEOM_Object) theShape1,
76 Handle(GEOM_Object) theShape2,
77 const Standard_Integer theOp,
78 const Standard_Boolean IsCheckSelfInte)
82 if (theShape1.IsNull() || theShape2.IsNull()) return NULL;
84 //Add a new Boolean object
85 Handle(GEOM_Object) aBool = GetEngine()->AddObject(GetDocID(), GEOM_BOOLEAN);
87 //Add a new Boolean function
88 Handle(GEOM_Function) aFunction;
90 aFunction = aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_COMMON);
91 } else if (theOp == 2) {
92 aFunction = aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_CUT);
93 } else if (theOp == 3) {
94 aFunction = aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_FUSE);
95 } else if (theOp == 4) {
96 aFunction = aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_SECTION);
99 if (aFunction.IsNull()) return NULL;
101 //Check if the function is set correctly
102 if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
104 GEOMImpl_IBoolean aCI (aFunction);
106 Handle(GEOM_Function) aRef1 = theShape1->GetLastFunction();
107 Handle(GEOM_Function) aRef2 = theShape2->GetLastFunction();
109 if (aRef1.IsNull() || aRef2.IsNull()) return NULL;
111 aCI.SetShape1(aRef1);
112 aCI.SetShape2(aRef2);
113 aCI.SetCheckSelfIntersection(IsCheckSelfInte);
115 //Compute the Boolean value
117 #if OCC_VERSION_LARGE > 0x06010000
120 if (!GetSolver()->ComputeFunction(aFunction)) {
121 SetErrorCode("Boolean driver failed");
125 catch (Standard_Failure) {
126 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
127 SetErrorCode(aFail->GetMessageString());
131 //Make a Python command
132 GEOM::TPythonDump pd (aFunction);
134 if (theOp == 1) pd << " = geompy.MakeCommon(";
135 else if (theOp == 2) pd << " = geompy.MakeCut(";
136 else if (theOp == 3) pd << " = geompy.MakeFuse(";
137 else if (theOp == 4) pd << " = geompy.MakeSection(";
139 pd << theShape1 << ", " << theShape2;
141 if (IsCheckSelfInte) {
151 //=============================================================================
155 //=============================================================================
156 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeFuseList
157 (const Handle(TColStd_HSequenceOfTransient)& theShapes,
158 const Standard_Boolean IsCheckSelfInte)
162 if (theShapes.IsNull()) return NULL;
164 //Add a new Boolean object
165 Handle(GEOM_Object) aBool = GetEngine()->AddObject(GetDocID(), GEOM_BOOLEAN);
167 //Add a new Boolean function
168 Handle(GEOM_Function) aFunction =
169 aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_FUSE_LIST);
171 if (aFunction.IsNull()) return NULL;
173 //Check if the function is set correctly
174 if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
176 GEOMImpl_IBoolean aCI (aFunction);
178 TCollection_AsciiString aDescription;
179 Handle(TColStd_HSequenceOfTransient) aShapesSeq =
180 getShapeFunctions(theShapes, aDescription);
182 if (aShapesSeq.IsNull()) return NULL;
184 aCI.SetShapes(aShapesSeq);
185 aCI.SetCheckSelfIntersection(IsCheckSelfInte);
187 //Compute the Boolean value
189 #if OCC_VERSION_LARGE > 0x06010000
192 if (!GetSolver()->ComputeFunction(aFunction)) {
193 SetErrorCode("Boolean driver failed");
197 catch (Standard_Failure) {
198 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
199 SetErrorCode(aFail->GetMessageString());
203 //Make a Python command
204 GEOM::TPythonDump pd (aFunction);
207 " = geompy.MakeFuseList([" << aDescription.ToCString() << "]";
209 if (IsCheckSelfInte) {
219 //=============================================================================
223 //=============================================================================
224 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeCommonList
225 (const Handle(TColStd_HSequenceOfTransient)& theShapes,
226 const Standard_Boolean IsCheckSelfInte)
230 if (theShapes.IsNull()) return NULL;
232 //Add a new Boolean object
233 Handle(GEOM_Object) aBool = GetEngine()->AddObject(GetDocID(), GEOM_BOOLEAN);
235 //Add a new Boolean function
236 Handle(GEOM_Function) aFunction =
237 aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_COMMON_LIST);
239 if (aFunction.IsNull()) return NULL;
241 //Check if the function is set correctly
242 if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
244 GEOMImpl_IBoolean aCI (aFunction);
246 TCollection_AsciiString aDescription;
247 Handle(TColStd_HSequenceOfTransient) aShapesSeq =
248 getShapeFunctions(theShapes, aDescription);
250 if (aShapesSeq.IsNull()) return NULL;
252 aCI.SetShapes(aShapesSeq);
253 aCI.SetCheckSelfIntersection(IsCheckSelfInte);
255 //Compute the Boolean value
257 #if OCC_VERSION_LARGE > 0x06010000
260 if (!GetSolver()->ComputeFunction(aFunction)) {
261 SetErrorCode("Boolean driver failed");
265 catch (Standard_Failure) {
266 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
267 SetErrorCode(aFail->GetMessageString());
271 //Make a Python command
272 GEOM::TPythonDump pd (aFunction);
275 " = geompy.MakeCommonList([" << aDescription.ToCString() << "]";
277 if (IsCheckSelfInte) {
287 //=============================================================================
291 //=============================================================================
292 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeCutList
293 (Handle(GEOM_Object) theMainShape,
294 const Handle(TColStd_HSequenceOfTransient)& theShapes,
295 const Standard_Boolean IsCheckSelfInte)
299 if (theShapes.IsNull()) return NULL;
301 //Add a new Boolean object
302 Handle(GEOM_Object) aBool = GetEngine()->AddObject(GetDocID(), GEOM_BOOLEAN);
304 //Add a new Boolean function
305 Handle(GEOM_Function) aFunction =
306 aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_CUT_LIST);
308 if (aFunction.IsNull()) return NULL;
310 //Check if the function is set correctly
311 if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
313 GEOMImpl_IBoolean aCI (aFunction);
314 Handle(GEOM_Function) aMainRef = theMainShape->GetLastFunction();
316 if (aMainRef.IsNull()) return NULL;
318 TCollection_AsciiString aDescription;
319 Handle(TColStd_HSequenceOfTransient) aShapesSeq =
320 getShapeFunctions(theShapes, aDescription);
322 if (aShapesSeq.IsNull()) return NULL;
324 aCI.SetShape1(aMainRef);
325 aCI.SetShapes(aShapesSeq);
326 aCI.SetCheckSelfIntersection(IsCheckSelfInte);
328 //Compute the Boolean value
330 #if OCC_VERSION_LARGE > 0x06010000
333 if (!GetSolver()->ComputeFunction(aFunction)) {
334 SetErrorCode("Boolean driver failed");
338 catch (Standard_Failure) {
339 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
340 SetErrorCode(aFail->GetMessageString());
344 //Make a Python command
345 GEOM::TPythonDump pd (aFunction);
347 pd << aBool << " = geompy.MakeCutList("
348 << theMainShape << ", [" << aDescription.ToCString() << "]";
350 if (IsCheckSelfInte) {
360 //=============================================================================
364 //=============================================================================
365 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakePartition
366 (const Handle(TColStd_HSequenceOfTransient)& theShapes,
367 const Handle(TColStd_HSequenceOfTransient)& theTools,
368 const Handle(TColStd_HSequenceOfTransient)& theKeepIns,
369 const Handle(TColStd_HSequenceOfTransient)& theRemoveIns,
370 const Standard_Integer theLimit,
371 const Standard_Boolean theRemoveWebs,
372 const Handle(TColStd_HArray1OfInteger)& theMaterials,
373 const Standard_Integer theKeepNonlimitShapes,
374 const Standard_Boolean thePerformSelfIntersections)
378 //Add a new Partition object
379 Handle(GEOM_Object) aPartition = GetEngine()->AddObject(GetDocID(), GEOM_PARTITION);
381 //Add a new Partition function
382 Handle(GEOM_Function) aFunction;
383 if (thePerformSelfIntersections)
384 aFunction = aPartition->AddFunction(GEOMImpl_PartitionDriver::GetID(), PARTITION_PARTITION);
386 aFunction = aPartition->AddFunction(GEOMImpl_PartitionDriver::GetID(), PARTITION_NO_SELF_INTERSECTIONS);
387 if (aFunction.IsNull()) return NULL;
389 //Check if the function is set correctly
390 if (aFunction->GetDriverGUID() != GEOMImpl_PartitionDriver::GetID()) return NULL;
392 GEOMImpl_IPartition aCI (aFunction);
394 Handle(TColStd_HSequenceOfTransient) aShapesSeq;
395 Handle(TColStd_HSequenceOfTransient) aToolsSeq;
396 Handle(TColStd_HSequenceOfTransient) aKeepInsSeq;
397 Handle(TColStd_HSequenceOfTransient) aRemInsSeq;
398 TCollection_AsciiString aShapesDescr, aToolsDescr, aKeepInsDescr, aRemoveInsDescr;
401 aShapesSeq = getShapeFunctions(theShapes, aShapesDescr);
403 if (aShapesSeq.IsNull()) {
404 SetErrorCode("NULL shape for Partition");
409 aToolsSeq = getShapeFunctions(theTools, aToolsDescr);
411 if (aToolsSeq.IsNull()) {
412 SetErrorCode("NULL tool shape for Partition");
417 aKeepInsSeq = getShapeFunctions(theKeepIns, aKeepInsDescr);
419 if (aKeepInsSeq.IsNull()) {
420 SetErrorCode("NULL <keep inside> shape for Partition");
425 aRemInsSeq = getShapeFunctions(theRemoveIns, aRemoveInsDescr);
427 if (aRemInsSeq.IsNull()) {
428 SetErrorCode("NULL <remove inside> shape for Partition");
432 aCI.SetShapes(aShapesSeq);
433 aCI.SetTools(aToolsSeq);
434 aCI.SetKeepIns(aKeepInsSeq);
435 aCI.SetRemoveIns(aRemInsSeq);
438 aCI.SetLimit(theLimit);
439 aCI.SetKeepNonlimitShapes(theKeepNonlimitShapes);
443 if (theMaterials.IsNull()) {
444 Handle(TColStd_HArray1OfInteger) aMaterials =
445 new TColStd_HArray1OfInteger (1, aShapesSeq->Length());
447 aCI.SetMaterials(aMaterials);
449 aCI.SetMaterials(theMaterials);
453 //Compute the Partition
455 #if OCC_VERSION_LARGE > 0x06010000
458 if (!GetSolver()->ComputeFunction(aFunction)) {
459 SetErrorCode("Partition driver failed");
463 catch (Standard_Failure) {
464 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
465 SetErrorCode(aFail->GetMessageString());
469 //Make a Python command
470 GEOM::TPythonDump pd (aFunction);
471 if (thePerformSelfIntersections)
472 pd << aPartition << " = geompy.MakePartition([";
474 pd << aPartition << " = geompy.MakePartitionNonSelfIntersectedShape([";
477 pd << aShapesDescr.ToCString() << "], [" << aToolsDescr.ToCString() << "], [";
478 // Keep Ins, Remove Ins
479 pd << aKeepInsDescr.ToCString() << "], [" << aRemoveInsDescr.ToCString() << "], ";
480 // Limit, Remove Webs
481 pd << TopAbs_ShapeEnum(theLimit) << ", " << (int)theRemoveWebs << ", [";
483 if (!theMaterials.IsNull() && theMaterials->Length() > 0) {
484 int i = theMaterials->Lower();
485 pd << theMaterials->Value(i);
487 for (; i <= theMaterials->Upper(); i++) {
488 pd << ", " << theMaterials->Value(i);
491 pd << "], " << theKeepNonlimitShapes <<")";
497 //=============================================================================
501 //=============================================================================
502 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeHalfPartition
503 (Handle(GEOM_Object) theShape, Handle(GEOM_Object) thePlane)
507 if (theShape.IsNull() || thePlane.IsNull()) return NULL;
509 //Add a new Boolean object
510 Handle(GEOM_Object) aPart = GetEngine()->AddObject(GetDocID(), GEOM_PARTITION);
512 //Add a new Partition function
513 Handle(GEOM_Function) aFunction =
514 aPart->AddFunction(GEOMImpl_PartitionDriver::GetID(), PARTITION_HALF);
515 if (aFunction.IsNull()) return NULL;
517 //Check if the function is set correctly
518 if (aFunction->GetDriverGUID() != GEOMImpl_PartitionDriver::GetID()) return NULL;
520 GEOMImpl_IPartition aCI (aFunction);
522 Handle(GEOM_Function) aRef1 = theShape->GetLastFunction();
523 Handle(GEOM_Function) aRef2 = thePlane->GetLastFunction();
525 if (aRef1.IsNull() || aRef2.IsNull()) return NULL;
530 //Compute the Partition value
532 #if OCC_VERSION_LARGE > 0x06010000
535 if (!GetSolver()->ComputeFunction(aFunction)) {
536 SetErrorCode("Partition driver failed");
540 catch (Standard_Failure) {
541 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
542 SetErrorCode(aFail->GetMessageString());
546 //Make a Python command
547 GEOM::TPythonDump(aFunction) << aPart << " = geompy.MakeHalfPartition("
548 << theShape << ", " << thePlane << ")";
554 //=============================================================================
558 //=============================================================================
559 Handle(TColStd_HSequenceOfTransient)
560 GEOMImpl_IBooleanOperations::getShapeFunctions
561 (const Handle(TColStd_HSequenceOfTransient)& theObjects,
562 TCollection_AsciiString &theDescription)
564 Handle(TColStd_HSequenceOfTransient) aResult =
565 new TColStd_HSequenceOfTransient;
566 Standard_Integer aNbObjects = theObjects->Length();
568 TCollection_AsciiString anEntry;
569 Handle(GEOM_Object) anObj;
570 Handle(GEOM_Function) aRefObj;
573 for (i = 1; i <= aNbObjects; i++) {
574 anObj = Handle(GEOM_Object)::DownCast(theObjects->Value(i));
575 aRefObj = anObj->GetLastFunction();
577 if (aRefObj.IsNull()) {
582 aResult->Append(aRefObj);
584 // For Python command
585 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
588 theDescription += ", ";
591 theDescription += anEntry;