Jump to content

Recommended Posts

Posted (edited)

hey every one,

                            i have to apply error trapper for above code, when user select same point twice then angle between  that  two point will

be zero

(defun angles_two_vect (v1 v2 / ang xa ya za xb yb zb acos asin)
(defun acos (x) (cond ((and(>= x -1.0)(<= x 1.0)) (-(* pi 0.5) (asin x)))))
(defun asin (x) (cond ((and(> x -1.0)(< x 1.0)) (atan (/ x (sqrt (- 1.0 (* x x)))))) ((= x -1.0) (* -1.0 (/ pi 2))) ((= x  1) (/ pi 2)) ) ) 
	(mapcar 'set '(xa ya za) v1)
	(mapcar 'set '(xb yb zb) v2)
	(setq ang 
		(acos 
			(/
				(+ (* xa xb)(* ya yb)(* za zb)) 
				(* (sqrt (+(* xa xa)(* ya ya)(* za za)))(sqrt (+(* xb xb)(* yb yb)(* zb zb))))
			)
		)
	)
	(if (> zb za) (setq ang (- (* pi 2.0) ang))) ; to test angle relative to horizontal vector in plane givs angle in full circle
	ang
)


(defun c:test2 ()
  (setq p1 (getpoint)
	p2 (getpoint)
	p3 (getpoint)
	p4 (getpoint)
	p5 (getpoint)
	p6 (getpoint)
	p7 (getpoint)
	p8 (getpoint)
	v1 (mapcar '- p2 p1)
	v2 (mapcar '- p3 p2)
	v3 (mapcar '- p4 p3)
	v4 (mapcar '- p5 p4)
	v5 (mapcar '- p6 p5)
	v6 (mapcar '- p7 p6)
	v7 (mapcar '- p8 p7)
	v8 (mapcar '- p8 p1)
	oldsnap (getvar 'osmode)
	)
  (setvar 'osmode 0)
  ( setq ang1 (angles_two_vect  v1  v8)
         ang2 (angles_two_vect  v2  v8)
         ang3 (angles_two_vect  v3  v8)
         ang4 (angles_two_vect  v4  v8)
         ang5 (angles_two_vect  v5  v8)
         ang6 (angles_two_vect  v6  v8)
         ang7 (angles_two_vect  v7  v8)
  )
  (setq 1p2 (polar p1 ang1 (distance p1 p2))
	1p3 (polar 1p2 ang2 (distance p2 p3))
	1p4 (polar 1p3 ang3 (distance p3 p4))
	1p5 (polar 1p4 ang4 (distance p4 p5))
	1p6 (polar 1p5 ang5 (distance p5 p6))
	1p7 (polar 1p6 ang6 (distance p6 p7))
	1p8 (polar 1p7 ang7 (distance p7 p8))
	)
  (command "pline" p1 1p2 1p3 1p4 1p5 1p6 1p7 1p8 "c")
  (setvar 'osmode oldsnap)
  )

 

Edited by Halsy
Posted

Try this to avoid selecting two points twice...

 

  (setq p1 (getpoint)
	p2 (getpoint p1)
	p3 (getpoint p2)
	p4 (getpoint p3)
	p5 (getpoint p4)
	p6 (getpoint p5)
	p7 (getpoint p6)
	p8 (getpoint p7)
  )

 

  • Like 1
Posted
(defun angles_two_vect (v1 v2 / ang xa ya za xb yb zb acos asin)
(defun acos (x) (cond ((and(>= x -1.0)(<= x 1.0)) (-(* pi 0.5) (asin x)))))
(defun asin (x) (cond ((and(> x -1.0)(< x 1.0)) (atan (/ x (sqrt (- 1.0 (* x x)))))) ((= x -1.0) (* -1.0 (/ pi 2))) ((= x  1) (/ pi 2)) ) ) 
	(mapcar 'set '(xa ya za) v1)
	(mapcar 'set '(xb yb zb) v2)
	(setq ang 
		(acos 
			(/
				(+ (* xa xb)(* ya yb)(* za zb)) 
				(* (sqrt (+(* xa xa)(* ya ya)(* za za)))(sqrt (+(* xb xb)(* yb yb)(* zb zb))))
			)
		)
	)
	(if (> zb za) (setq ang (- (* pi 2.0) ang))) ; to test angle relative to horizontal vector in plane givs angle in full circle
	ang
)

(defun _getpoint ( p1 txt / p2 i )
  (setq p2 p1)
  (setq i 0)
  (while (= p2 p1)
    (setq p2 (getpoint p1 (strcat (if (> i 0) "\n Same point as previous point. Please select again." "") txt)))
    (setq i (+ i 1))
  )
  p2
)

(defun c:test2 ()
  (setq p1 (getpoint "\n Select P1"))
  (setq p2 (_getpoint p1 "\n Select P2"))
  (setq p3 (_getpoint p2 "\n Select P3"))
  (setq p4 (_getpoint p3 "\n Select P4"))
  (setq p5 (_getpoint p4 "\n Select P5"))
  (setq p6 (_getpoint p5 "\n Select P6"))
  (setq p7 (_getpoint p6 "\n Select P7"))
  (setq p8 (_getpoint p7 "\n Select P8"))
  (setq v1 (mapcar '- p2 p1)
	v2 (mapcar '- p3 p2)
	v3 (mapcar '- p4 p3)
	v4 (mapcar '- p5 p4)
	v5 (mapcar '- p6 p5)
	v6 (mapcar '- p7 p6)
	v7 (mapcar '- p8 p7)
	v8 (mapcar '- p8 p1)
	oldsnap (getvar 'osmode)
	)
  (setvar 'osmode 0)
  ( setq ang1 (angles_two_vect  v1  v8)
         ang2 (angles_two_vect  v2  v8)
         ang3 (angles_two_vect  v3  v8)
         ang4 (angles_two_vect  v4  v8)
         ang5 (angles_two_vect  v5  v8)
         ang6 (angles_two_vect  v6  v8)
         ang7 (angles_two_vect  v7  v8)
  )
  (setq 1p2 (polar p1 ang1 (distance p1 p2))
	1p3 (polar 1p2 ang2 (distance p2 p3))
	1p4 (polar 1p3 ang3 (distance p3 p4))
	1p5 (polar 1p4 ang4 (distance p4 p5))
	1p6 (polar 1p5 ang5 (distance p5 p6))
	1p7 (polar 1p6 ang6 (distance p6 p7))
	1p8 (polar 1p7 ang7 (distance p7 p8))
	)
  (command "pline" p1 1p2 1p3 1p4 1p5 1p6 1p7 1p8 "c")
  (setvar 'osmode oldsnap)
  )

 

how about this?

Posted

no i have to apply vl-catch-all-apply this to above code and set ang to zero when two points are same

when user select same point twice then program gives error as error divide by zero

Posted
10 minutes ago, Halsy said:

no i have to apply vl-catch-all-apply this to above code and set ang to zero when two points are same

when user select same point twice then program gives error as error divide by zero

 

This means that you don't have to create error handler, but just force user to use different points, otherwise if they are the same, error should happen - divide by zero...

Posted

dont have to force user to select different point but same time user have to select same point that time this error happens, so  that i have to trap that error using vl fuction and set that angle to zero

Posted

I'd only use an error trap to catch something where the user does something unexpected - if it is expected that they will select the same points then confirmation and adjustments for this should be built in the code. You have a lot more options to report back problems if you are anticipating them and programming accordingly

Posted

Yes i accept that user select same point. for that i have to trap that error and set angle equal to zero for that line but i dont know how to trap that error using vl-catch-all-apply function

Posted (edited)

Selecting the same point is not functional Error. so it cannot be treated as an error

so you need to get error from divide by 0.

 

https://help.autodesk.com/view/ACDLT/2024/ENU/?guid=GUID-2138B8F3-F0E1-4DB4-97C4-EC26047550E0

this example is how to use vl-catch-all-apply when dividing by 0 is performed.

 

In this case, you know user input and you know which patterns are causing the problem.

If so, it is right to guide it on the right path before accident happens.

https://www.cadtutor.net/forum/topic/27768-vl-catch-all-apply-catching-errors/?do=findComment&comment=222867

 

Edited by exceed
Posted

It needs some form of loop checking is px = pnew. something along the lines of this it may need a loop function and 2 defuns for a yes no situation.

 

(setq x 1)
(setq p1 (getpoint "\n Select P1"))
(set (read (strcat "p" (rtos (1+ x) 2 0))) (getpoint (eval (read (strcat "p" (rtos x 2 0)))) "\n Select next point "))
(setq x (1+ x))

Still thinking about it.

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