Jump to content

Know the angle after selecting a line


Leika

Recommended Posts

Hello,
I'm trying to know when selecting a line the angle of the line.
The code works well with lines, but with a polyline I have to explode it first and select it again.
I would have liked that after the explode I know the angle immediately .
 

This is my code :

 

(defun C:AAA (/ lisel ln pt1 pt2)
    ;(vl-load-com)
    (while
        (setq lisel (entsel "\nSelect line <exit>: "))
        (if (wcmatch (cdr (assoc 0 (entget (setq ln (car lisel))))) "LINE") ; wcmatch performs a wild-card pattern match on a string
            (progn
                (prompt "\nSelected object is line.")
                (setq lisel (car lisel))
                (setq pt1 (cdr (assoc 10 (entget lisel))))
                (setq pt2 (cdr (assoc 11 (entget lisel))))
            
                (prompt "\n|")
                (prompt "\n|")
                (princ (strcat "\n| Degrees = " (angtos (angle pt1 pt2)) "°"))
                (princ (strcat "\n| Radians = " (rtos (angle pt1 pt2) 2 3)"\n"))
                (princ "\n|")
                (princ)    
            )
            (progn
                (command "_explode" ln)
                (prompt "\nNO LINE - Please select again.")
            )
        )
    )
    (princ)
)
(C:AAA)

 

Thank you to read this  🙂

Link to comment
Share on other sites

If you are selecting polyline with two vertices then just use angle function on the two CG 10 as follows:

(angle (cdr (assoc 10 (entget ln))) (cdr (assoc 10 (reverse (entget ln))))

 

  • Like 1
Link to comment
Share on other sites

Maybe this.

 

; Pline segment with angle

(defun c:plseg( / oldsnap pick plobj pick2 param segemnt co-ord pt1 pt2 ang)
(setq plent (entsel "\nSelect Pline or line "))
(setq oldsnap (getvar 'osmode))
(setvar "osmode" 0)
(cond 
((= (cdr (assoc 0 (entget (car plent)))) "LWPOLYLINE")
(setq
      pick (cadr plent)
      plObj (vlax-ename->vla-object (car plent))
      pick2 (vlax-curve-getclosestpointto plobj pick)
      param (vlax-curve-getparamatpoint plObj pick2)
      segment (fix param)
	  co-ord (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget (car plent))))
       pt1 (nth segment co-ord)
       pt2 (nth (+ segment 1) co-ord))
)
((= (cdr (assoc 0 (entget (car plent)))) "LINE")
(setq lObj (vlax-ename->vla-object (car plent))
  pt1 (vlax-curve-getstartPoint lobj)
  pt2 (vlax-curve-getEndPoint lobj))
 )
((alert "incorrect object selected Pline or line"))
)
 (setq ang (angle pt1 pt2))
(alert (strcat "angle is "  (rtos (/ (* ang 180.0) pi) 2 2) ))
(setvar 'osmode oldsnap)
(princ)
)

 

Edited by BIGAL
  • Like 2
Link to comment
Share on other sites

@Tharwat, thank you very much, I know now how to do it with two vertices.

@Bigal, thank you very much for this code!
With closed polylines, one line will give this error: error: bad argument type: 2D/3D point: nil
I tested on a rectangle - the other lines work perfectly as i want!

I'm one big step further 🥳

Link to comment
Share on other sites

Hello,
Do any of you know where I should write between the code Bigal :
If the polyline give me that error (closed) then it need be explode and select again as in my first code.
Do you do that by placing assoc 70 somewhere in between?
Then I intercept the error...

Link to comment
Share on other sites

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