b311480e |
1 | // Created on: 2009-05-06 |
2 | // Created by: Sergey ZARITCHNY |
973c2be1 |
3 | // Copyright (c) 2009-2014 OPEN CASCADE SAS |
b311480e |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
b311480e |
6 | // |
d5f74e42 |
7 | // This library is free software; you can redistribute it and/or modify it under |
8 | // the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
9 | // by the Free Software Foundation, with special exception defined in the file |
10 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
11 | // distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
7fd59977 |
15 | |
16 | #include <DNaming_FilletDriver.ixx> |
17 | #include <Precision.hxx> |
18 | #include <Standard_GUID.hxx> |
19 | #include <Standard_Real.hxx> |
20 | #include <TopExp_Explorer.hxx> |
21 | #include <TopTools_MapOfShape.hxx> |
22 | #include <TopTools_ListOfShape.hxx> |
23 | #include <BRepAlgo.hxx> |
24 | #include <BRepFilletAPI_MakeFillet.hxx> |
25 | #include <TopoDS.hxx> |
26 | #include <TopoDS_Shape.hxx> |
27 | #include <TopoDS_Solid.hxx> |
28 | #include <TopoDS_Edge.hxx> |
29 | #include <TDF_Label.hxx> |
30 | #include <TDataStd_Real.hxx> |
31 | #include <TDataStd_Integer.hxx> |
32 | #include <TNaming.hxx> |
33 | #include <TNaming_Iterator.hxx> |
34 | #include <TNaming_Builder.hxx> |
35 | #include <TNaming_NamedShape.hxx> |
36 | #include <TFunction_Logbook.hxx> |
37 | #include <TFunction_Function.hxx> |
38 | #include <DNaming.hxx> |
39 | #include <TopTools_DataMapOfShapeShape.hxx> |
40 | #include <ModelDefinitions.hxx> |
41 | |
42 | |
43 | //======================================================================= |
44 | //function : FilletDriver |
45 | //purpose : Constructor |
46 | //======================================================================= |
47 | DNaming_FilletDriver::DNaming_FilletDriver() |
48 | {} |
49 | |
50 | //======================================================================= |
51 | //function : Validate |
52 | //purpose : Validates labels of a function in <log>. |
53 | //======================================================================= |
35e08fe8 |
54 | void DNaming_FilletDriver::Validate(TFunction_Logbook&) const |
7fd59977 |
55 | {} |
56 | |
57 | //======================================================================= |
58 | //function : MustExecute |
59 | //purpose : Analyse in <log> if the loaded function must be executed |
60 | //======================================================================= |
35e08fe8 |
61 | Standard_Boolean DNaming_FilletDriver::MustExecute(const TFunction_Logbook&) const |
7fd59977 |
62 | { |
63 | return Standard_True; |
64 | } |
65 | |
66 | //======================================================================= |
67 | //function : Execute |
68 | //purpose : Execute the function and push in <log> the impacted labels |
69 | //======================================================================= |
70 | Standard_Integer DNaming_FilletDriver::Execute(TFunction_Logbook& theLog) const |
71 | { |
72 | Handle(TFunction_Function) aFunction; |
73 | Label().FindAttribute(TFunction_Function::GetID(),aFunction); |
74 | if(aFunction.IsNull()) return -1; |
75 | |
76 | Handle(TFunction_Function) aPrevFun = DNaming::GetPrevFunction(aFunction); |
77 | if(aPrevFun.IsNull()) return -1; |
78 | const TDF_Label& aLab = RESPOSITION(aPrevFun); |
79 | Handle(TNaming_NamedShape) aContextNS; |
80 | aLab.FindAttribute(TNaming_NamedShape::GetID(), aContextNS); |
81 | if (aContextNS.IsNull() || aContextNS->IsEmpty()) { |
63c629aa |
82 | #ifdef DNAMING_DEB |
7fd59977 |
83 | cout<<"FilletDriver:: Context is empty"<<endl; |
84 | #endif |
85 | aFunction->SetFailure(WRONG_ARGUMENT); |
86 | return -1; |
87 | } |
88 | |
89 | const Standard_Real aRadius = DNaming::GetReal(aFunction,FILLET_RADIUS)->Get(); |
90 | const ChFi3d_FilletShape aSurfaceType = |
91 | (ChFi3d_FilletShape) DNaming::GetInteger(aFunction,FILLET_SURFTYPE)->Get(); |
92 | |
93 | if(aRadius < Precision::Confusion()) { |
94 | aFunction->SetFailure(WRONG_ARGUMENT); |
63c629aa |
95 | #ifdef DNAMING_DEB |
7fd59977 |
96 | cout << "FilletDriver:: Radius < Precision::Confusion" << endl; |
97 | #endif |
98 | return -1; |
99 | } |
100 | |
101 | |
102 | Handle(TDataStd_UAttribute) aPathObj = DNaming::GetObjectArg(aFunction,FILLET_PATH); |
103 | Handle(TNaming_NamedShape) aPathNS = DNaming::GetObjectValue(aPathObj); |
104 | if (aPathNS.IsNull() || aPathNS->IsEmpty()) { |
63c629aa |
105 | #ifdef DNAMING_DEB |
7fd59977 |
106 | cout<<"FilletDriver:: Path is empty"<<endl; |
107 | #endif |
108 | aFunction->SetFailure(WRONG_ARGUMENT); |
109 | return -1; |
110 | } |
111 | |
112 | TopoDS_Shape aPATH = aPathNS->Get(); |
113 | TopoDS_Shape aCONTEXT = aContextNS->Get(); |
114 | if (aPATH.IsNull() || aCONTEXT.IsNull()) { |
63c629aa |
115 | #ifdef DNAMING_DEB |
7fd59977 |
116 | cout<<"FilletDriver:: Path or Context is null"<<endl; |
117 | #endif |
118 | aFunction->SetFailure(WRONG_ARGUMENT); |
119 | return -1; |
120 | } |
121 | |
122 | TopExp_Explorer expl; |
123 | TopTools_MapOfShape View; |
124 | |
125 | BRepFilletAPI_MakeFillet aMkFillet(aCONTEXT, aSurfaceType); |
126 | |
127 | if(aPATH.ShapeType() != TopAbs_EDGE && aPATH.ShapeType() != TopAbs_FACE) { |
128 | aFunction->SetFailure(WRONG_ARGUMENT); |
129 | return -1; |
130 | } |
131 | |
132 | if(aPATH.ShapeType() == TopAbs_FACE) { |
133 | for (expl.Init(aPATH, TopAbs_EDGE); expl.More(); expl.Next()){ |
134 | const TopoDS_Edge& anEdge = TopoDS::Edge(expl.Current()); |
135 | if (!View.Add(anEdge)) continue; |
136 | else |
137 | aMkFillet.Add(aRadius, anEdge); //Edge |
138 | } |
139 | } |
140 | else { |
141 | const TopoDS_Edge& anEdge = TopoDS::Edge(aPATH); |
142 | aMkFillet.Add(aRadius, anEdge); //Edge |
143 | } |
144 | |
145 | |
146 | aMkFillet.Build(); |
147 | |
148 | if (!aMkFillet.IsDone()) { |
149 | aFunction->SetFailure(ALGO_FAILED); |
150 | return -1; |
151 | } |
152 | TopTools_ListOfShape aLarg; |
153 | aLarg.Append(aCONTEXT); |
154 | if (!BRepAlgo::IsValid(aLarg, aMkFillet.Shape(),Standard_False,Standard_False)) { |
155 | aFunction->SetFailure(RESULT_NOT_VALID); |
156 | return -1; |
157 | } |
158 | |
159 | // Naming |
160 | LoadNamingDS(RESPOSITION(aFunction), aMkFillet, aCONTEXT); |
161 | |
162 | theLog.SetValid(RESPOSITION(aFunction),Standard_True); |
163 | aFunction->SetFailure(DONE); |
164 | return 0; |
165 | } |
166 | |
167 | //======================================================================= |
168 | //function : LoadAndName |
169 | //purpose : |
170 | //======================================================================= |
171 | void DNaming_FilletDriver::LoadNamingDS (const TDF_Label& theResultLabel, |
172 | BRepFilletAPI_MakeFillet& theMkFillet, |
173 | const TopoDS_Shape& theContext) const |
174 | { |
175 | TNaming_Builder aBuilder (theResultLabel); |
176 | TopoDS_Shape aResult = theMkFillet.Shape(); |
177 | |
178 | if (aResult.ShapeType() == TopAbs_COMPOUND) { |
179 | Standard_Integer nbSubResults = 0; |
180 | TopoDS_Iterator itr(aResult); |
181 | for (; itr.More(); itr.Next()) nbSubResults++; |
182 | if (nbSubResults == 1) { |
183 | itr.Initialize(aResult); |
184 | if (itr.More()) aResult = itr.Value(); |
185 | } |
186 | } |
187 | if (aResult.IsNull()) aBuilder.Generated(aResult); |
188 | else |
189 | aBuilder.Modify(theContext, aResult); |
190 | |
191 | TopTools_DataMapOfShapeShape SubShapes; |
192 | for (TopExp_Explorer Exp(aResult, TopAbs_FACE); Exp.More(); Exp.Next()) { |
193 | SubShapes.Bind(Exp.Current(),Exp.Current()); |
194 | } |
195 | |
196 | //New faces generated from edges |
197 | TNaming_Builder anEdgeBuilder(theResultLabel.FindChild(1,Standard_True)); |
198 | DNaming::LoadAndOrientGeneratedShapes(theMkFillet, theContext, TopAbs_EDGE, anEdgeBuilder, SubShapes); |
199 | |
200 | //Faces of the initial shape modified by theMkFillet |
201 | TNaming_Builder aFacesBuilder(theResultLabel.FindChild(2,Standard_True)); |
202 | DNaming::LoadAndOrientModifiedShapes(theMkFillet, theContext, TopAbs_FACE, aFacesBuilder, SubShapes); |
203 | |
204 | |
205 | //New faces generated from vertices (if exist) |
206 | TNaming_Builder aVFacesBuilder(theResultLabel.FindChild(3,Standard_True)); |
207 | DNaming::LoadAndOrientGeneratedShapes(theMkFillet, theContext, TopAbs_VERTEX, aVFacesBuilder, SubShapes); |
208 | |
209 | //Deleted faces of the initial shape |
210 | TNaming_Builder aDelBuilder(theResultLabel.FindChild(4,Standard_True)); |
211 | DNaming::LoadDeletedShapes(theMkFillet, theContext, TopAbs_FACE, aDelBuilder); |
212 | |
213 | } |