Visual LISP, AutoLISP and General Customization

## Visual LISP, AutoLISP and General Customization

Valued Contributor
Posts: 54
Registered: ‎11-12-2012
Message 1 of 4 (563 Views)

# Angle of polyline segment at specified point

563 Views, 3 Replies
03-19-2013 11:47 AM

Hi, hope somebody can help me out.

I am writing a routine that will place a piece of text at a specified point along a polyline. My problem is to determine the angle for the text. It needs to be the same as the segment of the polyline that it falls on. Just can't get my head around how to get that angle?

I can get a list of the vertex point of the polyline, but how do I determine the segment the point falls on?

Any help would be appreciated.

cj

*Expert Elite*
Posts: 5,237
Registered: ‎09-13-2004
Message 2 of 4 (554 Views)

# Re: Angle of polyline segment at specified point

03-19-2013 12:51 PM in reply to: cjacobi

cjacobi wrote:

....

I am writing a routine that will place a piece of text at a specified point along a polyline. My problem is to determine the angle for the text. It needs to be the same as the segment of the polyline that it falls on. ....

The way I've done that kind of thing is to take the first derivative of the Polyline at the selected point, which will give you a virtual point location at that angle relative to 0,0,0.  Try incorporating elements of this however they need to fit into whatever code you already have going:

(setq

esel (entsel "\nSelect Polyline: ")
pol (car esel); the Polyline's entity name

pt (osnap (cadr esel) "nea"); use Osnap to ensure it's really ON the Polyline
ang
(angle
'(0 0 0)
(vlax-curve-getFirstDeriv
pol
(vlax-curve-getParamAtPoint pol pt)
); end 1st deriv
); end angle & ang
); end setq

That will leave you with the angle stored in the 'ang' variable in radians, so you can use it directly that way if you're adding the Text using (entmake), or you would need to convert it with (angtos) to answer the rotation prompt in a Text command.

It can also be done by looking for the adjacent vertices, which is easier to do with Parameter values than with the coordinates from the entity data, something like this:

(setq

esel (entsel "\nSelect Polyline: ")
pol (car esel); the Polyline's entity name

pt (osnap (cadr esel) "nea"); use Osnap to ensure it's really ON the Polyline
par (fix (vlax-curve-getParamAtPoint pol pt)); the parameter value of the prior [upstream] vertex

ang
(angle
(vlax-curve-getPointAtParam pol par); location of upstream vertex
(vlax-curve-getPointAtParam pol (1+ par)); location of downstream vertex

); end angle & ang
); end setq

That's shorter than the first suggestion, but the first suggestion has the advantage [where applicable] that it will give the correct result even on Polyline arc segments, whereas the second suggestion will only work as you expect on line segments [unless you happen to pick an arc segment exactly at its midpoint].

Kent Cooper
Valued Contributor
Posts: 54
Registered: ‎11-12-2012
Message 3 of 4 (548 Views)

# Re: Angle of polyline segment at specified point

03-19-2013 01:00 PM in reply to: Kent1Cooper

that's awesome Kent, thanks so much.  Will try and work this into my code and hopefully get my command functioning the way I need to.

I do need to concider there may be arcs invovled, so thanks again for providing two options.

cj

*Expert Elite*
Posts: 5,237
Registered: ‎09-13-2004
Message 4 of 4 (544 Views)

# Re: Angle of polyline segment at specified point

03-19-2013 01:10 PM in reply to: cjacobi

cjacobi wrote:

that's awesome Kent, thanks so much.  Will try and work this into my code and hopefully get my command functioning the way I need to.

I do need to concider there may be arcs invovled, so thanks again for providing two options.

....

You're welcome.  You could also use the same [presumably with a change in the selection prompt] on any object in the "vlax-curve" class [which doesn't necessarily mean it has to be "curved" by the generic definition] -- Line, Arc, Circle, Ellipse, Spline, Xline, Ray, Leader, maybe a couple of other things.

Kent Cooper

### You are not logged in.

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.