Need help to solve an error in lisp

Need help to solve an error in lisp

smallƑish
Advocate Advocate
573 Views
10 Replies
Message 1 of 11

Need help to solve an error in lisp

smallƑish
Advocate
Advocate

in this code, if the routine is canceling or stopped due to invalid values, the Object snap is getting 0. any solution to set back the osnap to user_osmode in all situations?

 

 

(defun c:D2E ()
  (setq user_osmode (getvar"osmode"))
(setvar "osmode" 512)
(prompt "\nPick Outer Side of Main Duct: ")
(setq en1: (entsel))
(prompt "\nPick Inner Side of Main Duct: ")
(setq en2: (entsel))
(menucmd "s=numb_n")(SETQ CFM:M (GETREAL "\nMAIN CFM"))(menucmd "s=")
(prompt "\nPick Inner Side of FIRST Branch Duct: ")
(setq en3: (entsel))
(prompt "\nPick Outer Side of FIRST Branch Duct: ")
(setq en4: (entsel))
(menucmd "s=numb_n")(SETQ CFM:B1 (GETREAL "\nCFM of BRANCH 1: "))(menucmd "s=")
(prompt "\nPick INNER Side of SECOND Branch Duct: ")
(setq en5: (entsel))
(prompt "\nPick OUTER Side of SECOND Branch Duct: ")
(setq en6: (entsel))
(SETQ CFM:B2 (- cfm:m cfm:b1))
(setvar "osmode" 0)
(M:ELBOW EN1: EN2: EN3: EN4: CFM:B1 NIL r_o_d:1)
(SETQ LP:1 EN1:PP LP:A EN2:PP A:A A:1)
(COMMAND "LINE" LP:1 (POLAR LP:1 A:A  (- 1)) "") (SETQ EN?: (list (ENTLAST) LP:1))
(M:ELBOW EN1: EN?: EN5: EN6: CFM:M 1 r_o_d:2)
(SETQ LP:2 EN2:PP LP:B EN1:PP A:B A:1)
(SETQ 😧 (DISTANCE LP:1 LP:2)
)(IF (/= 😧 0)(PROGN
              (IF (= (ANGTOS (ANGLE LP:1 LP:2) 0 0) (ANGTOS  A:A 0 0))(COMMAND "PLINE" LP:1 (POLAR LP:A A:A D:) "")(COMMAND "PLINE" LP:1 (POLAR LP:B A:A D:) "")
              )
              )
 )
  (setvar "osmode" user_osmode)
)
0 Likes
574 Views
10 Replies
Replies (10)
Message 2 of 11

Kent1Cooper
Consultant
Consultant

This is what *error* handling is for.  Search for:

