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