Author Topic: Set dimscale by selecting from dropdown list  (Read 2872 times)

0 Members and 1 Guest are viewing this topic.

jlogan02

  • Bull Frog
  • Posts: 327
Set dimscale by selecting from dropdown list
« on: December 22, 2020, 03:14:15 PM »
This is part of a larger routine. A dialog box would launch for the user to pick scale of the drawing.
As a test I created this routine to set the dimscale based on the selection but get this error.

Error: AutoCAD variable setting rejected: DIMSCALE ("Full" "6\" = 1'-0\"" "3\" = 1'-0\"" "1-1/2\" = 1'-0\"" "1\" = 1'-0\"" "3/4\" = 1'-0\"" "1/2\" = 1'-0\"" "1/4\" = 1'-0\"")

I'm not getting the dismcale or associating it with the user choice from the list, is my guess. Not sure how to do that.

Ultimately though, I'd like my dropdown list to show drawing scales for the user but in the routine set the dimscale based on the user selection...

Code - Auto/Visual Lisp: [Select]
  1. (defun C:DScale ()                                      ;define function       
  2.  
  3.   (setq siz "1")                                        ;preset hole size
  4.  
  5.  ;;(setq dimsc (setvar 'dimscale)
  6.  
  7.  (setq NAMES '("6\" = 1'-0\"" "3\" = 1'-0\"" "1-1/2\" = 1'-0\"" "1\" = 1'-0\"" "3/4\" = 1'-0\"" "1/2\" = 1'-0\"" "1/4\" = 1'-0\"")     
  8.    );;;When user selects from the list it should convert that to the dimscale equivalent 1, 2, 4, 8, 12, 16, 24, or 48
  9.  
  10.   (setq dcl_id (load_dialog "Dscale.dcl"))              ;load dialog
  11.   (if (not (new_dialog "Dscale" dcl_id)                 ;test for dialog
  12.  
  13.       );not
  14.  
  15.     (exit)                                              ;exit if no dialog
  16.  
  17.   );if
  18.  
  19.    (start_list "selections")                            ;start the list box
  20.   (mapcar 'add_list NAMES)                              ;fill the list box
  21.   (end_list)                                            ;end list
  22.  
  23.     "cancel"                                            ;if cancel button pressed
  24.     "(done_dialog) (setq userclick nil)"                ;close dialog, set flag
  25.     );action_tile
  26.  
  27.     "accept"                                            ;if O.K. pressed
  28.     (strcat                                             ;string 'em together
  29.       "(progn
  30.        (setq SIZ (atof (get_tile \"selections\")))"     ;get list selection
  31.       " (done_dialog)(setq userclick T))"               ;close dialog, set flag
  32.     );strcat
  33.   );action tile
  34.  
  35.   (start_dialog)                                        ;start dialog
  36.  
  37.   (unload_dialog dcl_id)                                ;unload
  38.  
  39.    (if userclick                                        ;check O.K. was selected
  40.     (progn
  41.  
  42.       (setq SIZ (fix SIZ))                              ;convert to integer
  43.       (setvar 'dimscale NAMES)       ;;;thought it was as easy as this....Not so because I haven't associated the pick from the list with dimscale.
  44.          
  45.     );progn
  46.  
  47.   );if userclick
  48.  
  49.   (princ)
  50.  
  51. );defun C:samp
  52.  
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

Rustabout

  • Newt
  • Posts: 135
Re: Set dimscale by selecting from dropdown list
« Reply #1 on: December 22, 2020, 11:03:51 PM »
I'll try to help (likely better help on the way so bear with me). Caveat being I'm at 2 beers after a long day.

atof: As I've read your code atof isn't going to give you the result you desire. For example, plug (atof "6\" = 1'-0\"") into your console (it returns 6.0). Since you have a fixed number of scales you could get away with using 'cond' here. I see that your code is very similar to Afralisp.net's (amazing site!! I wish that guy also had tutorials on doing my taxes). I think he gets away with his code due to the leading integer value of each beam size being unique (not sure if I'm right here I'd have to look more).

A quick tip on dialog boxes: Although it's not necessary, and you added the 'progn' to your action_tile (very smart!!), pulling as much code as possible into a sub routine cures a lot of headaches (taking advantage of the (setq userClick T) part of the code).

So... finally, while typing I found the source of your error: You are trying to input your 'NAMES' list into the variable setting for dimscale (instead of SIZ?). Hence the nature of the error message. Wish I caught that 2 minutes ago ;-). I'll leave the rest of my post in case it's helpful.

jlogan02

  • Bull Frog
  • Posts: 327
Re: Set dimscale by selecting from dropdown list
« Reply #2 on: December 23, 2020, 03:14:01 PM »
I'll try to help (likely better help on the way so bear with me). Caveat being I'm at 2 beers after a long day.

atof: As I've read your code atof isn't going to give you the result you desire. For example, plug (atof "6\" = 1'-0\"") into your console (it returns 6.0). Since you have a fixed number of scales you could get away with using 'cond' here. I see that your code is very similar to Afralisp.net's (amazing site!! I wish that guy also had tutorials on doing my taxes). I think he gets away with his code due to the leading integer value of each beam size being unique (not sure if I'm right here I'd have to look more).

A quick tip on dialog boxes: Although it's not necessary, and you added the 'progn' to your action_tile (very smart!!), pulling as much code as possible into a sub routine cures a lot of headaches (taking advantage of the (setq userClick T) part of the code).

So... finally, while typing I found the source of your error: You are trying to input your 'NAMES' list into the variable setting for dimscale (instead of SIZ?). Hence the nature of the error message. Wish I caught that 2 minutes ago ;-). I'll leave the rest of my post in case it's helpful.

My code is literally Kenny's code stripped down to the part I needed for a start. Good eye!

atof ?? are you saying it should be cond here in lieu of atof???

Code - Auto/Visual Lisp: [Select]
  1.     "accept"                                            ;if O.K. pressed
  2.     (strcat                                             ;string 'em together
  3.       "(progn
  4.        (setq SIZ (atof (get_tile \"selections\")))"     ;get list selection
  5.       " (done_dialog)(setq userclick T))"               ;close dialog, set flag
  6.     );strcat
  7.   );action tile


I can't take credit for the progn on the action tile, that's all Kenny R.

Your point about "pulling as much code into a sub routine" Are you saying to put this...

Code - Auto/Visual Lisp: [Select]
  1. (setq siz "1")
  2.  
  3. ;;(setq siz (getvar 'dimscale))
  4.  
  5.   (cond
  6.  
  7.     (setq NAMES '("1" "6\" = 1'-0\"" "3\" = 1'-0\"" "1-1/2\" = 1'-0\"" "1\" = 1'-0\"" "3/4\" = 1'-0\"" "1/2\" = 1'-0\"" "1/4\" = 1'-0\""))      ;define list should be equal to DIMSCALE ("1" "2" "4" "8" "12" "16" "24" "48")
  8.   )
under the userclick and above the (progn  ???

Yes, the NAMES! That makes sense. Made that change, which eliminated the error message but gives me...

; error: Exception occurred: 0xC0000005 (Access Violation)

the second I pick another choice from the dropdown.

When I ok out of the dialog the dimscale does get changed but it almost seems as if it's random or it's counting or summing something. It changes but not to the desired dimscale.

J. Logan
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

Rustabout

  • Newt
  • Posts: 135
Re: Set dimscale by selecting from dropdown list
« Reply #3 on: December 23, 2020, 11:48:13 PM »
I gave some bad advice there regarding 'cond' or rather didn't explain how to use it:

EDIT: I forgot that list boxes don't output a string but rather a numerical value in string format ("0", "1") of the user's selection. I *think* the list with start with '0' you'll have to verify though. That was Kenny's reason for using atof but you can use cond instead. He converts the value returned by 'get_tile' from the list box into an integer so that it can be used with the 'nth' function on the same list used to fill the list box. Cond will work just fine for shorter lists but is a lot of code for longer lists.

  (cond
   ((= SIZ "0") (setvar "dimscale" 1)) ;;; for "1\"
   ((= SIZ "1") (setvar "dimscale" 2))  ;;; for "6\" = 1'-0\""
   ((= SIZ "2") (setvar "dimscale" 4)) ;;; for "3\" = 1'-0\""
   ((= SIZ "3") (setvar "dimscale" 8))
   ((= SIZ "4") (setvar "dimscale" 12))
   ((= SIZ "5") (setvar "dimscale" 16))
   ((= SIZ "6") (setvar "dimscale" 24))
   ((= SIZ "7") (setvar "dimscale" 48))
  )

Note that I never use dimscale (I use annotative stuff) so make sure you enter the numbers you actually want in the variables. Remember that with simple lines of code (and even not-so-simple lines of code) you can plug your code, line by line (or function by function, just double-lick the parenthesis to highlight the function, into the visual LISP console and test it out.
test out (atof "6\" = 1'-0\"") in your visual LISP console window (or even the command line within AutoCAD itself) and you'll see what I mean. ATOF cannot convert a text string that contains characters other than 0123456789 "." and maybe comma's but I'd have to test that out.
Cond is really just a better, more powerful "if" function.

See this link on Afralisp for cond (near the end of the tutorial: https://www.afralisp.net/autolisp/tutorials/conditionals.php

And Autodesk help on ATOF: https://documentation.help/AutoLISP-Functions/WS1a9193826455f5ff1a32d8d10ebc6b7ccc-6aa0.htm

Sub functions: Maybe shove that part of my last post aside for now. I should have deleted that in hindsight. The only reason I mentioned that is because it might come up in the future where you can't execute a certain function from within the action_tile itself, or while the dialog is loaded; you have to close the dialog. But you can always set a 'flag' and have that trigger something after the dialog is unloaded. Here's afralisp's page on the 'define function function':

https://www.afralisp.net/autolisp/tutorials/the-define-function.php

See if you can get that cond to work if not post your code and I'll actually have a go at it.




       
« Last Edit: December 24, 2020, 01:03:42 AM by Rustabout »

jlogan02

  • Bull Frog
  • Posts: 327
Re: Set dimscale by selecting from dropdown list
« Reply #4 on: January 04, 2021, 06:22:59 PM »
Back after a holiday break. I've got this working.

I'm still getting...
; error: Exception occurred: 0xC0000005 (Access Violation)... Even though the dimscale is being set from the dialog pick.


Code - Auto/Visual Lisp: [Select]
  1. (defun C:DScaletmp ( / names siz )                      ;define function        
  2.  
  3.   (setq SIZ "1")                                        
  4.   (setq NAMES '("1" "6\" = 1'-0\"" "3\" = 1'-0\"" "1-1/2\" = 1'-0\"" "1\" = 1'-0\"" "3/4\" = 1'-0\"" "1/2\" = 1'-0\"" "1/4\" = 1'-0\"") )
  5.  
  6.   (setq dcl_id (load_dialog "Dscaletmp.dcl"))           ;load dialog
  7.   (if (not (new_dialog "Dscaletmp" dcl_id)              ;test for dialog
  8.  
  9.       );not
  10.  
  11.     (exit)                                              ;exit if no dialog
  12.  
  13.   );if
  14.  
  15.   (start_list "selections")                             ;start the list box
  16.   (mapcar 'add_list NAMES)                              ;fill the list box
  17.   (end_list)                                            ;end list
  18.  
  19.     "cancel"                                            ;if cancel button pressed
  20.     "(done_dialog) (setq userclick nil)"                ;close dialog, set flag
  21.     )                                                   ;action_tile
  22.  
  23.     "accept"                                            ;if O.K. pressed
  24.     (strcat                                             ;string 'em together
  25.       "(progn
  26.         (setq SIZ (get_tile \"selections\")))"          ;get list selection
  27.       " (done_dialog)(setq userclick T))"               ;close dialog, set flag
  28.     )                                                   ;strcat
  29.   )                                                     ;action tile
  30.  
  31.   (start_dialog)                                        ;start dialog
  32.  
  33.   (unload_dialog dcl_id)                                ;unload
  34.  
  35.    (if userclick                                        ;check O.K. was selected
  36.      (progn
  37.        (cond
  38.         ((= SIZ "0") (setvar "dimscale" 1))
  39.         ((= SIZ "1") (setvar "dimscale" 2))
  40.         ((= SIZ "2") (setvar "dimscale" 4))
  41.         ((= SIZ "3") (setvar "dimscale" 8))
  42.         ((= SIZ "4") (setvar "dimscale" 12))
  43.         ((= SIZ "5") (setvar "dimscale" 16))
  44.         ((= SIZ "6") (setvar "dimscale" 24))
  45.         ((= SIZ "7") (setvar "dimscale" 48))
  46.        )
  47.      )                                                  ;progn
  48.    )                                                    ;if userclick
  49.  (princ)
  50. )                                                       ;defun
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

BIGAL

  • Swamp Rat
  • Posts: 1398
  • 40 + years of using Autocad
Re: Set dimscale by selecting from dropdown list
« Reply #5 on: January 04, 2021, 11:14:45 PM »
If you want a radio buttons versions this only needs like 3 lines of code to work as its a Library function makes DCL on the fly from you supplied list.

There are examples in code.

(if (not AH:Butts)(load "Multi Radio buttons.lsp"))
(if (not but)(setq but 1))
(setq ans (ah:butts but "h"  '("Choose scale " "1:1" "1:4" "1:8" "1:10"))) ; add more scales as required h is horizontal use V for vertical

ans holds same as list or you can use variable BUT which is what button number was pressed.
(ah:butts but "h"  '("Choose scale " "1:1" "1:4" "1:8" "1:10"))

See image

A man who never made a mistake never made anything

jlogan02

  • Bull Frog
  • Posts: 327
Re: Set dimscale by selecting from dropdown list
« Reply #6 on: January 05, 2021, 11:36:56 AM »
If you want a radio buttons versions this only needs like 3 lines of code to work as its a Library function makes DCL on the fly from you supplied list.

There are examples in code.

(if (not AH:Butts)(load "Multi Radio buttons.lsp"))
(if (not but)(setq but 1))
(setq ans (ah:butts but "h"  '("Choose scale " "1:1" "1:4" "1:8" "1:10"))) ; add more scales as required h is horizontal use V for vertical

ans holds same as list or you can use variable BUT which is what button number was pressed.
(ah:butts but "h"  '("Choose scale " "1:1" "1:4" "1:8" "1:10"))

See image



Huh!!! Now that's interesting!

So, is this a sub-function of the main lisp or is this a dcl file?

I'm knee deep into the code I have (read: I've put enough time into it, I'd like to see it work) so I'm going to forge ahead with it. I'm definitely going to look at this method though.

Thanks for that tip.
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

jlogan02

  • Bull Frog
  • Posts: 327
Re: Set dimscale by selecting from dropdown list
« Reply #7 on: January 05, 2021, 12:32:59 PM »
Hear is my latest attempt added into a larger piece of code.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:InsTB ( / blk1 dimsc dss dsty dstydata n scl_fact sname )
  2.    (setq SIZ "1")                                        
  3.   (setq NAMES '("1" "6\" = 1'-0\"" "3\" = 1'-0\"" "1-1/2\" = 1'-0\"" "1\" = 1'-0\"" "3/4\" = 1'-0\"" "1/2\" = 1'-0\"" "1/4\" = 1'-0\"") )
  4.  
  5.   (setq dcl_id (load_dialog "Dscaletmp.dcl"))           ;load dialog
  6.  
  7.   (if (not (new_dialog "Dscaletmp" dcl_id)              ;test for dialog
  8.        )                                                ;not
  9.        (exit)                                           ;exit if no dialog
  10.   )                                                     ;if
  11.  
  12.   (start_list "selections")                             ;start the list box
  13.   (mapcar 'add_list NAMES)                              ;fill the list box
  14.   (end_list)                                            ;end list
  15.  
  16.     "cancel"                                            ;if cancel button pressed
  17.     "(done_dialog) (setq userclick nil)"                ;close dialog, set flag
  18.     )                                                   ;action_tile
  19.  
  20.     "accept"                                            ;if O.K. pressed
  21.     (strcat                                             ;string 'em together
  22.       "(progn
  23.         (setq SIZ (get_tile \"selections\")))"          ;get list selection
  24.       " (done_dialog)(setq userclick T))"               ;close dialog, set flag
  25.     )                                                   ;strcat
  26.   )                                                     ;action tile
  27.  
  28.   (start_dialog)                                        ;start dialog
  29.  
  30.   (unload_dialog dcl_id)                                ;unload
  31.  
  32.    (if userclick                                        ;check O.K. was selected
  33.      (progn
  34.        (cond
  35.         ((= SIZ "0") (setvar "dimscale" 1))
  36.         ((= SIZ "1") (setvar "dimscale" 2))
  37.         ((= SIZ "2") (setvar "dimscale" 4))
  38.         ((= SIZ "3") (setvar "dimscale" 8))
  39.         ((= SIZ "4") (setvar "dimscale" 12))
  40.         ((= SIZ "5") (setvar "dimscale" 16))
  41.         ((= SIZ "6") (setvar "dimscale" 24))
  42.         ((= SIZ "7") (setvar "dimscale" 48))
  43.        )
  44.      )                                                  ;progn
  45.    )
  46.  
  47.   (setq dimsc (getvar 'dimscale))
  48.   (command "-layer" "unlock" "TBLK_BORD_LINES" "")
  49.   (if (setq blk1 (ssget "x" '((2 . "TBLK_BORD_CTL,TBLK_ATT_CTL"))))
  50.         (command "scale" blk1 "" "0,0" dimsc)
  51.    )
  52.  
  53.    (setvar 'dynmode 0)
  54.    (while (setq dsty (tblnext "dimstyle" (not dsty)))
  55.    (setq dstydata (entget (tblobjname "dimstyle" (cdr (assoc 2 dsty)))))
  56.    (if (assoc 40 dstydata)                                                      ; [because not included in data if default 1.0]
  57.        (entmod (subst (cons 40 dimsc) (assoc 40 dstydata) dstydata))            ; then -- replace it
  58.        (entmod (append dstydata (list (cons 40 dimsc))))                        ; else -- add it
  59.     )
  60.   )

Still getting this error...; error: Exception occurred: 0xC0000005 (Access Violation)...
To be fair I haven't change anything.
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

BIGAL

  • Swamp Rat
  • Posts: 1398
  • 40 + years of using Autocad
Re: Set dimscale by selecting from dropdown list
« Reply #8 on: January 05, 2021, 11:29:29 PM »
It is a library defun so no need to live in code, I have it load on startup, I wrote 3 versions radio buttons, toggle and Getvals I write everything now even down to 1 line input for a value I just find easier to type into a dcl in centre of screen . Go to Cadtutor downloads for the others, RLX has a multi toggle library function that allows for a grid style dcl where you have a lot of choices. Again it writes the dcl to a temporary file.

Instead of a initget Yes No I use radio button with Yes No.

The text is not recognising the inch " \" as I work metric never a problem will have a think about it not sure why not when passing the text to dcl.

Code: [Select]
(if (not AH:Butts)(load "Multi Radio buttons.lsp"))
(if (not but)(setq but 1))
(setq NAMES (list "Please choose scale" "1" "6 = 1'-0"  "3 = 1'-0" "1-1/2 = 1'-0" "1    = 1'-0"  "3/4 = 1'-0"  "1/2 = 1'-0" "1/4 = 1'-0"))
(ah:butts but "v"  names )
(Alert (strcat "button selected was " (rtos but 2 0)))

Code: [Select]
change this to get double spacing
;(if (or (= numch nil) (< numch 8))
(write-line "spacer_1 ;" fo)
;)
« Last Edit: January 06, 2021, 12:09:36 AM by BIGAL »
A man who never made a mistake never made anything

Rustabout

  • Newt
  • Posts: 135
Re: Set dimscale by selecting from dropdown list
« Reply #9 on: January 10, 2021, 12:19:50 PM »
Do you know which line of code the error occurs at? Sometimes, if you're (VERY) lucky, the error doesn't occur when VLIDE is closed. For example, you might have the DCL file open in the editor.

Code is coming along nicely!! Great work!

jlogan02

  • Bull Frog
  • Posts: 327
Re: Set dimscale by selecting from dropdown list
« Reply #10 on: January 12, 2021, 10:59:35 AM »
Do you know which line of code the error occurs at? Sometimes, if you're (VERY) lucky, the error doesn't occur when VLIDE is closed. For example, you might have the DCL file open in the editor.

Code is coming along nicely!! Great work!

I haven't gone through it line by line.
That's my next step. Tried running with Vlide closed but that didn't work.

I'll keep plugging. It's close cause of your help, thanks.
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

jlogan02

  • Bull Frog
  • Posts: 327
Re: Set dimscale by selecting from dropdown list
« Reply #11 on: January 26, 2021, 11:42:43 AM »
Just doubling back here and saying thanks for all the help. I chose to go the DCL route and all seems to be working fine.

Thanks again.

J.Logan
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10