请高手指点呀:怎样删除一个DWG文件
本帖最后由 作者 于 2005-11-28 11:41:49 编辑 <br /><br /> <P>想请教各位大侠一个问题</P><P>在ARX中怎么来实现,打开和删除一个DWG文件</P>
<P>请举个例子,在这里先谢谢了</P> <P>AcApDocument* pDoc;</P>
<P>pdoc = acdocmanager->curdocument(); <BR>acdocmanager->activatedocument(pdoc, adesk::ktrue); <BR>acdocmanager->closedocument(pdoc);</P>
<P>想用以上代码关闭一个DWG文件,但总会弹出是否保存该文件的对话框,<BR>若将文件保存该怎么用代码来实现,若不保存直接关闭又要怎么实现,还请高手指点呀!在此先谢过了<BR></P> <P>帮帮忙吧</P>
<P>谢谢了</P> errBoy发表于2005-11-25 17:27:00static/image/common/back.gif
AcApDocument* pDoc;
pdoc = acdocmanager->curdocument(); acdocmanager->activatedocument(pdoc, ...
<BR>我也想知道,而且如果在对话框中进行closeDocument的命令时,总要等到对话框结束以后才能关闭?不知道为什么 <P>// (C) Copyright 1998-2002 by Autodesk, Inc.<BR>//<BR>// Permission to use, copy, modify, and distribute this software in<BR>// object code form for any purpose and without fee is hereby granted,<BR>// provided that the above copyright notice appears in all copies and<BR>// that both that copyright notice and the limited warranty and<BR>// restricted rights notice below appear in all supporting<BR>// documentation.<BR>//<BR>// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.<BR>// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF<BR>// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.<BR>// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE<BR>// UNINTERRUPTED OR ERROR FREE.<BR>//<BR>// Use, duplication, or disclosure by the U.S. Government is subject to<BR>// restrictions set forth in FAR 52.227-19 (Commercial Computer<BR>// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)<BR>// (Rights in Technical Data and Computer Software), as applicable.<BR>//<BR>// MDITestDialog.cpp : implementation file<BR>//</P>
<P>#include "stdafx.h"<BR>#include "acdocman.h"<BR>#include "MDITestDialog.h"<BR>#include "common.h"</P>
<P>#ifdef _DEBUG<BR>#define new DEBUG_NEW<BR>#undef THIS_FILE<BR>static char THIS_FILE[] = __FILE__;<BR>#endif</P>
<P>extern "C" HWND adsw_acadMainWnd();</P>
<P>/////////////////////////////////////////////////////////////////////////////<BR>// MDITestDialog message handlers<BR>/////////////////////////////////////////////////////////////////////////////<BR>// CMDITestDialog dialog</P>
<P><BR>CMDITestDialog::CMDITestDialog(CWnd* pParent /*=NULL*/)<BR> : CDialog(CMDITestDialog::IDD, pParent)<BR>{<BR> //{{AFX_DATA_INIT(CMDITestDialog)<BR> //}}AFX_DATA_INIT<BR>}</P>
<P><BR>void CMDITestDialog::DoDataExchange(CDataExchange* pDX)<BR>{<BR> CDialog::DoDataExchange(pDX);<BR> //{{AFX_DATA_MAP(CMDITestDialog)<BR> DDX_Control(pDX, IDC_ACT_MYLK_STATUS_FIELD_STATIC, m_staticActDocMyLockStatus);<BR> DDX_Control(pDX, IDC_CURDOC_FIELD_STATIC, m_staticCurrentDoc);<BR> DDX_Control(pDX, IDC_CUR_LK_STATUS_FIELD_STATIC, m_staticCurDocLockStatus);<BR> DDX_Control(pDX, IDC_CUR_MYLK_STATUS_FIELD_STATIC, m_staticCurDocMyLockStatus);<BR> DDX_Control(pDX, IDC_ACT_LK_STATUS_FIELD_STATIC, m_staticActDocLockStatus);<BR> DDX_Control(pDX, IDC_LOCK_COMBO, m_lockCombo);<BR> DDX_Control(pDX, IDC_ACTIVEDOC_FIELD_STATIC, m_staticActiveDoc);<BR> DDX_Control(pDX, IDC_TOBECURRDOC_FIELD_STATIC, m_staticToBeCurrDoc);<BR> DDX_Control(pDX, IDC_ACTIVATION_CHECK, m_activationCheck);<BR> DDX_Control(pDX, IDC_NUMDOCS_STATIC, m_staticDocNum);<BR> DDX_Control(pDX, IDC_DOC_LIST, m_docListBox);<BR> //}}AFX_DATA_MAP<BR>}</P>
<P><BR>BEGIN_MESSAGE_MAP(CMDITestDialog, CDialog)<BR> ON_MESSAGE( WM_ACAD_KEEPFOCUS, onAcadKeepFocus )<BR> ON_MESSAGE( WM_ACAD_UPDATEDIALOG, onAcadUpdateDialog )<BR> ON_MESSAGE( WM_ACAD_CREATEDOC, onAcadCreateDoc )<BR> ON_MESSAGE( WM_ACAD_DESTROYDOC, onAcadDestroyDoc )<BR> ON_MESSAGE( WM_ACAD_LOCKMODCHANGED, onAcadLockModeChanged )<BR> ON_MESSAGE( WM_ACAD_LOCKMODWILLCHANGE, onAcadLockModeWillChange )<BR> ON_MESSAGE( WM_ACAD_CURRENTDOC, onAcadCurrDoc )<BR> ON_MESSAGE( WM_ACAD_ACTIVATEDOC, onAcadActivateDoc )<BR> ON_MESSAGE( WM_ACAD_DEACTIVATEDOC, onAcadDeactivateDoc )<BR> ON_MESSAGE( WM_ACAD_ACTMODIFIED, onAcadActModifiedDoc )<BR> //{{AFX_MSG_MAP(CMDITestDialog)<BR> ON_BN_CLICKED(IDC_NEWDOC_BUTTON, OnNewdocButton)<BR> ON_BN_CLICKED(IDC_APPNEWDOC_BUTTON, OnAppnewdocButton)<BR> ON_BN_CLICKED(IDC_APPOPENDOC_BUTTON, OnAppopendocButton)<BR> ON_BN_CLICKED(IDC_ACTIVATION_CHECK, OnActivationCheck)<BR> ON_BN_CLICKED(IDC_SENDSTRING_BUTTON, OnSendstringButton)<BR> ON_BN_CLICKED(IDC_OPENDOC_BUTTON, OnOpendocButton)<BR> ON_BN_CLICKED(IDC_ISQUIESCENT_BUTTON, OnIsquiescentButton)<BR> ON_BN_CLICKED(IDC_CLOSEDOC_BUTTON, OnClosedocButton)<BR> ON_BN_CLICKED(IDC_ACTIVATEDOC_BUTTON, OnActivatedocButton)<BR> ON_BN_CLICKED(IDC_SETCURRENTDOC_BUTTON, OnSetcurrentdocButton)<BR> ON_BN_CLICKED(ID_REFRESH_BUTTON, OnRefreshButton)<BR> //}}AFX_MSG_MAP<BR>END_MESSAGE_MAP()</P>
<P> </P>
<P>/////////////////////////////////////////////////////////////////////////////<BR>// CMDITestDialog message handlers</P>
<P>BOOL CMDITestDialog::Create( CWnd* pParent )<BR>{<BR> // TODO: Add your specialized code here and/or call the base class<BR> <BR> return CDialog::Create(CMDITestDialog::IDD, pParent);<BR>}</P>
<P>// This function gets called repeatedly whenever the mouse is moved over<BR>// the AutoCAD window.<BR>//<BR>LRESULT CMDITestDialog::onAcadKeepFocus( WPARAM, LPARAM )<BR>{<BR> return TRUE;<BR>}</P>
<P>LRESULT CMDITestDialog::onAcadUpdateDialog( WPARAM, LPARAM )<BR>{<BR> // update elements of the dialog to reflect the current<BR> // state of the documents</P>
<P> // get the current number of documents<BR> m_staticDocNum.SetWindowText( getDocCount() );</P>
<P> // check/uncheck document activation<BR> m_activationCheck.SetCheck( acDocManager->isDocumentActivationEnabled() );</P>
<P> // set the current document fields<BR> CString fName;<BR> AcApDocument *pCurrDoc = acDocManager->curDocument();<BR> if( pCurrDoc ) {<BR> fName = pCurrDoc->docTitle();<BR> m_staticCurrentDoc.SetWindowText(fName);<BR> m_staticToBeCurrDoc.SetWindowText(fName);<BR> m_staticCurDocLockStatus.SetWindowText( modeStr(pCurrDoc->lockMode()) );<BR> m_staticCurDocMyLockStatus.SetWindowText( modeStr(pCurrDoc->myLockMode()) );<BR> }<BR> else {<BR> m_staticCurrentDoc.SetWindowText(fName);<BR> m_staticToBeCurrDoc.SetWindowText("");<BR> m_staticCurDocLockStatus.SetWindowText("");<BR> m_staticCurDocMyLockStatus.SetWindowText("");<BR> }</P>
<P> // set the active document data<BR> AcApDocument *pActDoc = acDocManager->mdiActiveDocument();</P>
<P> if( pActDoc ) {<BR> // active document name<BR> fName = pActDoc->docTitle();<BR> m_staticActiveDoc.SetWindowText(fName);<BR> // active document lock modes<BR> m_staticActDocLockStatus.SetWindowText( modeStr(pActDoc->lockMode()) );<BR> m_staticActDocMyLockStatus.SetWindowText( modeStr(pActDoc->myLockMode()) );<BR> }<BR> else {<BR> m_staticActiveDoc.SetWindowText("");<BR> m_staticActDocLockStatus.SetWindowText("");<BR> m_staticActDocMyLockStatus.SetWindowText("");<BR> }</P>
<P> // rebuild listbox<BR> RebuildListBox();<BR> return TRUE;<BR>}</P>
<P> </P>
<P>///////////////////////////////////////////////////////////////<BR>// Note: here you could do different actions for each reactor<BR>// intercepted. For this sample I use a brute force approach:<BR>// everything just updates the ui (with few exceptions)<BR>///////////////////////////////////////////////////////////////<BR>LRESULT CMDITestDialog::onAcadCreateDoc ( WPARAM, LPARAM )<BR>{<BR> onAcadUpdateDialog(0,0L);<BR> return TRUE;<BR>}</P>
<P><BR>LRESULT CMDITestDialog::onAcadDestroyDoc ( WPARAM, LPARAM )<BR>{<BR> // if we went to 0 doc state, clean all the pertinent <BR> // ui elememnts of the dialog. Note however, that this function is<BR> // activated upon SendMessage (instead of PostMessage) so the document<BR> // count right here is still one more than the end result.<BR> // That's why 1 is subtracked from nDoc in the comparison.<BR> int nDoc = acDocManager->documentCount();<BR> if( 0 == nDoc - 1 )<BR> cleanUpUI();<BR> else<BR> onAcadUpdateDialog(0,0L);<BR> return TRUE;<BR>}</P>
<P><BR>LRESULT CMDITestDialog::onAcadLockModeChanged ( WPARAM, LPARAM pCh )<BR>{<BR> onAcadUpdateDialog(0,0L);<BR> return TRUE;<BR>}</P>
<P><BR>LRESULT CMDITestDialog::onAcadLockModeWillChange ( WPARAM, LPARAM pCh )<BR>{<BR> onAcadUpdateDialog(0,0L);<BR> return TRUE;<BR>}</P>
<P><BR>LRESULT CMDITestDialog::onAcadCurrDoc ( WPARAM, LPARAM )<BR>{<BR> onAcadUpdateDialog(0, 0L);<BR> return TRUE;<BR>}</P>
<P><BR>LRESULT CMDITestDialog::onAcadActivateDoc ( WPARAM, LPARAM )<BR>{<BR> onAcadUpdateDialog(0, 0L);<BR> return TRUE;<BR>}</P>
<P><BR>LRESULT CMDITestDialog::onAcadDeactivateDoc ( WPARAM, LPARAM )<BR>{<BR> // clear the fields related to the active document<BR> m_staticActiveDoc.SetWindowText("");<BR> m_staticActDocLockStatus.SetWindowText("");<BR> m_staticActDocMyLockStatus.SetWindowText("");<BR> return TRUE;<BR>}</P>
<P><BR>LRESULT CMDITestDialog::onAcadActModifiedDoc ( WPARAM, LPARAM bValue )<BR>{<BR> // check/uncheck document activation depending on the activation value sent<BR> if( 1L == bValue )<BR> m_activationCheck.SetCheck( TRUE );<BR> else<BR> m_activationCheck.SetCheck( FALSE );<BR> return TRUE;<BR>}</P>
<P> </P>
<P>/////////////////////////////////////////////////////////////////<BR>// ui related actions</P>
<P>void CMDITestDialog::OnNewdocButton() <BR>{<BR> // document context. <BR> Acad::ErrorStatus st = acDocManager->newDocument();<BR>}</P>
<P><BR>void CMDITestDialog::OnOpendocButton() <BR>{<BR> // document context. <BR> Acad::ErrorStatus st = acDocManager->openDocument();<BR>}</P>
<P><BR>void CMDITestDialog::OnAppnewdocButton() <BR>{<BR> static char pData[] = "acad.dwt";</P>
<P> Acad::ErrorStatus st = acDocManager->appContextNewDocument((const char *)pData);<BR>}</P>
<P><BR>void CMDITestDialog::OnAppopendocButton() <BR>{<BR> static char pData[] = "test1.dwg";</P>
<P> Acad::ErrorStatus st = acDocManager->appContextOpenDocument((const char *)pData);<BR>}</P>
<P>void CMDITestDialog::OnActivationCheck() <BR>{<BR> Acad::ErrorStatus st;<BR> // get status of the button and set the activation state from it.<BR> UINT check = m_activationCheck.GetCheck();</P>
<P> if( (check && 0x0003) == 0 ) {<BR> // unchecked - we need to disable the activation<BR> st = acDocManager->disableDocumentActivation();<BR> }<BR> else {<BR> // checked - we need to enable the activation<BR> st = acDocManager->enableDocumentActivation();<BR> }<BR>}</P>
<P>void CMDITestDialog::OnSendstringButton() <BR>{<BR> if( acDocManager->documentCount() == 0 )<BR> return;</P>
<P> AcApDocument *pDoc = getSelectedDocument();</P>
<P> if(pDoc) {<BR> Acad::ErrorStatus st = acDocManager->sendStringToExecute( pDoc, "_line 0,0 1,1 \n",<BR> false, true);<BR>// Acad::ErrorStatus st = acDocManager->sendStringToExecute( pDoc, "_text 5,5 0.2 15 Testing\r\r");<BR> }<BR> else<BR> AfxMessageBox("Please select a document on the list box");</P>
<P>}</P>
<P><BR>void CMDITestDialog::OnIsquiescentButton() <BR>{<BR> AcApDocument *pDoc = acDocManager->curDocument();</P>
<P> if(pDoc) {<BR> CString tempStr;<BR> tempStr.Format("Current Doc is %s\nlockMode() returned %s\nand myLockMode() returned %s.",<BR> pDoc->isQuiescent() ? "Quiescent." : "NOT Quiescent.",<BR> modeStr(pDoc->lockMode()),<BR> modeStr(pDoc->myLockMode()) );<BR> AfxMessageBox( tempStr );<BR> }<BR>}</P>
<P>void CMDITestDialog::OnClosedocButton() <BR>{<BR> if( acDocManager->documentCount() == 0 )<BR> return;</P>
<P> AcApDocument *pDoc = getSelectedDocument();</P>
<P> if(pDoc) {<BR> // if you close the doc, update the UI<BR> if( acDocManager->closeDocument(pDoc) == Acad::eOk ) {<BR> onAcadUpdateDialog(0, 0L);<BR> } <BR> }<BR> else<BR> AfxMessageBox("Please select a document on the list box");<BR>}</P>
<P>void CMDITestDialog::OnActivatedocButton() <BR>{<BR> if( acDocManager->documentCount() == 0 )<BR> return;</P>
<P> AcApDocument *pDoc = getSelectedDocument();<BR> if( pDoc )<BR> acDocManager->activateDocument( pDoc );<BR> else<BR> AfxMessageBox("Please select a document on the list box");<BR>}</P>
<P><BR>BOOL CMDITestDialog::OnInitDialog() <BR>{<BR> CDialog::OnInitDialog();</P>
<P> // select one of the elements of the CComboBox to be displayed <BR> m_lockCombo.SetCurSel(2);</P>
<P><BR> // set up all the UI elements<BR> // here I call the functions that update the ui elements directly<BR> // instead of waiting for posted messages.<BR> onAcadUpdateDialog(0, 0L);</P>
<P> return TRUE; // return TRUE unless you set the focus to a control<BR> // EXCEPTION: OCX Property Pages should return FALSE<BR>}</P>
<P><BR>void CMDITestDialog::OnSetcurrentdocButton() <BR>{<BR> AcApDocument *pDoc = getSelectedDocument();<BR> if( !pDoc ) {<BR> AfxMessageBox("Please select a document on the list box");<BR> return;<BR> }</P>
<P> // get the lock mode to be used from the combo box<BR> AcAp::DocLockMode lockMode = getSelectedLockMode();</P>
<P> acDocManager->unlockDocument( pDoc ); // in case we had lock it before<BR> acDocManager->setCurDocument( pDoc, lockMode );<BR>}</P>
<P>/////////////////////////////////////////////////////////////////////////////<BR>// other MDITestDialog member functions<BR>/////////////////////////////////////////////////////////////////////////////<BR>void CMDITestDialog::cleanUpUI( void ) <BR>{<BR> m_staticActDocMyLockStatus.SetWindowText("");<BR> m_staticCurrentDoc.SetWindowText("");<BR> m_staticCurDocLockStatus.SetWindowText("");<BR> m_staticCurDocMyLockStatus.SetWindowText("");<BR> m_staticActDocLockStatus.SetWindowText("");</P>
<P> m_staticActiveDoc.SetWindowText("");<BR> m_staticToBeCurrDoc.SetWindowText("");<BR> m_activationCheck.SetCheck( acDocManager->isDocumentActivationEnabled() );<BR> m_staticDocNum.SetWindowText( "0" );<BR> m_docListBox.ResetContent();<BR>}</P>
<P>CString CMDITestDialog::getDocCount( void )<BR>{<BR> // get the current number of documents<BR> int nDoc = acDocManager->documentCount();<BR> CString tempStr;<BR> tempStr.Format("%d",nDoc);<BR> return tempStr;<BR>}</P>
<P>CString CMDITestDialog::makeStringFromPtr( LONG pCh )<BR>{<BR> CString tempStr;</P>
<P> char *pChar = (char*) pCh;<BR> if( pChar ) {<BR> tempStr.Format("%s",pChar);<BR> if( *pChar )<BR> delete pChar;<BR> }<BR> else<BR> tempStr = "";</P>
<P> return tempStr;<BR>}</P>
<P>// getDocFromFilename()<BR>// Passed a filename, it finds the corresponding document pointer<BR>// Returns true if successful<BR>bool CMDITestDialog::getDocFromFilename(CString csFileName, AcApDocument* &pNewDocument)<BR>{<BR> // Iterate over the open documents. We will match the filename if:<BR> // The filename specified matches the fully qualified path<BR> // name, as returned by AcApDocument::filename()<BR> // -or-<BR> // The filename specified matches the filename portion of the <BR> // document name</P>
<P> AcApDocumentIterator* iter = acDocManager->newAcApDocumentIterator();<BR> AcApDocument* pThisDocument = NULL;<BR> CString csThisFilename;<BR> CString csThisFilenameShort;</P>
<P> csFileName.MakeUpper(); // uppercase comparisons</P>
<P> while(!iter->done()) { // Tiptoe through the tulips<BR> pThisDocument = iter->document();<BR> csThisFilename = pThisDocument->docTitle();<BR> csThisFilename.MakeUpper();<BR> csThisFilenameShort = csThisFilename.Right(csThisFilename.GetLength() -<BR> csThisFilename.ReverseFind('\\') - 1);</P>
<P> if(csFileName == csThisFilename || // Full path match<BR> csFileName == csThisFilenameShort || // Matches filename only<BR> csFileName == csThisFilenameShort.Left( // Filename less extension<BR> csThisFilenameShort.GetLength() - 4)) <BR> {<BR> pNewDocument = pThisDocument;<BR> if( iter )<BR> delete iter;<BR> return true;<BR> }<BR> iter->step();<BR> }</P>
<P> pNewDocument = NULL;<BR> if( iter )<BR> delete iter;<BR> // no match found <BR> return false;<BR>}<BR> </P>
<P>AcApDocument* CMDITestDialog::getSelectedDocument( void )<BR>{<BR> AcApDocument *pDoc = NULL;</P>
<P> CString fName;<BR> int ndx = m_docListBox.GetCurSel();</P>
<P> if( LB_ERR != ndx ) {<BR> m_docListBox.GetText(ndx, fName);<BR> // find out which document has this file name and get its document pointer.<BR> if( !getDocFromFilename( fName, pDoc ) )<BR> AfxMessageBox("Document NOT found."); <BR> }<BR> return pDoc;<BR>}</P>
<P><BR>AcAp::DocLockMode CMDITestDialog::getSelectedLockMode( void )<BR>{<BR> AcAp::DocLockMode lm = AcAp::kNone;</P>
<P> int ndx = m_lockCombo.GetCurSel();<BR> CString lModeStr;</P>
<P> if( LB_ERR != ndx ) {<BR> m_lockCombo.GetLBText(ndx, lModeStr);<BR> // to activate the document, first I need to find out which document<BR> // has this file name and get its document pointer.<BR> if( !getLockModeFromStr( lModeStr, lm ) )<BR> AfxMessageBox("Error with lock mode combo box"); //should never get here.<BR> }<BR> return lm;</P>
<P>}</P>
<P><BR>void CMDITestDialog::RebuildListBox()<BR>{<BR> m_docListBox.ResetContent(); // start from an empty list box</P>
<P> // at this moment, get all drawing names and add them to the list box.<BR> AcApDocumentIterator* iter = acDocManager->newAcApDocumentIterator();<BR> AcApDocument* pDoc = NULL;<BR> CString csFilename;</P>
<P> while(!iter->done()) { <BR> pDoc = iter->document();<BR> if(pDoc) { // #### make sure pDoc is not NULL ####<BR> csFilename = pDoc->docTitle(); <BR> m_docListBox.AddString( csFilename );<BR> }<BR> iter->step();<BR> }<BR> if( iter )<BR> delete iter;<BR>}</P>
<P>void CMDITestDialog::OnRefreshButton() <BR>{<BR> // Just to give peace of mind: if you think something is displayed wrong<BR> // in the dialog, this button will update all ui of the dialog<BR> onAcadUpdateDialog(0,0L);<BR>}</P>
<P> </P>
页:
[1]