vlax routine perpendicular lines along polyline.

I am working on this routine to draw perpendicular along a polyline. This routine will be creating some 3d geometry based on a couple of 3D pick points. The problem I haven't figured out just yet is how do I calculate the perpendicular angle at a specified distance along the polyline.

I have attached the code that will place a POINT entity at the selected places. The inserted point will be replaced with some code to draw the necessary 3d geometry. I already have the 3d geometry code written. I just need to calculate the perpendicular angle of the polyline. The polyline will most likely contain some kind of arc and possible more than one arc and a tangent.

Code:

`(defun C:TEST ()`

(setq ENTITY (entget (car (entsel "\nSelect alignment: ")))

VLAX-ENTITY (vlax-ename->vla-object (cdr (assoc -1 ENTITY)))

VLAX-LENGTH (if (vlax-property-available-p VLAX-ENTITY 'length)

(vlax-get-property VLAX-ENTITY 'length)

)

)

(setq OSM (getvar "osmode"))

(if VLAX-LENGTH

(progn

(setq INCREMENT (fix (/ VLAX-LENGTH 3.0)) ;_ Arbetrary length of 3.0 for testing.

INCREMENT (/ VLAX-LENGTH INCREMENT)

)

(setvar "osmode" 0)

(repeat (- (fix (/ VLAX-LENGTH 3.0)) 1)

(setq VLAX-LENGTH (- VLAX-LENGTH INCREMENT))

(setq TempPoint (vlax-curve-getpointatdist VLAX-ENTITY VLAX-LENGTH))

(command "_.point" TempPoint)

; This is the place in the code I need to calculate

; the perpendicular angle of the polyline. As you can

; see this angle will change based on where along the

; polyline the point is located.

)

)

)

(vlax-release-object VLAX-ENTITY)

(setvar "osmode" OSM)

)

RE: vlax routine perpendicular lines along polyline.

**vlax-curve-getFirstDeriv** will return a direction vector indicating the direction of the polyline at the given point. If the point is in a curve, the direction vector is the tangent at that point. You will need to rotate the bearing ±90° yourself to get the crossing vector.

Working with polylines can get a little tricky, since the polyline has a certain direction. The vector returned by vlax-curve-getFirstDeriv is based on the polyline direction, which is determined by how the polyline is created.

(But unlike arcs, at least polylines HAVE a direction... ;) Arcs always go counter-clockwise, which is sometimes a royal pain.)

RE: vlax routine perpendicular lines along polyline.

It depends on what kind of object you are calculating, line , arc, ellipse . . .

Here is a way point :

Code:

`(setq Ent (entsel "Select the polyline curve : " ) )`

(vl-load-com )

(setq VlaObj (vlax-ename->vla-object (car Ent )) )

(setq MyDist 250 ) ; <-- Your distance input

(setq Param (vlax-curve-getParamAtDist VlaObj MyDist ) )

line :

(setq Ang (- (* (/ (Angle (vlax-curve-getFirstDeriv VlaObj Param ) (vlax-curve-getSecondDeriv VlaObj Param ) ) pi ) 180 ) 90 ) )

arc :

(setq Ang (- (* (/ (Angle (vlax-curve-getFirstDeriv VlaObj Param ) (vlax-curve-getSecondDeriv VlaObj Param ) ) pi ) 180 ) 45 ) )

: ) Happy Computing !

kennet

RE: vlax routine perpendicular lines along polyline.

Quote:

Originally Posted by **Opie**

Thanks kennet. That gives me something to work with. Why is the arc at 45 degrees instead of 90 degrees?

That confuse me to, and I do not really know why, but for me it works that way. . and then you have the ellipse.

You have to test before you permanent the code, I may be wrong in this case.

: ) Happy Computing !

kennet

RE: vlax routine perpendicular lines along polyline.

The first derivative will give the slope at any point on a curve that is continuous, but in the case of a vertex, where the curve is not continuous, it will not. One suggestion is to use the vlax-curve-getpointatdist function to get the point 0.001 in front of your point and 0.001 behind your point and then calculate the angle between the two and then add pi/2 to get the angle of your perpendicular line. Using the polar command it would be easy to draw the line.

Another way is to determine the slope of one segment using the derivative and the slope of the next segment using the derivative and then average them and then add pi/2.

Peter

RE: vlax routine perpendicular lines along polyline.

WARNING: **vlax-curve-getSecondDeriv** returns 0 for lines and line segments.

RE: vlax routine perpendicular lines along polyline.

Quote:

Originally Posted by **peter**

The first derivative will give the slope at any point on a curve that is continuous, but in the case of a vertex, where the curve is not continuous, it will not. One suggestion is to use the vlax-curve-getpointatdist function to get the point 0.001 in front of your point and 0.001 behind your point and then calculate the angle between the two and then add pi/2 to get the angle of your perpendicular line. Using the polar command it would be easy to draw the line.

Another way is to determine the slope of one segment using the derivative and the slope of the next segment using the derivative and then average them and then add pi/2.

Peter

This is a little confusing, but it raises a valid point.

I don't think Peter means "continuous" - by definition, a polyline must be continuous. But for a polyline, the derivatives at each vertex are based on the following segment of the polyline (the "course out" of the vertex, so to speak). So a problem arises when there is an angle point at the vertex (i.e., the "course in" and the "course out" of the vertex are different).

As an example, assume you have a polyline with a bunch of line segments (no arcs). The first derivative at param=4.0 would be the same as the first derivative at param=4.01, but not the same as the first derivative at param=3.99. If the segment between param=4.0 and param=5.0 is an arc segment, the derivatives at param=4.0 yield tangent and radial vectors at the beginning of the arc segment. But in either case, the derivatives at param=4.0 are for the "course out", or the segment between param=4.0 and param=5.0. The only way to determine the values for the "course in" is to get the derivatives at a point where the param<4.0 by just a hair.

So, Peter's method will split the difference between the "course in" and the "course out", which may or may not be exactly what you want. The only thing I would add is you may want to make sure you calculate points around your vertex using distance methods, and not parameters. For polylines, the distance between parameters varies, depending on the length of the polyline segments.