1 // Copyright (C) 2007-2010 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.
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 < (Standard_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 < (Standard_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 < (Standard_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());
290 //=======================================================================
291 //function : GEOMImpl_ChamferDriver_Type_
293 //=======================================================================
294 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ChamferDriver_Type_()
297 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
298 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
299 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
300 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
301 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
302 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
305 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
306 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ChamferDriver",
307 sizeof(GEOMImpl_ChamferDriver),
309 (Standard_Address)_Ancestors,
310 (Standard_Address)NULL);
315 //=======================================================================
316 //function : DownCast
318 //=======================================================================
319 const Handle(GEOMImpl_ChamferDriver) Handle(GEOMImpl_ChamferDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
321 Handle(GEOMImpl_ChamferDriver) _anOtherObject;
323 if (!AnObject.IsNull()) {
324 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ChamferDriver))) {
325 _anOtherObject = Handle(GEOMImpl_ChamferDriver)((Handle(GEOMImpl_ChamferDriver)&)AnObject);
329 return _anOtherObject;