Author Topic: Getting a variable in VBA from ACAD  (Read 12954 times)

0 Members and 1 Guest are viewing this topic.

ML

  • Guest
Getting a variable in VBA from ACAD
« on: December 14, 2007, 02:48:24 PM »
Hi,

I am trying something here; in theory this should work but I think that I am doing something wrong

Using LISP (in a menu macro) I set my variable

Code: [Select]
(setq imgname (getfiled "Select Image File" (getvar "dwgprefix") "tif" 8))

Now, in VBA I want to use it.
Is this possible?  If so, how would I write the syntax?
Here was my attempt

Code: [Select]
Imgname = ThisDrawing.GetVariable("!Imgname")

In ACAD, if I simply do !Imgname at the command line, it is returning the proper file name that I assigned to the variable

but with VBA no luck


Thanks!

Mark


deegeecees

  • Guest
Re: Getting a variable in VBA from ACAD
« Reply #1 on: December 14, 2007, 02:53:51 PM »

Guest

  • Guest
Re: Getting a variable in VBA from ACAD
« Reply #2 on: December 14, 2007, 02:55:36 PM »
This should also work.

Code: [Select]
(setq USERS1 (getfiled "Select Image File" (getvar "dwgprefix") "tif" 8))

Code: [Select]
Imgname = ThisDrawing.GetVariable("USERS1")

ML

  • Guest
Re: Getting a variable in VBA from ACAD
« Reply #3 on: December 14, 2007, 04:15:41 PM »

Hey Matt

Been there done that; it didn't work

It is not passing the variable from ACAD to VBA

Mark

ML

  • Guest
Re: Getting a variable in VBA from ACAD
« Reply #4 on: December 14, 2007, 04:33:20 PM »

Dec..

That is cool but I am getting an user defined type not defined.

I tried setting a reference to The Visual Lisp Active X Module but that didn't help

What am I missing?

Mark

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Getting a variable in VBA from ACAD
« Reply #5 on: December 14, 2007, 04:40:52 PM »
What version of AutoCAD?

Hint ... vl.application.## ...

/guess
Engineering Technologist CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com http://cadanalyst.slack.com http://linkedin.com/in/cadanalyst

ML

  • Guest
Re: Getting a variable in VBA from ACAD
« Reply #6 on: December 14, 2007, 04:43:22 PM »

MP

R16.2

So, you are saying:

Dim vlax As New vlax

Set New vlax to vl.application. 16.2    ?

Mark

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Getting a variable in VBA from ACAD
« Reply #7 on: December 14, 2007, 05:00:43 PM »
Not quite.

