0023551: Move data models contained in samples subfolder of OCCT repository to common...
[occt.git] / samples / mfc / standard / 05_ImportExport / src / ImportExportApp.cpp
1 // ImportExportApp.cpp : Defines the class behaviors for the application.
2 //
3
4 #include "stdafx.h"
5
6 #include "ImportExportApp.h"
7
8 #include "OCC_MainFrame.h"
9 #include "OCC_3dChildFrame.h"
10 #include "ImportExportDoc.h"
11 #include <OCC_3dView.h>
12 #include <res/resource.h>
13
14 BEGIN_MESSAGE_MAP(CImportExportApp, OCC_App)
15         //{{AFX_MSG_MAP(CSerializeApp)
16         ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
17         //}}AFX_MSG_MAP
18 END_MESSAGE_MAP()
19
20 /////////////////////////////////////////////////////////////////////////////
21 // CImportExportApp construction
22
23 CImportExportApp::CImportExportApp() : OCC_App()
24 {
25   // Set the local system units
26   try
27   {
28     UnitsAPI::SetLocalSystem (UnitsAPI_MDTV);
29   }
30   catch (Standard_Failure)
31   {
32     AfxMessageBox (L"Fatal Error in units initialisation");
33   }
34
35   SampleName = "ImportExport"; //for about dialog
36   SetSamplePath (L"..\\..\\05_ImportExport");
37 }
38
39 /////////////////////////////////////////////////////////////////////////////
40 // The one and only CImportExportApp object
41
42 CImportExportApp theApp;
43
44 /////////////////////////////////////////////////////////////////////////////
45 // CImportExportApp initialization
46
47 BOOL CImportExportApp::InitInstance()
48 {
49         AfxEnableControlContainer();
50
51         // Standard initialization
52         // If you are not using these features and wish to reduce the size
53         //  of your final executable, you should remove from the following
54         //  the specific initialization routines you do not need.
55
56         // Change the registry key under which our settings are stored.
57         // You should modify this string to be something appropriate
58         // such as the name of your company or organization.
59         // Modified by CasCade :
60         SetRegistryKey(_T("Local CasCade Applications"));
61
62         LoadStdProfileSettings();  // Load standard INI file options (including MRU)
63
64         // Register the application's document templates.  Document templates
65         //  serve as the connection between documents, frame windows and views.
66
67         CMultiDocTemplate* pDocTemplate;
68     pDocTemplate = new CMultiDocTemplate(
69                 IDR_3DTYPE,
70                 RUNTIME_CLASS(CImportExportDoc),
71                 RUNTIME_CLASS(OCC_3dChildFrame), 
72                 RUNTIME_CLASS(OCC_3dView));
73         AddDocTemplate(pDocTemplate);
74
75         // create main MDI Frame window
76         OCC_MainFrame* pMainFrame = new OCC_MainFrame(with_AIS_TB);
77         if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
78                 return FALSE;
79         m_pMainWnd = pMainFrame;
80
81         // Parse command line for standard shell commands, DDE, file open
82         CCommandLineInfo cmdInfo;
83         ParseCommandLine(cmdInfo);
84
85         // Dispatch commands specified on the command line
86         if (!ProcessShellCommand(cmdInfo))
87                 return FALSE;
88
89         // The main window has been initialized, so show and update it.
90         pMainFrame->ShowWindow(m_nCmdShow);
91         pMainFrame->UpdateWindow();
92
93         return TRUE;
94 }
95
96 CDocument* CImportExportApp::OpenDocumentFile(LPCTSTR lpszFileName) 
97 {
98         CFile cf;
99         
100         if (!cf.Open(lpszFileName,CFile::modeReadWrite)){ 
101                 AfxMessageBox (L"File not found!");
102                 return NULL;
103         }
104         cf.Close();
105         return CWinApp::OpenDocumentFile(lpszFileName); 
106 }
107
108 void CImportExportApp::OnFileOpen() 
109 {
110   CFileDialog dlg(TRUE,
111                                   NULL,
112                                   NULL,
113                                   OFN_HIDEREADONLY | OFN_FILEMUSTEXIST,
114                                   NULL, 
115                                   NULL );
116   
117
118         CString initdir;
119         initdir.GetEnvironmentVariable (L"CSF_OCCTDataPath");
120
121         dlg.m_ofn.lpstrInitialDir = initdir;
122
123         CString strFilter;
124         CString strDefault;
125
126         POSITION pos = GetFirstDocTemplatePosition();
127
128         CDocTemplate* pTemplate = GetNextDocTemplate(pos);
129         CString strFilterExt, strFilterName;
130         if (pTemplate->GetDocString(strFilterExt, CDocTemplate::filterExt) &&
131                 !strFilterExt.IsEmpty() &&
132                 pTemplate->GetDocString(strFilterName, CDocTemplate::filterName) &&
133                 !strFilterName.IsEmpty()) {
134                 // add to filter
135                 strFilter += strFilterName;
136                 ASSERT(!strFilter.IsEmpty());  // must have a file type name
137                 strFilter += L'\0';  // next string please
138                 strFilter += L'*';
139                 strFilter += strFilterExt;
140                 strFilter += L'\0';  // next string please
141                 dlg.m_ofn.nMaxCustFilter++;             
142         }
143         // append the "*.*" all files filter
144         CString allFilter;
145         VERIFY(allFilter.LoadString(AFX_IDS_ALLFILTER));
146         strFilter += allFilter;
147         strFilter += L'\0';   // next string please
148         strFilter += L"*.*";
149         strFilter += L'\0';   // last string
150         dlg.m_ofn.nMaxCustFilter++;
151         dlg.m_ofn.lpstrFilter = strFilter;
152
153   if (dlg.DoModal() == IDOK) 
154   {
155         AfxGetApp()->OpenDocumentFile(dlg.GetPathName());
156   }     
157 }