Author Topic: VBA Statements in LISP  (Read 10000 times)

0 Members and 1 Guest are viewing this topic.

Peter Jamtgaard

  • Guest
VBA Statements in LISP
« on: December 07, 2006, 02:59:19 PM »
Hey Group,

Long time...

Have any of you ever wished that some activeX lisp expressions could be written using VBA syntax like:

(translateVBA "ThisDrawing.Blocks.Item(0).Item(2)")

instead of

(vla-item (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) 0) 2)

Just to make use of intellesense and to shorten up the code :?.

Peter


GDF

  • Water Moccasin
  • Posts: 2081
Re: VBA Statements in LISP
« Reply #1 on: December 07, 2006, 03:37:00 PM »
Hey Group,

Long time...

Have any of you ever wished that some activeX lisp expressions could be written using VBA syntax like:

(translateVBA "ThisDrawing.Blocks.Item(0).Item(2)")

instead of

(vla-item (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) 0) 2)

Just to make use of intellesense and to shorten up the code :?.

Peter





Peter

Good to see you at the swamp again.
; error: no function definition: TRANSLATEVBA

Do you have the missing function?

Gary

Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: VBA Statements in LISP
« Reply #2 on: December 07, 2006, 04:49:58 PM »

Peter

Good to see you at the swamp again.
; error: no function definition: TRANSLATEVBA

Do you have the missing function?

Gary


Hi Gary,
From his posts on the WAUN mail Group Peter is working on a beta routine.

I seem to recall Tony T' displaying similar functionality at one stage ...
.. but that may be my time-warped memory.

/// kwb
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Peter Jamtgaard

  • Guest
Re: VBA Statements in LISP
« Reply #3 on: December 08, 2006, 04:18:16 PM »
Gary,

Just thought I would stop in for a visit. MEGA busy lately.

I had an excellent response to my LISP Table Magic class I taught at AU.

The sample allows the user to create a bill of materials in a well proportioned
table with block name, block symbol and count. The bonus material allows the
user to enter pricing and exclude blocks (like the titleblock) from the table.
It includes blockname, blocksymbol, count, unit price, and subtotal price.

The whole table also produces a grand total. After updating the counts there is
a function to update the table. I also included a function to directly export a
table to excel. Of course all written in Visual LISP.

If you (or anyone) would like the code email me at peter@cordecksales.com.

As for the code I mentioned in my original post.

The code I have below translates VBA statement like "thisdrawing.blocks.item(0)"

into the corresponding (vlax-invoke (vlax-get (vlax-get (vlax-get-acad-object) "activedocument") "blocks") 0)

and evaluating it. I used the Vital LISP function syntax to simplify the arguments and returns.

It should also be able to accept more than just one argument for methods.

Bon Apetite,

Peter

Do you all have an upload site for code?
Can you attach files to the forum?

Code: [Select]
; Written By: Peter Jamtgaard copr 2006
; This routine translates "simple" vba statements into visual lisp and
; evaluates them like (translatevba "thisdrawing.blocks.item(0)")
; will return the first item in the blocks collection. make sure to
; not have any spaces in the expressions.

(defun VBA (strVBAStatement)(translateVBA strVBAStatement))

