Salome HOME
d5ae1466696de9225801b16d064bb7da6192923b
[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 <BRepFilletAPI_MakeChamfer.hxx>
11 #include <BRep_Tool.hxx>
12 #include <BRepTools.hxx>
13
14 #include <TopAbs.hxx>
15 #include <TopoDS.hxx>
16 #include <TopoDS_Shape.hxx>
17 #include <TopoDS_Edge.hxx>
18 #include <TopoDS_Face.hxx>
19 #include <TopoDS_Iterator.hxx>
20 #include <TopExp.hxx>
21 #include <TopExp_Explorer.hxx>
22 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
23 #include <TopTools_MapOfShape.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 : Execute
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   // to do: check the shape type.
94   // It have to be shell, or solid, or compsolid, or compound of these shapes
95   if (!isGoodForChamfer(aShapeBase)) {
96     StdFail_NotDone::Raise("Wrong shape. Must be shell or solid, or compsolid or compound of these shapes");
97   }
98
99   BRepFilletAPI_MakeChamfer fill (aShapeBase);
100
101   if (aType == CHAMFER_SHAPE_ALL) {
102     // symmetric chamfer on all edges
103     double aD = aCI.GetD();
104     TopTools_IndexedDataMapOfShapeListOfShape M;
105     GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
106     for (int i = 1; i <= M.Extent(); i++) {
107       TopoDS_Edge E = TopoDS::Edge(M.FindKey(i));
108       TopoDS_Face F = TopoDS::Face(M.FindFromIndex(i).First());
109       if (!BRepTools::IsReallyClosed(E, F) &&
110           !BRep_Tool::Degenerated(E) &&
111           M.FindFromIndex(i).Extent() == 2)
112         fill.Add(aD, E, F);
113     }
114   } else if (aType == CHAMFER_SHAPE_EDGE) {
115     // chamfer on edges, common to two faces, with D1 on the first face
116     double aD1 = aCI.GetD1();
117     double aD2 = aCI.GetD2();
118     TopoDS_Shape aFace1, aFace2;
119     if (GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace1(), aFace1) &&
120         GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace2(), aFace2)) {
121       TopoDS_Face F = TopoDS::Face(aFace1);
122
123       // fill map of edges of the second face
124       TopTools_MapOfShape aMap;
125       TopExp_Explorer Exp2 (aFace2, TopAbs_EDGE);
126       for (; Exp2.More(); Exp2.Next()) {
127         aMap.Add(Exp2.Current());
128       }
129
130       // find edges of the first face, common with the second face
131       TopExp_Explorer Exp (aFace1, TopAbs_EDGE);
132       for (Exp; Exp.More(); Exp.Next()) {
133         if (aMap.Contains(Exp.Current())) {
134           TopoDS_Edge E = TopoDS::Edge(Exp.Current());
135           if (!BRepTools::IsReallyClosed(E, F) && !BRep_Tool::Degenerated(E))
136             fill.Add(aD1, aD2, E, F);
137         }
138       }
139     }
140   } else if (aType == CHAMFER_SHAPE_FACES) {
141     // chamfer on all edges of the selected faces, with D1 on the selected face
142     // (on first selected face, if the edge belongs to two selected faces)
143     double aD1 = aCI.GetD1();
144     double aD2 = aCI.GetD2();
145     int aLen = aCI.GetLength();
146     int ind = 1;
147     TopTools_MapOfShape aMap;
148     TopTools_IndexedDataMapOfShapeListOfShape M;
149     GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
150     for (; ind <= aLen; ind++)
151     {
152       TopoDS_Shape aShapeFace;
153       if (GEOMImpl_ILocalOperations::GetSubShape
154           (aShapeBase, aCI.GetFace(ind), aShapeFace)) {
155         TopoDS_Face F = TopoDS::Face(aShapeFace);
156         TopExp_Explorer Exp (F, TopAbs_EDGE);
157         for (Exp; Exp.More(); Exp.Next()) {
158           if (!aMap.Contains(Exp.Current())) {
159             TopoDS_Edge E = TopoDS::Edge(Exp.Current());
160             if (!BRepTools::IsReallyClosed(E, F) &&
161                 !BRep_Tool::Degenerated(E) &&
162                 M.FindFromKey(E).Extent() == 2)
163               fill.Add(aD1, aD2, E, F);
164           }
165         }
166       }
167     }
168   } else {
169   }
170
171   fill.Build();
172   if (!fill.IsDone()) {
173     StdFail_NotDone::Raise("Chamfer can not be computed on the given shape with the given parameters");
174   }
175   aShape = fill.Shape();
176
177   if (aShape.IsNull()) return 0;
178
179   aFunction->SetValue(aShape);
180
181   log.SetTouched(Label());
182
183   return 1;
184 }
185
186
187 //=======================================================================
188 //function :  GEOMImpl_ChamferDriver_Type_
189 //purpose  :
190 //=======================================================================
191 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ChamferDriver_Type_()
192 {
193
194   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
195   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
196   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
197   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
198   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
199   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
200
201
202   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
203   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ChamferDriver",
204                                                          sizeof(GEOMImpl_ChamferDriver),
205                                                          1,
206                                                          (Standard_Address)_Ancestors,
207                                                          (Standard_Address)NULL);
208
209   return _aType;
210 }
211
212 //=======================================================================
213 //function : DownCast
214 //purpose  :
215 //=======================================================================
216 const Handle(GEOMImpl_ChamferDriver) Handle(GEOMImpl_ChamferDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
217 {
218   Handle(GEOMImpl_ChamferDriver) _anOtherObject;
219
220   if (!AnObject.IsNull()) {
221      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ChamferDriver))) {
222        _anOtherObject = Handle(GEOMImpl_ChamferDriver)((Handle(GEOMImpl_ChamferDriver)&)AnObject);
223      }
224   }
225
226   return _anOtherObject ;
227 }