Jump to content

Need some help with a dialog box


aawilds

Recommended Posts

Hello, I nee some help with a dialog box. I just started learning how to add dialog boxes to my lisp recently. I am trying add a dialog box to a lisp that asks 3 yes or no questions at the start. I made the DCL file and it looks the way it should. I than tried to add to the lisp file to reference this new file. The dialog box shows up I answer the questions hit OK and it stops. What am I missing?

Any help is very much appreciated.

 

(DEFUN C:MILROAD ( / OLDL NCNT WHICH CENTERL STRAIGHT )
 (setq dialog_name "CSDLB")
 (setq id (load_dialog "CSDLB.DCL"))
 (new_dialog dialog_name id "" )
 (setq dlcnt (get_tile "opt_cntyes"))
 (if (= dlcnt 1)
   (setq NCNT "Y"))
 (setq dlstr (get_tile "opt_stryes"))
 (if (= dlstr 1)
   (setq tm1 "Y"))
 (if (= dlstr 0)
   (setq tm1 "N"))
 (setq dlsym (get_tile "opt_symyes"))
 (if (= dlsym 1)
   (setq WHICH "Y"))
 (if (= dlsym 0)
   (setq WHICH "N"))
 (action_tile "cancle" "(done_dialog 0)")
 (action_tile "accept" "(done_dialog) (setq userclick T))")
 (start_dialog)
 (unload_dialog id)
 ;(SETQ NCNT (GETSTRING "\nDO YOU NEED A CENTERLINE: Y/N"))
 ;|(IF (= NCNT "Y")
   (SETQ STRAIGHT (GETSTRING "\nIS YOUR SRTEET STRAIGHT: Y/N")))|;
 (IF (and (= NCNT "Y") (= tm1 "Y"))
   (SETQ STRAIGHT "Y"))
 (IF (and (= NCNT "Y") (= tm1 "N"))
   (SETQ STRAIGHT "N"))
 ;(SETQ WHICH (GETSTRING "\nIS THE STREET SYMMETRICAL: Y/N"))
 (IF (= STRAIGHT "Y")
   (PROGN
     (SETQ OLDL (GETVAR "CLAYER"))
     (command "layer" "set" "MISC" "")
     (C:CPOLYSTRAIGHT)
     (setq CENTERL (ENTLAST))
     (command "layer" "set" OLDL "")
     ))
 (IF (= STRAIGHT "N")
   (PROGN
     (SETQ OLDL (GETVAR "CLAYER"))
     (command "layer" "set" "MISC" "")
     (C:CPOLYCURVE)
     (setq CENTERL (ENTLAST))
     (command "layer" "set" OLDL "")
     ))
 (IF (= WHICH "N")
   (C:MILROADONE))
 (IF (= WHICH "N")
   (C:MILROADONE))
 (IF (= WHICH "Y")
   (C:MILROADTWO))
 (IF (= NCNT "Y")
   (COMMAND "_.ERASE" CENTERL ""))
 (PRINC)
 )

CSDLB.DCL

Link to comment
Share on other sites

Hi aawilds,

 

did some cleaning to get your dialog working on my system without being to concerned about the routine itself

(defun c:milroad (/ dialog_name id drv oldl ncnt which centerl straight)
 (if (and (setq dialog_name (findfile "csdlb.dcl"))
      (setq id (load_dialog dialog_name))
      (new_dialog "CSDLB" id))
   (progn
     (setq dlcnt (get_tile "opt_cntyes"))
     (if (= dlcnt 1) (setq ncnt "y"))
     (setq dlstr (get_tile "opt_stryes"))
     (if (= dlstr 1) (setq tm1 "y"))
     (if (= dlstr 0) (setq tm1 "n"))
     (setq dlsym (get_tile "opt_symyes"))
     (if (= dlsym 1)(setq which "y"))
     (if (= dlsym 0)(setq which "n"))
     (action_tile "cancel" "(done_dialog 0)")
     (action_tile "accept" "(done_dialog 1) (setq userclick t))")
     (setq drv (start_dialog))(unload_dialog id)
     (if (and (= ncnt "y") (= tm1 "y")) (setq straight "y"))
     (if (and (= ncnt "y") (= tm1 "n")) (setq straight "n"))
     (if (= straight "y")
   (progn
     (setq oldl (getvar "clayer"))(command "layer" "set" "misc" "")(c:cpolystraight)
     (setq centerl (entlast))(command "layer" "set" oldl "")))
     (if (= straight "n")
   (progn (setq oldl (getvar "clayer"))(command "layer" "set" "misc" "")(c:cpolycurve)
          (setq centerl (entlast))(command "layer" "set" oldl "")))
     (if (= which "n") (c:milroadone))
     (if (= which "n") (c:milroadone))
     (if (= which "y") (c:milroadtwo))
     (if (= ncnt "y") (command "_.erase" centerl ""))
   )
 )
 (princ)
)

