@ArchD wrote:
I'm looking for a lisp that converts a 3D Polyline to a 2D Polyline set to the average elevation of the original 3D Polyline elevations. ....
Here's another kind-of interesting way to do it, using VLA Properties instead of entity data, so it doesn't need to mess with the fact that each vertex of a 3DPolyline is its own entity. Interestingly, you can impose a list of Coordinates on a Polyline object this way even without the number of vertices matching, so this just draws a little starting Polyline of one segment, and then imposes the XY-only list of Coordinates from the 3DPoly on that, and the average Elevation from the Z coordinates. [It's not a weighted average affected by segment lengths, as discussed in recent posts.]
(defun C:3DLWAvg ; = 3D polyline to LW polyline at Average elevation
(/ 3dpoly 3dcoords 3dpobj n XYs Zs lwp)
(setq
3dpoly (car (entsel "\n3DPolyline to make Average-elevation LWPolyline from: "))
3dcoords (vlax-get (setq 3dpobj (vlax-ename->vla-object 3dpoly)) 'Coordinates)
n -1
); setq
(repeat (/ (length 3dcoords) 3); for each vertex's worth of XYZ coordinates,
(setq
XYs (append XYs (list (nth (setq n (1+ n)) 3dcoords))); X coordinate
XYs (append XYs (list (nth (setq n (1+ n)) 3dcoords))); Y [into same list as X]
Zs (append Zs (list (nth (setq n (1+ n)) 3dcoords))); Z [separate list for Elevation]
); setq
); repeat
(command "_.pline" (getvar 'viewctr) "@1,0" ""); temporary quickie basis object
(vlax-put (setq lwp (vlax-ename->vla-object (entlast))) 'Coordinates XYs)
(vlax-put lwp 'Elevation (/ (apply '+ Zs) (length Zs))); average elevation
(vla-put-Closed lwp (vla-get-Closed 3dpobj))
(princ)
); defun
Lightly tested, and it could use all the usual other enhancements [though the typical Osnap control is not needed even though there's a (command) function involved], as well as some possible more-particular things like making sure PLINEWID is set to 0 [or setting that within the Pline command] if desired.
[I started out trying (cons) rather than (append) to build the separated-coordinates lists, which would use less code, but with these kinds of values that results in compounded dotted-pair lists.]
Kent Cooper, AIA