Salome HOME
c9df2274afefde9b8aa32302b48a5de7590798de
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ChamferDriver.cxx
1 // Copyright (C) 2007-2023  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         fill.Add(aD, aD, E, F);
134     }
135   }
136   else if (aType == CHAMFER_SHAPE_EDGE || aType == CHAMFER_SHAPE_EDGE_AD) {
137     // chamfer on edges, common to two faces, with D1 on the first face
138
139     TopoDS_Shape aFace1, aFace2;
140     if (GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace1(), aFace1) &&
141         GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace2(), aFace2))
142     {
143       TopoDS_Face F = TopoDS::Face(aFace1);
144
145       // fill map of edges of the second face
146       TopTools_MapOfShape aMap;
147       TopExp_Explorer Exp2 (aFace2, TopAbs_EDGE);
148       for (; Exp2.More(); Exp2.Next()) {
149         aMap.Add(Exp2.Current());
150       }
151
152       // find edges of the first face, common with the second face
153       TopExp_Explorer Exp (aFace1, TopAbs_EDGE);
154       for (; Exp.More(); Exp.Next()) {
155         if (aMap.Contains(Exp.Current())) {
156           TopoDS_Edge E = TopoDS::Edge(Exp.Current());
157           if (!BRepTools::IsReallyClosed(E, F) && !BRep_Tool::Degenerated(E))
158           {
159             if ( aType == CHAMFER_SHAPE_EDGE )
160             {
161               double aD1 = aCI.GetD1();
162               double aD2 = aCI.GetD2();
163               fill.Add(aD1, aD2, E, F);
164             }
165             else
166             {
167               double aD = aCI.GetD();
168               double anAngle = aCI.GetAngle();
169               if ( (anAngle > 0) && (anAngle < (M_PI/2.)) )
170                 fill.AddDA(aD, anAngle, E, F);
171             }
172           }
173         }
174       }
175     }
176   }
177   else if (aType == CHAMFER_SHAPE_FACES || aType == CHAMFER_SHAPE_FACES_AD) {
178     // chamfer on all edges of the selected faces, with D1 on the selected face
179     // (on first selected face, if the edge belongs to two selected faces)
180
181     int aLen = aCI.GetLength();
182     int ind = 1;
183     TopTools_MapOfShape aMap;
184     TopTools_IndexedDataMapOfShapeListOfShape M;
185     GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
186     for (; ind <= aLen; ind++)
187     {
188       TopoDS_Shape aShapeFace;
189       if (GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace(ind), aShapeFace))
190       {
191         TopoDS_Face F = TopoDS::Face(aShapeFace);
192         TopExp_Explorer Exp (F, TopAbs_EDGE);
193         for (; Exp.More(); Exp.Next()) {
194           if (!aMap.Contains(Exp.Current()))
195           {
196             TopoDS_Edge E = TopoDS::Edge(Exp.Current());
197             if (!BRepTools::IsReallyClosed(E, F) &&
198                 !BRep_Tool::Degenerated(E) &&
199                 M.FindFromKey(E).Extent() == 2)
200             {
201               if (aType == CHAMFER_SHAPE_FACES)
202               {
203                 double aD1 = aCI.GetD1();
204                 double aD2 = aCI.GetD2();
205                 fill.Add(aD1, aD2, E, F);
206               }
207               else
208               {
209                 double aD = aCI.GetD();
210                 double anAngle = aCI.GetAngle();
211                 if ( (anAngle > 0) && (anAngle < (M_PI/2.)) )
212                   fill.AddDA(aD, anAngle, E, F);
213               }
214             }
215           }
216         }
217       }
218     }
219   }
220   else if (aType == CHAMFER_SHAPE_EDGES || aType == CHAMFER_SHAPE_EDGES_AD)
221   {
222     // chamfer on selected edges with length param D1 & D2.
223
224     int aLen = aCI.GetLength();
225     int ind = 1;
226     TopTools_MapOfShape aMap;
227     TopTools_IndexedDataMapOfShapeListOfShape M;
228     GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
229     for (; ind <= aLen; ind++)
230     {
231       TopoDS_Shape aShapeEdge;
232       if (GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetEdge(ind), aShapeEdge))
233       {
234         TopoDS_Edge E = TopoDS::Edge(aShapeEdge);
235         const TopTools_ListOfShape& aFacesList = M.FindFromKey(E);
236         TopoDS_Face F = TopoDS::Face( aFacesList.First() );
237         if (aType == CHAMFER_SHAPE_EDGES)
238         {
239           double aD1 = aCI.GetD1();
240           double aD2 = aCI.GetD2();
241           fill.Add(aD1, aD2, E, F);
242         }
243         else
244         {
245           double aD = aCI.GetD();
246           double anAngle = aCI.GetAngle();
247           if ( (anAngle > 0) && (anAngle < (M_PI/2.)) )
248             fill.AddDA(aD, anAngle, E, F);
249         }
250       }
251     }
252   }
253   else {
254   }
255
256   fill.Build();
257   if (!fill.IsDone()) {
258     StdFail_NotDone::Raise("Chamfer can not be computed on the given shape with the given parameters");
259   }
260   aShape = GEOMUtils::ReduceCompound( fill.Shape() );
261
262   if (aShape.IsNull()) return 0;
263
264   // reduce tolerances
265   GEOMUtils::FixShapeTolerance( aShape );
266
267   // fix SameParameter flag
268   BRepLib::SameParameter(aShape, 1.E-5, Standard_True);
269
270   aFunction->SetValue(aShape);
271
272   log->SetTouched(Label());
273
274   return 1;
275 }
276
277 //================================================================================
278 /*!
279  * \brief Returns a name of creation operation and names and values of creation parameters
280  */
281 //================================================================================
282
283 bool GEOMImpl_ChamferDriver::
284 GetCreationInformation(std::string&             theOperationName,
285                        std::vector<GEOM_Param>& theParams)
286 {
287   if (Label().IsNull()) return 0;
288   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
289
290   GEOMImpl_IChamfer aCI( function );
291   Standard_Integer aType = function->GetType();
292
293   theOperationName = "CHAMFER";
294
295   switch ( aType ) {
296   case CHAMFER_SHAPE_ALL:
297     AddParam( theParams, "Main Object", aCI.GetShape() );
298     AddParam( theParams, "Selected Edges", "all" );
299     AddParam( theParams, "D", aCI.GetD() );
300     break;
301   case CHAMFER_SHAPE_EDGE:
302     AddParam( theParams, "Main Object", aCI.GetShape() );
303     AddParam( theParams, "Face 1", aCI.GetFace1() );
304     AddParam( theParams, "Face 2", aCI.GetFace2() );
305     AddParam( theParams, "D1", aCI.GetD1() );
306     AddParam( theParams, "D2", aCI.GetD2() );
307     break;
308   case CHAMFER_SHAPE_EDGE_AD:
309     AddParam( theParams, "Main Object", aCI.GetShape() );
310     AddParam( theParams, "Face 1", aCI.GetFace1() );
311     AddParam( theParams, "Face 2", aCI.GetFace2() );
312     AddParam( theParams, "D", aCI.GetD() );
313     AddParam( theParams, "Angle", aCI.GetAngle() );
314     break;
315   case CHAMFER_SHAPE_FACES:
316     AddParam( theParams, "Main Object", aCI.GetShape() );
317     AddParam( theParams, "Selected Faces" );
318     if ( aCI.GetLength() > 1 )
319       theParams[1] << aCI.GetLength() << " faces: ";
320     for ( int i = 1, nb = aCI.GetLength(); i <= nb; ++i )
321       theParams[1] << aCI.GetFace(i) << " ";
322     AddParam( theParams, "D1", aCI.GetD1() );
323     AddParam( theParams, "D2", aCI.GetD2() );
324     break;
325   case CHAMFER_SHAPE_FACES_AD:
326     AddParam( theParams, "Main Object", aCI.GetShape() );
327     AddParam( theParams, "Selected Faces" );
328     if ( aCI.GetLength() > 1 )
329       theParams[1] << aCI.GetLength() << " faces: ";
330     for ( int i = 1, nb = aCI.GetLength(); i <= nb; ++i )
331       theParams[1] << aCI.GetFace(i) << " ";
332     AddParam( theParams, "D", aCI.GetD() );
333     AddParam( theParams, "Angle", aCI.GetAngle() );
334     break;
335   case CHAMFER_SHAPE_EDGES:
336     AddParam( theParams, "Main Object", aCI.GetShape() );
337     AddParam( theParams, "Selected Edges" );
338     if ( aCI.GetLength() > 1 )
339       theParams[1] << aCI.GetLength() << " edges: ";
340     for ( int i = 1, nb = aCI.GetLength(); i <= nb; ++i )
341       theParams[1] << aCI.GetEdge(i) << " ";
342     AddParam( theParams, "D1", aCI.GetD1() );
343     AddParam( theParams, "D2", aCI.GetD2() );
344     break;
345   case CHAMFER_SHAPE_EDGES_AD:
346     AddParam( theParams, "Main Object", aCI.GetShape() );
347     AddParam( theParams, "Selected Edges" );
348     if ( aCI.GetLength() > 1 )
349       theParams[1] << aCI.GetLength() << " edges: ";
350     for ( int i = 1, nb = aCI.GetLength(); i <= nb; ++i )
351       theParams[1] << aCI.GetFace(i) << " ";
352     AddParam( theParams, "D", aCI.GetD() );
353     AddParam( theParams, "Angle", aCI.GetAngle() );
354     break;
355   default:
356     return false;
357   }
358
359   return true;
360 }
361
362 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_ChamferDriver,GEOM_BaseDriver)