Jump to content

Dialog problem with Toggle


woodman78

Recommended Posts

I am trying to write a lisp with a dialog to create a new layer. The layer dialog box is slow in my machine and I hate having to wait for it to load. I want to create a dialog box instead of using the command line. Mainly just for the challenge. But I have been reading about how to code a toggle and I can't seem to get it to work.

 

This is what I have so far. Everything works fine except the toggle.

layermakerccc: dialog { label = "Layer Creator";
 : edit_box { label = "Enter layer name :"; key = "name"; alignment = centered; edit_limit = 45; edit_width = 50; }
 : toggle {key = "tg1"; value = 1; label = "Set colour to be BYLAYER";}
 ok_only;
}

 

(defun c:layermakerccc ( / *error* elast id lst name old vars answer1 )

 (defun *error* ( msg )
   (if (< 0 id) (unload_dialog id))
   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
       (princ (strcat "\n** Error: " msg " **")))
   (princ)
 )
 (cond
   ( (or
       (<= (setq id (load_dialog "layermakerccc.dcl")) 0)
       (not (new_dialog "layermakerccc" id))
     )
     (princ "\n--> Error Loading Dialog.")
   )
   (t
     (setq *name1* (set_tile "name" (cond (*name1*) ("Default"))))
     (action_tile "name" "(setq *name1* $value)")
     (action_tile "tg1" "(setq answer1 $value)")
    
     (if
       (and
         (= 1 (start_dialog))
         (setq lst (acad_colordlg 1 nil))
       )
       (progn
         (setq name *name1*)

         (if (null (tblsearch "LAYER" name))
           (entmake
             (list
               (cons 0 "LAYER")
               (cons 100 "AcDbSymbolTableRecord")
               (cons 100 "AcDbLayerTableRecord")
               (cons 2 name)
     (cons 62 lst)
               (cons 70 0)
             )
           )
         )
         (setvar 'CLAYER name)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(if (answer1 = 1)
(progn
   (setvar "cecolor" "Bylayer")  
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
)
)
)
 )
 (if (< 0 id) (unload_dialog id))
 (mapcar 'setvar vars old)
 (princ)
)

 

Can someone point me in the right direction?

 

Thanks.

Link to comment
Share on other sites

The $value returned by a dialog action is always a string. So your check of (which should have read (= answer1 1)) won't ever happen. It should rather be:

(= answer1 "1")

 

Or preferably: (eq answer1 "1")

 

Or alternatively: (= (read answer1) 1)

Link to comment
Share on other sites

Sorry then I'm not seeing a problem with your code. Could you perhaps attach the DCL file? Maybe there's some "error" in that? That is if by the ByLayer check you want the colour to be set to ByLayer after the command completes.

 

Or do you mean you want the colour dialog not to display? In that case you'd need to modify the if condition regarding the acad_colordlg line. E.g. change the (setq lst (acad_colordlg 1 nil)) line to something like this:

(cond
 ((eq answer1 "1") 256)
 ((acad_colordlg 1 nil))
)

 

Edit: sorry scratch that - I'm being stupid! :oops: How would you set a layer's colour to bylayer? :shock: Not to mention, you've already given the DCL :wacko:

Link to comment
Share on other sites

Ok, just went through the whole thing. You're not initializing the answer1 variable. The action only happens when the user clicks on the toggle, so the default checked value is never shown. You have two options, either check for (not (eq answer1 "0")) or initialize it to "1" prior to starting the dialog. Here's some mods of your code, maked in green.

(defun c:layermakerccc (/ *error* elast id lst name old vars answer1 [color=green]load-layermakerccc[/color])
 [color=green](defun load-layermakerccc (/ fn f)
   (setq fn (strcat (getvar "TEMPPREFIX") "layermakerccc.dcl")
         f  (open fn "w")
   )
   (princ "layermakerccc: dialog { label = \"Layer Creator\";\n\r" f)
   (princ
     "  : edit_box { label = \"Enter layer name :\"; key = \"name\"; alignment = centered; edit_limit = 45; edit_width = 50; }\n\r"
     f
   )
   (princ "  : toggle {key = \"tg1\"; value = 1; label = \"Set colour to be BYLAYER\";}\n\r" f)
   (princ "  ok_only;\n\r" f)
   (princ "}\n\r" f)
   (close f)
   (load_dialog fn)
 )[/color]

 (defun *error* (msg)
   (if (< 0 id)
     (unload_dialog id)
   )
   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
       (princ (strcat "\n** Error: " msg " **"))
   )
   (princ)
 )
 (cond
   ((or
      (<= (setq id [color=green](load-layermakerccc)[/color]) 0)
      (not (new_dialog "layermakerccc" id))
    )
    (princ "\n--> Error Loading Dialog.")
   )
   (t
    (setq *name1* (set_tile "name"
                            (cond (*name1*)
                                  ("Default")
                            )
                  )
    )
    (action_tile "name" "(setq *name1* $value)")
    (action_tile "tg1" "(setq answer1 $value)")
    [color=green](setq answer1 "1")[/color]

    (if
      (and
        (= 1 (start_dialog))
        (setq lst (acad_colordlg 1 nil))
      )
       (progn
         (setq name *name1*)

         (if (null (tblsearch "LAYER" name))
           (entmake
             (list
               (cons 0 "LAYER")
               (cons 100 "AcDbSymbolTableRecord")
               (cons 100 "AcDbLayerTableRecord")
               (cons 2 name)
               (cons 62 lst)
               (cons 70 0)
             )
           )
         )
         (setvar 'CLAYER name)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

         [color=green](if (eq answer1 "1")
           (setvar "cecolor" "Bylayer")
         )[/color]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
       )
    )
   )
 )
 (if (< 0 id)
   (unload_dialog id)
 )
 (mapcar 'setvar vars old)
 (princ)
)

Link to comment
Share on other sites

Thanks irneb. That worked great. Have you combined the dcl file into the code?

Glad it worked for you.

 

Yes, I tend to now combine the DCL into the LSP. Makes it easier to post code on these forums, and you don't need to have either of the files in support folders anymore - makes it a bit simpler to have it work nearly anywhere. I've used a little lisp routine of mine to combine the DCL into a defun of its own:

;;; Convert a DCL file to LSP for inclusion into one file
(defun c:DCL2LSP (/ dclFName dcl lspFName lsp str n)
 ;; Get the DCL file's name
 (setq dclFName (findfile (getfiled "Select DCL to convert to LSP" "" "dcl" (+ 4 )))

 ;; Calculate the new LSP file's name
 (setq lspFName (strcat (vl-filename-directory dclFName) "\\load_dialog_" (vl-filename-base dclFName) ".lsp"))

 (setq dcl (open dclFName "r") ;Open DCL for reading
       lsp (open lspFName "w") ;Open LSP for write
 ) ;_ end of setq

 ;; Start LSP
 (setq str (strcat ";;; Function to load "
                   (vl-filename-base dclFName)
                   " dialog\n"
                   "(defun load_dialog_"
                   (vl-filename-base dclFName)
                   " (/ fn f)\n"
                   "  (setq fn (strcat (getvar \"TEMPPREFIX\") \""  (vl-filename-base dclFName) ".DCL\"))\n"
                   "  (setq f (open fn \"w\"))"
           ) ;_ end of strcat
 ) ;_ end of setq
 (write-line str lsp)

 ;; Read each line in DCL & write to LSP
 (while (setq str (read-line dcl))
   ;; prefix all back-slashes with a back-slash
   (setq n 1)
   (while (<= n (strlen str))
     (if (= (substr str n 1) "\\")
       (progn
         (if (= n 1)
           (setq str (strcat "\\" str))
           (setq str (strcat (substr str 1 (- n 1)) "\\" (substr str n)))
         ) ;_ end of if
         (setq n (+ n 2))
       ) ;_ end of progn
       (setq n (1+ n))
     ) ;_ end of if
   ) ;_ end of while

   ;; prefix all double quotes with a back-slash
   (setq n 1)
   (while (<= n (strlen str))
     (if (= (substr str n 1) "\"")
       (progn
         (if (= n 1)
           (setq str (strcat "\\" str))
           (setq str (strcat (substr str 1 (- n 1)) "\\" (substr str n)))
         ) ;_ end of if
         (setq n (+ n 2))
       ) ;_ end of progn
       (setq n (1+ n))
     ) ;_ end of if
   ) ;_ end of while

   ;; Write to file
   (write-line
     (strcat "  (write-line \"" str "\" f)")
     lsp
   ) ;_ end of write-line
 ) ;_ end of while

 (write-line "  (close f)" lsp)
 (write-line "  (load_dialog fn)" lsp)
 (write-line ") ;_ end of defun" lsp)
 (close lsp)
 (close dcl)
) ;_ end of defun

It creates a LSP file with a prefix of "load_dialog_" and then the original DCL's filename. Open and copy-n-paste to where you want it. Then replace the load_dialog calls to the new defun without any arguments.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...