TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: rus7755 on November 19, 2014, 02:46:21 PM

Title: Help for LISP beginner
Post by: rus7755 on November 19, 2014, 02:46:21 PM
I'm  somewhere beneath a rookie LISP programmer.  I was just try to write some short routines to reduce some extra clicking and to begin to learn something about LISP. 

What's wrong with this little routine written to simply rotate a selection set around a base point by 90 degrees?

(defun c:90()
   (SETQ SLT(SSGET))
   (SETQ PNT (GETPOINT "\nBASE POINT "))
       (command "ROTATE" SELECTION "" PNT "90")
   (SETQ SLT NIL)
)

;; Silent load.
(princ)


Thanks for the help,
rus7755
Title: Re: Help for LISP beginner
Post by: Keith™ on November 19, 2014, 02:54:12 PM
I'm  somewhere beneath a rookie LISP programmer.  I was just try to write some short routines to reduce some extra clicking and to begin to learn something about LISP. 

What's wrong with this little routine written to simply rotate a selection set around a base point by 90 degrees?

(defun c:90()
   (SETQ SLT(SSGET))
   (SETQ PNT (GETPOINT "\nBASE POINT "))
       (command "ROTATE" SELECTION "" PNT "90")
   (SETQ SLT NIL)
)

;; Silent load.
(princ)


Thanks for the help,
rus7755

You don't have the selection set correct in the rotate command. Also, it is good practice to use the language independent and internally defined commands where you are able.

Code: [Select]
(defun c:90()
   (SETQ SLT(SSGET))
   (SETQ PNT (GETPOINT "\nBASE POINT "))
       (command "._ROTATE" SLT "" PNT "90")
   (SETQ SLT NIL)
)

;; Silent load.
(princ)
Title: Re: Help for LISP beginner
Post by: rus7755 on November 19, 2014, 03:08:58 PM
Thanks for the reply Keith.

I tried the command again with the variable name corrected to "SLT", but it still fails when I pick the base point for rotation.  Do I have something wrong in the syntax for the GETPOINT command?


Thanks for the help,
rus7755
Title: Re: Help for LISP beginner
Post by: ronjonp on November 19, 2014, 03:11:21 PM
Also don't forget to localize variables ( set them to nothing )


Code - Auto/Visual Lisp: [Select]
  1. (defun c:90 (/ pnt slt);; <-- Localized variables after /
  2.   (if (and (setq slt (ssget)) (setq pnt (getpoint "\nBASE POINT ")))
  3.     (command "ROTATE" slt "" pnt "90")
  4.   )
  5.   (princ)
  6. )
Title: Re: Help for LISP beginner
Post by: BlackBox on November 19, 2014, 03:14:41 PM
... Or simply:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:90 (/ ss)
  2.   (if (setq ss (ssget "_:L"))
  3.     (command "._rotate" ss "" pause "90")
  4.   )
  5.   (princ)
  6. )
  7.  
Title: Re: Help for LISP beginner
Post by: dgorsman on November 19, 2014, 03:16:38 PM
Its also a good idea to provide readable command names, like PICKANDROTATE90.  Calling a command "90" isn't descriptive of the function and isn't very unique (every command must be a unique name).  If you want shorter commands, use the PGP file to assign a shortcut e.g. R90,    *PICKANDROTATE90
Title: Re: Help for LISP beginner
Post by: rus7755 on November 19, 2014, 03:27:57 PM
It still crashes when I pick the base point...

??????????


Thanks,
rus7755
Title: Re: Help for LISP beginner
Post by: Keith™ on November 19, 2014, 03:52:39 PM
Strange indeed ... I don't have AutoCAD installed at work so I cannot test your code, but it certainly appears correct.

I would verify that the code in your file is exactly as provided. Also, you do realize that you will need to reload the lisp each time you editing it.
Title: Re: Help for LISP beginner
Post by: ronjonp on November 19, 2014, 04:13:47 PM
It still crashes when I pick the base point...

 :? :? :? ?


Thanks,
rus7755
The one I posted works for me.
Title: Re: Help for LISP beginner
Post by: ChrisCarlson on November 19, 2014, 04:17:04 PM
BlackBox's assumes the item to be rotated was the last created entity.

But what is the error or crash message?
Title: Re: Help for LISP beginner
Post by: BlackBox on November 19, 2014, 04:20:38 PM
BlackBox's assumes the [user only wanted to rotate entities on unlocked layers].

FTFY :angel:
Title: Re: Help for LISP beginner
Post by: alanjt on November 19, 2014, 04:27:50 PM
Close enough to post for a quick mod...

