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