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_BoxDriver.h"
23
24 #include <BRepPrimAPI_MakeBox.hxx>
25 #include <Standard_GUID.hxx>
26 #include <TDataStd_Real.hxx>
27 #include <TNaming_Builder.hxx>
28
29 //=======================================================================
30 //function : GetID
31 //purpose :
32 //=======================================================================
GetID()33 const Standard_GUID& TOcafFunction_BoxDriver::GetID()
34 {
35 static const Standard_GUID anID("22D22E51-D69A-11d4-8F1A-0060B0EE18E8");
36 return anID;
37 }
38
39 //=======================================================================
40 //function : Validate
41 //purpose :
42 //=======================================================================
Validate(Handle (TFunction_Logbook)& log) const43 void TOcafFunction_BoxDriver::Validate(Handle(TFunction_Logbook)& log) const
44 {
45 // We validate the object label ( Label() ), all the arguments and the results of the object:
46 log->SetValid(Label(), Standard_True);
47 }
48
49 //=======================================================================
50 //function : MustExecute
51 //purpose :
52 //=======================================================================
MustExecute(const Handle (TFunction_Logbook)& log) const53 Standard_Boolean TOcafFunction_BoxDriver::MustExecute(const Handle(TFunction_Logbook)& log) const
54 {
55 // If the object's label is modified:
56 if (log->IsModified(Label())) return Standard_True;
57
58 // Cut (in our simple case) has two arguments: The original shape, and the tool shape.
59 // They are on the child labels of the box's label:
60 // So, OriginalNShape - is attached to the first child label
61 // ToolNShape - is attached to the second child label.
62 //
63 // Let's check them:
64 if (log->IsModified(Label().FindChild(1)))
65 {
66 return Standard_True; // width.
67 }
68 if (log->IsModified(Label().FindChild(2)))
69 {
70 return Standard_True; // length,
71 }
72 if (log->IsModified(Label().FindChild(3)))
73 {
74 return Standard_True; // width.
75 }
76 if (log->IsModified(Label().FindChild(4)))
77 {
78 return Standard_True; // length,
79 }
80 if (log->IsModified(Label().FindChild(5)))
81 {
82 return Standard_True; // width.
83 }
84 if (log->IsModified(Label().FindChild(6)))
85 {
86 return Standard_True; // length,
87 }
88 // if there are no any modifications concerned the box,
89 // it's not necessary to recompute (to call the method Execute()):
90 return Standard_False;
91 }
92
93 //=======================================================================
94 //function : Execute
95 //purpose :
96 //=======================================================================
Execute(Handle (TFunction_Logbook)&) const97 Standard_Integer TOcafFunction_BoxDriver::Execute(Handle(TFunction_Logbook)& /*log*/) const
98 {
99 // Get the values of dimension and position attributes
100 Handle(TDataStd_Real) TSR;
101 Standard_Real x, y, z, l, h, w;
102 if (!Label().FindChild(1).FindAttribute(TDataStd_Real::GetID(), TSR))
103 {
104 return 1;
105 }
106 l = TSR->Get();
107
108 if (!Label().FindChild(2).FindAttribute(TDataStd_Real::GetID(), TSR))
109 {
110 return 1;
111 }
112 h = TSR->Get();
113
114 if (!Label().FindChild(3).FindAttribute(TDataStd_Real::GetID(), TSR))
115 {
116 return 1;
117 }
118 w = TSR->Get();
119
120 if (!Label().FindChild(4).FindAttribute(TDataStd_Real::GetID(), TSR))
121 {
122 return 1;
123 }
124 x = TSR->Get();
125
126 if (!Label().FindChild(5).FindAttribute(TDataStd_Real::GetID(), TSR))
127 {
128 return 1;
129 }
130 y = TSR->Get();
131
132 if (!Label().FindChild(6).FindAttribute(TDataStd_Real::GetID(), TSR))
133 {
134 return 1;
135 }
136 z = TSR->Get();
137
138 // Build a box using the dimension and position attributes
139 BRepPrimAPI_MakeBox mkBox(gp_Pnt(x, y, z), l, h, w);
140 TopoDS_Shape ResultShape = mkBox.Shape();
141
142 // Build a TNaming_NamedShape using built box
143 TNaming_Builder B(Label());
144 B.Generated(ResultShape);
145 // That's all:
146 // If there are no any mistakes we return 0:
147 return 0;
148 }
149