About Referencing Objects in the Object Hierarchy (ActiveX)

You can reference objects directly or through a user-defined variable. To reference the objects directly, include the object in the calling hierarchy.

For example, the following statement adds a line in model space. Notice that the hierarchy starts with ThisDrawing, goes to the ModelSpace object, and then calls the AddLine method:

AutoLISP
(setq acadObj (vlax-get-acad-object)
      doc (vla-get-ActiveDocument acadObj))
    
(setq startPoint (vlax-3d-point 0 0 0)
      endPoint (vlax-3d-point 30 20 0))
    
(setq lineObj (vla-AddLine (vla-get-ModelSpace doc) startPoint endPoint))
VBA (AutoCAD Only)
Dim startPoint(0 To 2) As Double, endPoint(0 To 2) As Double
Dim LineObj as AcadLine
startPoint(0) = 0: startPoint(1) = 0: startPoint(2) = 0
endPoint(0) = 30: endPoint(1) = 20: endPoint(2) = 0
Set LineObj = ThisDrawing.ModelSpace.AddLine(startPoint,endPoint)

To reference the objects through a user-defined variable, define the variable as the desired type, then set the variable to the appropriate object. For example, the following code defines a variable (moSpace) of type AcadModelSpace and sets the variable equal to the current model space:

AutoLISP
(setq acadObj (vlax-get-acad-object)
      doc (vla-get-ActiveDocument acadObj)
      moSpace (vla-get-ModelSpace doc))
VBA (AutoCAD Only)
Dim moSpace As AcadModelSpace
Set moSpace = ThisDrawing.ModelSpace

The following statement then adds a line to the model space using the user-defined variable:

AutoLISP
(setq startPoint (vlax-3d-point 0 0 0)
      endPoint (vlax-3d-point 30 20 0))

(setq lineObj (vla-AddLine moSpace startPoint endPoint))
VBA (AutoCAD Only)
Dim startPoint(0 To 2) As Double, endPoint(0 To 2) As Double
Dim LineObj as AcadLine
startPoint(0) = 0: startPoint(1) = 0: startPoint(2) = 0
endPoint(0) = 30: endPoint(1) = 20: endPoint(2) = 0
Set LineObj = moSpace.AddLine(startPoint,endPoint)

Retrieving the first entity in model space

The following example returns the first entity object in model space. Similar code can do the same for paper space entities. Note that all drawing objects can be defined as AcadEntity objects:

AutoLISP
(vl-load-com)
(defun c:Ch2_FindFirstEntity()
    (setq acadObj (vlax-get-acad-object)
          doc (vla-get-ActiveDocument acadObj)
          moSpace (vla-get-ModelSpace doc))
  
    (if (> (vla-get-Count moSpace) 0)
        (progn
            (setq entity (vla-Item moSpace 0))
            (alert (strcat (vla-get-ObjectName entity)
                           " is the first entity in model space."
                   ))
        )
        (alert "There are no objects in model space.")
    )
)
VBA (AutoCAD Only)
Sub Ch2_FindFirstEntity()
    ' This example returns the first entity in model space
    On Error Resume Next

    Dim entity As AcadEntity
    If ThisDrawing.ModelSpace.count <> 0 Then
        Set entity = ThisDrawing.ModelSpace.Item(0)
        MsgBox entity.ObjectName + _
               " is the first entity in model space."
    Else
        MsgBox "There are no objects in model space."
    End If
End Sub