@phanaem wrote:
....
Finding the average angle is the key. Then you can apply .... Now I have this new version but I'm not sure is foolproof.
Here's a brute-force but I think foolproof [though lightly tested] way to find a kind of "average" angle of sort-of-same-direction Lines, regardless of in which comparative direction each was drawn, and/or whether their directions vary slightly crossing that pesky 0-degree direction, etc.
It Copies each Line other than the first one to put the copy's start point at the start point of the first one. If the copy's endpoint is farther from the first one's endpoint than their common start point is, the copy is "aiming" the wrong way, and it spins it around 180 degrees. Then it adds its endpoint into a running sum, and deletes the copy. When it's done that with all of them, it averages the endpoints [divides the coordinates of the accumulated sum by the quantity], and finds the angle from the common start point to there.
(setq osm (getvar 'osmode))
(setvar 'osmode 0)
(setq
ss (ssget '((0 . "LINE")))
qua (sslength ss)
start0 (vlax-curve-getStartPoint (setq line1 (ssname ss 0)))
sumend (setq end0 (vlax-curve-getEndPoint line1))
length0 (distance start0 end0)
); setq
(repeat (1- (setq n qua))
(command "_.copy" (setq line (ssname ss (setq n (1- n)))) "" (vlax-curve-getStartPoint line) start0)
(if
(> (distance end0 (vlax-curve-getEndPoint (setq line (entlast)))) length0); aims away?
(command "_rotate" line "" start0 "180"); then -- spin it
); if
(setq sumend (mapcar '+ sumend (vlax-curve-getEndPoint line)))
(entdel line)
); repeat
(setq angavg (angle start0 (mapcar '(lambda (c) (/ c qua)) sumend)))
; angle from common start point to averaged endpoint
(setvar 'osmode osm)
The result will not always be the true mathematical average of the directions, but [again, when they're not too far from parallel] should be close enough for purposes of this thread. It does give a "better" result from one point of view than a true mathematical average of only the angles of the Lines, in that it's "weighted" -- a longer Line has a greater impact on the result than a shorter one has. It seems to handle a fair degree of angular difference, but certain combinations of a Line's direction being more significantly different than the general direction of the rest, and [because of the distance comparison] its length relative to that of the first Line in the selection, can throw off the result.
Because of the Copying, it requires that the selected Lines not be on locked Layers -- that could be worked around if necessary.
Kent Cooper, AIA