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 <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, int theDocID)
51 : GEOM_IOperations(theEngine, theDocID)
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(GetDocID(), 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) {
122 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
123 SetErrorCode(aFail->GetMessageString());
127 //Make a Python command
128 GEOM::TPythonDump pd (aFunction);
130 if (theOp == 1) pd << " = geompy.MakeCommon(";
131 else if (theOp == 2) pd << " = geompy.MakeCut(";
132 else if (theOp == 3) pd << " = geompy.MakeFuse(";
133 else if (theOp == 4) pd << " = geompy.MakeSection(";
135 pd << theShape1 << ", " << theShape2;
137 if (IsCheckSelfInte) {
147 //=============================================================================
151 //=============================================================================
152 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeFuse
153 (Handle(GEOM_Object) theShape1,
154 Handle(GEOM_Object) theShape2,
155 const bool IsCheckSelfInte,
156 const bool IsRmExtraEdges)
160 if (theShape1.IsNull() || theShape2.IsNull()) return NULL;
162 //Add a new Boolean object
163 Handle(GEOM_Object) aBool = GetEngine()->AddObject(GetDocID(), GEOM_BOOLEAN);
165 //Add a new Boolean function
166 Handle(GEOM_Function) aFunction =
167 aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_FUSE);
169 if (aFunction.IsNull()) return NULL;
171 //Check if the function is set correctly
172 if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
174 GEOMImpl_IBoolean aCI (aFunction);
176 Handle(GEOM_Function) aRef1 = theShape1->GetLastFunction();
177 Handle(GEOM_Function) aRef2 = theShape2->GetLastFunction();
179 if (aRef1.IsNull() || aRef2.IsNull()) return NULL;
181 aCI.SetShape1(aRef1);
182 aCI.SetShape2(aRef2);
183 aCI.SetCheckSelfIntersection(IsCheckSelfInte);
184 aCI.SetRmExtraEdges(IsRmExtraEdges);
186 //Compute the Boolean value
189 if (!GetSolver()->ComputeFunction(aFunction)) {
190 SetErrorCode("Boolean driver failed");
194 catch (Standard_Failure) {
195 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
196 SetErrorCode(aFail->GetMessageString());
200 //Make a Python command
201 GEOM::TPythonDump pd (aFunction);
203 pd << aBool << " = geompy.MakeFuse(";
204 pd << theShape1 << ", " << theShape2 << ", "
205 << IsCheckSelfInte << ", " << IsRmExtraEdges << ")";
211 //=============================================================================
215 //=============================================================================
216 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeFuseList
217 (const Handle(TColStd_HSequenceOfTransient)& theShapes,
218 const bool IsCheckSelfInte,
219 const bool IsRmExtraEdges)
223 if (theShapes.IsNull()) return NULL;
225 //Add a new Boolean object
226 Handle(GEOM_Object) aBool = GetEngine()->AddObject(GetDocID(), GEOM_BOOLEAN);
228 //Add a new Boolean function
229 Handle(GEOM_Function) aFunction =
230 aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_FUSE_LIST);
232 if (aFunction.IsNull()) return NULL;
234 //Check if the function is set correctly
235 if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
237 GEOMImpl_IBoolean aCI (aFunction);
239 TCollection_AsciiString aDescription;
240 Handle(TColStd_HSequenceOfTransient) aShapesSeq =
241 getShapeFunctions(theShapes, aDescription);
243 if (aShapesSeq.IsNull()) return NULL;
245 aCI.SetShapes(aShapesSeq);
246 aCI.SetCheckSelfIntersection(IsCheckSelfInte);
247 aCI.SetRmExtraEdges(IsRmExtraEdges);
249 //Compute the Boolean value
252 if (!GetSolver()->ComputeFunction(aFunction)) {
253 SetErrorCode("Boolean driver failed");
257 catch (Standard_Failure) {
258 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
259 SetErrorCode(aFail->GetMessageString());
263 //Make a Python command
264 GEOM::TPythonDump pd (aFunction);
266 pd << aBool << " = geompy.MakeFuseList([" << aDescription.ToCString() << "], "
267 << IsCheckSelfInte << ", " << IsRmExtraEdges << ")";
273 //=============================================================================
277 //=============================================================================
278 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeCommonList
279 (const Handle(TColStd_HSequenceOfTransient)& theShapes,
280 const Standard_Boolean IsCheckSelfInte)
284 if (theShapes.IsNull()) return NULL;
286 //Add a new Boolean object
287 Handle(GEOM_Object) aBool = GetEngine()->AddObject(GetDocID(), GEOM_BOOLEAN);
289 //Add a new Boolean function
290 Handle(GEOM_Function) aFunction =
291 aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_COMMON_LIST);
293 if (aFunction.IsNull()) return NULL;
295 //Check if the function is set correctly
296 if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
298 GEOMImpl_IBoolean aCI (aFunction);
300 TCollection_AsciiString aDescription;
301 Handle(TColStd_HSequenceOfTransient) aShapesSeq =
302 getShapeFunctions(theShapes, aDescription);
304 if (aShapesSeq.IsNull()) return NULL;
306 aCI.SetShapes(aShapesSeq);
307 aCI.SetCheckSelfIntersection(IsCheckSelfInte);
309 //Compute the Boolean value
312 if (!GetSolver()->ComputeFunction(aFunction)) {
313 SetErrorCode("Boolean driver failed");
317 catch (Standard_Failure) {
318 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
319 SetErrorCode(aFail->GetMessageString());
323 //Make a Python command
324 GEOM::TPythonDump pd (aFunction);
327 " = geompy.MakeCommonList([" << aDescription.ToCString() << "]";
329 if (IsCheckSelfInte) {
339 //=============================================================================
343 //=============================================================================
344 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeCutList
345 (Handle(GEOM_Object) theMainShape,
346 const Handle(TColStd_HSequenceOfTransient)& theShapes,
347 const Standard_Boolean IsCheckSelfInte)
351 if (theShapes.IsNull()) return NULL;
353 //Add a new Boolean object
354 Handle(GEOM_Object) aBool = GetEngine()->AddObject(GetDocID(), GEOM_BOOLEAN);
356 //Add a new Boolean function
357 Handle(GEOM_Function) aFunction =
358 aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_CUT_LIST);
360 if (aFunction.IsNull()) return NULL;
362 //Check if the function is set correctly
363 if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
365 GEOMImpl_IBoolean aCI (aFunction);
366 Handle(GEOM_Function) aMainRef = theMainShape->GetLastFunction();
368 if (aMainRef.IsNull()) return NULL;
370 TCollection_AsciiString aDescription;
371 Handle(TColStd_HSequenceOfTransient) aShapesSeq =
372 getShapeFunctions(theShapes, aDescription);
374 if (aShapesSeq.IsNull()) return NULL;
376 aCI.SetShape1(aMainRef);
377 aCI.SetShapes(aShapesSeq);
378 aCI.SetCheckSelfIntersection(IsCheckSelfInte);
380 //Compute the Boolean value
383 if (!GetSolver()->ComputeFunction(aFunction)) {
384 SetErrorCode("Boolean driver failed");
388 catch (Standard_Failure) {
389 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
390 SetErrorCode(aFail->GetMessageString());
394 //Make a Python command
395 GEOM::TPythonDump pd (aFunction);
397 pd << aBool << " = geompy.MakeCutList("
398 << theMainShape << ", [" << aDescription.ToCString() << "]";
400 if (IsCheckSelfInte) {
410 //=============================================================================
414 //=============================================================================
415 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakePartition
416 (const Handle(TColStd_HSequenceOfTransient)& theShapes,
417 const Handle(TColStd_HSequenceOfTransient)& theTools,
418 const Handle(TColStd_HSequenceOfTransient)& theKeepIns,
419 const Handle(TColStd_HSequenceOfTransient)& theRemoveIns,
420 const Standard_Integer theLimit,
421 const Standard_Boolean theRemoveWebs,
422 const Handle(TColStd_HArray1OfInteger)& theMaterials,
423 const Standard_Integer theKeepNonlimitShapes,
424 const Standard_Boolean thePerformSelfIntersections,
425 const Standard_Boolean IsCheckSelfInte)
429 //Add a new Partition object
430 Handle(GEOM_Object) aPartition = GetEngine()->AddObject(GetDocID(), GEOM_PARTITION);
432 //Add a new Partition function
433 Handle(GEOM_Function) aFunction;
434 if (thePerformSelfIntersections)
435 aFunction = aPartition->AddFunction(GEOMImpl_PartitionDriver::GetID(), PARTITION_PARTITION);
437 aFunction = aPartition->AddFunction(GEOMImpl_PartitionDriver::GetID(), PARTITION_NO_SELF_INTERSECTIONS);
438 if (aFunction.IsNull()) return NULL;
440 //Check if the function is set correctly
441 if (aFunction->GetDriverGUID() != GEOMImpl_PartitionDriver::GetID()) return NULL;
443 GEOMImpl_IPartition aCI (aFunction);
445 Handle(TColStd_HSequenceOfTransient) aShapesSeq;
446 Handle(TColStd_HSequenceOfTransient) aToolsSeq;
447 Handle(TColStd_HSequenceOfTransient) aKeepInsSeq;
448 Handle(TColStd_HSequenceOfTransient) aRemInsSeq;
449 TCollection_AsciiString aShapesDescr, aToolsDescr, aKeepInsDescr, aRemoveInsDescr;
452 aShapesSeq = getShapeFunctions(theShapes, aShapesDescr);
454 if (aShapesSeq.IsNull()) {
455 SetErrorCode("NULL shape for Partition");
460 aToolsSeq = getShapeFunctions(theTools, aToolsDescr);
462 if (aToolsSeq.IsNull()) {
463 SetErrorCode("NULL tool shape for Partition");
468 aKeepInsSeq = getShapeFunctions(theKeepIns, aKeepInsDescr);
470 if (aKeepInsSeq.IsNull()) {
471 SetErrorCode("NULL <keep inside> shape for Partition");
476 aRemInsSeq = getShapeFunctions(theRemoveIns, aRemoveInsDescr);
478 if (aRemInsSeq.IsNull()) {
479 SetErrorCode("NULL <remove inside> shape for Partition");
483 aCI.SetShapes(aShapesSeq);
484 aCI.SetTools(aToolsSeq);
485 aCI.SetKeepIns(aKeepInsSeq);
486 aCI.SetRemoveIns(aRemInsSeq);
489 aCI.SetLimit(theLimit);
490 aCI.SetKeepNonlimitShapes(theKeepNonlimitShapes);
491 aCI.SetCheckSelfIntersection(IsCheckSelfInte);
495 if (theMaterials.IsNull()) {
496 Handle(TColStd_HArray1OfInteger) aMaterials =
497 new TColStd_HArray1OfInteger (1, aShapesSeq->Length());
499 aCI.SetMaterials(aMaterials);
501 aCI.SetMaterials(theMaterials);
505 //Compute the Partition
508 if (!GetSolver()->ComputeFunction(aFunction)) {
509 SetErrorCode("Partition driver failed");
513 catch (Standard_Failure) {
514 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
515 SetErrorCode(aFail->GetMessageString());
519 //Make a Python command
520 GEOM::TPythonDump pd (aFunction);
521 if (thePerformSelfIntersections)
522 pd << aPartition << " = geompy.MakePartition([";
524 pd << aPartition << " = geompy.MakePartitionNonSelfIntersectedShape([";
527 pd << aShapesDescr.ToCString() << "], [" << aToolsDescr.ToCString() << "], [";
528 // Keep Ins, Remove Ins
529 pd << aKeepInsDescr.ToCString() << "], [" << aRemoveInsDescr.ToCString() << "], ";
530 // Limit, Remove Webs
531 pd << TopAbs_ShapeEnum(theLimit) << ", " << (int)theRemoveWebs << ", [";
533 if (!theMaterials.IsNull() && theMaterials->Length() > 0) {
534 int i = theMaterials->Lower();
535 pd << theMaterials->Value(i);
537 for (; i <= theMaterials->Upper(); i++) {
538 pd << ", " << theMaterials->Value(i);
541 pd << "], " << theKeepNonlimitShapes;
543 if (IsCheckSelfInte && !thePerformSelfIntersections) {
553 //=============================================================================
557 //=============================================================================
558 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeHalfPartition
559 (Handle(GEOM_Object) theShape, Handle(GEOM_Object) thePlane)
563 if (theShape.IsNull() || thePlane.IsNull()) return NULL;
565 //Add a new Boolean object
566 Handle(GEOM_Object) aPart = GetEngine()->AddObject(GetDocID(), GEOM_PARTITION);
568 //Add a new Partition function
569 Handle(GEOM_Function) aFunction =
570 aPart->AddFunction(GEOMImpl_PartitionDriver::GetID(), PARTITION_HALF);
571 if (aFunction.IsNull()) return NULL;
573 //Check if the function is set correctly
574 if (aFunction->GetDriverGUID() != GEOMImpl_PartitionDriver::GetID()) return NULL;
576 GEOMImpl_IPartition aCI (aFunction);
578 Handle(GEOM_Function) aRef1 = theShape->GetLastFunction();
579 Handle(GEOM_Function) aRef2 = thePlane->GetLastFunction();
581 if (aRef1.IsNull() || aRef2.IsNull()) return NULL;
586 //Compute the Partition value
589 if (!GetSolver()->ComputeFunction(aFunction)) {
590 SetErrorCode("Partition driver failed");
594 catch (Standard_Failure) {
595 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
596 SetErrorCode(aFail->GetMessageString());
600 //Make a Python command
601 GEOM::TPythonDump pd (aFunction);
602 pd << aPart << " = geompy.MakeHalfPartition("
603 << theShape << ", " << thePlane << ")";
609 //=============================================================================
613 //=============================================================================
614 Handle(TColStd_HSequenceOfTransient)
615 GEOMImpl_IBooleanOperations::getShapeFunctions
616 (const Handle(TColStd_HSequenceOfTransient)& theObjects,
617 TCollection_AsciiString &theDescription)
619 Handle(TColStd_HSequenceOfTransient) aResult =
620 new TColStd_HSequenceOfTransient;
621 Standard_Integer aNbObjects = theObjects->Length();
623 TCollection_AsciiString anEntry;
624 Handle(GEOM_Object) anObj;
625 Handle(GEOM_Function) aRefObj;
628 for (i = 1; i <= aNbObjects; i++) {
629 anObj = Handle(GEOM_Object)::DownCast(theObjects->Value(i));
630 aRefObj = anObj->GetLastFunction();
632 if (aRefObj.IsNull()) {
637 aResult->Append(aRefObj);
639 // For Python command
640 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
643 theDescription += ", ";
646 theDescription += anEntry;