Salome HOME
Merge with version on tag OCC-V2_1_0d
[modules/geom.git] / src / GEOMImpl / GEOMImpl_HealingDriver.cxx
1
2 using namespace std;
3 #include "GEOMImpl_HealingDriver.hxx"
4 #include "GEOMImpl_Types.hxx"
5 #include "GEOMImpl_IHealing.hxx"
6 #include "GEOM_Function.hxx"
7
8 #include "ShHealOper_ShapeProcess.hxx"
9 #include "ShHealOper_RemoveFace.hxx"
10 #include "ShHealOper_CloseContour.hxx"
11 #include "ShHealOper_RemoveInternalWires.hxx"
12 #include "ShHealOper_FillHoles.hxx"
13 #include "ShHealOper_Sewing.hxx"
14 #include "ShHealOper_EdgeDivide.hxx"
15
16 #include <TopoDS.hxx>
17 #include <TopExp.hxx>
18 #include <TopTools_IndexedMapOfShape.hxx>
19
20 #include <StdFail_NotDone.hxx>
21
22 //=======================================================================
23 //function :  raiseNotDoneExeption
24 //purpose  :  global function: forms error message and raises exeption
25 //=======================================================================
26 void raiseNotDoneExeption( const int theErrorStatus )
27 {
28   switch ( theErrorStatus )
29   {
30   case ShHealOper_NotError:           StdFail_NotDone::Raise( "ShHealOper_NotError_msg" );
31   case ShHealOper_InvalidParameters:  StdFail_NotDone::Raise( "ShHealOper_InvalidParameters_msg" );
32   case ShHealOper_ErrorExecution:
33   default:                            StdFail_NotDone::Raise( "ShHealOper_ErrorExecution_msg" );
34   }
35 }
36
37 //=======================================================================
38 //function : GetID
39 //purpose  :
40 //=======================================================================
41 const Standard_GUID& GEOMImpl_HealingDriver::GetID()
42 {
43   static Standard_GUID aHealingDriver("FF1BBB61-5D14-4df2-980B-3A668264EA16");
44   return aHealingDriver;
45 }
46
47
48 //=======================================================================
49 //function : GEOMImpl_HealingDriver
50 //purpose  :
51 //=======================================================================
52
53 GEOMImpl_HealingDriver::GEOMImpl_HealingDriver()
54 {
55 }
56
57 //=======================================================================
58 //function : Execute
59 //purpose  :
60 //=======================================================================
61 Standard_Integer GEOMImpl_HealingDriver::Execute(TFunction_Logbook& log) const
62 {
63   if (Label().IsNull()) return 0;
64   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
65
66   if (aFunction.IsNull()) return 0;
67
68   GEOMImpl_IHealing HI (aFunction);
69   Standard_Integer aType = aFunction->GetType();
70   Handle(GEOM_Function) anOriginalFunction = HI.GetOriginal();
71   if (anOriginalFunction.IsNull()) return 0;
72   TopoDS_Shape aShape, anOriginalShape = anOriginalFunction->GetValue();
73   if (anOriginalShape.IsNull()) return 0;
74
75   switch (aType)
76   {
77   case SHAPE_PROCESS:
78     ShapeProcess(&HI, anOriginalShape, aShape);
79     break;
80   case SUPPRESS_FACES:
81     SuppressFaces(&HI, anOriginalShape, aShape);
82     break;
83   case CLOSE_CONTOUR:
84     CloseContour(&HI, anOriginalShape, aShape);
85     break;
86   case REMOVE_INT_WIRES:
87     RemoveIntWires(&HI, anOriginalShape, aShape);
88     break;
89   case FILL_HOLES:
90     RemoveHoles(&HI, anOriginalShape, aShape);
91     break;
92   case SEWING:
93     Sew(&HI, anOriginalShape, aShape);
94     break;
95   case DIVIDE_EDGE:
96     AddPointOnEdge(&HI, anOriginalShape, aShape);
97     break;
98   default:
99     return 0;
100   }
101
102   if (aShape.IsNull())
103     raiseNotDoneExeption( ShHealOper_ErrorExecution );
104
105   aFunction->SetValue(aShape);
106
107   log.SetTouched(Label());
108   return 1;
109 }
110
111 //=======================================================================
112 //function :  ShapeProcess
113 //purpose  :
114 //=======================================================================
115 Standard_Boolean GEOMImpl_HealingDriver::ShapeProcess (GEOMImpl_IHealing* theHI,
116                                                        const TopoDS_Shape& theOriginalShape,
117                                                        TopoDS_Shape& theOutShape) const
118 {
119   Handle(TColStd_HArray1OfExtendedString) anOperators = theHI->GetOperators();
120   Handle(TColStd_HArray1OfExtendedString) aParams = theHI->GetParameters();
121   Handle(TColStd_HArray1OfExtendedString) aValues = theHI->GetValues();
122
123   if (anOperators.IsNull() || anOperators->Length() <= 0)
124     return Standard_False;
125
126   Standard_Integer nbParams = 0, nbValues = 0;
127   if (!aParams.IsNull()) {
128     nbParams = aParams->Length();
129   }
130   if (!aValues.IsNull()) {
131     nbValues = aValues->Length();
132   }
133   if (nbParams != nbValues)
134     return Standard_False;
135
136   ShHealOper_ShapeProcess aHealer;
137   TColStd_SequenceOfAsciiString anOperatorsAS, aParamsAS, aValuesAS;
138   int i;
139   for (i = 1; i <= anOperators->Length(); i++)
140     anOperatorsAS.Append(TCollection_AsciiString(anOperators->Value(i)));
141
142   aHealer.SetOperators(anOperatorsAS);
143
144   for (i = 1; i <= nbParams; i++) {
145     aHealer.SetParameter(TCollection_AsciiString(aParams->Value(i)),
146                          TCollection_AsciiString(aValues->Value(i)));
147   }
148
149   aHealer.Perform(theOriginalShape, theOutShape);
150
151   if (!aHealer.isDone())
152     raiseNotDoneExeption( ShHealOper_NotError );
153
154   return Standard_True;
155 }
156
157 //=======================================================================
158 //function :  SupressFaces
159 //purpose  :
160 //=======================================================================
161 Standard_Boolean GEOMImpl_HealingDriver::SuppressFaces (GEOMImpl_IHealing* theHI,
162                                                         const TopoDS_Shape& theOriginalShape,
163                                                         TopoDS_Shape& theOutShape) const
164 {
165   Handle(TColStd_HArray1OfInteger) aFaces = theHI->GetFaces();
166
167   ShHealOper_RemoveFace aHealer (theOriginalShape);
168
169   Standard_Boolean aResult = Standard_False;
170   if (aFaces.IsNull()) // remove all faces
171   {
172     aResult = aHealer.Perform();
173   } else {
174     TopTools_SequenceOfShape aShapesFaces;
175     TopTools_IndexedMapOfShape aShapes;
176     TopExp::MapShapes(theOriginalShape, aShapes);
177     for (int i = 1; i <= aFaces->Length(); i++) {
178       int indexOfFace = aFaces->Value(i);
179       TopoDS_Shape aFace = aShapes.FindKey(indexOfFace);
180       aShapesFaces.Append(aFace);
181     }
182
183     aResult = aHealer.Perform(aShapesFaces);
184   }
185
186   if ( aResult )
187     theOutShape = aHealer.GetResultShape();
188   else
189     raiseNotDoneExeption( aHealer.GetErrorStatus() );
190
191   return aResult;
192 }
193
194 //=======================================================================
195 //function :  CloseContour
196 //purpose  :
197 //=======================================================================
198 Standard_Boolean GEOMImpl_HealingDriver::CloseContour (GEOMImpl_IHealing* theHI,
199                                                        const TopoDS_Shape& theOriginalShape,
200                                                        TopoDS_Shape& theOutShape) const
201 {
202   Standard_Boolean isByVertex = theHI->GetIsCommonVertex();
203   Handle(TColStd_HArray1OfInteger) aWires = theHI->GetWires();
204
205   ShHealOper_CloseContour aHealer (theOriginalShape);
206
207   Standard_Boolean aResult = Standard_False;
208   if ( aWires.IsNull() ) {
209     if ( theOriginalShape.ShapeType() == TopAbs_WIRE )
210       aResult = aHealer.Perform(TopoDS::Wire(theOriginalShape), isByVertex, !isByVertex);
211   }
212   else {
213     TopTools_SequenceOfShape aShapesWires;
214     TopTools_IndexedMapOfShape aShapes;
215     TopExp::MapShapes(theOriginalShape, aShapes);
216     for (int i = 1; i <= aWires->Length(); i++) {
217       int indexOfWire = aWires->Value(i);
218       TopoDS_Shape aWire = aShapes.FindKey(indexOfWire);
219       aShapesWires.Append(aWire);
220     }
221
222     aResult = aHealer.Perform( aShapesWires, isByVertex, !isByVertex );
223   }
224
225   if (aResult)
226     theOutShape = aHealer.GetResultShape();
227   else
228     raiseNotDoneExeption( aHealer.GetErrorStatus() );
229
230   return aResult;
231 }
232
233 //=======================================================================
234 //function :  RemoveIntWires
235 //purpose  :
236 //=======================================================================
237 Standard_Boolean GEOMImpl_HealingDriver::RemoveIntWires (GEOMImpl_IHealing* theHI,
238                                                          const TopoDS_Shape& theOriginalShape,
239                                                          TopoDS_Shape& theOutShape) const
240 {
241   Handle(TColStd_HArray1OfInteger) aWires = theHI->GetWires();
242
243   ShHealOper_RemoveInternalWires aHealer(theOriginalShape);
244
245   Standard_Boolean aResult = Standard_False;
246   if (aWires.IsNull()) { // remove all faces
247     aResult = aHealer.Remove();
248   } else {
249     TopTools_SequenceOfShape aShapesWires;
250     TopTools_IndexedMapOfShape aShapes;
251     TopExp::MapShapes(theOriginalShape, aShapes);
252     for (int i = 1; i <= aWires->Length(); i++) {
253       int indexOfWire = aWires->Value(i);
254       TopoDS_Shape aWire = aShapes.FindKey(indexOfWire);
255       aShapesWires.Append(aWire);
256     }
257
258     aResult = aHealer.Remove(aShapesWires);
259   }
260
261   if (aResult)
262     theOutShape = aHealer.GetResultShape();
263   else
264     raiseNotDoneExeption( aHealer.GetErrorStatus() );
265
266   return aResult;
267 }
268
269 //=======================================================================
270 //function :  RemoveHoles
271 //purpose  :
272 //=======================================================================
273 Standard_Boolean GEOMImpl_HealingDriver::RemoveHoles (GEOMImpl_IHealing* theHI,
274                                                       const TopoDS_Shape& theOriginalShape,
275                                                       TopoDS_Shape& theOutShape) const
276 {
277   Handle(TColStd_HArray1OfInteger) aWires = theHI->GetWires();
278
279   ShHealOper_FillHoles aHealer (theOriginalShape);
280
281   Standard_Boolean aResult = Standard_False;
282   if (aWires.IsNull()) { // remove all faces
283     aResult = aHealer.Fill();
284   } else {
285     TopTools_SequenceOfShape aShapesWires;
286     TopTools_IndexedMapOfShape aShapes;
287     TopExp::MapShapes(theOriginalShape, aShapes);
288     for (int i = 1; i <= aWires->Length(); i++) {
289       int indexOfWire = aWires->Value(i);
290       TopoDS_Shape aWire = aShapes.FindKey(indexOfWire);
291       aShapesWires.Append(aWire);
292     }
293
294     aResult = aHealer.Fill(aShapesWires);
295   }
296
297   if (aResult)
298     theOutShape = aHealer.GetResultShape();
299   else
300     raiseNotDoneExeption( aHealer.GetErrorStatus() );
301
302   return aResult;
303 }
304
305 //=======================================================================
306 //function :  Sew
307 //purpose  :
308 //=======================================================================
309 Standard_Boolean GEOMImpl_HealingDriver::Sew (GEOMImpl_IHealing* theHI,
310                                               const TopoDS_Shape& theOriginalShape,
311                                               TopoDS_Shape& theOutShape) const
312 {
313   Standard_Real aTol = theHI->GetTolerance();
314
315   ShHealOper_Sewing aHealer (theOriginalShape, aTol);
316
317   Standard_Boolean aResult = aHealer.Perform();
318
319   if (aResult)
320     theOutShape = aHealer.GetResultShape();
321   else
322     raiseNotDoneExeption( aHealer.GetErrorStatus() );
323
324   return aResult;
325 }
326
327 //=======================================================================
328 //function :  AddPointOnEdge
329 //purpose  :
330 //=======================================================================
331 Standard_Boolean GEOMImpl_HealingDriver::AddPointOnEdge (GEOMImpl_IHealing* theHI,
332                                                          const TopoDS_Shape& theOriginalShape,
333                                                          TopoDS_Shape& theOutShape) const
334 {
335   Standard_Boolean isByParameter = theHI->GetIsByParameter();
336   Standard_Integer anIndex = theHI->GetIndex();
337   Standard_Real aValue = theHI->GetDevideEdgeValue();
338
339   ShHealOper_EdgeDivide aHealer (theOriginalShape);
340
341   Standard_Boolean aResult = Standard_False;
342   if (anIndex == -1) { // apply algorythm for the whole shape which is EDGE
343     if (theOriginalShape.ShapeType() == TopAbs_EDGE)
344       aResult = aHealer.Perform(TopoDS::Edge(theOriginalShape), aValue, isByParameter);
345   } else {
346     TopTools_IndexedMapOfShape aShapes;
347     TopExp::MapShapes(theOriginalShape, aShapes);
348     TopoDS_Shape aEdgeShape = aShapes.FindKey(anIndex);
349     if (aEdgeShape.ShapeType() == TopAbs_EDGE)
350       aResult = aHealer.Perform(TopoDS::Edge(aEdgeShape), aValue, isByParameter);
351   }
352
353   if (aResult)
354     theOutShape = aHealer.GetResultShape();
355   else
356     raiseNotDoneExeption( aHealer.GetErrorStatus() );
357
358   return aResult;
359 }
360
361
362 //=======================================================================
363 //function :  GEOMImpl_HealingDriver_Type_
364 //purpose  :
365 //=======================================================================
366 Standard_EXPORT Handle_Standard_Type& GEOMImpl_HealingDriver_Type_()
367 {
368
369   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
370   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
371   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
372   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
373   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
374   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
375
376
377   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
378   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_HealingDriver",
379                                                          sizeof(GEOMImpl_HealingDriver),
380                                                          1,
381                                                          (Standard_Address)_Ancestors,
382                                                          (Standard_Address)NULL);
383
384   return _aType;
385 }
386
387 //=======================================================================
388 //function : DownCast
389 //purpose  :
390 //=======================================================================
391
392 const Handle(GEOMImpl_HealingDriver) Handle(GEOMImpl_HealingDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
393 {
394   Handle(GEOMImpl_HealingDriver) _anOtherObject;
395
396   if (!AnObject.IsNull()) {
397      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_HealingDriver))) {
398        _anOtherObject = Handle(GEOMImpl_HealingDriver)((Handle(GEOMImpl_HealingDriver)&)AnObject);
399      }
400   }
401
402   return _anOtherObject ;
403 }
404
405