can't really check the rest of the program because functions like (C:CPOLYSTRAIGHT) or (C:CPOLYCURVE) or (C:MILROADTWO) are not included.

What Roy says is valid and one of the first things that catches my attention if I look at this small piece of code :

 

(setq dlsym (get_tile "opt_symyes"))   (if (= dlsym 1)     (setq WHICH "Y"))   (if (= dlsym 0)     (setq WHICH "N"))   (action_tile "cancle" "(done_dialog 0)")

get_tile returns a string , so not 1 but "1" (or in this case always "0" because you never use action_tile for this tile)

I think action tile cancle should be cancel

Think much of you code can be compacter and more elegant but for a first attempt not bad.

 

gr.Rlx

Link to comment
Share on other sites

Correction:

2. The get_tile function returns a string if the tile key is valid and the function is called in the context of an action_tile action. Else the function returns nil.

Link to comment
Share on other sites

Correction:

2. The get_tile function returns a string if the tile key is valid and the function is called in the context of an action_tile action. Else the function returns nil.

 

 

could give the complete routine in one go but what would be the fun in that, this site is meant to learn , step by step ;-)

 

 

well ok then...

 

(defun c:milroad2 (/ dcl_name dcl-id drv use-centerline straight-street symmetrical-street)
 (if (and (setq dcl-name (findfile "csdlb.dcl"))
            (setq dcl-id (load_dialog dcl-name))
            (new_dialog "Curbs_and_Sidewalks" dcl-id))
   (progn
     (action_tile "tg_use_centerline" "(setq use-centerline $value)")
     (action_tile "tg_straight_street" "(setq straight-street $value)")
     (action_tile "tg_symmetrical_street" "(setq symmetrical-street $value)")
     (action_tile "cancel" "(done_dialog 0)")
     (action_tile "accept" "(done_dialog 1)")
     (setq drv (start_dialog))(unload_dialog dcl-id)
     (if (= drv 1)
       (progn
         (if (= use-centerline "1")
             (princ "\nUsing centerline") (princ "\nNot using centerline"))
         (if (= straight-street "1")
             (princ "\nStreet is straight") (princ "\nStreet is not straight"))
         (if (= symmetrical-street "1")
             (princ "\nStreet is symmetrical") (princ "\nStreet is not symmetrical"))
       )
     )
   )
  )
 (princ)
)

Curbs_and_Sidewalks : dialog { label = "Create Curbs and Sidewalks";
 : boxed_column { label = "Street options :";
   : toggle { label = "Use centerline"; key = "tg_use_centerline";}
   : toggle { label = "Straight street"; key = "tg_straight_street";}
   : toggle { label = "Symmetrical street"; key = "tg_symmetrical_street";}
     spacer;
 }
 spacer;ok_cancel;
}

gr.Rlx

Edited by rlx
Link to comment
Share on other sites

Rlx, you don't need these predefined actions:

(action_tile "cancel" "(done_dialog 0)")
(action_tile "accept" "(done_dialog 1)")

 

 

I'd wrap the whole thing within a single and function, and I'd use the dialog's keys as names for the localised variables in the lisp code:

