1 // File: DNaming_CylinderDriver.cxx
2 // Created: Mon May 4 12:02:03 2009
3 // Author: Sergey ZARITCHNY <sergey.zaritchny@opencascade.com>
4 // Copyright: Open CasCade SA 2009
8 #include <DNaming_CylinderDriver.ixx>
9 #include <TFunction_Function.hxx>
10 #include <TDataStd_Real.hxx>
11 #include <TDataStd_Integer.hxx>
12 #include <TFunction_Logbook.hxx>
13 #include <TNaming.hxx>
14 #include <TNaming_NamedShape.hxx>
15 #include <BRepPrimAPI_MakeCylinder.hxx>
16 #include <BRepCheck_Analyzer.hxx>
17 #include <TopLoc_Location.hxx>
18 #include <TopoDS_Solid.hxx>
19 #include <Standard_GUID.hxx>
20 #include <Standard_Real.hxx>
21 #include <TDF_Label.hxx>
22 #include <ModelDefinitions.hxx>
24 #include <DNaming.hxx>
25 #include <TNaming_Builder.hxx>
27 #include <TopExp_Explorer.hxx>
28 #include <BRepAdaptor_Curve.hxx>
31 #include <Geom_TrimmedCurve.hxx>
32 #include <Geom_Line.hxx>
33 //=======================================================================
34 //function : DNaming_CylinderDriver
35 //purpose : Constructor
36 //=======================================================================
37 DNaming_CylinderDriver::DNaming_CylinderDriver()
40 //=======================================================================
42 //purpose : Validates labels of a function in <log>.
43 //=======================================================================
44 void DNaming_CylinderDriver::Validate(TFunction_Logbook& theLog) const
47 //=======================================================================
48 //function : MustExecute
49 //purpose : Analyse in <log> if the loaded function must be executed
50 //=======================================================================
51 Standard_Boolean DNaming_CylinderDriver::MustExecute(const TFunction_Logbook& theLog) const
56 //=======================================================================
58 //purpose : Execute the function and push in <log> the impacted labels
59 //=======================================================================
60 Standard_Integer DNaming_CylinderDriver::Execute(TFunction_Logbook& theLog) const
62 Handle(TFunction_Function) aFunction;
63 Label().FindAttribute(TFunction_Function::GetID(),aFunction);
64 if(aFunction.IsNull()) return -1;
68 Standard_Real aRadius = DNaming::GetReal(aFunction,CYL_RADIUS)->Get();
69 Standard_Real aHeight = DNaming::GetReal(aFunction,CYL_HEIGHT)->Get();
70 Handle(TDataStd_UAttribute) anObject = DNaming::GetObjectArg(aFunction,CYL_AXIS);
71 Handle(TNaming_NamedShape) aNSAxis = DNaming::GetObjectValue(anObject);
72 if (aNSAxis->IsEmpty()) {
74 cout<<"CylinderDriver:: Axis is empty"<<endl;
76 aFunction->SetFailure(WRONG_AXIS);
79 TopoDS_Shape aTopoDSAxis = aNSAxis->Get();
80 if (aTopoDSAxis.IsNull()) {
82 cout<<"CylinderDriver:: Axis is null"<<endl;
84 aFunction->SetFailure(WRONG_AXIS);
87 // Creation of gp axis (gp_Ax2):
88 if (aTopoDSAxis.ShapeType() != TopAbs_EDGE && aTopoDSAxis.ShapeType() != TopAbs_WIRE) {
90 cout<<"CylinderDriver:: Wrong axis, ShapeType = " << aTopoDSAxis.ShapeType() <<endl;
92 aFunction->SetFailure(WRONG_AXIS);
97 if (aTopoDSAxis.ShapeType() == TopAbs_WIRE) {
98 TopExp_Explorer anExplorer(aTopoDSAxis, TopAbs_EDGE);
99 aTopoDSAxis = anExplorer.Current();
102 BRepAdaptor_Curve aCurveAda(TopoDS::Edge(aTopoDSAxis));
103 if (aCurveAda.GetType() == GeomAbs_Line) {
104 gp_Lin aLin = aCurveAda.Line();
105 anAxis = gp_Ax2(aLin.Location(), aLin.Direction());
106 if(!aTopoDSAxis.Infinite()) {
107 TopoDS_Vertex V1, V2;
108 TopExp::Vertices(TopoDS::Edge(aTopoDSAxis), V1, V2);
109 gp_Pnt aP1 = BRep_Tool::Pnt(V1);
110 anAxis.SetLocation(aP1);
114 cout<<"CylinderDriver:: I don't support wires for a while"<<endl;
116 aFunction->SetFailure(WRONG_AXIS);
120 Handle(TNaming_NamedShape) aPrevCyl = DNaming::GetFunctionResult(aFunction);
122 TopLoc_Location aLocation;
123 if (!aPrevCyl.IsNull() && !aPrevCyl->IsEmpty()) {
124 aLocation = aPrevCyl->Get().Location();
127 BRepPrimAPI_MakeCylinder aMakeCylinder(anAxis, aRadius, aHeight);
128 aMakeCylinder.Build();
129 if (!aMakeCylinder.IsDone()) {
130 aFunction->SetFailure(ALGO_FAILED);
134 TopoDS_Shape aResult = aMakeCylinder.Solid();
135 BRepCheck_Analyzer aCheck(aResult);
136 if (!aCheck.IsValid(aResult)) {
137 aFunction->SetFailure(RESULT_NOT_VALID);
142 LoadNamingDS(RESPOSITION(aFunction), aMakeCylinder);
145 if(!aLocation.IsIdentity())
146 TNaming::Displace(RESPOSITION(aFunction), aLocation, Standard_True);
149 theLog.SetValid(RESPOSITION(aFunction), Standard_True);
150 aFunction->SetFailure(DONE);
155 //=======================================================================
156 //function : LoadAndName
158 //=======================================================================
159 void DNaming_CylinderDriver::LoadNamingDS (const TDF_Label& theResultLabel,
160 BRepPrimAPI_MakeCylinder& MS) const
162 TNaming_Builder Builder (theResultLabel);
163 Builder.Generated (MS.Solid());
165 BRepPrim_Cylinder& S = MS.Cylinder();
167 //Load faces of the Cyl :
169 TopoDS_Face BottomFace = S.BottomFace ();
170 TNaming_Builder BOF (theResultLabel.FindChild(1,Standard_True));
171 BOF.Generated (BottomFace);
175 TopoDS_Face TopFace = S.TopFace ();
176 TNaming_Builder TOF (theResultLabel.FindChild(2,Standard_True));
177 TOF.Generated (TopFace);
180 TopoDS_Face LateralFace = S.LateralFace();
181 TNaming_Builder LOF (theResultLabel.FindChild(3,Standard_True));
182 LOF.Generated(LateralFace);
185 TopoDS_Face StartFace = S.StartFace();
186 TNaming_Builder SF(theResultLabel.FindChild(4,Standard_True));
187 SF.Generated(StartFace);
188 TopoDS_Face EndFace = S.EndFace();
189 TNaming_Builder EF(theResultLabel.FindChild(5,Standard_True));
190 EF.Generated(EndFace);