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,
375 const Standard_Boolean IsCheckSelfInte)
379 //Add a new Partition object
380 Handle(GEOM_Object) aPartition = GetEngine()->AddObject(GetDocID(), GEOM_PARTITION);
382 //Add a new Partition function
383 Handle(GEOM_Function) aFunction;
384 if (thePerformSelfIntersections)
385 aFunction = aPartition->AddFunction(GEOMImpl_PartitionDriver::GetID(), PARTITION_PARTITION);
387 aFunction = aPartition->AddFunction(GEOMImpl_PartitionDriver::GetID(), PARTITION_NO_SELF_INTERSECTIONS);
388 if (aFunction.IsNull()) return NULL;
390 //Check if the function is set correctly
391 if (aFunction->GetDriverGUID() != GEOMImpl_PartitionDriver::GetID()) return NULL;
393 GEOMImpl_IPartition aCI (aFunction);
395 Handle(TColStd_HSequenceOfTransient) aShapesSeq;
396 Handle(TColStd_HSequenceOfTransient) aToolsSeq;
397 Handle(TColStd_HSequenceOfTransient) aKeepInsSeq;
398 Handle(TColStd_HSequenceOfTransient) aRemInsSeq;
399 TCollection_AsciiString aShapesDescr, aToolsDescr, aKeepInsDescr, aRemoveInsDescr;
402 aShapesSeq = getShapeFunctions(theShapes, aShapesDescr);
404 if (aShapesSeq.IsNull()) {
405 SetErrorCode("NULL shape for Partition");
410 aToolsSeq = getShapeFunctions(theTools, aToolsDescr);
412 if (aToolsSeq.IsNull()) {
413 SetErrorCode("NULL tool shape for Partition");
418 aKeepInsSeq = getShapeFunctions(theKeepIns, aKeepInsDescr);
420 if (aKeepInsSeq.IsNull()) {
421 SetErrorCode("NULL <keep inside> shape for Partition");
426 aRemInsSeq = getShapeFunctions(theRemoveIns, aRemoveInsDescr);
428 if (aRemInsSeq.IsNull()) {
429 SetErrorCode("NULL <remove inside> shape for Partition");
433 aCI.SetShapes(aShapesSeq);
434 aCI.SetTools(aToolsSeq);
435 aCI.SetKeepIns(aKeepInsSeq);
436 aCI.SetRemoveIns(aRemInsSeq);
439 aCI.SetLimit(theLimit);
440 aCI.SetKeepNonlimitShapes(theKeepNonlimitShapes);
441 aCI.SetCheckSelfIntersection(IsCheckSelfInte);
445 if (theMaterials.IsNull()) {
446 Handle(TColStd_HArray1OfInteger) aMaterials =
447 new TColStd_HArray1OfInteger (1, aShapesSeq->Length());
449 aCI.SetMaterials(aMaterials);
451 aCI.SetMaterials(theMaterials);
455 //Compute the Partition
457 #if OCC_VERSION_LARGE > 0x06010000
460 if (!GetSolver()->ComputeFunction(aFunction)) {
461 SetErrorCode("Partition driver failed");
465 catch (Standard_Failure) {
466 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
467 SetErrorCode(aFail->GetMessageString());
471 //Make a Python command
472 GEOM::TPythonDump pd (aFunction);
473 if (thePerformSelfIntersections)
474 pd << aPartition << " = geompy.MakePartition([";
476 pd << aPartition << " = geompy.MakePartitionNonSelfIntersectedShape([";
479 pd << aShapesDescr.ToCString() << "], [" << aToolsDescr.ToCString() << "], [";
480 // Keep Ins, Remove Ins
481 pd << aKeepInsDescr.ToCString() << "], [" << aRemoveInsDescr.ToCString() << "], ";
482 // Limit, Remove Webs
483 pd << TopAbs_ShapeEnum(theLimit) << ", " << (int)theRemoveWebs << ", [";
485 if (!theMaterials.IsNull() && theMaterials->Length() > 0) {
486 int i = theMaterials->Lower();
487 pd << theMaterials->Value(i);
489 for (; i <= theMaterials->Upper(); i++) {
490 pd << ", " << theMaterials->Value(i);
493 pd << "], " << theKeepNonlimitShapes;
495 if (IsCheckSelfInte) {
505 //=============================================================================
509 //=============================================================================
510 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeHalfPartition
511 (Handle(GEOM_Object) theShape, Handle(GEOM_Object) thePlane,
512 const Standard_Boolean IsCheckSelfInte)
516 if (theShape.IsNull() || thePlane.IsNull()) return NULL;
518 //Add a new Boolean object
519 Handle(GEOM_Object) aPart = GetEngine()->AddObject(GetDocID(), GEOM_PARTITION);
521 //Add a new Partition function
522 Handle(GEOM_Function) aFunction =
523 aPart->AddFunction(GEOMImpl_PartitionDriver::GetID(), PARTITION_HALF);
524 if (aFunction.IsNull()) return NULL;
526 //Check if the function is set correctly
527 if (aFunction->GetDriverGUID() != GEOMImpl_PartitionDriver::GetID()) return NULL;
529 GEOMImpl_IPartition aCI (aFunction);
531 Handle(GEOM_Function) aRef1 = theShape->GetLastFunction();
532 Handle(GEOM_Function) aRef2 = thePlane->GetLastFunction();
534 if (aRef1.IsNull() || aRef2.IsNull()) return NULL;
538 aCI.SetCheckSelfIntersection(IsCheckSelfInte);
540 //Compute the Partition value
542 #if OCC_VERSION_LARGE > 0x06010000
545 if (!GetSolver()->ComputeFunction(aFunction)) {
546 SetErrorCode("Partition driver failed");
550 catch (Standard_Failure) {
551 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
552 SetErrorCode(aFail->GetMessageString());
556 //Make a Python command
557 GEOM::TPythonDump pd (aFunction);
558 pd << aPart << " = geompy.MakeHalfPartition("
559 << theShape << ", " << thePlane;
561 if (IsCheckSelfInte) {
571 //=============================================================================
575 //=============================================================================
576 Handle(TColStd_HSequenceOfTransient)
577 GEOMImpl_IBooleanOperations::getShapeFunctions
578 (const Handle(TColStd_HSequenceOfTransient)& theObjects,
579 TCollection_AsciiString &theDescription)
581 Handle(TColStd_HSequenceOfTransient) aResult =
582 new TColStd_HSequenceOfTransient;
583 Standard_Integer aNbObjects = theObjects->Length();
585 TCollection_AsciiString anEntry;
586 Handle(GEOM_Object) anObj;
587 Handle(GEOM_Function) aRefObj;
590 for (i = 1; i <= aNbObjects; i++) {
591 anObj = Handle(GEOM_Object)::DownCast(theObjects->Value(i));
592 aRefObj = anObj->GetLastFunction();
594 if (aRefObj.IsNull()) {
599 aResult->Append(aRefObj);
601 // For Python command
602 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
605 theDescription += ", ";
608 theDescription += anEntry;