1 // Created on: 2009-05-04
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 2009-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <DNaming_CylinderDriver.ixx>
17 #include <TFunction_Function.hxx>
18 #include <TDataStd_Real.hxx>
19 #include <TDataStd_Integer.hxx>
20 #include <TFunction_Logbook.hxx>
21 #include <TNaming.hxx>
22 #include <TNaming_NamedShape.hxx>
23 #include <BRepPrimAPI_MakeCylinder.hxx>
24 #include <BRepCheck_Analyzer.hxx>
25 #include <TopLoc_Location.hxx>
26 #include <TopoDS_Solid.hxx>
27 #include <Standard_GUID.hxx>
28 #include <Standard_Real.hxx>
29 #include <TDF_Label.hxx>
30 #include <ModelDefinitions.hxx>
32 #include <DNaming.hxx>
33 #include <TNaming_Builder.hxx>
35 #include <TopExp_Explorer.hxx>
36 #include <BRepAdaptor_Curve.hxx>
39 #include <Geom_TrimmedCurve.hxx>
40 #include <Geom_Line.hxx>
41 //=======================================================================
42 //function : DNaming_CylinderDriver
43 //purpose : Constructor
44 //=======================================================================
45 DNaming_CylinderDriver::DNaming_CylinderDriver()
48 //=======================================================================
50 //purpose : Validates labels of a function in <log>.
51 //=======================================================================
52 void DNaming_CylinderDriver::Validate(TFunction_Logbook&) const
55 //=======================================================================
56 //function : MustExecute
57 //purpose : Analyse in <log> if the loaded function must be executed
58 //=======================================================================
59 Standard_Boolean DNaming_CylinderDriver::MustExecute(const TFunction_Logbook&) const
64 //=======================================================================
66 //purpose : Execute the function and push in <log> the impacted labels
67 //=======================================================================
68 Standard_Integer DNaming_CylinderDriver::Execute(TFunction_Logbook& theLog) const
70 Handle(TFunction_Function) aFunction;
71 Label().FindAttribute(TFunction_Function::GetID(),aFunction);
72 if(aFunction.IsNull()) return -1;
76 Standard_Real aRadius = DNaming::GetReal(aFunction,CYL_RADIUS)->Get();
77 Standard_Real aHeight = DNaming::GetReal(aFunction,CYL_HEIGHT)->Get();
78 Handle(TDataStd_UAttribute) anObject = DNaming::GetObjectArg(aFunction,CYL_AXIS);
79 Handle(TNaming_NamedShape) aNSAxis = DNaming::GetObjectValue(anObject);
80 if (aNSAxis->IsEmpty()) {
82 cout<<"CylinderDriver:: Axis is empty"<<endl;
84 aFunction->SetFailure(WRONG_AXIS);
87 TopoDS_Shape aTopoDSAxis = aNSAxis->Get();
88 if (aTopoDSAxis.IsNull()) {
90 cout<<"CylinderDriver:: Axis is null"<<endl;
92 aFunction->SetFailure(WRONG_AXIS);
95 // Creation of gp axis (gp_Ax2):
96 if (aTopoDSAxis.ShapeType() != TopAbs_EDGE && aTopoDSAxis.ShapeType() != TopAbs_WIRE) {
98 cout<<"CylinderDriver:: Wrong axis, ShapeType = " << aTopoDSAxis.ShapeType() <<endl;
100 aFunction->SetFailure(WRONG_AXIS);
105 if (aTopoDSAxis.ShapeType() == TopAbs_WIRE) {
106 TopExp_Explorer anExplorer(aTopoDSAxis, TopAbs_EDGE);
107 aTopoDSAxis = anExplorer.Current();
110 BRepAdaptor_Curve aCurveAda(TopoDS::Edge(aTopoDSAxis));
111 if (aCurveAda.GetType() == GeomAbs_Line) {
112 gp_Lin aLin = aCurveAda.Line();
113 anAxis = gp_Ax2(aLin.Location(), aLin.Direction());
114 if(!aTopoDSAxis.Infinite()) {
115 TopoDS_Vertex V1, V2;
116 TopExp::Vertices(TopoDS::Edge(aTopoDSAxis), V1, V2);
117 gp_Pnt aP1 = BRep_Tool::Pnt(V1);
118 anAxis.SetLocation(aP1);
122 cout<<"CylinderDriver:: I don't support wires for a while"<<endl;
124 aFunction->SetFailure(WRONG_AXIS);
128 Handle(TNaming_NamedShape) aPrevCyl = DNaming::GetFunctionResult(aFunction);
130 TopLoc_Location aLocation;
131 if (!aPrevCyl.IsNull() && !aPrevCyl->IsEmpty()) {
132 aLocation = aPrevCyl->Get().Location();
135 BRepPrimAPI_MakeCylinder aMakeCylinder(anAxis, aRadius, aHeight);
136 aMakeCylinder.Build();
137 if (!aMakeCylinder.IsDone()) {
138 aFunction->SetFailure(ALGO_FAILED);
142 TopoDS_Shape aResult = aMakeCylinder.Solid();
143 BRepCheck_Analyzer aCheck(aResult);
144 if (!aCheck.IsValid(aResult)) {
145 aFunction->SetFailure(RESULT_NOT_VALID);
150 LoadNamingDS(RESPOSITION(aFunction), aMakeCylinder);
153 if(!aLocation.IsIdentity())
154 TNaming::Displace(RESPOSITION(aFunction), aLocation, Standard_True);
157 theLog.SetValid(RESPOSITION(aFunction), Standard_True);
158 aFunction->SetFailure(DONE);
163 //=======================================================================
164 //function : LoadAndName
166 //=======================================================================
167 void DNaming_CylinderDriver::LoadNamingDS (const TDF_Label& theResultLabel,
168 BRepPrimAPI_MakeCylinder& MS) const
170 TNaming_Builder Builder (theResultLabel);
171 Builder.Generated (MS.Solid());
173 BRepPrim_Cylinder& S = MS.Cylinder();
175 //Load faces of the Cyl :
177 TopoDS_Face BottomFace = S.BottomFace ();
178 TNaming_Builder BOF (theResultLabel.FindChild(1,Standard_True));
179 BOF.Generated (BottomFace);
183 TopoDS_Face TopFace = S.TopFace ();
184 TNaming_Builder TOF (theResultLabel.FindChild(2,Standard_True));
185 TOF.Generated (TopFace);
188 TopoDS_Face LateralFace = S.LateralFace();
189 TNaming_Builder LOF (theResultLabel.FindChild(3,Standard_True));
190 LOF.Generated(LateralFace);
193 TopoDS_Face StartFace = S.StartFace();
194 TNaming_Builder SF(theResultLabel.FindChild(4,Standard_True));
195 SF.Generated(StartFace);
196 TopoDS_Face EndFace = S.EndFace();
197 TNaming_Builder EF(theResultLabel.FindChild(5,Standard_True));
198 EF.Generated(EndFace);