In the vlax.cls definition (which I don't have) there's a reference to vl.application. Ensure it references "vl.application.16".

PS: May not be the problem, I'm shooting blind from your description.
Engineering Technologist CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com http://cadanalyst.slack.com http://linkedin.com/in/cadanalyst

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: Getting a variable in VBA from ACAD
« Reply #8 on: December 14, 2007, 06:05:23 PM »
Now, in VBA I want to use it.
Is this possible? 

No.

The AutoLISP interpreter inside AutoCAD and VBA were never designed to communicate. In later versions of AutoLISP (read VisualLISP) you can access ActiveX components on the system, but you still can't pass variables in between the two environments (easily). You can do what 'Matt W' suggested and 'park' you resultant variable(s) in the 'USERX' system envornment varaible(s) inside AutoCAD, but they can't house complex types.

You could go down the ugly (IMO) VLAX Class route, which was developed a few years ago, by hooking into the AutoCAD VisualLisp ActiveX process and automating it, but I would seriously suggest against it.

Me

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Getting a variable in VBA from ACAD
« Reply #9 on: December 14, 2007, 06:28:49 PM »
You could go down the ugly (IMO) VLAX Class route, which was developed a few years ago, by hooking into the AutoCAD VisualLisp ActiveX process and automating it, but I would seriously suggest against it.

Have to agree. I'd revisit the solution -- what is it that is "mandating" the vb <-> lisp exchange?
Engineering Technologist CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com http://cadanalyst.slack.com http://linkedin.com/in/cadanalyst

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: Getting a variable in VBA from ACAD
« Reply #10 on: December 14, 2007, 06:59:00 PM »
What MP said++
Me

ML

  • Guest
Re: Getting a variable in VBA from ACAD
« Reply #11 on: December 14, 2007, 07:37:52 PM »

Hey guys

Already there;

I got the whole long VLAX Class Module

I don't like it too much; it seems like a lot just to pass a lousy variable to VBA.

With The VLAX Class Module inserted into your project, you can do this:
Code: [Select]
Sub VBAToLisp()

Dim Obj As VLAX
Dim strText As String

Set Obj = New VLAX

strText = "Hello!"

Obj.SetLispSymbol "Test", strText

Debug.Print strText

then in ACAD, type !Test

It should return Hello!

If so, then the variable Test was just passed from VBA to ACAD or LISP which is very cool

---------------

On the flip side; I am trying to read in a variable but no luck so far
Here is what I have:

Code: [Select]
Dim Obj As VLAX
Dim strText As String

strText = "M"   'M was set to Mark in ACAD (setq M "Mark")

Set Obj = New VLAX

Obj.GetLispSymbol , strText 
Debug.Print strText

Have a look at this:

I am actually more interested in getting this method to work but still no luck.

Code: [Select]
Sub VBAToLisp()

Dim VL As Object
Dim strText As String

Set VL = CreateObject("VL.Application.16")

strText = "Hello!"

VL.SetLispSymbol "Test", strText
Debug.Print strText


End Sub

I do not have the methods of The VL.Application Object, so I don't even know if The GetLispSymbol and SetLispSymbol methods are even available in VLISP. The error is saying that object doesn't support this method, so I guess that is my answer.

Anyone? Any Ideas?

Thanks
Mark


ML

  • Guest
Re: Getting a variable in VBA from ACAD
« Reply #12 on: December 14, 2007, 07:41:05 PM »

Also I believe you will need to set a reference to The Visual LISP Active X Module Library.
Then you will have access to The VLISP objects

Mark

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
Re: Getting a variable in VBA from ACAD
« Reply #13 on: December 14, 2007, 08:44:53 PM »
I would try to stick with just VBA if it were me.  You could declare a Global variable, and store info there.  From what I read so far, why couldn't you do the whole thing in VBA?  Prompt for select object, filtering for images, and pull the info from the selection set.  Or are you just experimenting?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second

SomeCallMeDave

  • Guest
Re: Getting a variable in VBA from ACAD
« Reply #14 on: December 14, 2007, 09:28:30 PM »
Here is one that I cobbled together several years ago using the VLAX as a guide.

You will have to adjust the .GetInterfaceObject to suit your version of ACAD (this one works in 2004)

Code: [Select]
Public Function vbGetLispList(pVarName As String) As Variant

Dim VLisp As Object
Dim VLispFunc As Object
Dim varTemp As Variant
Dim varRetVal As Variant
Dim varType As Variant

Dim obj1 As Object
Dim obj2 As Object

Dim strHnd As String
Dim strVer As String

Dim lngCount As Long
Dim i As Long
Dim j As Long

On Error GoTo vbGetLispVarError

If Me.Application.Version = "16.0" Then
  Set VLisp = ThisDrawing.Application.GetInterfaceObject("VL.Application.16")
Else
  Set VLisp = ThisDrawing.Application.GetInterfaceObject("VL.Application.1")
End If

Set VLispFunc = VLisp.ActiveDocument.Functions
Set obj1 = VLispFunc.Item("read").funcall("pSCMDtemp")
  varRetVal = VLispFunc.Item("set").funcall(obj1, pVarName)
Set obj1 = VLispFunc.Item("read").funcall("(type (eval (read pSCMDtemp)))")
Set obj2 = VLispFunc.Item("eval").funcall(obj1)
varType = VLispFunc.Item("vl-princ-to-string").funcall(obj2)

Select Case varType
  Case "LIST"
    Set obj1 = VLispFunc.Item("read").funcall(pVarName)
    Set obj2 = VLispFunc.Item("eval").funcall(obj1)
    varRetVal = VLispFunc.Item("vl-princ-to-string").funcall(obj2)
  Case "SYM", "INT", "STR", "REAL"
    Set obj1 = VLispFunc.Item("read").funcall(pVarName)
    varRetVal = VLispFunc.Item("eval").funcall(obj1)
  Case Else
    varRetVal = "nil"
End Select

vbGetLispList = varRetVal

'clean up the newly created LISP symbols
Set obj1 = VLispFunc.Item("read").funcall("(setq pSCMDtemp nil)")
  varRetVal = VLispFunc.Item("eval").funcall(obj1)


 
'release the objects or Autocad gets squirrely
Set obj2 = Nothing
Set obj1 = Nothing
Set VLispFunc = Nothing
Set VLisp = Nothing

Exit Function

vbGetLispVarError:
  Set obj2 = Nothing
  Set obj1 = Nothing
  Set VLispFunc = Nothing
  Set VLisp = Nothing
  Debug.Print Err.Number
  'MsgBox "Error occurred " & Err.Description

End Function

This is a simple test function that does what I think you are trying to do

Code: [Select]
Public Sub test2()
  Dim varTest As Variant
     

  varTest = vbGetLispList("temp2")
  MsgBox varTest
 
End Sub

At the ACAD command line, set temp2 with (setq temp2 (''string of your choice"))

Maybe this will help.