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