© 博学网 (Boxue58)

 Forgot password?
 注册register
Search
1234Next
Return to list New
View: 14762|Reply: 30
Collapse the left

VBA通过VL类与VLISP进行交互

  [Copy link]
Post time 2009-3-15 14:41:07 | Show all posts |Read mode

I want to Be A member...

You have to Login for download or view attachment(s). No Account? Register

x
本帖最后由 wuyunpeng888 于 2009-10-8 09:57 编辑

我们都知道VBA支持COM,Visual LISP也支持COM,这样就可以COM把它们联系起来
我们在VBA里可以通过调用VL类与VLISP进行交互,从而可实现与VLISP之间进行数据传递和调用
下面我举一个简单的例子
在VBA里添加一个类模块名为cL
类模块CL里的代码如下:
  1. Option Explicit
  2. Public Enum VarType
  3. NoObject = 0
  4. YesObject = 1
  5. End Enum
  6. Private VL As Object
  7. Private VLF As Object

  8. Private Sub Class_Initialize()
  9. On Error GoTo ErrInterface
  10. With Application
  11. ThisDrawing.SendCommand  "(vl-load-com)" & vbCr
  12. Select Case Left(.Version, 2)
  13. Case "15"
  14. Set VL = .GetInterfaceObject("VL.Application.1")
  15. Case "16", "17"
  16. Set VL = .GetInterfaceObject("VL.Application.16")
  17. End Select
  18. Set VLF = VL.ActiveDocument.Functions
  19. End With

  20. Exit Sub

  21. ErrInterface:
  22. MsgBox Err.Number & vbNewLine & Err.Description
  23. End Sub

  24. Public Function EvalLispExpression(lispStatement As String, Optional ByVal VariantType As VarType = VarType.NoObject) As Variant

  25. Dim sym As Object, ret As Object, retval As Variant

  26. Set sym = VLF.Item("read").funcall(lispStatement) '这段是先实现(read str)返回Lisp表达式

  27. On Error GoTo ErrRead
  28. If VariantType = NoObject Then
  29. retval = VLF.Item("eval").funcall(sym) '这段再用Lisp的eval函数解释执行
  30. EvalLispExpression = retval
  31. Else
  32. Set retval = VLF.Item("eval").funcall(sym) '返回对象用
  33. Set EvalLispExpression = retval
  34. End If
  35. Exit Function

  36. ErrRead:
  37. EvalLispExpression = ""
  38. End Function

  39. Private Sub Class_Terminate()
  40. Set VLF = Nothing
  41. Set VL = Nothing
  42. End Sub
Copy the Code
 Author| Post time 2009-3-15 14:41:51 | Show all posts
Thisdrawing里的代码如下:

Guest, if you want to see the hidden content, please Reply
 Author| Post time 2009-3-15 14:44:29 | Show all posts
这个例子是通过LISP函数(grread t)获得当前光标处的CAD坐标,用这个坐标实现几个圆跟踪光标的小东东

源程序在这里

LispToVBA.rar

11 KB, Downloads: 52

 Author| Post time 2009-3-15 14:50:34 | Show all posts
建议在空文档里测试此程序
Post time 2009-3-26 19:22:41 | Show all posts
VL类如何才能比较稳定???请楼主再说说
 Author| Post time 2009-3-26 20:16:51 | Show all posts
估计是这样调用得太频繁了
Post time 2009-3-27 14:40:49 | Show all posts
楼主我觉得CL类再增加一项,可能稳定性要好一点,就是反复操作也没有问题,就是释放资源:
Private Sub Class_Terminate()
'类析构时,释放内存
Set VLF = Nothing
Set VL = Nothing
End Sub
 Author| Post time 2009-3-27 20:21:43 | Show all posts
到是忘记了释放了,过程结束时对象就结束了,或者把变量声明在外边
 Author| Post time 2009-3-27 20:35:02 | Show all posts
可能是我的软件有问题,刚一打开时就有一个错误框,把CAD重装一下
 Author| Post time 2009-3-27 20:49:00 | Show all posts
可以用这个类实现很多比较强的功能,比如添加标注对象后使标注的位置随光标动态改变,点光标后位置确定,或根据光标的位置来确定更换标注类型
类似CAD的动态复制、移动
等等 等等
总之动态的功能都可以用它尝试
You have to log in before you can reply Login | 注册register

Points Rules

QQ|Dark|Moble|博学网 (Boxue58/Boxuesky) ( wechat: boxuesky )

2020-9-27 15:40 GMT+8

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

Quick Reply To Top Return to the list