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

0 Members and 1 Guest are viewing this topic.

ML

  • Guest
Re: Getting a variable in VBA from ACAD
« Reply #15 on: December 17, 2007, 10:25:27 AM »

Hi Some!

Is this a function that will work without That VLAX Class module?

Mark

ML

  • Guest
Re: Getting a variable in VBA from ACAD
« Reply #16 on: December 17, 2007, 10:30:41 AM »

CM,

To answer your question;
The post started like this

Quote
This should also work.

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

Imgname = ThisDrawing.GetVariable("USERS1")


Instead of having to use The really long FileOpenDialog in ACAD - VBA just to browse (using Windows) and grab a lousy file; I thought that I would just use the getfiled method in LISP instead. That is when this problem occurred. With LISP, I can set my variable in CAD; however, that variable will not carry over to VBA; hence this.

God, in Excel, THe FileOpenDialog is built into VBA and it is like 4 lines.

Mark

Mark

ML

  • Guest
Re: Getting a variable in VBA from ACAD
« Reply #17 on: December 17, 2007, 10:33:36 AM »

Does anyone think that they could get this method working, without VLAX? I think it is much cleaner.
I have done some research on The Net and it does look like we can effectively set a reference to The VLISP object, the question is, how do we use the VLISP methods in VBA?

Mark

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

SomeCallMeDave

  • Guest
Re: Getting a variable in VBA from ACAD
« Reply #18 on: December 17, 2007, 11:23:59 AM »

Hi Some!

Is this a function that will work without That VLAX Class module?

Mark

Mark,
Yes, it will work without the VLAX class, but will require a reference to be set to the visual LISP activeX module

ML

  • Guest
Re: Getting a variable in VBA from ACAD
« Reply #19 on: December 17, 2007, 12:40:07 PM »

Hey Some

So, the code (setting the reference) that I posted just above you last post; along with your function should work together?

Mark


ML

  • Guest
Re: Getting a variable in VBA from ACAD
« Reply #20 on: December 17, 2007, 12:41:53 PM »

This is really ugly and I am not sure that I would use this approach but can anyone tell me how to get this
Code: [Select]
(setq imgname (getfiled "Select Image File" (getvar "dwgprefix") "tif" 8))

To The ACAD Command Line?

Thisdrawing.SendCommand  (setq imgname (getfiled "Select Image File" (getvar "dwgprefix") "tif" 8))

Thanks
Mark

Fatty

  • Guest
Re: Getting a variable in VBA from ACAD
« Reply #21 on: December 17, 2007, 04:29:53 PM »
Try this one

Code: [Select]
Sub Try()
Dim fname As String
ThisDrawing.SetVariable "USERS1", ""
     ThisDrawing.SendCommand "(setvar " & Chr(34) & "USERS1" & Chr(34) & _
                             " (setq imgname (getfiled " & Chr(34) & _
                             "Select Image File " & Chr(34) & Chr(34) & _
                             "C:\\ " & Chr(34) & Chr(32) & Chr(34) & "jpg" & Chr(34) & " 16)))" & vbCr
     If ThisDrawing.GetVariable("USERS1") <> vbNullString Then
     [color=red]fname[/color] = ThisDrawing.GetVariable("USERS1")
     MsgBox "You choosen: " & vbCr & fname
     End If
End Sub

~'J'~

Glenn R

  • Guest
Re: Getting a variable in VBA from ACAD
« Reply #22 on: December 17, 2007, 06:12:11 PM »
ML,

I was going to comment, but I won't bother - you're obviously not learning.

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
Re: Getting a variable in VBA from ACAD
« Reply #23 on: December 17, 2007, 07:35:07 PM »
The post started like this
Quote
This should also work.
Code: [Select]
(setq USERS1 (getfiled "Select Image File" (getvar "dwgprefix") "tif" 8))

Imgname = ThisDrawing.GetVariable("USERS1")

If Matt would've typed what he meant :-) , you may not have needed to go this far.....
Code: [Select]
(setvar "USERS1" (getfiled "Select Image File" (getvar "dwgprefix") "tif" 8))
I should add, however, that I concur with what Michael, Glenn, & David have all tried to say.....stick with a VBA solution.
« Last Edit: December 17, 2007, 07:46:52 PM by Jeff_M »

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
Re: Getting a variable in VBA from ACAD
« Reply #24 on: December 17, 2007, 07:55:22 PM »
Another thought, Mark, since you seem to know how to get this in Excel, why not link to Excel's ActiveX and use it's FIleDialog?

