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