(defun c:milroad2 (/ dcl_name dcl-id drv tg_use_centerline tg_straight_street tg_symmetrical_street)
 
 ; Toggle associator - connect toggle value (0 or 1) with symbol value (nil or T):
 (defun tgassoc ( keyorval ) (cadr (assoc keyorval '((nil "0")(T "1")("0" nil)("1" T)))) )
 
 (and 
   ; "can't really check the rest of the program because functions like (C:CPOLYSTRAIGHT) or (C:CPOLYCURVE) or (C:MILROADTWO) are not included."
   (or C:CPOLYSTRAIGHT (prompt "\nFunction C:CPOLYSTRAIGHT nod defined."))
   (or C:CPOLYCURVE (prompt "\nFunction C:CPOLYCURVE nod defined."))
   (or C:MILROADTWO (prompt "\nFunction C:MILROADTWO nod defined."))
   (setq dcl-name (findfile "csdlb.dcl"))
   (setq dcl-id (load_dialog dcl-name))
   (new_dialog "Curbs_and_Sidewalks" dcl-id)
   (progn
     (mapcar 
       '(lambda (x) (action_tile x (strcat "(setq $key (tgassoc $value))")))
       '("tg_use_centerline" "tg_straight_street" "tg_symmetrical_street")
     )
     (setq drv (start_dialog)) (unload_dialog dcl-id) (= drv 1)
   ); progn    
   (progn
     (if tg_use_centerline (princ "\nUsing centerline") (princ "\nNot using centerline"))
     (if tg_straight_street (princ "\nStreet is straight") (princ "\nStreet is not straight"))
     (if tg_symmetrical_street (princ "\nStreet is symmetrical") (princ "\nStreet is not symmetrical"))
   ); progn
 ); and 
 (princ)
); defun

 

Just sharing my preferences - your example should be more understandable for the OP. ;)

Link to comment
Share on other sites

@Grrr, indeed you don't really need them but for someone who's just starting, this code might be just a little bit too complex. I use a lot of mapcars and lambda's too but not everybody is comfortable with these command. Also , maybe one day a new person needs to edit the code. He might be Einstein , he might be Lee ... or not ;-)

Have done very little coding lately, not enough hours in a day... but, always nice to hear from you! :beer:

 

 

gr.Rlx

Link to comment
Share on other sites

Rlx,

I definitely do not doubt about your skills - I think that the question here is 'what would be the most short and understandable example'.

I tried avoid using mapcar'n'lambda for the simplier examples too, but its just too tempting to shorten the code. :lol:

So just trying to provide example thats somewhere in the middle...

 

Maybe I should use:

(foreach x '("tg_use_centerline" "tg_straight_street" "tg_symmetrical_street")
 (action_tile x (strcat "(setq $key (tgassoc $value))"))
)

 

Instead of:

(mapcar 
 '(lambda (x) (action_tile x (strcat "(setq $key (tgassoc $value))")))
 '("tg_use_centerline" "tg_straight_street" "tg_symmetrical_street")
)

 

Because I just now realised that foreach will be more suitable for simplicity, effectivness and shortness.

 

Have done very little coding lately, not enough hours in a day... but, always nice to hear from you! :beer:

 

Same here, I've got too much aside work to keep me off from my hobby... Still 5-10 minutes coding a day should keep anyone in shape. :working:

Link to comment
Share on other sites

Rlx, I definitely do not doubt about your skills

 

 

thanx for the confidence but I really think my skills are a little exaggerated haha. I sometimes have original ideas but I lack speed, so experience is good , but IQ is better :geek:

 

 

gr. Rlx

Link to comment
Share on other sites

@Roy, I didn't tested the code - but good catch..

 

At first I intended to write:

(action_tile x (strcat "(setq " x " (tgassoc $value))"))

but then I've realised that I could use just the $key attribute, but forgot to remove strcat. :oops:

so it should be:

(action_tile x "(setq $key (tgassoc $value))")

 

However strcat doesn't seems to fail anyway:

_$ (strcat "abc") -> "abc"

Link to comment
Share on other sites

You have misunderstood. Strcat is not the issue. Just test your code...

 

Ok, just tested...

 

This doesn't work:

(action_tile x "(setq $key (tgassoc $value))")

 

This works:

(action_tile x (strcat "(setq " x " (tgassoc $value))"))

 

Thanks for pointing that out, Roy! It was literally assigning the value to a symbol named "$key".

Wondering why my expectation differs from the facts...

Link to comment
Share on other sites

Thank you all for the help. I am still having a bit of a problem. I used RLX's fix and thought at I could replace the princ with setting variables, but it runs like the if statements are not being checked. It always makes a centerline even when you leave it unchecked. Here is the full lisp. What have I missed? Thanks again for the help.

(DEFUN C:MILROADONE ( / CNTL ROW PTOT DTBOC DTFOS DTBOS OLDL )
 (SETQ CNTL (ENTSEL "\nSELECT THE CENTERLINE OF THE ROAD" ))
 (SETQ ROW (ENTSEL "\nSELECT RIGHT OF WAY WORKING TOWARD" ))
 (SETQ PTOT (CDR (ASSOC 10 (ENTGET (CAR ROW)))))
 (SETQ DTBOC (GETINT "\nENTER DISTANCE TO BOC FROM CENTERLINE"))
 (SETQ DTFOS (+ (GETINT "\nENTER DISTANCE FROM FRONT OF SIDEWALK TO BOC") DTBOC))
 (SETQ DTBOS (+ (GETINT "\nENTER WIDTH OF SIDEWALK") DTFOS))
 (SETQ OLDL (GETVAR "CLAYER"))
 (command "layer" "set" "BOC" "")
 (vl-cmdf "_.offset" "_L" "_C" DTBOC CNTL PTOT "")
 (command "layer" "set" "CWALK" "")
 (vl-cmdf "_.offset" "_L" "_C" DTFOS CNTL PTOT "")
 (vl-cmdf "_.offset" "_L" "_C" DTBOS CNTL PTOT "")
 (command "layer" "set" OLDL "")
 (PRINC)
 )


(DEFUN C:MILROADTWO ( / CNTL ROW1 ROW2 PTOT1 PTOT2 DTBOC DTFOS DTBOS OLDL )
 (SETQ CENTERL (ENTSEL "\nSELECT THE CENTERLINE OF THE ROAD" ))
 (SETQ ROW1 (ENTSEL "\nSELECT FIRST RIGHT OF WAY" ))
 (SETQ ROW2 (ENTSEL "\nSELECT SECOND RIGHT OF WAY" ))
 (SETQ PTOT1 (CDR (ASSOC 10 (ENTGET (CAR ROW1)))))
 (SETQ PTOT2 (CDR (ASSOC 10 (ENTGET (CAR ROW2)))))
 (SETQ DTBOC (/ (GETINT "\nENTER DISTANCE FROM BOC TO BOC") 2))
 (SETQ DTFOS (+ (GETINT "\nENTER DISTANCE FROM FRONT OF SIDEWALK TO BOC") DTBOC))
 (SETQ DTBOS (+ (GETINT "\nENTER WIDTH OF SIDEWALK") DTFOS))
 (SETQ OLDL (GETVAR "CLAYER"))
 (command "layer" "set" "BOC" "")
 (vl-cmdf "_.offset" "_L" "_C" DTBOC CENTERL PTOT1 "")
 (vl-cmdf "_.offset" "_L" "_C" DTBOC CENTERL PTOT2 "")
 (command "layer" "set" "CWALK" "")
 (vl-cmdf "_.offset" "_L" "_C" DTFOS CENTERL PTOT1 "")
 (vl-cmdf "_.offset" "_L" "_C" DTFOS CENTERL PTOT2 "")
 (vl-cmdf "_.offset" "_L" "_C" DTBOS CENTERL PTOT1 "")
 (vl-cmdf "_.offset" "_L" "_C" DTBOS CENTERL PTOT2 "")
 (command "layer" "set" OLDL "")
 (PRINC)
 )

;Original from Lee Mac
(defun c:cPolySTRAIGHT (/ ent1 ent2 i len pt p1 ptlst)
 (vl-load-com)

 (if (and (setq ent1 (car (entsel "\nSelect First ROW: ")))
          (wcmatch (cdr (assoc 0 (entget ent1))) "*POLYLINE")
          (setq ent2 (car (entsel "\nSelect Second ROW: ")))
          (wcmatch (cdr (assoc 0 (entget ent2))) "*POLYLINE"))
   (progn
     (setq i -1 len (/ (vla-get-Length
                         (vlax-ename->vla-object ent1)) 3.))
     
     (while (setq pt (vlax-curve-getPointatDist ent1 (* (setq i (1+ i)) len)))
       (setq p1    (vlax-curve-getClosestPointto ent2 pt t)
             ptlst (cons (polar pt (angle pt p1) (/ (distance pt p1) 2.)) ptlst)))
     
     (setq ptlst (apply (function append)
                        (mapcar
                          (function
                            (lambda (x) (list (car x) (cadr x)))) ptlst)))
     
     (vla-AddLightWeightPolyline
       (vla-get-ModelSpace
         (vla-get-ActiveDocument (vlax-get-acad-object)))
       (variant
         (vlax-safearray-fill
           (safearray vlax-VBDouble (cons 0 (1- (length ptlst)))) ptlst)))))

 (princ))

;Original from Lee Mac
(defun c:cPolyCURVE (/ ent1 ent2 i len pt p1 ptlst)
 (vl-load-com)

 (if (and (setq ent1 (car (entsel "\nSelect First ROW: ")))
          (wcmatch (cdr (assoc 0 (entget ent1))) "*POLYLINE")
          (setq ent2 (car (entsel "\nSelect Second ROW: ")))
          (wcmatch (cdr (assoc 0 (entget ent2))) "*POLYLINE"))
   (progn
     (setq i -1 len (/ (vla-get-Length
                         (vlax-ename->vla-object ent1)) 25.))
     
     (while (setq pt (vlax-curve-getPointatDist ent1 (* (setq i (1+ i)) len)))
       (setq p1    (vlax-curve-getClosestPointto ent2 pt t)
             ptlst (cons (polar pt (angle pt p1) (/ (distance pt p1) 2.)) ptlst)))
     
     (setq ptlst (apply (function append)
                        (mapcar
                          (function
                            (lambda (x) (list (car x) (cadr x)))) ptlst)))
     
     (vla-AddLightWeightPolyline
       (vla-get-ModelSpace
         (vla-get-ActiveDocument (vlax-get-acad-object)))
       (variant
         (vlax-safearray-fill
           (safearray vlax-VBDouble (cons 0 (1- (length ptlst)))) ptlst)))))

 (princ))

(defun c:milroad (/ dcl_name dcl-id drv use-centerline straight-street symmetrical-street NCNT WHICH STRAIGHT OLDL CENTERL)
 (if (and (setq dcl-name (findfile "csdlb.dcl"))
            (setq dcl-id (load_dialog dcl-name))
            (new_dialog "Curbs_and_Sidewalks" dcl-id))
   (progn
     (action_tile "tg_use_centerline" "(setq use-centerline $value)")
     (action_tile "tg_straight_street" "(setq straight-street $value)")
     (action_tile "tg_symmetrical_street" "(setq symmetrical-street $value)")
     (setq drv (start_dialog))(unload_dialog dcl-id)
         (if (= use-centerline "1")
             (SETQ NCNT "Y")(SETQ NCNT "N"))
         (if (= straight-street "1")
             (SETQ STRAIGHT "Y") (SETQ STRAIGHT "N"))
         (if (= symmetrical-street "1")
             (SETQ WHICH "Y") (SETQ WHICH "N"))
   )
  )
 (IF (AND (= STRAIGHT "Y") (= NCNT "Y"))
   (PROGN
     (SETQ OLDL (GETVAR "CLAYER"))
     (command "layer" "set" "MISC" "")
     (C:CPOLYSTRAIGHT)
     (setq CENTERL (ENTLAST))
     (command "layer" "set" OLDL "")
     ))
 (IF (AND (= STRAIGHT "N") (= NCNT "Y"))
   (PROGN
     (SETQ OLDL (GETVAR "CLAYER"))
     (command "layer" "set" "MISC" "")
     (C:CPOLYCURVE)
     (setq CENTERL (ENTLAST))
     (command "layer" "set" OLDL "")
     ))
 (IF (= WHICH "N")
   (C:MILROADONE))
 (IF (= WHICH "N")
   (C:MILROADONE))
 (IF (= WHICH "Y")
   (C:MILROADTWO))
 (IF (= NCNT "Y")
   (COMMAND "_.ERASE" CENTERL ""))
 (princ)
)

 

One quick other question. Why use toggles for a yes or no question? I see that they work I just was wondering.

Edited by aawilds
fixed my code
Link to comment
Share on other sites

One quick other question. Why use toggles for a yes or no question? I see that they work I just was wondering.

You can have two lightswitches if you like, one that says 'on' and one that says 'off' , or .... you can have just one switch and if it isn't on then it must be off :P Edited by rlx
Link to comment
Share on other sites

That makes sense. Thank you.

 

 

you're welcome.

 

 

Think for example when you plot. Portrait or landscape are just 2 choices and you must choose one or the other , so a radio button makes the most sense. But whether you want to use fit , rotate or wireframe etc , that's optional and so toggles are usually the first option that come to mind. But if you like radio buttons or popups more , it's not wrong , mainly a matter of taste or personal style.

 

 

Good luck with your appie and happy coding.

 

 

gr. Rlx

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...