(defun TranslateVBA (strVBAStatement ; String VBA Statement
                    /
                     lstStatement    ; List of properties and methods
                     lstStatement2   ; Returned lisp expression
                     strItem         ; String Item in list of M and P
                     strObject       ; Base object for functions                     
                    )
 (setq strVBAStatement (replaceSubStrings " " "" strVBAStatement))
 (setq lstStatement    (mapcar 'strcase (csvstringtolist strVBAStatement ".")))
 (if (= (strcase (setq strObject (car lstStatement))) "THISDRAWING")
  (setq lstStatement2 (quote (vlax-get (vlax-get-acad-object) "Activedocument")))
  (setq lstStatement2 (read strObject))
 )
 (if (cdr lstStatement)
  (foreach strItem (cdr lstStatement)
   (if (or (vl-string-search "(" strItem 0)
           (vlax-method-applicable-p (eval lstStatement2) strItem)
       )
    (setq lstStatement2 (append (list (read "vlax-invoke")) (list lstStatement2))
          lstStatement2 (append lstStatement2 
                                (if (vl-string-search "(" strItem 0)
                                 (GetMethodArguments strItem)
                                 (list strItem)
                                )
                        )                                             
    ) 
    (setq lstStatement2 (append (list (read "vlax-get")) (list lstStatement2))
          lstStatement2 (append lstStatement2 (list strItem))
    )
   )
  )
 )
 (eval lstStatement2)
)

; Converts the aguments inside the parenthesis of the vba statement
; into a list of arguments


(defun GetMethodArguments (strItem
                          /
                           lstItems
                           strItems
                           strMethod
                          )
 (setq lstItems     (vl-string->list strItem)
       strMethod    (vl-list->string (reverse (cdr (member 40 (reverse lstItems)))))
       strItems     (vl-list->string (member 40 lstItems))
       strItems     (replaceSubStrings "," " " strItems)
 )
 (setq lstItems     (append (list strMethod) (read strItems)))     
)



(defun ReplaceSubStrings (strPattern strNewString strItem / intPosition)
 (setq intPosition 0)
 (while (setq intPosition (vl-string-search strPattern strItem intPosition))
  (setq strItem (vl-string-subst strNewString strPattern strItem intPosition))
 )
 strItem
)


; Parsing a textstring to a list.

(defun CSVStringToList  (strText strChar / intPosition lstStrings)
 (while (setq intPosition (vl-string-search strChar strText 0))
  (setq lstStrings  (cons (substr strText 1 intPosition) lstStrings)
        strText     (substr strText (+ intPosition 1 (strlen strChar)))
  )
 )
 (reverse (cons strText lstStrings))
)


(princ)

GDF

  • Water Moccasin
  • Posts: 2081
Re: VBA Statements in LISP
« Reply #4 on: December 08, 2006, 04:38:24 PM »
Peter

Thanks. Come on over more often.

I still receive emails from WUAN and saw the posted lisp.
Thanks again for sharing it. Vlisp and VBA are still beyond my horizon.

Gary
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: VBA Statements in LISP
« Reply #5 on: December 08, 2006, 05:11:38 PM »
Do you all have an upload site for code?
Can you attach files to the forum?

Peter,
First of all, Thanks.

You can attach just about anything.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Peter Jamtgaard

  • Guest
Re: VBA Statements in LISP
« Reply #6 on: December 09, 2006, 08:28:00 AM »
Here is the tablemagic stuff.

Put AU06TableMagic.lsp, AU06TableMagicBonus.lsp, UpdateTable.lsp and AU06.dcl in
a directory in your serach path.

Load AU06TableMagic and AU06TableMagicBonus and UpdateTable in that order.

Open any drawing with multiple blocks in it or the AU06_Table_Magic.dwg example drawing.

Type BOMDialog at the command line.

Set the prices and show values for each block.

Make sure the textsize system variable is not set to 0.

Type BillOfMaterials at the command line.

Pick an insertion point and press enter a couple times (you can remove the getstring expressions)
it will draw a bill of materials, with pricing and a grand total.

If you change the number of blocks, use the updatetable function to update your table.

Check it out, it was well received at AU.

Peter



CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: VBA Statements in LISP
« Reply #7 on: December 09, 2006, 09:59:51 AM »
Very Cool Peter...

I did have this problem using ACAD2006 SP1

Quote
;   SetRowHeight (2)
;   SetSubSelection (4)
;   SetText (3)
;   SetTextHeight
Press Enter:

Press Enter:

Press Enter:
 (2)
;   SetTextRotation (3)
;   SetTextStyle (2)
;   SetXData (2)
;   TransformBy (1)
;   UnmergeCells (4)
;   Update ()

Press Enter:
; error: no function definition: VLA-GETCELLVALUE
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Peter Jamtgaard

  • Guest
Re: VBA Statements in LISP
« Reply #8 on: December 09, 2006, 02:43:49 PM »

I was aware of that change in the table object.

The au06tablemagic.lsp routine version of the bill of materials uses the getcelltext method instead of getcellvalue method.

You  can change the code, and I think it will work OK but the $ will not appear in the table. Maybe it will.

The second routine AU06TableMagicBonus was written for A2k7

The header in the AU06TableMagic.lsp routine specifies

Quote
; These routines are based on AutoCAD 2007 Visual LISP functions.

Peter



Very Cool Peter...

I did have this problem using ACAD2006 SP1

Quote
;   SetRowHeight (2)
;   SetSubSelection (4)
;   SetText (3)
;   SetTextHeight
Press Enter:

Press Enter:

Press Enter:
 (2)
;   SetTextRotation (3)
;   SetTextStyle (2)
;   SetXData (2)
;   TransformBy (1)
;   UnmergeCells (4)
;   Update ()

Press Enter:
; error: no function definition: VLA-GETCELLVALUE

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: VBA Statements in LISP
« Reply #9 on: December 09, 2006, 04:24:07 PM »
Well I didn't read the header, just fired it up. :)
I'm one of those guys that read the instructions as a last resort.
But No joy on the alternate function. Don't sweat it though.

Quote
Press Enter:
 (2)
;   SetTextRotation (3)
;   SetTextStyle (2)
;   SetXData (2)
;   TransformBy (1)
;   UnmergeCells (4)
;   Update ()

Press Enter:
; error: no function definition: VLA-GETCELLTEXT

Is it my installation or is the table access slow.
Counting in my head these are the SECONDs between key press & the next prompt.
3
7
12
1
5
13
« Last Edit: December 09, 2006, 04:59:09 PM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Peter Jamtgaard

  • Guest
Re: VBA Statements in LISP
« Reply #10 on: December 09, 2006, 04:49:41 PM »
It is slow.

Every time a cell in the table is updated, Autocad has to rethink the whole table. I would think it would have been better to make each cell a separate object...

I think the 41 seconds or so is worth the wait. Don't you?

You can take out the getstrings and pick a drawing with less blocks too.

I left the getstrings in to show the class what it was actually doing.

Peter

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: VBA Statements in LISP
« Reply #11 on: December 09, 2006, 04:58:03 PM »
41 seconds is fine, I wasn't complaining, I was just concerned that there might be a problem on my end.
That's a great tool.
Thanks for sharing you knowledge.
 :-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Peter Jamtgaard

  • Guest
Re: VBA Statements in LISP
« Reply #12 on: December 09, 2006, 05:15:25 PM »
I always am trying to figure out how to make VL better.
Sone people think it is a dead language, I think it is very much alive.

As long as we keep coding and teaching it, it is alive.

Coding these kinds of functions make it more useful.

They are general enough to make it do other useful things too...

Glad you liked it.

It is now I have to figure out what to do for next year....

That is why I wrote the vba translator, just thought it might spark some conversation.

Peter

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: VBA Statements in LISP
« Reply #13 on: December 09, 2006, 05:49:34 PM »
Your vba translator is a cool tool but I don't do vba. I do love LISP & starting to get a handle on vLisp.
I have No plans of abandoning vLisp. :)

If you are going to continue to work on the Table lisp perhaps a way to tag blocks to exclude from the table via the BOMDialog.
Just a suggestion. Or perhaps a table ID so you could create several tables. Thinking aloud here.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Peter Jamtgaard

  • Guest
Re: VBA Statements in LISP
« Reply #14 on: December 10, 2006, 04:58:27 PM »
Your vba translator is a cool tool but I don't do vba. I do love LISP & starting to get a handle on vLisp.
I have No plans of abandoning vLisp. :)

