21, Feb, 2019

How to insert a Block inside AutoCAD drawing using VBA

autocad vba draw line

Add a command button in to the form and save a block (a cad drawing to be inserted) as Block.dwg in your C: drive

Please refer our previous examples on how to insert Userform and Command button etc

copy below code under command button_click event

Private Sub CommandButton1_Click()
InsertBlock “C:\Block.dwg”, 0 ‘

‘Change the 0 to another value (in degrees) to rotate the block’
End Sub

Add below Function to your module

Function InsertBlock(ByVal blockpath As String, ByVal rotation As Double)
ThisDrawing.Application.Visible = True
Dim blockobj As AcadBlockReference
Dim insertionPnt As Variant
Dim prompt1 As String
rotateAngle = rotation
rotateAngle = rotation * 3.141592 / 180#
‘Prompt is used to show instructions in the command bar
prompt1 = vbCrLf & “Enter block insert point: “
ThisDrawing.ActiveSpace = acModelSpace
insertionPnt = ThisDrawing.Utility.GetPoint(, prompt1)
Set blockRefObj = ThisDrawing.ModelSpace.InsertBlock(insertionPnt, blockpath, 1#, 1#, 1#, rotateAngle)
End Function

you can also define the insertions points in the VBA, instead of waiting the user to click

Dim Startpoint(0 To 2) As Double
‘Start Coordinates Coordinates XYZ
Startpoint(0) = 100: Startpoint(1) = 50: Startpoint(2) = 0

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>