0024943: Port MFC samples to UNICODE for compatibility with VS2013
[occt.git] / samples / mfc / standard / 06_Ocaf / src / TOcaf_Commands.cxx
CommitLineData
7fd59977 1// File: TOcaf_Commands.cxx
2// Created: Tue Feb 8 17:56:02 2000
3// Author: Modelistation
4// <model@brunox.paris1.matra-dtv.fr>
5
6
7#include <stdafx.h>
8#include <TOcaf_Commands.hxx>
9#include <TDF_Label.hxx>
10#include <TDF_TagSource.hxx>
11#include <TDataStd_Real.hxx>
12#include <TDataStd_Integer.hxx>
13#include <TDataStd_TreeNode.hxx>
14#include <TDataStd_ChildNodeIterator.hxx>
15#include <gp_Pnt.hxx>
16#include <gp_Sphere.hxx>
17#include <gp_Trsf.hxx>
18#include <TopoDS_Face.hxx>
19#include <TopoDS.hxx>
20#include <TopExp_Explorer.hxx>
21#include <TopLoc_Location.hxx>
22#include <TNaming_Tool.hxx>
23#include <BRep_Tool.hxx>
24#include <BRepPrimAPI_MakeSphere.hxx>
25#include <Geom_SphericalSurface.hxx>
26#include <AIS_InteractiveContext.hxx>
27#include <TPrsStd_AISViewer.hxx>
234e52be 28#include "TDataStd_Name.hxx"
29#include "TFunction_Function.hxx"
30#include "TOcafFunction_BoxDriver.hxx"
31#include "TFunction_DriverTable.hxx"
32#include "TOcafFunction_CylDriver.hxx"
33#include "TDF_Reference.hxx"
34#include "TOcafFunction_CutDriver.hxx"
7fd59977 35
36//=======================================================================
37//function : TOcaf_Commands
38//purpose :
39//=======================================================================
40
41TOcaf_Commands::TOcaf_Commands(const TDF_Label& Main)
42{
43 MainLab = Main;
44}
45
46//=======================================================================
47//function : CreateBox
48//purpose : Create a box containing a Named shape, a name, a Function
49// and a Data structure containing box conctruction parameters
50//=======================================================================
51
52TDF_Label TOcaf_Commands::CreateBox(Standard_Real x, Standard_Real y, Standard_Real z, Standard_Real w, Standard_Real l, Standard_Real h, const TCollection_ExtendedString& Name)
53{
54 // A data structure for our box:
55 // the box itself is attached to the BoxLabel label (as his name and his function attribute)
56 // its arguments (dimensions: width, length and height; and position: x, y, z)
57 // are attached to the child labels of the box:
58 //
59 // 0:1 Box Label ---> Name ---> Named shape ---> Function
60 // |
61 // 0:1:1 -- Width Label
62 // |
63 // 0:1:2 -- Length Label
64 // |
65 // 0:1:3 -- Height Label
66 // |
67 // 0:1:4 -- X Label
68 // |
69 // 0:1:5 -- Y Label
70 // |
71 // 0:1:6 -- Z Label
72
73 // Create a new label in the data structure for the box
74 TDF_Label L = TDF_TagSource::NewChild(MainLab);
75
76 // Create the data structure : Set the dimensions, position and name attributes
77 TDataStd_Real::Set(L.FindChild(1), w);
78 TDataStd_Real::Set(L.FindChild(2), l);
79 TDataStd_Real::Set(L.FindChild(3), h);
80 TDataStd_Real::Set(L.FindChild(4), x);
81 TDataStd_Real::Set(L.FindChild(5), y);
82 TDataStd_Real::Set(L.FindChild(6), z);
83 TDataStd_Name::Set(L, Name);
84
85
86 // Instanciate a TFunction_Function attribute connected to the current box driver
87 // and attach it to the data structure as an attribute of the Box Label
88 Handle(TFunction_Function) myFunction = TFunction_Function::Set(L, TOcafFunction_BoxDriver::GetID());
89
90 // Initialize and execute the box driver (look at the "Execute()" code)
91 TFunction_Logbook log;
92
93 Handle(TOcafFunction_BoxDriver) myBoxDriver;
94 // Find the TOcafFunction_BoxDriver in the TFunction_DriverTable using its GUID
95 if(!TFunction_DriverTable::Get()->FindDriver(TOcafFunction_BoxDriver::GetID(), myBoxDriver)) return L;
96
97 myBoxDriver->Init(L);
576f8b11 98 if (myBoxDriver->Execute(log)) MessageBoxW (NULL, L"DFunction_Execute : failed", L"Box", MB_ICONERROR);
7fd59977 99
100 return L;
101}
102
103
104//=======================================================================
105//function : CreateCyl
106//purpose : Create a cylinder containing a Named shape, a name, a Function
107// and a Data structure containing cylinder conctruction parameters
108//=======================================================================
109
110TDF_Label TOcaf_Commands::CreateCyl(Standard_Real x, Standard_Real y, Standard_Real z, Standard_Real r, Standard_Real h, const TCollection_ExtendedString& Name)
111{
112 // A data structure for our cylinder:
113 // the cylinder itself is attached to the CylinderLabel label (as his name and his function attribute)
114 // its arguments (dimensions: radius and height; and position: x, y, z)
115 // are attached to the child labels of the cylinder:
116 //
117 // 0:1 Cylinder Label ---> Name ---> Named shape ---> Function
118 // |
119 // 0:1:1 -- Radius Label
120 // |
121 // 0:1:2 -- Height Label
122 // |
123 // 0:1:3 -- X Label
124 // |
125 // 0:1:4 -- Y Label
126 // |
127 // 0:1:5 -- Z Label
128
129 // Create a new label in the data structure for the cylinder
130 TDF_Label L = TDF_TagSource::NewChild(MainLab);
131
132 // Create the data structure : Set the dimensions, position and name attributes
133 TDataStd_Real::Set(L.FindChild(1), r);
134 TDataStd_Real::Set(L.FindChild(2), h);
135 TDataStd_Real::Set(L.FindChild(3), x);
136 TDataStd_Real::Set(L.FindChild(4), y);
137 TDataStd_Real::Set(L.FindChild(5), z);
138 TDataStd_Name::Set(L, Name);
139
140
141 // Instanciate a TFunction_Function attribute connected to the current cylinder driver
142 // and attach it to the data structure as an attribute of the Cylinder Label
143 Handle(TFunction_Function) myFunction = TFunction_Function::Set(L, TOcafFunction_CylDriver::GetID());
144
145 // Initialize and execute the cylinder driver (look at the "Execute()" code)
146 TFunction_Logbook log;
147
148 Handle(TOcafFunction_CylDriver) myCylDriver;
149 // Find the TOcafFunction_CylDriver in the TFunction_DriverTable using its GUID
150 if(!TFunction_DriverTable::Get()->FindDriver(TOcafFunction_CylDriver::GetID(), myCylDriver)) return L;
151
152 myCylDriver->Init(L);
576f8b11 153 if (myCylDriver->Execute(log)) MessageBoxW (NULL, L"DFunction_Execute : failed", L"Cylinder", MB_ICONERROR);
7fd59977 154
155 return L;
156}
157
158
159TDF_Label TOcaf_Commands::ModifyBox(Standard_Real x, Standard_Real y, Standard_Real z, Standard_Real w, Standard_Real l, Standard_Real h, const TCollection_ExtendedString &Name, TFunction_Logbook &log)
160{
161 // Set the name attribute (if it's not null)
162 Handle(TDataStd_Name) myStdName;
163 if(Name.Length())
164 TDataStd_Name::Set(MainLab, Name);
165
166 // Set the dimensions and position attributes (if changed)
167 Handle(TDataStd_Real) curReal;
168 MainLab.FindChild(1).FindAttribute(TDataStd_Real::GetID(),curReal);
169 if(w!=curReal->Get())
170 {
171 TDataStd_Real::Set(MainLab.FindChild(1), w);
172 // Set the label of the attribute as touched for next recomputation
173 log.SetTouched(curReal->Label());
174 }
175
176 MainLab.FindChild(2).FindAttribute(TDataStd_Real::GetID(),curReal);
177 if(l!=curReal->Get())
178 {
179 TDataStd_Real::Set(MainLab.FindChild(2), l);
180 // Set the label of the attribute as touched for next recomputation
181 log.SetTouched(curReal->Label());
182 }
183
184 MainLab.FindChild(3).FindAttribute(TDataStd_Real::GetID(),curReal);
185 if(h!=curReal->Get())
186 {
187 TDataStd_Real::Set(MainLab.FindChild(3), h);
188 // Set the label of the attribute as touched for next recomputation
189 log.SetTouched(curReal->Label());
190 }
191
192 MainLab.FindChild(4).FindAttribute(TDataStd_Real::GetID(),curReal);
193 if(x!=curReal->Get())
194 {
195 TDataStd_Real::Set(MainLab.FindChild(4), x);
196 // Set the label of the attribute as touched for next recomputation
197 log.SetTouched(curReal->Label());
198 }
199
200 MainLab.FindChild(5).FindAttribute(TDataStd_Real::GetID(),curReal);
201 if(y!=curReal->Get())
202 {
203 TDataStd_Real::Set(MainLab.FindChild(5), y);
204 // Set the label of the attribute as touched for next recomputation
205 log.SetTouched(curReal->Label());
206 }
207
208 MainLab.FindChild(6).FindAttribute(TDataStd_Real::GetID(),curReal);
209 if(z!=curReal->Get())
210 {
211 TDataStd_Real::Set(MainLab.FindChild(6), z);
212 // Set the label of the attribute as touched for next recomputation
213 log.SetTouched(curReal->Label());
214 }
215
216 // Get the TFunction_Function used to create the box
217 Handle(TFunction_Function) TFF;
218 if ( !MainLab.FindAttribute(TFunction_Function::GetID(),TFF) )
219 {
576f8b11 220 MessageBox (NULL, L"Object cannot be modify.", L"Modification", MB_ICONEXCLAMATION);
7fd59977 221 return MainLab;
222 }
223
224 // Get the TFunction_FunctionDriver GUID used with the TFunction_Function
225 Standard_GUID myDriverID=TFF->GetDriverGUID();
226 Handle(TOcafFunction_BoxDriver) myBoxDriver;
227 // Find the TOcafFunction_BoxDriver in the TFunction_DriverTable using its GUID
228 TFunction_DriverTable::Get()->FindDriver(myDriverID, myBoxDriver);
229 // Execute the cut if it must be (if an attribute changes)
230 myBoxDriver->Init(MainLab);
231 if (myBoxDriver->MustExecute(log))
232 {
233 // Set the box touched, it will be usefull to recompute an object which used this box as attribute
234 log.SetTouched(MainLab);
235 if(myBoxDriver->Execute(log))
576f8b11 236 MessageBox (NULL, L"Recompute failed", L"Modify box", MB_ICONEXCLAMATION);
7fd59977 237 }
238
239 return MainLab;
240}
241
242TDF_Label TOcaf_Commands::ModifyCyl(Standard_Real x, Standard_Real y, Standard_Real z, Standard_Real r, Standard_Real h, const TCollection_ExtendedString &Name, TFunction_Logbook &log)
243{
244 // Set the name attribute (if it's not null)
245 Handle(TDataStd_Name) myStdName;
246 if(Name.Length())
247 TDataStd_Name::Set(MainLab, Name);
248
249 // Set the dimensions and position attributes (if changed)
250 Handle(TDataStd_Real) curReal;
251 MainLab.FindChild(1).FindAttribute(TDataStd_Real::GetID(),curReal);
252 if(r!=curReal->Get())
253 {
254 TDataStd_Real::Set(MainLab.FindChild(1), r);
255 // Set the label of the attribute as touched for next recomputation
256 log.SetTouched(curReal->Label());
257 }
258
259 MainLab.FindChild(2).FindAttribute(TDataStd_Real::GetID(),curReal);
260 if(h!=curReal->Get())
261 {
262 TDataStd_Real::Set(MainLab.FindChild(2), h);
263 // Set the label of the attribute as touched for next recomputation
264 log.SetTouched(curReal->Label());
265 }
266
267 MainLab.FindChild(3).FindAttribute(TDataStd_Real::GetID(),curReal);
268 if(x!=curReal->Get())
269 {
270 TDataStd_Real::Set(MainLab.FindChild(3), x);
271 // Set the label of the attribute as touched for next recomputation
272 log.SetTouched(curReal->Label());
273 }
274
275 MainLab.FindChild(4).FindAttribute(TDataStd_Real::GetID(),curReal);
276 if(y!=curReal->Get())
277 {
278 TDataStd_Real::Set(MainLab.FindChild(4), y);
279 // Set the label of the attribute as touched for next recomputation
280 log.SetTouched(curReal->Label());
281 }
282
283 MainLab.FindChild(5).FindAttribute(TDataStd_Real::GetID(),curReal);
284 if(z!=curReal->Get())
285 {
286 TDataStd_Real::Set(MainLab.FindChild(5), z);
287 // Set the label of the attribute as touched for next recomputation
288 log.SetTouched(curReal->Label());
289 }
290
291 // Get the TFunction_Function used to create the cylinder
292 Handle(TFunction_Function) TFF;
293 if ( !MainLab.FindAttribute(TFunction_Function::GetID(),TFF) )
294 {
576f8b11 295 MessageBoxW (NULL, L"Object cannot be modify.", L"Modification", MB_ICONEXCLAMATION);
7fd59977 296 return MainLab;
297 }
298
299 // Get the TFunction_FunctionDriver GUID used with the TFunction_Function
300 Standard_GUID myDriverID=TFF->GetDriverGUID();
301 Handle(TOcafFunction_CylDriver) myCylDriver;
302 // Find the TOcafFunction_CylDriver in the TFunction_DriverTable using its GUID
303 TFunction_DriverTable::Get()->FindDriver(myDriverID, myCylDriver);
304 // Execute the cut if it must be (if an attribute changes)
305 myCylDriver->Init(MainLab);
306 if (myCylDriver->MustExecute(log))
307 {
308 // Set the cylinder touched, it will be usefull to recompute an object which used this cylinder as attribute
309 log.SetTouched(MainLab);
310 if(myCylDriver->Execute(log))
576f8b11 311 MessageBoxW (NULL, L"Recompute failed", L"Modify cylinder", MB_ICONEXCLAMATION);
7fd59977 312 }
313
314 return MainLab;
315}
316
317TDF_Label TOcaf_Commands::Cut(TDF_Label ObjectLab, TDF_Label ToolObjectLab)
318{
319 // A data structure for our cut operation:
320 // the result itself is attached to the Result label (as his name and his function attribute)
321 // its arguments (Original object label; Tool object label) are attached to the child
322 // labels of the Result Label:
323 //
324 // 0:1 Result Label ---> Name ---> Named shape ---> Function
325 // |
326 // 0:1:1 -- Original object label Label
327 // |
328 // 0:1:2 -- object label Label
329 //
330
331 // Create a new label in the data structure for the box
332 TDF_Label L = TDF_TagSource::NewChild(MainLab);
333
334 // Create the data structure : Set a reference attribute on the Original and the Tool objects, set the name attribute
335 TDF_Reference::Set(L.FindChild(1), ObjectLab);
336 TDF_Reference::Set(L.FindChild(2), ToolObjectLab);
337 Handle(TDataStd_Name) ObjectName;
338 ObjectLab.FindAttribute(TDataStd_Name::GetID(),ObjectName);
339 TDataStd_Name::Set(L, ObjectName->Get());
340
341
342 // Instanciate a TFunction_Function attribute connected to the current cut driver
343 // and attach it to the data structure as an attribute of the Result Label
344 Handle(TFunction_Function) myFunction = TFunction_Function::Set(L, TOcafFunction_CutDriver::GetID());
345
346 // Initialize and execute the cut driver (look at the "Execute()" code)
347 TFunction_Logbook log;
348
349 Handle(TOcafFunction_CutDriver) myCutDriver;
350 // Find the TOcafFunction_CutDriver in the TFunction_DriverTable using its GUID
351 if(!TFunction_DriverTable::Get()->FindDriver(TOcafFunction_CutDriver::GetID(), myCutDriver)) return L;
352
353 myCutDriver->Init(L);
576f8b11 354 if (myCutDriver->Execute(log)) MessageBoxW (NULL, L"DFunction_Execute : failed", L"Cut", MB_ICONERROR);
7fd59977 355
356 return L;
357}