Salome HOME
Mantis issue 0021414: There is a difference between vectors and other edges in Geometry.
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IHealingOperations.cxx
1 // Copyright (C) 2007-2011  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.
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 #ifdef WNT
23 #pragma warning( disable:4786 )
24 #endif
25
26 #include <Standard_Version.hxx>
27 #include <Standard_Stream.hxx>
28
29 #include <GEOMImpl_IHealingOperations.hxx>
30
31 #include <GEOM_PythonDump.hxx>
32
33 #include <GEOMImpl_HealingDriver.hxx>
34 #include <GEOMImpl_Types.hxx>
35 #include <GEOMImpl_IHealing.hxx>
36 #include <GEOMImpl_IVector.hxx>
37 #include <GEOMImpl_VectorDriver.hxx>
38 #include <GEOMImpl_CopyDriver.hxx>
39
40 #include <Basics_OCCTVersion.hxx>
41
42 #include "utilities.h"
43 #include <OpUtil.hxx>
44 #include <Utils_ExceptHandlers.hxx>
45
46 #include <ShHealOper_ShapeProcess.hxx>
47
48 #include <ShapeAnalysis_FreeBounds.hxx>
49
50 #include <TopoDS_Compound.hxx>
51 #include <TopExp_Explorer.hxx>
52
53 #include <TColStd_HArray1OfExtendedString.hxx>
54 #include <TColStd_HSequenceOfTransient.hxx>
55 #include <TCollection_AsciiString.hxx>
56
57 #include <TDF_Tool.hxx>
58
59 #include <Standard_Failure.hxx>
60 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
61
62 //=============================================================================
63 /*!
64  *   constructor:
65  */
66 //=============================================================================
67 GEOMImpl_IHealingOperations::GEOMImpl_IHealingOperations (GEOM_Engine* theEngine, int theDocID)
68 : GEOM_IOperations(theEngine, theDocID)
69 {
70   MESSAGE("GEOMImpl_IHealingOperations::GEOMImpl_IHealingOperations");
71 }
72
73 //=============================================================================
74 /*!
75  *  destructor
76  */
77 //=============================================================================
78 GEOMImpl_IHealingOperations::~GEOMImpl_IHealingOperations()
79 {
80   MESSAGE("GEOMImpl_IHealingOperations::~GEOMImpl_IHealingOperations");
81 }
82
83
84 //=============================================================================
85 /*!
86  *  ShapeProcess
87  */
88 //=============================================================================
89 Handle(GEOM_Object) GEOMImpl_IHealingOperations::ShapeProcess (Handle(GEOM_Object) theObject,
90                                   const Handle(TColStd_HArray1OfExtendedString)& theOperators,
91                                   const Handle(TColStd_HArray1OfExtendedString)& theParams,
92                                   const Handle(TColStd_HArray1OfExtendedString)& theValues)
93 {
94   // set error code, check parameters
95   SetErrorCode(KO);
96
97   if (theObject.IsNull())
98     return NULL;
99
100   if (theOperators.IsNull() || theOperators->Length() <= 0) {
101     SetErrorCode("No operators requested");
102     return NULL;
103   }
104
105   Standard_Integer nbParams = 0, nbValues = 0;
106   if (!theParams.IsNull()) {
107     nbParams = theParams->Length();
108   }
109   if (!theValues.IsNull()) {
110     nbValues = theValues->Length();
111   }
112
113   if (nbParams != nbValues) {
114     SetErrorCode("Number of parameter values must be equal to the number of parameters");
115     return NULL;
116   }
117
118   Handle(GEOM_Function) aFunction, aLastFunction = theObject->GetLastFunction();
119   if (aLastFunction.IsNull()) return NULL; //There is no function which creates an object to be processed
120
121   // Add a new object
122   Handle(GEOM_Object) aNewObject = GetEngine()->AddObject( GetDocID(), GEOM_COPY );
123
124   //Add the function
125   aFunction = aNewObject->AddFunction(GEOMImpl_HealingDriver::GetID(), SHAPE_PROCESS);
126
127   if (aFunction.IsNull()) return NULL;
128
129   //Check if the function is set correctly
130   if (aFunction->GetDriverGUID() != GEOMImpl_HealingDriver::GetID()) return NULL;
131
132   // prepare "data container" class IHealing
133   GEOMImpl_IHealing HI(aFunction);
134   HI.SetOriginal(aLastFunction);
135   HI.SetOperators( theOperators );
136   if (nbParams > 0) {
137     HI.SetParameters( theParams );
138     HI.SetValues( theValues );
139   }
140
141   //Compute the translation
142   try {
143 #if OCC_VERSION_LARGE > 0x06010000
144     OCC_CATCH_SIGNALS;
145 #endif
146     if (!GetSolver()->ComputeFunction(aFunction))
147     {
148       SetErrorCode("Shape Healing algorithm failed");
149       return NULL;
150     }
151   }
152   catch (Standard_Failure)
153   {
154     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
155     SetErrorCode(aFail->GetMessageString());
156     return NULL;
157   }
158
159   //Make a Python command
160   GEOM::TPythonDump pd (aFunction);
161   pd << aNewObject << " = geompy.ProcessShape(" << theObject << ", [";
162
163   // list of operators
164   int i = theOperators->Lower(), nb = theOperators->Upper();
165   for ( ; i <= nb; i++) {
166     pd << "\"" << TCollection_AsciiString(theOperators->Value( i )).ToCString()
167       << (( i < nb ) ? "\", " : "\"");
168   }
169   pd << "], [";
170   // list of parameters
171   i = theParams->Lower(); nb = theParams->Upper();
172   for ( ; i <= nb; i++) {
173     pd << "\"" << TCollection_AsciiString(theParams->Value( i )).ToCString()
174       << (( i < nb ) ? "\", " : "\"");
175   }
176   pd << "], [";
177   // list of values
178   i = theValues->Lower(); nb = theValues->Upper();
179   for ( ; i <= nb; i++) {
180     pd << "\"" << TCollection_AsciiString(theValues->Value( i )).ToCString()
181       << (( i < nb ) ? "\", " : "\"");
182   }
183   pd << "])";
184
185   SetErrorCode(OK);
186   return aNewObject;
187 }
188
189 //=============================================================================
190 /*!
191  *  ShapeProcess
192  */
193 //=============================================================================
194 void GEOMImpl_IHealingOperations::GetShapeProcessParameters (std::list<std::string>& theOperations,
195                                                              std::list<std::string>& theParams,
196                                                              std::list<std::string>& theValues)
197 {
198   ShHealOper_ShapeProcess aHealer;
199   TColStd_SequenceOfAsciiString anOperators;
200   int nbOperatorErrors( 0 );
201   if ( aHealer.GetOperators( anOperators ) )
202   {
203     for ( Standard_Integer i = 1; i <= anOperators.Length(); i++ )
204     {
205       std::string anOperation = anOperators.Value( i ).ToCString();
206       if ( GetOperatorParameters( anOperation, theParams, theValues ) )
207         theOperations.push_back( anOperation );
208       else
209         nbOperatorErrors++;
210     }
211   }
212   else
213   {
214     SetErrorCode("ERROR retrieving operators (GEOMImpl_IHealingOperations)");
215   }
216
217   if ( nbOperatorErrors ) {
218     TCollection_AsciiString aMsg ("ERRORS retrieving ShapeProcess parameters (GEOMImpl_IHealingOperations): nbOperatorErrors = ");
219     aMsg += TCollection_AsciiString( nbOperatorErrors );
220     MESSAGE(aMsg.ToCString());
221   }
222 }
223
224 //=============================================================================
225 /*!
226  *  GetOperatorParameters
227  */
228 //=============================================================================
229 bool GEOMImpl_IHealingOperations::GetOperatorParameters( const std::string theOperation,
230                                                          std::list<std::string>& theParams,
231                                                          std::list<std::string>& theValues )
232 {
233   ShHealOper_ShapeProcess aHealer;
234   int nbParamValueErrors( 0 );
235   std::list<std::string> aParams;
236   if ( GetParameters( theOperation, aParams ) ) {
237     for ( std::list<std::string>::iterator it = aParams.begin(); it != aParams.end(); ++it ) {
238       TCollection_AsciiString aParam( (Standard_CString)(*it).c_str() );
239       TCollection_AsciiString aValue;
240       if ( aHealer.GetParameter( aParam, aValue ) ) {
241         theParams.push_back( aParam.ToCString() );
242         theValues.push_back( aValue.ToCString() );
243       }
244       else
245         nbParamValueErrors++;
246     }
247   }
248   else
249     return false;
250
251   if ( nbParamValueErrors ) {
252     TCollection_AsciiString aMsg ("ERRORS retrieving ShapeProcess parameter values (GEOMImpl_IHealingOperations): nbParamValueErrors = ");
253     aMsg += TCollection_AsciiString( nbParamValueErrors );
254     MESSAGE(aMsg.ToCString());
255   }
256
257   return true;
258 }
259
260 //=============================================================================
261 /*!
262  *  GetParameters
263  */
264 //=============================================================================
265 bool GEOMImpl_IHealingOperations::GetParameters (const std::string theOperation,
266                                                  std::list<std::string>& theParams)
267 {
268   if ( theOperation == "SplitAngle" ) {
269     theParams.push_back( "SplitAngle.Angle" );
270     theParams.push_back( "SplitAngle.MaxTolerance" );
271
272   } else if ( theOperation == "SplitClosedFaces" ) {
273     theParams.push_back( "SplitClosedFaces.NbSplitPoints" );
274
275   } else if ( theOperation == "FixFaceSize" ) {
276     theParams.push_back( "FixFaceSize.Tolerance" );
277
278   } else if( theOperation == "DropSmallEdges" ) {
279     theParams.push_back( "DropSmallEdges.Tolerance3d" );
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
350   //Compute the translation
351   try {
352 #if OCC_VERSION_LARGE > 0x06010000
353     OCC_CATCH_SIGNALS;
354 #endif
355     if (!GetSolver()->ComputeFunction(aFunction))
356     {
357       SetErrorCode("Healing driver failed");
358       return NULL;
359     }
360   }
361   catch (Standard_Failure)
362   {
363     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
364     SetErrorCode(aFail->GetMessageString());
365     return NULL;
366   }
367
368   //Make a Python command
369   GEOM::TPythonDump pd (aFunction);
370   pd << aNewObject << " = geompy.SuppressFaces(" << theObject << ", [";
371
372   // list of face ids
373   int i = theFaces->Lower(), nb = theFaces->Upper();
374   for ( ; i <= nb; i++)
375     pd << theFaces->Value( i ) << (( i < nb ) ? ", " : "])");
376
377   SetErrorCode(OK);
378   return aNewObject;
379 }
380
381 //=============================================================================
382 /*!
383  *  CloseContour
384  */
385 //=============================================================================
386 Handle(GEOM_Object) GEOMImpl_IHealingOperations::CloseContour
387                     (Handle(GEOM_Object) theObject,
388                      const Handle(TColStd_HArray1OfInteger)& theWires,
389                      bool isCommonVertex)
390 {
391   // set error code, check parameters
392   SetErrorCode(KO);
393
394   if (theObject.IsNull())
395   {
396     SetErrorCode("NULL object given");
397     return NULL;
398   }
399
400   Handle(GEOM_Function) aFunction, aLastFunction = theObject->GetLastFunction();
401   if (aLastFunction.IsNull()) return NULL; //There is no function which creates an object to be processed
402
403   // Add a new object
404   Handle(GEOM_Object) aNewObject = GetEngine()->AddObject( GetDocID(), GEOM_COPY );
405
406   //Add the function
407   aFunction = aNewObject->AddFunction(GEOMImpl_HealingDriver::GetID(), CLOSE_CONTOUR);
408
409   if (aFunction.IsNull()) return NULL;
410
411   //Check if the function is set correctly
412   if(aFunction->GetDriverGUID() != GEOMImpl_HealingDriver::GetID()) return NULL;
413
414   // prepare "data container" class IHealing
415   GEOMImpl_IHealing HI(aFunction);
416   HI.SetWires( theWires );
417   HI.SetIsCommonVertex( isCommonVertex );
418   HI.SetOriginal( aLastFunction );
419
420   //Compute the translation
421   try {
422 #if OCC_VERSION_LARGE > 0x06010000
423     OCC_CATCH_SIGNALS;
424 #endif
425     if (!GetSolver()->ComputeFunction(aFunction))
426     {
427       SetErrorCode("Healing driver failed");
428       return NULL;
429     }
430   }
431   catch (Standard_Failure)
432   {
433         Handle(Standard_Failure) aFail = Standard_Failure::Caught();
434     SetErrorCode(aFail->GetMessageString());
435     return NULL;
436   }
437
438   //Make a Python command
439   GEOM::TPythonDump pd (aFunction);
440   pd << aNewObject << " = geompy.CloseContour(" << theObject << ", [";
441
442   // list of wire ids
443   if (!theWires.IsNull())
444   {
445     int i = theWires->Lower(), nb = theWires->Upper();
446     pd << theWires->Value(i++);
447     while (i <= nb)
448       pd << ", " << theWires->Value(i++);
449   }
450   pd << "], " << (int)isCommonVertex << ")";
451
452   SetErrorCode(OK);
453   return aNewObject;
454 }
455
456 //=============================================================================
457 /*!
458  *  RemoveIntWires
459  */
460 //=============================================================================
461 Handle(GEOM_Object) GEOMImpl_IHealingOperations::RemoveIntWires
462        (Handle(GEOM_Object) theObject, const Handle(TColStd_HArray1OfInteger)& theWires)
463 {
464   // set error code, check parameters
465   SetErrorCode(KO);
466
467   if (theObject.IsNull()) // if theWires is NULL it's OK, it means that ALL wires must be removed
468     return NULL;
469
470   Handle(GEOM_Function) aFunction, aLastFunction = theObject->GetLastFunction();
471   if (aLastFunction.IsNull()) return NULL; //There is no function which creates an object to be processed
472
473   // Add a new object
474   Handle(GEOM_Object) aNewObject = GetEngine()->AddObject( GetDocID(), GEOM_COPY );
475
476   //Add the function
477   aFunction = aNewObject->AddFunction(GEOMImpl_HealingDriver::GetID(), REMOVE_INT_WIRES);
478
479   if (aFunction.IsNull()) return NULL;
480
481   //Check if the function is set correctly
482   if (aFunction->GetDriverGUID() != GEOMImpl_HealingDriver::GetID()) return NULL;
483
484   // prepare "data container" class IHealing
485   GEOMImpl_IHealing HI(aFunction);
486   HI.SetWires( theWires );
487   HI.SetOriginal( aLastFunction );
488
489   //Compute the translation
490   try {
491 #if OCC_VERSION_LARGE > 0x06010000
492     OCC_CATCH_SIGNALS;
493 #endif
494     if (!GetSolver()->ComputeFunction(aFunction))
495     {
496       SetErrorCode("Healing driver failed");
497       return NULL;
498     }
499   }
500   catch (Standard_Failure)
501   {
502     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
503     SetErrorCode(aFail->GetMessageString());
504     return NULL;
505   }
506
507   //Make a Python command
508   GEOM::TPythonDump pd (aFunction);
509   pd << aNewObject << " = geompy.SuppressInternalWires(" << theObject << ", [";
510
511   // list of wire ids
512   if (!theWires.IsNull()) {
513     int i = theWires->Lower(), nb = theWires->Upper();
514     for ( ; i <= nb; i++)
515       pd << theWires->Value( i ) << (( i < nb ) ? ", " : "])");
516   } else {
517     pd << "])";
518   }
519
520   SetErrorCode(OK);
521   return aNewObject;
522 }
523
524 //=============================================================================
525 /*!
526  *  FillHoles
527  */
528 //=============================================================================
529 Handle(GEOM_Object) GEOMImpl_IHealingOperations::FillHoles (Handle(GEOM_Object) theObject,
530                                                             const Handle(TColStd_HArray1OfInteger)& theWires)
531 {
532   // set error code, check parameters
533   SetErrorCode(KO);
534
535   if (theObject.IsNull()) // if theWires is NULL it's OK, it means that ALL holes must be removed
536     return NULL;
537
538   Handle(GEOM_Function) aFunction, aLastFunction = theObject->GetLastFunction();
539   if (aLastFunction.IsNull()) return NULL; //There is no function which creates an object to be processed
540
541   // Add a new object
542   Handle(GEOM_Object) aNewObject = GetEngine()->AddObject( GetDocID(), GEOM_COPY );
543
544   //Add the function
545   aFunction = aNewObject->AddFunction(GEOMImpl_HealingDriver::GetID(), FILL_HOLES);
546
547   if (aFunction.IsNull()) return NULL;
548
549   //Check if the function is set correctly
550   if (aFunction->GetDriverGUID() != GEOMImpl_HealingDriver::GetID()) return NULL;
551
552   // prepare "data container" class IHealing
553   GEOMImpl_IHealing HI(aFunction);
554   HI.SetWires( theWires );
555   HI.SetOriginal( aLastFunction );
556
557   //Compute the translation
558   try {
559 #if OCC_VERSION_LARGE > 0x06010000
560     OCC_CATCH_SIGNALS;
561 #endif
562     if (!GetSolver()->ComputeFunction(aFunction))
563     {
564       SetErrorCode("Healing driver failed");
565       return NULL;
566     }
567   }
568   catch (Standard_Failure)
569   {
570         Handle(Standard_Failure) aFail = Standard_Failure::Caught();
571     SetErrorCode(aFail->GetMessageString());
572     return NULL;
573   }
574
575   //Make a Python command
576   GEOM::TPythonDump pd (aFunction);
577   pd << aNewObject << " = geompy.SuppressHoles(" << theObject << ", [";
578
579   // list of wire ids
580   if ( theWires.IsNull() )
581     pd << "])";
582   else {
583     int i = theWires->Lower(), nb = theWires->Upper();
584     for ( ; i <= nb; i++)
585       pd << theWires->Value( i ) << (( i < nb ) ? ", " : "])");
586   }
587
588   SetErrorCode(OK);
589   return aNewObject;
590 }
591
592 //=============================================================================
593 /*!
594  *  Sew
595  */
596 //=============================================================================
597 Handle(GEOM_Object) GEOMImpl_IHealingOperations::Sew (Handle(GEOM_Object) theObject,
598                                                       double theTolerance)
599 {
600   // set error code, check parameters
601   SetErrorCode(KO);
602
603   if (theObject.IsNull())
604     return NULL;
605
606   Handle(GEOM_Function) aFunction, aLastFunction = theObject->GetLastFunction();
607   if (aLastFunction.IsNull()) return NULL; //There is no function which creates an object to be processed
608
609   // Add a new object
610   Handle(GEOM_Object) aNewObject = GetEngine()->AddObject( GetDocID(), GEOM_COPY );
611
612   //Add the function
613   aFunction = aNewObject->AddFunction(GEOMImpl_HealingDriver::GetID(), SEWING);
614
615   if (aFunction.IsNull()) return NULL;
616
617   //Check if the function is set correctly
618   if (aFunction->GetDriverGUID() != GEOMImpl_HealingDriver::GetID()) return NULL;
619
620   // prepare "data container" class IHealing
621   GEOMImpl_IHealing HI(aFunction);
622   HI.SetTolerance( theTolerance );
623   HI.SetOriginal( aLastFunction );
624
625   //Compute the translation
626   try {
627 #if OCC_VERSION_LARGE > 0x06010000
628     OCC_CATCH_SIGNALS;
629 #endif
630     if (!GetSolver()->ComputeFunction(aFunction))
631     {
632       SetErrorCode("Healing driver failed");
633       return NULL;
634     }
635   }
636   catch (Standard_Failure)
637   {
638         Handle(Standard_Failure) aFail = Standard_Failure::Caught();
639     SetErrorCode(aFail->GetMessageString());
640     return NULL;
641   }
642
643   //Make a Python command
644   GEOM::TPythonDump(aFunction) << aNewObject << " = geompy.Sew("
645                                << theObject << ", " << theTolerance << ")";
646
647   SetErrorCode(OK);
648   return aNewObject;
649 }
650
651 //=============================================================================
652 /*!
653  *  DivideEdge
654  */
655 //=============================================================================
656 Handle(GEOM_Object) GEOMImpl_IHealingOperations::DivideEdge (Handle(GEOM_Object) theObject,
657                                                              int theIndex,
658                                                              double theValue,
659                                                              bool isByParameter)
660 {
661   // set error code, check parameters
662   SetErrorCode(KO);
663
664   if (theObject.IsNull())
665     return NULL;
666
667   Handle(GEOM_Function) aFunction, aLastFunction = theObject->GetLastFunction();
668   if (aLastFunction.IsNull()) return NULL; //There is no function which creates an object to be processed
669
670   // Add a new object
671   Handle(GEOM_Object) aNewObject = GetEngine()->AddObject( GetDocID(), GEOM_COPY );
672
673   //Add the function
674   aFunction = aNewObject->AddFunction(GEOMImpl_HealingDriver::GetID(), DIVIDE_EDGE);
675
676   if (aFunction.IsNull()) return NULL;
677
678   //Check if the function is set correctly
679   if (aFunction->GetDriverGUID() != GEOMImpl_HealingDriver::GetID()) return NULL;
680
681   // prepare "data container" class IHealing
682   GEOMImpl_IHealing HI(aFunction);
683   HI.SetIndex( theIndex );
684   HI.SetDevideEdgeValue( theValue );
685   HI.SetIsByParameter( isByParameter );
686   HI.SetOriginal( aLastFunction );
687
688   //Compute the translation
689   try {
690 #if OCC_VERSION_LARGE > 0x06010000
691     OCC_CATCH_SIGNALS;
692 #endif
693     if (!GetSolver()->ComputeFunction(aFunction))
694     {
695       SetErrorCode("Healing driver failed");
696       return NULL;
697     }
698   }
699   catch (Standard_Failure)
700   {
701         Handle(Standard_Failure) aFail = Standard_Failure::Caught();
702     SetErrorCode(aFail->GetMessageString());
703     return NULL;
704   }
705
706   //Make a Python command
707   GEOM::TPythonDump(aFunction) << aNewObject << " = geompy.DivideEdge(" << theObject
708     << ", " << theIndex << ", " << theValue << ", " << (int)isByParameter << ")";
709
710   SetErrorCode(OK);
711   return aNewObject;
712 }
713
714 //=============================================================================
715 /*!
716  *  GetFreeBoundary
717  */
718 //=============================================================================
719 bool GEOMImpl_IHealingOperations::GetFreeBoundary (Handle(GEOM_Object) theObject,
720                                                    Handle(TColStd_HSequenceOfTransient)& theClosed,
721                                                    Handle(TColStd_HSequenceOfTransient)& theOpen )
722 {
723   // set error code, check parameters
724   SetErrorCode(KO);
725
726   if ( theObject.IsNull() || theClosed.IsNull() || theOpen.IsNull() )
727     return false;
728
729   TopoDS_Shape aShape = theObject->GetValue();
730   if ( aShape.IsNull() )
731     return false;
732
733   // get free boundary shapes
734
735 #if OCC_VERSION_LARGE > 0x06030008
736   ShapeAnalysis_FreeBounds anAnalizer(aShape, Standard_False,
737                                       Standard_True, Standard_True);
738 #else
739   ShapeAnalysis_FreeBounds anAnalizer(aShape);
740 #endif
741   TopoDS_Compound aClosed = anAnalizer.GetClosedWires();
742   TopoDS_Compound anOpen = anAnalizer.GetOpenWires();
743
744   // iterate through shapes and append them to the return sequence
745   Handle(GEOM_Object) anObj;
746   Handle(GEOM_Function) aFunction;
747   TopExp_Explorer anExp;
748   for ( anExp.Init( aClosed, TopAbs_WIRE ); anExp.More(); anExp.Next() )
749   {
750     anObj = GetEngine()->AddObject( GetDocID(), GEOM_FREE_BOUNDS );
751     aFunction = anObj->AddFunction( GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF );
752     TopoDS_Shape aValueShape = anExp.Current();
753     aFunction->SetValue( aValueShape );
754     theClosed->Append(anObj);
755   }
756   for ( anExp.Init( anOpen, TopAbs_WIRE ); anExp.More(); anExp.Next() )
757   {
758     anObj = GetEngine()->AddObject( GetDocID(), GEOM_FREE_BOUNDS );
759     aFunction = anObj->AddFunction( GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF );
760     TopoDS_Shape aValueShape = anExp.Current();
761     aFunction->SetValue( aValueShape );
762     theOpen->Append(anObj);
763   }
764
765   if(!aFunction.IsNull()) {
766
767     //Make a Python command
768     GEOM::TPythonDump pd (aFunction);
769
770     Standard_Integer i, aLen = theClosed->Length();
771     if (aLen > 0) {
772       pd << "(isDone, [";
773       for (i = 1; i <= aLen; i++) {
774         Handle(GEOM_Object) anObj_i = Handle(GEOM_Object)::DownCast(theClosed->Value(i));
775         pd << anObj_i << ((i < aLen) ? ", " : "");
776       }
777       pd << "], ";
778     } else {
779       pd << "(isDone, empty_list, ";
780     }
781
782     aLen = theOpen->Length();
783     if (aLen > 0) {
784       pd << "[";
785       for (i = 1; i <= aLen; i++) {
786         Handle(GEOM_Object) anObj_i = Handle(GEOM_Object)::DownCast(theOpen->Value(i));
787         pd << anObj_i << ((i < aLen) ? ", " : "");
788       }
789       pd << "]";
790     } else {
791       pd << "empty_list";
792     }
793
794     pd << ") = geompy.GetFreeBoundary(" << theObject << ")";
795   }
796
797   SetErrorCode(OK);
798   return true;
799 }
800
801
802 //=============================================================================
803 /*!
804  *  ChangeOrientation
805  */
806 //=============================================================================
807 Handle(GEOM_Object) GEOMImpl_IHealingOperations::ChangeOrientation (Handle(GEOM_Object) theObject)
808 {
809   // set error code, check parameters
810   SetErrorCode(KO);
811
812   if (theObject.IsNull())
813     return NULL;
814
815   if (!theObject->IsMainShape()) {
816     SetErrorCode("Sub shape cannot be transformed - need to create a copy");
817     return NULL;
818   }
819
820   Handle(GEOM_Function) aFunction, aLastFunction = theObject->GetLastFunction();
821   if (aLastFunction.IsNull())
822     return NULL; //There is no function which creates an object to be processed
823
824   if (theObject->GetType() == GEOM_VECTOR) { // Mantis issue 21066
825     //Add the function
826     aFunction = theObject->AddFunction(GEOMImpl_VectorDriver::GetID(), VECTOR_REVERSE);
827
828     //Check if the function is set correctly
829     if (aFunction.IsNull()) return NULL;
830     if (aFunction->GetDriverGUID() != GEOMImpl_VectorDriver::GetID()) return NULL;
831
832     // prepare "data container" class IVector
833     GEOMImpl_IVector aVI (aFunction);
834     aVI.SetCurve(aLastFunction);
835   }
836   else {
837     //Add the function
838     aFunction = theObject->AddFunction(GEOMImpl_HealingDriver::GetID(), CHANGE_ORIENTATION);
839
840     //Check if the function is set correctly
841     if (aFunction.IsNull()) return NULL;
842     if (aFunction->GetDriverGUID() != GEOMImpl_HealingDriver::GetID()) return NULL;
843
844     // prepare "data container" class IHealing
845     GEOMImpl_IHealing HI (aFunction);
846     HI.SetOriginal(aLastFunction);
847   }
848
849   //Compute the translation
850   try {
851 #if OCC_VERSION_LARGE > 0x06010000
852     OCC_CATCH_SIGNALS;
853 #endif
854     if (!GetSolver()->ComputeFunction(aFunction)) {
855       SetErrorCode("Healing driver failed");
856       return NULL;
857     }
858   }
859   catch (Standard_Failure) {
860     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
861     SetErrorCode(aFail->GetMessageString());
862     return NULL;
863   }
864
865   //Make a Python command
866   GEOM::TPythonDump(aFunction) << "geompy.ChangeOrientationShell("
867                                << theObject << ")";
868
869   SetErrorCode(OK);
870   return theObject;
871 }
872
873 //=============================================================================
874 /*!
875  *  ChangeOrientationCopy
876  */
877 //=============================================================================
878 Handle(GEOM_Object) GEOMImpl_IHealingOperations::ChangeOrientationCopy (Handle(GEOM_Object) theObject)
879 {
880   // set error code, check parameters
881   SetErrorCode(KO);
882
883   if (theObject.IsNull())
884     return NULL;
885
886   Handle(GEOM_Function) aFunction, aLastFunction = theObject->GetLastFunction();
887   if (aLastFunction.IsNull())
888     return NULL; //There is no function which creates an object to be processed
889
890   // Add a new object
891   Handle(GEOM_Object) aNewObject = GetEngine()->AddObject(GetDocID(), theObject->GetType());
892
893   if (theObject->GetType() == GEOM_VECTOR) { // Mantis issue 21066
894     //Add the function
895     aFunction = aNewObject->AddFunction(GEOMImpl_VectorDriver::GetID(), VECTOR_REVERSE);
896
897     //Check if the function is set correctly
898     if (aFunction.IsNull()) return NULL;
899     if (aFunction->GetDriverGUID() != GEOMImpl_VectorDriver::GetID()) return NULL;
900
901     // prepare "data container" class IVector
902     GEOMImpl_IVector aVI (aFunction);
903     aVI.SetCurve(aLastFunction);
904   }
905   else {
906     //Add the function
907     aFunction = aNewObject->AddFunction(GEOMImpl_HealingDriver::GetID(), CHANGE_ORIENTATION);
908
909     //Check if the function is set correctly
910     if (aFunction.IsNull()) return NULL;
911     if (aFunction->GetDriverGUID() != GEOMImpl_HealingDriver::GetID()) return NULL;
912
913     // prepare "data container" class IHealing
914     GEOMImpl_IHealing aHI (aFunction);
915     aHI.SetOriginal(aLastFunction);
916   }
917
918   // Compute the result
919   try {
920 #if OCC_VERSION_LARGE > 0x06010000
921     OCC_CATCH_SIGNALS;
922 #endif
923     if (!GetSolver()->ComputeFunction(aFunction)) {
924       SetErrorCode("Healing driver failed");
925       return NULL;
926     }
927   }
928   catch (Standard_Failure) {
929     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
930     SetErrorCode(aFail->GetMessageString());
931     return NULL;
932   }
933
934   //Make a Python command
935   GEOM::TPythonDump(aFunction) << aNewObject << " = geompy.ChangeOrientationShellCopy("
936                                << theObject << ")";
937
938   SetErrorCode(OK);
939   return aNewObject;
940 }
941
942 //=============================================================================
943 /*!
944  *  LimitTolerance
945  */
946 //=============================================================================
947 Handle(GEOM_Object) GEOMImpl_IHealingOperations::LimitTolerance (Handle(GEOM_Object) theObject,
948                                                                  double theTolerance)
949 {
950   // Set error code, check parameters
951   SetErrorCode(KO);
952
953   if (theObject.IsNull())
954     return NULL;
955
956   Handle(GEOM_Function) aFunction, aLastFunction = theObject->GetLastFunction();
957   if (aLastFunction.IsNull())
958     return NULL; // There is no function which creates an object to be processed
959
960   // Add a new object
961   Handle(GEOM_Object) aNewObject = GetEngine()->AddObject(GetDocID(), theObject->GetType());
962
963   // Add the function
964   aFunction = aNewObject->AddFunction(GEOMImpl_HealingDriver::GetID(), LIMIT_TOLERANCE);
965
966   if (aFunction.IsNull())
967     return NULL;
968
969   // Check if the function is set correctly
970   if (aFunction->GetDriverGUID() != GEOMImpl_HealingDriver::GetID()) return NULL;
971
972   // Prepare "data container" class IHealing
973   GEOMImpl_IHealing HI (aFunction);
974   HI.SetOriginal(aLastFunction);
975   HI.SetTolerance(theTolerance);
976
977   // Compute
978   try {
979 #if OCC_VERSION_LARGE > 0x06010000
980     OCC_CATCH_SIGNALS;
981 #endif
982     if (!GetSolver()->ComputeFunction(aFunction)) {
983       SetErrorCode("Healing driver failed");
984       return NULL;
985     }
986   }
987   catch (Standard_Failure) {
988     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
989     SetErrorCode(aFail->GetMessageString());
990     return NULL;
991   }
992
993   // Make a Python command
994   GEOM::TPythonDump(aFunction) << aNewObject << " = geompy.LimitTolerance("
995                                << theObject << ", " << theTolerance << ")";
996
997   SetErrorCode(OK);
998   return aNewObject;
999 }