Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ChamferDriver.cxx
1 //  Copyright (C) 2007-2008  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.
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 #include <Standard_Stream.hxx>
23
24 #include <GEOMImpl_ChamferDriver.hxx>
25 #include <GEOMImpl_IChamfer.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOMImpl_ILocalOperations.hxx>
28 #include <GEOM_Function.hxx>
29 #include <GEOMImpl_Block6Explorer.hxx>
30
31 #include <BRep_Tool.hxx>
32 #include <BRepTools.hxx>
33 #include <BRepFilletAPI_MakeChamfer.hxx>
34
35 #include <TopAbs.hxx>
36 #include <TopoDS.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopoDS_Face.hxx>
39 #include <TopoDS_Shape.hxx>
40 #include <TopoDS_Iterator.hxx>
41 #include <TopExp.hxx>
42 #include <TopExp_Explorer.hxx>
43 #include <TopTools_MapOfShape.hxx>
44 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
45
46 #include <Precision.hxx>
47 #include <gp_Pnt.hxx>
48 #include <StdFail_NotDone.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 //=======================================================================
62 //function : GEOMImpl_ChamferDriver
63 //purpose  :
64 //=======================================================================
65 GEOMImpl_ChamferDriver::GEOMImpl_ChamferDriver()
66 {
67 }
68
69 //=======================================================================
70 //function : isGoodForChamfer
71 //purpose  :
72 //=======================================================================
73 static Standard_Boolean isGoodForChamfer (const TopoDS_Shape& theShape)
74 {
75   if (theShape.ShapeType() == TopAbs_SHELL ||
76       theShape.ShapeType() == TopAbs_SOLID ||
77       theShape.ShapeType() == TopAbs_COMPSOLID) {
78     return Standard_True;
79   }
80
81   if (theShape.ShapeType() == TopAbs_COMPOUND) {
82     TopTools_MapOfShape mapShape;
83     TopoDS_Iterator It (theShape, Standard_False, Standard_False);
84     for (; It.More(); It.Next()) {
85       if (mapShape.Add(It.Value())) {
86         if (!isGoodForChamfer(It.Value())) {
87           return Standard_False;
88         }
89       }
90     }
91     return Standard_True;
92   }
93
94   return Standard_False;
95 }
96
97 //=======================================================================
98 //function : Execute
99 //purpose  :
100 //=======================================================================
101 Standard_Integer GEOMImpl_ChamferDriver::Execute(TFunction_Logbook& log) const
102 {
103   if (Label().IsNull()) return 0;
104   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
105
106   GEOMImpl_IChamfer aCI (aFunction);
107   Standard_Integer aType = aFunction->GetType();
108
109   TopoDS_Shape aShape;
110
111   Handle(GEOM_Function) aRefShape = aCI.GetShape();
112   TopoDS_Shape aShapeBase = aRefShape->GetValue();
113
114   // Check the shape type. It have to be shell
115   // or solid, or compsolid, or compound of these shapes.
116   if (!isGoodForChamfer(aShapeBase)) {
117     StdFail_NotDone::Raise
118       ("Wrong shape. Must be shell or solid, or compsolid or compound of these shapes");
119   }
120
121   BRepFilletAPI_MakeChamfer fill (aShapeBase);
122
123   if (aType == CHAMFER_SHAPE_ALL) {
124     // symmetric chamfer on all edges
125     double aD = aCI.GetD();
126     TopTools_IndexedDataMapOfShapeListOfShape M;
127     GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
128     for (int i = 1; i <= M.Extent(); i++) {
129       TopoDS_Edge E = TopoDS::Edge(M.FindKey(i));
130       TopoDS_Face F = TopoDS::Face(M.FindFromIndex(i).First());
131       if (!BRepTools::IsReallyClosed(E, F) &&
132           !BRep_Tool::Degenerated(E) &&
133           M.FindFromIndex(i).Extent() == 2)
134         fill.Add(aD, E, F);
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 < (Standard_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                   if (aType == CHAMFER_SHAPE_FACES)
201                     {
202                       double aD1 = aCI.GetD1();
203                       double aD2 = aCI.GetD2();
204                       fill.Add(aD1, aD2, E, F);
205                     }
206                   else
207                     {
208                       double aD = aCI.GetD();
209                       double anAngle = aCI.GetAngle();
210                       if ( (anAngle > 0) && (anAngle < (Standard_PI/2)) )
211                         fill.AddDA(aD, anAngle, E, F);
212                     }
213               }
214           }
215         }
216     }
217   }  
218 else if (aType == CHAMFER_SHAPE_EDGES || aType == CHAMFER_SHAPE_EDGES_AD)
219   {
220     // chamfer on selected edges with lenght param D1 & D2.
221
222     int aLen = aCI.GetLength();
223     int ind = 1;
224     TopTools_MapOfShape aMap;
225     TopTools_IndexedDataMapOfShapeListOfShape M;
226     GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
227     for (; ind <= aLen; ind++)
228     {
229       TopoDS_Shape aShapeEdge;
230       if (GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetEdge(ind), aShapeEdge))
231         {
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)
236             {
237               double aD1 = aCI.GetD1();
238               double aD2 = aCI.GetD2();
239               fill.Add(aD1, aD2, E, F);
240             }
241           else
242             {
243               double aD = aCI.GetD();
244               double anAngle = aCI.GetAngle();
245               if ( (anAngle > 0) && (anAngle < (Standard_PI/2)) )
246                 fill.AddDA(aD, anAngle, E, F);
247             }
248         } 
249     } 
250   }
251   else {
252   }
253
254   fill.Build();
255   if (!fill.IsDone()) {
256     StdFail_NotDone::Raise("Chamfer can not be computed on the given shape with the given parameters");
257   }
258   aShape = fill.Shape();
259
260   if (aShape.IsNull()) return 0;
261
262   aFunction->SetValue(aShape);
263
264   log.SetTouched(Label());
265
266   return 1;
267 }
268
269
270 //=======================================================================
271 //function :  GEOMImpl_ChamferDriver_Type_
272 //purpose  :
273 //=======================================================================
274 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ChamferDriver_Type_()
275 {
276
277   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
278   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
279   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
280   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
281   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
282   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
283
284
285   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
286   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ChamferDriver",
287                                                          sizeof(GEOMImpl_ChamferDriver),
288                                                          1,
289                                                          (Standard_Address)_Ancestors,
290                                                          (Standard_Address)NULL);
291
292   return _aType;
293 }
294
295 //=======================================================================
296 //function : DownCast
297 //purpose  :
298 //=======================================================================
299 const Handle(GEOMImpl_ChamferDriver) Handle(GEOMImpl_ChamferDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
300 {
301   Handle(GEOMImpl_ChamferDriver) _anOtherObject;
302
303   if (!AnObject.IsNull()) {
304      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ChamferDriver))) {
305        _anOtherObject = Handle(GEOMImpl_ChamferDriver)((Handle(GEOMImpl_ChamferDriver)&)AnObject);
306      }
307   }
308
309   return _anOtherObject ;
310 }