0031570: Samples - add Qt samples similar to standard MFC samples
[occt.git] / samples / OCCTOverview / code / TOcafFunction_CylDriver.cxx
1 // Copyright (c) 2020 OPEN CASCADE SAS
2 //
3 // This file is part of the examples of the Open CASCADE Technology software library.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
21
22 #include "TOcafFunction_CylDriver.h"
23
24 #include <BRepPrimAPI_MakeCylinder.hxx>
25 #include <Standard_GUID.hxx>
26 #include <TCollection_AsciiString.hxx>
27 #include <TDF_Tool.hxx>
28 #include <TDataStd_Real.hxx>
29 #include <TNaming_Builder.hxx>
30
31 //=======================================================================
32 //function : GetID
33 //purpose  :
34 //=======================================================================
35 const Standard_GUID& TOcafFunction_CylDriver::GetID()
36 {
37   static const Standard_GUID anID("22D22E53-D69A-11d4-8F1A-0060B0EE18E8");
38   return anID;
39 }
40
41 //=======================================================================
42 //function : TPartStd_CylDriver
43 //purpose  :
44 //=======================================================================
45 TOcafFunction_CylDriver::TOcafFunction_CylDriver()
46 {
47   //
48 }
49
50 //=======================================================================
51 //function : Validate
52 //purpose  :
53 //=======================================================================
54 void TOcafFunction_CylDriver::Validate (Handle(TFunction_Logbook)& log) const
55 {
56   // We validate the object label ( Label() ), all the arguments and the results of the object:
57   log->SetValid(Label(), Standard_True);
58 }
59
60 //=======================================================================
61 //function : MustExecute
62 //purpose  :
63 //=======================================================================
64 Standard_Boolean TOcafFunction_CylDriver::MustExecute(const Handle(TFunction_Logbook)& log) const
65 {
66   // If the object's label is modified:
67   if (log->IsModified(Label())) return Standard_True;
68
69   // Cylinder (in our simple case) has 5 arguments:
70   //
71   // Let's check them:
72   if (log->IsModified(Label().FindChild(1)))
73   {
74     return Standard_True; // radius.
75   }
76   if (log->IsModified(Label().FindChild(2)))
77   {
78     return Standard_True; // height,
79   }
80   if (log->IsModified(Label().FindChild(3)))
81   {
82     return Standard_True; // x.
83   }
84   if (log->IsModified(Label().FindChild(4)))
85   {
86     return Standard_True; // y,
87   }
88   if (log->IsModified(Label().FindChild(5)))
89   {
90     return Standard_True; // z.
91   }
92   // if there are no any modifications concerned the Cyl,
93   // it's not necessary to recompute (to call the method Execute()):
94   return Standard_False;
95 }
96
97 //=======================================================================
98 //function : Execute
99 //purpose  :
100 //=======================================================================
101 Standard_Integer TOcafFunction_CylDriver::Execute(Handle(TFunction_Logbook)& /*log*/) const
102 {
103   // Get the values of dimension and position attributes
104   Handle(TDataStd_Real) TSR;
105   Standard_Real x, y, z, r, h;
106   if (!Label().FindChild(1).FindAttribute(TDataStd_Real::GetID(), TSR))
107   {
108     return 1;
109   }
110   r = TSR->Get();
111
112   if (!Label().FindChild(2).FindAttribute(TDataStd_Real::GetID(), TSR))
113   {
114     return 1;
115   }
116   h = TSR->Get();
117
118   if (!Label().FindChild(3).FindAttribute(TDataStd_Real::GetID(), TSR))
119   {
120     return 1;
121   }
122   x = TSR->Get();
123
124   if (!Label().FindChild(4).FindAttribute(TDataStd_Real::GetID(), TSR))
125   {
126     return 1;
127   }
128   y = TSR->Get();
129
130   if (!Label().FindChild(5).FindAttribute(TDataStd_Real::GetID(), TSR))
131   {
132     return 1;
133   }
134   z = TSR->Get();
135
136   // Build a Cyl using the dimension and position attributes
137   BRepPrimAPI_MakeCylinder mkCyl(gp_Ax2(gp_Pnt(x, y, z), gp_Dir(0, 0, 1)), r, h);
138   TopoDS_Shape ResultShape = mkCyl.Shape();
139
140
141   // Build a TNaming_NamedShape using built Cyl
142   TNaming_Builder B(Label());
143   B.Generated(ResultShape);
144   // That's all:
145   // If there are no any mistakes we return 0:
146   return 0;
147 }