by Troy Gates (1 Submission)
Category: Microsoft Office Apps/VBA
Compatability: Visual Basic 5.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (2 Votes)
SrA John Spence asked for some examples for VBA with AutoCAD, so here is the first.
This is an example for creating a new drawing in AutoCAD and inserting a template. This is coded for VBA but is easily ported to VB 6. I can show that also if someone wants it.
Assumes
Must have AutoCAD
This needs to be in its own module or form and not ThisDrawing. Easiest is to add a new form and a button and put it in the button click event.
Dim acadApp As AcadApplication 'reference to the AutoCAD application
Dim acadDocs As AcadDocuments 'reference to the AutoCAD Documents collection
Dim acadDoc As AcadDocument 'reference to a Document in the Collection
Dim acadBlock As AcadBlockReference 'Block reference
Dim strTemplate As String 'path to template file
Dim dblInsertPt(2) As Double 'array with insert points (X,Y,Z)
strTemplate = "S:\D+Acad\D+Templates\APF-Floor Plan.dwt" 'change to the path of the template you want
Set acadApp = ThisDrawing.Application 'connect to AutoCAD application
Set acadDocs = acadApp.Documents 'get the Documents collection
Set acadDoc = acadDocs.Add 'create an empty document
'set the inseration points to 0,0,0
dblInsertPt(0) = 0# 'X
dblInsertPt(1) = 0# 'Y
dblInsertPt(2) = 0# 'Z
'Insert the template with no XYZ scale and no rotation
Set acadBlock = acadDoc.ModelSpace.InsertBlock(dblInsertPt, strTemplate, 1, 1, 1, 0)
acadBlock.Explode 'explode the template
acadApp.ZoomExtents 'zoom to the extents
'clear objects from memory
Set acadBlock = Nothing
Set acadDoc = Nothing
Set acadDocs = Nothing
Set acadApp = Nothing