aff5997d |
1 | // CylinderDriver.cpp: implementation of the CylinderDriver class. |
2 | // |
3 | ////////////////////////////////////////////////////////////////////// |
4 | |
5 | #include "CylinderDriver.h" |
6 | |
7 | #include <Standard_GUID.hxx> |
8 | #include <TDF_Reference.hxx> |
9 | #include <TNaming_Builder.hxx> |
10 | #include <TNaming_NamedShape.hxx> |
11 | #include <TDataStd_RealArray.hxx> |
12 | |
13 | #include <TopoDS.hxx> |
14 | #include <ElCLib.hxx> |
15 | #include <gp_Circ.hxx> |
16 | #include <Precision.hxx> |
17 | #include <TopoDS_Face.hxx> |
18 | #include <TopoDS_Edge.hxx> |
19 | #include <TopExp_Explorer.hxx> |
20 | #include <BRepAdaptor_Curve.hxx> |
21 | #include <BRepBuilderAPI_MakeEdge.hxx> |
22 | #include <BRepBuilderAPI_MakeWire.hxx> |
23 | #include <BRepBuilderAPI_MakeFace.hxx> |
24 | #include <BRepPrimAPI_MakeCylinder.hxx> |
25 | |
26 | IMPLEMENT_STANDARD_HANDLE(CylinderDriver,BaseDriver) |
27 | IMPLEMENT_STANDARD_RTTIEXT(CylinderDriver,BaseDriver) |
28 | |
29 | // ID of the function driver |
30 | const Standard_GUID& CylinderDriver::GetID() |
31 | { |
32 | static const Standard_GUID id("9CE1FCDA-C8EA-4b09-B1C4-021FCD44F3F3"); |
33 | return id; |
34 | } |
35 | |
36 | // Constructor |
37 | CylinderDriver::CylinderDriver() |
38 | { |
39 | |
40 | } |
41 | |
42 | // Execution. |
43 | Standard_Integer CylinderDriver::Execute(Handle(TFunction_Logbook)& log) const |
44 | { |
45 | // Usual check... |
46 | if (Label().IsNull()) |
47 | return 1; |
48 | |
49 | // Take the arguments (radius, angle, height) |
50 | Handle(TDataStd_RealArray) arr; |
51 | if (!Label().FindAttribute(TDataStd_RealArray::GetID(), arr)) |
52 | return 2; |
53 | double radius = arr->Value(1); |
54 | double angle = arr->Value(2); |
55 | double height = arr->Value(3); |
56 | if (radius < Precision::Confusion() || height < Precision::Confusion()) |
57 | return 3; |
58 | |
59 | // Take the arguments (top face) |
60 | Handle(TDF_Reference) ref; |
61 | if (!Label().FindChild(1).FindChild(1).FindAttribute(TDF_Reference::GetID(), ref)) |
62 | return 4; |
63 | Handle(TNaming_NamedShape) n; |
64 | if (!ref->Get().FindAttribute(TNaming_NamedShape::GetID(), n) || n->IsEmpty()) |
65 | return 5; |
66 | TopoDS_Face F = TopoDS::Face(n->Get()); |
67 | |
68 | // Take the circle |
69 | TopExp_Explorer expl(F, TopAbs_EDGE); |
70 | if (!expl.More()) |
71 | return 6; |
72 | TopoDS_Edge E = TopoDS::Edge(expl.Current()); |
73 | BRepAdaptor_Curve A(E); |
74 | gp_Circ C = A.Circle(); |
75 | |
76 | // Center of the cylinder |
77 | gp_Pnt p = ElCLib::Value(angle * M_PI / 180.0, C); |
78 | gp_Vec v(p, C.Location()); |
79 | v.Normalize(); |
80 | p.Translate(radius * v); |
81 | |
82 | // Make the result |
83 | BRepPrimAPI_MakeCylinder mkCylinder(gp_Ax2(p, gp::DZ()), radius, height); |
84 | mkCylinder.Build(); |
85 | if (!mkCylinder.IsDone()) |
86 | return 7; |
87 | TopoDS_Shape Cyl = mkCylinder.Shape(); |
88 | |
89 | // Make the top face of the cylinder for next functions |
90 | gp_Circ Ctop(gp_Ax2(p.Translated(height * gp::DZ()), gp::DZ()), radius); |
91 | TopoDS_Edge Etop = BRepBuilderAPI_MakeEdge(Ctop); |
92 | TopoDS_Wire Wtop = BRepBuilderAPI_MakeWire(Etop); |
93 | TopoDS_Face Ftop = BRepBuilderAPI_MakeFace(Wtop); |
94 | |
95 | // Set the result |
96 | TNaming_Builder B(Label()); |
97 | B.Generated(Cyl); |
98 | TNaming_Builder B2(Label().FindChild(3)); |
99 | B2.Generated(Ftop); |
100 | |
101 | return BaseDriver::Execute(log); |
102 | } |