newer 发表于 2021-1-31 00:10:54

使用VBA存取LDATA值

Accessing LDATA values from VBA



问题:
A VLISP function can be used to create a dictionary just like:

(vlax-ldata-put "Test" "Helper" "Good")

And a dictionary named "Test" is created, and a (vlax-ldata-get "Test" "Helper")
retrieves the return value, "Good". From VBA, how can I get the dictionary data
value of "Good"?

解答:

VLISP ldata is attached to a custom object with class name "vlo_VL" and then the
object is added to the Named Object Dictionary. However, a custom object is not
penetrable by means of the VBA object model alone.You cannot create custom
entities/objects with VBA, buty ou can develop custom entities with ObjectARX
and create an ActiveX interface to it so that it can be used within VBA.
Unfortunately, there is no direct interface (or Automation server) to a vlo_VL
object.

The AutoCAD 2000 Visual LISP Automation server (an unsupported interface) can be
used to bridge the gap between VBA macro and the LISP environment, though.

If the following has already been entered successfully at the command line:
(vl-load-com)
(vlax-ldata-put "Test" "Helper" "Good")
Then, with a little effort a VBA macro can use the LISP server's generalized
object model:

Function readEvalHelper(app As Object)
   ' this is a helper function
   Set vld = app.ActiveDocument

   Dim vlf_read As Object
   Set vlf_read = vld.Functions.Item("read")

   Dim vl_obj1 As Object
   Set vl_obj1 = vlf_read.funcall("(defun read-eval (arg)(eval (read arg)))")

   Dim vlf_eval As Object
   Set vlf_eval = vld.Functions.Item("eval")

   Dim vl_obj2 As Object
   Set vl_obj2 = vlf_eval.funcall(vl_obj1)

End Function


Public Sub vlsGetLData()
   Dim vlApp As Object
   Set vlApp = CreateObject("VL.Application.1")

   readEvalHelper vlApp

   Dim invokeIt As Object
   Set invokeIt = vlApp.ActiveDocument.Functions.Item("read-eval")
   invokeIt.funcall ("(setq lDatum (vlax-ldata-get ""Test"" ""Helper""))")

   Set sym = vlApp.ActiveDocument.Functions.Item("read").funcall("lDatum")
   GetLispSym = vlApp.ActiveDocument.Functions.Item("eval").funcall(sym)
   MsgBox ("Key's data is:" + Chr(13) + GetLispSym)

End Sub

See also DevNote #30485 for more discussion and illustrated examples of how to
exploit Visual LISP Server.

页: [1]
查看完整版本: 使用VBA存取LDATA值