Author Topic: Evaluating LISP expressions (scripts?) in VBA  (Read 3029 times)

0 Members and 1 Guest are viewing this topic.

sparky

  • Guest
Evaluating LISP expressions (scripts?) in VBA
« on: September 12, 2008, 03:25:05 PM »
(vlax-curve-getClosestPointTo curve-obj givenPnt [extend]) 

Anyone know how I can use that Lisp function in Vba. Or does VBA have an equivalent method??

Spike Wilbury

  • Guest
Re: Evaluating LISP expressions (scripts?) in VBA
« Reply #1 on: September 12, 2008, 03:30:27 PM »

sparky

  • Guest
Re: Evaluating LISP expressions (scripts?) in VBA
« Reply #2 on: September 12, 2008, 03:44:48 PM »
Right...Brilliant.  What I know of Class librarys is dangerous, but I guess they hold lots of Vlax and curve functions which are open to the public.  Am I right so far?  How do I use them? (Sorry!)  They are class modules?

Got to go for a cigarette then I'll read up on them.  Thanks for the link.

sparky

  • Guest
Re: Evaluating LISP expressions (scripts?) in VBA
« Reply #3 on: September 12, 2008, 04:00:06 PM »
Not quite sure how to use the code but i'll have a lash at it. But I can see the code, functions and all.

sparky

  • Guest
Re: Evaluating LISP expressions (scripts?) in VBA
« Reply #4 on: September 12, 2008, 05:22:29 PM »
This is part of the header....

' VLAX.cls allows developers to evaluate AutoLISP expressions from
' Visual Basic or VBA
'
' Notes:
' All code for this class module is publicly available througout various posts
' at news://discussion.autodesk.com/autodesk.autocad.customization.vba. I do not
' claim copyright or authorship on code presented in these posts, only on this
' compilation of that code. In addition, a great big "Thank you!" to Cyrille Fauvel
' demonstrating the use of the VisualLISP ActiveX Module.
'
' Dependencies:
' Use of this class module requires the following application:
' 1. VisualLISP


Note the Bold italics bit at the end.  Question: Does Autocad have this VisualLISP application installed.  I've written some code and I Instantiate a variable of the VLAX Class in the This Drawing Module...

                Dim VlaxClass As VLAX
                Dim ClosestPt As Variant
                Dim Idunno As Variant 'I dunno what it'll be
                               
                VlaxClass = New VLAX
                '(vlax-curve-getClosestPointTo curve-obj givenPnt [extend])
                VlaxClass.SetLispSymbol "Curveobj", AcadSSet2.Item(0)
                VlaxClass.SetLispSymbol "givenPnt", InsPoint
                VlaxClass.EvalLispExpression "(setq ClosestPt (vlax-curve-getClosestPointTo curve-obj givenPnt)"
                Set ClosestPt = VlaxClass.GetLispSymbol("ClosestPt") 'not sure yet what i'll get
                Set Idunno = VlaxClass.GetLispList("ClosestPt")           'not sure either!

I step thru the code and at the "VlaxClass = New VLAX" I  switch to the Class module Initialise procedure and it fails at this...

Private VL As Object
Private VLF As Object

Private Sub Class_Initialize()

    Set VL = ThisDrawing.Application.GetInterfaceObject("VL.Application.1")       <-------Here!!!
    Set VLF = VL.ActiveDocument.Functions

End Sub

Where can I find this Application?  I tried opening the VLisp API but that didn't help. Still broke down there.

Any suggestions?


sparky

  • Guest
Re: Evaluating LISP expressions (scripts?) in VBA
« Reply #5 on: September 12, 2008, 05:38:30 PM »
Actually I just searched the Autocad 2008 Directory and found an ARX application called vl.arx.  Any idea what prog-ID it has for this version of Autocad?

sparky

  • Guest
Re: Evaluating LISP expressions (scripts?) in VBA
« Reply #6 on: September 12, 2008, 06:51:58 PM »
Managed to get the Vl application to be opened.

On continuing through the code I get this error...Runtime Error  94 Object variable or With block variable not set.

Can someone help me?
 

Bryco

  • Water Moccasin
  • Posts: 1850
Re: Evaluating LISP expressions (scripts?) in VBA
« Reply #7 on: September 13, 2008, 02:18:25 PM »
There's a fair bit of stuff available here when you search for vlax
I use below to get the correct vl number
Private Sub Class_Initialize()
    If AcadVer = 2000 Then
        Set VL = ThisDrawing.Application.GetInterfaceObject("VL.Application.1")
    Else
        Set VL = ThisDrawing.Application.GetInterfaceObject("VL.Application.16")
    End If
    Set VLF = VL.ActiveDocument.Functions

