Jump to content

Recommended Posts

Posted

I'm trying to write a routine that scans all lines in a drawing and turns their color to magenta if their angle is not 0 or an increment of 15.

Like this:

 

 
(vlax-for
 obj mspace
     (if (= (vla-get-objectname obj) "AcDbLine")
 (progn
    (setq ent (vlax-vla-object->ename obj))
    (setq elist (entget ent))
    (setq pt1 (cdr (assoc 10 elist)))
    (setq pt2 (cdr (assoc 11 elist)))
    (setq chkang (* 180 (/ (angle pt1 pt2) pi)))
 (if (not (or (= chkang 0.0)
       (= chkang 15.0)
       (= chkang 30.0)
       (= chkang 45.0)
       (= chkang 60.0)
       (= chkang 75.0)
       (= chkang 90.0)
       (= chkang 105.0)
       (= chkang 120.0)
       (= chkang 135.0)
       (= chkang 150.0)
       (= chkang 165.0)
       (= chkang 180.0)
       (= chkang 195.0)
       (= chkang 210.0)
       (= chkang 225.0)
       (= chkang 240.0)
       (= chkang 255.0)
       (= chkang 270.0)
       (= chkang 285.0)
       (= chkang 300.0)
       (= chkang 315.0)
       (= chkang 330.0)
       (= chkang 345.0)
   )
     )
    (vla-put-color obj 6)
 )
 )
     )
  )

 

The problem is that this logic doesn't seem to work.

Even when the value of chkang is 45.0,

 

 
(= chkang 45.0)

 

has a return value of nil.

Can someone please explain to me why AutoCAD doesn't think that 45.0 is equal to 45.0?

I've tried using radians instead. Same outcome.

I've tried the fix and abs functions. No joy.

 

In fact, when chkang has a value of 45.0, a fix of chkang has a return value of 44!

 

Am I missing something or has my AutoCAD had a stroke?

Posted

Use the "equal" function with a fuzz factor over "=" to add some tolerance to the comparison.

Posted

You're routine would work if you put the angle values into radians (without testing)

 

(defun [b]rtd[/b](ar) (/(* ar 180.0)pi)) 

 

(= (rtd chkang) 15.0)

Posted

Consider the following code:

 

(defun c:checklines ( / a e i s )
   (if (setq s (ssget "_X" '((0 . "LINE") (410 . "Model"))))
       (repeat (setq i (sslength s))
           (setq e (entget (ssname s (setq i (1- i))))
                 a (rem (* 180.0 (/ (angle (cdr (assoc 10 e)) (cdr (assoc 11 e))) pi)) 15)
           )
           (if (not (or (equal 0.0 a 1e- (equal 15.0 a 1e-))
               (entmod (append e '((62 . 6))))
           )
       )
   )
   (princ)
)

Posted
You're routine would work if you put the angle values into radians (without testing)

 

(defun [b]rtd[/b](ar) (/(* ar 180.0)pi)) 

(= (rtd chkang) 15.0)

 

(setq chkang (* 180 (/ (angle pt1 pt2) pi)))

 

He did ^^...

Posted

Very good suggestions. Thank you!

It may not have been the best way to do it, but I made it work by converting the angle in radians over to a string using angtos and then used strings for my comparison.

When using strings, it returns the expected result.

I still think that's odd that I have to do that, though.

Posted
It may not have been the best way to do it, but I made it work by converting the angle in radians over to a string using angtos and then used strings for my comparison.

When using strings, it returns the expected result.

 

I would still recommend using the equal function, equally when working with comparison of point lists. Perhaps study how I have performed the calculation in my above example.

 

I still think that's odd that I have to do that, though.

 

Not too odd when you think about how data is stored and manipulated in a computer. AutoLISP Reals are stored as 32-bit Doubles, which offer 15 decimal places of precision (base 10), not an infinite level of precision as found in mathematics. This means for every arithmetical operation, rounding occurs at the 15 decimal place, introducing a small but cumulative error. (More info here).

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