PDA

View Full Version : Condensing Lisp


fletch97
2005-10-18, 05:58 PM
Morning -

I've attached a lisp I just started writing and I need some help trying to make this a little more efficient. I have a ton of code written just for 2 objects....I'll have a ton more by 60!! You'll see when you take a look at the lisp.

Thanks for any help!

Opie
2005-10-18, 06:07 PM
Fletch,

Just a quick look. The first thing that I see is too many redundant commands that could be moved out of your conditional statements. Instead of repeating your explodes and moves, you could move those out of the conditional and execute after the conditional is completed. I hope I haven't lost you on that one.

You could also create a subroutine to do the inserting, exploding, and.or moving and just past the information of the block to the subroutine.

fletch97
2005-10-18, 06:16 PM
Thanks Richard....I was thinking along the same lines and came up with this instead. Let me know if you think this is better?

Opie
2005-10-18, 06:23 PM
What happens when your number of devices is greater than 3? DVS variable? And why not put those if statements into a conditional statement?

fletch97
2005-10-18, 06:29 PM
Richard -

As you can see by the most recent attached copy of that lisp......that as the numbers increase the amount of commands and moves increases.

Not sure how to utilize the conditional statement?

Thanks again!

Opie
2005-10-18, 06:40 PM
Fletch, can you explain what this routine is supposed to do?

And why are you exploding the blocks? You could be using those blocks for quantity calculations.

Instead of moving the last item over @24,0,0, why not calculate the new point?
;;;Custom Lisp Routine created for Johnson COnsulting
;;;By Steven Fletcher

(defun C:DEVICE ()
(setvar "cmdecho" 0)
(setq PT1 (getpoint "\nPick a starting point ")
DVS
(getint
"\n How many devices in the circuit? (1 to 60): "
)
;;removed strcase function
ANG 0.0 ;_ angle in radians
DIST 24.0 ;_ distance from last point
)
(repeat DVS
(STRING)
(setq PT1 (polar PT1 ANG DIST))
)
(setvar "cmdecho" 1)
(princ)
)

(defun STRING ()
(setvar "cmdecho" 0)
(setq OBJ (strcase (getstring "\n What Device? (S, B, H, C or F): ")))
(cond ((= OBJ "S")
(command "-insert" "c:/steve/jce-library/blocks/smoke.dwg" PT1 "" "" "")
)
((= OBJ "B")
(command "-insert" "c:/steve/jce-library/blocks/beam.dwg" PT1 "" "" "")
)
((= OBJ "H")
(command "-insert" "c:/steve/jce-library/blocks/heat.dwg" PT1 "" "" "")
)
((= OBJ "C")
(command "-insert" "c:/steve/jce-library/blocks/relay.dwg" PT1 "" "" "")
)
((= OBJ "F")
(command "-insert" "c:/steve/jce-library/blocks/pull.dwg" PT1 "" "" "")
)
)
)

fletch97
2005-10-18, 06:47 PM
Richard -

The lisp is to help generate strings like the screen shot.......does that help?

fletch97
2005-10-18, 06:49 PM
Oh and for some reason it won't work after I put in more than 3 variables for DVS?

fletch97
2005-10-18, 06:52 PM
I just tried your last code and I got this error.....


Command: device

Pick a starting point
How many devices in the circuit? (1 to 60): 3
; error: bad argument type: stringp 3

Opie
2005-10-18, 06:57 PM
I just tried your last code and I got this error.....


Command: device

Pick a starting point
How many devices in the circuit? (1 to 60): 3
; error: bad argument type: stringp 3
Fletch,

Either go back and recopy my editted code or remove the strcase function and corresponding open and close parenthesis.

fletch97
2005-10-18, 07:11 PM
That worked......but how would I tell your code to jog down for every 15 items.....see the screen shot above?

Opie
2005-10-18, 07:18 PM
That worked......but how would I tell your code to jog down for every 15 items.....see the screen shot above?
You should probably change it to a while loop with a repeat loop inside. You would need to do some more calculations to accomplish this.


Suggestion only: Write out your routine with the manual steps you want to accomplish. Make a flow chart as well to flesh out how your routine will execute (how you would do it if you were manually accomplishing this w/ your logic behind the decisions.)

After taking these steps, you should be able to accomplish what you want to do in your routine. If you stumble along the way, you already know you can always ask for help in here.

jwanstaett
2005-10-18, 07:45 PM
this will do will insert the device and is not limited to 60

