1 // Copyright (C) 2007-2014 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 <Standard_Stream.hxx>
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>
31 #include <GEOM_Function.hxx>
33 #include <BRepLib.hxx>
34 #include <BRep_Tool.hxx>
35 #include <BRepTools.hxx>
36 #include <BRepFilletAPI_MakeChamfer.hxx>
38 #include <ShapeFix_Shape.hxx>
39 #include <ShapeFix_ShapeTolerance.hxx>
43 #include <TopoDS_Edge.hxx>
44 #include <TopoDS_Face.hxx>
45 #include <TopoDS_Shape.hxx>
46 #include <TopoDS_Iterator.hxx>
48 #include <TopExp_Explorer.hxx>
49 #include <TopTools_MapOfShape.hxx>
50 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
52 #include <Precision.hxx>
54 #include <StdFail_NotDone.hxx>
56 //=======================================================================
59 //=======================================================================
60 const Standard_GUID& GEOMImpl_ChamferDriver::GetID()
62 static Standard_GUID aChamferDriver("FF1BBB42-5D14-4df2-980B-3A668264EA16");
63 return aChamferDriver;
67 //=======================================================================
68 //function : GEOMImpl_ChamferDriver
70 //=======================================================================
71 GEOMImpl_ChamferDriver::GEOMImpl_ChamferDriver()
75 //=======================================================================
76 //function : isGoodForChamfer
78 //=======================================================================
79 static Standard_Boolean isGoodForChamfer (const TopoDS_Shape& theShape)
81 if (theShape.ShapeType() == TopAbs_SHELL ||
82 theShape.ShapeType() == TopAbs_SOLID ||
83 theShape.ShapeType() == TopAbs_COMPSOLID) {
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;
100 return Standard_False;
103 //=======================================================================
106 //=======================================================================
107 Standard_Integer GEOMImpl_ChamferDriver::Execute(TFunction_Logbook& log) const
109 if (Label().IsNull()) return 0;
110 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
112 GEOMImpl_IChamfer aCI (aFunction);
113 Standard_Integer aType = aFunction->GetType();
117 Handle(GEOM_Function) aRefShape = aCI.GetShape();
118 TopoDS_Shape aShapeBase = aRefShape->GetValue();
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");
127 BRepFilletAPI_MakeChamfer fill (aShapeBase);
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)
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
146 TopoDS_Shape aFace1, aFace2;
147 if (GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace1(), aFace1) &&
148 GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace2(), aFace2))
150 TopoDS_Face F = TopoDS::Face(aFace1);
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());
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))
166 if ( aType == CHAMFER_SHAPE_EDGE )
168 double aD1 = aCI.GetD1();
169 double aD2 = aCI.GetD2();
170 fill.Add(aD1, aD2, E, F);
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);
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)
188 int aLen = aCI.GetLength();
190 TopTools_MapOfShape aMap;
191 TopTools_IndexedDataMapOfShapeListOfShape M;
192 GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
193 for (; ind <= aLen; ind++)
195 TopoDS_Shape aShapeFace;
196 if (GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace(ind), aShapeFace))
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()))
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)
208 if (aType == CHAMFER_SHAPE_FACES)
210 double aD1 = aCI.GetD1();
211 double aD2 = aCI.GetD2();
212 fill.Add(aD1, aD2, E, F);
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);
227 else if (aType == CHAMFER_SHAPE_EDGES || aType == CHAMFER_SHAPE_EDGES_AD)
229 // chamfer on selected edges with lenght param D1 & D2.
231 int aLen = aCI.GetLength();
233 TopTools_MapOfShape aMap;
234 TopTools_IndexedDataMapOfShapeListOfShape M;
235 GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
236 for (; ind <= aLen; ind++)
238 TopoDS_Shape aShapeEdge;
239 if (GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetEdge(ind), aShapeEdge))
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)
246 double aD1 = aCI.GetD1();
247 double aD2 = aCI.GetD2();
248 fill.Add(aD1, aD2, E, F);
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);
264 if (!fill.IsDone()) {
265 StdFail_NotDone::Raise("Chamfer can not be computed on the given shape with the given parameters");
267 aShape = fill.Shape();
269 if (aShape.IsNull()) return 0;
272 ShapeFix_ShapeTolerance aSFT;
273 aSFT.LimitTolerance(aShape, Precision::Confusion(),
274 Precision::Confusion(), TopAbs_SHAPE);
275 Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
277 aShape = aSfs->Shape();
279 // fix SameParameter flag
280 BRepLib::SameParameter(aShape, 1.E-5, Standard_True);
282 aFunction->SetValue(aShape);
284 log.SetTouched(Label());
289 //================================================================================
291 * \brief Returns a name of creation operation and names and values of creation parameters
293 //================================================================================
295 bool GEOMImpl_ChamferDriver::
296 GetCreationInformation(std::string& theOperationName,
297 std::vector<GEOM_Param>& theParams)
299 if (Label().IsNull()) return 0;
300 Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
302 GEOMImpl_IChamfer aCI( function );
303 Standard_Integer aType = function->GetType();
305 theOperationName = "CHAMFER";
308 case CHAMFER_SHAPE_ALL:
309 AddParam( theParams, "Main Object", aCI.GetShape() );
310 AddParam( theParams, "Selected Edges", "all" );
311 AddParam( theParams, "D", aCI.GetD() );
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() );
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() );
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() );
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() );
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() );
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() );
374 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_ChamferDriver,GEOM_BaseDriver);
376 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_ChamferDriver,GEOM_BaseDriver);