Salome HOME
Merge with version on tag OCC-V2_1_0d
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ChamferDriver.cxx
1
2 using namespace std;
3 #include "GEOMImpl_ChamferDriver.hxx"
4 #include "GEOMImpl_IChamfer.hxx"
5 #include "GEOMImpl_Types.hxx"
6 #include "GEOMImpl_ILocalOperations.hxx"
7 #include "GEOM_Function.hxx"
8 #include "GEOMImpl_Block6Explorer.hxx"
9
10 #include <BRep_Tool.hxx>
11 #include <BRepTools.hxx>
12 #include <BRepFilletAPI_MakeChamfer.hxx>
13
14 #include <TopAbs.hxx>
15 #include <TopoDS.hxx>
16 #include <TopoDS_Edge.hxx>
17 #include <TopoDS_Face.hxx>
18 #include <TopoDS_Shape.hxx>
19 #include <TopoDS_Iterator.hxx>
20 #include <TopExp.hxx>
21 #include <TopExp_Explorer.hxx>
22 #include <TopTools_MapOfShape.hxx>
23 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
24
25 #include <Precision.hxx>
26 #include <gp_Pnt.hxx>
27 #include <StdFail_NotDone.hxx>
28
29 //=======================================================================
30 //function : GetID
31 //purpose  :
32 //=======================================================================
33 const Standard_GUID& GEOMImpl_ChamferDriver::GetID()
34 {
35   static Standard_GUID aChamferDriver("FF1BBB42-5D14-4df2-980B-3A668264EA16");
36   return aChamferDriver;
37 }
38
39
40 //=======================================================================
41 //function : GEOMImpl_ChamferDriver
42 //purpose  :
43 //=======================================================================
44 GEOMImpl_ChamferDriver::GEOMImpl_ChamferDriver()
45 {
46 }
47
48 //=======================================================================
49 //function : isGoodForChamfer
50 //purpose  :
51 //=======================================================================
52 static Standard_Boolean isGoodForChamfer (const TopoDS_Shape& theShape)
53 {
54   if (theShape.ShapeType() == TopAbs_SHELL ||
55       theShape.ShapeType() == TopAbs_SOLID ||
56       theShape.ShapeType() == TopAbs_COMPSOLID) {
57     return Standard_True;
58   }
59
60   if (theShape.ShapeType() == TopAbs_COMPOUND) {
61     TopTools_MapOfShape mapShape;
62     TopoDS_Iterator It (theShape, Standard_False, Standard_False);
63     for (; It.More(); It.Next()) {
64       if (mapShape.Add(It.Value())) {
65         if (!isGoodForChamfer(It.Value())) {
66           return Standard_False;
67         }
68       }
69     }
70     return Standard_True;
71   }
72
73   return Standard_False;
74 }
75
76 //=======================================================================
77 //function : Execute
78 //purpose  :
79 //=======================================================================
80 Standard_Integer GEOMImpl_ChamferDriver::Execute(TFunction_Logbook& log) const
81 {
82   if (Label().IsNull()) return 0;
83   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
84
85   GEOMImpl_IChamfer aCI (aFunction);
86   Standard_Integer aType = aFunction->GetType();
87
88   TopoDS_Shape aShape;
89
90   Handle(GEOM_Function) aRefShape = aCI.GetShape();
91   TopoDS_Shape aShapeBase = aRefShape->GetValue();
92
93   // Check the shape type. It have to be shell
94   // or solid, or compsolid, or compound of these shapes.
95   if (!isGoodForChamfer(aShapeBase)) {
96     StdFail_NotDone::Raise
97       ("Wrong shape. Must be shell or solid, or compsolid or compound of these shapes");
98   }
99
100   BRepFilletAPI_MakeChamfer fill (aShapeBase);
101
102   if (aType == CHAMFER_SHAPE_ALL) {
103     // symmetric chamfer on all edges
104     double aD = aCI.GetD();
105     TopTools_IndexedDataMapOfShapeListOfShape M;
106     GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
107     for (int i = 1; i <= M.Extent(); i++) {
108       TopoDS_Edge E = TopoDS::Edge(M.FindKey(i));
109       TopoDS_Face F = TopoDS::Face(M.FindFromIndex(i).First());
110       if (!BRepTools::IsReallyClosed(E, F) &&
111           !BRep_Tool::Degenerated(E) &&
112           M.FindFromIndex(i).Extent() == 2)
113         fill.Add(aD, E, F);
114     }
115   } else if (aType == CHAMFER_SHAPE_EDGE) {
116     // chamfer on edges, common to two faces, with D1 on the first face
117     double aD1 = aCI.GetD1();
118     double aD2 = aCI.GetD2();
119     TopoDS_Shape aFace1, aFace2;
120     if (GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace1(), aFace1) &&
121         GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace2(), aFace2)) {
122       TopoDS_Face F = TopoDS::Face(aFace1);
123
124       // fill map of edges of the second face
125       TopTools_MapOfShape aMap;
126       TopExp_Explorer Exp2 (aFace2, TopAbs_EDGE);
127       for (; Exp2.More(); Exp2.Next()) {
128         aMap.Add(Exp2.Current());
129       }
130
131       // find edges of the first face, common with the second face
132       TopExp_Explorer Exp (aFace1, TopAbs_EDGE);
133       for (; Exp.More(); Exp.Next()) {
134         if (aMap.Contains(Exp.Current())) {
135           TopoDS_Edge E = TopoDS::Edge(Exp.Current());
136           if (!BRepTools::IsReallyClosed(E, F) && !BRep_Tool::Degenerated(E))
137             fill.Add(aD1, aD2, E, F);
138         }
139       }
140     }
141   } else if (aType == CHAMFER_SHAPE_FACES) {
142     // chamfer on all edges of the selected faces, with D1 on the selected face
143     // (on first selected face, if the edge belongs to two selected faces)
144     double aD1 = aCI.GetD1();
145     double aD2 = aCI.GetD2();
146     int aLen = aCI.GetLength();
147     int ind = 1;
148     TopTools_MapOfShape aMap;
149     TopTools_IndexedDataMapOfShapeListOfShape M;
150     GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
151     for (; ind <= aLen; ind++)
152     {
153       TopoDS_Shape aShapeFace;
154       if (GEOMImpl_ILocalOperations::GetSubShape
155           (aShapeBase, aCI.GetFace(ind), aShapeFace)) {
156         TopoDS_Face F = TopoDS::Face(aShapeFace);
157         TopExp_Explorer Exp (F, TopAbs_EDGE);
158         for (; Exp.More(); Exp.Next()) {
159           if (!aMap.Contains(Exp.Current())) {
160             TopoDS_Edge E = TopoDS::Edge(Exp.Current());
161             if (!BRepTools::IsReallyClosed(E, F) &&
162                 !BRep_Tool::Degenerated(E) &&
163                 M.FindFromKey(E).Extent() == 2)
164               fill.Add(aD1, aD2, E, F);
165           }
166         }
167       }
168     }
169   } else {
170   }
171
172   fill.Build();
173   if (!fill.IsDone()) {
174     StdFail_NotDone::Raise("Chamfer can not be computed on the given shape with the given parameters");
175   }
176   aShape = fill.Shape();
177
178   if (aShape.IsNull()) return 0;
179
180   aFunction->SetValue(aShape);
181
182   log.SetTouched(Label());
183
184   return 1;
185 }
186
187
188 //=======================================================================
189 //function :  GEOMImpl_ChamferDriver_Type_
190 //purpose  :
191 //=======================================================================
192 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ChamferDriver_Type_()
193 {
194
195   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
196   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
197   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
198   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
199   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
200   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
201
202
203   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
204   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ChamferDriver",
205                                                          sizeof(GEOMImpl_ChamferDriver),
206                                                          1,
207                                                          (Standard_Address)_Ancestors,
208                                                          (Standard_Address)NULL);
209
210   return _aType;
211 }
212
213 //=======================================================================
214 //function : DownCast
215 //purpose  :
216 //=======================================================================
217 const Handle(GEOMImpl_ChamferDriver) Handle(GEOMImpl_ChamferDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
218 {
219   Handle(GEOMImpl_ChamferDriver) _anOtherObject;
220
221   if (!AnObject.IsNull()) {
222      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ChamferDriver))) {
223        _anOtherObject = Handle(GEOMImpl_ChamferDriver)((Handle(GEOMImpl_ChamferDriver)&)AnObject);
224      }
225   }
226
227   return _anOtherObject ;
228 }