home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2002 March / PCWMAR02.iso / software / turbocad / v8trial / TurboCADv8ProfessionalNoReg.exe / Data.Cab / F37607_ViewWnd.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-09-22  |  12.3 KB  |  596 lines

  1. /******************************************************************/
  2. /*                                                                */
  3. /*                      TurboCAD for Windows                      */
  4. /*                   Copyright (c) 1993 - 2001                    */
  5. /*             International Microcomputer Software, Inc.         */
  6. /*                            (IMSI)                              */
  7. /*                      All rights reserved.                      */
  8. /*                                                                */
  9. /******************************************************************/
  10.  
  11. // ViewWnd.cpp : implementation file
  12. //
  13.  
  14. #include "stdafx.h"
  15. #include "tcintf.h"
  16. #include "InsTool.h"
  17. #include "InsSymb.h"
  18. #include "ViewWnd.h"
  19. #include "TDialog.h"
  20.  
  21.  
  22.  
  23. #ifdef _DEBUG
  24. #define new DEBUG_NEW
  25. #undef THIS_FILE
  26. static char THIS_FILE[] = __FILE__;
  27. #endif
  28.  
  29. /////////////////////////////////////////////////////////////////////////////
  30. // CPreviewWnd
  31.  
  32. CPreviewWnd::CPreviewWnd(CInsSymb* pTool)
  33. {
  34.     EnableAutomation();
  35.     m_FirstStart = TRUE;
  36.     m_pIApp = NULL;
  37.     m_pDwgs = NULL;
  38.     m_pPreviewDrawing = NULL;
  39.     m_pPreviewGrs = NULL;
  40.     m_pPreviewView = NULL; 
  41.     m_pViewsCol = NULL;
  42.     m_hCurActDr = NULL;
  43.     
  44.     {
  45.         m_pTool = pTool;
  46.     }
  47. }
  48.  
  49. CPreviewWnd::~CPreviewWnd()
  50. {
  51. }
  52.  
  53. void CPreviewWnd::OnFinalRelease()
  54. {
  55.     // When the last reference for an automation object is released
  56.     // OnFinalRelease is called.  The base class will automatically
  57.     // deletes the object.  Add additional cleanup required for your
  58.     // object before calling the base class.
  59.  
  60.     CWnd::OnFinalRelease();
  61. }
  62.  
  63.  
  64. BEGIN_MESSAGE_MAP(CPreviewWnd, CWnd)
  65.     //{{AFX_MSG_MAP(CPreviewWnd)
  66.     ON_WM_PAINT()
  67.     ON_WM_ERASEBKGND()
  68.     //}}AFX_MSG_MAP
  69. END_MESSAGE_MAP()
  70.  
  71.  
  72. BEGIN_DISPATCH_MAP(CPreviewWnd, CWnd)
  73.     //{{AFX_DISPATCH_MAP(CPreviewWnd)
  74.         // NOTE - the ClassWizard will add and remove mapping macros here.
  75.     //}}AFX_DISPATCH_MAP
  76. END_DISPATCH_MAP()
  77.  
  78. // Note: we add support for IID_IViewWnd to support typesafe binding
  79. //  from VBA.  This IID must match the GUID that is attached to the 
  80. //  dispinterface in the .ODL file.
  81.  
  82. static const IID IID_IViewWnd =
  83. { 0x4b1aa263, 0x851c, 0x11d2, { 0x8b, 0xea, 0x0, 0x40, 0x33, 0x38, 0xc5, 0x4 } };
  84.  
  85.  
  86. const IID IID_ITcApp =  {0x6A481200,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  87. const IID IID_ITcDrawing = {0x6A481201,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  88.  
  89. BEGIN_INTERFACE_MAP(CPreviewWnd, CWnd)
  90.     INTERFACE_PART(CPreviewWnd, IID_IViewWnd, Dispatch)
  91. END_INTERFACE_MAP()
  92.  
  93. /////////////////////////////////////////////////////////////////////////////
  94. // CPreviewWnd message handlers
  95.  
  96. BOOL CPreviewWnd::CreatePreview()
  97. {
  98.     HRESULT hRes = E_FAIL;
  99.     COleVariant varType((const long)imsiPolyline);
  100.     COleVariant varOptional(varMissing);
  101.  
  102.  
  103.     try
  104.     {    
  105.         ClearAll();
  106.         HMODULE    hDBAPI = NULL;
  107.         hDBAPI = GetModuleHandle(DBAPI80);
  108.         if (hDBAPI == NULL)
  109.         {
  110.             hDBAPI = GetModuleHandle(DBAPI70);
  111.  
  112.             if (hDBAPI == NULL)
  113.             {
  114.                 hDBAPI = GetModuleHandle(DBAPI60);
  115.             }        
  116.         }
  117.  
  118.         if (hDBAPI == NULL)
  119.             return FALSE;
  120.         else 
  121.         m_pfGetApp = (APPGETCURRENTAPP)GetProcAddress(hDBAPI, _T("AppGetCurrentApp"));
  122.         if (m_pfGetApp == NULL)                                                    
  123.             return FALSE;
  124.  
  125.         m_lApp = (*m_pfGetApp)();
  126.         if (m_lApp == 0)
  127.             return FALSE;
  128.             
  129.         m_pfFileOpen = (GRAPHICFILEOPENEX)GetProcAddress(hDBAPI, _T("GraphicFileOpenEx"));
  130.         if (m_pfFileOpen == NULL)                                                    
  131.             return FALSE;
  132.  
  133.         m_pfAppDwgNew = (APPDRAWINGNEWEX)GetProcAddress(hDBAPI, _T("AppDrawingNewEx"));
  134.         if (m_pfAppDwgNew == NULL)                                                    
  135.             return FALSE;
  136.  
  137.         BOOL i;
  138.         i = TRUE;
  139.         COleVariant varItem = (0L);
  140.         long nDrawings = 0; 
  141.  
  142.         m_lDwg = (*m_pfAppDwgNew)(m_lApp, i, NULL, NULL);
  143.  
  144.         hRes = ((CTDialog*)GetParent())->m_pThisTool->get_Application(&m_pIApp);                
  145.         CHECK_HRESULT(hRes)
  146.  
  147.         hRes = m_pIApp->get_Drawings(&m_pDwgs);
  148.         CHECK_HRESULT(hRes)
  149.  
  150.         hRes = m_pDwgs->get_Count(&nDrawings);
  151.         CHECK_HRESULT(hRes)
  152.             
  153.         varItem = nDrawings - 1L;
  154.             
  155.         hRes = m_pDwgs->get_Item(&varItem, &m_pPreviewDrawing);
  156.         CHECK_HRESULT(hRes)
  157.             
  158.         hRes = m_pPreviewDrawing->get_Views(&m_pViewsCol);
  159.         CHECK_HRESULT(hRes)
  160.             
  161.             
  162.         HWND hWnd = ::GetDlgItem(GetParent()->m_hWnd, IDC_PREVIEW);
  163.         COleVariant varHwnd((long)hWnd);
  164.         long nViews = 0;
  165.  
  166.         hRes = m_pViewsCol->Add(&varHwnd, &varOptional, &m_pPreviewView );
  167.         CHECK_HRESULT(hRes)
  168.             
  169.         hRes = m_pPreviewView->put_Update(FALSE);
  170.         CHECK_HRESULT(hRes)
  171.             
  172.  
  173.         hRes = m_pPreviewView->put_MappingMode(MM_TEXT);
  174.         CHECK_HRESULT(hRes)
  175.  
  176.         hRes = m_pPreviewView->put_FixedAspectRatio(TRUE);
  177.         CHECK_HRESULT(hRes)
  178.     
  179.         hRes = m_pPreviewView->put_Margins(FALSE);
  180.         CHECK_HRESULT(hRes)
  181.  
  182.         CPreviewWnd *pPreview = (CPreviewWnd *) (((CTDialog*)GetParent())->GetDlgItem(IDC_PREVIEW));
  183.  
  184.         AFX_MANAGE_STATE(m_pTool->m_pTCState);
  185.  
  186.         CRect rcBounds;
  187.         pPreview->GetClientRect(&rcBounds);
  188.         pPreview->Invalidate(); 
  189.  
  190.         hRes = m_pPreviewView->put_ScreenLeft(rcBounds.left);
  191.         CHECK_HRESULT(hRes)
  192.  
  193.         hRes = m_pPreviewView->put_ScreenTop(rcBounds.top);
  194.         CHECK_HRESULT(hRes)
  195.  
  196.         hRes = m_pPreviewView->put_ScreenWidth(rcBounds.Width());
  197.         CHECK_HRESULT(hRes)
  198.  
  199.         hRes = m_pPreviewView->put_ScreenHeight(rcBounds.Height());
  200.         CHECK_HRESULT(hRes)
  201.  
  202.         hRes = m_pPreviewView->ZoomToExtents();
  203.         CHECK_HRESULT(hRes)
  204.  
  205.         VariantClear(&varType);
  206.         VariantClear(&varOptional);
  207.         
  208.     }
  209.     catch (...)
  210.     {
  211.  
  212.         TRACE_EXCEPTION("CPreviewWnd::CreatePreview")
  213.         ClearAll();
  214.         _clearfp();
  215.  
  216.     }
  217.     if(m_pIApp != NULL)
  218.     {
  219.         m_pIApp->Release();
  220.         m_pIApp = NULL;
  221.     }
  222.     return TRUE;
  223. }
  224.  
  225. BOOL CPreviewWnd::DoPreview()
  226. {
  227.     HRESULT hRes = E_FAIL;
  228.     BOOL bRes = FALSE;
  229.     COleVariant varType((const long)imsiGroup);
  230.     COleVariant varOptional(varMissing);
  231.     CString cstrError;
  232.     BOOL curMode;
  233.     BOOL i;
  234.     i = TRUE;
  235.     long IDwg1 = 0;
  236.     COleVariant varItem = (0L);
  237.     long nDrawings = 0; 
  238.     IDrawing* pTmpDwg = NULL;
  239.  
  240.     Properties *pProps = NULL;
  241.     Property *pProp = NULL;
  242.     Properties *pProps1 = NULL;
  243.     Property *pProp1 = NULL;
  244.     CMerge merge;
  245.     IGraphic* pIGrTmp = NULL;
  246.     IGraphic* pIGrTmp1 = NULL;
  247.  
  248.     ITcDrawing* pITcDr = NULL;
  249.     ITcApp* pTcApp = NULL;
  250.     IDrawing* pIDwg = NULL;
  251.  
  252.     try
  253.     {
  254.         CButton *pCheckBox = (CButton *) (GetParent()->GetDlgItem(IDC_SPACEMODE));
  255.  
  256.         
  257.         CSymbolsList *pList = (CSymbolsList *) (GetParent()->GetDlgItem(IDC_SYMBOLSLIST));
  258.  
  259.         int index = pList->GetCurSel();
  260.  
  261.         pList->GetText(index, m_selected);
  262.  
  263.         IDwg1 = (*m_pfAppDwgNew)(m_lApp, i, NULL, NULL);
  264.             
  265.         hRes = m_pDwgs->get_Count(&nDrawings);
  266.         CHECK_HRESULT(hRes)
  267.             
  268.         varItem = nDrawings - 1L;
  269.             
  270.         hRes = m_pDwgs->get_Item(&varItem, &pTmpDwg);
  271.         CHECK_HRESULT(hRes)
  272.  
  273.         AFX_MANAGE_STATE(m_pTool->m_pTCState);
  274.  
  275.         DWORD dwRes = (*m_pfFileOpen)(0, IDwg1, m_lApp, (LPTSTR)(LPCSTR)m_selected, NULL, cstrError.GetBuffer(256), 255,  GF_OPENMODE, 0, NULL );
  276.  
  277.         if (dwRes == NULL)
  278.             return FALSE;
  279.         
  280.         CString cstrName("TileMode");//# Non-localizable string#
  281.         BSTR bstrName = cstrName.AllocSysString();
  282.  
  283.       ::VariantInit(&varItem);
  284.         varItem.vt = VT_BSTR;
  285.         varItem.bstrVal = bstrName;
  286.  
  287.         hRes = pTmpDwg->get_Properties(&pProps);
  288.         CHECK_HRESULT(hRes)
  289.  
  290.         hRes = pProps->get_Item(&varItem, &pProp);
  291.         CHECK_HRESULT(hRes)
  292.  
  293.         hRes = m_pPreviewDrawing->get_Properties(&pProps1);
  294.         CHECK_HRESULT(hRes)
  295.  
  296.         hRes = pProps1->get_Item(&varItem, &pProp1);
  297.         CHECK_HRESULT(hRes)
  298.  
  299.         COleVariant varVal;
  300.         varVal.vt = VT_I2;
  301.  
  302.         pProp->get_Value(NULL, &varVal);
  303.         long dd = varVal.iVal;
  304.  
  305.         if (varVal.iVal == 1)
  306.         {
  307.             curMode = 1;//imsiModelSpace;
  308.         }
  309.         else 
  310.         {
  311.             curMode = 0;//imsiPaperSpace;
  312.         
  313.         }
  314.  
  315.  
  316.  
  317. // switch the Preview drawing and temp drawing to paper space. 
  318.         varVal.iVal = 0;
  319.         hRes = pProp1->put_Value(NULL, &varVal);
  320.         CHECK_HRESULT(hRes)
  321.  
  322.         hRes = pProp->put_Value(NULL, &varVal);
  323.         CHECK_HRESULT(hRes)
  324.  
  325.         BOOL bGrsOnly;
  326.         bGrsOnly = FALSE;
  327. // merge the paper space graphics collection and blocks, styles, layers...    
  328.         hRes = merge.Init(pTmpDwg, m_pPreviewDrawing, FALSE);
  329.         CHECK_HRESULT(hRes)
  330.         
  331.         hRes = merge.Run(&pIGrTmp, bGrsOnly);
  332.         CHECK_HRESULT(hRes)
  333.  
  334.             
  335. // switch the Preview drawing and temp drawing to model space. 
  336.       
  337.         varVal.iVal = 1;
  338.  
  339.         hRes = pProp1->put_Value(NULL, &varVal);
  340.         CHECK_HRESULT(hRes)
  341.  
  342.         hRes = pProp->put_Value(NULL, &varVal);
  343.         CHECK_HRESULT(hRes)
  344.  
  345.         // merge the model space graphics collection    
  346.  
  347.         bGrsOnly = FALSE;
  348.         hRes = merge.Init(pTmpDwg, m_pPreviewDrawing, FALSE);
  349.         CHECK_HRESULT(hRes)
  350.  
  351.         hRes = merge.Run(&pIGrTmp1, bGrsOnly);
  352.         CHECK_HRESULT(hRes)
  353.  
  354.  
  355.  
  356. // set the space mode for Preview drawing the same like in file that is open.
  357.         if (curMode == 0) //imsiPaperSpace 
  358.         {
  359.             varVal.iVal = 0;
  360.  
  361.             hRes = pProp1->put_Value(NULL, &varVal);
  362.             CHECK_HRESULT(hRes)
  363.  
  364.         }
  365.         else
  366.         {
  367.             
  368.             //    curMode = imsiModelSpace;
  369.             varVal.iVal = 1;
  370.  
  371.             hRes = pProp1->put_Value(NULL, &varVal);
  372.             CHECK_HRESULT(hRes)
  373.  
  374.         }
  375.  
  376.         hRes = m_pPreviewDrawing->get_Application(&m_pIApp);
  377.         CHECK_HRESULT(hRes)
  378.     
  379.  
  380.         hRes = m_pIApp->QueryInterface(IID_ITcApp, (void **) &pTcApp); 
  381.         CHECK_HRESULT(hRes)
  382.             
  383.         hRes = pTcApp->GetActiveDrawing(&pITcDr);
  384.         CHECK_HRESULT(hRes)
  385.             
  386.                 
  387.         hRes = pITcDr->QueryInterface(IID_IDrawing, (void **) &pIDwg); 
  388.         CHECK_HRESULT(hRes)
  389.  
  390.         hRes = pIDwg->get__Handle((long*)&m_hCurActDr);
  391.         CHECK_HRESULT(hRes)
  392.  
  393.         if (pProp != NULL)
  394.         {
  395.             pProp->Release();
  396.             pProp = NULL;
  397.         }
  398.  
  399.         if (pProps != NULL)
  400.         {
  401.             pProps->Release();
  402.             pProps = NULL;
  403.         }
  404.  
  405.         if(pIGrTmp !=NULL)
  406.         {
  407.             pIGrTmp->Release();
  408.             pIGrTmp = NULL;
  409.         }
  410.  
  411.         COleVariant varOptional(varMissing);
  412.         pTmpDwg->Close(varOptional, varOptional, varOptional);
  413.  
  414.         if (m_hCurActDr != NULL)
  415.         {
  416.             hRes = m_pIApp->QueryInterface(IID_ITcApp, (void **) &pTcApp); 
  417.             CHECK_HRESULT(hRes)
  418.  
  419.             hRes = pTcApp->SetActiveDrawing(m_hCurActDr, NULL);
  420.             CHECK_HRESULT(hRes)
  421.  
  422.             m_hCurActDr = NULL;
  423.         }
  424.             
  425.  
  426.         hRes = m_pPreviewView->ZoomToExtents();
  427.         CHECK_HRESULT(hRes)
  428.  
  429.         if(pCheckBox == NULL)
  430.             return FALSE;
  431.  
  432.         if (curMode == 1) //imsiModelSpace 
  433.             pCheckBox->SetCheck(1);
  434.  
  435.         else
  436.             pCheckBox->SetCheck(0);
  437.  
  438.         bRes = TRUE;
  439.  
  440.     }
  441.     catch (...)
  442.     {
  443.         _clearfp();
  444.         TRACE_EXCEPTION("CPreviewWnd::DoPreview")
  445.         hRes = E_FAIL;
  446.     }
  447.  
  448.     if(pIDwg !=NULL)
  449.     {
  450.         pIDwg->Release();
  451.         pIDwg = NULL;
  452.     }
  453.         
  454.     if(pITcDr != NULL)
  455.     {
  456.         pITcDr->Release();
  457.         pITcDr = NULL;
  458.     }
  459.     if(pTcApp != NULL)
  460.     {
  461.         pTcApp->Release();
  462.         pTcApp = NULL;
  463.     }
  464.  
  465.     if (pProp1 != NULL)
  466.     {
  467.         pProp1->Release();
  468.         pProp1 = NULL;
  469.     }
  470.     if (pProps1 != NULL)
  471.     {
  472.         pProps1->Release();
  473.         pProps1 = NULL;
  474.     }
  475.  
  476.     if(pTmpDwg != NULL)
  477.     {
  478.         pTmpDwg->Release();
  479.         pTmpDwg = NULL;
  480.     }
  481.     if(pTcApp != NULL)
  482.     {
  483.         pTcApp->Release();
  484.         pTcApp = NULL;
  485.     }
  486.     if(m_pIApp != NULL)
  487.     {
  488.         m_pIApp->Release();
  489.         m_pIApp = NULL;
  490.     }
  491.  
  492.     return bRes;
  493. }
  494.  
  495. void CPreviewWnd::RefreshList()
  496. {
  497.  
  498. }
  499.  
  500. void CPreviewWnd::Clear()
  501. {
  502.     Invalidate();
  503. }
  504.  
  505. void CPreviewWnd::ClearAll()
  506. {
  507.     try
  508.     {
  509.  
  510.  
  511.         if (m_pPreviewGrs != NULL)
  512.         {
  513.             m_pPreviewGrs->Release();
  514.             m_pPreviewGrs = NULL;
  515.  
  516.         }
  517.         if (m_pPreviewView != NULL)
  518.         {
  519.             m_pPreviewView->Delete();
  520.             m_pPreviewView->Release();
  521.             m_pPreviewView = NULL;
  522.         
  523.         }
  524.         if (m_pViewsCol != NULL)
  525.         {
  526.             m_pViewsCol->Release();
  527.             m_pViewsCol = NULL;
  528.  
  529.         }
  530.         if (m_pPreviewDrawing != NULL)
  531.         {
  532.  
  533.             COleVariant varOptional(varMissing);
  534.             HRESULT hRes = m_pPreviewDrawing->Close(varOptional, varOptional, varOptional);
  535.             CHECK_HRESULT(hRes)
  536.             m_pPreviewDrawing->Release();
  537.             m_pPreviewDrawing = NULL;
  538.         }
  539.  
  540.         m_FirstStart = TRUE;
  541.     }
  542.  
  543.     catch (...)
  544.     {
  545.         _clearfp();
  546.         TRACE_EXCEPTION("CPreviewWnd::ClearAll")
  547.     }
  548.  
  549.     if (m_pDwgs != NULL)
  550.     {
  551.         m_pDwgs->Release();
  552.         m_pDwgs = NULL;
  553.     }
  554.  
  555.     if (m_pIApp != NULL)
  556.     {
  557.         m_pIApp->Release();
  558.         m_pIApp = NULL;
  559.     }
  560.  
  561. }
  562.  
  563. void CPreviewWnd::OnPaint() 
  564. {
  565.     CPaintDC dc(this); // device context for painting
  566.     
  567.     // TODO: Add your message handler code here
  568.     if (m_pPreviewView == NULL)
  569.         return;
  570.  
  571.     // Force redraw of view window
  572.     try
  573.     {
  574.         HRESULT hRes = m_pPreviewView->Refresh();
  575.         CHECK_HRESULT(hRes)
  576.  
  577.     }
  578.     catch (...)
  579.     {
  580.         _clearfp();
  581.         TRACE_EXCEPTION("CPreviewWnd::Paint")
  582.     }
  583.     
  584.     // Do not call CWnd::OnPaint() for painting messages
  585. }
  586.  
  587. BOOL CPreviewWnd::OnEraseBkgnd(CDC* pDC) 
  588. {
  589.     // TODO: Add your message handler code here and/or call default
  590.     CRect rect;
  591.     GetClientRect(rect);
  592.     pDC->FillSolidRect(rect, ::GetSysColor(COLOR_WINDOW));
  593.     return TRUE;
  594. }
  595.  
  596.