Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Angle of polyline segment at specified point

9 REPLIES 9
Reply
Message 1 of 10
coralie.jacobi
4454 Views, 9 Replies

Angle of polyline segment at specified point

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

9 REPLIES 9
Message 2 of 10


@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:
 

(vl-load-com); just in case

(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, AIA
Message 3 of 10

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

Message 4 of 10


@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, AIA
Message 5 of 10
tcorey
in reply to: Kent1Cooper

The gift that keeps on giving, a year and a half later! Thanks, Kent.

 

Tim

 



Tim Corey
MicroCAD Training and Consulting, Inc.
Redding, CA
Autodesk Gold Reseller

New knowledge is the most valuable commodity on earth. -- Kurt Vonnegut
Message 6 of 10
stevor
in reply to: coralie.jacobi

May want to test the generated slope value at the ends of the polylines, as they may not be what you expect.
S
Message 7 of 10
Kent1Cooper
in reply to: stevor


@stevor wrote:
May want to test the generated slope value at the ends of the polylines, as they may not be what you expect.

True, though only if selected precisely at the very end.  Given what they are looking to do, that doesn't seem very likely, but it's something to be aware of.

Kent Cooper, AIA
Message 8 of 10
btlsp2014
in reply to: coralie.jacobi

Another approach would be to id the line segment of the polyline having an endpoint nearest the point(pt1) with which the polyline is selected and thereafter finding the slope of the line segment using the endpoints(pt2, pt3).  I have been away from lisp a number of years so it would be something if I could even stab at the code.  Would someone get me started?  Loop through the line segments.  Minimize "d".

 

min.png

Message 9 of 10
Kent1Cooper
in reply to: btlsp2014


@btlsp2014 wrote:

Another approach would be to id the line segment of the polyline having an endpoint nearest the point(pt1) with which the polyline is selected and thereafter finding the slope of the line segment using the endpoints(pt2, pt3). .....


That's what the second routine in my message with two routines in it does.  [I'd specify a Message number, but for the time being those are not showing -- I hope they'll come back soon.]

Kent Cooper, AIA
Message 10 of 10
PB1
Contributor
in reply to: Kent1Cooper

Hi Kent and everybody else,

 

So glad I found your routine:

 

 

(vl-load-com); just in case

(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

 

 

I have managed to integrate it in to a routine I have cobbled together from various sources over the years, its a real mess but works..

How can I edit this code above so that if a polyline isn't selected the ang variable is set to 0 deg by default?

 

I have attached my full lisp if it helps.

Many thanks.

Pads

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost