Author Topic: VBA AND LISP - Introduction  (Read 18521 times)

0 Members and 1 Guest are viewing this topic.

CB_Cal_UK

  • Guest
VBA AND LISP - Introduction
« on: October 24, 2003, 09:51:40 AM »
(Didn't know whether to post this in 'Teach Me' or 'Vlisp Hacker' so I posted it in both!
Mark et al Please feel free to move it or delete it if you want!)

Since coming to these forums I've become more and more interested in Lisp and VBA. But I (like a lot of other people I'm sure) have no way of knowing what Lisp and VBA are suitable for!

Is it a case of learning Lisp/VBA and then applying it to what you want to achieve or is it deciding what you want to achieve and then looking to Lisp/VBA for a way to do it?

We see a lot of code in these sites which performs some pretty complicated things (a lot of it I can never see a practical use for . . . in my line of work anyway)

For all us code newbies with an interest and no skill, can you guys give some examples of practical applications (not code but examples of what you've used Lisp/VBA to achieve) and how much it's affected your productivty. . . .


Chris

SMadsen

  • Guest
VBA AND LISP - Introduction
« Reply #1 on: October 24, 2003, 10:53:30 AM »
Quote from: CB_Cal_UK
Is it a case of learning Lisp/VBA and then applying it to what you want to achieve or is it deciding what you want to achieve and then looking to Lisp/VBA for a way to do it?
Hi Chris,
It kinda bites its own tail sometimes. If you don't know Lisp/VBA then it's hard to know what you can achieve with one or the other. On the other hand, if you know both languages, it almost comes natural to pick one for the task ahead.

I would say (others will say the complete opposite, of course) that if you never have to build tools that need to talk with the "outside" world and you have no intention of building tools for other applications then learn AutoLISP. Then - if time and interest allows it - fiddle with VBA and in time you'll probably become proficient in both languages.

Lisp has the excellent advantage of being directly accessible through the command line in AutoCAD. Quite often I find myself making small functions at the command line to solve some momentary but tedious tasks. Other times I open the Visual Lisp editor, open a temporary file and create the functions there.

Here's a practical application: Just today I got a call from someone who is planning to level out some floors in an old building. She asked if I could produce a plan with curves showing the levels. I don't know if I have such a function but instead of spending 10 minutes to look for one only to find that I probably don't have one that fits, I wrote an interpolation thingy in 5 minutes and was done with the curves within an hour. Of course, the same could easily be done in VBA. With interpolation at the screen it would probably take a couple of days.

There's no doubt that using customized tools is affecting productivity - you are probably using customized tools every day with or without noticing it. The question is if it'll affect your productivity in a positive way if you are also writing the tools. Some would say that it is doing the opposite - and in many cases they'd probably be right! But I guess it all depends.

daron

  • Guest
VBA AND LISP - Introduction
« Reply #2 on: October 24, 2003, 11:26:24 AM »
This is my two cents about productivity and programming. The guy who now does plot plans came to me the other day and told me that he had 30 plot plans and that he did them in two hours. I'd like to see anybody produce that many in that time without customization and standardization.

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
VBA AND LISP - Introduction
« Reply #3 on: October 24, 2003, 11:54:52 AM »
yep lisp at the command line is a great asset! I use those shorties all the time.  I would have to say that im always creating variables for holding objects for modifications im doing, or just  doing misc. math functions on the command line with lisp.  Lisp is great at being accessable and fast for the end user.

Once you start customizing AutoCAD you'll never stop. Once you start writing your own function for autocad instead of using the generic ones supplied, you'll be working so fast you wont know what to do.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Bill::Meat4Brains

  • Guest
VBA AND LISP - Introduction
« Reply #4 on: October 24, 2003, 12:54:16 PM »
Yep - lisp is the ticket for AC.

I know vb(a) form ms access databases.  Wrote a big old project management app and think in vb when in access or other ms apps.

But in AC I just can't make the jump the vb.  I keep thinking in lisp and am able to get the results I need.  I keep parametric block data in txt files rather than a db or a spreadsheet.  Yea it's alittle harder to edit but I don't edit them often - Just once and then go make lots-o-blocks.

I don't know if I ever will go to vb in AC.  Just don't see the need.  Even VLisp seems not that useful with all the safe-array translation you got to do just to pass a 3d point.  I would rather use "command" in my lisps and send the points as a list.  It also makes it easy to change one of the point members pretty easily.

I guess AC is wired differently than ms apps in my meat4brains.


(here's a tip on editing tab delimited text files that lisp understands in excel... - Open the text file in notepad and replace all the quote marks with the carat symbol "^".  Then open the file in excell to see the data organized in nice little columns.  When done editing then open again with notepad and replace the ^ with the quote symbol and lisp wil read the data just fine.)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
VBA AND LISP - Introduction
« Reply #5 on: October 24, 2003, 05:43:13 PM »
CB_

These are some simple task that i repeat OVER & OVER & OVER & OVER.
Well you get the picture..  The routines are very simple and easy to write.
Start simple & build up. Here are some i started with.
(a note here, having started with these I have not gone back to make them correct
or fancier. If it aint broke i don't have time to fix it  :)


Here is one i use a lot, pick a point for SNAPBASE then start the HATCH command.
It is useful for placing a tile hatch just so, or a barrel tile hatch so it is aligned.
It has many uses..

Code: [Select]
^C^CSNAPBASE;\(while (progn (initdia) (command "BHATCH" pause)));


Here is another I use to hatch hip roof with tile hatch.
The 10" wide hatch looks better offset 5" from the peak so by selecting the peak the
routine offsets SNAPBASE by 5" then hatches the area below.

Code: [Select]
(defun C:rft(/ usercmd  useros pt)
    (setq usercmd (getvar "CMDECHO"))
  (setvar "CMDECHO" 0)
  (setq useros (getvar "osmode"))
  (setvar "osmode" 137)
  (Setq pt (getpoint "Pick Center point of hatch"))
  (if pt
    (progn
     (setvar "osmode" 0)
    (command "snapbase" (polar pt 0 5))
    (command "-BHATCH" "_P" "_USER" "90" "10" "N" (polar pt 4.7 5) "")
  ))
  (setvar "osmode" useros)
  (setvar "CMDECHO" usercmd)
 (princ)
) ;_end of defun


Here is one I use to trace an object the pick a side to offset to then enter the distance.
Creates a polyline offset. This is good for many tasks.
I use it a lot to draw the roof permitter by tracing the house & give it a 24" overhang.


Code: [Select]
(defun C:DrawO (/ pt1 pt2 usercmd str en1 en2    )
;;; -------  Some Housekeeping   ------------------
  (setq usercmd (getvar "CMDECHO"))
  (setvar "CMDECHO" 0)
  (setvar "PLINEWID" 0)
  (setq useros (getvar "osmode")
str "")
  (setvar "osmode" 175)
  (prompt "/nPick points, Enter when done.")
  ;;; Draw the pline
  (setq pt1 (getpoint))
  (command "PLINE" pt1 (Setq pt2 (getpoint pt1))) ;_ COMMAND
  (while (setq pt2 (getpoint pt2 "\nNext point: "))(command pt2)) ;_ WHILE
  (command "")
  (princ)
  (setq en1 (entlast))
  (initget 1)
  (setq pto (getpoint "\nSide to offset:"))
  (setq dist (getreal "\nEnter offset distance:"))
  (command "_.offset" dist en1 pto "")
  ;(setq en2 (entlast))
  (entdel en1) ; remove the user drawn line
  (setvar "osmode" useros)
  (setvar "CMDECHO" usercmd)
  (princ)
) ;_end of defun


A variation of the offset routine I have just for closet shelves.
The houses i draw have odd shaped closets with many sides. The routine
will draw the shelf at 12" with a rod (dashed line) at 10" to the side
I pick after tracing the walls.

I have a routine that inserts arcs on the end of my barrel tile hatch so the roof like looks fancy.

I have a one that after picking the width & depth of the room inserts a text of the dims, (14'-6"X16'-4")

One to hatch between two parallel lines, lets me add a hatch that indicates the exterior block walls.


These are the simple routines that just make life easer & more productive.

The only problem I see is it's addictive & before you know it your in over your head...  

But there are plenty of people here that are addicted too. They help me out all the time..

Have Fun  :D

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.

CB_Cal_UK

  • Guest
VBA AND LISP - Introduction
« Reply #6 on: October 26, 2003, 11:22:35 AM »
Thanks Guys. Can you recommend a good source of beginner information for lisp and vba? I'm going to dip my toe in the water and see how wet I get! (Time permitting)

Quote from: CAB
I have a one that after picking the width & depth of the room inserts a text of the dims, (14'-6"X16'-4")


CAB. This one sounds especially useful, I'm assuming somehow you insert the result of the distance command into an mtext entry. I label all my internal rooms with areas not dimensions. Could the same be done with the area command?

I've been experimenting with a little VBA routine that counts the number of block entries within a drawing. If I were to create objects within my drawing as blocks rather than simple lines I could use this routine to simplify the creation of Bills of Material. Given a set of parameters could I create simple blocks using lisp?

Chris

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
VBA AND LISP - Introduction
« Reply #7 on: October 26, 2003, 11:32:39 AM »
Quote
Thanks Guys. Can you recommend a good source of beginner information for lisp and vba? I'm going to dip my toe in the water and see how wet I get! (Time permitting)

For autolisp see Primer on Stig's website. http://www.smadsen.com/
TheSwamp.org  (serving the CAD community since 2003)

hendie

  • Guest
VBA AND LISP - Introduction
« Reply #8 on: October 27, 2003, 06:05:28 AM »
I kind of wandered into learning Lisp and VBA by default.

I started by learning Lisp first then got into VBA when I tried to design a dialogue box in Lisp. (the Lisp dialogue attempt was soon aborted). Dialogues are SOOoo much easier in VBA (for me anyway)

Practical examples: well...
when I first started here, it took about 25 ~ 35 minutes to complete a design. I found that we had a series of calculations applied to each design based on certain criteria. The calcs were always the same, only certain elements changed. The calculations were all done by hand.
I built a series of modules in VBA which completed all the calcs automatically.
Now I hit the button, input my dimensions and VBA does all my calcs (I use VBA to query an Access database.) and inputs the data into attributes and inserts the block into my drawing. I've now got the basic design down from around 25 minutes to around 3 ~ 4 minutes. (more play time for me !)

Here's another practical example.
I was recently tasked with designing a ductwork system. It was a bit wierd as each piece of duct was actually a sub assembly of various components.
Rather than drawing a whole load of sub assy's all over the place, I decided to go for a much more linear approach. In the end, my ductwork design was created purely by lines and arcs.
I knew that for each linear metre of duct, so many each of components X, Y & Z were required. And for each Arc, then so many of A, B & C etc were required.
I had all my "ductwork" on one layer so in the end, I created a couple of selection sets of the lines and arcs. got totals for lengths, bends etc and based on that info, worked out all the quantities of sub-components required.
I could create a complete BoM and output it to a spreadsheet in a few seconds.
In fact, it was so quick, I built in a "delay" and displayed a progress meter so it looked like it was doing a lot more work than it actually was !  :twisted:

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
VBA AND LISP - Introduction
« Reply #9 on: October 27, 2003, 07:22:59 AM »
Here is the one i use for room size.
Note that there is NO error checking of user input.


Code: [Select]
;;        RmSize.lsp
;;      Created by C. Alan Butler  2003
;;
;; error function & Routine Exit
(defun *error* (msg)
  (if
    (not
      (member
msg
'("console break" "Function cancelled" "quit / exit abort")
      )
    )
     (princ (strcat "\nError: " msg))
  ) ; if
    ;;;==========  Exit Sequence  ============
  (setvar "osmode" useros)
  (setvar "luprec" uLuprec)(princ)
) ;
 ;end error function
;;  pre set global variable, it remember your entry

 ;======================
 ;    Start of Routine
 ;======================
(defun C:RmSize (/ usercmd useros Lp pt1 pt2 pt3 txtpt Len Wid txt dwg_ht )
;;; -------  Some Housekeeping   ------------------
  (setq usercmd (getvar "CMDECHO"))
  (setvar "CMDECHO" 0)
  (setq useros (getvar "osmode")
        uLuprec (getvar "luprec")
Lp     1
  )
  (setvar "luprec" 0)
  (while (= Lp 1) ; loop until ESCAPE pressed
  (setvar "osmode" 175)

    (setq pt1 (getpoint "\nPick Room Width:")
 pt2 (getpoint "\nSelect second point: ")
 Wid (abs(-(abs(car pt1)) (abs(car pt2))))
 Pt3 (getpoint "\nPick Room Length:")
 pt4 (getpoint "\nSelect second point: ")
 Len (abs(-(abs(cadr pt3)) (abs(cadr pt4))))
 txtpt (getpoint "\nPick Center of text: ")
    ) ;_end of setq

    (setq txt (strcat (rtos wid ) " X " (rtos len))) ; create the text "Width X Length"

;;;Check if the drawing text height is set to 0:
    (setq  dwg_ht (cdr (assoc 40 (tblsearch "style" (getvar "textstyle")))) )
   (setvar "osmode" 0)
;;; If text height is undefined (signified by 0 in the table)
    (if (= dwg_ht 0)
;;; Draw the text using the CURRENT text height (textsize)
      (command "text" "c" txtpt "" 0 txt)
;;; Otherwise use the defined text height
      (command "text" "c" txtpt 0 txt)
    ) ; endif
  ) ; end while
  ;;;==========  Exit Sequence  ============
  (setvar "osmode" useros)
  (setvar "luprec" uLuprec)
 ;;; Exit quietly
 (princ)

) ;_end of defun

;;; Notify user program ready to use
(prompt "\n Room Size Label Routine Loaded:   Type 'RmSize' to run it.")
(princ)
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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
VBA AND LISP - Introduction
« Reply #10 on: October 27, 2003, 08:01:34 AM »
CA_

Yes you can do an area in a similar manner.

You could try using .bpoly to create an poly line around the room and then get the area of that pline.
This theoretically would require one to pick a spot inside the room.
The problem is going to be the door as bpoly looks outward from the pick point for a hatch border.
Doors swing into the room will cause the pline to bulge out around the door.
Another pit fall is when the door swings out, the bulge will add area to the room, unless you have a line
across the door like threshold line.

These are the things you must think about when creating a routine.
The way you do it manually may differ from the way the routine must do it.

The easiest way to have the ROUTINE do it is to have the user draw a pline around the area of the room.

Here is the logic

Loop
User pick corners of room
Test user pick, exit when pick point = 1st pick point
draw a closed pline using user points
get area of pline
delete pline
create text string of area
Prompt user for location of text
put text there.
done


Note that there are many was to solve programing problems and its the creative programer that
discover them (not me, I'm a code thief, you may need to look up that thread to understand this comment)
You can often take pieces of routines that do what you
want and put the together to get the results you need.


Good luck
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.

SMadsen

  • Guest
VBA AND LISP - Introduction
« Reply #11 on: October 27, 2003, 09:28:10 AM »
If anyone (i.e. more than one) is interested, we could create a new thread and do a little online course in creating a room area routine? Step by step for beginners?

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
VBA AND LISP - Introduction
« Reply #12 on: October 27, 2003, 01:12:26 PM »
You got my vote.

I learn something new on every one of you examples..

Thanks sMad :D
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.

daron

  • Guest
VBA AND LISP - Introduction
« Reply #13 on: October 27, 2003, 01:21:40 PM »
Don't forget about http://www.afralisp.com. It's a tutorial website for lisp and vba. I know some of the guys here learned and have even written some of he articles found there. Also, Thanks Kenny for promoting this site in your newsletter.

SMadsen

  • Guest
VBA AND LISP - Introduction
« Reply #14 on: October 27, 2003, 01:44:29 PM »
Kenny does have a great tutorial about labelling areas at http://www.afralisp.com/lisp/area.htm. Go read it, please. Go... Shoo! It's good stuff!

I was thinking more of an interactive learning session of how to get about such a program (to kill those dark winter nights).

I would also like to thank Kenny for promoting this site in his newsletter. Beers are on me (if you ever get in the neighborhood)! :D

SMadsen

  • Guest
VBA AND LISP - Introduction
« Reply #15 on: October 27, 2003, 01:45:32 PM »
Oops, MY neighborhood, that is! Duh

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
VBA AND LISP - Introduction
« Reply #16 on: October 27, 2003, 02:05:08 PM »
w00t! Free Beer!

Im a wuss when it comes to beer now-a-days. I had a few beers a couple of weeks ago and i ended up with a hangover. ...An I only had three beers too! :? :lol:
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

SMadsen

  • Guest
VBA AND LISP - Introduction
« Reply #17 on: October 27, 2003, 02:33:28 PM »
Wuss!

erhmmm, kegs you mean? 3 kegs?

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
VBA AND LISP - Introduction
« Reply #18 on: October 27, 2003, 03:22:04 PM »
lmao   :shock:  ...Ummm, yeah. (Did i say beers?! I ment Kegs!)
 :roll:
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CB_Cal_UK

  • Guest
Thanks!
« Reply #19 on: October 28, 2003, 11:39:25 AM »
A big 'Thanks' to all for your help and comments  :!:

I checked out Stigs AutoLISP primer and thought it was great. Very well written and definitely catering for the beginner, it didn't gloss over the really important parts or assume any prior programming knowledge which is refreshing.

As I read through some of the code extracts I can kind of see the outline of what the routine is supposed to do (It's like the 80/20 rule. 80% of it's straightforward it's the difficult to understand 20% that makes the routine workable!)

I especially found CAB's step by step logic useful. Just about everyone here is fluent in 'thinking' in AutoCAD even if we're not familiar with programming. If you guys could outline the actual AutoCAD steps you are trying to achieve with the code it would certainly make it more understandable.

I especially like the sound of a 'training' forum to go through little bits of code. Already on this site we've seen you 'Lisp Hackers' come to the rescue of several people who've had trouble creating routines.

I know Se7en got upset with people using him as a resource for all their coding needs and I'd like to think any 'training' forum would be grateful for all the expertise the great people on this site have. It could be a kind of pre-school for all those of us who want to join in on the VLisp Hacker topics but are totally out of our depth :!:



Anyway. Thanks again  :D

Chris

SMadsen

  • Guest
VBA AND LISP - Introduction
« Reply #20 on: October 28, 2003, 12:25:05 PM »
CB_Cal_UK, thanks for your kind words on my small texts. Glad you could use it.

A distinct training forum is perhaps a wee bit over the edge. It's up to the individual user here to get involved in a thread or not, while a training forum more or less should have a faculty - which is maybe a little off from the purpose or general idea of a forum like this.

What I had in mind was just another thread where we could work towards a small area routine and maybe learn something in the process.

This could be an outline of a call to such a thing:

Command: PAREA (or whatever)
Specify first point or [Select] <Select>:
Specify next point:
...
Specify insertion point or [?/Options/Select] <Select>:

And pling! The area (either by picked points or selecting an existing pline) would be inserted as an attributed block.

CB_Cal_UK

  • Guest
The Missing Link . . .
« Reply #21 on: November 04, 2003, 08:52:19 AM »
Quote from: SMadsen
Kenny does have a great tutorial about labelling areas at http://www.afralisp.com/lisp/area.htm. Go read it, please. Go... Shoo! It's good stuff!


Stig,

Followed your orders and went to read it straight away! but the link is broken! Can you re-post please?

Chris

SMadsen

  • Guest
VBA AND LISP - Introduction
« Reply #22 on: November 04, 2003, 08:58:56 AM »
Oops, a period got into that url somehow. Just remove the period:
http://www.afralisp.com/lisp/area.htm

hendie

  • Guest
VBA AND LISP - Introduction
« Reply #23 on: November 04, 2003, 08:59:57 AM »
try http://www.afralisp.com/lisp/area.htm ~ I think Stig had a misplaced period at the end of the url

hendie

  • Guest
VBA AND LISP - Introduction
« Reply #24 on: November 04, 2003, 09:01:01 AM »
that's just great

now my post looks stoopid !  :(
(Stig beat me to it by a millisecond)