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