Salome HOME
e845c72a71a9f396ff63766943f0ec978015fdb7
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ChamferDriver.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 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_ChamferDriver.hxx>
26 #include <GEOMImpl_IChamfer.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOMImpl_ILocalOperations.hxx>
29 #include <GEOMImpl_Block6Explorer.hxx>
30
31 #include <GEOM_Function.hxx>
32
33 #include <BRepLib.hxx>
34 #include <BRep_Tool.hxx>
35 #include <BRepTools.hxx>
36 #include <BRepFilletAPI_MakeChamfer.hxx>
37
38 #include <ShapeFix_Shape.hxx>
39 #include <ShapeFix_ShapeTolerance.hxx>
40
41 #include <TopAbs.hxx>
42 #include <TopoDS.hxx>
43 #include <TopoDS_Edge.hxx>
44 #include <TopoDS_Face.hxx>
45 #include <TopoDS_Shape.hxx>
46 #include <TopoDS_Iterator.hxx>
47 #include <TopExp.hxx>
48 #include <TopExp_Explorer.hxx>
49 #include <TopTools_MapOfShape.hxx>
50 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
51
52 #include <Precision.hxx>
53 #include <gp_Pnt.hxx>
54 #include <StdFail_NotDone.hxx>
55
56 //=======================================================================
57 //function : GetID
58 //purpose  :
59 //=======================================================================
60 const Standard_GUID& GEOMImpl_ChamferDriver::GetID()
61 {
62   static Standard_GUID aChamferDriver("FF1BBB42-5D14-4df2-980B-3A668264EA16");
63   return aChamferDriver;
64 }
65
66
67 //=======================================================================
68 //function : GEOMImpl_ChamferDriver
69 //purpose  :
70 //=======================================================================
71 GEOMImpl_ChamferDriver::GEOMImpl_ChamferDriver()
72 {
73 }
74
75 //=======================================================================
76 //function : isGoodForChamfer
77 //purpose  :
78 //=======================================================================
79 static Standard_Boolean isGoodForChamfer (const TopoDS_Shape& theShape)
80 {
81   if (theShape.ShapeType() == TopAbs_SHELL ||
82       theShape.ShapeType() == TopAbs_SOLID ||
83       theShape.ShapeType() == TopAbs_COMPSOLID) {
84     return Standard_True;
85   }
86
87   if (theShape.ShapeType() == TopAbs_COMPOUND) {
88     TopTools_MapOfShape mapShape;
89     TopoDS_Iterator It (theShape, Standard_False, Standard_False);
90     for (; It.More(); It.Next()) {
91       if (mapShape.Add(It.Value())) {
92         if (!isGoodForChamfer(It.Value())) {
93           return Standard_False;
94         }
95       }
96     }
97     return Standard_True;
98   }
99
100   return Standard_False;
101 }
102
103 //=======================================================================
104 //function : Execute
105 //purpose  :
106 //=======================================================================
107 Standard_Integer GEOMImpl_ChamferDriver::Execute(TFunction_Logbook& log) const
108 {
109   if (Label().IsNull()) return 0;
110   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
111
112   GEOMImpl_IChamfer aCI (aFunction);
113   Standard_Integer aType = aFunction->GetType();
114
115   TopoDS_Shape aShape;
116
117   Handle(GEOM_Function) aRefShape = aCI.GetShape();
118   TopoDS_Shape aShapeBase = aRefShape->GetValue();
119
120   // Check the shape type. It have to be shell
121   // or solid, or compsolid, or compound of these shapes.
122   if (!isGoodForChamfer(aShapeBase)) {
123     StdFail_NotDone::Raise
124       ("Wrong shape. Must be shell or solid, or compsolid or compound of these shapes");
125   }
126
127   BRepFilletAPI_MakeChamfer fill (aShapeBase);
128
129   if (aType == CHAMFER_SHAPE_ALL) {
130     // symmetric chamfer on all edges
131     double aD = aCI.GetD();
132     TopTools_IndexedDataMapOfShapeListOfShape M;
133     GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
134     for (int i = 1; i <= M.Extent(); i++) {
135       TopoDS_Edge E = TopoDS::Edge(M.FindKey(i));
136       TopoDS_Face F = TopoDS::Face(M.FindFromIndex(i).First());
137       if (!BRepTools::IsReallyClosed(E, F) &&
138           !BRep_Tool::Degenerated(E) &&
139           M.FindFromIndex(i).Extent() == 2)
140         fill.Add(aD, E, F);
141     }
142   }
143   else if (aType == CHAMFER_SHAPE_EDGE || aType == CHAMFER_SHAPE_EDGE_AD) {
144     // chamfer on edges, common to two faces, with D1 on the first face
145
146     TopoDS_Shape aFace1, aFace2;
147     if (GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace1(), aFace1) &&
148         GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace2(), aFace2))
149     {
150       TopoDS_Face F = TopoDS::Face(aFace1);
151
152       // fill map of edges of the second face
153       TopTools_MapOfShape aMap;
154       TopExp_Explorer Exp2 (aFace2, TopAbs_EDGE);
155       for (; Exp2.More(); Exp2.Next()) {
156         aMap.Add(Exp2.Current());
157       }
158
159       // find edges of the first face, common with the second face
160       TopExp_Explorer Exp (aFace1, TopAbs_EDGE);
161       for (; Exp.More(); Exp.Next()) {
162         if (aMap.Contains(Exp.Current())) {
163           TopoDS_Edge E = TopoDS::Edge(Exp.Current());
164           if (!BRepTools::IsReallyClosed(E, F) && !BRep_Tool::Degenerated(E))
165           {
166             if ( aType == CHAMFER_SHAPE_EDGE )
167             {
168               double aD1 = aCI.GetD1();
169               double aD2 = aCI.GetD2();
170               fill.Add(aD1, aD2, E, F);
171             }
172             else
173             {
174               double aD = aCI.GetD();
175               double anAngle = aCI.GetAngle();
176               if ( (anAngle > 0) && (anAngle < (M_PI/2.)) )
177                 fill.AddDA(aD, anAngle, E, F);
178             }
179           }
180         }
181       }
182     }
183   }
184   else if (aType == CHAMFER_SHAPE_FACES || aType == CHAMFER_SHAPE_FACES_AD) {
185     // chamfer on all edges of the selected faces, with D1 on the selected face
186     // (on first selected face, if the edge belongs to two selected faces)
187
188     int aLen = aCI.GetLength();
189     int ind = 1;
190     TopTools_MapOfShape aMap;
191     TopTools_IndexedDataMapOfShapeListOfShape M;
192     GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
193     for (; ind <= aLen; ind++)
194     {
195       TopoDS_Shape aShapeFace;
196       if (GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace(ind), aShapeFace))
197       {
198         TopoDS_Face F = TopoDS::Face(aShapeFace);
199         TopExp_Explorer Exp (F, TopAbs_EDGE);
200         for (; Exp.More(); Exp.Next()) {
201           if (!aMap.Contains(Exp.Current()))
202           {
203             TopoDS_Edge E = TopoDS::Edge(Exp.Current());
204             if (!BRepTools::IsReallyClosed(E, F) &&
205                 !BRep_Tool::Degenerated(E) &&
206                 M.FindFromKey(E).Extent() == 2)
207             {
208               if (aType == CHAMFER_SHAPE_FACES)
209               {
210                 double aD1 = aCI.GetD1();
211                 double aD2 = aCI.GetD2();
212                 fill.Add(aD1, aD2, E, F);
213               }
214               else
215               {
216                 double aD = aCI.GetD();
217                 double anAngle = aCI.GetAngle();
218                 if ( (anAngle > 0) && (anAngle < (M_PI/2.)) )
219                   fill.AddDA(aD, anAngle, E, F);
220               }
221             }
222           }
223         }
224       }
225     }
226   }
227   else if (aType == CHAMFER_SHAPE_EDGES || aType == CHAMFER_SHAPE_EDGES_AD)
228   {
229     // chamfer on selected edges with lenght param D1 & D2.
230
231     int aLen = aCI.GetLength();
232     int ind = 1;
233     TopTools_MapOfShape aMap;
234     TopTools_IndexedDataMapOfShapeListOfShape M;
235     GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
236     for (; ind <= aLen; ind++)
237     {
238       TopoDS_Shape aShapeEdge;
239       if (GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetEdge(ind), aShapeEdge))
240       {
241         TopoDS_Edge E = TopoDS::Edge(aShapeEdge);
242         const TopTools_ListOfShape& aFacesList = M.FindFromKey(E);
243         TopoDS_Face F = TopoDS::Face( aFacesList.First() );
244         if (aType == CHAMFER_SHAPE_EDGES)
245         {
246           double aD1 = aCI.GetD1();
247           double aD2 = aCI.GetD2();
248           fill.Add(aD1, aD2, E, F);
249         }
250         else
251         {
252           double aD = aCI.GetD();
253           double anAngle = aCI.GetAngle();
254           if ( (anAngle > 0) && (anAngle < (M_PI/2.)) )
255             fill.AddDA(aD, anAngle, E, F);
256         }
257       }
258     }
259   }
260   else {
261   }
262
263   fill.Build();
264   if (!fill.IsDone()) {
265     StdFail_NotDone::Raise("Chamfer can not be computed on the given shape with the given parameters");
266   }
267   aShape = fill.Shape();
268
269   if (aShape.IsNull()) return 0;
270
271   // reduce tolerances
272   ShapeFix_ShapeTolerance aSFT;
273   aSFT.LimitTolerance(aShape, Precision::Confusion(),
274                       Precision::Confusion(), TopAbs_SHAPE);
275   Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
276   aSfs->Perform();
277   aShape = aSfs->Shape();
278
279   // fix SameParameter flag
280   BRepLib::SameParameter(aShape, 1.E-5, Standard_True);
281
282   aFunction->SetValue(aShape);
283
284   log.SetTouched(Label());
285
286   return 1;
287 }
288
289 //================================================================================
290 /*!
291  * \brief Returns a name of creation operation and names and values of creation parameters
292  */
293 //================================================================================
294
295 bool GEOMImpl_ChamferDriver::
296 GetCreationInformation(std::string&             theOperationName,
297                        std::vector<GEOM_Param>& theParams)
298 {
299   if (Label().IsNull()) return 0;
300   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
301
302   GEOMImpl_IChamfer aCI( function );
303   Standard_Integer aType = function->GetType();
304
305   theOperationName = "CHAMFER";
306
307   switch ( aType ) {
308   case CHAMFER_SHAPE_ALL:
309     AddParam( theParams, "Main Object", aCI.GetShape() );
310     AddParam( theParams, "Selected Edges", "all" );
311     AddParam( theParams, "D", aCI.GetD() );
312     break;
313   case CHAMFER_SHAPE_EDGE:
314     AddParam( theParams, "Main Object", aCI.GetShape() );
315     AddParam( theParams, "Face 1", aCI.GetFace1() );
316     AddParam( theParams, "Face 2", aCI.GetFace2() );
317     AddParam( theParams, "D1", aCI.GetD1() );
318     AddParam( theParams, "D2", aCI.GetD2() );
319     break;
320   case CHAMFER_SHAPE_EDGE_AD:
321     AddParam( theParams, "Main Object", aCI.GetShape() );
322     AddParam( theParams, "Face 1", aCI.GetFace1() );
323     AddParam( theParams, "Face 2", aCI.GetFace2() );
324     AddParam( theParams, "D", aCI.GetD() );
325     AddParam( theParams, "Angle", aCI.GetAngle() );
326     break;
327   case CHAMFER_SHAPE_FACES:
328     AddParam( theParams, "Main Object", aCI.GetShape() );
329     AddParam( theParams, "Selected Faces" );
330     if ( aCI.GetLength() > 1 )
331       theParams[1] << aCI.GetLength() << " faces: ";
332     for ( int i = 1, nb = aCI.GetLength(); i <= nb; ++i )
333       theParams[1] << aCI.GetFace(i) << " ";
334     AddParam( theParams, "D1", aCI.GetD1() );
335     AddParam( theParams, "D2", aCI.GetD2() );
336     break;
337   case CHAMFER_SHAPE_FACES_AD:
338     AddParam( theParams, "Main Object", aCI.GetShape() );
339     AddParam( theParams, "Selected Faces" );
340     if ( aCI.GetLength() > 1 )
341       theParams[1] << aCI.GetLength() << " faces: ";
342     for ( int i = 1, nb = aCI.GetLength(); i <= nb; ++i )
343       theParams[1] << aCI.GetFace(i) << " ";
344     AddParam( theParams, "D", aCI.GetD() );
345     AddParam( theParams, "Angle", aCI.GetAngle() );
346     break;
347   case CHAMFER_SHAPE_EDGES:
348     AddParam( theParams, "Main Object", aCI.GetShape() );
349     AddParam( theParams, "Selected Edges" );
350     if ( aCI.GetLength() > 1 )
351       theParams[1] << aCI.GetLength() << " edges: ";
352     for ( int i = 1, nb = aCI.GetLength(); i <= nb; ++i )
353       theParams[1] << aCI.GetEdge(i) << " ";
354     AddParam( theParams, "D1", aCI.GetD1() );
355     AddParam( theParams, "D2", aCI.GetD2() );
356     break;
357   case CHAMFER_SHAPE_EDGES_AD:
358     AddParam( theParams, "Main Object", aCI.GetShape() );
359     AddParam( theParams, "Selected Edges" );
360     if ( aCI.GetLength() > 1 )
361       theParams[1] << aCI.GetLength() << " edges: ";
362     for ( int i = 1, nb = aCI.GetLength(); i <= nb; ++i )
363       theParams[1] << aCI.GetFace(i) << " ";
364     AddParam( theParams, "D", aCI.GetD() );
365     AddParam( theParams, "Angle", aCI.GetAngle() );
366     break;
367   default:
368     return false;
369   }
370
371   return true;
372 }
373
374 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_ChamferDriver,GEOM_BaseDriver);
375
376 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_ChamferDriver,GEOM_BaseDriver);