1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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>
31 #include <BRepLib.hxx>
32 #include <BRep_Tool.hxx>
33 #include <BRepTools.hxx>
34 #include <BRepFilletAPI_MakeChamfer.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>
46 #include <StdFail_NotDone.hxx>
48 //=======================================================================
51 //=======================================================================
52 const Standard_GUID& GEOMImpl_ChamferDriver::GetID()
54 static Standard_GUID aChamferDriver("FF1BBB42-5D14-4df2-980B-3A668264EA16");
55 return aChamferDriver;
58 //=======================================================================
59 //function : GEOMImpl_ChamferDriver
61 //=======================================================================
62 GEOMImpl_ChamferDriver::GEOMImpl_ChamferDriver()
66 //=======================================================================
67 //function : isGoodForChamfer
69 //=======================================================================
70 static Standard_Boolean isGoodForChamfer (const TopoDS_Shape& theShape)
72 if (theShape.ShapeType() == TopAbs_SHELL ||
73 theShape.ShapeType() == TopAbs_SOLID ||
74 theShape.ShapeType() == TopAbs_COMPSOLID) {
78 if (theShape.ShapeType() == TopAbs_COMPOUND) {
79 TopTools_MapOfShape mapShape;
80 TopoDS_Iterator It (theShape, Standard_False, Standard_False);
81 for (; It.More(); It.Next()) {
82 if (mapShape.Add(It.Value())) {
83 if (!isGoodForChamfer(It.Value())) {
84 return Standard_False;
91 return Standard_False;
94 //=======================================================================
97 //=======================================================================
98 Standard_Integer GEOMImpl_ChamferDriver::Execute(LOGBOOK& log) const
100 if (Label().IsNull()) return 0;
101 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
103 GEOMImpl_IChamfer aCI (aFunction);
104 Standard_Integer aType = aFunction->GetType();
108 Handle(GEOM_Function) aRefShape = aCI.GetShape();
109 TopoDS_Shape aShapeBase = aRefShape->GetValue();
111 // Check the shape type. It have to be shell
112 // or solid, or compsolid, or compound of these shapes.
113 if (!isGoodForChamfer(aShapeBase)) {
114 StdFail_NotDone::Raise
115 ("Wrong shape. Must be shell or solid, or compsolid or compound of these shapes");
118 BRepFilletAPI_MakeChamfer fill (aShapeBase);
120 if (aType == CHAMFER_SHAPE_ALL) {
121 // symmetric chamfer on all edges
122 double aD = aCI.GetD();
123 TopTools_IndexedDataMapOfShapeListOfShape M;
124 GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
125 for (int i = 1; i <= M.Extent(); i++) {
126 TopoDS_Edge E = TopoDS::Edge(M.FindKey(i));
127 TopoDS_Face F = TopoDS::Face(M.FindFromIndex(i).First());
128 if (!BRepTools::IsReallyClosed(E, F) &&
129 !BRep_Tool::Degenerated(E) &&
130 M.FindFromIndex(i).Extent() == 2)
134 else if (aType == CHAMFER_SHAPE_EDGE || aType == CHAMFER_SHAPE_EDGE_AD) {
135 // chamfer on edges, common to two faces, with D1 on the first face
137 TopoDS_Shape aFace1, aFace2;
138 if (GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace1(), aFace1) &&
139 GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace2(), aFace2))
141 TopoDS_Face F = TopoDS::Face(aFace1);
143 // fill map of edges of the second face
144 TopTools_MapOfShape aMap;
145 TopExp_Explorer Exp2 (aFace2, TopAbs_EDGE);
146 for (; Exp2.More(); Exp2.Next()) {
147 aMap.Add(Exp2.Current());
150 // find edges of the first face, common with the second face
151 TopExp_Explorer Exp (aFace1, TopAbs_EDGE);
152 for (; Exp.More(); Exp.Next()) {
153 if (aMap.Contains(Exp.Current())) {
154 TopoDS_Edge E = TopoDS::Edge(Exp.Current());
155 if (!BRepTools::IsReallyClosed(E, F) && !BRep_Tool::Degenerated(E))
157 if ( aType == CHAMFER_SHAPE_EDGE )
159 double aD1 = aCI.GetD1();
160 double aD2 = aCI.GetD2();
161 fill.Add(aD1, aD2, E, F);
165 double aD = aCI.GetD();
166 double anAngle = aCI.GetAngle();
167 if ( (anAngle > 0) && (anAngle < (M_PI/2.)) )
168 fill.AddDA(aD, anAngle, E, F);
175 else if (aType == CHAMFER_SHAPE_FACES || aType == CHAMFER_SHAPE_FACES_AD) {
176 // chamfer on all edges of the selected faces, with D1 on the selected face
177 // (on first selected face, if the edge belongs to two selected faces)
179 int aLen = aCI.GetLength();
181 TopTools_MapOfShape aMap;
182 TopTools_IndexedDataMapOfShapeListOfShape M;
183 GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
184 for (; ind <= aLen; ind++)
186 TopoDS_Shape aShapeFace;
187 if (GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace(ind), aShapeFace))
189 TopoDS_Face F = TopoDS::Face(aShapeFace);
190 TopExp_Explorer Exp (F, TopAbs_EDGE);
191 for (; Exp.More(); Exp.Next()) {
192 if (!aMap.Contains(Exp.Current()))
194 TopoDS_Edge E = TopoDS::Edge(Exp.Current());
195 if (!BRepTools::IsReallyClosed(E, F) &&
196 !BRep_Tool::Degenerated(E) &&
197 M.FindFromKey(E).Extent() == 2)
199 if (aType == CHAMFER_SHAPE_FACES)
201 double aD1 = aCI.GetD1();
202 double aD2 = aCI.GetD2();
203 fill.Add(aD1, aD2, E, F);
207 double aD = aCI.GetD();
208 double anAngle = aCI.GetAngle();
209 if ( (anAngle > 0) && (anAngle < (M_PI/2.)) )
210 fill.AddDA(aD, anAngle, E, F);
218 else if (aType == CHAMFER_SHAPE_EDGES || aType == CHAMFER_SHAPE_EDGES_AD)
220 // chamfer on selected edges with lenght param D1 & D2.
222 int aLen = aCI.GetLength();
224 TopTools_MapOfShape aMap;
225 TopTools_IndexedDataMapOfShapeListOfShape M;
226 GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
227 for (; ind <= aLen; ind++)
229 TopoDS_Shape aShapeEdge;
230 if (GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetEdge(ind), aShapeEdge))
232 TopoDS_Edge E = TopoDS::Edge(aShapeEdge);
233 const TopTools_ListOfShape& aFacesList = M.FindFromKey(E);
234 TopoDS_Face F = TopoDS::Face( aFacesList.First() );
235 if (aType == CHAMFER_SHAPE_EDGES)
237 double aD1 = aCI.GetD1();
238 double aD2 = aCI.GetD2();
239 fill.Add(aD1, aD2, E, F);
243 double aD = aCI.GetD();
244 double anAngle = aCI.GetAngle();
245 if ( (anAngle > 0) && (anAngle < (M_PI/2.)) )
246 fill.AddDA(aD, anAngle, E, F);
255 if (!fill.IsDone()) {
256 StdFail_NotDone::Raise("Chamfer can not be computed on the given shape with the given parameters");
258 aShape = GEOMUtils::ReduceCompound( fill.Shape() );
260 if (aShape.IsNull()) return 0;
263 GEOMUtils::FixShapeTolerance( aShape );
265 // fix SameParameter flag
266 BRepLib::SameParameter(aShape, 1.E-5, Standard_True);
268 aFunction->SetValue(aShape);
270 #if OCC_VERSION_MAJOR < 7
271 log.SetTouched(Label());
273 log->SetTouched(Label());
279 //================================================================================
281 * \brief Returns a name of creation operation and names and values of creation parameters
283 //================================================================================
285 bool GEOMImpl_ChamferDriver::
286 GetCreationInformation(std::string& theOperationName,
287 std::vector<GEOM_Param>& theParams)
289 if (Label().IsNull()) return 0;
290 Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
292 GEOMImpl_IChamfer aCI( function );
293 Standard_Integer aType = function->GetType();
295 theOperationName = "CHAMFER";
298 case CHAMFER_SHAPE_ALL:
299 AddParam( theParams, "Main Object", aCI.GetShape() );
300 AddParam( theParams, "Selected Edges", "all" );
301 AddParam( theParams, "D", aCI.GetD() );
303 case CHAMFER_SHAPE_EDGE:
304 AddParam( theParams, "Main Object", aCI.GetShape() );
305 AddParam( theParams, "Face 1", aCI.GetFace1() );
306 AddParam( theParams, "Face 2", aCI.GetFace2() );
307 AddParam( theParams, "D1", aCI.GetD1() );
308 AddParam( theParams, "D2", aCI.GetD2() );
310 case CHAMFER_SHAPE_EDGE_AD:
311 AddParam( theParams, "Main Object", aCI.GetShape() );
312 AddParam( theParams, "Face 1", aCI.GetFace1() );
313 AddParam( theParams, "Face 2", aCI.GetFace2() );
314 AddParam( theParams, "D", aCI.GetD() );
315 AddParam( theParams, "Angle", aCI.GetAngle() );
317 case CHAMFER_SHAPE_FACES:
318 AddParam( theParams, "Main Object", aCI.GetShape() );
319 AddParam( theParams, "Selected Faces" );
320 if ( aCI.GetLength() > 1 )
321 theParams[1] << aCI.GetLength() << " faces: ";
322 for ( int i = 1, nb = aCI.GetLength(); i <= nb; ++i )
323 theParams[1] << aCI.GetFace(i) << " ";
324 AddParam( theParams, "D1", aCI.GetD1() );
325 AddParam( theParams, "D2", aCI.GetD2() );
327 case CHAMFER_SHAPE_FACES_AD:
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, "D", aCI.GetD() );
335 AddParam( theParams, "Angle", aCI.GetAngle() );
337 case CHAMFER_SHAPE_EDGES:
338 AddParam( theParams, "Main Object", aCI.GetShape() );
339 AddParam( theParams, "Selected Edges" );
340 if ( aCI.GetLength() > 1 )
341 theParams[1] << aCI.GetLength() << " edges: ";
342 for ( int i = 1, nb = aCI.GetLength(); i <= nb; ++i )
343 theParams[1] << aCI.GetEdge(i) << " ";
344 AddParam( theParams, "D1", aCI.GetD1() );
345 AddParam( theParams, "D2", aCI.GetD2() );
347 case CHAMFER_SHAPE_EDGES_AD:
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.GetFace(i) << " ";
354 AddParam( theParams, "D", aCI.GetD() );
355 AddParam( theParams, "Angle", aCI.GetAngle() );
364 OCCT_IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_ChamferDriver,GEOM_BaseDriver);