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 (Handle(GEOM_Object) theShape1,
75 Handle(GEOM_Object) theShape2,
76 Standard_Integer theOp)
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);
112 //Compute the Boolean value
114 #if OCC_VERSION_LARGE > 0x06010000
117 if (!GetSolver()->ComputeFunction(aFunction)) {
118 SetErrorCode("Boolean driver failed");
122 catch (Standard_Failure) {
123 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
124 SetErrorCode(aFail->GetMessageString());
128 //Make a Python command
129 GEOM::TPythonDump pd (aFunction);
131 if (theOp == 1) pd << " = geompy.MakeCommon(";
132 else if (theOp == 2) pd << " = geompy.MakeCut(";
133 else if (theOp == 3) pd << " = geompy.MakeFuse(";
134 else if (theOp == 4) pd << " = geompy.MakeSection(";
136 pd << theShape1 << ", " << theShape2 << ")";
142 //=============================================================================
146 //=============================================================================
147 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeFuseList
148 (const Handle(TColStd_HSequenceOfTransient)& theShapes)
152 if (theShapes.IsNull()) return NULL;
154 //Add a new Boolean object
155 Handle(GEOM_Object) aBool = GetEngine()->AddObject(GetDocID(), GEOM_BOOLEAN);
157 //Add a new Boolean function
158 Handle(GEOM_Function) aFunction =
159 aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_FUSE_LIST);
161 if (aFunction.IsNull()) return NULL;
163 //Check if the function is set correctly
164 if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
166 GEOMImpl_IBoolean aCI (aFunction);
168 TCollection_AsciiString aDescription;
169 Handle(TColStd_HSequenceOfTransient) aShapesSeq =
170 getShapeFunctions(theShapes, aDescription);
172 if (aShapesSeq.IsNull()) return NULL;
174 aCI.SetShapes(aShapesSeq);
176 //Compute the Boolean value
178 #if OCC_VERSION_LARGE > 0x06010000
181 if (!GetSolver()->ComputeFunction(aFunction)) {
182 SetErrorCode("Boolean driver failed");
186 catch (Standard_Failure) {
187 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
188 SetErrorCode(aFail->GetMessageString());
192 //Make a Python command
193 GEOM::TPythonDump(aFunction) << aBool <<
194 " = geompy.MakeFuseList([" << aDescription.ToCString() << "])";
200 //=============================================================================
204 //=============================================================================
205 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeCommonList
206 (const Handle(TColStd_HSequenceOfTransient)& theShapes)
210 if (theShapes.IsNull()) return NULL;
212 //Add a new Boolean object
213 Handle(GEOM_Object) aBool = GetEngine()->AddObject(GetDocID(), GEOM_BOOLEAN);
215 //Add a new Boolean function
216 Handle(GEOM_Function) aFunction =
217 aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_COMMON_LIST);
219 if (aFunction.IsNull()) return NULL;
221 //Check if the function is set correctly
222 if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
224 GEOMImpl_IBoolean aCI (aFunction);
226 TCollection_AsciiString aDescription;
227 Handle(TColStd_HSequenceOfTransient) aShapesSeq =
228 getShapeFunctions(theShapes, aDescription);
230 if (aShapesSeq.IsNull()) return NULL;
232 aCI.SetShapes(aShapesSeq);
234 //Compute the Boolean value
236 #if OCC_VERSION_LARGE > 0x06010000
239 if (!GetSolver()->ComputeFunction(aFunction)) {
240 SetErrorCode("Boolean driver failed");
244 catch (Standard_Failure) {
245 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
246 SetErrorCode(aFail->GetMessageString());
250 //Make a Python command
251 GEOM::TPythonDump(aFunction) << aBool <<
252 " = geompy.MakeCommonList([" << aDescription.ToCString() << "])";
258 //=============================================================================
262 //=============================================================================
263 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeCutList
264 (Handle(GEOM_Object) theMainShape,
265 const Handle(TColStd_HSequenceOfTransient)& theShapes)
269 if (theShapes.IsNull()) return NULL;
271 //Add a new Boolean object
272 Handle(GEOM_Object) aBool = GetEngine()->AddObject(GetDocID(), GEOM_BOOLEAN);
274 //Add a new Boolean function
275 Handle(GEOM_Function) aFunction =
276 aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_CUT_LIST);
278 if (aFunction.IsNull()) return NULL;
280 //Check if the function is set correctly
281 if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
283 GEOMImpl_IBoolean aCI (aFunction);
284 Handle(GEOM_Function) aMainRef = theMainShape->GetLastFunction();
286 if (aMainRef.IsNull()) return NULL;
288 TCollection_AsciiString aDescription;
289 Handle(TColStd_HSequenceOfTransient) aShapesSeq =
290 getShapeFunctions(theShapes, aDescription);
292 if (aShapesSeq.IsNull()) return NULL;
294 aCI.SetShape1(aMainRef);
295 aCI.SetShapes(aShapesSeq);
297 //Compute the Boolean value
299 #if OCC_VERSION_LARGE > 0x06010000
302 if (!GetSolver()->ComputeFunction(aFunction)) {
303 SetErrorCode("Boolean driver failed");
307 catch (Standard_Failure) {
308 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
309 SetErrorCode(aFail->GetMessageString());
313 //Make a Python command
314 GEOM::TPythonDump(aFunction) << aBool << " = geompy.MakeCutList("
315 << theMainShape << ", [" << aDescription.ToCString() << "])";
321 //=============================================================================
325 //=============================================================================
326 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakePartition
327 (const Handle(TColStd_HSequenceOfTransient)& theShapes,
328 const Handle(TColStd_HSequenceOfTransient)& theTools,
329 const Handle(TColStd_HSequenceOfTransient)& theKeepIns,
330 const Handle(TColStd_HSequenceOfTransient)& theRemoveIns,
331 const Standard_Integer theLimit,
332 const Standard_Boolean theRemoveWebs,
333 const Handle(TColStd_HArray1OfInteger)& theMaterials,
334 const Standard_Integer theKeepNonlimitShapes,
335 const Standard_Boolean thePerformSelfIntersections)
339 //Add a new Partition object
340 Handle(GEOM_Object) aPartition = GetEngine()->AddObject(GetDocID(), GEOM_PARTITION);
342 //Add a new Partition function
343 Handle(GEOM_Function) aFunction;
344 if (thePerformSelfIntersections)
345 aFunction = aPartition->AddFunction(GEOMImpl_PartitionDriver::GetID(), PARTITION_PARTITION);
347 aFunction = aPartition->AddFunction(GEOMImpl_PartitionDriver::GetID(), PARTITION_NO_SELF_INTERSECTIONS);
348 if (aFunction.IsNull()) return NULL;
350 //Check if the function is set correctly
351 if (aFunction->GetDriverGUID() != GEOMImpl_PartitionDriver::GetID()) return NULL;
353 GEOMImpl_IPartition aCI (aFunction);
355 Handle(TColStd_HSequenceOfTransient) aShapesSeq;
356 Handle(TColStd_HSequenceOfTransient) aToolsSeq;
357 Handle(TColStd_HSequenceOfTransient) aKeepInsSeq;
358 Handle(TColStd_HSequenceOfTransient) aRemInsSeq;
359 TCollection_AsciiString aShapesDescr, aToolsDescr, aKeepInsDescr, aRemoveInsDescr;
362 aShapesSeq = getShapeFunctions(theShapes, aShapesDescr);
364 if (aShapesSeq.IsNull()) {
365 SetErrorCode("NULL shape for Partition");
370 aToolsSeq = getShapeFunctions(theTools, aToolsDescr);
372 if (aToolsSeq.IsNull()) {
373 SetErrorCode("NULL tool shape for Partition");
378 aKeepInsSeq = getShapeFunctions(theKeepIns, aKeepInsDescr);
380 if (aKeepInsSeq.IsNull()) {
381 SetErrorCode("NULL <keep inside> shape for Partition");
386 aRemInsSeq = getShapeFunctions(theRemoveIns, aRemoveInsDescr);
388 if (aRemInsSeq.IsNull()) {
389 SetErrorCode("NULL <remove inside> shape for Partition");
393 aCI.SetShapes(aShapesSeq);
394 aCI.SetTools(aToolsSeq);
395 aCI.SetKeepIns(aKeepInsSeq);
396 aCI.SetRemoveIns(aRemInsSeq);
399 aCI.SetLimit(theLimit);
400 aCI.SetKeepNonlimitShapes(theKeepNonlimitShapes);
404 if (theMaterials.IsNull()) {
405 Handle(TColStd_HArray1OfInteger) aMaterials =
406 new TColStd_HArray1OfInteger (1, aShapesSeq->Length());
408 aCI.SetMaterials(aMaterials);
410 aCI.SetMaterials(theMaterials);
414 //Compute the Partition
416 #if OCC_VERSION_LARGE > 0x06010000
419 if (!GetSolver()->ComputeFunction(aFunction)) {
420 SetErrorCode("Partition driver failed");
424 catch (Standard_Failure) {
425 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
426 SetErrorCode(aFail->GetMessageString());
430 //Make a Python command
431 GEOM::TPythonDump pd (aFunction);
432 if (thePerformSelfIntersections)
433 pd << aPartition << " = geompy.MakePartition([";
435 pd << aPartition << " = geompy.MakePartitionNonSelfIntersectedShape([";
438 pd << aShapesDescr.ToCString() << "], [" << aToolsDescr.ToCString() << "], [";
439 // Keep Ins, Remove Ins
440 pd << aKeepInsDescr.ToCString() << "], [" << aRemoveInsDescr.ToCString() << "], ";
441 // Limit, Remove Webs
442 pd << TopAbs_ShapeEnum(theLimit) << ", " << (int)theRemoveWebs << ", [";
444 if (!theMaterials.IsNull() && theMaterials->Length() > 0) {
445 int i = theMaterials->Lower();
446 pd << theMaterials->Value(i);
448 for (; i <= theMaterials->Upper(); i++) {
449 pd << ", " << theMaterials->Value(i);
452 pd << "], " << theKeepNonlimitShapes <<")";
458 //=============================================================================
462 //=============================================================================
463 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeHalfPartition
464 (Handle(GEOM_Object) theShape, Handle(GEOM_Object) thePlane)
468 if (theShape.IsNull() || thePlane.IsNull()) return NULL;
470 //Add a new Boolean object
471 Handle(GEOM_Object) aPart = GetEngine()->AddObject(GetDocID(), GEOM_PARTITION);
473 //Add a new Partition function
474 Handle(GEOM_Function) aFunction =
475 aPart->AddFunction(GEOMImpl_PartitionDriver::GetID(), PARTITION_HALF);
476 if (aFunction.IsNull()) return NULL;
478 //Check if the function is set correctly
479 if (aFunction->GetDriverGUID() != GEOMImpl_PartitionDriver::GetID()) return NULL;
481 GEOMImpl_IPartition aCI (aFunction);
483 Handle(GEOM_Function) aRef1 = theShape->GetLastFunction();
484 Handle(GEOM_Function) aRef2 = thePlane->GetLastFunction();
486 if (aRef1.IsNull() || aRef2.IsNull()) return NULL;
491 //Compute the Partition value
493 #if OCC_VERSION_LARGE > 0x06010000
496 if (!GetSolver()->ComputeFunction(aFunction)) {
497 SetErrorCode("Partition driver failed");
501 catch (Standard_Failure) {
502 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
503 SetErrorCode(aFail->GetMessageString());
507 //Make a Python command
508 GEOM::TPythonDump(aFunction) << aPart << " = geompy.MakeHalfPartition("
509 << theShape << ", " << thePlane << ")";
515 //=============================================================================
519 //=============================================================================
520 Handle(TColStd_HSequenceOfTransient)
521 GEOMImpl_IBooleanOperations::getShapeFunctions
522 (const Handle(TColStd_HSequenceOfTransient)& theObjects,
523 TCollection_AsciiString &theDescription)
525 Handle(TColStd_HSequenceOfTransient) aResult =
526 new TColStd_HSequenceOfTransient;
527 Standard_Integer aNbObjects = theObjects->Length();
529 TCollection_AsciiString anEntry;
530 Handle(GEOM_Object) anObj;
531 Handle(GEOM_Function) aRefObj;
534 for (i = 1; i <= aNbObjects; i++) {
535 anObj = Handle(GEOM_Object)::DownCast(theObjects->Value(i));
536 aRefObj = anObj->GetLastFunction();
538 if (aRefObj.IsNull()) {
543 aResult->Append(aRefObj);
545 // For Python command
546 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
549 theDescription += ", ";
552 theDescription += anEntry;