Jump to content
Developer Wiki and Function Reference Links Read more... ×
Sam Jones

3D Polyline length

Recommended Posts

Is there a way to retrieve the length of a 3D Poly?  Do I have to get the vertices locations with GetPolyPt3D and then compute the distances between them?


Share this post

Link to post

Unless @MullinRJ or @JBenghiat or @Julian Carr has a solution, I think you need to write your own subroutine to walk the vertices and add up the lengths.


I would probably just write a function to take the object handle and walk the vertices and use Distance3D to get the distances.

Share this post

Link to post

Hi Sam,

   After a quick search, I could not find a function that works on 3D Poly, like hPerim() works on 2D Polys. It may be out there, but I could write the routine to sum the legs faster than I could search for a function that does or doesn't exist. But, @Pat Stanford is correct, I've already done that and found one in my script archive. Damn, I wrote it more than a decade ago. I'm getting old.


   Hey, it's another example using Vectors. DOUBLE BONUS!

PROCEDURE Get3DPolyLen_Example; 
{ 20 Dec 2008 - Raymond J Mullin } 

	function Get3DPolyLen(H :Handle) :Real;
	{ Return the perimiter length of 3DPoly H. If H is not a 3DPoly, return 0. }
		I, N :Integer; 
		L :Real; 
		P0, Pa, Pb :Vector;
		L := 0;
		if (GetType(H) = 25) then begin 
			N := GetVertNum(H); 
			GetPolyPt3D(H, 0, P0.x, P0.y, P0.z);	{ 1st vertex }
			Pa := P0;		{ copy of 1st vertex }
			for I := 1 to N-1 do begin 
				GetPolyPt3D(H, I, Pb.x, Pb.y, Pb.z); 	{ vertices 2 through N }
				L := L + Norm(Pb-Pa);		{ NORM is the vector DISTANCE function }
				Pa := Pb;
			end;		{ for }
			if IsPolyClosed(H) then  
				L := L + Norm(P0-Pb);	{ distance from last to first points }
		end;		{ if GetType }
		Get3DPolyLen := L;
	End;		{ Get3DPolyLen }

	AlrtDialog(concat('3D Poly Length = ', Num2StrF(Get3DPolyLen(FSActLayer)))); 





PS - if a native function exists, my bet is @Julian Carr or @JBenghiat would know.

Share this post

Link to post

This might be quicker. Convert a copy to nurbs then get its length using HLength():


Procedure T;
    hTemp, h3DPoly : HANDLE;
    h3DPoly := FSActLayer;
    hTemp := ConvertToNURBS(h3DPoly, True);
    Message('3D Poly Length: ', HLength(hTemp));

Share this post

Link to post

Hi Julian,

   Interesting approach. I knew you'd approach it differently than I. When you said your code might be quicker, it got me thinking. Your code is definitely more concise (quicker to code), but with the create and delete operations the execution speed can be an issue if large numbers of Polys need processing.


   For small object counts (<1000), both routines execute fast enough to be efficient. However, given enough objects the NURBS routine can appear painfully slow. I measured the two routines processing 1K, 10K, and 100K 3D Polygons. Each poly had 10 vertices. Here's what I got.


   Poly  |       JC        RM    |      JC         RM

Count  |    Ticks    Ticks  |    Time     Time

    1K    |       57           4    |     .95s       .07 s

  10K    |    551         21    |    9.2 s     0.35 s

100K    |  5586      175    |  93.1 s     2.92 s


   Sam, Bottom Line: pick the routine that will work best for your application based on your expected object count. 1-1000 Polys, shorter code looks better and is easier to support. >1000 Polys, summing the vertex distances will be much faster. VectorScript can get data from objects and process numbers and calculations much faster than it can create and delete objects. Just something to keep in mind when designing your coding approach.




Share this post

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


7150 Riverwood Drive, Columbia, Maryland 21046, USA   |   Contact Us:   410-290-5114


© 2018 Vectorworks, Inc. All Rights Reserved. Vectorworks, Inc. is part of the Nemetschek Group.