End Sub

MP

  • Seagull
  • Posts: 17488
Re: Evaluating LISP expressions (scripts?) in VBA
« Reply #8 on: September 13, 2008, 03:23:57 PM »
This is part of the header ...
You should include Frank Oquendo's name with that code. Despite Frank's dismissal of his contribution to that code, he assembled it an made it widely available to the masses via his acadx.com (no defunct I think) website. Earlier versions may have also been attributable to Tony Tanzillo too if (my failing) memory serves me. I'd google search, find the code directly related to the code you posted and note the attribution accordingly.
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

Jeff_M

  • King Gator
  • Posts: 3946
  • C3D user & customizer
Re: Evaluating LISP expressions (scripts?) in VBA
« Reply #9 on: September 13, 2008, 07:17:03 PM »
Sparky,
In addition to what Bryco noted and MP suggests, here's an example of a few of the functions. Note that you do NOT need to do all of the hoop jumping when you use the CURVE.CLS.

In a Module:
Code: [Select]
'functions to find the midpoint, perpendicular point, and selected segment of a pline _
 the mid & perp will work on ANY linear object (line, arc, spline, polyline) _
 Authors are Jeff Mishler and Frank Oquendo, as noted
Option Explicit
Dim obj As Curve

'by Jeff Mishler
Function mid_Of(oEnt As AcadEntity) As Variant
Dim length As Double
Dim midPt As Variant

Set obj = New Curve
Set obj.Entity = oEnt

length = obj.length
midPt = obj.GetPointAtDistance(length / 2)

mid_Of = midPt
End Function

'by Jeff Mishler, based on the assumption (which is USUALLY correct) that the
'perpendicular point is the point on the object closest to the known point
Public Function PerpendicularTo(oEnt As AcadEntity, dPoint As Variant) As Variant
Dim perpPoint As Variant

Set obj = New Curve
Set obj.Entity = oEnt

perpPoint = obj.GetClosestPointTo(dPoint, True)
PerpendicularTo = perpPoint
       
End Function
'More code from Frank Oquendo
Public Function SelectedSegment(ent As AcadEntity, pt) As Integer

    Dim dist As Double, i As Integer
    Dim max As Integer, segment As Integer

    Set obj = New Curve
    Set obj.Entity = ent
    dist = obj.GetDistanceAtPoint(obj.GetClosestPointTo(pt))
    max = (UBound(ent.Coordinates) + 1) / 2

    For i = 0 To max - 1
        If obj.GetDistanceAtPoint(ent.Coordinate(i)) > dist Then Exit For
        segment = segment + 1
    Next

    SelectedSegment = segment

End Function
'This is just a sample of how to use the mid & perp functions
Sub mid_perp_Sample()
Dim oEnt1 As AcadEntity
Dim oEnt2 As AcadEntity
Dim pick As Variant
Dim point1 As Variant
Dim point2 As Variant

ThisDrawing.Utility.GetEntity oEnt1, pick, vbCrLf & "Select entity to calc mid point of: "
ThisDrawing.Utility.GetEntity oEnt2, pick, vbCrLf & "Select entity to go perpendicular to: "
point1 = mid_Of(oEnt1)
point2 = PerpendicularTo(oEnt2, point1)

ThisDrawing.ModelSpace.AddLine point1, point2

End Sub
HTH

sparky

  • Guest
Re: Evaluating LISP expressions (scripts?) in VBA
« Reply #10 on: September 14, 2008, 04:38:10 AM »
This is greaat! I'm a seasoned VLisp writer but have decided to learn VBA. It is my ambitioun to step from there up to VB.Net.  Yes, I know... a long term strategy. I've written a few simple scripts but reading the Object Model noticed no Curve/Vlax methods.  All your answers are very gracious and I appreciate them. 

I've still not realised what the 94 Error code is...but maybe I need to use the curve class...Need to find time to sit down and work through it.  If I have that sinking feeling that I'm just not getting it I'd appreciate if I could come back to you all. 

I'll search here for vlax...I wonder if you can get the AciveX Object back from the VL App (Variants, Polys etc.  Hmmmm)

Maybe...You lot are 'Meister' (German for Masters!).



Jeff_M

  • King Gator
  • Posts: 3946
  • C3D user & customizer
Re: Evaluating LISP expressions (scripts?) in VBA
« Reply #11 on: September 14, 2008, 12:09:31 PM »
The 94 Error code is coming from using the Set statement with Variants. Set is only used with Objects.

If you don't want to use the CURVE.CLS, at least study how it works and it will make your code easier to create.