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.
17 #include <BRepAdaptor_Curve.hxx>
18 #include <BRepCheck_Analyzer.hxx>
19 #include <BRepPrimAPI_MakeCylinder.hxx>
20 #include <DNaming.hxx>
21 #include <DNaming_CylinderDriver.hxx>
22 #include <Geom_Line.hxx>
23 #include <Geom_TrimmedCurve.hxx>
25 #include <ModelDefinitions.hxx>
26 #include <Standard_GUID.hxx>
27 #include <Standard_Real.hxx>
28 #include <Standard_Type.hxx>
29 #include <TDataStd_Integer.hxx>
30 #include <TDataStd_Real.hxx>
31 #include <TDF_Label.hxx>
32 #include <TFunction_Function.hxx>
33 #include <TFunction_Logbook.hxx>
34 #include <TNaming.hxx>
35 #include <TNaming_Builder.hxx>
36 #include <TNaming_NamedShape.hxx>
38 #include <TopExp_Explorer.hxx>
39 #include <TopLoc_Location.hxx>
41 #include <TopoDS_Solid.hxx>
43 //=======================================================================
44 //function : DNaming_CylinderDriver
45 //purpose : Constructor
46 //=======================================================================
47 DNaming_CylinderDriver::DNaming_CylinderDriver()
50 //=======================================================================
52 //purpose : Validates labels of a function in <log>.
53 //=======================================================================
54 void DNaming_CylinderDriver::Validate(TFunction_Logbook&) const
57 //=======================================================================
58 //function : MustExecute
59 //purpose : Analyse in <log> if the loaded function must be executed
60 //=======================================================================
61 Standard_Boolean DNaming_CylinderDriver::MustExecute(const TFunction_Logbook&) const
66 //=======================================================================
68 //purpose : Execute the function and push in <log> the impacted labels
69 //=======================================================================
70 Standard_Integer DNaming_CylinderDriver::Execute(TFunction_Logbook& theLog) const
72 Handle(TFunction_Function) aFunction;
73 Label().FindAttribute(TFunction_Function::GetID(),aFunction);
74 if(aFunction.IsNull()) return -1;
78 Standard_Real aRadius = DNaming::GetReal(aFunction,CYL_RADIUS)->Get();
79 Standard_Real aHeight = DNaming::GetReal(aFunction,CYL_HEIGHT)->Get();
80 Handle(TDataStd_UAttribute) anObject = DNaming::GetObjectArg(aFunction,CYL_AXIS);
81 Handle(TNaming_NamedShape) aNSAxis = DNaming::GetObjectValue(anObject);
82 if (aNSAxis->IsEmpty()) {
84 cout<<"CylinderDriver:: Axis is empty"<<endl;
86 aFunction->SetFailure(WRONG_AXIS);
89 TopoDS_Shape aTopoDSAxis = aNSAxis->Get();
90 if (aTopoDSAxis.IsNull()) {
92 cout<<"CylinderDriver:: Axis is null"<<endl;
94 aFunction->SetFailure(WRONG_AXIS);
97 // Creation of gp axis (gp_Ax2):
98 if (aTopoDSAxis.ShapeType() != TopAbs_EDGE && aTopoDSAxis.ShapeType() != TopAbs_WIRE) {
100 cout<<"CylinderDriver:: Wrong axis, ShapeType = " << aTopoDSAxis.ShapeType() <<endl;
102 aFunction->SetFailure(WRONG_AXIS);
107 if (aTopoDSAxis.ShapeType() == TopAbs_WIRE) {
108 TopExp_Explorer anExplorer(aTopoDSAxis, TopAbs_EDGE);
109 aTopoDSAxis = anExplorer.Current();
112 BRepAdaptor_Curve aCurveAda(TopoDS::Edge(aTopoDSAxis));
113 if (aCurveAda.GetType() == GeomAbs_Line) {
114 gp_Lin aLin = aCurveAda.Line();
115 anAxis = gp_Ax2(aLin.Location(), aLin.Direction());
116 if(!aTopoDSAxis.Infinite()) {
117 TopoDS_Vertex V1, V2;
118 TopExp::Vertices(TopoDS::Edge(aTopoDSAxis), V1, V2);
119 gp_Pnt aP1 = BRep_Tool::Pnt(V1);
120 anAxis.SetLocation(aP1);
124 cout<<"CylinderDriver:: I don't support wires for a while"<<endl;
126 aFunction->SetFailure(WRONG_AXIS);
130 Handle(TNaming_NamedShape) aPrevCyl = DNaming::GetFunctionResult(aFunction);
132 TopLoc_Location aLocation;
133 if (!aPrevCyl.IsNull() && !aPrevCyl->IsEmpty()) {
134 aLocation = aPrevCyl->Get().Location();
137 BRepPrimAPI_MakeCylinder aMakeCylinder(anAxis, aRadius, aHeight);
138 aMakeCylinder.Build();
139 if (!aMakeCylinder.IsDone()) {
140 aFunction->SetFailure(ALGO_FAILED);
144 TopoDS_Shape aResult = aMakeCylinder.Solid();
145 BRepCheck_Analyzer aCheck(aResult);
146 if (!aCheck.IsValid(aResult)) {
147 aFunction->SetFailure(RESULT_NOT_VALID);
152 LoadNamingDS(RESPOSITION(aFunction), aMakeCylinder);
155 if(!aLocation.IsIdentity())
156 TNaming::Displace(RESPOSITION(aFunction), aLocation, Standard_True);
159 theLog.SetValid(RESPOSITION(aFunction), Standard_True);
160 aFunction->SetFailure(DONE);
165 //=======================================================================
166 //function : LoadAndName
168 //=======================================================================
169 void DNaming_CylinderDriver::LoadNamingDS (const TDF_Label& theResultLabel,
170 BRepPrimAPI_MakeCylinder& MS) const
172 TNaming_Builder Builder (theResultLabel);
173 Builder.Generated (MS.Solid());
175 BRepPrim_Cylinder& S = MS.Cylinder();
177 //Load faces of the Cyl :
179 TopoDS_Face BottomFace = S.BottomFace ();
180 TNaming_Builder BOF (theResultLabel.FindChild(1,Standard_True));
181 BOF.Generated (BottomFace);
185 TopoDS_Face TopFace = S.TopFace ();
186 TNaming_Builder TOF (theResultLabel.FindChild(2,Standard_True));
187 TOF.Generated (TopFace);
190 TopoDS_Face LateralFace = S.LateralFace();
191 TNaming_Builder LOF (theResultLabel.FindChild(3,Standard_True));
192 LOF.Generated(LateralFace);
195 TopoDS_Face StartFace = S.StartFace();
196 TNaming_Builder SF(theResultLabel.FindChild(4,Standard_True));
197 SF.Generated(StartFace);
198 TopoDS_Face EndFace = S.EndFace();
199 TNaming_Builder EF(theResultLabel.FindChild(5,Standard_True));
200 EF.Generated(EndFace);