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