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