1 // Copyright (C) 2007-2008 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
22 #include <Standard_Stream.hxx>
24 #include <GEOMImpl_ChamferDriver.hxx>
25 #include <GEOMImpl_IChamfer.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOMImpl_ILocalOperations.hxx>
28 #include <GEOMImpl_Block6Explorer.hxx>
30 #include <GEOM_Function.hxx>
32 #include <BRepLib.hxx>
33 #include <BRep_Tool.hxx>
34 #include <BRepTools.hxx>
35 #include <BRepFilletAPI_MakeChamfer.hxx>
37 #include <ShapeFix_Shape.hxx>
38 #include <ShapeFix_ShapeTolerance.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <TopoDS_Face.hxx>
44 #include <TopoDS_Shape.hxx>
45 #include <TopoDS_Iterator.hxx>
47 #include <TopExp_Explorer.hxx>
48 #include <TopTools_MapOfShape.hxx>
49 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
51 #include <Precision.hxx>
53 #include <StdFail_NotDone.hxx>
55 //=======================================================================
58 //=======================================================================
59 const Standard_GUID& GEOMImpl_ChamferDriver::GetID()
61 static Standard_GUID aChamferDriver("FF1BBB42-5D14-4df2-980B-3A668264EA16");
62 return aChamferDriver;
66 //=======================================================================
67 //function : GEOMImpl_ChamferDriver
69 //=======================================================================
70 GEOMImpl_ChamferDriver::GEOMImpl_ChamferDriver()
74 //=======================================================================
75 //function : isGoodForChamfer
77 //=======================================================================
78 static Standard_Boolean isGoodForChamfer (const TopoDS_Shape& theShape)
80 if (theShape.ShapeType() == TopAbs_SHELL ||
81 theShape.ShapeType() == TopAbs_SOLID ||
82 theShape.ShapeType() == TopAbs_COMPSOLID) {
86 if (theShape.ShapeType() == TopAbs_COMPOUND) {
87 TopTools_MapOfShape mapShape;
88 TopoDS_Iterator It (theShape, Standard_False, Standard_False);
89 for (; It.More(); It.Next()) {
90 if (mapShape.Add(It.Value())) {
91 if (!isGoodForChamfer(It.Value())) {
92 return Standard_False;
99 return Standard_False;
102 //=======================================================================
105 //=======================================================================
106 Standard_Integer GEOMImpl_ChamferDriver::Execute(TFunction_Logbook& log) const
108 if (Label().IsNull()) return 0;
109 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
111 GEOMImpl_IChamfer aCI (aFunction);
112 Standard_Integer aType = aFunction->GetType();
116 Handle(GEOM_Function) aRefShape = aCI.GetShape();
117 TopoDS_Shape aShapeBase = aRefShape->GetValue();
119 // Check the shape type. It have to be shell
120 // or solid, or compsolid, or compound of these shapes.
121 if (!isGoodForChamfer(aShapeBase)) {
122 StdFail_NotDone::Raise
123 ("Wrong shape. Must be shell or solid, or compsolid or compound of these shapes");
126 BRepFilletAPI_MakeChamfer fill (aShapeBase);
128 if (aType == CHAMFER_SHAPE_ALL) {
129 // symmetric chamfer on all edges
130 double aD = aCI.GetD();
131 TopTools_IndexedDataMapOfShapeListOfShape M;
132 GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
133 for (int i = 1; i <= M.Extent(); i++) {
134 TopoDS_Edge E = TopoDS::Edge(M.FindKey(i));
135 TopoDS_Face F = TopoDS::Face(M.FindFromIndex(i).First());
136 if (!BRepTools::IsReallyClosed(E, F) &&
137 !BRep_Tool::Degenerated(E) &&
138 M.FindFromIndex(i).Extent() == 2)
142 else if (aType == CHAMFER_SHAPE_EDGE || aType == CHAMFER_SHAPE_EDGE_AD) {
143 // chamfer on edges, common to two faces, with D1 on the first face
145 TopoDS_Shape aFace1, aFace2;
146 if (GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace1(), aFace1) &&
147 GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace2(), aFace2))
149 TopoDS_Face F = TopoDS::Face(aFace1);
151 // fill map of edges of the second face
152 TopTools_MapOfShape aMap;
153 TopExp_Explorer Exp2 (aFace2, TopAbs_EDGE);
154 for (; Exp2.More(); Exp2.Next()) {
155 aMap.Add(Exp2.Current());
158 // find edges of the first face, common with the second face
159 TopExp_Explorer Exp (aFace1, TopAbs_EDGE);
160 for (; Exp.More(); Exp.Next()) {
161 if (aMap.Contains(Exp.Current())) {
162 TopoDS_Edge E = TopoDS::Edge(Exp.Current());
163 if (!BRepTools::IsReallyClosed(E, F) && !BRep_Tool::Degenerated(E))
165 if ( aType == CHAMFER_SHAPE_EDGE )
167 double aD1 = aCI.GetD1();
168 double aD2 = aCI.GetD2();
169 fill.Add(aD1, aD2, E, F);
173 double aD = aCI.GetD();
174 double anAngle = aCI.GetAngle();
175 if ( (anAngle > 0) && (anAngle < (Standard_PI/2)) )
176 fill.AddDA(aD, anAngle, E, F);
183 else if (aType == CHAMFER_SHAPE_FACES || aType == CHAMFER_SHAPE_FACES_AD) {
184 // chamfer on all edges of the selected faces, with D1 on the selected face
185 // (on first selected face, if the edge belongs to two selected faces)
187 int aLen = aCI.GetLength();
189 TopTools_MapOfShape aMap;
190 TopTools_IndexedDataMapOfShapeListOfShape M;
191 GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
192 for (; ind <= aLen; ind++)
194 TopoDS_Shape aShapeFace;
195 if (GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace(ind), aShapeFace))
197 TopoDS_Face F = TopoDS::Face(aShapeFace);
198 TopExp_Explorer Exp (F, TopAbs_EDGE);
199 for (; Exp.More(); Exp.Next()) {
200 if (!aMap.Contains(Exp.Current()))
202 TopoDS_Edge E = TopoDS::Edge(Exp.Current());
203 if (!BRepTools::IsReallyClosed(E, F) &&
204 !BRep_Tool::Degenerated(E) &&
205 M.FindFromKey(E).Extent() == 2)
207 if (aType == CHAMFER_SHAPE_FACES)
209 double aD1 = aCI.GetD1();
210 double aD2 = aCI.GetD2();
211 fill.Add(aD1, aD2, E, F);
215 double aD = aCI.GetD();
216 double anAngle = aCI.GetAngle();
217 if ( (anAngle > 0) && (anAngle < (Standard_PI/2)) )
218 fill.AddDA(aD, anAngle, E, F);
226 else if (aType == CHAMFER_SHAPE_EDGES || aType == CHAMFER_SHAPE_EDGES_AD)
228 // chamfer on selected edges with lenght param D1 & D2.
230 int aLen = aCI.GetLength();
232 TopTools_MapOfShape aMap;
233 TopTools_IndexedDataMapOfShapeListOfShape M;
234 GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
235 for (; ind <= aLen; ind++)
237 TopoDS_Shape aShapeEdge;
238 if (GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetEdge(ind), aShapeEdge))
240 TopoDS_Edge E = TopoDS::Edge(aShapeEdge);
241 const TopTools_ListOfShape& aFacesList = M.FindFromKey(E);
242 TopoDS_Face F = TopoDS::Face( aFacesList.First() );
243 if (aType == CHAMFER_SHAPE_EDGES)
245 double aD1 = aCI.GetD1();
246 double aD2 = aCI.GetD2();
247 fill.Add(aD1, aD2, E, F);
251 double aD = aCI.GetD();
252 double anAngle = aCI.GetAngle();
253 if ( (anAngle > 0) && (anAngle < (Standard_PI/2)) )
254 fill.AddDA(aD, anAngle, E, F);
263 if (!fill.IsDone()) {
264 StdFail_NotDone::Raise("Chamfer can not be computed on the given shape with the given parameters");
266 aShape = fill.Shape();
268 if (aShape.IsNull()) return 0;
271 ShapeFix_ShapeTolerance aSFT;
272 aSFT.LimitTolerance(aShape, Precision::Confusion(),
273 Precision::Confusion(), TopAbs_SHAPE);
274 Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
276 aShape = aSfs->Shape();
278 // fix SameParameter flag
279 BRepLib::SameParameter(aShape, 1.E-5, Standard_True);
281 aFunction->SetValue(aShape);
283 log.SetTouched(Label());
289 //=======================================================================
290 //function : GEOMImpl_ChamferDriver_Type_
292 //=======================================================================
293 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ChamferDriver_Type_()
296 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
297 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
298 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
299 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
300 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
301 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
304 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
305 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ChamferDriver",
306 sizeof(GEOMImpl_ChamferDriver),
308 (Standard_Address)_Ancestors,
309 (Standard_Address)NULL);
314 //=======================================================================
315 //function : DownCast
317 //=======================================================================
318 const Handle(GEOMImpl_ChamferDriver) Handle(GEOMImpl_ChamferDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
320 Handle(GEOMImpl_ChamferDriver) _anOtherObject;
322 if (!AnObject.IsNull()) {
323 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ChamferDriver))) {
324 _anOtherObject = Handle(GEOMImpl_ChamferDriver)((Handle(GEOMImpl_ChamferDriver)&)AnObject);
328 return _anOtherObject;