Code: [Select]
(defun c:RR (/ ss pt)
  ;; Rotate selected object(s) 180°
  ;; Alan J. Thompson
  (if (and (princ "\nSelect object(s) to rotate 180°: ")
           (setq ss (ssget "_:L"))
           (setq pt (getpoint "\nSpecify rotation base point: "))
      )
    (command "_.rotate" ss "" "_non" pt 180.)
  )
  (princ)
)
Title: Re: Help for LISP beginner
Post by: rus7755 on November 19, 2014, 04:47:43 PM
alanjt,

I've been reloading my lisp routines through APPLOAD.  I'm assuming this brings in a fresh copy of the routine each time.  Is this true?

I ran the routine and it still behaved the same way  - bombing out after the base point is selected - started thinking it might not be loading the newest version of the routine.  Just not sure.

Also, can you explain this line:   ssget "_:L"

thanks,
rus7755
Title: Re: Help for LISP beginner
Post by: alanjt on November 19, 2014, 05:00:01 PM
What version are you running?

the "_:L" tells ssget to ignore objects on locked layers.
Title: Re: Help for LISP beginner
Post by: rus7755 on November 19, 2014, 05:09:33 PM
We're running AutoCAD 2014.
Title: Re: Help for LISP beginner
Post by: roy_043 on November 19, 2014, 06:20:06 PM
@  rus7755:
If your Lisp file contains multiple versions of c:90 the newest version should come last in the file.
Title: Re: Help for LISP beginner
Post by: owenwengerd on November 19, 2014, 06:41:58 PM
I ran the routine and it still behaved the same way  - bombing out after the base point is selected...

Phrases like "bombing out" are really not appropriate for a technical forum where precise descriptions are critical. You're more likely to resolve the problem if you copy and paste the exact error message, and include the exact steps you took that led up to the error.
Title: Re: Help for LISP beginner
Post by: BlackBox on November 19, 2014, 07:06:33 PM
1+

Always nice to see you at TheSwamp, Owen.
Title: Re: Help for LISP beginner
Post by: owenwengerd on November 19, 2014, 07:25:59 PM
Always nice to see you at TheSwamp, Owen.

Thanks BB. I lurk a lot, just don't jump into the fray much.
Title: Re: Help for LISP beginner
Post by: Kerry on November 19, 2014, 07:29:16 PM
1+

Always nice to see you at TheSwamp, Owen.

+ 12



I ran the routine and it still behaved the same way  -
< .. >
thanks,
rus7755

rus7755,

Which version did you load and run ?

The versions posted by Keith, Ron, Alan and BlackBox should all work.
Title: Re: Help for LISP beginner
Post by: Jeff H on November 19, 2014, 10:01:24 PM
I am a very green rookie of lisp but try loading selection and it will not bomb out.

Phrases like "bombing out" are really not appropriate for a technical forum where precise descriptions are critical. You're more likely to resolve the problem if you copy and paste the exact error message, and include the exact steps you took that led up to the error.
Or as Owen advised and trying to be more descriptive and focused.





(http://i1221.photobucket.com/albums/dd473/Jeffrey_H/loadselection_zps80b71aec.png)
Title: Re: Help for LISP beginner
Post by: Kerry on November 19, 2014, 10:15:07 PM
I am a very green rookie of lisp but try loading selection and it will not bomb out.

< .. >

I'd do a couple of additional things,

SAVE the file before you attempt to load and run a command.
   That way, if AutoCAD does lock up ( it's been known to happen ) you won't lose all your changes.

click DEBUG on the menu
   Toggle Break on Error to be true.
   If the routine does 'Bomb' ( nerdy technical term) you can select Debug->Last Break Source from the menu to see where the error was.

Check the code
   Menu Tools->Check code in editor
   This can save all sorts of grief.

Add a comment  hyperlink address to the code source.
Title: Re: Help for LISP beginner
Post by: ChrisCarlson on November 20, 2014, 07:54:51 AM
BlackBox's assumes the [user only wanted to rotate entities on unlocked layers].

FTFY :angel:

Ok now I feel like I jerk... I was reading it as _L
Title: Re: Help for LISP beginner
Post by: rus7755 on November 20, 2014, 08:21:18 AM
Finally figured it out - the file I was reloading with APPLOAD was not the correct one.

The code that alanjt submitted worked great and I learned a few things too.

This is a great resource!


Thanks for everybody's help,
rus7755
Title: Re: Help for LISP beginner
Post by: BlackBox on November 20, 2014, 08:29:32 AM
BlackBox's assumes the [user only wanted to rotate entities on unlocked layers].

FTFY :angel:

Ok now I feel like I jerk... I was reading it as _L

No worries; now you know the difference....

(http://cdn.churchm.ag/wp-content/uploads/2014/03/knowing-is-half-the-battle.jpg)

 :lol: