Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

AutoLISP and DCL

7 REPLIES 7
Reply
Message 1 of 8
zph
Collaborator
734 Views, 7 Replies

AutoLISP and DCL

Good day!

 

I am slowly venturing into the world of DCL and with the help of online tutorials I've written my first successful AutoLISP/DCL program.

 

However, I've now hit a wall and after searching around a bit I was not successful in finding help with this particular case.

 

AutoLISP code:

 

Spoiler

;;;;;--------   CVU   --------;;;;;
;
; Goal: Provide local routine to convert quantities between basic unit types.
;
; ACAD button call CVU (none)
;
;;; by Zachary Hull
;;; 5.24.2016
;===================================;


(defun c:cvu ( / adoc eFlag uList *error* uI u1 u2 uR tw th bw bh)

(vl-load-com)
(vla-startundomark (setq adoc (vla-get-activedocument (vlax-get-acad-object))))
(setvar "cmdecho" 0)
(setq eFlag 1)

(setq uList
 (list
  "in" "inches" 
  "ft" "feet" 
  "mm" "millimeters"  
  "cm" "centimeters"
  "me" "meters" 
 ) ;list
)

(defun *error* (msg)
 (if (member msg '("quit / exit abort" "Conversion failed." "*Cancel*"))
  (progn
  (setvar "cmdecho" 1)
  (vla-endundomark adoc)
   (cond
    ((= eFlag 1) (princ "\n <!> Routine aborted <!> "))
    ((= eFlag 2) (princ "\n <!> Routine finished <!> "))
   ) ;cond
  ) ;progn
 ) ;if
) ;*error*


(defun conv1 ()
(strcat (rtos uI) " " u1 " = " (rtos (cvunit uI u1 u2)) " " u2)
)


(if (not (setq CVU_DCL_ID (load_dialog "CVU.DCL")))

 (progn (princ "\n\033\n Verify CVU.dcl file path ")(exit))

 (if (not (new_dialog "main" CVU_DCL_ID))

  (progn (princ "\n\033\n Verify CVU.DCL 'main : dialog' naming definition ")(exit))

  (progn

;*********************** Bottom color bar **********************;
        ;
  (setq bw (dimx_tile "botBar"))   ;
  (setq bh (dimy_tile "botBar"))   ;
  (start_image "botBar")    ;
  (fill_image 0 0 bw bh 5)   ;
  (end_image)     ;
        ;
;******************** Unit conversion types ********************;
        ;
  (action_tile "f_In" "(setq u1 \"in\")")  ;
  (action_tile "f_Ft" "(setq u1 \"ft\")")  ;
  (action_tile "f_Mm" "(setq u1 \"mm\")")  ;
  (action_tile "f_Cm" "(setq u1 \"cm\")")  ;
  (action_tile "f_Me" "(setq u1 \"me\")")  ;
        ;
  (action_tile "t_In" "(setq u2 \"in\")")  ;
  (action_tile "t_Ft" "(setq u2 \"ft\")")  ;
  (action_tile "t_Mm" "(setq u2 \"mm\")")  ;
  (action_tile "t_Cm" "(setq u2 \"cm\")")  ;
  (action_tile "t_Me" "(setq u2 \"me\")")  ;
        ;
;***************************************************************;


  (action_tile "Quantity" "(setq uI (distof $value))")
  (set_tile "result" "(conv1)")


  (action_tile "done" "(done_dialog 1)") ;"Exit"
  (action_tile "conv" "(done_dialog 2)") ;"Convert"
  (setq ddiag (start_dialog))
  (unload_dialog CVU_DCL_ID) 
   (cond
    ((= ddiag 1)
     (progn (princ "\n Closing dialog box ")(setq eFlag 2)(exit))
    )

    ((= ddiag 2)
     (progn
     (princ "\n Selected 'Convert' ")

     (if (or (= uI nil)(= u1 nil)(= u2 nil))
      (progn
       (cond
       ((= uI nil) (princ "\n\033\n 'Base' value empty"))
       ((= u1 nil) (princ "\n\033\n 'Base' unit not selected"))
       ((= u2 nil) (princ "\n\033\n 'Desired' unit not selected"))
       ) ;cond
      (exit)
      ) ;progn
     ) ;if

     (setq uR (cvunit uI u1 u2))

      (if (member u1 uList)
      (setq u1 (nth (1+ (vl-position u1 uList)) uList))
      (progn (princ "\n\033\n see variable 'uList'")(exit))
      ) ;if

      (if (member u2 uList)
      (setq u2 (nth (1+ (vl-position u2 uList)) uList))
      (progn (princ "\n\033\n see variable 'uList'")(exit))
      ) ;if

     (alert (strcat (rtos uI) " " u1 " = " (rtos uR) " " u2))
     (princ (strcat "\n   * "(rtos uI) " " u1 " = " (rtos uR) " " u2 " * "))
     ) ;progn
    )
   ) ;cond
  ) ;progn
 ) ;if
) ;if

 

;----;

;(if (setq uI (getreal "\n\033\n Quantity to convert: "))
;
; (progn
; (initget "in ft mm cm me")
; (setq u1 (getkword "\n\033\n First unit type [in/ft/mm/cm/me]: "))
; (initget "in ft mm cm me")
; (setq u2 (getkword "\n\033\n Second unit type [in/ft/mm/cm/me]: "))
; (setq uR (cvunit uI u1 u2))
; ) ;progn
;
; (progn (princ "\n\033\n Quantity required ")(exit))
😉 ;if
;
;(if (member u1 uList)
;(setq u1 (nth (1+ (vl-position u1 uList)) uList))
;(progn (princ "\n\033\n see routine CVU, unit value not included in variable 'uList'")(exit))
😉 ;if
;
;(if (member u2 uList)
;(setq u2 (nth (1+ (vl-position u2 uList)) uList))
;(progn (princ "\n\033\n see routine CVU, unit value not included in variable 'uList'")(exit))
😉 ;if
;
;(alert (strcat "\n " (rtos uI) " " u1 "\n\t          = \n\t\t" (rtos uR) " " u2 "."))
;(setq eFlag 2)
;(exit)
(princ)
) ;cvu


;**********************************    End of File     **********************************;

 

 

DCL code:

 

Spoiler

main : dialog
{label = "CVU - Convert Units";

: boxed_row
{
label = "Provide 'Base' quantity in text box";

 : edit_box
 {
 key = "Quantity";
 label = "Quantity";
 edit_width = 15;
 value = "";
 initial_focus = true;
 }

 : button
 {
 key = "conv";
 label = "Convert";
 is_default = false;
 }
} //quantity & convert

: boxed_row
{
label = "Conversion Results";
fixed_height = true;
height = 2;

 : text
 {
 key = "result";
 value = "Select 'Convert' to display results...";
 }
} //results box

: boxed_row
{
label = "Unit Conversion Options";

 : radio_column
 {
 key = "unit1";
 label = "'Base' Unit";
 fixed_width = true;
 width = 19;

  : radio_button
  {
  label = "Inches";
  key = "f_In";
  mnemonic = "i";
  }

  : radio_button
  {
  label = "Feet";
  key = "f_Ft";
  mnemonic = "f";
  }

  : radio_button
  {
  label = "Millimeters";
  key = "f_Mm";
  mnemonic = "m";
  }

  : radio_button
  {
  label = "Centimeters";
  key = "f_Cm";
  mnemonic = "c";
  }

  : radio_button
  {
  label = "Meters";
  key = "f_Me";
  mnemonic = "e";
  }
 } // radio column

 : radio_column
 {
 key = "unit2";
 label = "Desired Unit";
 fixed_width = true;
 width = 19;

  : radio_button
  {
  label = "Inches";
  key = "t_In";
  mnemonic = "i";
  }

  : radio_button
  {
  label = "Feet";
  key = "t_Ft";
  mnemonic = "f";
  }

  : radio_button
  {
  label = "Millimeters";
  key = "t_Mm";
  mnemonic = "m";
  fixed_width = true;
  }

  : radio_button
  {
  label = "Centimeters";
  key = "t_Cm";
  mnemonic = "c";
  }

  : radio_button
  {
  label = "Meters";
  key = "t_Me";
  mnemonic = "e";
  }
 } // radio column
} // units conversion options

spacer_1;

: row
{
 : button
 {
 key = "done";
 label = "Exit";
 is_default = false;
 is_cancel = true;
 }
} //Exit box

: image
{
key = "botBar";
height = 1.0;
width = 1.0;
}

} // main

 

I've highlited the parts of both pieces of code in RED that are related to the issue.  When I comment this lines out, the program executes without error.

 

Currently, the program takes three inputs (based upon the CVUNIT function) and the result (if all three inputs are 'acceptable') will be displayed using the ALERT function and the dialog box closes.

 

What I am trying to figure out is how to keep the dialog box 'open' after selecting the 'Convert' button and by selecting the 'Convert' button, display what would be displayed by the ALERT function as the value of the "results" text in the dialog box.  I want the dialog box to ONLY close after the 'Exit' button is selected.

 

Does this make sense?

7 REPLIES 7
Message 2 of 8
ВeekeeCZ
in reply to: zph

Just quess... didn't go thru it all...

 

I would suspect wrong position of (unload_dialog CVU_DCL_ID) line.
It should be inside of
(cond
((= ddiag 1) ..HERE)

)

Message 3 of 8
zph
Collaborator
in reply to: ВeekeeCZ

Yeah, I was thinking about the 'unload' function...I moved it inside the 'cond', but nothing changed and I think it will stay that way until I change these parts as well:

 

>>>  (action_tile "conv" "(done_dialog 2)") ;"Convert"

 

>>> (set_tile "result" "(conv1)")

 

(for reference)

 

(defun conv1 ()
(strcat (rtos uI) " " u1 " = " (rtos (cvunit uI u1 u2)) " " u2)
)

 

---

 

The "conv" button still closes the dialog box due to (done_dialog...)

Message 4 of 8
ВeekeeCZ
in reply to: zph

Looking into it... Try...

Btw. You need to move (setq uR (cvunit uI u1 u2)) line.

 

Spoiler
;;;;--------   CVU   --------;;;;;
;
; Goal: Provide local routine to convert quantities between basic unit types.
;
; ACAD button call CVU (none)
;
;;; by Zachary Hull
;;; 5.24.2016
;===================================;

(defun c:cvu ( / adoc eFlag uList *error* uI u1 u2 uR tw th bw bh done)
  (vl-load-com)
  (vla-startundomark (setq adoc (vla-get-activedocument (vlax-get-acad-object))))
  (setvar "cmdecho" 0)
  (setq eFlag 1)
  (setq uList
	 (list
	   "in" "inches"
	   "ft" "feet"
	   "mm" "millimeters"
	   "cm" "centimeters"
	   "me" "meters"
	   ) ;list
	)
  (defun *error* (msg)
    (if (member msg '("quit / exit abort" "Conversion failed." "*Cancel*"))
      (progn
	(setvar "cmdecho" 1)
	(vla-endundomark adoc)
	(cond
	  ((= eFlag 1) (princ "\n <!> Routine aborted <!> "))
	  ((= eFlag 2) (princ "\n <!> Routine finished <!> "))
	  ) ;cond
	) ;progn
      ) ;if
    ) ;*error*
  
  (defun conv1 ()
    (strcat (rtos uI) " " u1 " = " (rtos (cvunit uI u1 u2)) " " u2)
    )

  (while (not done)
  (if (not (setq CVU_DCL_ID (load_dialog "CVU.DCL")))
    (progn (princ "\n\033\n Verify CVU.dcl file path ")(exit))
    (if (not (new_dialog "main" CVU_DCL_ID))
      (progn (princ "\n\033\n Verify CVU.DCL 'main : dialog' naming definition ")(exit))
      (progn
	;*********************** Bottom color bar **********************;
	;
	(setq bw (dimx_tile "botBar"))   ;
	(setq bh (dimy_tile "botBar"))   ;
	(start_image "botBar")    ;
	(fill_image 0 0 bw bh 5)   ;
	(end_image)     ;
	;
	;******************** Unit conversion types ********************;
	;
	(action_tile "f_In" "(setq u1 \"in\")")  ;
	(action_tile "f_Ft" "(setq u1 \"ft\")")  ;
	(action_tile "f_Mm" "(setq u1 \"mm\")")  ;
	(action_tile "f_Cm" "(setq u1 \"cm\")")  ;
	(action_tile "f_Me" "(setq u1 \"me\")")  ;
	;
	(action_tile "t_In" "(setq u2 \"in\")")  ;
	(action_tile "t_Ft" "(setq u2 \"ft\")")  ;
	(action_tile "t_Mm" "(setq u2 \"mm\")")  ;
	(action_tile "t_Cm" "(setq u2 \"cm\")")  ;
	(action_tile "t_Me" "(setq u2 \"me\")")  ;
	;
	;***************************************************************;
	
	(action_tile "Quantity" "(setq uI (distof $value))")
	(set_tile "result" "(conv1)")
	
	(action_tile "done" "(done_dialog 1)") ;"Exit"
	(action_tile "conv" "(done_dialog 2)") ;"Convert"
	(setq ddiag (start_dialog))
	(unload_dialog CVU_DCL_ID)
	(cond
	  ((= ddiag 1)
	   (progn (princ "\n Closing dialog box ") (setq done T) (setq eFlag 2)(exit))
	   )
	  ((= ddiag 2)
	   (progn
	     (princ "\n Selected 'Convert' ")
	     (if (or (= uI nil)(= u1 nil)(= u2 nil))
	       (progn
		 (cond
		   ((= uI nil) (princ "\n\033\n 'Base' value empty"))
		   ((= u1 nil) (princ "\n\033\n 'Base' unit not selected"))
		   ((= u2 nil) (princ "\n\033\n 'Desired' unit not selected"))
		   ) ;cond
		 (exit)
		 ) ;progn
	       ) ;if
	     (if (member u1 uList)
	       (setq u1 (nth (1+ (vl-position u1 uList)) uList))
	       (progn (princ "\n\033\n see variable 'uList'")(exit))
	       ) ;if
	     (if (member u2 uList)
	       (setq u2 (nth (1+ (vl-position u2 uList)) uList))
	       (progn (princ "\n\033\n see variable 'uList'")(exit))
	       ) ;if
	     (setq uR (cvunit uI u1 u2))
	     (alert (strcat (rtos uI) " " u1 " = " (rtos uR) " " u2))
	     (princ (strcat "\n   * "(rtos uI) " " u1 " = " (rtos uR) " " u2 " * "))
	     ) ;progn
	   )
	  ) ;cond
	) ;progn
      ) ;if
    ) ;if
  )
  ;----;
  ;(if (setq uI (getreal "\n\033\n Quantity to convert: "))
  ;
  ; (progn
  ; (initget "in ft mm cm me")
  ; (setq u1 (getkword "\n\033\n First unit type [in/ft/mm/cm/me]: "))
  ; (initget "in ft mm cm me")
  ; (setq u2 (getkword "\n\033\n Second unit type [in/ft/mm/cm/me]: "))
  ; (setq uR (cvunit uI u1 u2))
  ; ) ;progn
  ;
  ; (progn (princ "\n\033\n Quantity required ")(exit))
  ;) ;if
  ;
  ;(if (member u1 uList)
  ;(setq u1 (nth (1+ (vl-position u1 uList)) uList))
  ;(progn (princ "\n\033\n see routine CVU, unit value not included in variable 'uList'")(exit))
  ;) ;if
  ;
  ;(if (member u2 uList)
  ;(setq u2 (nth (1+ (vl-position u2 uList)) uList))
  ;(progn (princ "\n\033\n see routine CVU, unit value not included in variable 'uList'")(exit))
  ;) ;if
  ;
  ;(alert (strcat "\n " (rtos uI) " " u1 "\n\t          = \n\t\t" (rtos uR) " " u2 "."))
  ;(setq eFlag 2)
  ;(exit)
  (princ)
  ) ;cvu

 

Not sure if you want to keep set the units... if so.. use (set_tile f_def "1") where d_def is (setq f_def "c_Cm"), key you need to change by last selected.

 

	(setq f_def "f_Cm")
	(setq bw (dimx_tile "botBar"))   ;
	(setq bh (dimy_tile "botBar"))   ;
	(start_image "botBar")    ;
	(fill_image 0 0 bw bh 5)   ;
	(end_image)     ;

	(set_tile f_def "1")

 

 

 

Message 5 of 8
zph
Collaborator
in reply to: ВeekeeCZ

Thank you for the tip.  I figured it out 🙂

 

Both files took some major changes.  I've attached them.

Message 6 of 8
ВeekeeCZ
in reply to: zph


@zph wrote:

Thank you for the tip.  I figured it out 🙂

 

Both files took some major changes.  I've attached them.


Thank you for sharing. I tried that... unfortunatly it still have some issues. (tested on ACAD 2015)

 

When you set the conversion and hit "Convert" more times then it circles the units by one step... and again and again. You should use a different names...

 

 

(if (member u1 uList) (setq w1 (nth (1+ (vl-position u1 uList)) uList)))
(if (member u2 uList) (setq w2 (nth (1+ (vl-position u2 uList)) uList)))
(set_tile "resultUI" (strcat (rtos uI) " " w1 " = " (rtos uR) " " w2))
(princ (strcat "\n * " (rtos uI) " " w1 " = " (rtos uR) " " w2 " * "))

 

The second issue i have found is about the reporting to command line. Third conversion (why third??) doesn't report anything... and after each other it reports the result of one conversion behind. I don't know why and how to fix this 😞

Message 7 of 8
zph
Collaborator
in reply to: ВeekeeCZ

Funny that you mention this. Shortly after posting, I discovered this issue and solved the exact same way you did - by creating two new variables (what you are calling w1 and w2).

I don't know about the third conversion not reporting to the cmd line, but I may have inadvertently fixed it since as well.

Now I'm trying to add options to change the format of the units from decimal to architectural and vice versa. The logic may get a little tricky 🙂

 

I did, however, have an issue with running this routine on a different computer.  The routine (on the other computer) throws the error about this:

 

(if (not (new_dialog "main" CVU_DCL_ID))
				
(progn (princ "\n\033\n Verify CVU.DCL 'main : dialog' naming definition ")(exit))

....

 

I check it out, and of course, I haven't changed anything - so I don't know what the problem is there...yet 😉

Message 8 of 8
ВeekeeCZ
in reply to: zph

Then good luck with that!

 

BTW One TIP for you. Maybe for inspiration 🙂

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report