If you are going to continue to work on the Table lisp perhaps a way to tag blocks to exclude from the table via the BOMDialog.
Just a suggestion. Or perhaps a table ID so you could create several tables. Thinking aloud here.

The BOMDialog will already exclude the blocks like you mentioned.

In the dialog Highlight the block or blocks, and change the show button at the bottom.

Run the UpdateTable command line function on the table.

Let me know if that isn't working.

Not a bad idea to have multiple schedules. Any ideas on how to do it?

One would need to allow each table to have its own filter.

Say we use the xdata on the block definition objects as the default for New schedules.

Once the table is created, the routine could attach xdata to that table itself and save the custom filter for itself.

INside the BOMDialog Dialog box you could include a list box that would like the default setting and also list all of the existing tables. You could select one of the tables and the dialog would populate with the list for that table...

Or something like that...


CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: VBA Statements in LISP
« Reply #15 on: December 10, 2006, 08:17:18 PM »
I just took a look at one of my drawings to see how I might implement this routine.
It would be a nice feature for me to include the fixture count for the electrical BOM.
I would not include the price but would leave that column blank.
Creating a filter, I came up with this (setq ss (ssget "X" '((0 . "INSERT")(8 . "E1_*,E2_*"))))
Although I use Arch-T in Vanilla ACAD the block naming has a few odd blocks that prevent
a name filter. It could have been me that added the few odd blocks. When I get time I'll research that.
Other wise the name filter would work. (2 . "ESYM*,16P")

On my house plans I usually limit the tables to Door Schedule & a Window Schedule
Although once set up the electrical schedule would be painless to include & would delight the electrician.

Oh I could use Arch-T to create the table but I enjoy messing with this stuff. :)
Just kicking around ideas here.


PS  You may not have noticed but I have been exploring plain text tables for my enjoyment. :)
http://www.theswamp.org/index.php?topic=13469.0
« Last Edit: December 10, 2006, 08:22:48 PM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Peter Jamtgaard

  • Guest
Re: VBA Statements in LISP
« Reply #16 on: December 11, 2006, 09:59:27 AM »
If you do not load the bonus functions, the original billofmaterials in LISPTableMagic command line function will create the schedule without the prices. It doesn't exclude blocks from BOMDialog though. You can add a filter in the countallblocks function to remove them.

You can modify the code to create a electrical schedule too.

Peter

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: VBA Statements in LISP
« Reply #17 on: July 25, 2007, 09:32:34 PM »
Peter,

I've been playing with Tables recently and found your routines very helpfull for sorting out the requirements.

Thanks for posting this !

I just wish A'desk had used another name ... doing a search for 'table' produces a prodigious amount of irrelevant info.

« Last Edit: July 25, 2007, 09:33:52 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.