1 // OcafDoc.cpp : implementation of the COcafDoc class
9 #include <ImportExport/ImportExport.h>
10 #include "AISDialogs.h"
12 // Dialog boxes classes
13 #include <ResultDialog.h>
14 #include <NewBoxDlg.h>
15 #include <NewCylDlg.h>
17 #include <TDF_Tool.hxx>
19 #include <DebugBrowser.hxx>
22 //#define new DEBUG_NEW // by cascade
24 static char THIS_FILE[] = __FILE__;
27 /////////////////////////////////////////////////////////////////////////////
30 IMPLEMENT_DYNCREATE(COcafDoc, CDocument)
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)
52 /////////////////////////////////////////////////////////////////////////////
53 // COcafDoc construction/destruction
63 BOOL COcafDoc::OnNewDocument()
65 if (!CDocument::OnNewDocument())
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();
71 // Create a new Ocaf document
72 OcafApp->NewDocument("BinOcaf",myOcafDoc);
73 TPrsStd_AISViewer::New(myOcafDoc->Main(),myViewer);
75 Handle(AIS_InteractiveContext) CTX;
76 TPrsStd_AISViewer::Find(myOcafDoc->Main(), CTX);
77 CTX->SetDisplayMode (AIS_Shaded, Standard_True);
80 // Set the maximum number of available "undo" actions
81 myOcafDoc->SetUndoLimit(10);
84 TCollection_AsciiString Message ("\
85 // Creation of a new document \n\
87 Handle(TOcaf_Application) OcafApp= ((COcafApp*)AfxGetApp())->GetApp(); \n\
89 // Creating the new document \n\
90 OcafApp->NewDocument(\"BinOcaf\", myOcafDoc); \n\
92 // Creation of a new TPrsStd_AISViewer connected to the current V3d_Viewer\n\
93 TPrsStd_AISViewer::New(myOcafDoc->Main(),myViewer); \n\
95 // Setting the number of memorized undos \n\
96 myOcafDoc->SetUndoLimit(10); \n\
99 myCResultDialog.SetTitle("New document");
100 CString text(Message.ToCString());
101 myCResultDialog.SetText(text);
108 void COcafDoc::ActivateFrame(CRuntimeClass* pViewClass,int nCmdShow)
110 POSITION position = GetFirstViewPosition();
111 while (position != (POSITION)NULL)
113 CView* pCurrentView = (CView*)GetNextView(position);
114 if(pCurrentView->IsKindOf(pViewClass) )
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);
126 /////////////////////////////////////////////////////////////////////////////
127 // COcafDoc diagnostics
130 void COcafDoc::AssertValid() const
132 CDocument::AssertValid();
135 void COcafDoc::Dump(CDumpContext& dc) const
141 /////////////////////////////////////////////////////////////////////////////
144 void COcafDoc::OnEditRedo()
147 myOcafDoc->CommitCommand();
148 myAISContext->UpdateCurrentViewer();
150 UpdateAllViews(NULL);
152 TCollection_AsciiString Message = TCollection_AsciiString("\
153 // Redo last undoes operation \n\
155 myOcafDoc->Redo(); \n\
157 myOcafDoc->CommitCommand(); \n\
161 myCResultDialog.SetTitle("Redo");
162 CString text(Message.ToCString());
163 myCResultDialog.SetText(text);
166 void COcafDoc::OnEditUndo()
169 myOcafDoc->CommitCommand();
170 myAISContext->UpdateCurrentViewer();
172 UpdateAllViews(NULL);
174 TCollection_AsciiString Message = TCollection_AsciiString("\
175 // Undo last operation \n\
177 myOcafDoc->Undo(); \n\
179 myOcafDoc->CommitCommand(); \n\
183 myCResultDialog.SetTitle("Undo");
184 CString text(Message.ToCString());
185 myCResultDialog.SetText(text);
188 void COcafDoc::OnUpdateEditRedo(CCmdUI* pCmdUI)
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);
195 void COcafDoc::OnUpdateEditUndo(CCmdUI* pCmdUI)
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);
202 void COcafDoc::OnCreatebox()
206 if(Dlg.DoModal()!=IDOK) return;
208 Handle(TDocStd_Document) D = GetOcafDoc();
210 // Open a new command (for undo)
212 TOcaf_Commands TSC(D->Main());
214 // Create a new box using the CNewBoxDlg Dialog parameters as attributes
215 TDF_Label L = TSC.CreateBox (Dlg.m_x, Dlg.m_y, Dlg.m_z, Dlg.m_w, Dlg.m_l, Dlg.m_h,
216 TCollection_ExtendedString ((const wchar_t* )Dlg.m_Name));
218 // Get the TPrsStd_AISPresentation of the new box TNaming_NamedShape
219 Handle(TPrsStd_AISPresentation) prs= TPrsStd_AISPresentation::Set(L, TNaming_NamedShape::GetID());
223 // Attach an integer attribute to L to memorize it's displayed
224 TDataStd_Integer::Set(L, 1);
225 myAISContext->UpdateCurrentViewer();
227 // Close the command (for undo)
231 TCollection_AsciiString Message ("\
232 // Creation of a new box using Ocaf attributes \n\
234 Handle(TDocStd_Document) D = GetOcafDoc(); \n\
236 // Openning a new command (for undo/redo) \n\
239 TOcaf_Commands TSC(D->Main()); \n\
240 // Look at the TOcaf_Commands::CreateBox() function \n\
241 TDF_Label L=TSC.CreateBox(m_x, m_y, m_z, m_w, m_l, m_h, Name); \n\
243 // Set the TPrsStd_AISPresentation of the box \n\
244 Handle(TPrsStd_AISPresentation) prs; \n\
245 prs= TPrsStd_AISPresentation::Set(L, TNaming_NamedShape::GetID()); \n\
247 // Displaying the box \n\
250 // Commint the command (for undo/redo) \n\
251 D->CommitCommand(); \n\
254 myCResultDialog.SetTitle("Create box");
255 CString text(Message.ToCString());
256 myCResultDialog.SetText(text);
259 void COcafDoc::OnCreatecyl()
263 if(Dlg.DoModal()!=IDOK) return;
265 Handle(TDocStd_Document) D = GetOcafDoc();
267 // Open a new command (for undo)
269 TOcaf_Commands TSC(D->Main());
271 // Create a new box using the CNewCylDlg Dialog parameters as attributes
272 TDF_Label L = TSC.CreateCyl (Dlg.m_x, Dlg.m_y, Dlg.m_z, Dlg.m_r, Dlg.m_h,
273 TCollection_ExtendedString ((const wchar_t* )Dlg.m_Name));
275 // Get the TPrsStd_AISPresentation of the new cylinder TNaming_NamedShape
276 Handle(TPrsStd_AISPresentation) prs= TPrsStd_AISPresentation::Set(L, TNaming_NamedShape::GetID());
280 // Attach an integer attribute to L to memorize it's displayed
281 TDataStd_Integer::Set(L, 1);
282 myAISContext->UpdateCurrentViewer();
284 // Close the command (for undo)
288 TCollection_AsciiString Message ("\
289 // Creation of a new cylinder using Ocaf attributes \n\
291 Handle(TDocStd_Document) D = GetOcafDoc(); \n\
293 // Openning a new command (for undo/redo) \n\
296 TOcaf_Commands TSC(D->Main()); \n\
297 // Look at the TOcaf_Commands::CreateCyl() function \n\
298 TDF_Label L=TSC.CreateCyl(m_x, m_y, m_z, m_r, m_h, Name); \n\
300 // Set the TPrsStd_AISPresentation of the cylinder \n\
301 Handle(TPrsStd_AISPresentation) prs; \n\
302 prs= TPrsStd_AISPresentation::Set(L, TNaming_NamedShape::GetID()); \n\
304 // Displaying the cylinder \n\
307 // Commint the command (for undo/redo) \n\
308 D->CommitCommand(); \n\
311 myCResultDialog.SetTitle("Create cylinder");
312 CString text(Message.ToCString());
313 myCResultDialog.SetText(text);
316 void COcafDoc::OnModify()
318 // Get the selected interactive object
319 myAISContext->InitSelected();
320 Handle(AIS_InteractiveObject) curAISObject = myAISContext->SelectedInteractive();
323 // Get the main label of the selected object
324 Handle(TPrsStd_AISPresentation) ObjectPrs =
325 Handle(TPrsStd_AISPresentation)::DownCast(curAISObject->GetOwner());
326 TDF_Label LabObject = ObjectPrs->Label();
328 // Get the TFunction_Function attribute of the selected object
329 Handle(TFunction_Function) TFF;
330 if ( !LabObject.FindAttribute(TFunction_Function::GetID(),TFF) )
332 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Object cannot be modify.", L"Modification", MB_ICONEXCLAMATION);
335 // Get the Standard_GUID of the TFunction_FunctionDriver of the selected object TFunction_Function attribute
336 Standard_GUID myDriverID=TFF->GetDriverGUID();
338 Handle(TDocStd_Document) D = GetOcafDoc();
339 Handle(TFunction_Logbook) log = TFunction_Logbook::Set(D->Main());
341 TCollection_AsciiString Message("\
342 // Modification and recomputation of the selected object \n\
344 Handle(TDocStd_Document) D = GetOcafDoc(); \n\
346 // Getting the TPrsStd_AISPresentation of the selected object \n\
347 Handle(TPrsStd_AISPresentation) ObjectPrs = \n\
348 Handle(TPrsStd_AISPresentation)::DownCast(curAISObject->GetOwner()); \n\
350 // Getting the Label of the selected object using the TPrsStd_AISPresentation \n\
351 TDF_Label LabObject = ObjectPrs->Label(); \n\
353 // Getting the TFunction_FunctionDriver ID attached to this label \n\
354 Handle(TFunction_Function) TFF; \n\
356 TCollection_AsciiString Suite;
358 // Case of a box created with the box function driver
359 if(myDriverID==TOcafFunction_BoxDriver::GetID())
362 Standard_Real x, y, z, w, l, h;
364 // Get the attributes values of the current box
365 Handle(TDataStd_Real) curReal;
366 LabObject.FindChild(1).FindAttribute(TDataStd_Real::GetID(),curReal);
368 LabObject.FindChild(2).FindAttribute(TDataStd_Real::GetID(),curReal);
370 LabObject.FindChild(3).FindAttribute(TDataStd_Real::GetID(),curReal);
372 LabObject.FindChild(4).FindAttribute(TDataStd_Real::GetID(),curReal);
374 LabObject.FindChild(5).FindAttribute(TDataStd_Real::GetID(),curReal);
376 LabObject.FindChild(6).FindAttribute(TDataStd_Real::GetID(),curReal);
378 Handle(TDataStd_Name) stdName;
379 LabObject.FindAttribute(TDataStd_Name::GetID(),stdName);
381 // Initialize the dialog box with the values of the current box
382 Dlg.InitFields(x, y, z, w, l, h, stdName->Get());
384 if(Dlg.DoModal()!=IDOK) return;
386 // Open a new command (for undo)
390 TOcaf_Commands TSC(LabObject);
391 TSC.ModifyBox (Dlg.m_x, Dlg.m_y, Dlg.m_z, Dlg.m_w, Dlg.m_l, Dlg.m_h,
392 TCollection_ExtendedString ((const wchar_t* )Dlg.m_Name), log);
394 // Get the presentation of the box, display it and set it selected
395 Handle(TPrsStd_AISPresentation) prs= TPrsStd_AISPresentation::Set(LabObject, TNaming_NamedShape::GetID());
396 TDataStd_Integer::Set(LabObject, 1);
398 myAISContext->UpdateCurrentViewer();
399 // Close the command (for undo)
404 // In this case the TFunction_FunctionDriver ID is a BoxDriver \n\
405 if(myDriverID==TOcafFunction_BoxDriver::GetID()){ \n\
407 // Getting values of box attributes \n\
408 Handle(TDataStd_Real) curReal; \n\
409 LabObject.FindChild(1).FindAttribute(TDataStd_Real::GetID(),curReal); \n\
410 w=curReal->Get(); \n\
411 LabObject.FindChild(2).FindAttribute(TDataStd_Real::GetID(),curReal); \n\
412 l=curReal->Get(); \n\
413 LabObject.FindChild(3).FindAttribute(TDataStd_Real::GetID(),curReal); \n\
414 h=curReal->Get(); \n\
415 LabObject.FindChild(4).FindAttribute(TDataStd_Real::GetID(),curReal); \n\
416 x=curReal->Get(); \n\
417 LabObject.FindChild(5).FindAttribute(TDataStd_Real::GetID(),curReal); \n\
418 y=curReal->Get(); \n\
419 LabObject.FindChild(6).FindAttribute(TDataStd_Real::GetID(),curReal); \n\
420 z=curReal->Get(); \n\
421 Handle(TDataStd_Name) stdName; \n\
422 LabObject.FindAttribute(TDataStd_Name::GetID(),stdName); \n\
424 // Openning a new command \n\
427 TOcaf_Commands TSC(LabObject); \n\
428 // Look at the TOcaf_Commands::ModifyBox() function \n\
429 TSC.ModifyBox(m_x, m_y, m_z, m_w, m_l, m_h, Name); \n\
431 // Set the TPrsStd_AISPresentation of the box \n\
432 Handle(TPrsStd_AISPresentation) prs; \n\
433 prs= TPrsStd_AISPresentation::Set(LabObject, TNaming_NamedShape::GetID()); \n\
435 // Display the box \n\
438 // Commit the command \n\
439 D->CommitCommand(); \n\
443 myCResultDialog.SetTitle("Modify Box");
445 // Case of a cylinder created with the box function driver
446 else if(myDriverID==TOcafFunction_CylDriver::GetID())
449 Standard_Real x, y, z, r, h;
451 // Get the attributes values of the current box
452 Handle(TDataStd_Real) curReal;
453 LabObject.FindChild(1).FindAttribute(TDataStd_Real::GetID(),curReal);
455 LabObject.FindChild(2).FindAttribute(TDataStd_Real::GetID(),curReal);
457 LabObject.FindChild(3).FindAttribute(TDataStd_Real::GetID(),curReal);
459 LabObject.FindChild(4).FindAttribute(TDataStd_Real::GetID(),curReal);
461 LabObject.FindChild(5).FindAttribute(TDataStd_Real::GetID(),curReal);
463 Handle(TDataStd_Name) stdName;
464 LabObject.FindAttribute(TDataStd_Name::GetID(),stdName);
466 // Initialize the dialog cylinder with the values of the current cylinder
467 Dlg.InitFields(x, y, z, r, h, stdName->Get());
469 if(Dlg.DoModal()!=IDOK) return;
471 // Open a new command (for undo)
474 // Modify the cylinder
475 TOcaf_Commands TSC(LabObject);
476 TSC.ModifyCyl (Dlg.m_x, Dlg.m_y, Dlg.m_z, Dlg.m_r, Dlg.m_h,
477 TCollection_ExtendedString ((const wchar_t* )Dlg.m_Name), log);
479 // Get the presentation of the cylinder, display it and set it selected
480 Handle(TPrsStd_AISPresentation) prs= TPrsStd_AISPresentation::Set(LabObject, TNaming_NamedShape::GetID());
481 TDataStd_Integer::Set(LabObject, 1);
483 myAISContext->UpdateCurrentViewer();
484 // Close the command (for undo)
489 // In this case the TFunction_FunctionDriver ID is a CylDriver \n\
490 if(myDriverID==TOcafFunction_CylDriver::GetID()){ \n\
492 // Getting values of box cylinder \n\
493 Handle(TDataStd_Real) curReal; \n\
494 LabObject.FindChild(1).FindAttribute(TDataStd_Real::GetID(),curReal); \n\
495 r=curReal->Get(); \n\
496 LabObject.FindChild(2).FindAttribute(TDataStd_Real::GetID(),curReal); \n\
497 h=curReal->Get(); \n\
498 LabObject.FindChild(3).FindAttribute(TDataStd_Real::GetID(),curReal); \n\
499 x=curReal->Get(); \n\
500 LabObject.FindChild(4).FindAttribute(TDataStd_Real::GetID(),curReal); \n\
501 y=curReal->Get(); \n\
502 LabObject.FindChild(5).FindAttribute(TDataStd_Real::GetID(),curReal); \n\
503 z=curReal->Get(); \n\
504 Handle(TDataStd_Name) stdName; \n\
505 LabObject.FindAttribute(TDataStd_Name::GetID(),stdName); \n\
507 // Openning a new command \n\
510 TOcaf_Commands TSC(LabObject); \n\
511 // Look at the TOcaf_Commands::ModifyCyl() function \n\
512 TSC.ModifyCyl(m_x, m_y, m_z, m_r, m_h, Name); \n\
514 // Set the TPrsStd_AISPresentation of the cylinder \n\
515 Handle(TPrsStd_AISPresentation) prs; \n\
516 prs= TPrsStd_AISPresentation::Set(LabObject, TNaming_NamedShape::GetID()); \n\
518 // Display the cylinder \n\
521 // Commit the command \n\
522 D->CommitCommand(); \n\
526 myCResultDialog.SetTitle("Modify cylinder");
528 // Case of a cut solid created with the cut function driver
529 else if(myDriverID==TOcafFunction_CutDriver::GetID())
531 // Open a new command (for undo)
534 // Get the reference of the Original object used to make the cut object,
535 // this reference is here attached to the first child of the cut object label
536 Handle(TDF_Reference) OriginalRef;
537 LabObject.FindChild(1).FindAttribute(TDF_Reference::GetID(),OriginalRef);
539 // Get the presentation of the Original object
540 Handle(TPrsStd_AISPresentation) OriginalPrs= TPrsStd_AISPresentation::Set(OriginalRef->Get(), TNaming_NamedShape::GetID());
542 // Get the reference of the Tool object used to make the cut object,
543 // this reference is here attached to the second child of the cut object label
544 Handle(TDF_Reference) ToolRef;
545 LabObject.FindChild(2).FindAttribute(TDF_Reference::GetID(),ToolRef);
546 TDF_Label ToolLab=ToolRef->Get();
549 // Get the presentation of the Tool object
550 Handle(TPrsStd_AISPresentation) ToolPrs= TPrsStd_AISPresentation::Set(ToolLab, TNaming_NamedShape::GetID());
552 // Display the Original object and the Tool object, erase the cut object.
553 TDataStd_Integer::Set(OriginalRef->Get(), 1);
554 OriginalPrs->Display(1);
555 TDataStd_Integer::Set(ToolLab, 1);
557 TDataStd_Integer::Set(LabObject, 0);
559 myAISContext->UpdateCurrentViewer();
561 // In this we decided to modify the tool object which is a cylinder,
562 // so we open the cylinder parameters dialog box
564 Standard_Real x, y, z, h, r;
566 // Get the attributes values of the tool cylinder
567 Handle(TDataStd_Real) curReal;
568 ToolLab.FindChild(1).FindAttribute(TDataStd_Real::GetID(),curReal);
570 ToolLab.FindChild(2).FindAttribute(TDataStd_Real::GetID(),curReal);
572 ToolLab.FindChild(3).FindAttribute(TDataStd_Real::GetID(),curReal);
574 ToolLab.FindChild(4).FindAttribute(TDataStd_Real::GetID(),curReal);
576 ToolLab.FindChild(5).FindAttribute(TDataStd_Real::GetID(),curReal);
578 Handle(TDataStd_Name) stdName;
579 ToolLab.FindAttribute(TDataStd_Name::GetID(),stdName);
581 // Initialize the dialog box with the values of the tool cylinder
582 Dlg.InitFields(x, y, z, r, h, stdName->Get());
584 if(Dlg.DoModal()!=IDOK)
587 myAISContext->UpdateCurrentViewer();
591 // Modify the cylinder
592 TOcaf_Commands ToolTSC(ToolLab);
593 ToolTSC.ModifyCyl (Dlg.m_x, Dlg.m_y, Dlg.m_z, Dlg.m_r, Dlg.m_h,
594 TCollection_ExtendedString ((const wchar_t* )Dlg.m_Name), log);
596 // Redisplay the modified Tool object
597 TDataStd_Integer::Set(ToolLab, 1);
599 myAISContext->UpdateCurrentViewer();
602 // Get the TOcafFunction_CutDriver using its Standard_GUID in the TFunction_DriverTable
603 Handle(TFunction_Driver) myCutDriver;
604 if (TFunction_DriverTable::Get()->FindDriver(myDriverID, myCutDriver))
605 myCutDriver->Init(LabObject);
607 // Recompute the cut object if it must be (look at the MustExecute function code)
608 // if (myCutDriver->MustExecute(log))
610 log->SetTouched(LabObject);
611 if(myCutDriver->Execute(log))
612 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Recompute failed", L"Modify cut", MB_ICONEXCLAMATION);
615 // Erase the Original object and the Tool objectedisplay the modified Tool object
616 TDataStd_Integer::Set(ToolLab, 0);
618 TDataStd_Integer::Set(OriginalRef->Get(), 0);
619 OriginalPrs->Erase(0);
620 ObjectPrs=TPrsStd_AISPresentation::Set(LabObject, TNaming_NamedShape::GetID());
621 TDataStd_Integer::Set(LabObject, 1);
622 ObjectPrs->Display(1);
623 myAISContext->UpdateCurrentViewer();
624 // Close the command (for undo)
629 // In this case the TFunction_FunctionDriver ID is a CutDriver \n\
630 if(myDriverID==TOcafFunction_CutDriver::GetID()){ \n\
632 // Getting values of cut attributes (which are reference to the shapes)\n\
633 Handle(TDF_Reference) OriginalRef; \n\
634 LabObject.FindChild(1).FindAttribute(TDF_Reference::GetID(),OriginalRef); \n\
635 Handle(TDF_Reference) ToolRef; \n\
636 LabObject.FindChild(2).FindAttribute(TDF_Reference::GetID(),ToolRef); \n\
638 // Getting the label of the tool shape (to modify it)\n\
639 TDF_Label ToolLab=ToolRef->Get(); \n\
641 TOcaf_Commands TSC(ToolLab); \n\
642 // Look at the TOcaf_Commands::ModifyBox() function \n\
643 TSC.ModifyBox(m_x, m_y, m_z, m_w, m_l, m_h, Name); \n\
645 // Getting the TOcafFunction_CutDriver used to create the cut\n\
646 Handle(TFunction_Driver) myCutDriver; \n\
647 TFunction_DriverTable::Get()->FindDriver(myDriverID, myCutDriver); \n\
649 // Recompute the cut if it must be (if an attribute was modified)\n\
650 if (myCutDriver->MustExecute(log)) { \n\
651 log.SetTouched(LabObject); \n\
652 myCutDriver->Execute(log) \n\
654 Handle(TPrsStd_AISPresentation) prs; \n\
656 // Setting the TPrsStd_AISPresentation of the cut object\n\
657 prs= TPrsStd_AISPresentation::Set(LabObject, TNaming_NamedShape::GetID()); \n\
659 // Display the TPrsStd_AISPresentation of the cut object\n\
662 // Commit the command\n\
663 D->CommitCommand(); \n\
667 myCResultDialog.SetTitle("Modify Cut");
671 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"No associated function driver", L"Modify", MB_OK);
674 CString text(Message.ToCString());
675 myCResultDialog.SetText(text);
678 void COcafDoc::OnUpdateModify(CCmdUI* pCmdUI)
680 // Disable the "modify" button if there is no selected object or several selected objects
681 myAISContext->InitSelected();
682 if(myAISContext->NbSelected()!=1)
684 pCmdUI->Enable(Standard_False);
688 // Get the root label of the selected object using its TPrsStd_AISPresentation
689 myAISContext->InitSelected();
690 Handle(TPrsStd_AISPresentation) ObjectPrs =
691 Handle(TPrsStd_AISPresentation)::DownCast(myAISContext->SelectedInteractive()->GetOwner());
692 if (!ObjectPrs.IsNull()){
693 TDF_Label LabObject = ObjectPrs->Label();
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) );
701 pCmdUI->Enable(FALSE);
705 void COcafDoc::OnCut()
708 Handle(TDocStd_Document) D = GetOcafDoc();
709 TDF_Label L = D->Main();
711 TOcaf_Commands TSC(L);
713 // Open a new command (for undo)
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);
724 // Wait a second to see the construction
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);
739 Handle(TPrsStd_AISPresentation) prs= TPrsStd_AISPresentation::Set(L, TNaming_NamedShape::GetID());
741 // Erase the two boxes and display the cut object
742 TDataStd_Integer::Set(L1, 0);
744 TDataStd_Integer::Set(L2, 0);
746 TDataStd_Integer::Set(L, 1);
748 myAISContext->UpdateCurrentViewer();
750 // Close the cut operation command (for undo)
754 TCollection_AsciiString Message ("\
755 // Cut operation between a cylinder and a box \n\
757 Handle(TDocStd_Document) D = GetOcafDoc(); \n\
758 TDF_Label L = D->Main(); \n\
760 // Openning a new command\n\
763 TOcaf_Commands TSC(D->Main()); \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\
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\
775 // Cut the cylinder with the box \n\
776 L=TSC.Cut(L1, L2); \n\
777 Handle(TPrsStd_AISPresentation) prs; \n\
779 // Setting the TPrsStd_AISPresentation of the cut object\n\
780 prs= TPrsStd_AISPresentation::Set(L, TNaming_NamedShape::GetID()); \n\
782 // Displaying the TPrsStd_AISPresentation of the cut object\n\
785 // Commit the command\n\
786 D->CommitCommand(); \n\
789 myCResultDialog.SetTitle("Cut operation");
790 CString text(Message.ToCString());
791 myCResultDialog.SetText(text);
795 void COcafDoc::Fit3DViews()
797 POSITION position = GetFirstViewPosition();
798 while (position != (POSITION)NULL)
800 CView* pCurrentView = (CView*)GetNextView(position);
801 if(pCurrentView->IsKindOf( RUNTIME_CLASS( OCC_3dView ) ) )
803 ASSERT_VALID(pCurrentView);
804 OCC_3dView* aOCC_3dView = (OCC_3dView*)pCurrentView;
805 aOCC_3dView->FitAll();
811 void COcafDoc::OnObjectDelete()
813 Handle(TDocStd_Document) D = GetOcafDoc();
817 AIS_SequenceOfInteractive aSequence;
818 for(myAISContext->InitSelected();
819 myAISContext->MoreSelected();
820 myAISContext->NextSelected())
821 aSequence.Append(myAISContext->SelectedInteractive());
823 for(int iter=1;iter <=aSequence.Length();iter++)
825 if (myAISContext->DisplayStatus(aSequence(iter)) == AIS_DS_Displayed)
827 Handle(TPrsStd_AISPresentation) CurrentPrs
828 = Handle(TPrsStd_AISPresentation)::DownCast(aSequence(iter)->GetOwner());
829 TDataStd_Integer::Set(CurrentPrs->Label(), 0);
830 CurrentPrs->Erase(1);
835 myAISContext->UpdateCurrentViewer();
837 TCollection_AsciiString Message ("\
838 // Delete selected object \n\
840 Handle(TDocStd_Document) D = GetOcafDoc(); \n\
842 // Openning a new command\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\
849 // Erasing the TPrsStd_AISPresentation of the selected object\n\
850 CurrentPrs->Erase(1); \n\
852 // Commit the command\n\
853 D->CommitCommand(); \n\
856 myCResultDialog.SetTitle("Delete");
857 CString text(Message.ToCString());
858 myCResultDialog.SetText(text);
861 void COcafDoc::OnUpdateObjectDelete(CCmdUI* pCmdUI)
863 myAISContext->InitSelected();
864 pCmdUI->Enable (myAISContext->MoreSelected());
867 void COcafDoc::DisplayPrs()
869 TDF_Label LabSat = myOcafDoc->Main();
871 for (TDF_ChildIterator it(LabSat); it.More(); it.Next())
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;
878 // To know if the object was displayed
879 if (L.FindAttribute(TDataStd_Integer::GetID(), TDI))
880 if(!TDI->Get()) continue;
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);
889 myAISContext->UpdateCurrentViewer();
892 void COcafDoc::OnCloseDocument()
894 Handle(TOcaf_Application) OcafApp = ((COcafApp*)AfxGetApp())->GetApp();
895 OcafApp->Close(myOcafDoc);
896 CDocument::OnCloseDocument();
899 void COcafDoc::OnFileSaveAs()
901 const wchar_t* SPathName = PathName;
902 TCollection_ExtendedString TPathName (SPathName);
906 if (TPathName.SearchFromEnd(".xml") > 0){
907 Filter = "OCAFSample(XML) (*.xml)|*.xml|OCAFSample(Binary) (*.cbf)|*.cbf||";
909 Filter = "OCAFSample(Binary) (*.cbf)|*.cbf|OCAFSample(XML) (*.xml)|*.xml||";
912 CFileDialog dlg(FALSE,
915 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
920 Handle(TOcaf_Application) m_App= ((COcafApp*)AfxGetApp())->GetApp();
921 if (dlg.DoModal() != IDOK) return;
923 SetTitle(dlg.GetFileTitle());
925 CWaitCursor aWaitCursor;
926 CString CSPath = dlg.GetPathName();
928 std::cout << "Save As " << CSPath << std::endl;
930 const wchar_t* SPath = CSPath;
931 TCollection_ExtendedString TPath (SPath);
933 if (TPath.SearchFromEnd(".xml") > 0)
935 // The document must be saved in XML format
936 myOcafDoc->ChangeStorageFormat("XmlOcaf");
938 else if (TPath.SearchFromEnd(".cbf") > 0)
940 // The document must be saved in binary format
941 myOcafDoc->ChangeStorageFormat("BinOcaf");
946 // Saves the document in the current application
947 m_App->SaveAs(myOcafDoc,TPath);
951 AfxMessageBox (L"Error! The file wasn't saved.");
954 // // save the document in the current application
955 // m_App->SaveAs(myOcafDoc,TPath);
959 TCollection_AsciiString Message = TCollection_AsciiString("\
960 // Storing the document as \n\
962 Handle(TOcaf_Application) m_App= ((COcafApp*)AfxGetApp())->GetApp(); \n\
964 // Saving current OCAF document at TPath \n\
965 m_App->SaveAs(myOcafDoc,(TCollection_ExtendedString) TPath); \n\
968 myCResultDialog.SetTitle("Save a document");
969 CString text(Message.ToCString());
970 myCResultDialog.SetText(text);
973 void COcafDoc::OnFileSave()
975 Handle(TOcaf_Application) m_App= ((COcafApp*)AfxGetApp())->GetApp();
978 if (myOcafDoc.IsNull())
980 AfxMessageBox (L"Error during saving! Empty document.");
986 const wchar_t* SPath = PathName;
987 TCollection_ExtendedString TPath (SPath);
989 if (TPath.SearchFromEnd(".xml") > 0)
991 // The document must be saved in XML format
992 myOcafDoc->ChangeStorageFormat("XmlOcaf");
994 else if (TPath.SearchFromEnd(".cbf") > 0)
996 // The document must be saved in binary format
997 myOcafDoc->ChangeStorageFormat("BinOcaf");
1002 // Saves the document in the current application
1003 m_App->SaveAs(myOcafDoc,TPath);
1007 AfxMessageBox (L"Error! The file wasn't saved.");
1011 TCollection_AsciiString Message = TCollection_AsciiString("\
1012 // Storing the document \n\
1014 Handle(TOcaf_Application) m_App= ((COcafApp*)AfxGetApp())->GetApp(); \n\
1016 // Saving current OCAF document at TPath \n\
1017 m_App->SaveAs(myOcafDoc,(TCollection_ExtendedString) TPath); \n\
1020 myCResultDialog.SetTitle("Save a document");
1021 CString text(Message.ToCString());
1022 myCResultDialog.SetText(text);
1026 CFileDialog dlg(FALSE,
1029 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
1030 L"OCAFSample(Binary) (*.cbf)|*.cbf|OCAFSample(XML) (*.xml)|*.xml||",
1033 if (dlg.DoModal() != IDOK) return;
1035 SetTitle(dlg.GetFileTitle());
1037 CWaitCursor aWaitCursor;
1038 CString CSPath = dlg.GetPathName();
1040 const wchar_t* SPath = CSPath;
1041 TCollection_ExtendedString TPath (SPath);
1043 // Choose storage format
1044 if (TPath.SearchFromEnd(".xml") > 0)
1046 // The document must be saved in XML format
1047 myOcafDoc->ChangeStorageFormat("XmlOcaf");
1049 else if (TPath.SearchFromEnd(".cbf") > 0)
1051 // The document must be saved in binary format
1052 myOcafDoc->ChangeStorageFormat("BinOcaf");
1057 // Saves the document in the current application
1058 m_App->SaveAs(myOcafDoc,TPath);
1062 AfxMessageBox (L"Error! The file wasn't saved.");
1070 TCollection_AsciiString Message = TCollection_AsciiString("\
1071 // Storing the document as \n\
1073 Handle(TOcaf_Application) m_App= ((COcafApp*)AfxGetApp())->GetApp(); \n\
1075 // Saving current OCAF document at TPath \n\
1076 m_App->SaveAs(myOcafDoc,(TCollection_ExtendedString) TPath); \n\
1079 myCResultDialog.SetTitle("Save a document");
1080 CString text(Message.ToCString());
1081 myCResultDialog.SetText(text);
1084 BOOL COcafDoc::OnOpenDocument(LPCTSTR lpszPathName)
1086 if (!CDocument::OnOpenDocument(lpszPathName))
1091 Handle(TOcaf_Application) m_App= ((COcafApp*)AfxGetApp())->GetApp();
1093 CWaitCursor aWaitCursor;
1094 PathName = lpszPathName;
1096 const wchar_t* aPathName = lpszPathName;
1097 TCollection_ExtendedString anOccPathName (aPathName);
1099 // Open the document in the current application
1100 //PCDM_ReaderStatus RS = m_App->Open(TPath,myOcafDoc);
1101 m_App->Open(anOccPathName, myOcafDoc);
1102 //CDF_RetrievableStatus RS = m_App->Open(TPath,myOcafDoc);
1104 // Connect the document CAF (myDoc) with the AISContext (myAISContext)
1105 TPrsStd_AISViewer::New (myOcafDoc->Main(), myViewer);
1106 myOcafDoc->SetUndoLimit (10);
1108 Handle(AIS_InteractiveContext) aContext;
1109 TPrsStd_AISViewer::Find (myOcafDoc->Main(), aContext);
1110 aContext->SetDisplayMode (AIS_Shaded, Standard_True);
1111 myAISContext = aContext;
1113 // Display the presentations (which was not stored in the document)
1116 TCollection_AsciiString Message = TCollection_AsciiString("\
1117 // Retrieve a document \n\
1119 Handle(TOcaf_Application) m_App= ((COcafApp*)AfxGetApp())->GetApp(); \n\
1121 // Openning the OCAF document from the TPath file\n\
1122 m_App->Open((TCollection_ExtendedString) TPath,myOcafDoc); \n\
1124 // Creation of a new TPrsStd_AISViewer connected to the current V3d_Viewer\n\
1125 TPrsStd_AISViewer::New(myOcafDoc->Main(),myViewer); \n\
1127 // Setting the number of memorized undos \n\
1128 myOcafDoc->SetUndoLimit(10); \n\
1132 myCResultDialog.SetTitle ("Open a document");
1133 CString text (Message.ToCString());
1134 myCResultDialog.SetText (text);
1141 void COcafDoc::EraseAll()
1143 myOcafDoc->NewCommand();
1145 TDF_Label LabSat = myOcafDoc->Main();
1147 for (TDF_ChildIterator it(LabSat); it.More(); it.Next())
1149 TDF_Label L = it.Value();
1150 Handle(TNaming_NamedShape) TNS;
1151 if (!L.FindAttribute(TNaming_NamedShape::GetID(), TNS)) continue;
1152 Handle(TDataStd_Integer) TDI;
1154 // To know if the object was displayed
1155 if (L.FindAttribute(TDataStd_Integer::GetID(), TDI))
1156 if(!TDI->Get()) continue;
1158 Handle(TPrsStd_AISPresentation) prs;
1159 if (!L.FindAttribute(TPrsStd_AISPresentation::GetID(),prs))
1160 prs = TPrsStd_AISPresentation::Set(L,TNaming_NamedShape::GetID());
1161 prs->SetColor(Quantity_NOC_ORANGE);
1165 myAISContext->UpdateCurrentViewer();
1167 myOcafDoc->CommitCommand();
1170 void COcafDoc::Popup(const Standard_Integer x,
1171 const Standard_Integer y ,
1172 const Handle(V3d_View)& aView )
1174 Standard_Integer PopupMenuNumber=0;
1175 myAISContext->InitSelected();
1176 if (myAISContext->MoreSelected())
1180 VERIFY(menu.LoadMenu(IDR_Popup3D));
1181 CMenu* pPopup = menu.GetSubMenu(PopupMenuNumber);
1183 ASSERT(pPopup != NULL);
1184 if (PopupMenuNumber == 1) // more than 1 object.
1186 bool OneOrMoreInShading = false;
1187 for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ())
1188 if (myAISContext->IsDisplayed(myAISContext->SelectedInteractive(),1)) OneOrMoreInShading=true;
1189 if(!OneOrMoreInShading)
1190 pPopup->EnableMenuItem(5, MF_BYPOSITION | MF_DISABLED | MF_GRAYED);
1193 POINT winCoord = { x , y };
1194 Handle(WNT_Window) aWNTWindow=
1195 Handle(WNT_Window)::DownCast(aView->Window());
1196 ClientToScreen ( (HWND)(aWNTWindow->HWindow()),&winCoord);
1197 pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON , winCoord.x, winCoord.y ,
1204 //void COcafDoc::OnDfbr()
1206 // // TODO: Add your command handler code here
1207 // DebugBrowser::DFBrowser(myOcafDoc);