Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ChamferDriver.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21 #include <Standard_Stream.hxx>
22
23 #include <GEOMImpl_ChamferDriver.hxx>
24 #include <GEOMImpl_IChamfer.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOMImpl_ILocalOperations.hxx>
27 #include <GEOM_Function.hxx>
28 #include <GEOMImpl_Block6Explorer.hxx>
29
30 #include <BRep_Tool.hxx>
31 #include <BRepTools.hxx>
32 #include <BRepFilletAPI_MakeChamfer.hxx>
33
34 #include <TopAbs.hxx>
35 #include <TopoDS.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopoDS_Face.hxx>
38 #include <TopoDS_Shape.hxx>
39 #include <TopoDS_Iterator.hxx>
40 #include <TopExp.hxx>
41 #include <TopExp_Explorer.hxx>
42 #include <TopTools_MapOfShape.hxx>
43 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
44
45 #include <Precision.hxx>
46 #include <gp_Pnt.hxx>
47 #include <StdFail_NotDone.hxx>
48
49 //=======================================================================
50 //function : GetID
51 //purpose  :
52 //=======================================================================
53 const Standard_GUID& GEOMImpl_ChamferDriver::GetID()
54 {
55   static Standard_GUID aChamferDriver("FF1BBB42-5D14-4df2-980B-3A668264EA16");
56   return aChamferDriver;
57 }
58
59
60 //=======================================================================
61 //function : GEOMImpl_ChamferDriver
62 //purpose  :
63 //=======================================================================
64 GEOMImpl_ChamferDriver::GEOMImpl_ChamferDriver()
65 {
66 }
67
68 //=======================================================================
69 //function : isGoodForChamfer
70 //purpose  :
71 //=======================================================================
72 static Standard_Boolean isGoodForChamfer (const TopoDS_Shape& theShape)
73 {
74   if (theShape.ShapeType() == TopAbs_SHELL ||
75       theShape.ShapeType() == TopAbs_SOLID ||
76       theShape.ShapeType() == TopAbs_COMPSOLID) {
77     return Standard_True;
78   }
79
80   if (theShape.ShapeType() == TopAbs_COMPOUND) {
81     TopTools_MapOfShape mapShape;
82     TopoDS_Iterator It (theShape, Standard_False, Standard_False);
83     for (; It.More(); It.Next()) {
84       if (mapShape.Add(It.Value())) {
85         if (!isGoodForChamfer(It.Value())) {
86           return Standard_False;
87         }
88       }
89     }
90     return Standard_True;
91   }
92
93   return Standard_False;
94 }
95
96 //=======================================================================
97 //function : Execute
98 //purpose  :
99 //=======================================================================
100 Standard_Integer GEOMImpl_ChamferDriver::Execute(TFunction_Logbook& log) const
101 {
102   if (Label().IsNull()) return 0;
103   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
104
105   GEOMImpl_IChamfer aCI (aFunction);
106   Standard_Integer aType = aFunction->GetType();
107
108   TopoDS_Shape aShape;
109
110   Handle(GEOM_Function) aRefShape = aCI.GetShape();
111   TopoDS_Shape aShapeBase = aRefShape->GetValue();
112
113   // Check the shape type. It have to be shell
114   // or solid, or compsolid, or compound of these shapes.
115   if (!isGoodForChamfer(aShapeBase)) {
116     StdFail_NotDone::Raise
117       ("Wrong shape. Must be shell or solid, or compsolid or compound of these shapes");
118   }
119
120   BRepFilletAPI_MakeChamfer fill (aShapeBase);
121
122   if (aType == CHAMFER_SHAPE_ALL) {
123     // symmetric chamfer on all edges
124     double aD = aCI.GetD();
125     TopTools_IndexedDataMapOfShapeListOfShape M;
126     GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
127     for (int i = 1; i <= M.Extent(); i++) {
128       TopoDS_Edge E = TopoDS::Edge(M.FindKey(i));
129       TopoDS_Face F = TopoDS::Face(M.FindFromIndex(i).First());
130       if (!BRepTools::IsReallyClosed(E, F) &&
131           !BRep_Tool::Degenerated(E) &&
132           M.FindFromIndex(i).Extent() == 2)
133         fill.Add(aD, E, F);
134     }
135   }else if (aType == CHAMFER_SHAPE_EDGE || aType == CHAMFER_SHAPE_EDGE_AD) {
136     // chamfer on edges, common to two faces, with D1 on the first face
137    
138     TopoDS_Shape aFace1, aFace2;
139     if (GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace1(), aFace1) &&
140         GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace2(), aFace2))
141       {
142         TopoDS_Face F = TopoDS::Face(aFace1);
143
144         // fill map of edges of the second face
145         TopTools_MapOfShape aMap;
146         TopExp_Explorer Exp2 (aFace2, TopAbs_EDGE);
147         for (; Exp2.More(); Exp2.Next()) {
148           aMap.Add(Exp2.Current());
149         }
150         
151         // find edges of the first face, common with the second face
152         TopExp_Explorer Exp (aFace1, TopAbs_EDGE);
153         for (; Exp.More(); Exp.Next()) {
154           if (aMap.Contains(Exp.Current())) {
155             TopoDS_Edge E = TopoDS::Edge(Exp.Current());
156             if (!BRepTools::IsReallyClosed(E, F) && !BRep_Tool::Degenerated(E))
157               {
158                 if ( aType == CHAMFER_SHAPE_EDGE )
159                   {
160                     double aD1 = aCI.GetD1();
161                     double aD2 = aCI.GetD2();
162                     fill.Add(aD1, aD2, E, F);
163                   }
164                 else
165                   {
166                     double aD = aCI.GetD();
167                     double anAngle = aCI.GetAngle();
168                     if ( (anAngle > 0) && (anAngle < (Standard_PI/2)) )
169                       fill.AddDA(aD, anAngle, E, F);
170                   }
171               }
172           }
173         }
174       }
175   }
176   else if (aType == CHAMFER_SHAPE_FACES || aType == CHAMFER_SHAPE_FACES_AD) {
177     // chamfer on all edges of the selected faces, with D1 on the selected face
178     // (on first selected face, if the edge belongs to two selected faces)
179
180     int aLen = aCI.GetLength();
181     int ind = 1;
182     TopTools_MapOfShape aMap;
183     TopTools_IndexedDataMapOfShapeListOfShape M;
184     GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
185     for (; ind <= aLen; ind++)
186     {
187       TopoDS_Shape aShapeFace;
188       if (GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace(ind), aShapeFace))
189         {
190           TopoDS_Face F = TopoDS::Face(aShapeFace);
191           TopExp_Explorer Exp (F, TopAbs_EDGE);
192           for (; Exp.More(); Exp.Next()) {
193             if (!aMap.Contains(Exp.Current()))
194               {
195                 TopoDS_Edge E = TopoDS::Edge(Exp.Current());
196                 if (!BRepTools::IsReallyClosed(E, F) &&
197                     !BRep_Tool::Degenerated(E) &&
198                     M.FindFromKey(E).Extent() == 2)
199                   if (aType == CHAMFER_SHAPE_FACES)
200                     {
201                       double aD1 = aCI.GetD1();
202                       double aD2 = aCI.GetD2();
203                       fill.Add(aD1, aD2, E, F);
204                     }
205                   else
206                     {
207                       double aD = aCI.GetD();
208                       double anAngle = aCI.GetAngle();
209                       if ( (anAngle > 0) && (anAngle < (Standard_PI/2)) )
210                         fill.AddDA(aD, anAngle, E, F);
211                     }
212               }
213           }
214         }
215     }
216   }  
217 else if (aType == CHAMFER_SHAPE_EDGES || aType == CHAMFER_SHAPE_EDGES_AD)
218   {
219     // chamfer on selected edges with lenght param D1 & D2.
220
221     int aLen = aCI.GetLength();
222     int ind = 1;
223     TopTools_MapOfShape aMap;
224     TopTools_IndexedDataMapOfShapeListOfShape M;
225     GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
226     for (; ind <= aLen; ind++)
227     {
228       TopoDS_Shape aShapeEdge;
229       if (GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetEdge(ind), aShapeEdge))
230         {
231           TopoDS_Edge E = TopoDS::Edge(aShapeEdge);
232           const TopTools_ListOfShape& aFacesList = M.FindFromKey(E);
233           TopoDS_Face F = TopoDS::Face( aFacesList.First() );
234           if (aType == CHAMFER_SHAPE_EDGES)
235             {
236               double aD1 = aCI.GetD1();
237               double aD2 = aCI.GetD2();
238               fill.Add(aD1, aD2, E, F);
239             }
240           else
241             {
242               double aD = aCI.GetD();
243               double anAngle = aCI.GetAngle();
244               if ( (anAngle > 0) && (anAngle < (Standard_PI/2)) )
245                 fill.AddDA(aD, anAngle, E, F);
246             }
247         } 
248     } 
249   }
250   else {
251   }
252
253   fill.Build();
254   if (!fill.IsDone()) {
255     StdFail_NotDone::Raise("Chamfer can not be computed on the given shape with the given parameters");
256   }
257   aShape = fill.Shape();
258
259   if (aShape.IsNull()) return 0;
260
261   aFunction->SetValue(aShape);
262
263   log.SetTouched(Label());
264
265   return 1;
266 }
267
268
269 //=======================================================================
270 //function :  GEOMImpl_ChamferDriver_Type_
271 //purpose  :
272 //=======================================================================
273 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ChamferDriver_Type_()
274 {
275
276   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
277   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
278   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
279   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
280   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
281   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
282
283
284   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
285   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ChamferDriver",
286                                                          sizeof(GEOMImpl_ChamferDriver),
287                                                          1,
288                                                          (Standard_Address)_Ancestors,
289                                                          (Standard_Address)NULL);
290
291   return _aType;
292 }
293
294 //=======================================================================
295 //function : DownCast
296 //purpose  :
297 //=======================================================================
298 const Handle(GEOMImpl_ChamferDriver) Handle(GEOMImpl_ChamferDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
299 {
300   Handle(GEOMImpl_ChamferDriver) _anOtherObject;
301
302   if (!AnObject.IsNull()) {
303      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ChamferDriver))) {
304        _anOtherObject = Handle(GEOMImpl_ChamferDriver)((Handle(GEOMImpl_ChamferDriver)&)AnObject);
305      }
306   }
307
308   return _anOtherObject ;
309 }