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