Salome HOME
Remove unnecessary includes
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IHealingOperations.cxx
1 // Copyright (C) 2007-2021  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 #ifdef WIN32
24 #pragma warning( disable:4786 )
25 #endif
26
27 #include <Standard_Version.hxx>
28
29 #include <GEOMImpl_IHealingOperations.hxx>
30 #include <GEOM_PythonDump.hxx>
31 #include <GEOMImpl_HealingDriver.hxx>
32 #include <GEOMImpl_Types.hxx>
33 #include <GEOMImpl_IHealing.hxx>
34 #include <GEOMImpl_IVector.hxx>
35 #include <GEOMImpl_VectorDriver.hxx>
36 #include <GEOMImpl_CopyDriver.hxx>
37 #include <ShHealOper_ModifStats.hxx>
38 #include <ShHealOper_ShapeProcess.hxx>
39
40 #include <utilities.h>
41 #include <Utils_ExceptHandlers.hxx>
42
43 #include <BRep_Builder.hxx>
44 #include <ShapeAnalysis_FreeBounds.hxx>
45 #include <TColStd_HArray1OfExtendedString.hxx>
46 #include <TColStd_HSequenceOfTransient.hxx>
47 #include <TCollection_AsciiString.hxx>
48 #include <TopExp_Explorer.hxx>
49 #include <TopTools_SequenceOfShape.hxx>
50 #include <TopoDS_Compound.hxx>
51
52 #include <Standard_Failure.hxx>
53 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
54
55 //=============================================================================
56 /*!
57  *   constructor:
58  */
59 //=============================================================================
60 GEOMImpl_IHealingOperations::GEOMImpl_IHealingOperations (GEOM_Engine* theEngine)
61 : GEOM_IOperations(theEngine)
62 {
63   myModifStats = new ShHealOper_ModifStats;
64   MESSAGE("GEOMImpl_IHealingOperations::GEOMImpl_IHealingOperations");
65 }
66
67 //=============================================================================
68 /*!
69  *  destructor
70  */
71 //=============================================================================
72 GEOMImpl_IHealingOperations::~GEOMImpl_IHealingOperations()
73 {
74   delete myModifStats;
75   MESSAGE("GEOMImpl_IHealingOperations::~GEOMImpl_IHealingOperations");
76 }
77
78
79 //=============================================================================
80 /*!
81  *  ShapeProcess
82  */
83 //=============================================================================
84 Handle(GEOM_Object) GEOMImpl_IHealingOperations::ShapeProcess (Handle(GEOM_Object) theObject,
85                                   const Handle(TColStd_HArray1OfExtendedString)& theOperators,
86                                   const Handle(TColStd_HArray1OfExtendedString)& theParams,
87                                   const Handle(TColStd_HArray1OfExtendedString)& theValues)
88 {
89   // set error code, check parameters
90   SetErrorCode(KO);
91
92   if (theObject.IsNull())
93     return NULL;
94
95   if (theOperators.IsNull() || theOperators->Length() <= 0) {
96     SetErrorCode("No operators requested");
97     return NULL;
98   }
99
100   Standard_Integer nbParams = 0, nbValues = 0;
101   if (!theParams.IsNull()) {
102     nbParams = theParams->Length();
103   }
104   if (!theValues.IsNull()) {
105     nbValues = theValues->Length();
106   }
107
108   if (nbParams != nbValues) {
109     SetErrorCode("Number of parameter values must be equal to the number of parameters");
110     return NULL;
111   }
112
113   Handle(GEOM_Function) aFunction, aLastFunction = theObject->GetLastFunction();
114   if (aLastFunction.IsNull()) return NULL; //There is no function which creates an object to be processed
115
116   // Add a new object
117   Handle(GEOM_Object) aNewObject = GetEngine()->AddObject( GEOM_COPY );
118
119   //Add the function
120   aFunction = aNewObject->AddFunction(GEOMImpl_HealingDriver::GetID(), SHAPE_PROCESS);
121
122   if (aFunction.IsNull()) return NULL;
123
124   //Check if the function is set correctly
125   if (aFunction->GetDriverGUID() != GEOMImpl_HealingDriver::GetID()) return NULL;
126
127   // prepare "data container" class IHealing
128   GEOMImpl_IHealing HI(aFunction);
129   HI.SetOriginal(aLastFunction);
130   HI.SetOperators( theOperators );
131   if (nbParams > 0) {
132     HI.SetParameters( theParams );
133     HI.SetValues( theValues );
134   }
135   HI.SetStatistics( myModifStats );
136
137   //Compute the translation
138   try {
139     OCC_CATCH_SIGNALS;
140     if (!GetSolver()->ComputeFunction(aFunction))
141     {
142       SetErrorCode("Shape Healing algorithm failed");
143       return NULL;
144     }
145   }
146   catch (Standard_Failure& aFail)
147   {
148     SetErrorCode(aFail.GetMessageString());
149     return NULL;
150   }
151
152   //Make a Python command
153   GEOM::TPythonDump pd (aFunction);
154   pd << aNewObject << " = geompy.ProcessShape(" << theObject << ", [";
155
156   // list of operators
157   int i = theOperators->Lower(), nb = theOperators->Upper();
158   for ( ; i <= nb; i++) {
159     pd << "\"" << TCollection_AsciiString(theOperators->Value( i )).ToCString()
160       << (( i < nb ) ? "\", " : "\"");
161   }
162   pd << "], [";
163   // list of parameters
164   i = theParams->Lower(); nb = theParams->Upper();
165   for ( ; i <= nb; i++) {
166     pd << "\"" << TCollection_AsciiString(theParams->Value( i )).ToCString()
167       << (( i < nb ) ? "\", " : "\"");
168   }
169   pd << "], [";
170   // list of values
171   i = theValues->Lower(); nb = theValues->Upper();
172   for ( ; i <= nb; i++) {
173     pd << "\"" << TCollection_AsciiString(theValues->Value( i )).ToCString()
174       << (( i < nb ) ? "\", " : "\"");
175   }
176   pd << "])";
177
178   SetErrorCode(OK);
179   return aNewObject;
180 }
181
182 //=============================================================================
183 /*!
184  *  ShapeProcess
185  */
186 //=============================================================================
187 void GEOMImpl_IHealingOperations::GetShapeProcessParameters (std::list<std::string>& theOperations,
188                                                              std::list<std::string>& theParams,
189                                                              std::list<std::string>& theValues)
190 {
191   ShHealOper_ShapeProcess aHealer;
192   TColStd_SequenceOfAsciiString anOperators;
193   int nbOperatorErrors( 0 );
194   if ( aHealer.GetOperators( anOperators ) )
195   {
196     for ( Standard_Integer i = 1; i <= anOperators.Length(); i++ )
197     {
198       std::string anOperation = anOperators.Value( i ).ToCString();
199       if ( GetOperatorParameters( anOperation, theParams, theValues ) )
200         theOperations.push_back( anOperation );
201       else
202         nbOperatorErrors++;
203     }
204   }
205   else
206   {
207     SetErrorCode("ERROR retrieving operators (GEOMImpl_IHealingOperations)");
208   }
209
210   if ( nbOperatorErrors ) {
211     TCollection_AsciiString aMsg ("ERRORS retrieving ShapeProcess parameters (GEOMImpl_IHealingOperations): nbOperatorErrors = ");
212     aMsg += TCollection_AsciiString( nbOperatorErrors );
213     MESSAGE(aMsg.ToCString());
214   }
215 }
216
217 //=============================================================================
218 /*!
219  *  GetOperatorParameters
220  */
221 //=============================================================================
222 bool GEOMImpl_IHealingOperations::GetOperatorParameters( const std::string &     theOperation,
223                                                          std::list<std::string>& theParams,
224                                                          std::list<std::string>& theValues )
225 {
226   ShHealOper_ShapeProcess aHealer;
227   int nbParamValueErrors( 0 );
228   std::list<std::string> aParams;
229   if ( GetParameters( theOperation, aParams ) ) {
230     for ( std::list<std::string>::iterator it = aParams.begin(); it != aParams.end(); ++it ) {
231       TCollection_AsciiString aParam( (Standard_CString)(*it).c_str() );
232       TCollection_AsciiString aValue;
233       if ( aHealer.GetParameter( aParam, aValue ) ) {
234         theParams.push_back( aParam.ToCString() );
235         theValues.push_back( aValue.ToCString() );
236       }
237       else
238         nbParamValueErrors++;
239     }
240   }
241   else
242     return false;
243
244   if ( nbParamValueErrors ) {
245     TCollection_AsciiString aMsg ("ERRORS retrieving ShapeProcess parameter values (GEOMImpl_IHealingOperations): nbParamValueErrors = ");
246     aMsg += TCollection_AsciiString( nbParamValueErrors );
247     MESSAGE(aMsg.ToCString());
248   }
249
250   return true;
251 }
252
253 //=============================================================================
254 /*!
255  *  GetParameters
256  */
257 //=============================================================================
258 bool GEOMImpl_IHealingOperations::GetParameters (const std::string theOperation,
259                                                  std::list<std::string>& theParams)
260 {
261   if ( theOperation == "SplitAngle" ) {
262     theParams.push_back( "SplitAngle.Angle" );
263     theParams.push_back( "SplitAngle.MaxTolerance" );
264
265   } else if ( theOperation == "SplitClosedFaces" ) {
266     theParams.push_back( "SplitClosedFaces.NbSplitPoints" );
267
268   } else if ( theOperation == "FixFaceSize" ) {
269     theParams.push_back( "FixFaceSize.Tolerance" );
270
271   } else if( theOperation == "DropSmallEdges" ) {
272     theParams.push_back( "DropSmallEdges.Tolerance3d" );
273
274   } else if( theOperation == "DropSmallSolids" ) {
275     theParams.push_back( "DropSmallSolids.WidthFactorThreshold" );
276     theParams.push_back( "DropSmallSolids.VolumeThreshold" );
277     theParams.push_back( "DropSmallSolids.MergeSolids" );
278
279   } else if( theOperation == "BSplineRestriction" ) {
280     theParams.push_back( "BSplineRestriction.SurfaceMode" );
281     theParams.push_back( "BSplineRestriction.Curve3dMode" );
282     theParams.push_back( "BSplineRestriction.Curve2dMode" );
283     theParams.push_back( "BSplineRestriction.Tolerance3d" );
284     theParams.push_back( "BSplineRestriction.Tolerance2d" );
285     theParams.push_back( "BSplineRestriction.RequiredDegree" );
286     theParams.push_back( "BSplineRestriction.RequiredNbSegments" );
287     theParams.push_back( "BSplineRestriction.Continuity3d" );
288     theParams.push_back( "BSplineRestriction.Continuity2d" );
289
290   } else if( theOperation == "SplitContinuity" ) {
291     theParams.push_back( "SplitContinuity.Tolerance3d" );
292     theParams.push_back( "SplitContinuity.SurfaceContinuity" );
293     theParams.push_back( "SplitContinuity.CurveContinuity" );
294
295   } else if( theOperation == "ToBezier" ) {
296     theParams.push_back( "ToBezier.SurfaceMode" );
297     theParams.push_back( "ToBezier.Curve3dMode" );
298     theParams.push_back( "ToBezier.Curve2dMode" );
299     theParams.push_back( "ToBezier.MaxTolerance" );
300
301   } else if( theOperation == "SameParameter" ) {
302     theParams.push_back( "SameParameter.Tolerance3d" );
303
304   } else if( theOperation == "FixShape" ) {
305     theParams.push_back( "FixShape.Tolerance3d" );
306     theParams.push_back( "FixShape.MaxTolerance3d" );
307
308   } else {
309     return false;
310   }
311
312   return true;
313 }
314
315 //=============================================================================
316 /*!
317  *  SuppressFaces
318  */
319 //=============================================================================
320 Handle(GEOM_Object) GEOMImpl_IHealingOperations::SuppressFaces
321        (Handle(GEOM_Object) theObject, const Handle(TColStd_HArray1OfInteger)& theFaces)
322 {
323   // set error code, check parameters
324   SetErrorCode(KO);
325
326   if (theObject.IsNull()) // if theFaces.IsNull() - it's OK, it means that ALL faces must be removed..
327     return NULL;
328
329   Handle(GEOM_Function) aFunction, aLastFunction = theObject->GetLastFunction();
330   if (aLastFunction.IsNull()) return NULL; //There is no function which creates an object to be processed
331
332   // Add a new object
333   Handle(GEOM_Object) aNewObject = GetEngine()->AddObject(GEOM_COPY);
334
335   //Add the function
336   aFunction = aNewObject->AddFunction(GEOMImpl_HealingDriver::GetID(), SUPPRESS_FACES);
337
338   if (aFunction.IsNull()) return NULL;
339
340   //Check if the function is set correctly
341   if (aFunction->GetDriverGUID() != GEOMImpl_HealingDriver::GetID()) return NULL;
342
343   // prepare "data container" class IHealing
344   GEOMImpl_IHealing HI (aFunction);
345   HI.SetFaces(theFaces);
346   HI.SetOriginal(aLastFunction);
347   HI.SetStatistics( myModifStats );
348
349   //Compute the translation
350   try {
351     OCC_CATCH_SIGNALS;
352     if (!GetSolver()->ComputeFunction(aFunction))
353     {
354       SetErrorCode("Healing driver failed");
355       return NULL;
356     }
357   }
358   catch (Standard_Failure& aFail)
359   {
360     SetErrorCode(aFail.GetMessageString());
361     return NULL;
362   }
363
364   //Make a Python command
365   GEOM::TPythonDump pd (aFunction);
366   pd << aNewObject << " = geompy.SuppressFaces(" << theObject << ", [";
367
368   // list of face ids
369   int i = theFaces->Lower(), nb = theFaces->Upper();
370   for ( ; i <= nb; i++)
371     pd << theFaces->Value( i ) << (( i < nb ) ? ", " : "])");
372
373   SetErrorCode(OK);
374   return aNewObject;
375 }
376
377 //=============================================================================
378 /*!
379  *  CloseContour
380  */
381 //=============================================================================
382 Handle(GEOM_Object) GEOMImpl_IHealingOperations::CloseContour
383                     (Handle(GEOM_Object) theObject,
384                      const Handle(TColStd_HArray1OfInteger)& theWires,
385                      bool isCommonVertex)
386 {
387   // set error code, check parameters
388   SetErrorCode(KO);
389
390   if (theObject.IsNull())
391   {
392     SetErrorCode("NULL object given");
393     return NULL;
394   }
395
396   Handle(GEOM_Function) aFunction, aLastFunction = theObject->GetLastFunction();
397   if (aLastFunction.IsNull()) return NULL; //There is no function which creates an object to be processed
398
399   // Add a new object
400   Handle(GEOM_Object) aNewObject = GetEngine()->AddObject( GEOM_COPY );
401
402   //Add the function
403   aFunction = aNewObject->AddFunction(GEOMImpl_HealingDriver::GetID(), CLOSE_CONTOUR);
404
405   if (aFunction.IsNull()) return NULL;
406
407   //Check if the function is set correctly
408   if(aFunction->GetDriverGUID() != GEOMImpl_HealingDriver::GetID()) return NULL;
409
410   // prepare "data container" class IHealing
411   GEOMImpl_IHealing HI(aFunction);
412   HI.SetWires( theWires );
413   HI.SetIsCommonVertex( isCommonVertex );
414   HI.SetOriginal( aLastFunction );
415   HI.SetStatistics( myModifStats );
416
417   //Compute the translation
418   try {
419     OCC_CATCH_SIGNALS;
420     if (!GetSolver()->ComputeFunction(aFunction))
421     {
422       SetErrorCode("Healing driver failed");
423       return NULL;
424     }
425   }
426   catch (Standard_Failure& aFail)
427   {
428     SetErrorCode(aFail.GetMessageString());
429     return NULL;
430   }
431
432   //Make a Python command
433   GEOM::TPythonDump pd (aFunction);
434   pd << aNewObject << " = geompy.CloseContour(" << theObject << ", [";
435
436   // list of wire ids
437   if (!theWires.IsNull())
438   {
439     int i = theWires->Lower(), nb = theWires->Upper();
440     pd << theWires->Value(i++);
441     while (i <= nb)
442       pd << ", " << theWires->Value(i++);
443   }
444   pd << "], " << (int)isCommonVertex << ")";
445
446   SetErrorCode(OK);
447   return aNewObject;
448 }
449
450 //=============================================================================
451 /*!
452  *  RemoveIntWires
453  */
454 //=============================================================================
455 Handle(GEOM_Object) GEOMImpl_IHealingOperations::RemoveIntWires
456        (Handle(GEOM_Object) theObject, const Handle(TColStd_HArray1OfInteger)& theWires)
457 {
458   // set error code, check parameters
459   SetErrorCode(KO);
460
461   if (theObject.IsNull()) // if theWires is NULL it's OK, it means that ALL wires must be removed
462     return NULL;
463
464   Handle(GEOM_Function) aFunction, aLastFunction = theObject->GetLastFunction();
465   if (aLastFunction.IsNull()) return NULL; //There is no function which creates an object to be processed
466
467   // Add a new object
468   Handle(GEOM_Object) aNewObject = GetEngine()->AddObject( GEOM_COPY );
469
470   //Add the function
471   aFunction = aNewObject->AddFunction(GEOMImpl_HealingDriver::GetID(), REMOVE_INT_WIRES);
472
473   if (aFunction.IsNull()) return NULL;
474
475   //Check if the function is set correctly
476   if (aFunction->GetDriverGUID() != GEOMImpl_HealingDriver::GetID()) return NULL;
477
478   // prepare "data container" class IHealing
479   GEOMImpl_IHealing HI(aFunction);
480   HI.SetWires( theWires );
481   HI.SetOriginal( aLastFunction );
482   HI.SetStatistics( myModifStats );
483
484   //Compute the translation
485   try {
486     OCC_CATCH_SIGNALS;
487     if (!GetSolver()->ComputeFunction(aFunction))
488     {
489       SetErrorCode("Healing driver failed");
490       return NULL;
491     }
492   }
493   catch (Standard_Failure& aFail)
494   {
495     SetErrorCode(aFail.GetMessageString());
496     return NULL;
497   }
498
499   //Make a Python command
500   GEOM::TPythonDump pd (aFunction);
501   pd << aNewObject << " = geompy.SuppressInternalWires(" << theObject << ", [";
502
503   // list of wire ids
504   if (!theWires.IsNull()) {
505     int i = theWires->Lower(), nb = theWires->Upper();
506     for ( ; i <= nb; i++)
507       pd << theWires->Value( i ) << (( i < nb ) ? ", " : "])");
508   } else {
509     pd << "])";
510   }
511
512   SetErrorCode(OK);
513   return aNewObject;
514 }
515
516 //=============================================================================
517 /*!
518  *  FillHoles
519  */
520 //=============================================================================
521 Handle(GEOM_Object) GEOMImpl_IHealingOperations::FillHoles (Handle(GEOM_Object) theObject,
522                                                             const Handle(TColStd_HArray1OfInteger)& theWires)
523 {
524   // set error code, check parameters
525   SetErrorCode(KO);
526
527   if (theObject.IsNull()) // if theWires is NULL it's OK, it means that ALL holes must be removed
528     return NULL;
529
530   Handle(GEOM_Function) aFunction, aLastFunction = theObject->GetLastFunction();
531   if (aLastFunction.IsNull()) return NULL; //There is no function which creates an object to be processed
532
533   // Add a new object
534   Handle(GEOM_Object) aNewObject = GetEngine()->AddObject( GEOM_COPY );
535
536   //Add the function
537   aFunction = aNewObject->AddFunction(GEOMImpl_HealingDriver::GetID(), FILL_HOLES);
538
539   if (aFunction.IsNull()) return NULL;
540
541   //Check if the function is set correctly
542   if (aFunction->GetDriverGUID() != GEOMImpl_HealingDriver::GetID()) return NULL;
543
544   // prepare "data container" class IHealing
545   GEOMImpl_IHealing HI(aFunction);
546   HI.SetWires( theWires );
547   HI.SetOriginal( aLastFunction );
548   HI.SetStatistics( myModifStats );
549
550   //Compute the translation
551   try {
552     OCC_CATCH_SIGNALS;
553     if (!GetSolver()->ComputeFunction(aFunction))
554     {
555       SetErrorCode("Healing driver failed");
556       return NULL;
557     }
558   }
559   catch (Standard_Failure& aFail)
560   {
561     SetErrorCode(aFail.GetMessageString());
562     return NULL;
563   }
564
565   //Make a Python command
566   GEOM::TPythonDump pd (aFunction);
567   pd << aNewObject << " = geompy.SuppressHoles(" << theObject << ", [";
568
569   // list of wire ids
570   if ( theWires.IsNull() )
571     pd << "])";
572   else {
573     int i = theWires->Lower(), nb = theWires->Upper();
574     for ( ; i <= nb; i++)
575       pd << theWires->Value( i ) << (( i < nb ) ? ", " : "])");
576   }
577
578   SetErrorCode(OK);
579   return aNewObject;
580 }
581
582 //=============================================================================
583 /*!
584  *  Sew
585  */
586 //=============================================================================
587 Handle(GEOM_Object)
588 GEOMImpl_IHealingOperations::Sew (std::list<Handle(GEOM_Object)>& theObjects,
589                                   double                          theTolerance,
590                                   bool                            isAllowNonManifold)
591 {
592   // set error code, check parameters
593   SetErrorCode(KO);
594
595   if (theObjects.empty())
596     return NULL;
597
598   Handle(TColStd_HSequenceOfTransient) objects =
599     GEOM_Object::GetLastFunctions( theObjects );
600   if ( objects.IsNull() || objects->IsEmpty() ) {
601     SetErrorCode("NULL argument shape");
602     return NULL;
603   }
604
605   // Add a new object
606   Handle(GEOM_Object) aNewObject = GetEngine()->AddObject( GEOM_COPY );
607
608   //Add the function
609   int aFunctionType = (isAllowNonManifold ? SEWING_NON_MANIFOLD : SEWING);
610   Handle(GEOM_Function) aFunction =
611     aNewObject->AddFunction(GEOMImpl_HealingDriver::GetID(), aFunctionType);
612   if (aFunction.IsNull()) return NULL;
613
614   //Check if the function is set correctly
615   if (aFunction->GetDriverGUID() != GEOMImpl_HealingDriver::GetID()) return NULL;
616
617   // prepare "data container" class IHealing
618   GEOMImpl_IHealing HI(aFunction);
619   HI.SetTolerance( theTolerance );
620   HI.SetOriginal( theObjects.front()->GetLastFunction() ); objects->Remove(1);
621   HI.SetShapes( objects );
622   HI.SetStatistics( myModifStats );
623
624   //Compute the result
625   try {
626     OCC_CATCH_SIGNALS;
627     if (!GetSolver()->ComputeFunction(aFunction))
628     {
629       SetErrorCode("Healing driver failed");
630       return NULL;
631     }
632   }
633   catch (Standard_Failure& aFail) {
634     SetErrorCode(aFail.GetMessageString());
635     return NULL;
636   }
637
638   //Make a Python command
639   GEOM::TPythonDump pd(aFunction);
640   
641   pd << aNewObject << " = geompy.Sew(" << theObjects << ", " << theTolerance;
642
643   if (isAllowNonManifold) {
644     pd << ", True";
645   }
646
647   pd << ")";
648
649   SetErrorCode(OK);
650   return aNewObject;
651 }
652
653 //=============================================================================
654 /*!
655  *  RemoveInternalFaces
656  */
657 //=============================================================================
658 Handle(GEOM_Object)
659 GEOMImpl_IHealingOperations::RemoveInternalFaces (std::list< Handle(GEOM_Object)> & theSolids)
660 {
661   // set error code, check parameters
662   SetErrorCode(KO);
663
664   if (theSolids.empty())
665     return NULL;
666
667   Handle(TColStd_HSequenceOfTransient) objects = GEOM_Object::GetLastFunctions( theSolids );
668   if ( objects.IsNull() || objects->IsEmpty() ) {
669     SetErrorCode("NULL argument shape");
670     return NULL;
671   }
672
673   // Add a new object
674   Handle(GEOM_Object) aNewObject = GetEngine()->AddObject(GEOM_COPY);
675
676   //Add the function
677   Handle(GEOM_Function)
678     aFunction = aNewObject->AddFunction(GEOMImpl_HealingDriver::GetID(), REMOVE_INTERNAL_FACES);
679   if (aFunction.IsNull()) return NULL;
680
681   //Check if the function is set correctly
682   if (aFunction->GetDriverGUID() != GEOMImpl_HealingDriver::GetID()) return NULL;
683
684   // prepare "data container" class IHealing
685   GEOMImpl_IHealing HI (aFunction);
686   HI.SetOriginal( theSolids.front()->GetLastFunction() ); objects->Remove(1);
687   HI.SetShapes( objects );
688   HI.SetStatistics( myModifStats );
689
690   //Compute the result
691   try {
692     OCC_CATCH_SIGNALS;
693     if (!GetSolver()->ComputeFunction(aFunction))
694     {
695       SetErrorCode("Healing driver failed");
696       return NULL;
697     }
698   }
699   catch (Standard_Failure& aFail) {
700     SetErrorCode(aFail.GetMessageString());
701     return NULL;
702   }
703
704   //Make a Python command
705   GEOM::TPythonDump(aFunction) << aNewObject << " = geompy.RemoveInternalFaces(" << theSolids << ")";
706
707   SetErrorCode(OK);
708   return aNewObject;
709 }
710
711 //=============================================================================
712 /*!
713  *  DivideEdge
714  */
715 //=============================================================================
716 Handle(GEOM_Object) GEOMImpl_IHealingOperations::DivideEdge (Handle(GEOM_Object) theObject,
717                                                              int theIndex,
718                                                              double theValue,
719                                                              bool isByParameter)
720 {
721   // set error code, check parameters
722   SetErrorCode(KO);
723
724   if (theObject.IsNull())
725     return NULL;
726
727   Handle(GEOM_Function) aFunction, aLastFunction = theObject->GetLastFunction();
728   if (aLastFunction.IsNull()) return NULL; //There is no function which creates an object to be processed
729
730   // Add a new object
731   Handle(GEOM_Object) aNewObject = GetEngine()->AddObject( GEOM_COPY );
732
733   //Add the function
734   aFunction = aNewObject->AddFunction(GEOMImpl_HealingDriver::GetID(), DIVIDE_EDGE);
735
736   if (aFunction.IsNull()) return NULL;
737
738   //Check if the function is set correctly
739   if (aFunction->GetDriverGUID() != GEOMImpl_HealingDriver::GetID()) return NULL;
740
741   // prepare "data container" class IHealing
742   GEOMImpl_IHealing HI(aFunction);
743   HI.SetIndex( theIndex );
744   HI.SetDevideEdgeValue( theValue );
745   HI.SetIsByParameter( isByParameter );
746   HI.SetOriginal( aLastFunction );
747   HI.SetStatistics( myModifStats );
748
749   //Compute the translation
750   try {
751     OCC_CATCH_SIGNALS;
752     if (!GetSolver()->ComputeFunction(aFunction)) {
753       SetErrorCode("Healing driver failed");
754       return NULL;
755     }
756   }
757   catch (Standard_Failure& aFail) {
758     SetErrorCode(aFail.GetMessageString());
759     return NULL;
760   }
761
762   //Make a Python command
763   GEOM::TPythonDump(aFunction) << aNewObject << " = geompy.DivideEdge(" << theObject
764     << ", " << theIndex << ", " << theValue << ", " << (int)isByParameter << ")";
765
766   SetErrorCode(OK);
767   return aNewObject;
768 }
769
770 //=============================================================================
771 /*!
772  *  DivideEdgeByPoint
773  */
774 //=============================================================================
775 Handle(GEOM_Object)
776 GEOMImpl_IHealingOperations::DivideEdgeByPoint (Handle(GEOM_Object)               theObject,
777                                                 int                               theIndex,
778                                                 std::list< Handle(GEOM_Object)> & thePoints)
779 {
780   // set error code, check parameters
781   SetErrorCode(KO);
782
783   if (theObject.IsNull() || thePoints.empty() )
784     return NULL;
785
786   Handle(GEOM_Function) aFunction, aLastFunction = theObject->GetLastFunction();
787   if (aLastFunction.IsNull() )
788     return NULL; //There is no function which creates an object to be processed
789
790   Handle(TColStd_HSequenceOfTransient) aPointFunc = GEOM_Object::GetLastFunctions( thePoints );
791   if ( aPointFunc.IsNull() || aPointFunc->IsEmpty() ) {
792     SetErrorCode("NULL argument points");
793     return NULL;
794   }
795
796   // Add a new object
797   Handle(GEOM_Object) aNewObject = GetEngine()->AddObject( GEOM_COPY );
798
799   //Add the function
800   aFunction = aNewObject->AddFunction(GEOMImpl_HealingDriver::GetID(), DIVIDE_EDGE_BY_POINT);
801
802   if (aFunction.IsNull()) return NULL;
803
804   //Check if the function is set correctly
805   if (aFunction->GetDriverGUID() != GEOMImpl_HealingDriver::GetID()) return NULL;
806
807   // prepare "data container" class IHealing
808   GEOMImpl_IHealing HI(aFunction);
809   HI.SetIndex     ( theIndex );
810   HI.SetOriginal  ( aLastFunction );
811   HI.SetShapes    ( aPointFunc );
812
813   HI.SetStatistics( myModifStats );
814
815   //Compute the translation
816   try {
817     OCC_CATCH_SIGNALS;
818     if (!GetSolver()->ComputeFunction(aFunction)) {
819       SetErrorCode("Healing driver failed");
820       return NULL;
821     }
822   }
823   catch (Standard_Failure& aFail) {
824     SetErrorCode(aFail.GetMessageString());
825     return NULL;
826   }
827
828   //Make a Python command
829   GEOM::TPythonDump(aFunction)
830     << aNewObject << " = geompy.DivideEdgeByPoint(" << theObject
831     << ", " << theIndex << ", " << thePoints << ")";
832
833   SetErrorCode(OK);
834   return aNewObject;
835 }
836
837 //=============================================================================
838 /*!
839  *  FuseCollinearEdgesWithinWire
840  */
841 //=============================================================================
842 Handle(GEOM_Object) GEOMImpl_IHealingOperations::FuseCollinearEdgesWithinWire
843                                    (Handle(GEOM_Object) theWire,
844                                     std::list<Handle(GEOM_Object)> theVertices)
845 {
846   SetErrorCode(KO);
847
848   if (theWire.IsNull()) return NULL;
849
850   // Add a new object
851   Handle(GEOM_Object) aRes = GetEngine()->AddObject(theWire->GetType());
852
853   // Add a new function
854   Handle(GEOM_Function) aFunction;
855   aFunction = aRes->AddFunction(GEOMImpl_HealingDriver::GetID(), FUSE_COLLINEAR_EDGES);
856   if (aFunction.IsNull()) return NULL;
857
858   // Check if the function is set correctly
859   if (aFunction->GetDriverGUID() != GEOMImpl_HealingDriver::GetID()) return NULL;
860
861   GEOMImpl_IHealing aCI (aFunction);
862   aCI.SetStatistics( myModifStats );
863
864   Handle(GEOM_Function) aRefShape = theWire->GetLastFunction();
865   if (aRefShape.IsNull()) return NULL;
866   aCI.SetOriginal(aRefShape);
867
868   Handle(TColStd_HSequenceOfTransient) aVertices = new TColStd_HSequenceOfTransient;
869   std::list<Handle(GEOM_Object)>::iterator it = theVertices.begin();
870   for (; it != theVertices.end(); it++) {
871     Handle(GEOM_Function) aRefSh = (*it)->GetLastFunction();
872     if (aRefSh.IsNull()) {
873       SetErrorCode("NULL argument shape for the shape construction");
874       return NULL;
875     }
876     aVertices->Append(aRefSh);
877   }
878   aCI.SetShapes(aVertices);
879
880   // Compute the new wire
881   try {
882     OCC_CATCH_SIGNALS;
883     if (!GetSolver()->ComputeFunction(aFunction)) {
884       SetErrorCode("Healing driver failed");
885       return NULL;
886     }
887   }
888   catch (Standard_Failure& aFail) {
889     SetErrorCode(aFail.GetMessageString());
890     return NULL;
891   }
892
893   // Make a Python command
894   GEOM::TPythonDump pd (aFunction);
895   pd << aRes << " = geompy.FuseCollinearEdgesWithinWire(" << theWire << ", [";
896   // Vertices
897   it = theVertices.begin();
898   if (it != theVertices.end()) {
899     pd << (*it++);
900     while (it != theVertices.end()) {
901       pd << ", " << (*it++);
902     }
903   }
904   pd << "])";
905
906   SetErrorCode(OK);
907   return aRes;
908 }
909
910 //=============================================================================
911 /*!
912  *  GetFreeBoundary
913  */
914 //=============================================================================
915 bool GEOMImpl_IHealingOperations::GetFreeBoundary (Handle(TColStd_HSequenceOfTransient)& theObjects,
916                                                    Handle(TColStd_HSequenceOfTransient)& theClosed,
917                                                    Handle(TColStd_HSequenceOfTransient)& theOpen )
918 {
919   // set error code, check parameters
920   SetErrorCode(KO);
921
922   if ( theObjects.IsNull() || theObjects->Length() == 0 ||
923        theClosed.IsNull()  || theOpen.IsNull() )
924     return false;
925
926   TopoDS_Shape aShape;
927   TopTools_SequenceOfShape shapes;
928   for ( int ind = 1; ind <= theObjects->Length(); ind++)
929   {
930     Handle(GEOM_Object) aRefShape = Handle(GEOM_Object)::DownCast( theObjects->Value(ind));
931     if ( aRefShape.IsNull() )
932       return false;
933     aShape = aRefShape->GetValue();
934     if ( aShape.IsNull() )
935       return false;
936     shapes.Append( aShape );
937   }
938
939   if ( shapes.Length() > 1 )
940   {
941     TopoDS_Compound compound;
942     BRep_Builder builder;
943     builder.MakeCompound( compound );
944     for ( int i = 1; i <= shapes.Length(); ++i )
945       builder.Add( compound, shapes( i ) );
946
947     aShape = compound;
948   }
949
950   // get free boundary shapes
951
952   ShapeAnalysis_FreeBounds anAnalizer(aShape, Standard_False,
953                                       Standard_True, Standard_True);
954   TopoDS_Compound aClosed = anAnalizer.GetClosedWires();
955   TopoDS_Compound anOpen = anAnalizer.GetOpenWires();
956
957   // iterate through shapes and append them to the return sequence
958   Handle(GEOM_Object) anObj;
959   Handle(GEOM_Function) aFunction;
960   TopExp_Explorer anExp;
961   for ( anExp.Init( aClosed, TopAbs_WIRE ); anExp.More(); anExp.Next() )
962   {
963     anObj = GetEngine()->AddObject( GEOM_FREE_BOUNDS );
964     aFunction = anObj->AddFunction( GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF );
965     TopoDS_Shape aValueShape = anExp.Current();
966     aFunction->SetValue( aValueShape );
967     theClosed->Append(anObj);
968   }
969   for ( anExp.Init( anOpen, TopAbs_WIRE ); anExp.More(); anExp.Next() )
970   {
971     anObj = GetEngine()->AddObject( GEOM_FREE_BOUNDS );
972     aFunction = anObj->AddFunction( GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF );
973     TopoDS_Shape aValueShape = anExp.Current();
974     aFunction->SetValue( aValueShape );
975     theOpen->Append(anObj);
976   }
977
978   if(!aFunction.IsNull()) {
979
980     //Make a Python command
981     GEOM::TPythonDump pd (aFunction);
982
983     Standard_Integer i, aLen = theClosed->Length();
984     if (aLen > 0) {
985       pd << "(isDone, [";
986       for (i = 1; i <= aLen; i++) {
987         Handle(GEOM_Object) anObj_i = Handle(GEOM_Object)::DownCast(theClosed->Value(i));
988         pd << anObj_i << ((i < aLen) ? ", " : "");
989       }
990       pd << "], ";
991     } else {
992       pd << "(isDone, empty_list, ";
993     }
994
995     aLen = theOpen->Length();
996     if (aLen > 0) {
997       pd << "[";
998       for (i = 1; i <= aLen; i++) {
999         Handle(GEOM_Object) anObj_i = Handle(GEOM_Object)::DownCast(theOpen->Value(i));
1000         pd << anObj_i << ((i < aLen) ? ", " : "");
1001       }
1002       pd << "]";
1003     } else {
1004       pd << "empty_list";
1005     }
1006
1007     pd << ") = geompy.GetFreeBoundary(" << theObjects << ")";
1008   }
1009
1010   SetErrorCode(OK);
1011   return true;
1012 }
1013
1014
1015 //=============================================================================
1016 /*!
1017  *  ChangeOrientation
1018  */
1019 //=============================================================================
1020 Handle(GEOM_Object) GEOMImpl_IHealingOperations::ChangeOrientation (Handle(GEOM_Object) theObject)
1021 {
1022   // set error code, check parameters
1023   SetErrorCode(KO);
1024
1025   if (theObject.IsNull())
1026     return NULL;
1027
1028   if (!theObject->IsMainShape()) {
1029     SetErrorCode("Sub-shape cannot be transformed - need to create a copy");
1030     return NULL;
1031   }
1032
1033   Handle(GEOM_Function) aFunction, aLastFunction = theObject->GetLastFunction();
1034   if (aLastFunction.IsNull())
1035     return NULL; //There is no function which creates an object to be processed
1036
1037   if (theObject->GetType() == GEOM_VECTOR) { // Mantis issue 21066
1038     //Add the function
1039     aFunction = theObject->AddFunction(GEOMImpl_VectorDriver::GetID(), VECTOR_REVERSE);
1040
1041     //Check if the function is set correctly
1042     if (aFunction.IsNull()) return NULL;
1043     if (aFunction->GetDriverGUID() != GEOMImpl_VectorDriver::GetID()) return NULL;
1044
1045     // prepare "data container" class IVector
1046     GEOMImpl_IVector aVI (aFunction);
1047     aVI.SetCurve(aLastFunction);
1048
1049     myModifStats->Clear();
1050     myModifStats->AddModif( "Vector reversed" );
1051   }
1052   else {
1053     //Add the function
1054     aFunction = theObject->AddFunction(GEOMImpl_HealingDriver::GetID(), CHANGE_ORIENTATION);
1055
1056     //Check if the function is set correctly
1057     if (aFunction.IsNull()) return NULL;
1058     if (aFunction->GetDriverGUID() != GEOMImpl_HealingDriver::GetID()) return NULL;
1059
1060     // prepare "data container" class IHealing
1061     GEOMImpl_IHealing HI (aFunction);
1062     HI.SetOriginal(aLastFunction);
1063     HI.SetStatistics( myModifStats );
1064   }
1065
1066   //Compute the translation
1067   try {
1068     OCC_CATCH_SIGNALS;
1069     if (!GetSolver()->ComputeFunction(aFunction)) {
1070       SetErrorCode("Healing driver failed");
1071       return NULL;
1072     }
1073   }
1074   catch (Standard_Failure& aFail) {
1075     SetErrorCode(aFail.GetMessageString());
1076     return NULL;
1077   }
1078
1079   //Make a Python command
1080   GEOM::TPythonDump(aFunction) << "geompy.ChangeOrientationShell("
1081                                << theObject << ")";
1082
1083   SetErrorCode(OK);
1084   return theObject;
1085 }
1086
1087 //=============================================================================
1088 /*!
1089  *  ChangeOrientationCopy
1090  */
1091 //=============================================================================
1092 Handle(GEOM_Object) GEOMImpl_IHealingOperations::ChangeOrientationCopy (Handle(GEOM_Object) theObject)
1093 {
1094   // set error code, check parameters
1095   SetErrorCode(KO);
1096
1097   if (theObject.IsNull())
1098     return NULL;
1099
1100   Handle(GEOM_Function) aFunction, aLastFunction = theObject->GetLastFunction();
1101   if (aLastFunction.IsNull())
1102     return NULL; //There is no function which creates an object to be processed
1103
1104   // Add a new object
1105   Handle(GEOM_Object) aNewObject = GetEngine()->AddObject(GEOM_COPY);
1106
1107   if (theObject->GetType() == GEOM_VECTOR) { // Mantis issue 21066
1108     //Add the function
1109     aFunction = aNewObject->AddFunction(GEOMImpl_VectorDriver::GetID(), VECTOR_REVERSE);
1110
1111     //Check if the function is set correctly
1112     if (aFunction.IsNull()) return NULL;
1113     if (aFunction->GetDriverGUID() != GEOMImpl_VectorDriver::GetID()) return NULL;
1114
1115     // prepare "data container" class IVector
1116     GEOMImpl_IVector aVI (aFunction);
1117     aVI.SetCurve(aLastFunction);
1118
1119     myModifStats->Clear();
1120     myModifStats->AddModif( "Vector reversed" );
1121   }
1122   else {
1123     //Add the function
1124     aFunction = aNewObject->AddFunction(GEOMImpl_HealingDriver::GetID(), CHANGE_ORIENTATION);
1125
1126     //Check if the function is set correctly
1127     if (aFunction.IsNull()) return NULL;
1128     if (aFunction->GetDriverGUID() != GEOMImpl_HealingDriver::GetID()) return NULL;
1129
1130     // prepare "data container" class IHealing
1131     GEOMImpl_IHealing aHI (aFunction);
1132     aHI.SetOriginal(aLastFunction);
1133     aHI.SetStatistics( myModifStats );
1134   }
1135
1136   // Compute the result
1137   try {
1138     OCC_CATCH_SIGNALS;
1139     if (!GetSolver()->ComputeFunction(aFunction)) {
1140       SetErrorCode("Healing driver failed");
1141       return NULL;
1142     }
1143   }
1144   catch (Standard_Failure& aFail) {
1145     SetErrorCode(aFail.GetMessageString());
1146     return NULL;
1147   }
1148
1149   //Make a Python command
1150   GEOM::TPythonDump(aFunction) << aNewObject << " = geompy.ChangeOrientationShellCopy("
1151                                << theObject << ")";
1152
1153   SetErrorCode(OK);
1154   return aNewObject;
1155 }
1156
1157 //=============================================================================
1158 /*!
1159  *  LimitTolerance
1160  */
1161 //=============================================================================
1162 Handle(GEOM_Object) GEOMImpl_IHealingOperations::LimitTolerance (Handle(GEOM_Object) theObject,
1163                                                                  double theTolerance,
1164                                                                  TopAbs_ShapeEnum theType)
1165 {
1166   // Set error code, check parameters
1167   SetErrorCode(KO);
1168
1169   if (theObject.IsNull())
1170     return NULL;
1171
1172   Handle(GEOM_Function) aFunction, aLastFunction = theObject->GetLastFunction();
1173   if (aLastFunction.IsNull())
1174     return NULL; // There is no function which creates an object to be processed
1175
1176   // Add a new object
1177   Handle(GEOM_Object) aNewObject = GetEngine()->AddObject(theObject->GetType());
1178
1179   // Add the function
1180   aFunction = aNewObject->AddFunction(GEOMImpl_HealingDriver::GetID(), LIMIT_TOLERANCE);
1181
1182   if (aFunction.IsNull())
1183     return NULL;
1184
1185   // Check if the function is set correctly
1186   if (aFunction->GetDriverGUID() != GEOMImpl_HealingDriver::GetID()) return NULL;
1187
1188   // Prepare "data container" class IHealing
1189   GEOMImpl_IHealing HI (aFunction);
1190   HI.SetOriginal(aLastFunction);
1191   HI.SetTolerance(theTolerance);
1192   HI.SetType(theType);
1193   HI.SetStatistics( myModifStats );
1194
1195   // Compute
1196   try {
1197     OCC_CATCH_SIGNALS;
1198     if (!GetSolver()->ComputeFunction(aFunction)) {
1199       SetErrorCode("Healing driver failed");
1200       return NULL;
1201     }
1202   }
1203   catch (Standard_Failure& aFail) {
1204     SetErrorCode(aFail.GetMessageString());
1205     return NULL;
1206   }
1207
1208   // Make a Python command
1209   GEOM::TPythonDump(aFunction) << aNewObject << " = geompy.LimitTolerance("
1210                                << theObject << ", " << theTolerance << ")";
1211
1212   SetErrorCode(OK);
1213   return aNewObject;
1214 }