VB icon

Insert Template in AutoCAD

Email
Submitted on: 1/30/2015 11:03:00 AM
By: Troy Gates (from psc cd)  
Level: Beginner
User Rating: By 2 Users
Compatibility: VB 6.0
Views: 1476
 
     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.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Insert Template in AutoCAD
' Description: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.
' By: Troy Gates (from psc cd)
'
' 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


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.