Salome HOME
5ddcd3889d1806e5b5bc7b2d27fffd8b0f90900b
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IBooleanOperations.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_IBooleanOperations.hxx>
26
27 #include <GEOM_Function.hxx>
28 #include <GEOM_PythonDump.hxx>
29
30 #include <GEOMImpl_Types.hxx>
31
32 #include <GEOMImpl_BooleanDriver.hxx>
33 #include <GEOMImpl_IBoolean.hxx>
34
35 #include <GEOMImpl_PartitionDriver.hxx>
36 #include <GEOMImpl_IPartition.hxx>
37
38 #include <TDF_Tool.hxx>
39
40 #include "utilities.h"
41
42 #include <Standard_Failure.hxx>
43 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
44
45 //=============================================================================
46 /*!
47  *   constructor:
48  */
49 //=============================================================================
50 GEOMImpl_IBooleanOperations::GEOMImpl_IBooleanOperations (GEOM_Engine* theEngine)
51 : GEOM_IOperations(theEngine)
52 {
53   MESSAGE("GEOMImpl_IBooleanOperations::GEOMImpl_IBooleanOperations");
54 }
55
56 //=============================================================================
57 /*!
58  *  destructor
59  */
60 //=============================================================================
61 GEOMImpl_IBooleanOperations::~GEOMImpl_IBooleanOperations()
62 {
63   MESSAGE("GEOMImpl_IBooleanOperations::~GEOMImpl_IBooleanOperations");
64 }
65
66
67 //=============================================================================
68 /*!
69  *  MakeBoolean
70  */
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)
77 {
78   SetErrorCode(KO);
79
80   if (theShape1.IsNull() || theShape2.IsNull()) return NULL;
81
82   //Add a new Boolean object
83   Handle(GEOM_Object) aBool = GetEngine()->AddObject(GEOM_BOOLEAN);
84
85   //Add a new Boolean function
86   Handle(GEOM_Function) aFunction;
87   if (theOp == 1) {
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);
95   } else {
96   }
97   if (aFunction.IsNull()) return NULL;
98
99   //Check if the function is set correctly
100   if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
101
102   GEOMImpl_IBoolean aCI (aFunction);
103
104   Handle(GEOM_Function) aRef1 = theShape1->GetLastFunction();
105   Handle(GEOM_Function) aRef2 = theShape2->GetLastFunction();
106
107   if (aRef1.IsNull() || aRef2.IsNull()) return NULL;
108
109   aCI.SetShape1(aRef1);
110   aCI.SetShape2(aRef2);
111   aCI.SetCheckSelfIntersection(IsCheckSelfInte);
112
113   //Compute the Boolean value
114   try {
115     OCC_CATCH_SIGNALS;
116     if (!GetSolver()->ComputeFunction(aFunction)) {
117       SetErrorCode("Boolean driver failed");
118       return NULL;
119     }
120   }
121   catch (Standard_Failure& aFail) {
122     SetErrorCode(aFail.GetMessageString());
123     return NULL;
124   }
125
126   //Make a Python command
127   GEOM::TPythonDump pd (aFunction);
128   pd << aBool;
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(";
133   else {}
134   pd << theShape1 << ", " << theShape2;
135
136   if (IsCheckSelfInte) {
137     pd << ", True";
138   }
139
140   pd << ")";
141
142   SetErrorCode(OK);
143   return aBool;
144 }
145
146 //=============================================================================
147 /*!
148  *  MakeFuse
149  */
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)
156 {
157   SetErrorCode(KO);
158
159   if (theShape1.IsNull() || theShape2.IsNull()) return NULL;
160
161   //Add a new Boolean object
162   Handle(GEOM_Object) aBool = GetEngine()->AddObject(GEOM_BOOLEAN);
163
164   //Add a new Boolean function
165   Handle(GEOM_Function) aFunction =
166     aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_FUSE);
167
168   if (aFunction.IsNull()) return NULL;
169
170   //Check if the function is set correctly
171   if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
172
173   GEOMImpl_IBoolean aCI (aFunction);
174
175   Handle(GEOM_Function) aRef1 = theShape1->GetLastFunction();
176   Handle(GEOM_Function) aRef2 = theShape2->GetLastFunction();
177
178   if (aRef1.IsNull() || aRef2.IsNull()) return NULL;
179
180   aCI.SetShape1(aRef1);
181   aCI.SetShape2(aRef2);
182   aCI.SetCheckSelfIntersection(IsCheckSelfInte);
183   aCI.SetRmExtraEdges(IsRmExtraEdges);
184
185   //Compute the Boolean value
186   try {
187     OCC_CATCH_SIGNALS;
188     if (!GetSolver()->ComputeFunction(aFunction)) {
189       SetErrorCode("Boolean driver failed");
190       return NULL;
191     }
192   }
193   catch (Standard_Failure& aFail) {
194     SetErrorCode(aFail.GetMessageString());
195     return NULL;
196   }
197
198   //Make a Python command
199   GEOM::TPythonDump pd (aFunction);
200
201   pd << aBool << " = geompy.MakeFuse(";
202   pd << theShape1 << ", " << theShape2 << ", "
203      << IsCheckSelfInte << ", " << IsRmExtraEdges << ")";
204
205   SetErrorCode(OK);
206   return aBool;
207 }
208
209 //=============================================================================
210 /*!
211  *  MakeFuseList
212  */
213 //=============================================================================
214 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeFuseList
215                   (const Handle(TColStd_HSequenceOfTransient)& theShapes,
216                    const bool                                  IsCheckSelfInte,
217                    const bool                                  IsRmExtraEdges)
218 {
219   SetErrorCode(KO);
220
221   if (theShapes.IsNull()) return NULL;
222
223   //Add a new Boolean object
224   Handle(GEOM_Object) aBool = GetEngine()->AddObject(GEOM_BOOLEAN);
225
226   //Add a new Boolean function
227   Handle(GEOM_Function) aFunction =
228     aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_FUSE_LIST);
229
230   if (aFunction.IsNull()) return NULL;
231
232   //Check if the function is set correctly
233   if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
234
235   GEOMImpl_IBoolean aCI (aFunction);
236
237   TCollection_AsciiString aDescription;
238   Handle(TColStd_HSequenceOfTransient) aShapesSeq =
239     getShapeFunctions(theShapes, aDescription);
240
241   if (aShapesSeq.IsNull()) return NULL;
242
243   aCI.SetShapes(aShapesSeq);
244   aCI.SetCheckSelfIntersection(IsCheckSelfInte);
245   aCI.SetRmExtraEdges(IsRmExtraEdges);
246
247   //Compute the Boolean value
248   try {
249     OCC_CATCH_SIGNALS;
250     if (!GetSolver()->ComputeFunction(aFunction)) {
251       SetErrorCode("Boolean driver failed");
252       return NULL;
253     }
254   }
255   catch (Standard_Failure& aFail) {
256     SetErrorCode(aFail.GetMessageString());
257     return NULL;
258   }
259
260   //Make a Python command
261   GEOM::TPythonDump pd (aFunction);
262
263   pd << aBool << " = geompy.MakeFuseList([" << aDescription.ToCString() << "], "
264      << IsCheckSelfInte << ", " << IsRmExtraEdges << ")";
265
266   SetErrorCode(OK);
267   return aBool;
268 }
269
270 //=============================================================================
271 /*!
272  *  MakeCommonList
273  */
274 //=============================================================================
275 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeCommonList
276                   (const Handle(TColStd_HSequenceOfTransient)& theShapes,
277                    const Standard_Boolean IsCheckSelfInte)
278 {
279   SetErrorCode(KO);
280
281   if (theShapes.IsNull()) return NULL;
282
283   //Add a new Boolean object
284   Handle(GEOM_Object) aBool = GetEngine()->AddObject(GEOM_BOOLEAN);
285
286   //Add a new Boolean function
287   Handle(GEOM_Function) aFunction =
288     aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_COMMON_LIST);
289
290   if (aFunction.IsNull()) return NULL;
291
292   //Check if the function is set correctly
293   if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
294
295   GEOMImpl_IBoolean aCI (aFunction);
296
297   TCollection_AsciiString aDescription;
298   Handle(TColStd_HSequenceOfTransient) aShapesSeq =
299     getShapeFunctions(theShapes, aDescription);
300
301   if (aShapesSeq.IsNull()) return NULL;
302
303   aCI.SetShapes(aShapesSeq);
304   aCI.SetCheckSelfIntersection(IsCheckSelfInte);
305
306   //Compute the Boolean value
307   try {
308     OCC_CATCH_SIGNALS;
309     if (!GetSolver()->ComputeFunction(aFunction)) {
310       SetErrorCode("Boolean driver failed");
311       return NULL;
312     }
313   }
314   catch (Standard_Failure& aFail) {
315     SetErrorCode(aFail.GetMessageString());
316     return NULL;
317   }
318
319   //Make a Python command
320   GEOM::TPythonDump pd (aFunction);
321
322   pd << aBool <<
323     " = geompy.MakeCommonList([" << aDescription.ToCString() << "]";
324
325   if (IsCheckSelfInte) {
326     pd << ", True";
327   }
328
329   pd << ")";
330
331   SetErrorCode(OK);
332   return aBool;
333 }
334
335 //=============================================================================
336 /*!
337  *  MakeCutList
338  */
339 //=============================================================================
340 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeCutList
341                   (Handle(GEOM_Object) theMainShape,
342                    const Handle(TColStd_HSequenceOfTransient)& theShapes,
343                    const Standard_Boolean IsCheckSelfInte)
344 {
345   SetErrorCode(KO);
346
347   if (theShapes.IsNull()) return NULL;
348
349   //Add a new Boolean object
350   Handle(GEOM_Object) aBool = GetEngine()->AddObject(GEOM_BOOLEAN);
351
352   //Add a new Boolean function
353   Handle(GEOM_Function) aFunction =
354     aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_CUT_LIST);
355
356   if (aFunction.IsNull()) return NULL;
357
358   //Check if the function is set correctly
359   if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
360
361   GEOMImpl_IBoolean aCI (aFunction);
362   Handle(GEOM_Function) aMainRef = theMainShape->GetLastFunction();
363
364   if (aMainRef.IsNull()) return NULL;
365
366   TCollection_AsciiString aDescription;
367   Handle(TColStd_HSequenceOfTransient) aShapesSeq =
368     getShapeFunctions(theShapes, aDescription);
369
370   if (aShapesSeq.IsNull()) return NULL;
371
372   aCI.SetShape1(aMainRef);
373   aCI.SetShapes(aShapesSeq);
374   aCI.SetCheckSelfIntersection(IsCheckSelfInte);
375
376   //Compute the Boolean value
377   try {
378     OCC_CATCH_SIGNALS;
379     if (!GetSolver()->ComputeFunction(aFunction)) {
380       SetErrorCode("Boolean driver failed");
381       return NULL;
382     }
383   }
384   catch (Standard_Failure& aFail) {
385     SetErrorCode(aFail.GetMessageString());
386     return NULL;
387   }
388
389   //Make a Python command
390   GEOM::TPythonDump pd (aFunction);
391
392   pd << aBool << " = geompy.MakeCutList("
393     << theMainShape << ", [" << aDescription.ToCString() << "]";
394
395   if (IsCheckSelfInte) {
396     pd << ", True";
397   }
398
399   pd << ")";
400
401   SetErrorCode(OK);
402   return aBool;
403 }
404
405 //=============================================================================
406 /*!
407  *  MakePartition
408  */
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)
421 {
422   SetErrorCode(KO);
423
424   //Add a new Partition object
425   Handle(GEOM_Object) aPartition = GetEngine()->AddObject(GEOM_PARTITION);
426
427   //Add a new Partition function
428   Handle(GEOM_Function) aFunction;
429   if (thePerformSelfIntersections)
430     aFunction = aPartition->AddFunction(GEOMImpl_PartitionDriver::GetID(), PARTITION_PARTITION);
431   else
432     aFunction = aPartition->AddFunction(GEOMImpl_PartitionDriver::GetID(), PARTITION_NO_SELF_INTERSECTIONS);
433   if (aFunction.IsNull()) return NULL;
434
435   //Check if the function is set correctly
436   if (aFunction->GetDriverGUID() != GEOMImpl_PartitionDriver::GetID()) return NULL;
437
438   GEOMImpl_IPartition aCI (aFunction);
439
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;
445
446   // Shapes
447   aShapesSeq = getShapeFunctions(theShapes, aShapesDescr);
448
449   if (aShapesSeq.IsNull()) {
450     SetErrorCode("NULL shape for Partition");
451     return NULL;
452   }
453
454   // Tools
455   aToolsSeq = getShapeFunctions(theTools, aToolsDescr);
456
457   if (aToolsSeq.IsNull()) {
458     SetErrorCode("NULL tool shape for Partition");
459     return NULL;
460   }
461
462   // Keep Inside
463   aKeepInsSeq = getShapeFunctions(theKeepIns, aKeepInsDescr);
464
465   if (aKeepInsSeq.IsNull()) {
466     SetErrorCode("NULL <keep inside> shape for Partition");
467     return NULL;
468   }
469
470   // Remove Inside
471   aRemInsSeq  = getShapeFunctions(theRemoveIns, aRemoveInsDescr);
472
473   if (aRemInsSeq.IsNull()) {
474     SetErrorCode("NULL <remove inside> shape for Partition");
475     return NULL;
476   }
477
478   aCI.SetShapes(aShapesSeq);
479   aCI.SetTools(aToolsSeq);
480   aCI.SetKeepIns(aKeepInsSeq);
481   aCI.SetRemoveIns(aRemInsSeq);
482
483   // Limit
484   aCI.SetLimit(theLimit);
485   aCI.SetKeepNonlimitShapes(theKeepNonlimitShapes);
486   aCI.SetCheckSelfIntersection(IsCheckSelfInte);
487
488   // Materials
489   if (theRemoveWebs) {
490     if (theMaterials.IsNull()) {
491       Handle(TColStd_HArray1OfInteger) aMaterials =
492         new TColStd_HArray1OfInteger (1, aShapesSeq->Length());
493       aMaterials->Init(0);
494       aCI.SetMaterials(aMaterials);
495     } else {
496       aCI.SetMaterials(theMaterials);
497     }
498   }
499
500   //Compute the Partition
501   try {
502     OCC_CATCH_SIGNALS;
503     if (!GetSolver()->ComputeFunction(aFunction)) {
504       SetErrorCode("Partition driver failed");
505       return NULL;
506     }
507   }
508   catch (Standard_Failure& aFail) {
509     SetErrorCode(aFail.GetMessageString());
510     return NULL;
511   }
512
513   //Make a Python command
514   GEOM::TPythonDump pd (aFunction);
515   if (thePerformSelfIntersections)
516     pd << aPartition << " = geompy.MakePartition([";
517   else
518     pd << aPartition << " = geompy.MakePartitionNonSelfIntersectedShape([";
519
520   // Shapes, Tools
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 << ", [";
526   // Materials
527   if (!theMaterials.IsNull() && theMaterials->Length() > 0) {
528     int i = theMaterials->Lower();
529     pd << theMaterials->Value(i);
530     i++;
531     for (; i <= theMaterials->Upper(); i++) {
532       pd << ", " << theMaterials->Value(i);
533     }
534   }
535   pd << "], " << theKeepNonlimitShapes;
536
537   if (IsCheckSelfInte && !thePerformSelfIntersections) {
538     pd << ", True";
539   }
540
541   pd << ")";
542
543   SetErrorCode(OK);
544   return aPartition;
545 }
546
547 //=============================================================================
548 /*!
549  *  MakeHalfPartition
550  */
551 //=============================================================================
552 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeHalfPartition
553        (Handle(GEOM_Object) theShape, Handle(GEOM_Object) thePlane)
554 {
555   SetErrorCode(KO);
556
557   if (theShape.IsNull() || thePlane.IsNull()) return NULL;
558
559   //Add a new Boolean object
560   Handle(GEOM_Object) aPart = GetEngine()->AddObject(GEOM_PARTITION);
561
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;
566
567   //Check if the function is set correctly
568   if (aFunction->GetDriverGUID() != GEOMImpl_PartitionDriver::GetID()) return NULL;
569
570   GEOMImpl_IPartition aCI (aFunction);
571
572   Handle(GEOM_Function) aRef1 = theShape->GetLastFunction();
573   Handle(GEOM_Function) aRef2 = thePlane->GetLastFunction();
574
575   if (aRef1.IsNull() || aRef2.IsNull()) return NULL;
576
577   aCI.SetShape(aRef1);
578   aCI.SetPlane(aRef2);
579
580   //Compute the Partition value
581   try {
582     OCC_CATCH_SIGNALS;
583     if (!GetSolver()->ComputeFunction(aFunction)) {
584       SetErrorCode("Partition driver failed");
585       return NULL;
586     }
587   }
588   catch (Standard_Failure& aFail) {
589     SetErrorCode(aFail.GetMessageString());
590     return NULL;
591   }
592
593   //Make a Python command
594   GEOM::TPythonDump pd (aFunction);
595   pd << aPart << " = geompy.MakeHalfPartition("
596      << theShape << ", " << thePlane << ")";
597
598   SetErrorCode(OK);
599   return aPart;
600 }
601
602 //=============================================================================
603 /*!
604  *  getShapeFunctions
605  */
606 //=============================================================================
607 Handle(TColStd_HSequenceOfTransient)
608   GEOMImpl_IBooleanOperations::getShapeFunctions
609                   (const Handle(TColStd_HSequenceOfTransient)& theObjects,
610                          TCollection_AsciiString &theDescription)
611 {
612   Handle(TColStd_HSequenceOfTransient) aResult =
613     new TColStd_HSequenceOfTransient;
614   Standard_Integer aNbObjects = theObjects->Length();
615   Standard_Integer i;
616   TCollection_AsciiString anEntry;
617   Handle(GEOM_Object) anObj;
618   Handle(GEOM_Function) aRefObj;
619
620   // Shapes
621   for (i = 1; i <= aNbObjects; i++) {
622     anObj = Handle(GEOM_Object)::DownCast(theObjects->Value(i));
623     aRefObj = anObj->GetLastFunction();
624
625     if (aRefObj.IsNull()) {
626       aResult.Nullify();
627       break;
628     }
629
630     aResult->Append(aRefObj);
631
632     // For Python command
633     TDF_Tool::Entry(anObj->GetEntry(), anEntry);
634
635     if (i > 1) {
636       theDescription += ", ";
637     }
638
639     theDescription += anEntry;
640   }
641
642   return aResult;
643 }