(defun *error*

in this Forum for many examples.

Kent Cooper, AIA
Message 3 of 11

rkmcswain
Mentor
Mentor

@smallƑish - you can read all about. error handling in autolisp here.

R.K. McSwain     | CADpanacea | on twitter
Message 4 of 11

ВeekeeCZ
Consultant
Consultant

Here, it is missing some variable and possibly more.

 

(SETQ some-variable-here (DISTANCE LP:1 LP:2)
)

(IF (/= 0)(PROGN
(IF (= (ANGTOS (ANGLE LP:1 LP:2) 0 0) (ANGTOS A:A 0 0))(COMMAND "PLINE" LP:1 (POLAR LP:A A:A D:) "")(COMMAND "PLINE" LP:1 (POLAR LP:B A:A D:) "")
)
)
)

Message 5 of 11

smallƑish
Advocate
Advocate

tried, But not that 

0 Likes
Message 6 of 11

ВeekeeCZ
Consultant
Consultant

The syntax seems right. You need to provide a sample drawing and describe what should be selected and what values should be entered.

 

(defun c:d2e ()
  (setq user_osmode (getvar"osmode"))
  (setvar "osmode" 512)
  
  (prompt "\npick outer side of main duct: ")
  (setq en1: (entsel))
  
  (prompt "\npick inner side of main duct: ")
  (setq en2: (entsel))
  
  (menucmd "s=numb_n")
  (setq cfm:m (getreal "\nmain cfm"))
  (menucmd "s=")
  
  (prompt "\npick inner side of first branch duct: ")
  (setq en3: (entsel))
  
  (prompt "\npick outer side of first branch duct: ")
  (setq en4: (entsel))
  
  (menucmd "s=numb_n")
  (setq cfm:b1 (getreal "\ncfm of branch 1: "))
  (menucmd "s=")
  
  (prompt "\npick inner side of second branch duct: ")
  (setq en5: (entsel))
  
  (prompt "\npick outer side of second branch duct: ")
  (setq en6: (entsel))
  
  (setq cfm:b2 (- cfm:m cfm:b1))
  (setvar "osmode" 0)
  
  (m:elbow en1: en2: en3: en4: cfm:b1 	nil 	r_o_d:1)
  
  (setq lp:1 en1:pp
	lp:a en2:pp
	a:a a:1)
  
  (command "line" lp:1 (polar lp:1 a:a  (- 1)) "")
  (setq en?: (list (entlast) lp:1))
  
  (m:elbow en1: en?: en5: en6: cfm:m 	1 	r_o_d:2)
  
  (setq lp:2 en2:pp
	lp:b en1:pp
	a:b a:1)
  (setq var (distance lp:1 lp:2))
  
  (if (/=  0)
    (progn
      (if (= (angtos (angle lp:1 lp:2) 0 0)
	     (angtos a:a 0 0)
	     )
	(command "pline" lp:1 (polar lp:a a:a d:) "")
	(command "pline" lp:1 (polar lp:b a:a d:) "")
	)
      )
    )
  (setvar "osmode" user_osmode)
  )

 

Message 7 of 11

smallƑish
Advocate
Advocate

Please find attached

gif( for working)

complete lisp file 

and sample dwg 

vsdc-sr 2023-12-15 23-13-42.gif

 

0 Likes
Message 8 of 11

ВeekeeCZ
Consultant
Consultant

Well, it works for me.

Although... you should definitely start systematically localizing all the variables, add *error* function to make sure that you reset all the sysvars that you have touched, and somehow solve or remove all those first 20 lines that are not part of any defun. 

If you keep on programming with this mess, you'll see these flaws more and more often and never know what causes them and what interferes with what.

Also, compare my formatting and your mess. Make your code nice and clean. It will help you see the issues more easily.

0 Likes
Message 9 of 11

smallƑish
Advocate
Advocate

My copy-paste attitude made the file a little mess. Please accept my apology.

I have a final question. For example, if you select all 5 lines correctly, and the 6th line made a selection of 1st line (by mistake)(as per the attached gif the second time command run) still you are getting back to user_osmode?

 

 

2 case (1).gif

0 Likes
Message 10 of 11

Sea-Haven
Mentor
Mentor

Read my post Again & Again I think 3rd time I made a suggestion about dragging over the lines in sequence, stops the miss picks.

 

If an administrator reads this, needs like the 3 separate posts to be merged into 1 so conflicting answers are not being provided.

0 Likes
Message 11 of 11

ВeekeeCZ
Consultant
Consultant

@smallƑish wrote:

... For example, if you select all 5 lines correctly, and the 6th line made a selection of 1st line (by mistake)(as per the attached gif the second time command run) still you are getting back to user_osmode?


 

In that case you need to add *error* function that will restore it. 

 

Here is some example... note that *error* IS and NEEDS to be localized. Just the purple line is essential, the rest can always be the same.

 

Also note the structure...

(if (and (pick first point...)

              (pick second point...)

              (pick third point...)

              )

    (do stuff)

)

... so it's doing the stuff only if the user sets all the points...

 

(defun c:LMID ( / *error* osm 1a 1b 1m 2a 2b 2m)
  
  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break,end"))
      (princ (strcat "\nError: " errmsg)))
    (if osm (setvar 'osmode osm))
    (princ))
  
  (setq osm (getvar 'osmode))
  (setvar 'osmode 512)
  
  (if (and (setq 1a (getpoint "\nPoint 1a: "))
	   (setq 1b (getpoint 1a "\nPoint 1b: "))
	   (setq 1m (mapcar '/ (mapcar '+ 1a 1b) '(2 2)))
	   (setq 2a (getpoint "\nPoint 2a: "))
	   (setq 2b (getpoint 2a "\nPoint 2b: "))
	   (setq 2m (mapcar '/ (mapcar '+ 2a 2b) '(2 2)))
	   )
    (progn
      (command "_.line" "_non" 1m "_non" 2m "")
      (princ "\n>> LINE")))
  (if osm (setvar 'osmode osm))
  (princ)
  )