by putting the * before the file name the block will be explode when it is inserted


;;Custom Lisp Routine created for Johnson COnsulting
;;By Steven Fletcher
;; change By John w. Anstaett

(defun c:device ()
(setvar "cmdecho" 0)
(setq PT1 (getpoint "nPick a starting point ")
DVS (getint ;us integer not a stirng
"n How many devices in the circuit? (1 to 60): ")
)
(repeat dvs ;call the string function dvs times
(string)
(setq pt1 (list (+ (car pt1) 24) (cadr pt1) (caddr pt1)))
;move the pt1
)
;-----------------------------


;-----------------------------


(setvar "cmdecho" 1)
(princ)
)

;-----------------------------


(defun String ()
(setvar "cmdecho" 0)
(setq OBJ
(strcase (getstring "n What Device? (S, B, H, C or F): "))
)
(if (= OBJ "S")
(progn
(command "-insert"
"*c:/steve/jce-library/blocks/smoke.dwg"
PT1 ""
"") ;put a * before the file name will explode when inserting block
) ;and drop one set of "" it will ask for y sclae
)
(if (= OBJ "B")
(progn
(command "-insert"
"*c:/steve/jce-library/blocks/beam.dwg"
PT1 ""
"")
)
)
(if (= OBJ "H")
(progn
(command "-insert"
"*c:/steve/jce-library/blocks/heat.dwg"
PT1 ""
"")
)
)
(if (= OBJ "C")
(progn
(command "-insert"
"*c:/steve/jce-library/blocks/relay.dwg"
PT1 ""
"")
)
)
(if (= OBJ "F")
(progn
(command "-insert"
"*c:/steve/jce-library/blocks/pull.dwg"
PT1 ""
"")
)
)
(setvar "cmdecho" 1)
(princ)
)

miff
2005-10-18, 08:34 PM
Hi Fletch,
Here's one that makes the sequence jog down every 15 items. Note that I needed to change the block names in the insert for my testing so change them to your path prior to trying it out.

Jeff

;;;Custom Lisp Routine created for Johnson Consulting
;;;By Steven Fletcher, revised by Jeff Mishler

(defun C:DEVICE (/ string ang dist dvs idx pt1 pt2 pt3)
(defun STRING (pt1 / obj)
(setvar "cmdecho" 0)
(initget 1 "S B H C F")
(setq OBJ (getkword "\n What Device? (S, B, H, C or F): "))
(cond ((= OBJ "S")
(command "-insert" "S" PT1 "1" "1" "0")
)
((= OBJ "B")
(command "-insert" "B" PT1 "1" "1" "0")
)
((= OBJ "H")
(command "-insert" "H" PT1 "1" "1" "0")
)
((= OBJ "C")
(command "-insert" "C" PT1 "1" "1" "0")
)
((= OBJ "F")
(command "-insert" "F" PT1 "1" "1" "0")
)
)
)
(setvar "cmdecho" 0)
(setq PT1 (getpoint "\nPick a starting point ")
DVS
(getint "\n How many devices in the circuit? (1 to 60): ")
ANG 0.0 ;_ angle in radians
DIST 24.0 ;_ distance from last point
idx 0 ;counter
)
(while (<= (setq idx (1+ idx)) dvs)
(cond ((or (< idx 15)
(< 30 idx 45)
)
(STRING pt1)
(setq PT1 (polar PT1 ANG DIST))
)
((or (< 15 idx 30)
(> 31 idx 45)
)
(STRING pt1)
(setq PT1 (polar PT1 ANG (- DIST)))
)
((or (= idx 15)
(= idx 45)
)
(STRING pt1)
(if (< idx dvs)
(progn
(setq PT2 (polar PT1 ANG (/ DIST 2.0)))
(setq pt3 (polar PT2 (* pi 1.5) DIST))
(command ".line" pt2 pt3 "")
(setq pt1 (polar PT1 (* pi 1.5) DIST))
)
)
)
((= idx 30)
(STRING pt1)
(if (< idx dvs)
(progn
(setq PT2 (polar PT1 ANG (- (/ DIST 2.0))))
(setq pt3 (polar PT2 (* pi 1.5) DIST))
(command ".line" pt2 pt3 "")
(setq pt1 (polar PT1 (* pi 1.5) DIST))
)
)
)
)
)
(setvar "cmdecho" 1)
(princ)
)