Add forward slashes to clickable options in custom command

Add forward slashes to clickable options in custom command

darinrUQ882
Explorer Explorer
656 Views
8 Replies
Message 1 of 9

Add forward slashes to clickable options in custom command

darinrUQ882
Explorer
Explorer

Hello all,

I'm creating a new command for users at my company to insert blocks by. I've got the command working fine for now, but I'm using prompts such as

What size? <5/16" 3/8" 1/2" 5/8">:
I would like to change those static options over to clickable ones by changing the angle brackets to square brackets and separating the options with forward slashes, which is where I run into some trouble. I've tried escaping the forward slashes (\/), doubling them (//) and using concatenation and character codes ((strcat "5" (chr 47) "16\"")), all with no luck. I've even tried using unicode char 824 (the long solidus) and a space or unicode char 8260 (the actual fraction slash) with either strcat or just directly in the prompt, but that only returns the numerator, e.g. "5" or "1", which won't work because several of the options have the same numerator. I know I can't use the initget and getkword functions, because you can't have forward slashes in the keywords.

 

Does anyone know of a way to push forward slashes through to the command line without them being intercepted and turned into dividers for the clickable options? Or perhaps there's a way to return a different value when clicked than is displayed? I would be fine with the command returning something like "A" "B" or "C", but I'm unaware of how to do that. The only resource I've been able to locate on this function was a remarkably short blog post about it whose URL is now buried in my history.

 

Thanks.

0 Likes
657 Views
8 Replies
Replies (8)
Message 2 of 9

ВeekeeCZ
Consultant
Consultant

AFAIK, there is no trick. 

 

Probably the best workaround would be to use a dash.

(progn (initget "5-6 3-8 1-2 5-8") (getkword "What size? [5-6\"/3-8\"/1-2\"/5-8\"]: "))

0 Likes
Message 3 of 9

CodeDing
Advisor
Advisor

@darinrUQ882 ,

 

You could use the Unicode "Fraction Slash" (chr 8260) to inject into your selection string, but it's more work to identify which option the user selected. But it works:

(progn
  (setq slash (chr 8260))
  (setq fractions
    (list '(5 16) '(3 8) '(1 2) '(5 8))
  )
  (setq iStr (substr (apply 'strcat (mapcar '(lambda (f) (strcat " " (itoa (car f)) slash (itoa (cadr f)))) fractions)) 2))
  (setq gStr (substr (apply 'strcat (mapcar '(lambda (f) (strcat "/" (itoa (car f)) slash (itoa (cadr f)))) fractions)) 2))
  (initget iStr)
  (getkword (strcat "What size? [" gStr "]: "))
)

 

Unicode "Fraction Slash" symbol found in "Unicode Symbols" table, here:

https://en.wikipedia.org/wiki/List_of_Unicode_characters#Table_Unicode_symbols 

 

image.png

image.png

 

Best,

~DD

0 Likes
Message 4 of 9

darinrUQ882
Explorer
Explorer

While that appears to be a somewhat elegant approach, it still returns only the numerator if the user clicks on one of the ambiguous ones. Interestingly, it does return the entire string when the user clicks one "1/2". I do appreciate the effort, however.

0 Likes
Message 5 of 9

paullimapa
Mentor
Mentor

How’s about selecting these options from a dialog box?


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 6 of 9

darinrUQ882
Explorer
Explorer

I'm trying to avoid dialogs for most of the commands I'm making, in order to reduce overhead. The machines my team works on aren't the greatest, and I've gotten noticeable results from little hacky optimization tricks I normally wouldn't bother doing. Besides, I hate DCL with a passion that burns only slightly less bright than my hatred for VBA. (that's a joke)

 

If it's not possible to do, that's fine, it's just strange to me that Autocad would have this feature, and then exclude something so obvious as fractions from being used in it.

0 Likes
Message 7 of 9

paullimapa
Mentor
Mentor

I think it was an oversight on Autodesk to implement the slash as a separator for this  type of command line selection since it does not accommodate for imperial unit fractions as you’ve demonstrated. 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 8 of 9

CodeDing
Advisor
Advisor

@darinrUQ882 ,

 


@darinrUQ882 wrote:

it still returns only the numerator if the user clicks on one of the ambiguous ones


I could not duplicate that behavior.

 


@darinrUQ882 wrote:

Or perhaps there's a way to return a different value when clicked than is displayed? I would be fine with the command returning something like "A" "B" or "C", but I'm unaware of how to do that.


For this approach, we could try this:

(defun c:TEST ( / fractions n)
  (setq fractions
    '((5 16) (3 8) (1 2) (5 8))
  )
  (setq fractions
    (mapcar
     '(lambda (f)
        (strcat (itoa (car f)) (chr 8260) (itoa (cadr f)))
      )
      fractions
    )
  )
  ;; StringReturn MUST return a value, only time it will not is if user cancelled function
  (setq n (StringReturn fractions))
  (alert (princ (strcat "\nUser Selected: " (nth n fractions))))
  (princ)
)


;; Created by Denon Deterding, 13 Nov 2018
;; strlist - list, of strings for user to select from
;; returns - int, user selected string as nth from strList (0 or 1 or 2 etc...)
(defun StringReturn (strList / tmp len promptStr initStr str#)
  (cond
    ;; return nil if no list provided, or list is empty
    ((not (listp strList)) (prompt "\nList not provided to function.") (setq str# nil));cond 1
    ((not (> (length strList) 0)) (prompt "\nEmpty list provided to function.") (setq str# nil));cond 2
    ;; otherwise begin work
    (t
      (setq len (length strList) initStr "1" promptStr "" tmp 0)
      ;; if more than one item in list, create initStr values
      (if (> len 1) (progn (repeat (- len 1) (setq initStr (strcat initStr " " (itoa (+ tmp 2))) tmp (1+ tmp))) (setq tmp 0)))
      ;; Create prompt string defaults for user
      (repeat len
        (setq promptStr (strcat promptStr (itoa (+ 1 tmp)) " ... " (nth tmp strList)) tmp (1+ tmp))
        (if (not (= tmp len)) (setq promptStr (strcat promptStr "/")))
      );repeat
      ;; use initStr for easy user selection
      (initget initStr 1)
      ;; convert user response to number correlating to nth in sizeList
      (setq str# (- (atoi (getkword (strcat "\nSelect item [" promptStr "]: "))) 1))
    );cond 3
  );cond
  ;; return nth selection
  str#
);defun

 

Looks like:

image.png

image.png

 

Best,

~DD

0 Likes
Message 9 of 9

Sea-Haven
Mentor
Mentor

This will do 1/16 3/8 etc. You can include all the dcl code in your lisp or just use the 3 lines as shown. Up to about 20 buttons.

SeaHaven_0-1747709125466.png

 

if (not AH:Butts)(load "Multi Radio buttons.lsp"))
(if (= but nil)(setq but 1))
(setq ans (ah:butts but "V" '("Choose a fraction" "1/16" "1/8" "3/16" "1/4" "5/16"))) ; ans holds the button picked 

 

Hidden is also the button number can be used 1/16=button 1. Just save Multi radio buttons.lsp to a support path so it autoloads or add full path to (load. The multi radio buttons works in any lisp code. I autoload on startup so it can be found.

0 Likes