Glenn R

  • Guest
Re: Getting a variable in VBA from ACAD
« Reply #25 on: December 17, 2007, 08:05:16 PM »
You're asking for trouble there Jeff  :wink:

ML

  • Guest
Re: Getting a variable in VBA from ACAD
« Reply #26 on: December 17, 2007, 08:18:28 PM »

Hey Guys,

I think I got it!
Please try it out; it seems to be working very well for me.
Here are 2 short examples and the nec. functions
With this, you should be able to assign (LISP) variables to ACAD from VBA and vice versa.

Mark

Code: [Select]
Dim VL As Object
Sub Lisp2VBA()
 A = GetLispSym("M") 'In ACAD, at the command line, set your variable ie (setq M "Test")
 Debug.Print "Variable = " & A
'MsgBox A
End Sub

Sub VBA2Lisp()
'Dim Value As Double
 Dim Value As String
 Value = "Test" '(120#)
 PutLispSym "B", Value 'In ACAD, type !B. The variable (B) should return Test
End Sub

Function GetLispSym(symbolName As String) As Variant
 Dim sym As Object
 Set VL = CreateObject("VL.Application.16")
 Set sym = VL.ActiveDocument.Functions.Item("read").funcall(symbolName)
 GetLispSym = VL.ActiveDocument.Functions.Item("eval").funcall(sym)
End Function

Function PutLispSym(symbolName As String, Value As Variant)
 Dim sym As Object
 Set VL = CreateObject("VL.Application.16")
 Set sym = VL.ActiveDocument.Functions.Item("read").funcall(symbolName)
 VL.ActiveDocument.Functions.Item("set").funcall sym, Value
End Function





ML

  • Guest
Re: Getting a variable in VBA from ACAD
« Reply #27 on: December 18, 2007, 11:07:35 AM »

Hey Glenn,

I saw your comment but I chose to ignore it. I don't have time for that kind of negativity. Sorry!


ML

  • Guest
Re: Getting a variable in VBA from ACAD
« Reply #28 on: December 18, 2007, 11:25:29 AM »

Anyhow, back to better things:
I was finally able to acheive what I had set out to do:

I have a menu macro in ACAD

This mixes up VBA and LISP a bit
So, with The VBA Expression, I set PaperSpace as current, then with the getfiled method in LISP, I set my variable imgname to the name of the tif file; then run my Sub in VBA.

Code: [Select]
^c^cvbastmt;ThisDrawing.ActiveSpace = acPaperSpace;(setq imgname (getfiled "Select Image File" (getvar "dwgprefix") "tif" 8));^c^c-vbarun;"K:/Type/in your path/Projectname.dvb!Module.Macro";

Then, this is an abbreviated version of my Sub:
Code: [Select]
Sub Name ()
Dim RastImg As AcadRasterImage
Dim Imgpth As String
Dim Imgname As String
Dim InsertPnt(0 To 2) As Double, scalefactor As Double, RotAngle As Double

Imgpth = ThisDrawing.Path & "\"
Imgname = GetLispSym("Imgname") 'Here, calling the below (GetLispSym) function, I am retreiving the variable (filename) from ACAD and successfully assigning it to the Set RastImg line and so on and so on.

InsertPnt(0) = -8#: InsertPnt(1) = 0#: InsertPnt(2) = 0#
scalefactor = 1
RotAngle = 0

On Error GoTo Errorhandler
Set RastImg = ThisDrawing.PaperSpace.AddRaster(Imgpth & Imgname, InsertPnt, scalefactor, RotAngle)

Function GetLispSym(symbolName As String) As Variant
  Dim VL As Object
  Dim sym As Object
  Set VL = CreateObject("VL.Application.16")
  Set sym = VL.ActiveDocument.Functions.Item("read").funcall(symbolName)
  GetLispSym = VL.ActiveDocument.Functions.Item("eval").funcall(sym)
End Function



ML

  • Guest
Re: Getting a variable in VBA from ACAD
« Reply #29 on: December 18, 2007, 11:33:29 AM »

SomeCall

I pretty much did the same as you :)
I kind of muffled through The VLAX Class as well, saw your function and an old post that helped me to narrow it down to pretty much just want I need. Trust me, I do not know VLISP at all and very little LISP.

I do like that you test for the version and that is a good idea to Set the objects to nothing at the end.

Thanks for the help!

Mark