Make Block with ActiveX

code in VBA and Visual Lisp for comparison,
connect, establish application and document objects, draw line in modelspace, add a new block, draw line in the block, insert the block.

Public acadApp As AcadApplication
Public acadDoc As AcadDocument
Public mspace As AcadModelSpace

Sub demo()

'autocad is running and doc open
Set acadApp = GetObject(, "AutoCAD.Application")
Set acadDoc = acadApp.ActiveDocument
Set mspace = acadDoc.modelspace

Dim lineobj As AcadLine
Dim pt1(0 To 2) As Double, pt2(0 To 2) As Double
pt1(0) = 2: pt1(1) = 1: pt1(2) = 0
pt2(0) = 6: pt2(1) = 7: pt2(2) = 0

'Set lineobj = acadDoc.modelspace.AddLine(pt1, pt2)
Set lineobj = mspace.AddLine(pt1, pt2)

Dim pt3(0 To 2) As Double
pt3(0) = 0: pt3(1) = 0: pt3(2) = 0

Dim blks As AcadBlocks
Set blks = acadDoc.blocks

Dim blkdef As AcadBlock
Set blkdef = blks.Add(pt3, "Blk_Demo2")

Set lineobj = blkdef.AddLine(pt1, pt2)
Set lineobj = blkdef.AddLine(pt2, pt3)

Dim blkref As AcadBlockReference
Set blkref = mspace.InsertBlock(pt3, "Blk_Demo2", 1, 1, 1, 0)

End Sub

   (setq acadApp (vlax-get-acad-object))

   (setq acadDoc (vla-get-ActiveDocument acadApp))

   (setq mSpace (vla-get-ModelSpace acadDoc))

   (setq pt1 (vlax-3d-point 1 1 0)
         pt2 (vlax-3d-point 7 5 0))
    (setq lineObj (vla-AddLine mSpace pt1 pt2))

    (setq pt3 (vlax-3d-point 0 0 0))

    (setq blks (vla-get-blocks acadDoc))

    (setq blkdef (vla-Add blks pt3 "Blk_Demo1"))
    (setq lineobj (vla-AddLine blkdef pt1 pt2))
    (setq lineobj (vla-AddLine blkdef pt2 pt3))

    (setq blkref (vla-insertblock mspace pt3 "Blk_Demo1" 1 1 1 0 ))

side by side,

this is an intuitively different process than creating a block by manual drafting methods. the graphic methods such as addline are the same for drawing in modelspace and drawing in blockspace. modelspace is a type of block, autodesk tells us. we create a block with nothing in it, then use the object returned just as we obtain the modelspace object to put objects in modelspace.

this example is very similar to the autodesk activeX Insertblock help page.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.