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(Handle(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 Handle(TFunction_Logbook)&) const
66 //=======================================================================
68 //purpose : Execute the function and push in <log> the impacted labels
69 //=======================================================================
70 Standard_Integer DNaming_CylinderDriver::Execute(Handle(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);
158 theLog->SetValid(RESPOSITION(aFunction), Standard_True);
159 aFunction->SetFailure(DONE);
164 //=======================================================================
165 //function : LoadAndName
167 //=======================================================================
168 void DNaming_CylinderDriver::LoadNamingDS (const TDF_Label& theResultLabel,
169 BRepPrimAPI_MakeCylinder& MS) const
171 TNaming_Builder Builder (theResultLabel);
172 Builder.Generated (MS.Solid());
174 BRepPrim_Cylinder& S = MS.Cylinder();
176 //Load faces of the Cyl :
178 TopoDS_Face BottomFace = S.BottomFace ();
179 TNaming_Builder BOF (theResultLabel.FindChild(1,Standard_True));
180 BOF.Generated (BottomFace);
184 TopoDS_Face TopFace = S.TopFace ();
185 TNaming_Builder TOF (theResultLabel.FindChild(2,Standard_True));
186 TOF.Generated (TopFace);
189 TopoDS_Face LateralFace = S.LateralFace();
190 TNaming_Builder LOF (theResultLabel.FindChild(3,Standard_True));
191 LOF.Generated(LateralFace);
194 TopoDS_Face StartFace = S.StartFace();
195 TNaming_Builder SF(theResultLabel.FindChild(4,Standard_True));
196 SF.Generated(StartFace);
197 TopoDS_Face EndFace = S.EndFace();
198 TNaming_Builder EF(theResultLabel.FindChild(5,Standard_True));
199 EF.Generated(EndFace);