Author Topic: Function template for "shorties"  (Read 718 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10655
Function template for "shorties"
« on: April 18, 2023, 03:41:28 PM »
Backstory: Since I started back up with AutoCAD I am still trying to recreate some of my old tools; I used to have a nice set of keyboard shortcuts and I wanted to remake some of those.

I'm not sure if this is overkill or not :) but it is sort of useful, so I thought I'd share my monstrosity to get some discussion going. I did a write-up on bottom-up design which can be very useful and sometimes result in interesting designs and I think that inspired me to "stop and reevaluate" when I wanted to make a few "shortie" functions for drawing a line, pline, qleader, etc..

I'm not sure if I would call it "useful" for more than a few instances but, I think, it's kind of cool.

Thoughts?

Code - Auto/Visual Lisp: [Select]
  1. (defun define ( name cmd lst / functiondef )
  2.   ;; (define <NAME> <CMD> <LST>)
  3.   ;; Creates a lisp function for a `NAME` which has a built-in error trapping.
  4.   ;;
  5.   ;; I found myself wanting to create a more customized version of some built-in
  6.   ;; commands, like: line, pline, etc. where layer, osnap, orthomode changing was
  7.   ;; a little more automatic but the code to do this was very redundant (the same
  8.   ;; essential routine for each of these commands). I created this to offer myself
  9.   ;; an easy(er) way to create those functions which allows me to focus on the
  10.   ;; actual code I care about--and not the support functions.
  11.   ;;
  12.   ;; For example, the following will create a simple function to draw a line.
  13.   ;;    ;; -define a line command with no enviorment variables saved.
  14.   ;;    (define "bl" '(command "_.line") nil)
  15.   ;;
  16.   ;; This will create a function which looks like this:
  17.   ;;    (DEFUN C:BL nil
  18.   ;;      (EVAL (COMMAND "_.line"))
  19.   ;;      (WHILE (EQ 1 (LOGAND 1 (GETVAR (QUOTE CMDACTIVE)))) (COMMAND PAUSE))
  20.   ;;      (QUIT)
  21.   ;;    )
  22.   ;;
  23.   ;; Or using a more complicated example the following will produce more complicated code.
  24.   ;;
  25.   ;;   ;; -Define a line function that will adopt the entities layer under the picked point.
  26.   ;;   (define
  27.   ;;     "b"                                                      ;; -name
  28.   ;;     '((lambda (pt)                                           ;; -command to run
  29.   ;;         (setvar 'CLAYER (getlayerfrompickedpoint pt))        ;; -using custom function to get layer
  30.   ;;         (command "_.line" pt) )
  31.   ;;       (getpoint "\nSelect Point: ") )
  32.   ;;     '(("CLAYER"))                                            ;; -Variables to save/restore
  33.   ;;     )
  34.   ;;
  35.   ;; This will create a function which looks like this:
  36.   ;;    (DEFUN C:B (/ *ERROR* *ERROR-PUSH-VARS*)
  37.   ;;      (DEFUN *ERROR* (MSG) (MAPCAR (QUOTE EVAL) #ERROR_LST#) (SETQ #ERROR_LST# nil))
  38.   ;;      (DEFUN *ERROR-PUSH-VARS* (LST / PUSH->LST)
  39.   ;;        (DEFUN PUSH->LST (SYM LST) (SET LST (CONS SYM (EVAL LST))))
  40.   ;;        (MAPCAR
  41.   ;;          (FUNCTION
  42.   ;;            (LAMBDA (X)
  43.   ;;              (PUSH->LST
  44.   ;;                (LIST (QUOTE SETVAR) (CAR X) (GETVAR (CAR X)))
  45.   ;;                (QUOTE #ERROR_LST#))
  46.   ;;              (IF (CADR X) (SETVAR (CAR X) (CADR X)))))
  47.   ;;          LST
  48.   ;;        )
  49.   ;;      )
  50.   ;;      (*ERROR-PUSH-VARS* (QUOTE (("CLAYER"))))
  51.   ;;      (EVAL
  52.   ;;        ((LAMBDA (PT)
  53.   ;;           (SETVAR (QUOTE CLAYER) (GETLAYERFROMPICKEDPOINT PT))
  54.   ;;           (COMMAND "_.line" PT))
  55.   ;;          (GETPOINT "\nSelect Point: ")))
  56.   ;;      (WHILE (EQ 1 (LOGAND 1 (GETVAR (QUOTE CMDACTIVE)))) (COMMAND PAUSE))
  57.   ;;      (QUIT)
  58.   ;;    )
  59.   (setq functiondef (list 'defun (read (strcat "c:" name))))
  60.   (if lst
  61.     (setq functiondef (append functiondef
  62.             (list (list '/ '*error* '*error-push-vars*))
  63.             (list (list 'defun '*error* (list 'msg) (list 'mapcar (quote 'eval) '#error_lst#) (list 'setq '#error_lst# 'nil)))
  64.             (list (list 'defun '*error-push-vars* (list 'lst '/ 'push->lst)
  65.                   (list 'defun 'push->lst (list 'sym 'lst)
  66.                         (list 'set 'lst
  67.                               (list 'cons
  68.                                     'sym
  69.                                     (list
  70.                                       'eval
  71.                                       'lst)))) ;end defun push->lst
  72.                   (list
  73.                     'mapcar
  74.                     (list
  75.                       'function
  76.                       (list
  77.                         'lambda (list 'x)
  78.                         (list 'push->lst (list 'list (quote 'setvar) (list 'car 'x) (list 'getvar (list 'car 'x))) (quote '#error_lst#))
  79.                         (list 'if (list 'cadr 'x)
  80.                               (list 'setvar (list 'car 'x) (list 'cadr 'x)))))
  81.                     'lst)
  82.                   ))
  83.             (list (list '*error-push-vars* (list 'quote lst)))
  84.             )
  85.       ) ; end setq
  86.     (setq functiondef (append functiondef (list 'nil)))
  87.    ) ; end if
  88.   (setq functiondef (append functiondef
  89.     (list (list 'eval cmd)
  90.     (list 'while (list 'eq '1 (list 'logand '1 (list 'getvar (quote 'CMDACTIVE))))
  91.           (list 'command 'PAUSE)))
  92.     (list (list 'quit))
  93.     )
  94.   ) ; end setq
  95.   (eval functiondef)
  96. )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

BIGAL

  • Swamp Rat
  • Posts: 1423
  • 40 + years of using Autocad
Re: Function template for "shorties"
« Reply #1 on: April 19, 2023, 09:44:33 PM »
This is sort of like something I did which returns object properties, but based on what you want not the full amount of properties.

eg circle so the call would be (prop obj (list "rad" "layer"))
eg circle so the call would be (prop obj (list "cen" "rad"))
eg circle so the call would be (prop obj (list "cen"))
eg pline (prop obj "pts")
eg pline (prop obj "area")
and so on combinations in any order as each item in list is returned.

The variable values returned are plain english like Layer Area Color Rad Length co-ords and so on.
A man who never made a mistake never made anything