Message 1 of 5
Polyline total lisp: Issues with rotation angle
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello I was wondering if anyone can help me with a section of my lisp. I wrote a lisp that will: prompt the user to select polylines on a certain layer, specify what type of line it is, specify scale of the block, gets the total vertices of each polyline, gets the total length of the polyline, and then places a block at the midpoint of vertices 1 and 2 with the total footage and offsets it by 4. My problem is getting the correct rotation angle for each polyline. Polylines that are drawn from left to right are correct. Any polyline that is drawn from right to left is either upside-down or not anywhere close to the midpoint of vertices 1 and 2. See the uploaded screenshot as a reference.
(defun c:Crossing (/ UserLayer TrenchType TxtScale pl plObj vts pt1 pt2 CrossingDist ang blkoffset midptoffset angstr FootageTick tlen tlenTick)
(setq UserLayer (getvar "CLAYER"))
(setvar "CLAYER" "K-TRSECT$")
(setvar "ATTDIA" 0)
(initget "M1 P1")
(setq TrenchType (strcase (getstring "\nSpecify crossing type: [M1/P1]? ")))
(cond
((= "M1" TrenchType)
(setq TrenchType "M1"))
((= "P1" TrenchType)
(setq TrenchType "P1"))
((= "M" TrenchType)
(setq TrenchType "M1"))
((= "P" TrenchType)
(setq TrenchType "P1"))
)
(initget "20 30")
(setq TxtScale (getstring "\nDetermine the scale of your crossing text: [20/30] scale "))
(cond
((= "2" TxtScale)
(setq TxtScale "2.5"))
((= "3" TxtScale)
(setq TxtScale "3.5"))
((= "20" TxtScale)
(setq TxtScale "2.5"))
((= "30" TxtScale)
(setq TxtScale "3.5"))
)
(setq pl (ssget '((0 . "LWPOLYLINE") (8 . "K-XING"))))
(if pl
(progn
(setq e 0)
(repeat (sslength pl)
(setq ent (ssname pl e))
(setq plObj (vlax-ename->vla-object ent))
(setq vts (mapcar 'cdr (vl-remove-if-not '(lambda (x) (vl-position (car x) '(10 11))) (entget ent))))
(setq pt1 (car vts))
(setq pt2 (cadr vts))
(setq CrossingDist (distance pt2 pt1))
(if (< (car pt1) (car pt2))
(setq ang (angle pt1 pt2))
(setq ang (- (angle pt2 pt1)))
) ;end ang if
(setq blkoffset 4)
(setq midptoffset (polar (polar pt1 ang (/ CrossingDist 2)) (+ ang (/ pi 2)) blkoffset))
(setq angstr (angtos ang))
(setq FootageTick "'")
(setq tlen (vla-get-length plObj))
(setq tlen (fix (+ tlen (if (minusp tlen) -0.5 0.5))))
(setq tlen (rtos tlen))
(setq tlenTick (strcat tlen FootageTick))
(command "-insert" "K-SEC" "s" TxtScale midptoffset angstr TrenchType tlenTick "")
(setq e (1+ e))
) ;end repeat
) ;end progn
(prompt "\nNo Crossings have been selected")
) ;end first if
(setvar "CLAYER" UserLayer)
(setvar "ATTDIA" 1)
(princ)
) ;end defun