Vector Load and Save

Start with the project you created in Creating an empty vector.

Take the following steps to add vector load and save functionality:

1.

Select View->ClassWizard .

2.

In the Class Name drop down box, select CTutorialDoc.

3.

In the Object IDs drop down box, select CTutorialDoc.

4.

In the Messages drop down box, select OnOpenDocument.

5.

Click the Add Function button, and then click the Edit Code button.

6.

Add the following code to CTutorialDoc::OnOpenDocument():

BOOL CTutorialDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
   L_TCHAR szTemp[_MAX_PATH];
   L_BOOL bRet;
   L_INT  nRet;
   
   if (!CDocument::OnOpenDocument(lpszPathName))
      return FALSE;
   
   lstrcpy(szTemp, lpszPathName);
   nRet = m_pVectorWindow->Load (szTemp);
   
   if (nRet == SUCCESS)
   {
      m_pVectorWindow->EnableAutoScroll (TRUE);
      m_pVectorWindow->SetEngine ();
      m_pVectorWindow->Reset ();
      
   }
   else
   {
      CString strTmp;
      
      strTmp.Format(TEXT("Error Loading File[%d]"), nRet);
      AfxMessageBox(strTmp);
      bRet = FALSE;
   }
   return bRet; 
}

7.

Select View->ClassWizard

8.

In the Class Name drop down box, select CTutorialDoc

9.

In the Object IDs drop down box, select CTutorialDoc

10.

In the Messages drop down box, select OnSaveDocument

11.

Click the Add Function button, and then click the Edit Code button.

12.

Code CTutorialDoc::OnSaveDocument() as follows:

BOOL CTutorialDoc::OnSaveDocument(LPCTSTR lpszPathName) 
{
   L_TCHAR       szFileName[_MAX_PATH];
   L_INT        nRet = 0;
   
   lstrcpy(szFileName, lpszPathName);
   
   //Apply any transformation before the save
   m_pVectorWindow->ApplyTransformation ();
   nRet = m_pVectorWindow->Save ( szFileName, m_iFileSaveType);
   if (nRet != SUCCESS)
   {
      CString strTmp;
      
      strTmp.Format(TEXT("Error Saving File[%d]"), nRet);
      AfxMessageBox(strTmp);
   }
   
   if (nRet == SUCCESS)
      SetModifiedFlag(FALSE);
   return (nRet == SUCCESS);
}

13.

Click on the "Class View" tab, right-click CTutorialDoc, and choose "Add Member Variable..."

14.

For "Variable Type" enter

L_INT

15.

For "Variable Name" enter

m_iFileSaveType

16.

Leave "Access" as public and click "Ok"

17.

Select View->ClassWizard

18.

In the Class Name drop down box, select CTutorialView

19.

In the Object IDs drop down box, select ID_FILE_SAVE

20.

In the Messages drop down box, select COMMAND

21.

Click the Add Function button, and then click the Edit Code button.

22.

Add the following code to CTutorialView::OnFileSave ()


   static const L_TCHAR szSaveFileFilter[] = TEXT("DXF(*.dxf)|*.dxf|WMF(*.wmf)|*.wmf|EMF(*.emf)|*.emf|DRW(*.drw)|*.drw|CGM(*.cgm)|*.cgm|PLT(*.plt)|*.plt|PCT(*.pct)|*.pct|Intergraph Vector(*.vec)|*.vec|TIF DXF(*.tif)|*.tif||");
   L_UINT uNotUsed;
   L_TCHAR szFileName[_MAX_PATH], *pszFileName;
   L_INT  nRet;

   nRet = GetDocument()->m_pVectorWindow->GetFileName (szFileName, &uNotUsed);
   pszFileName = (nRet == SUCCESS) ? szFileName : NULL;

   CFileDialog SaveDialog(
      FALSE, 
      TEXT("dxf"),
      pszFileName, 
      OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
      szSaveFileFilter,
      this
      );

   nRet = SaveDialog.DoModal();
   if (IDOK == nRet)
   {
      CString strFilename;
      strFilename = SaveDialog.GetPathName();
      L_INT nIndex = SaveDialog.m_ofn.nFilterIndex;
      switch (nIndex)
      {
      case 1:
         GetDocument()->m_iFileSaveType = FILE_DXF;
         break;

      case 2:
         GetDocument()->m_iFileSaveType = FILE_WMF;
         break;

      case 3:
         GetDocument()->m_iFileSaveType = FILE_EMF;
         break;

      case 4:
         GetDocument()->m_iFileSaveType = FILE_DRW;
         break;
         
      case 5 :
         GetDocument()->m_iFileSaveType = FILE_CGM;
         break;
         
      case 6:
         GetDocument()->m_iFileSaveType = FILE_PLT;
         break;
         
      case 7:
         GetDocument()->m_iFileSaveType = FILE_PCT;
         break;
         
      case 8:
         GetDocument()->m_iFileSaveType = FILE_INTERGRAPH_VECTOR;
         break;
         
      case 9:
         GetDocument()->m_iFileSaveType = FILE_TIF_DXF;
         break;
         
      }
      GetDocument()->OnSaveDocument(strFilename);
   }
}

23.

Select View->ClassWizard

24.

In the Class Name drop down box, select CTutorialView

25.

In the Object IDs drop down box, select ID_FILE_SAVE_AS

26.

In the Messages drop down box, select COMMAND

27.

Click the Add Function button, and then click the Edit Code button.

28.

Add the following code to CTutorialView::OnFileSaveAs ()

void CTutorialView::OnFileSaveAs() 
{
   OnFileSave();
}

29.

Compile and run the project. You will be able to view, load and save vector files as you browse your local hard disk. You can also use the numeric key pad to zoom in (+ key) and zoom out (- key).