Porting samples for OCCT 6.5.2
[occt.git] / samples / mfc / standard / 06_Ocaf / src / OcafDoc.cpp
1 // OcafDoc.cpp : implementation of the COcafDoc class
2 //
3
4 #include "StdAfx.h"
5
6 #include "OcafDoc.h"
7
8 #include "OcafApp.h"
9 #include <ImportExport/ImportExport.h>
10 #include "AISDialogs.h"
11
12 // Dialog boxes classes
13 #include <ResultDialog.h>
14 #include <NewBoxDlg.h>
15 #include <NewCylDlg.h>
16
17 #include <TDF_Tool.hxx>
18
19 #include <DebugBrowser.hxx>
20
21 #ifdef _DEBUG
22 //#define new DEBUG_NEW  // by cascade
23 #undef THIS_FILE
24 static char THIS_FILE[] = __FILE__;
25 #endif
26
27 /////////////////////////////////////////////////////////////////////////////
28 // COcafDoc
29
30 IMPLEMENT_DYNCREATE(COcafDoc, CDocument)
31
32 BEGIN_MESSAGE_MAP(COcafDoc, OCC_3dBaseDoc)
33         //{{AFX_MSG_MAP(COcafDoc)
34         ON_COMMAND(ID_CREATEBOX, OnCreatebox)
35         ON_COMMAND(ID_EDIT_REDO, OnEditRedo)
36         ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
37         ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
38         ON_COMMAND(ID_MODIFY, OnModify)
39         ON_UPDATE_COMMAND_UI(ID_MODIFY, OnUpdateModify)
40         ON_UPDATE_COMMAND_UI(ID_EDIT_REDO, OnUpdateEditRedo)
41         ON_COMMAND(ID_CUT, OnCut)
42         ON_COMMAND(ID_OBJECT_DELETE, OnObjectDelete)
43         ON_UPDATE_COMMAND_UI(ID_OBJECT_DELETE, OnUpdateObjectDelete)
44         ON_COMMAND(ID_FILE_SAVE, OnFileSave)
45         ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
46         ON_COMMAND(ID_CREATECYL, OnCreatecyl)
47 //      ON_COMMAND(ID_DFBR, OnDfbr)
48         //}}AFX_MSG_MAP
49
50 END_MESSAGE_MAP()
51
52 /////////////////////////////////////////////////////////////////////////////
53 // COcafDoc construction/destruction
54
55 COcafDoc::COcafDoc()
56 {
57 }
58
59 COcafDoc::~COcafDoc()
60 {
61 }
62
63  BOOL COcafDoc::OnNewDocument()
64  {
65         if (!CDocument::OnNewDocument())
66                 return FALSE;
67  
68         // Get an Handle on the current TOcaf_Application (which is initialized in the "Ocaf.h" file)
69         Handle(TOcaf_Application) OcafApp = ((COcafApp*)AfxGetApp())->GetApp();
70  
71         // Create a new Ocaf document
72         OcafApp->NewDocument("MDTV-Standard",myOcafDoc);
73         TPrsStd_AISViewer::New(myOcafDoc->Main(),myViewer);
74  
75         Handle(AIS_InteractiveContext) CTX;
76         TPrsStd_AISViewer::Find(myOcafDoc->Main(), CTX);
77         CTX->SetDisplayMode(AIS_Shaded);
78         myAISContext=CTX;
79  
80         // Set the maximum number of available "undo" actions
81         myOcafDoc->SetUndoLimit(10);
82  
83  
84                 TCollection_AsciiString Message ("\
85  //  Creation of a new document \n\
86   \n\
87  Handle(TOcaf_Application) OcafApp= ((COcafApp*)AfxGetApp())->GetApp(); \n\
88   \n\
89  //  Creating the new document \n\
90  OcafApp->NewDocument(\"Ocaf-Sample\", myOcafDoc); \n\
91   \n\
92  //  Creation of a new TPrsStd_AISViewer connected to the current V3d_Viewer\n\
93  TPrsStd_AISViewer::New(myOcafDoc->Main(),myViewer); \n\
94   \n\
95  //  Setting the number of memorized undos \n\
96  myOcafDoc->SetUndoLimit(10); \n\
97  \n");
98  
99         myCResultDialog.SetTitle(CString("New document"));
100         CString text(Message.ToCString());
101         myCResultDialog.SetText(text);
102  
103         PathName="";
104  
105         return TRUE;
106  }
107
108 void COcafDoc::ActivateFrame(CRuntimeClass* pViewClass,int nCmdShow)
109 {
110   POSITION position = GetFirstViewPosition();
111   while (position != (POSITION)NULL)
112   {
113     CView* pCurrentView = (CView*)GetNextView(position);
114      if(pCurrentView->IsKindOf(pViewClass) )
115     {
116         ASSERT_VALID(pCurrentView);
117         CFrameWnd* pParentFrm = pCurrentView->GetParentFrame();
118             ASSERT(pParentFrm != (CFrameWnd *)NULL);
119         // simply make the frame window visible
120             pParentFrm->ActivateFrame(nCmdShow);
121     }
122   }
123
124 }
125
126 /////////////////////////////////////////////////////////////////////////////
127 // COcafDoc diagnostics
128
129 #ifdef _DEBUG
130 void COcafDoc::AssertValid() const
131 {
132         CDocument::AssertValid();
133 }
134
135 void COcafDoc::Dump(CDumpContext& dc) const
136 {
137         CDocument::Dump(dc);
138 }
139 #endif //_DEBUG
140
141 /////////////////////////////////////////////////////////////////////////////
142 // COcafDoc commands
143
144 void COcafDoc::OnEditRedo() 
145 {
146         myOcafDoc->Redo();
147         myOcafDoc->CommitCommand();
148         myAISContext->UpdateCurrentViewer();
149
150         UpdateAllViews(NULL);
151
152         TCollection_AsciiString Message = TCollection_AsciiString("\
153 //  Redo last undoes operation \n\
154  \n\
155 myOcafDoc->Redo(); \n\
156  \n\
157 myOcafDoc->CommitCommand(); \n\
158  \n\
159 \n");
160
161         myCResultDialog.SetTitle(CString("Redo"));
162         CString text(Message.ToCString());
163         myCResultDialog.SetText(text);
164 }
165
166 void COcafDoc::OnEditUndo() 
167 {
168         myOcafDoc->Undo();
169         myOcafDoc->CommitCommand();
170         myAISContext->UpdateCurrentViewer();
171         
172         UpdateAllViews(NULL);
173
174         TCollection_AsciiString Message = TCollection_AsciiString("\
175 //  Undo last operation \n\
176  \n\
177 myOcafDoc->Undo(); \n\
178  \n\
179 myOcafDoc->CommitCommand(); \n\
180  \n\
181 \n");
182
183         myCResultDialog.SetTitle(CString("Undo"));
184         CString text(Message.ToCString());
185         myCResultDialog.SetText(text);
186 }
187
188 void COcafDoc::OnUpdateEditRedo(CCmdUI* pCmdUI) 
189 {
190         // Disable the "redo" button if there is no availlables redo actions
191         if (myOcafDoc->GetAvailableRedos()>0) pCmdUI->Enable(Standard_True);
192         else pCmdUI->Enable(Standard_False);    
193 }
194
195 void COcafDoc::OnUpdateEditUndo(CCmdUI* pCmdUI) 
196 {
197         // Disable the "undo" button if there is no availlables undo actions
198         if (myOcafDoc->GetAvailableUndos()>0) pCmdUI->Enable(Standard_True);
199         else pCmdUI->Enable(Standard_False);
200 }
201
202 void COcafDoc::OnCreatebox() 
203 {
204         EraseAll();
205         CNewBoxDlg Dlg;
206         if(Dlg.DoModal()!=IDOK) return;
207
208         Handle(TDocStd_Document) D = GetOcafDoc();
209
210         // Open a new command (for undo)
211         D->NewCommand();
212         TOcaf_Commands TSC(D->Main());
213
214         TCollection_AsciiString Name((Standard_CString)(LPCTSTR)Dlg.m_Name);
215
216         // Create a new box using the CNewBoxDlg Dialog parameters as attributes
217         TDF_Label L=TSC.CreateBox(Dlg.m_x, Dlg.m_y, Dlg.m_z, Dlg.m_w, Dlg.m_l, Dlg.m_h, TCollection_ExtendedString(Name));
218
219         // Get the TPrsStd_AISPresentation of the new box TNaming_NamedShape
220         Handle(TPrsStd_AISPresentation) prs= TPrsStd_AISPresentation::Set(L, TNaming_NamedShape::GetID()); 
221         // Display it
222         prs->Display(1);
223         Fit3DViews();
224         // Attach an integer attribute to L to memorize it's displayed
225         TDataStd_Integer::Set(L, 1);
226         myAISContext->UpdateCurrentViewer();
227
228         // Close the command (for undo)
229         D->CommitCommand();
230
231
232                 TCollection_AsciiString Message ("\
233 //  Creation of a new box using Ocaf attributes \n\
234  \n\
235 Handle(TDocStd_Document) D = GetOcafDoc(); \n\
236  \n\
237 //  Openning a new command (for undo/redo) \n\
238 D->NewCommand(); \n\
239  \n\
240 TOcaf_Commands TSC(D->Main()); \n\
241 // Look at the TOcaf_Commands::CreateBox() function \n\
242 TDF_Label L=TSC.CreateBox(m_x, m_y, m_z, m_w, m_l, m_h, Name); \n\
243  \n\
244 // Set the TPrsStd_AISPresentation of the box \n\
245 Handle(TPrsStd_AISPresentation) prs; \n\
246 prs= TPrsStd_AISPresentation::Set(L, TNaming_NamedShape::GetID()); \n\
247  \n\
248 //  Displaying the box \n\
249 prs->Display(1); \n\
250  \n\
251 //  Commint the command (for undo/redo) \n\
252 D->CommitCommand(); \n\
253 \n");
254
255         myCResultDialog.SetTitle(CString("Create box"));
256         CString text(Message.ToCString());
257         myCResultDialog.SetText(text);
258 }
259
260 void COcafDoc::OnCreatecyl() 
261 {
262         EraseAll();
263         CNewCylDlg Dlg;
264         if(Dlg.DoModal()!=IDOK) return;
265
266         Handle(TDocStd_Document) D = GetOcafDoc();
267
268         // Open a new command (for undo)
269         D->NewCommand();
270         TOcaf_Commands TSC(D->Main());
271
272         TCollection_AsciiString Name((Standard_CString)(LPCTSTR)Dlg.m_Name);
273
274         // Create a new box using the CNewCylDlg Dialog parameters as attributes
275         TDF_Label L=TSC.CreateCyl(Dlg.m_x, Dlg.m_y, Dlg.m_z, Dlg.m_r, Dlg.m_h, TCollection_ExtendedString(Name));
276
277         // Get the TPrsStd_AISPresentation of the new cylinder TNaming_NamedShape
278         Handle(TPrsStd_AISPresentation) prs= TPrsStd_AISPresentation::Set(L, TNaming_NamedShape::GetID()); 
279         // Display it
280         prs->Display(1);
281         Fit3DViews();
282         // Attach an integer attribute to L to memorize it's displayed
283         TDataStd_Integer::Set(L, 1);
284         myAISContext->UpdateCurrentViewer();
285
286         // Close the command (for undo)
287         D->CommitCommand();
288
289
290                 TCollection_AsciiString Message ("\
291 //  Creation of a new cylinder using Ocaf attributes \n\
292  \n\
293 Handle(TDocStd_Document) D = GetOcafDoc(); \n\
294  \n\
295 //  Openning a new command (for undo/redo) \n\
296 D->NewCommand(); \n\
297  \n\
298 TOcaf_Commands TSC(D->Main()); \n\
299 // Look at the TOcaf_Commands::CreateCyl() function \n\
300 TDF_Label L=TSC.CreateCyl(m_x, m_y, m_z, m_r, m_h, Name); \n\
301  \n\
302 // Set the TPrsStd_AISPresentation of the cylinder \n\
303 Handle(TPrsStd_AISPresentation) prs; \n\
304 prs= TPrsStd_AISPresentation::Set(L, TNaming_NamedShape::GetID()); \n\
305  \n\
306 //  Displaying the cylinder \n\
307 prs->Display(1); \n\
308  \n\
309 //  Commint the command (for undo/redo) \n\
310 D->CommitCommand(); \n\
311 \n");
312
313         myCResultDialog.SetTitle(CString("Create cylinder"));
314         CString text(Message.ToCString());
315         myCResultDialog.SetText(text);
316 }
317
318 void COcafDoc::OnModify() 
319 {
320         // Get the selected interactive object
321         myAISContext->InitCurrent();
322         Handle(AIS_InteractiveObject) curAISObject = myAISContext->Current();
323
324
325         // Get the main label of the selected object
326         Handle(TPrsStd_AISPresentation) ObjectPrs = 
327                 Handle(TPrsStd_AISPresentation)::DownCast(curAISObject->GetOwner());
328         TDF_Label LabObject = ObjectPrs->Label();
329
330         // Get the TFunction_Function attribute of the selected object
331         Handle(TFunction_Function) TFF; 
332         if ( !LabObject.FindAttribute(TFunction_Function::GetID(),TFF) )
333         {
334                 MessageBox(0,"Object cannot be modify.", "Modification", MB_ICONEXCLAMATION);
335                 return;
336         }
337         // Get the Standard_GUID of the TFunction_FunctionDriver of the selected object TFunction_Function attribute
338         Standard_GUID myDriverID=TFF->GetDriverGUID();
339
340         Handle(TDocStd_Document) D = GetOcafDoc();
341         TFunction_Logbook log;
342
343         TCollection_AsciiString Message("\
344 //  Modification and recomputation of the selected object \n\
345  \n\
346 Handle(TDocStd_Document) D = GetOcafDoc(); \n\
347  \n\
348 //  Getting the TPrsStd_AISPresentation of the selected object \n\
349 Handle(TPrsStd_AISPresentation) ObjectPrs =  \n\
350         Handle(TPrsStd_AISPresentation)::DownCast(curAISObject->GetOwner()); \n\
351  \n\
352 //  Getting the Label of the selected object using the TPrsStd_AISPresentation \n\
353 TDF_Label LabObject = ObjectPrs->Label(); \n\
354  \n\
355 //  Getting the TFunction_FunctionDriver ID attached to this label \n\
356 Handle(TFunction_Function) TFF; \n\
357 \n");
358         TCollection_AsciiString Suite;
359
360         // Case of a box created with the box function driver
361         if(myDriverID==TOcafFunction_BoxDriver::GetID())
362         {
363                 CNewBoxDlg Dlg;
364                 Standard_Real x, y, z, w, l, h;
365
366                 // Get the attributes values of the current box
367                 Handle(TDataStd_Real) curReal;
368                 LabObject.FindChild(1).FindAttribute(TDataStd_Real::GetID(),curReal);
369                 w=curReal->Get();
370                 LabObject.FindChild(2).FindAttribute(TDataStd_Real::GetID(),curReal);
371                 l=curReal->Get();
372                 LabObject.FindChild(3).FindAttribute(TDataStd_Real::GetID(),curReal);
373                 h=curReal->Get();
374                 LabObject.FindChild(4).FindAttribute(TDataStd_Real::GetID(),curReal);
375                 x=curReal->Get();
376                 LabObject.FindChild(5).FindAttribute(TDataStd_Real::GetID(),curReal);
377                 y=curReal->Get();
378                 LabObject.FindChild(6).FindAttribute(TDataStd_Real::GetID(),curReal);
379                 z=curReal->Get();
380                 Handle(TDataStd_Name) stdName;
381                 LabObject.FindAttribute(TDataStd_Name::GetID(),stdName);
382
383                 // Initialize the dialog box with the values of the current box
384                 Dlg.InitFields(x, y, z, w, l, h, stdName->Get());
385
386                 if(Dlg.DoModal()!=IDOK) return;
387
388                 // Open a new command (for undo)
389                 D->NewCommand();
390
391                 TCollection_AsciiString Name((Standard_CString)(LPCTSTR)Dlg.m_Name);
392
393                 // Modify the box
394                 TOcaf_Commands TSC(LabObject);
395                 TSC.ModifyBox(Dlg.m_x, Dlg.m_y, Dlg.m_z, Dlg.m_w, Dlg.m_l, Dlg.m_h, TCollection_ExtendedString(Name), log);
396
397                 // Get the presentation of the box, display it and set it selected
398                 Handle(TPrsStd_AISPresentation) prs= TPrsStd_AISPresentation::Set(LabObject, TNaming_NamedShape::GetID()); 
399                 TDataStd_Integer::Set(LabObject, 1);
400                 prs->Display(1);
401                 myAISContext->UpdateCurrentViewer();
402                 // Close the command (for undo)
403                 D->CommitCommand();
404
405                 Message+=("\
406  \n\
407 //  In this case the TFunction_FunctionDriver ID is a BoxDriver \n\
408 if(myDriverID==TOcafFunction_BoxDriver::GetID()){ \n\
409  \n\
410 //  Getting values of box attributes \n\
411 Handle(TDataStd_Real) curReal; \n\
412 LabObject.FindChild(1).FindAttribute(TDataStd_Real::GetID(),curReal); \n\
413 w=curReal->Get(); \n\
414 LabObject.FindChild(2).FindAttribute(TDataStd_Real::GetID(),curReal); \n\
415 l=curReal->Get(); \n\
416 LabObject.FindChild(3).FindAttribute(TDataStd_Real::GetID(),curReal); \n\
417 h=curReal->Get(); \n\
418 LabObject.FindChild(4).FindAttribute(TDataStd_Real::GetID(),curReal); \n\
419 x=curReal->Get(); \n\
420 LabObject.FindChild(5).FindAttribute(TDataStd_Real::GetID(),curReal); \n\
421 y=curReal->Get(); \n\
422 LabObject.FindChild(6).FindAttribute(TDataStd_Real::GetID(),curReal); \n\
423 z=curReal->Get(); \n\
424 Handle(TDataStd_Name) stdName; \n\
425 LabObject.FindAttribute(TDataStd_Name::GetID(),stdName); \n\
426  \n\
427 //  Openning a new command \n\
428 D->NewCommand(); \n\
429  \n\
430 TOcaf_Commands TSC(LabObject); \n\
431 // Look at the TOcaf_Commands::ModifyBox() function \n\
432 TSC.ModifyBox(m_x, m_y, m_z, m_w, m_l, m_h, Name); \n\
433  \n\
434 // Set the TPrsStd_AISPresentation of the box \n\
435 Handle(TPrsStd_AISPresentation) prs; \n\
436 prs= TPrsStd_AISPresentation::Set(LabObject, TNaming_NamedShape::GetID()); \n\
437  \n\
438 // Display the box \n\
439 prs->Display(1); \n\
440  \n\
441 // Commit the command \n\
442 D->CommitCommand(); \n\
443 } \n\
444 \n");
445                 myCResultDialog.SetTitle(CString("Modify Box"));
446         }
447         // Case of a cylinder created with the box function driver
448         else if(myDriverID==TOcafFunction_CylDriver::GetID())
449         {
450                 CNewCylDlg Dlg;
451                 Standard_Real x, y, z, r, h;
452
453                 // Get the attributes values of the current box
454                 Handle(TDataStd_Real) curReal;
455                 LabObject.FindChild(1).FindAttribute(TDataStd_Real::GetID(),curReal);
456                 r=curReal->Get();
457                 LabObject.FindChild(2).FindAttribute(TDataStd_Real::GetID(),curReal);
458                 h=curReal->Get();
459                 LabObject.FindChild(3).FindAttribute(TDataStd_Real::GetID(),curReal);
460                 x=curReal->Get();
461                 LabObject.FindChild(4).FindAttribute(TDataStd_Real::GetID(),curReal);
462                 y=curReal->Get();
463                 LabObject.FindChild(5).FindAttribute(TDataStd_Real::GetID(),curReal);
464                 z=curReal->Get();
465                 Handle(TDataStd_Name) stdName;
466                 LabObject.FindAttribute(TDataStd_Name::GetID(),stdName);
467
468                 // Initialize the dialog cylinder with the values of the current cylinder
469                 Dlg.InitFields(x, y, z, r, h, stdName->Get());
470
471                 if(Dlg.DoModal()!=IDOK) return;
472
473                 // Open a new command (for undo)
474                 D->NewCommand();
475
476                 TCollection_AsciiString Name((Standard_CString)(LPCTSTR)Dlg.m_Name);
477
478                 // Modify the cylinder
479                 TOcaf_Commands TSC(LabObject);
480                 TSC.ModifyCyl(Dlg.m_x, Dlg.m_y, Dlg.m_z, Dlg.m_r, Dlg.m_h, TCollection_ExtendedString(Name), log);
481
482                 // Get the presentation of the cylinder, display it and set it selected
483                 Handle(TPrsStd_AISPresentation) prs= TPrsStd_AISPresentation::Set(LabObject, TNaming_NamedShape::GetID()); 
484                 TDataStd_Integer::Set(LabObject, 1);
485                 prs->Display(1);
486                 myAISContext->UpdateCurrentViewer();
487                 // Close the command (for undo)
488                 D->CommitCommand();
489
490                 Message+=("\
491  \n\
492 //  In this case the TFunction_FunctionDriver ID is a CylDriver \n\
493 if(myDriverID==TOcafFunction_CylDriver::GetID()){ \n\
494  \n\
495 //  Getting values of box cylinder \n\
496 Handle(TDataStd_Real) curReal; \n\
497 LabObject.FindChild(1).FindAttribute(TDataStd_Real::GetID(),curReal); \n\
498 r=curReal->Get(); \n\
499 LabObject.FindChild(2).FindAttribute(TDataStd_Real::GetID(),curReal); \n\
500 h=curReal->Get(); \n\
501 LabObject.FindChild(3).FindAttribute(TDataStd_Real::GetID(),curReal); \n\
502 x=curReal->Get(); \n\
503 LabObject.FindChild(4).FindAttribute(TDataStd_Real::GetID(),curReal); \n\
504 y=curReal->Get(); \n\
505 LabObject.FindChild(5).FindAttribute(TDataStd_Real::GetID(),curReal); \n\
506 z=curReal->Get(); \n\
507 Handle(TDataStd_Name) stdName; \n\
508 LabObject.FindAttribute(TDataStd_Name::GetID(),stdName); \n\
509  \n\
510 //  Openning a new command \n\
511 D->NewCommand(); \n\
512  \n\
513 TOcaf_Commands TSC(LabObject); \n\
514 // Look at the TOcaf_Commands::ModifyCyl() function \n\
515 TSC.ModifyCyl(m_x, m_y, m_z, m_r, m_h, Name); \n\
516  \n\
517 // Set the TPrsStd_AISPresentation of the cylinder \n\
518 Handle(TPrsStd_AISPresentation) prs; \n\
519 prs= TPrsStd_AISPresentation::Set(LabObject, TNaming_NamedShape::GetID()); \n\
520  \n\
521 // Display the cylinder \n\
522 prs->Display(1); \n\
523  \n\
524 // Commit the command \n\
525 D->CommitCommand(); \n\
526 } \n\
527 \n");
528                 myCResultDialog.SetTitle(CString("Modify cylinder"));
529         }
530         // Case of a cut solid created with the cut function driver
531         else if(myDriverID==TOcafFunction_CutDriver::GetID())
532         {
533                 // Open a new command (for undo)
534                 D->NewCommand();
535
536                 // Get the reference of the Original object used to make the cut object, 
537                 // this reference is here attached to the first child of the cut object label 
538                 Handle(TDF_Reference) OriginalRef;
539                 LabObject.FindChild(1).FindAttribute(TDF_Reference::GetID(),OriginalRef);
540
541                 // Get the presentation of the Original object
542                 Handle(TPrsStd_AISPresentation) OriginalPrs= TPrsStd_AISPresentation::Set(OriginalRef->Get(), TNaming_NamedShape::GetID()); 
543
544                 // Get the reference of the Tool object used to make the cut object, 
545                 // this reference is here attached to the second child of the cut object label 
546                 Handle(TDF_Reference) ToolRef;
547                 LabObject.FindChild(2).FindAttribute(TDF_Reference::GetID(),ToolRef);
548                 TDF_Label ToolLab=ToolRef->Get();
549
550                 
551                 // Get the presentation of the Tool object
552                 Handle(TPrsStd_AISPresentation) ToolPrs= TPrsStd_AISPresentation::Set(ToolLab, TNaming_NamedShape::GetID()); 
553
554                 // Display the Original object and the Tool object, erase the cut object.
555                 TDataStd_Integer::Set(OriginalRef->Get(), 1);
556                 OriginalPrs->Display(1);
557                 TDataStd_Integer::Set(ToolLab, 1);
558                 ToolPrs->Display(1);
559                 TDataStd_Integer::Set(LabObject, 0);
560                 ObjectPrs->Erase(1);
561                 myAISContext->UpdateCurrentViewer();
562
563                 // In this  we decided to modify the tool object which is a cylinder, 
564                 // so we open the cylinder parameters dialog box
565                 CNewCylDlg Dlg;
566                 Standard_Real x, y, z, h, r;
567
568                 // Get the attributes values of the tool cylinder
569                 Handle(TDataStd_Real) curReal;
570                 ToolLab.FindChild(1).FindAttribute(TDataStd_Real::GetID(),curReal);
571                 r=curReal->Get();
572                 ToolLab.FindChild(2).FindAttribute(TDataStd_Real::GetID(),curReal);
573                 h=curReal->Get();
574                 ToolLab.FindChild(3).FindAttribute(TDataStd_Real::GetID(),curReal);
575                 x=curReal->Get();
576                 ToolLab.FindChild(4).FindAttribute(TDataStd_Real::GetID(),curReal);
577                 y=curReal->Get();
578                 ToolLab.FindChild(5).FindAttribute(TDataStd_Real::GetID(),curReal);
579                 z=curReal->Get();
580                 Handle(TDataStd_Name) stdName;
581                 ToolLab.FindAttribute(TDataStd_Name::GetID(),stdName);
582
583                 // Initialize the dialog box with the values of the tool cylinder
584                 Dlg.InitFields(x, y, z, r, h, stdName->Get());
585
586                 if(Dlg.DoModal()!=IDOK) 
587                 {
588                         D->AbortCommand();
589                         myAISContext->UpdateCurrentViewer();
590                         return;
591                 }
592                 TCollection_AsciiString Name((Standard_CString)(LPCTSTR)Dlg.m_Name);
593
594                 // Modify the cylinder
595                 TOcaf_Commands ToolTSC(ToolLab);
596                 ToolTSC.ModifyCyl(Dlg.m_x, Dlg.m_y, Dlg.m_z, Dlg.m_r, Dlg.m_h, TCollection_ExtendedString(Name), log);
597
598                 // Redisplay the modified Tool object
599                 TDataStd_Integer::Set(ToolLab, 1);
600                 ToolPrs->Display(1);
601                 myAISContext->UpdateCurrentViewer();
602                 Sleep(1000);
603
604                 // Get the TOcafFunction_CutDriver using its Standard_GUID in the TFunction_DriverTable
605                 Handle(TOcafFunction_CutDriver) myCutDriver;
606                 TFunction_DriverTable::Get()->FindDriver(myDriverID, myCutDriver);
607
608                 // Recompute the cut object if it must be (look at the MustExecute function code)
609 //              if (myCutDriver->MustExecute(log))
610 //              {
611                         log.SetTouched(LabObject);
612                         if(myCutDriver->Execute(log))
613                                 MessageBox(0,"Recompute failed","Modify cut",MB_ICONEXCLAMATION);
614 //              }
615
616                 // Erase the Original object and the Tool objectedisplay the modified Tool object
617                 TDataStd_Integer::Set(ToolLab, 0);
618                 ToolPrs->Erase(0);
619                 TDataStd_Integer::Set(OriginalRef->Get(), 0);
620                 OriginalPrs->Erase(0);
621                 ObjectPrs=TPrsStd_AISPresentation::Set(LabObject, TNaming_NamedShape::GetID()); 
622                 TDataStd_Integer::Set(LabObject, 1);
623                 ObjectPrs->Display(1);
624                 myAISContext->UpdateCurrentViewer();
625                 // Close the command (for undo)
626                 D->CommitCommand();
627
628                 Message+=("\
629  \n\
630 //  In this case the TFunction_FunctionDriver ID is a CutDriver \n\
631 if(myDriverID==TOcafFunction_CutDriver::GetID()){ \n\
632  \n\
633 //  Getting values of cut attributes (which are reference to the shapes)\n\
634 Handle(TDF_Reference) OriginalRef; \n\
635 LabObject.FindChild(1).FindAttribute(TDF_Reference::GetID(),OriginalRef); \n\
636 Handle(TDF_Reference) ToolRef; \n\
637 LabObject.FindChild(2).FindAttribute(TDF_Reference::GetID(),ToolRef); \n\
638  \n\
639 //  Getting the label of the tool shape (to modify it)\n\
640 TDF_Label ToolLab=ToolRef->Get(); \n\
641  \n\
642 TOcaf_Commands TSC(ToolLab); \n\
643 // Look at the TOcaf_Commands::ModifyBox() function \n\
644 TSC.ModifyBox(m_x, m_y, m_z, m_w, m_l, m_h, Name); \n\
645  \n\
646 //  Getting the TOcafFunction_CutDriver used to create the cut\n\
647 Handle(TOcafFunction_CutDriver) myCutDriver; \n\
648 TFunction_DriverTable::Get()->FindDriver(myDriverID, myCutDriver); \n\
649  \n\
650 //  Recompute the cut if it must be (if an attribute was modified)\n\
651 if (myCutDriver->MustExecute(log)) { \n\
652         log.SetTouched(LabObject); \n\
653         myCutDriver->Execute(log) \n\
654 } \n\
655 Handle(TPrsStd_AISPresentation) prs; \n\
656  \n\
657 //  Setting the TPrsStd_AISPresentation of the cut object\n\
658 prs= TPrsStd_AISPresentation::Set(LabObject, TNaming_NamedShape::GetID()); \n\
659  \n\
660 //  Display the TPrsStd_AISPresentation of the cut object\n\
661 prs->Display(1); \n\
662  \n\
663 //  Commit the command\n\
664 D->CommitCommand(); \n\
665 } \n\
666 \n");
667                 myCResultDialog.SetTitle(CString("Modify Cut"));
668         }
669         else
670         {
671                 MessageBox(0,"No associated function driver","Modify",MB_OK);
672         }
673
674         CString text(Message.ToCString());
675         myCResultDialog.SetText(text);
676 }
677
678 void COcafDoc::OnUpdateModify(CCmdUI* pCmdUI) 
679 {
680         // Disable the "modify" button if there is no selected object or several selected objects 
681         myAISContext->InitCurrent();
682         if(myAISContext->NbCurrents()!=1)
683         {
684                 pCmdUI->Enable(Standard_False);
685                 return;
686         }
687
688         // Get the root label of the selected object using its TPrsStd_AISPresentation
689         myAISContext->InitCurrent();
690         Handle(TPrsStd_AISPresentation) ObjectPrs = 
691                 Handle(TPrsStd_AISPresentation)::DownCast(myAISContext->Current()->GetOwner());
692         if (!ObjectPrs.IsNull()){
693                 TDF_Label LabObject = ObjectPrs->Label();
694
695
696                 // Disable the "modify" button if selected object don't have a TFunction_Function attribute
697                 Handle(TFunction_Function) TFF; 
698                 pCmdUI->Enable(LabObject.FindAttribute(TFunction_Function::GetID(),TFF) );
699         }
700         else
701                 pCmdUI->Enable(FALSE);
702 }
703
704
705 void COcafDoc::OnCut() 
706 {
707         EraseAll();
708         Handle(TDocStd_Document) D = GetOcafDoc();
709         TDF_Label L = D->Main();
710
711         TOcaf_Commands TSC(L);
712
713         // Open a new command (for undo)
714         D->NewCommand();
715
716         // Create a new cyl (10*20 dimensions)
717         TDF_Label L2=TSC.CreateCyl(0, 10, -10, 5, 80, "Cylinder");
718         Handle(TPrsStd_AISPresentation) prs1= TPrsStd_AISPresentation::Set(L2, TNaming_NamedShape::GetID()); 
719         prs1->SetColor(Quantity_NOC_MATRABLUE);
720         TDataStd_Integer::Set(L2, 1);
721         prs1->Display(1);
722         Fit3DViews();
723
724         // Wait a second to see the construction
725         Sleep(500);
726
727         // Create a new box (20*20*20 dimensions, at the (-12,0,10) position)
728         TDF_Label L1=TSC.CreateBox(-12, 0, 10, 20, 30, 40, "Box");
729         Handle(TPrsStd_AISPresentation) prs2= TPrsStd_AISPresentation::Set(L1, TNaming_NamedShape::GetID()); 
730         prs2->SetColor(Quantity_NOC_YELLOW);
731         TDataStd_Integer::Set(L1, 1);
732         prs2->Display(1);
733         Fit3DViews();
734
735         Sleep(1000);
736
737         // Cut the boxes
738         L=TSC.Cut(L1, L2);
739         Handle(TPrsStd_AISPresentation) prs= TPrsStd_AISPresentation::Set(L, TNaming_NamedShape::GetID());
740
741         // Erase the two boxes and display the cut object
742         TDataStd_Integer::Set(L1, 0);
743         prs1->Erase(0);
744         TDataStd_Integer::Set(L2, 0);
745         prs2->Erase(0);
746         TDataStd_Integer::Set(L, 1);
747         prs->Display(1);
748         myAISContext->UpdateCurrentViewer();
749
750         // Close the cut operation command (for undo)
751         D->CommitCommand();
752
753
754                 TCollection_AsciiString Message ("\
755 //  Cut operation between a cylinder and a box \n\
756  \n\
757 Handle(TDocStd_Document) D = GetOcafDoc(); \n\
758 TDF_Label L = D->Main(); \n\
759  \n\
760 //  Openning a new command\n\
761 D->NewCommand(); \n\
762  \n\
763 TOcaf_Commands TSC(D->Main()); \n\
764  \n\
765 //  Create a box \n\
766 // Look at the TOcaf_Commands::CreateBox() function \n\
767 TDF_Label L1=TSC.CreateBox(-12, 0, 10, 24, 20, 20, '\"'Box'\"'); \n\
768 // Look at the TOcaf_Commands::Cut() function \n\
769  \n\
770 //  Create a cylinder \n\
771 // Look at the TOcaf_Commands::CreateCyl() function \n\
772 //  Create two boxes \n\
773 TDF_Label L2=TSC.CreateCyl(0, 0, 0, 10, 20, '\"'Cylinder'\"'); \n\
774  \n\
775 //  Cut the cylinder with the box \n\
776 L=TSC.Cut(L1, L2); \n\
777 Handle(TPrsStd_AISPresentation) prs; \n\
778  \n\
779 //  Setting the TPrsStd_AISPresentation of the cut object\n\
780 prs= TPrsStd_AISPresentation::Set(L, TNaming_NamedShape::GetID()); \n\
781  \n\
782 //  Displaying the TPrsStd_AISPresentation of the cut object\n\
783 prs->Display(1); \n\
784  \n\
785 //  Commit the command\n\
786 D->CommitCommand(); \n\
787 \n");
788
789         myCResultDialog.SetTitle(CString("Cut operation"));
790         CString text(Message.ToCString());
791         myCResultDialog.SetText(text);
792 }
793
794
795 void COcafDoc::Fit3DViews()
796 {
797   POSITION position = GetFirstViewPosition();
798   while (position != (POSITION)NULL)
799   {
800     CView* pCurrentView = (CView*)GetNextView(position);
801      if(pCurrentView->IsKindOf( RUNTIME_CLASS( OCC_3dView ) ) )
802     {
803         ASSERT_VALID(pCurrentView);
804         OCC_3dView* aOCC_3dView = (OCC_3dView*)pCurrentView;
805         aOCC_3dView->FitAll();
806     }
807   }
808
809 }
810
811 void COcafDoc::OnObjectDelete() 
812 {
813         Handle(TDocStd_Document) D = GetOcafDoc();
814
815         D->NewCommand();
816
817         AIS_SequenceOfInteractive aSequence;
818         for(myAISContext->InitCurrent();
819       myAISContext->MoreCurrent();
820       myAISContext->NextCurrent())
821         aSequence.Append(myAISContext->Current());
822         
823         for(int iter=1;iter <=aSequence.Length();iter++)
824         {
825                 if (myAISContext->DisplayStatus(aSequence(iter)) == AIS_DS_Displayed)
826                 {
827                         Handle(TPrsStd_AISPresentation) CurrentPrs
828                                 = Handle(TPrsStd_AISPresentation)::DownCast(aSequence(iter)->GetOwner()); 
829                         TDataStd_Integer::Set(CurrentPrs->Label(), 0);
830                         CurrentPrs->Erase(1);
831                 }
832         }
833  
834         D->CommitCommand();
835         myAISContext->UpdateCurrentViewer();
836  
837                 TCollection_AsciiString Message ("\
838  // Delete selected object \n\
839   \n\
840  Handle(TDocStd_Document) D = GetOcafDoc(); \n\
841   \n\
842  //  Openning a new command\n\
843  D->NewCommand(); \n\
844   \n\
845  // Getting the  TPrsStd_AISPresentation of the selected object\n\
846  Handle(TPrsStd_AISPresentation) CurrentPrs \n\
847      =Handle(TPrsStd_AISPresentation)::DownCast(myAISContext->Current()->GetOwner());  \n\
848   \n\
849  // Erasing the  TPrsStd_AISPresentation of the selected object\n\
850  CurrentPrs->Erase(1); \n\
851   \n\
852  //  Commit the command\n\
853  D->CommitCommand(); \n\
854  \n");
855  
856         myCResultDialog.SetTitle(CString("Delete"));
857         CString text(Message.ToCString());
858         myCResultDialog.SetText(text);
859  }
860  
861  void COcafDoc::OnUpdateObjectDelete(CCmdUI* pCmdUI) 
862  {
863      myAISContext->InitCurrent();
864         pCmdUI->Enable (myAISContext->MoreCurrent());           
865  }
866  
867  void COcafDoc::DisplayPrs()
868  {
869         TDF_Label LabSat = myOcafDoc->Main();
870  
871         for (TDF_ChildIterator it(LabSat); it.More(); it.Next())
872         {
873                 TDF_Label L = it.Value();
874                 Handle(TNaming_NamedShape) TNS;
875                 if (!L.FindAttribute(TNaming_NamedShape::GetID(), TNS)) continue;
876                 Handle(TDataStd_Integer) TDI;
877
878                 // To know if the object was displayed
879                 if (L.FindAttribute(TDataStd_Integer::GetID(), TDI))
880                         if(!TDI->Get())  continue;
881
882                 Handle(TPrsStd_AISPresentation) prs;
883                 if (!L.FindAttribute(TPrsStd_AISPresentation::GetID(),prs))
884                         prs = TPrsStd_AISPresentation::Set(L,TNaming_NamedShape::GetID());
885                 prs->SetColor(Quantity_NOC_ORANGE);
886                 prs->Display(1);
887     }
888
889         myAISContext->UpdateCurrentViewer();
890 }
891
892 void COcafDoc::OnCloseDocument() 
893 {
894         Handle(TOcaf_Application) OcafApp = ((COcafApp*)AfxGetApp())->GetApp();
895         OcafApp->Close(myOcafDoc);
896         CDocument::OnCloseDocument();
897 }
898
899 void COcafDoc::OnFileSaveAs() 
900 {
901         Standard_CString SPathName = (Standard_CString) (LPCTSTR) PathName;
902         TCollection_ExtendedString TPathName(SPathName);
903
904         CString Filter;
905
906         if (TPathName.SearchFromEnd(".xml") > 0){
907                 Filter = "OCAFSample(XML) (*.xml)|*.xml|OCAFSample(STA) (*.sta)|*.sta|OCAFSample(Binary) (*.cbf)|*.cbf||";
908         }
909         else if (TPathName.SearchFromEnd(".cbf") > 0){
910                 Filter = "OCAFSample(Binary) (*.cbf)|*.cbf|OCAFSample(STA) (*.sta)|*.sta|OCAFSample(XML) (*.xml)|*.xml||";
911         }
912         else{
913                 Filter = "OCAFSample(STA) (*.sta)|*.sta|OCAFSample(XML) (*.xml)|*.xml|OCAFSample(Binary) (*.cbf)|*.cbf||";
914         }
915         
916         CFileDialog dlg(FALSE,
917                         "sta",
918                         GetTitle(),
919                         OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
920                         Filter,
921 //                      "sta Files (*.sta)|*.sta; |All Files (*.*)|*.*||", 
922                         NULL );
923         
924
925         Handle(TOcaf_Application) m_App= ((COcafApp*)AfxGetApp())->GetApp();
926         if (dlg.DoModal() != IDOK) return;
927
928         SetTitle(dlg.GetFileTitle());
929
930         CWaitCursor aWaitCursor;
931         CString CSPath = dlg.GetPathName();
932
933 //      if((CSPath.Find(CString(".sta")))==-1 )
934 //              CSPath = CSPath + ".sta";
935
936         cout << "Save As " << CSPath << endl;
937         PathName=CSPath;
938         Standard_CString SPath = (Standard_CString) (LPCTSTR) CSPath;
939         TCollection_ExtendedString TPath(SPath);
940
941             if (TPath.SearchFromEnd(".xml") > 0)
942                 {
943                   // The document must be saved in XML format
944                         myOcafDoc->ChangeStorageFormat("XmlOcaf");
945                 }
946                 else if (TPath.SearchFromEnd(".cbf") > 0)
947                 {
948                         // The document must be saved in binary format
949                         myOcafDoc->ChangeStorageFormat("BinOcaf");
950                 }
951                 else
952                 {
953                         // The document must be saved in standard format
954                         myOcafDoc->ChangeStorageFormat("MDTV-Standard");
955                 }
956
957                 try
958                 {
959                         // Saves the document in the current application
960                         m_App->SaveAs(myOcafDoc,TPath);
961                 }
962                 catch(...)
963                 {
964                         AfxMessageBox("Error! The file wasn't saved.");
965                 return;
966                 }
967 //      // save the document in the current application
968 //      m_App->SaveAs(myOcafDoc,TPath);
969
970         SetPathName(SPath);
971
972         TCollection_AsciiString Message = TCollection_AsciiString("\
973 //  Storing the document as \n\
974  \n\
975 Handle(TOcaf_Application) m_App= ((COcafApp*)AfxGetApp())->GetApp(); \n\
976  \n\
977 //  Saving current OCAF document at TPath \n\
978 m_App->SaveAs(myOcafDoc,(TCollection_ExtendedString) TPath); \n\
979 \n");
980
981         myCResultDialog.SetTitle(CString("Save a document"));
982         CString text(Message.ToCString());
983         myCResultDialog.SetText(text);
984 }
985
986 void COcafDoc::OnFileSave() 
987 {
988         Handle(TOcaf_Application) m_App= ((COcafApp*)AfxGetApp())->GetApp();
989
990         
991         if (myOcafDoc.IsNull())
992         {
993           AfxMessageBox("Error during saving! Empty document.");
994           return;
995         }
996
997         if(PathName!="")
998         {
999                 Standard_CString SPath = (Standard_CString) (LPCTSTR) PathName;
1000                 TCollection_ExtendedString TPath(SPath);
1001
1002             if (TPath.SearchFromEnd(".xml") > 0)
1003                 {
1004                   // The document must be saved in XML format
1005                         myOcafDoc->ChangeStorageFormat("XmlOcaf");
1006                 }
1007                 else if (TPath.SearchFromEnd(".cbf") > 0)
1008                 {
1009                         // The document must be saved in binary format
1010                         myOcafDoc->ChangeStorageFormat("BinOcaf");
1011                 }
1012                 else
1013                 {
1014                         // The document must be saved in standard format
1015                         myOcafDoc->ChangeStorageFormat("MDTV-Standard");
1016                 }
1017
1018                 try
1019                 {
1020                         // Saves the document in the current application
1021                         m_App->SaveAs(myOcafDoc,TPath);
1022                 }
1023                 catch(...)
1024                 {
1025                         AfxMessageBox("Error! The file wasn't saved.");
1026                     return;
1027                 }
1028
1029         TCollection_AsciiString Message = TCollection_AsciiString("\
1030 //  Storing the document \n\
1031  \n\
1032 Handle(TOcaf_Application) m_App= ((COcafApp*)AfxGetApp())->GetApp(); \n\
1033  \n\
1034 //  Saving current OCAF document at TPath \n\
1035 m_App->SaveAs(myOcafDoc,(TCollection_ExtendedString) TPath); \n\
1036 \n");
1037
1038                 myCResultDialog.SetTitle(CString("Save a document"));
1039                 CString text(Message.ToCString());
1040                 myCResultDialog.SetText(text);
1041                 return;
1042         }
1043
1044         CFileDialog dlg(FALSE,
1045                         "sta",
1046                         GetTitle(),
1047                         OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
1048                         "OCAFSample(STA) (*.sta)|*.sta|OCAFSample(XML) (*.xml)|*.xml|OCAFSample(Binary) (*.cbf)|*.cbf||",
1049 //                      "sta Files (*.sta)|*.sta; |All Files (*.*)|*.*||", 
1050                         NULL );
1051
1052         if (dlg.DoModal() != IDOK) return;
1053
1054         SetTitle(dlg.GetFileTitle());
1055
1056         CWaitCursor aWaitCursor;
1057         CString CSPath = dlg.GetPathName();
1058
1059         Standard_CString SPath = (Standard_CString)(LPCTSTR) CSPath;
1060     TCollection_ExtendedString TPath(SPath);
1061
1062     // Choose storage format
1063     if (TPath.SearchFromEnd(".xml") > 0)
1064         {
1065           // The document must be saved in XML format
1066           myOcafDoc->ChangeStorageFormat("XmlOcaf");
1067         }
1068         else if (TPath.SearchFromEnd(".cbf") > 0)
1069         {
1070           // The document must be saved in binary format
1071           myOcafDoc->ChangeStorageFormat("BinOcaf");
1072         }
1073         else
1074         {
1075                 // The document must be saved in standard format
1076                 myOcafDoc->ChangeStorageFormat("Ocaf-Sample");
1077         }
1078
1079    try
1080    {
1081           // Saves the document in the current application
1082           m_App->SaveAs(myOcafDoc,TPath);
1083    }
1084    catch(...)
1085    {
1086           AfxMessageBox("Error! The file wasn't saved.");
1087           return;
1088    }
1089
1090
1091 //      if((CSPath.Find(CString(".sta")))==-1 )
1092 //              CSPath = CSPath + ".sta";
1093
1094 //      cout << "Save As " << CSPath << endl;
1095 //      PathName=CSPath;
1096 //      Standard_CString SPath = (Standard_CString) (LPCTSTR) CSPath;
1097 //      TCollection_ExtendedString TPath(SPath);
1098
1099         // save the document in the current application
1100 //      m_App->SaveAs(myOcafDoc,TPath);
1101
1102         SetPathName(SPath);
1103         PathName=CSPath;
1104
1105
1106         TCollection_AsciiString Message = TCollection_AsciiString("\
1107 //  Storing the document as \n\
1108  \n\
1109 Handle(TOcaf_Application) m_App= ((COcafApp*)AfxGetApp())->GetApp(); \n\
1110  \n\
1111 //  Saving current OCAF document at TPath \n\
1112 m_App->SaveAs(myOcafDoc,(TCollection_ExtendedString) TPath); \n\
1113 \n");
1114
1115         myCResultDialog.SetTitle(CString("Save a document"));
1116         CString text(Message.ToCString());
1117         myCResultDialog.SetText(text);
1118 }
1119
1120 BOOL COcafDoc::OnOpenDocument(LPCTSTR lpszPathName) 
1121 {
1122         if (!CDocument::OnOpenDocument(lpszPathName))
1123                 return FALSE;
1124         
1125         Handle(TOcaf_Application) m_App= ((COcafApp*)AfxGetApp())->GetApp();
1126
1127         CWaitCursor aWaitCursor;
1128         PathName=lpszPathName;
1129
1130         Standard_CString SPath = (Standard_CString) lpszPathName;
1131         TCollection_ExtendedString TPath(SPath);
1132         PathName=lpszPathName;
1133
1134         // open the document in the current application
1135         PCDM_ReaderStatus RS = m_App->Open(TPath,myOcafDoc);
1136 //      CDF_RetrievableStatus RS = m_App->Open(TPath,myOcafDoc);
1137
1138         //connect the document CAF (myDoc) with the AISContext (myAISContext)
1139 //      TPrsStd_AISViewer::Has(myOcafDoc->Main());
1140         TPrsStd_AISViewer::New(myOcafDoc->Main(),myViewer);
1141         myOcafDoc->SetUndoLimit(10);
1142
1143         Handle(AIS_InteractiveContext) CTX;
1144         TPrsStd_AISViewer::Find(myOcafDoc->Main(),CTX);
1145         CTX->SetDisplayMode(AIS_Shaded);
1146
1147         myAISContext = CTX;
1148
1149         // Display the presentations (which was not stored in the document)
1150         DisplayPrs();
1151         
1152         TCollection_AsciiString Message = TCollection_AsciiString("\
1153 //  Retrieve a document \n\
1154  \n\
1155  Handle(TOcaf_Application) m_App= ((COcafApp*)AfxGetApp())->GetApp(); \n\
1156  \n\
1157 //  Openning the OCAF document from the TPath file\n\
1158 m_App->Open((TCollection_ExtendedString) TPath,myOcafDoc); \n\
1159  \n\
1160 //  Creation of a new TPrsStd_AISViewer connected to the current V3d_Viewer\n\
1161 TPrsStd_AISViewer::New(myOcafDoc->Main(),myViewer); \n\
1162  \n\
1163 //  Setting the number of memorized undos \n\
1164 myOcafDoc->SetUndoLimit(10); \n\
1165  \n\
1166 \n");
1167         myCResultDialog.SetTitle(CString("Open a document"));
1168         CString text(Message.ToCString());
1169         myCResultDialog.SetText(text);
1170
1171         return TRUE;
1172
1173 }
1174
1175
1176 void COcafDoc::EraseAll()
1177 {
1178         myOcafDoc->NewCommand();
1179
1180         TDF_Label LabSat = myOcafDoc->Main();
1181
1182         for (TDF_ChildIterator it(LabSat); it.More(); it.Next())
1183         {
1184                 TDF_Label L = it.Value();
1185                 Handle(TNaming_NamedShape) TNS;
1186                 if (!L.FindAttribute(TNaming_NamedShape::GetID(), TNS)) continue;
1187                 Handle(TDataStd_Integer) TDI;
1188
1189                 // To know if the object was displayed
1190                 if (L.FindAttribute(TDataStd_Integer::GetID(), TDI))
1191                         if(!TDI->Get())  continue;
1192
1193                 Handle(TPrsStd_AISPresentation) prs;
1194                 if (!L.FindAttribute(TPrsStd_AISPresentation::GetID(),prs))
1195                         prs = TPrsStd_AISPresentation::Set(L,TNaming_NamedShape::GetID());
1196                 prs->SetColor(Quantity_NOC_ORANGE);
1197                 prs->Erase(1);
1198     }
1199
1200         myAISContext->UpdateCurrentViewer();
1201
1202         myOcafDoc->CommitCommand();
1203 }
1204
1205 void  COcafDoc::Popup(const Standard_Integer  x,
1206                                                            const Standard_Integer  y ,
1207                                const Handle(V3d_View)& aView   ) 
1208 {
1209   Standard_Integer PopupMenuNumber=0;
1210  myAISContext->InitCurrent();
1211   if (myAISContext->MoreCurrent())
1212     PopupMenuNumber=1;
1213
1214   CMenu menu;
1215   VERIFY(menu.LoadMenu(IDR_Popup3D));
1216   CMenu* pPopup = menu.GetSubMenu(PopupMenuNumber);
1217
1218   ASSERT(pPopup != NULL);
1219    if (PopupMenuNumber == 1) // more than 1 object.
1220   {
1221     bool OneOrMoreInShading = false;
1222         for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
1223     if (myAISContext->IsDisplayed(myAISContext->Current(),1)) OneOrMoreInShading=true;
1224         if(!OneOrMoreInShading)
1225         pPopup->EnableMenuItem(5, MF_BYPOSITION | MF_DISABLED | MF_GRAYED);
1226    }
1227
1228   POINT winCoord = { x , y };
1229   Handle(WNT_Window) aWNTWindow=
1230   Handle(WNT_Window)::DownCast(aView->Window());
1231   ClientToScreen ( (HWND)(aWNTWindow->HWindow()),&winCoord);
1232   pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON , winCoord.x, winCoord.y , 
1233                          AfxGetMainWnd());
1234
1235
1236 }
1237
1238
1239 //void COcafDoc::OnDfbr() 
1240 //{
1241 //      // TODO: Add your command handler code here
1242 //      DebugBrowser::DFBrowser(myOcafDoc);
1243 //      
1244 //}