The Documents collection and Document object provide access to the AutoCAD file functions.
To create a new drawing, or open an existing drawing, use the methods on the Documents collection. The Add method creates a new drawing and adds that drawing to the Documents collection. The Open method opens an existing drawing. There is also a Close method on the Documents collection that closes all the drawings open in the AutoCAD session.
Use either the Save or SaveAs method to save a drawing. Occasionally you will want to check if the active drawing has any unsaved changes. It is a good idea to do this before you quit the AutoCAD session or start a new drawing. Use the Saved property to make sure that the current drawing does not contain any unsaved changes.
To import and export drawings, use the Import and Export methods on the Document object.
The Open method can be used to open an existing drawing. You can check for the existence of the file before trying to open it with Dir function in VBA and the findfile function in AutoLISP.
(vl-load-com) (defun c:Ch3_OpenDrawing ( / ) (setq acadObj (vlax-get-acad-object) docs (vla-get-Documents acadObj)) (setq dwgName "c:\\Autodesk\\campus.dwg") (if (findfile dwgName) (vla-Open docs dwgName) (alert (strcat "File " dwgName " does not exist.")) ) )
Sub Ch3_OpenDrawing() Dim dwgName As String dwgName = "c:\campus.dwg" If Dir(dwgName) <> "" Then ThisDrawing.Application.Documents.Open dwgName Else MsgBox "File " & dwgName & " does not exist." End If End Sub
This example uses the Add method to create a new drawing based on the default template.
(vl-load-com) (defun c:Ch3_NewDrawing ( / ) (setq acadObj (vlax-get-acad-object) docs (vla-get-Documents acadObj)) (setq docObj (vla-Add docs)) )
Sub Ch3_NewDrawing() Dim docObj As AcadDocument Set docObj = ThisDrawing.Application.Documents.Add End Sub
This example saves the active drawing under its current name and again under a new name.
(vl-load-com) (defun c:Ch3_SaveActiveDrawing ( / ) (setq acadObj (vlax-get-acad-object) doc (vla-get-ActiveDocument acadObj)) ;; Save the active drawing under the current name (vla-Save doc) ;; Save the active drawing under a new name (vla-SaveAs doc "MyDrawing.dwg") )
Sub Ch3_SaveActiveDrawing() ' Save the active drawing under the current name ThisDrawing.Save ' Save the active drawing under a new name ThisDrawing.SaveAs "MyDrawing.dwg" End Sub
This example checks to see if there are unsaved changes and verifies with the user that it is OK to save the drawing (if it is not OK, skip to the end). If OK, use the Save method to save the current drawing, as shown here.
(vl-load-com) (defun c:Ch3_TestIfSaved ( / ) (setq acadObj (vlax-get-acad-object) doc (vla-get-ActiveDocument acadObj) utilObj (vla-get-Utility doc)) ;; Define and call InitializeUserInput to set up the keywords (setq keywordList "Yes No") (vla-InitializeUserInput utilObj 0 keywordList) (if (= :vlax-false (vla-get-Saved doc)) (if (/= "No" (vla-GetKeyword utilObj "\nDo you wish to save this drawing? [Yes/No] <Yes>: ")) (vla-Save doc) ) ) )
Sub Ch3_TestIfSaved() If Not (ThisDrawing.Saved) Then If MsgBox("Do you wish to save this drawing?", _ vbYesNo) = vbYes Then ThisDrawing.Save End If End If End Sub