1 // Copyright (C) 2007-2024 CEA, EDF, 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, or (at your option) any later version.
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 <TDF_Tool.hxx>
40 #include "utilities.h"
42 #include <Standard_Failure.hxx>
43 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
45 //=============================================================================
49 //=============================================================================
50 GEOMImpl_IBooleanOperations::GEOMImpl_IBooleanOperations (GEOM_Engine* theEngine)
51 : GEOM_IOperations(theEngine)
53 MESSAGE("GEOMImpl_IBooleanOperations::GEOMImpl_IBooleanOperations");
56 //=============================================================================
60 //=============================================================================
61 GEOMImpl_IBooleanOperations::~GEOMImpl_IBooleanOperations()
63 MESSAGE("GEOMImpl_IBooleanOperations::~GEOMImpl_IBooleanOperations");
67 //=============================================================================
71 //=============================================================================
72 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeBoolean
73 (Handle(GEOM_Object) theShape1,
74 Handle(GEOM_Object) theShape2,
75 const Standard_Integer theOp,
76 const Standard_Boolean IsCheckSelfInte)
80 if (theShape1.IsNull() || theShape2.IsNull()) return NULL;
82 //Add a new Boolean object
83 Handle(GEOM_Object) aBool = GetEngine()->AddObject(GEOM_BOOLEAN);
85 //Add a new Boolean function
86 Handle(GEOM_Function) aFunction;
88 aFunction = aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_COMMON);
89 } else if (theOp == 2) {
90 aFunction = aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_CUT);
91 } else if (theOp == 3) {
92 aFunction = aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_FUSE);
93 } else if (theOp == 4) {
94 aFunction = aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_SECTION);
97 if (aFunction.IsNull()) return NULL;
99 //Check if the function is set correctly
100 if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
102 GEOMImpl_IBoolean aCI (aFunction);
104 Handle(GEOM_Function) aRef1 = theShape1->GetLastFunction();
105 Handle(GEOM_Function) aRef2 = theShape2->GetLastFunction();
107 if (aRef1.IsNull() || aRef2.IsNull()) return NULL;
109 aCI.SetShape1(aRef1);
110 aCI.SetShape2(aRef2);
111 aCI.SetCheckSelfIntersection(IsCheckSelfInte);
113 //Compute the Boolean value
116 if (!GetSolver()->ComputeFunction(aFunction)) {
117 SetErrorCode("Boolean driver failed");
121 catch (Standard_Failure& aFail) {
122 SetErrorCode(aFail.GetMessageString());
126 //Make a Python command
127 GEOM::TPythonDump pd (aFunction);
129 if (theOp == 1) pd << " = geompy.MakeCommon(";
130 else if (theOp == 2) pd << " = geompy.MakeCut(";
131 else if (theOp == 3) pd << " = geompy.MakeFuse(";
132 else if (theOp == 4) pd << " = geompy.MakeSection(";
134 pd << theShape1 << ", " << theShape2;
136 if (IsCheckSelfInte) {
146 //=============================================================================
150 //=============================================================================
151 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeFuse
152 (Handle(GEOM_Object) theShape1,
153 Handle(GEOM_Object) theShape2,
154 const bool IsCheckSelfInte,
155 const bool IsRmExtraEdges)
159 if (theShape1.IsNull() || theShape2.IsNull()) return NULL;
161 //Add a new Boolean object
162 Handle(GEOM_Object) aBool = GetEngine()->AddObject(GEOM_BOOLEAN);
164 //Add a new Boolean function
165 Handle(GEOM_Function) aFunction =
166 aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_FUSE);
168 if (aFunction.IsNull()) return NULL;
170 //Check if the function is set correctly
171 if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
173 GEOMImpl_IBoolean aCI (aFunction);
175 Handle(GEOM_Function) aRef1 = theShape1->GetLastFunction();
176 Handle(GEOM_Function) aRef2 = theShape2->GetLastFunction();
178 if (aRef1.IsNull() || aRef2.IsNull()) return NULL;
180 aCI.SetShape1(aRef1);
181 aCI.SetShape2(aRef2);
182 aCI.SetCheckSelfIntersection(IsCheckSelfInte);
183 aCI.SetRmExtraEdges(IsRmExtraEdges);
185 //Compute the Boolean value
188 if (!GetSolver()->ComputeFunction(aFunction)) {
189 SetErrorCode("Boolean driver failed");
193 catch (Standard_Failure& aFail) {
194 SetErrorCode(aFail.GetMessageString());
198 //Make a Python command
199 GEOM::TPythonDump pd (aFunction);
201 pd << aBool << " = geompy.MakeFuse(";
202 pd << theShape1 << ", " << theShape2 << ", "
203 << IsCheckSelfInte << ", " << IsRmExtraEdges << ")";
209 //=============================================================================
213 //=============================================================================
214 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeFuseList
215 (const Handle(TColStd_HSequenceOfTransient)& theShapes,
216 const bool IsCheckSelfInte,
217 const bool IsRmExtraEdges)
221 if (theShapes.IsNull()) return NULL;
223 //Add a new Boolean object
224 Handle(GEOM_Object) aBool = GetEngine()->AddObject(GEOM_BOOLEAN);
226 //Add a new Boolean function
227 Handle(GEOM_Function) aFunction =
228 aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_FUSE_LIST);
230 if (aFunction.IsNull()) return NULL;
232 //Check if the function is set correctly
233 if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
235 GEOMImpl_IBoolean aCI (aFunction);
237 TCollection_AsciiString aDescription;
238 Handle(TColStd_HSequenceOfTransient) aShapesSeq =
239 getShapeFunctions(theShapes, aDescription);
241 if (aShapesSeq.IsNull()) return NULL;
243 aCI.SetShapes(aShapesSeq);
244 aCI.SetCheckSelfIntersection(IsCheckSelfInte);
245 aCI.SetRmExtraEdges(IsRmExtraEdges);
247 //Compute the Boolean value
250 if (!GetSolver()->ComputeFunction(aFunction)) {
251 SetErrorCode("Boolean driver failed");
255 catch (Standard_Failure& aFail) {
256 SetErrorCode(aFail.GetMessageString());
260 //Make a Python command
261 GEOM::TPythonDump pd (aFunction);
263 pd << aBool << " = geompy.MakeFuseList([" << aDescription.ToCString() << "], "
264 << IsCheckSelfInte << ", " << IsRmExtraEdges << ")";
270 //=============================================================================
274 //=============================================================================
275 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeCommonList
276 (const Handle(TColStd_HSequenceOfTransient)& theShapes,
277 const Standard_Boolean IsCheckSelfInte)
281 if (theShapes.IsNull()) return NULL;
283 //Add a new Boolean object
284 Handle(GEOM_Object) aBool = GetEngine()->AddObject(GEOM_BOOLEAN);
286 //Add a new Boolean function
287 Handle(GEOM_Function) aFunction =
288 aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_COMMON_LIST);
290 if (aFunction.IsNull()) return NULL;
292 //Check if the function is set correctly
293 if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
295 GEOMImpl_IBoolean aCI (aFunction);
297 TCollection_AsciiString aDescription;
298 Handle(TColStd_HSequenceOfTransient) aShapesSeq =
299 getShapeFunctions(theShapes, aDescription);
301 if (aShapesSeq.IsNull()) return NULL;
303 aCI.SetShapes(aShapesSeq);
304 aCI.SetCheckSelfIntersection(IsCheckSelfInte);
306 //Compute the Boolean value
309 if (!GetSolver()->ComputeFunction(aFunction)) {
310 SetErrorCode("Boolean driver failed");
314 catch (Standard_Failure& aFail) {
315 SetErrorCode(aFail.GetMessageString());
319 //Make a Python command
320 GEOM::TPythonDump pd (aFunction);
323 " = geompy.MakeCommonList([" << aDescription.ToCString() << "]";
325 if (IsCheckSelfInte) {
335 //=============================================================================
339 //=============================================================================
340 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeCutList
341 (Handle(GEOM_Object) theMainShape,
342 const Handle(TColStd_HSequenceOfTransient)& theShapes,
343 const Standard_Boolean IsCheckSelfInte)
347 if (theShapes.IsNull()) return NULL;
349 //Add a new Boolean object
350 Handle(GEOM_Object) aBool = GetEngine()->AddObject(GEOM_BOOLEAN);
352 //Add a new Boolean function
353 Handle(GEOM_Function) aFunction =
354 aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_CUT_LIST);
356 if (aFunction.IsNull()) return NULL;
358 //Check if the function is set correctly
359 if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
361 GEOMImpl_IBoolean aCI (aFunction);
362 Handle(GEOM_Function) aMainRef = theMainShape->GetLastFunction();
364 if (aMainRef.IsNull()) return NULL;
366 TCollection_AsciiString aDescription;
367 Handle(TColStd_HSequenceOfTransient) aShapesSeq =
368 getShapeFunctions(theShapes, aDescription);
370 if (aShapesSeq.IsNull()) return NULL;
372 aCI.SetShape1(aMainRef);
373 aCI.SetShapes(aShapesSeq);
374 aCI.SetCheckSelfIntersection(IsCheckSelfInte);
376 //Compute the Boolean value
379 if (!GetSolver()->ComputeFunction(aFunction)) {
380 SetErrorCode("Boolean driver failed");
384 catch (Standard_Failure& aFail) {
385 SetErrorCode(aFail.GetMessageString());
389 //Make a Python command
390 GEOM::TPythonDump pd (aFunction);
392 pd << aBool << " = geompy.MakeCutList("
393 << theMainShape << ", [" << aDescription.ToCString() << "]";
395 if (IsCheckSelfInte) {
405 //=============================================================================
409 //=============================================================================
410 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakePartition
411 (const Handle(TColStd_HSequenceOfTransient)& theShapes,
412 const Handle(TColStd_HSequenceOfTransient)& theTools,
413 const Handle(TColStd_HSequenceOfTransient)& theKeepIns,
414 const Handle(TColStd_HSequenceOfTransient)& theRemoveIns,
415 const Standard_Integer theLimit,
416 const Standard_Boolean theRemoveWebs,
417 const Handle(TColStd_HArray1OfInteger)& theMaterials,
418 const Standard_Integer theKeepNonlimitShapes,
419 const Standard_Boolean thePerformSelfIntersections,
420 const Standard_Boolean IsCheckSelfInte)
424 //Add a new Partition object
425 Handle(GEOM_Object) aPartition = GetEngine()->AddObject(GEOM_PARTITION);
427 //Add a new Partition function
428 Handle(GEOM_Function) aFunction;
429 if (thePerformSelfIntersections)
430 aFunction = aPartition->AddFunction(GEOMImpl_PartitionDriver::GetID(), PARTITION_PARTITION);
432 aFunction = aPartition->AddFunction(GEOMImpl_PartitionDriver::GetID(), PARTITION_NO_SELF_INTERSECTIONS);
433 if (aFunction.IsNull()) return NULL;
435 //Check if the function is set correctly
436 if (aFunction->GetDriverGUID() != GEOMImpl_PartitionDriver::GetID()) return NULL;
438 GEOMImpl_IPartition aCI (aFunction);
440 Handle(TColStd_HSequenceOfTransient) aShapesSeq;
441 Handle(TColStd_HSequenceOfTransient) aToolsSeq;
442 Handle(TColStd_HSequenceOfTransient) aKeepInsSeq;
443 Handle(TColStd_HSequenceOfTransient) aRemInsSeq;
444 TCollection_AsciiString aShapesDescr, aToolsDescr, aKeepInsDescr, aRemoveInsDescr;
447 aShapesSeq = getShapeFunctions(theShapes, aShapesDescr);
449 if (aShapesSeq.IsNull()) {
450 SetErrorCode("NULL shape for Partition");
455 aToolsSeq = getShapeFunctions(theTools, aToolsDescr);
457 if (aToolsSeq.IsNull()) {
458 SetErrorCode("NULL tool shape for Partition");
463 aKeepInsSeq = getShapeFunctions(theKeepIns, aKeepInsDescr);
465 if (aKeepInsSeq.IsNull()) {
466 SetErrorCode("NULL <keep inside> shape for Partition");
471 aRemInsSeq = getShapeFunctions(theRemoveIns, aRemoveInsDescr);
473 if (aRemInsSeq.IsNull()) {
474 SetErrorCode("NULL <remove inside> shape for Partition");
478 aCI.SetShapes(aShapesSeq);
479 aCI.SetTools(aToolsSeq);
480 aCI.SetKeepIns(aKeepInsSeq);
481 aCI.SetRemoveIns(aRemInsSeq);
484 aCI.SetLimit(theLimit);
485 aCI.SetKeepNonlimitShapes(theKeepNonlimitShapes);
486 aCI.SetCheckSelfIntersection(IsCheckSelfInte);
490 if (theMaterials.IsNull()) {
491 Handle(TColStd_HArray1OfInteger) aMaterials =
492 new TColStd_HArray1OfInteger (1, aShapesSeq->Length());
494 aCI.SetMaterials(aMaterials);
496 aCI.SetMaterials(theMaterials);
500 //Compute the Partition
503 if (!GetSolver()->ComputeFunction(aFunction)) {
504 SetErrorCode("Partition driver failed");
508 catch (Standard_Failure& aFail) {
509 SetErrorCode(aFail.GetMessageString());
513 //Make a Python command
514 GEOM::TPythonDump pd (aFunction);
515 if (thePerformSelfIntersections)
516 pd << aPartition << " = geompy.MakePartition([";
518 pd << aPartition << " = geompy.MakePartitionNonSelfIntersectedShape([";
521 pd << aShapesDescr.ToCString() << "], [" << aToolsDescr.ToCString() << "], [";
522 // Keep Ins, Remove Ins
523 pd << aKeepInsDescr.ToCString() << "], [" << aRemoveInsDescr.ToCString() << "], ";
524 // Limit, Remove Webs
525 pd << TopAbs_ShapeEnum(theLimit) << ", " << (int)theRemoveWebs << ", [";
527 if (!theMaterials.IsNull() && theMaterials->Length() > 0) {
528 int i = theMaterials->Lower();
529 pd << theMaterials->Value(i);
531 for (; i <= theMaterials->Upper(); i++) {
532 pd << ", " << theMaterials->Value(i);
535 pd << "], " << theKeepNonlimitShapes;
537 if (IsCheckSelfInte && !thePerformSelfIntersections) {
547 //=============================================================================
551 //=============================================================================
552 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeHalfPartition
553 (Handle(GEOM_Object) theShape, Handle(GEOM_Object) thePlane)
557 if (theShape.IsNull() || thePlane.IsNull()) return NULL;
559 //Add a new Boolean object
560 Handle(GEOM_Object) aPart = GetEngine()->AddObject(GEOM_PARTITION);
562 //Add a new Partition function
563 Handle(GEOM_Function) aFunction =
564 aPart->AddFunction(GEOMImpl_PartitionDriver::GetID(), PARTITION_HALF);
565 if (aFunction.IsNull()) return NULL;
567 //Check if the function is set correctly
568 if (aFunction->GetDriverGUID() != GEOMImpl_PartitionDriver::GetID()) return NULL;
570 GEOMImpl_IPartition aCI (aFunction);
572 Handle(GEOM_Function) aRef1 = theShape->GetLastFunction();
573 Handle(GEOM_Function) aRef2 = thePlane->GetLastFunction();
575 if (aRef1.IsNull() || aRef2.IsNull()) return NULL;
580 //Compute the Partition value
583 if (!GetSolver()->ComputeFunction(aFunction)) {
584 SetErrorCode("Partition driver failed");
588 catch (Standard_Failure& aFail) {
589 SetErrorCode(aFail.GetMessageString());
593 //Make a Python command
594 GEOM::TPythonDump pd (aFunction);
595 pd << aPart << " = geompy.MakeHalfPartition("
596 << theShape << ", " << thePlane << ")";
602 //=============================================================================
606 //=============================================================================
607 Handle(TColStd_HSequenceOfTransient)
608 GEOMImpl_IBooleanOperations::getShapeFunctions
609 (const Handle(TColStd_HSequenceOfTransient)& theObjects,
610 TCollection_AsciiString &theDescription)
612 Handle(TColStd_HSequenceOfTransient) aResult =
613 new TColStd_HSequenceOfTransient;
614 Standard_Integer aNbObjects = theObjects->Length();
616 TCollection_AsciiString anEntry;
617 Handle(GEOM_Object) anObj;
618 Handle(GEOM_Function) aRefObj;
621 for (i = 1; i <= aNbObjects; i++) {
622 anObj = Handle(GEOM_Object)::DownCast(theObjects->Value(i));
623 aRefObj = anObj->GetLastFunction();
625 if (aRefObj.IsNull()) {
630 aResult->Append(aRefObj);
632 // For Python command
633 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
636 theDescription += ", ";
639 theDescription += anEntry;