How to go to next function?

How to go to next function?

Anonymous
Not applicable
1,069 Views
4 Replies
Message 1 of 5

How to go to next function?

Anonymous
Not applicable

Hi,

 

I'm very beginner in autolisp, and I need a help.
I'm making a lisp, that when I click the number, the function 'call' block from the relative path.

How is it possible when I click "End!", to go to the certain function, without writing 100+ more line code.
Do I need if function, or something like that?

(defun c:c4x6 ()
(setq insertpt1 (getpoint "\nChoose point: "))
(command "_insert" "./_blokovi/kabl4x6.dwg" insertpt1 "" "" "")
	
	(setq input "1" vlakno1 "1 2 3 4 5 6 End!")

        (progn
        (initget vlakno1)
        (setq input (cond ((getkword (strcat "\nNumber in 1. coulmn?: [" (vl-string-translate " " "/" vlakno1) "] <" input ">: ")))  (input))))
        (apply (read (strcat "c:e" input)) nil)

      (setq input "1" vlakno2 "1 2 3 4 5 6 End!")

        (progn
        (initget vlakno2)
        (setq input (cond ((getkword (strcat "\nNumber in 2. coulmn?: [" (vl-string-translate " " "/" vlakno2) "] <" input ">: ")))  (input))))
        (apply (read (strcat "c:f" input)) nil)
.
.
. 
;etc.

(defun c:e1 ()
(setq insertpt1 (getpoint "\nChoose point: "))
(command "_insert" "./_blokovi/1vlakno.dwg" insertpt1 "" "" "")
(princ)
)

(defun c:e2 ()
(setq insertpt1 (getpoint "\nChoose point: "))
(command "_insert" "./_blokovi/2vlakno.dwg" insertpt1 "" "" "")
(princ)
)
.
.

(defun c:f1 ()
(setq insertpt1 (getpoint "\nChoose point: "))
(command "_insert" "./_blokovi/2vlakno.dwg" insertpt1 "" "" "")
(princ)
)
.
.

 

 

0 Likes
Accepted solutions (1)
1,070 Views
4 Replies
Replies (4)
Message 2 of 5

CodeDing
Advisor
Advisor
Accepted solution

 

 

@Anonymous ,

 

Personally, I think there are better ways to handle what it appears you're going for here..

But to answer your direct question, is this what you're looking for perhaps?

 

(defun c:c4x6 ( / input vlakno1 vlakno2)
  (command "_insert" "./_blokovi/kabl4x6.dwg" pause "" "" "")

  (setq input "1" vlakno1 "1 2 3 4 5 6 End!")
  (initget vlakno1)
  (setq input
    (cond
      ((getkword (strcat "\nNumber in 1. coulmn?: [" (vl-string-translate " " "/" vlakno1) "] <" input ">: ")))
      (input)
    );cond
  );setq
  (if (eq "End!" input)
    (exit) ;<-- Call your End function here
    (eval (read (strcat "(c:e" input ")")))
  );if

  (setq input "1" vlakno2 "1 2 3 4 5 6 End!")
  (initget vlakno2)
  (setq input
    (cond
      ((getkword (strcat "\nNumber in 2. coulmn?: [" (vl-string-translate " " "/" vlakno2) "] <" input ">: ")))
      (input)
    );cond
  );setq
  (if (eq "End!" input)
    (exit) ;<-- Call your End function here
    (eval (read (strcat "(c:f" input ")")))
  );if
.
.
. 
;etc.

(defun c:e1 ( / )
  (command "_insert" "./_blokovi/1vlakno.dwg" pause "" "" "")
  (princ)
)

(defun c:e2 ( / )
  (command "_insert" "./_blokovi/2vlakno.dwg" pause "" "" "")
  (princ)
)
.
.

(defun c:f1 ( / )
  (command "_insert" "./_blokovi/2vlakno.dwg" pause "" "" "")
  (princ)
)
.
.

 

Let me know if that helps.

Best,

~DD

Message 3 of 5

Anonymous
Not applicable

It works. Thank you!

0 Likes
Message 4 of 5

Moshe-A
Mentor
Mentor

@Anonymous  hi,

 

here is my contribution 😀

in order to achieve a command with options and offer a default value to user

here is a c:cmdOpt command that offer 3 options (Height,Width,Rotation) to modify text objects (just an example)

 

it starts with a call to (init_default) function to set initial default values to our options.

it uses some AutoCAD hidden system variables for this purpose: USERR1-3

 

i dedicate USERR1 for height

USERR2 for width factor

USERR3 for rotation angle

 

the body of the program is a big while loop, as long as the user keep choosing options, the command continue untill a null reply is entered (just like many AutoCAD commands).

 

inside the while is switch cases (cond) function to handle the user selection.

in each case a call to a local function (get_real_value) / (get_angle_value) to handle the user input.

 

it maybe looks a very long code for you at this point but as you will learn/understand it, it would be very simple and clear for implementing any options you choose 😀

 

enjoy

moshe

 

 

 

 

 

(defun c:cmdOpt (/ init_defaults get_real_value get_angle_value ;| declaration of local functions |;
		   prtm opn hgt wth rot                         ;| declaration of local variables |; )

 (defun init_defaults ()
  (if (= (getvar "userr1") 0.0)
   (setvar "userr1" 0.2)
  )

  (if (= (getvar "userr2") 0.0)
   (setvar "userr2" 1.0)
  )

  (if (= (getvar "userr3") 0.0)
   (setvar "userr3" (/ pi 4))
  )
 ); init_defaults

  
 (defun get_real_value (msg def / v)
  (initget (+ 2 4)) ; prevent user from entering zero or negative value
	   
  (if (not (setq v (getreal (strcat "\n" msg " <" (rtos def 2) ">: "))))
   (setq v def)
   (setq def v)
  )
 ); get_real_value

  
 (defun get_angle_value (msg def)
  (initget 4) ; prevent user from entering negative value
   
  (if (not (setq v (getangle (strcat "\n" msg " <" (angtos def 0) ">: "))))
   (setq v def)
   (setq def v)
  )
 ); get_angle_value

   
 ; here start c:CmdOpt
 (init_defaults) ; set command defaults value
 (setq opt t)
  
 (while opt      ; loop as long as opn is not nil
  (initget (setq prtm "Height Width Rotation"))
  (setq opt (getkword (strcat "\n" (vl-string-translate " " "/" prtm) ": ")))	; responding with enter here, stop the loop (while)
   
  (cond  
   ((eq opt "Height")
    (setvar "userr1" (setq hgt (get_real_value "New height" (getvar "userr1"))))
    ; handle text height
    ; ...
    ; ...
   ); case
   ((eq opt "Width")
    (setvar "userr2" (setq wth (get_real_value "New width factor" (getvar "userr2"))))
    ; handle text width factor
    ; ...
    ; ...
   ); case
   ((eq opt "Rotation")
    (setvar "userr3" (setq rot (get_angle_value "New rotation angle" (getvar "userr3"))))
    ; handle text rotation angle
    ; ...
    ; ...
   ); case
   ( t
    nil ; handle other options
   ); case
  ); cond  
 ); while

 (princ); quiet exit to autocad command line
); c:cmdopt

 

0 Likes
Message 5 of 5

Sea-Haven
Mentor
Mentor

Maybe something like this. The code could be neatened a fair bit using cond more.

 

To use this need to start again. The dcl is a library routine so it is 2 lines of code in your code and replaces initget.

 

screenshot184.png

0 Likes