1 // Copyright (C) 2007-2016 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, 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 <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
118 if (!GetSolver()->ComputeFunction(aFunction)) {
119 SetErrorCode("Boolean driver failed");
123 catch (Standard_Failure) {
124 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
125 SetErrorCode(aFail->GetMessageString());
129 //Make a Python command
130 GEOM::TPythonDump pd (aFunction);
132 if (theOp == 1) pd << " = geompy.MakeCommon(";
133 else if (theOp == 2) pd << " = geompy.MakeCut(";
134 else if (theOp == 3) pd << " = geompy.MakeFuse(";
135 else if (theOp == 4) pd << " = geompy.MakeSection(";
137 pd << theShape1 << ", " << theShape2;
139 if (IsCheckSelfInte) {
149 //=============================================================================
153 //=============================================================================
154 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeFuse
155 (Handle(GEOM_Object) theShape1,
156 Handle(GEOM_Object) theShape2,
157 const bool IsCheckSelfInte,
158 const bool IsRmExtraEdges)
162 if (theShape1.IsNull() || theShape2.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);
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 Handle(GEOM_Function) aRef1 = theShape1->GetLastFunction();
179 Handle(GEOM_Function) aRef2 = theShape2->GetLastFunction();
181 if (aRef1.IsNull() || aRef2.IsNull()) return NULL;
183 aCI.SetShape1(aRef1);
184 aCI.SetShape2(aRef2);
185 aCI.SetCheckSelfIntersection(IsCheckSelfInte);
186 aCI.SetRmExtraEdges(IsRmExtraEdges);
188 //Compute the Boolean value
191 if (!GetSolver()->ComputeFunction(aFunction)) {
192 SetErrorCode("Boolean driver failed");
196 catch (Standard_Failure) {
197 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
198 SetErrorCode(aFail->GetMessageString());
202 //Make a Python command
203 GEOM::TPythonDump pd (aFunction);
205 pd << aBool << " = geompy.MakeFuse(";
206 pd << theShape1 << ", " << theShape2 << ", "
207 << IsCheckSelfInte << ", " << IsRmExtraEdges << ")";
213 //=============================================================================
217 //=============================================================================
218 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeFuseList
219 (const Handle(TColStd_HSequenceOfTransient)& theShapes,
220 const bool IsCheckSelfInte,
221 const bool IsRmExtraEdges)
225 if (theShapes.IsNull()) return NULL;
227 //Add a new Boolean object
228 Handle(GEOM_Object) aBool = GetEngine()->AddObject(GetDocID(), GEOM_BOOLEAN);
230 //Add a new Boolean function
231 Handle(GEOM_Function) aFunction =
232 aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_FUSE_LIST);
234 if (aFunction.IsNull()) return NULL;
236 //Check if the function is set correctly
237 if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
239 GEOMImpl_IBoolean aCI (aFunction);
241 TCollection_AsciiString aDescription;
242 Handle(TColStd_HSequenceOfTransient) aShapesSeq =
243 getShapeFunctions(theShapes, aDescription);
245 if (aShapesSeq.IsNull()) return NULL;
247 aCI.SetShapes(aShapesSeq);
248 aCI.SetCheckSelfIntersection(IsCheckSelfInte);
249 aCI.SetRmExtraEdges(IsRmExtraEdges);
251 //Compute the Boolean value
254 if (!GetSolver()->ComputeFunction(aFunction)) {
255 SetErrorCode("Boolean driver failed");
259 catch (Standard_Failure) {
260 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
261 SetErrorCode(aFail->GetMessageString());
265 //Make a Python command
266 GEOM::TPythonDump pd (aFunction);
268 pd << aBool << " = geompy.MakeFuseList([" << aDescription.ToCString() << "], "
269 << IsCheckSelfInte << ", " << IsRmExtraEdges << ")";
275 //=============================================================================
279 //=============================================================================
280 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeCommonList
281 (const Handle(TColStd_HSequenceOfTransient)& theShapes,
282 const Standard_Boolean IsCheckSelfInte)
286 if (theShapes.IsNull()) return NULL;
288 //Add a new Boolean object
289 Handle(GEOM_Object) aBool = GetEngine()->AddObject(GetDocID(), GEOM_BOOLEAN);
291 //Add a new Boolean function
292 Handle(GEOM_Function) aFunction =
293 aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_COMMON_LIST);
295 if (aFunction.IsNull()) return NULL;
297 //Check if the function is set correctly
298 if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
300 GEOMImpl_IBoolean aCI (aFunction);
302 TCollection_AsciiString aDescription;
303 Handle(TColStd_HSequenceOfTransient) aShapesSeq =
304 getShapeFunctions(theShapes, aDescription);
306 if (aShapesSeq.IsNull()) return NULL;
308 aCI.SetShapes(aShapesSeq);
309 aCI.SetCheckSelfIntersection(IsCheckSelfInte);
311 //Compute the Boolean value
314 if (!GetSolver()->ComputeFunction(aFunction)) {
315 SetErrorCode("Boolean driver failed");
319 catch (Standard_Failure) {
320 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
321 SetErrorCode(aFail->GetMessageString());
325 //Make a Python command
326 GEOM::TPythonDump pd (aFunction);
329 " = geompy.MakeCommonList([" << aDescription.ToCString() << "]";
331 if (IsCheckSelfInte) {
341 //=============================================================================
345 //=============================================================================
346 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeCutList
347 (Handle(GEOM_Object) theMainShape,
348 const Handle(TColStd_HSequenceOfTransient)& theShapes,
349 const Standard_Boolean IsCheckSelfInte)
353 if (theShapes.IsNull()) return NULL;
355 //Add a new Boolean object
356 Handle(GEOM_Object) aBool = GetEngine()->AddObject(GetDocID(), GEOM_BOOLEAN);
358 //Add a new Boolean function
359 Handle(GEOM_Function) aFunction =
360 aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_CUT_LIST);
362 if (aFunction.IsNull()) return NULL;
364 //Check if the function is set correctly
365 if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
367 GEOMImpl_IBoolean aCI (aFunction);
368 Handle(GEOM_Function) aMainRef = theMainShape->GetLastFunction();
370 if (aMainRef.IsNull()) return NULL;
372 TCollection_AsciiString aDescription;
373 Handle(TColStd_HSequenceOfTransient) aShapesSeq =
374 getShapeFunctions(theShapes, aDescription);
376 if (aShapesSeq.IsNull()) return NULL;
378 aCI.SetShape1(aMainRef);
379 aCI.SetShapes(aShapesSeq);
380 aCI.SetCheckSelfIntersection(IsCheckSelfInte);
382 //Compute the Boolean value
385 if (!GetSolver()->ComputeFunction(aFunction)) {
386 SetErrorCode("Boolean driver failed");
390 catch (Standard_Failure) {
391 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
392 SetErrorCode(aFail->GetMessageString());
396 //Make a Python command
397 GEOM::TPythonDump pd (aFunction);
399 pd << aBool << " = geompy.MakeCutList("
400 << theMainShape << ", [" << aDescription.ToCString() << "]";
402 if (IsCheckSelfInte) {
412 //=============================================================================
416 //=============================================================================
417 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakePartition
418 (const Handle(TColStd_HSequenceOfTransient)& theShapes,
419 const Handle(TColStd_HSequenceOfTransient)& theTools,
420 const Handle(TColStd_HSequenceOfTransient)& theKeepIns,
421 const Handle(TColStd_HSequenceOfTransient)& theRemoveIns,
422 const Standard_Integer theLimit,
423 const Standard_Boolean theRemoveWebs,
424 const Handle(TColStd_HArray1OfInteger)& theMaterials,
425 const Standard_Integer theKeepNonlimitShapes,
426 const Standard_Boolean thePerformSelfIntersections,
427 const Standard_Boolean IsCheckSelfInte)
431 //Add a new Partition object
432 Handle(GEOM_Object) aPartition = GetEngine()->AddObject(GetDocID(), GEOM_PARTITION);
434 //Add a new Partition function
435 Handle(GEOM_Function) aFunction;
436 if (thePerformSelfIntersections)
437 aFunction = aPartition->AddFunction(GEOMImpl_PartitionDriver::GetID(), PARTITION_PARTITION);
439 aFunction = aPartition->AddFunction(GEOMImpl_PartitionDriver::GetID(), PARTITION_NO_SELF_INTERSECTIONS);
440 if (aFunction.IsNull()) return NULL;
442 //Check if the function is set correctly
443 if (aFunction->GetDriverGUID() != GEOMImpl_PartitionDriver::GetID()) return NULL;
445 GEOMImpl_IPartition aCI (aFunction);
447 Handle(TColStd_HSequenceOfTransient) aShapesSeq;
448 Handle(TColStd_HSequenceOfTransient) aToolsSeq;
449 Handle(TColStd_HSequenceOfTransient) aKeepInsSeq;
450 Handle(TColStd_HSequenceOfTransient) aRemInsSeq;
451 TCollection_AsciiString aShapesDescr, aToolsDescr, aKeepInsDescr, aRemoveInsDescr;
454 aShapesSeq = getShapeFunctions(theShapes, aShapesDescr);
456 if (aShapesSeq.IsNull()) {
457 SetErrorCode("NULL shape for Partition");
462 aToolsSeq = getShapeFunctions(theTools, aToolsDescr);
464 if (aToolsSeq.IsNull()) {
465 SetErrorCode("NULL tool shape for Partition");
470 aKeepInsSeq = getShapeFunctions(theKeepIns, aKeepInsDescr);
472 if (aKeepInsSeq.IsNull()) {
473 SetErrorCode("NULL <keep inside> shape for Partition");
478 aRemInsSeq = getShapeFunctions(theRemoveIns, aRemoveInsDescr);
480 if (aRemInsSeq.IsNull()) {
481 SetErrorCode("NULL <remove inside> shape for Partition");
485 aCI.SetShapes(aShapesSeq);
486 aCI.SetTools(aToolsSeq);
487 aCI.SetKeepIns(aKeepInsSeq);
488 aCI.SetRemoveIns(aRemInsSeq);
491 aCI.SetLimit(theLimit);
492 aCI.SetKeepNonlimitShapes(theKeepNonlimitShapes);
493 aCI.SetCheckSelfIntersection(IsCheckSelfInte);
497 if (theMaterials.IsNull()) {
498 Handle(TColStd_HArray1OfInteger) aMaterials =
499 new TColStd_HArray1OfInteger (1, aShapesSeq->Length());
501 aCI.SetMaterials(aMaterials);
503 aCI.SetMaterials(theMaterials);
507 //Compute the Partition
510 if (!GetSolver()->ComputeFunction(aFunction)) {
511 SetErrorCode("Partition driver failed");
515 catch (Standard_Failure) {
516 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
517 SetErrorCode(aFail->GetMessageString());
521 //Make a Python command
522 GEOM::TPythonDump pd (aFunction);
523 if (thePerformSelfIntersections)
524 pd << aPartition << " = geompy.MakePartition([";
526 pd << aPartition << " = geompy.MakePartitionNonSelfIntersectedShape([";
529 pd << aShapesDescr.ToCString() << "], [" << aToolsDescr.ToCString() << "], [";
530 // Keep Ins, Remove Ins
531 pd << aKeepInsDescr.ToCString() << "], [" << aRemoveInsDescr.ToCString() << "], ";
532 // Limit, Remove Webs
533 pd << TopAbs_ShapeEnum(theLimit) << ", " << (int)theRemoveWebs << ", [";
535 if (!theMaterials.IsNull() && theMaterials->Length() > 0) {
536 int i = theMaterials->Lower();
537 pd << theMaterials->Value(i);
539 for (; i <= theMaterials->Upper(); i++) {
540 pd << ", " << theMaterials->Value(i);
543 pd << "], " << theKeepNonlimitShapes;
545 if (IsCheckSelfInte && !thePerformSelfIntersections) {
555 //=============================================================================
559 //=============================================================================
560 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeHalfPartition
561 (Handle(GEOM_Object) theShape, Handle(GEOM_Object) thePlane)
565 if (theShape.IsNull() || thePlane.IsNull()) return NULL;
567 //Add a new Boolean object
568 Handle(GEOM_Object) aPart = GetEngine()->AddObject(GetDocID(), GEOM_PARTITION);
570 //Add a new Partition function
571 Handle(GEOM_Function) aFunction =
572 aPart->AddFunction(GEOMImpl_PartitionDriver::GetID(), PARTITION_HALF);
573 if (aFunction.IsNull()) return NULL;
575 //Check if the function is set correctly
576 if (aFunction->GetDriverGUID() != GEOMImpl_PartitionDriver::GetID()) return NULL;
578 GEOMImpl_IPartition aCI (aFunction);
580 Handle(GEOM_Function) aRef1 = theShape->GetLastFunction();
581 Handle(GEOM_Function) aRef2 = thePlane->GetLastFunction();
583 if (aRef1.IsNull() || aRef2.IsNull()) return NULL;
588 //Compute the Partition value
591 if (!GetSolver()->ComputeFunction(aFunction)) {
592 SetErrorCode("Partition driver failed");
596 catch (Standard_Failure) {
597 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
598 SetErrorCode(aFail->GetMessageString());
602 //Make a Python command
603 GEOM::TPythonDump pd (aFunction);
604 pd << aPart << " = geompy.MakeHalfPartition("
605 << theShape << ", " << thePlane << ")";
611 //=============================================================================
615 //=============================================================================
616 Handle(TColStd_HSequenceOfTransient)
617 GEOMImpl_IBooleanOperations::getShapeFunctions
618 (const Handle(TColStd_HSequenceOfTransient)& theObjects,
619 TCollection_AsciiString &theDescription)
621 Handle(TColStd_HSequenceOfTransient) aResult =
622 new TColStd_HSequenceOfTransient;
623 Standard_Integer aNbObjects = theObjects->Length();
625 TCollection_AsciiString anEntry;
626 Handle(GEOM_Object) anObj;
627 Handle(GEOM_Function) aRefObj;
630 for (i = 1; i <= aNbObjects; i++) {
631 anObj = Handle(GEOM_Object)::DownCast(theObjects->Value(i));
632 aRefObj = anObj->GetLastFunction();
634 if (aRefObj.IsNull()) {
639 aResult->Append(aRefObj);
641 // For Python command
642 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
645 theDescription += ", ";
648 theDescription += anEntry;