Results 1 to 3 of 3

Thread: Calling AutoLisp functions from AutoCad VBA

  1. #1
    Login to Give a bone
    0

    Default Calling AutoLisp functions from AutoCad VBA

    VBA does not provide a function to get the handle of the object that was just added to the drawing. AutoLisp has such a function entget (entLast) I need to understand the steps required to connect VBA to a Lisp function. Here is the code (I Hope) The Lisp function is edited and saved as Application.1.lsp and loaded with the Lisp Editor. The VBA is loaded with the VBA editor. When i run the Testme public function it generates an error as "Problem in loading Application" What am I missing. As always thanks in advance.

    AutoLisp Function
    Code:
    ( defun *entlast* ()
    (cdr (assoc 5 (entget (entlast))))
    )
    Vba Function
    Code:
    Public Function entLast() As AcadEntity
    Dim vl As Object
    Dim vlFun As Object
    Dim Result As String
    
    Set vl = ThisDrawing.Application.GetInterfaceObject("VL.Application.1")
    Set vlFun = vl.ActiveDocument.Functions
    Result = vlFun.Item("*entlast*")
    Set entLast = ThisDrawing.HandleToObject(Result)
    
    End Function
    
    Vba Test Code
    Public Sub testme()
    Dim ent As AcadEntity
    Set ent = entLast
    Debug.Print ent.ObjectName
    End Sub
    Last edited by Ed Jobe; 2025-03-01 at 04:24 AM.

  2. #2
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,577
    Login to Give a bone
    0

    Default Re: Calling AutoLisp functions from AutoCad VBA

    Welcome to AUGI. Please post your code using CODE tags. To to this, click on the Go Advanced button when you are posting. Then highlight your code and click on the Hashtag # button. This willl surround your code with tags.

    It is entirely possible to get the Handle and Object ID's of a new entity using VBA. In the following sub, the ModelSpace.AddXXX functions return the object just added. Don't just add the entity, but use it to set a variable. Here I've added an MLeader object and at the end I print out the handle and objectName
    Code:
    Sub AddMLeader(ptcenter As Variant, txtpoint As Variant, Noofdigit As Integer) 'place coordinate with leader
        Dim oML As AcadMLeader
        Dim pts(0 To 5) As Double
        Dim xText As String
        Dim yText As String
        Dim txtCoord As String
        
        xText = digit(ptcenter(0), Noofdigit)
        yText = digit(ptcenter(1), Noofdigit)
        txtCoord = xText & " E" & vbCrLf & yText & " N"
        ' Define the leader points
        pts(0) = ptcenter(0): pts(1) = ptcenter(1): pts(2) = ptcenter(2)
        pts(3) = txtpoint(0): pts(4) = txtpoint(1): pts(5) = txtpoint(2)
        
        Set oML = ThisDrawing.ModelSpace.AddMLeader(pts, 0)
        
        oML.TextString = txtCoord 'show coordinate
        oML.TextLeftAttachmentType = acAttachmentMiddle
        oML.TextRightAttachmentType = acAttachmentMiddle
        
        If ptcenter(0) > txtpoint(0) Then
        
            oML.TextJustify = acAttachmentPointMiddleRight
            
            'oML.TextDirection = acRightToLeft
        
        
        Else
            oML.TextJustify = acAttachmentPointMiddleLeft
        
        End If
        
        oML.Update
        Debug.Print oML.ObjectID
        Debug.Print oML.ObjectName
    End Sub
    Last edited by Ed Jobe; 2025-03-03 at 05:25 PM.
    C:> ED WORKING....


    LinkedIn

  3. #3
    Login to Give a bone
    0

    Thumbs up Re: Calling AutoLisp functions from AutoCad VBA

    Ed, Thanks. That worked great. Added the function "digit" to work as noted below. And thanks for the Annotation example. That was next on my list of todos.
    Code:
    Private Function Digit(ByVal pnt As Double, ByVal Noofdigit As Long) As String
        Digit = Left(CStr(pnt), InStr(1, CStr(pnt), ".", vbTextCompare) + Noofdigit)
    End Function

Similar Threads

  1. Calling AutoLISP function from a field?
    By whackoprogrammer760234 in forum AutoLISP
    Replies: 3
    Last Post: 2017-12-04, 12:13 PM
  2. Replies: 0
    Last Post: 2012-11-24, 07:38 PM
  3. Calling API functions from VLISP
    By rajat_bapi_mallick in forum AutoLISP
    Replies: 2
    Last Post: 2006-05-01, 02:24 AM
  4. AutoLISP dictionary functions in LT
    By jrd.chapman in forum AutoLISP
    Replies: 4
    Last Post: 2005-03-29, 07:46 PM
  5. Autolisp functions traceability
    By acad.user in forum AutoLISP
    Replies: 11
    Last Post: 2004-12-08, 10:50 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •