1 // File: TOcaf_Commands.cxx
2 // Created: Tue Feb 8 17:56:02 2000
3 // Author: Modelistation
4 // <model@brunox.paris1.matra-dtv.fr>
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>
16 #include <gp_Sphere.hxx>
17 #include <gp_Trsf.hxx>
18 #include <TopoDS_Face.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>
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"
36 //=======================================================================
37 //function : TOcaf_Commands
39 //=======================================================================
41 TOcaf_Commands::TOcaf_Commands(const TDF_Label& Main)
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 //=======================================================================
52 TDF_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)
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:
59 // 0:1 Box Label ---> Name ---> Named shape ---> Function
61 // 0:1:1 -- Width Label
63 // 0:1:2 -- Length Label
65 // 0:1:3 -- Height Label
73 // Create a new label in the data structure for the box
74 TDF_Label L = TDF_TagSource::NewChild(MainLab);
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);
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());
90 // Initialize and execute the box driver (look at the "Execute()" code)
91 TFunction_Logbook log;
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;
98 if (myBoxDriver->Execute(log)) MessageBoxW (NULL, L"DFunction_Execute : failed", L"Box", MB_ICONERROR);
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 //=======================================================================
110 TDF_Label TOcaf_Commands::CreateCyl(Standard_Real x, Standard_Real y, Standard_Real z, Standard_Real r, Standard_Real h, const TCollection_ExtendedString& Name)
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:
117 // 0:1 Cylinder Label ---> Name ---> Named shape ---> Function
119 // 0:1:1 -- Radius Label
121 // 0:1:2 -- Height Label
129 // Create a new label in the data structure for the cylinder
130 TDF_Label L = TDF_TagSource::NewChild(MainLab);
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);
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());
145 // Initialize and execute the cylinder driver (look at the "Execute()" code)
146 TFunction_Logbook log;
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;
152 myCylDriver->Init(L);
153 if (myCylDriver->Execute(log)) MessageBoxW (NULL, L"DFunction_Execute : failed", L"Cylinder", MB_ICONERROR);
159 TDF_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)
161 // Set the name attribute (if it's not null)
162 Handle(TDataStd_Name) myStdName;
164 TDataStd_Name::Set(MainLab, Name);
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())
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());
176 MainLab.FindChild(2).FindAttribute(TDataStd_Real::GetID(),curReal);
177 if(l!=curReal->Get())
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());
184 MainLab.FindChild(3).FindAttribute(TDataStd_Real::GetID(),curReal);
185 if(h!=curReal->Get())
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());
192 MainLab.FindChild(4).FindAttribute(TDataStd_Real::GetID(),curReal);
193 if(x!=curReal->Get())
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());
200 MainLab.FindChild(5).FindAttribute(TDataStd_Real::GetID(),curReal);
201 if(y!=curReal->Get())
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());
208 MainLab.FindChild(6).FindAttribute(TDataStd_Real::GetID(),curReal);
209 if(z!=curReal->Get())
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());
216 // Get the TFunction_Function used to create the box
217 Handle(TFunction_Function) TFF;
218 if ( !MainLab.FindAttribute(TFunction_Function::GetID(),TFF) )
220 MessageBox (NULL, L"Object cannot be modify.", L"Modification", MB_ICONEXCLAMATION);
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))
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))
236 MessageBox (NULL, L"Recompute failed", L"Modify box", MB_ICONEXCLAMATION);
242 TDF_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)
244 // Set the name attribute (if it's not null)
245 Handle(TDataStd_Name) myStdName;
247 TDataStd_Name::Set(MainLab, Name);
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())
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());
259 MainLab.FindChild(2).FindAttribute(TDataStd_Real::GetID(),curReal);
260 if(h!=curReal->Get())
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());
267 MainLab.FindChild(3).FindAttribute(TDataStd_Real::GetID(),curReal);
268 if(x!=curReal->Get())
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());
275 MainLab.FindChild(4).FindAttribute(TDataStd_Real::GetID(),curReal);
276 if(y!=curReal->Get())
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());
283 MainLab.FindChild(5).FindAttribute(TDataStd_Real::GetID(),curReal);
284 if(z!=curReal->Get())
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());
291 // Get the TFunction_Function used to create the cylinder
292 Handle(TFunction_Function) TFF;
293 if ( !MainLab.FindAttribute(TFunction_Function::GetID(),TFF) )
295 MessageBoxW (NULL, L"Object cannot be modify.", L"Modification", MB_ICONEXCLAMATION);
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))
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))
311 MessageBoxW (NULL, L"Recompute failed", L"Modify cylinder", MB_ICONEXCLAMATION);
317 TDF_Label TOcaf_Commands::Cut(TDF_Label ObjectLab, TDF_Label ToolObjectLab)
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:
324 // 0:1 Result Label ---> Name ---> Named shape ---> Function
326 // 0:1:1 -- Original object label Label
328 // 0:1:2 -- object label Label
331 // Create a new label in the data structure for the box
332 TDF_Label L = TDF_TagSource::NewChild(MainLab);
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());
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());
346 // Initialize and execute the cut driver (look at the "Execute()" code)
347 TFunction_Logbook log;
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;
353 myCutDriver->Init(L);
354 if (myCutDriver->Execute(log)) MessageBoxW (NULL, L"DFunction_Execute : failed", L"Cut", MB_ICONERROR);