.NET

Reply
Mentor
SRSDS
Posts: 255
Registered: ‎04-15-2011
Message 1 of 10 (605 Views)

Spline Length

605 Views, 9 Replies
05-20-2013 03:04 PM

Hi,

Is there a way to determine the length of a spline?

Distinguished Mentor
gasty1001
Posts: 582
Registered: ‎04-11-2010
Message 2 of 10 (595 Views)

Re: Spline Length

05-20-2013 04:03 PM in reply to: SRSDS

Hi,

 

Look here: Curve Length , and also here: Quirky Polylines for more enlightenment.

 

Gaston Nunez

 

 

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 3 of 10 (591 Views)

Re: Spline Length

05-20-2013 04:47 PM in reply to: gasty1001

Hi Gasty, the code at the first link you referenced is not terribly robust. It will fail on curves that have no length (e.g., Rays and XLines), and assumes that the caller is passing the result of the 'as' operator to cast the object to a Curve, but not retaining the reference to the Curve (or having to do that again), and it doesn't leverage extension methods.

 

This extension method will return a bool to indicate if the a length was acquired, or false if not, and will set the result parameter to the curve's length.  You can use this method on any Curve without it throwing an exception, but you should still filter those types that consistently do not produce a result (like Rays and XLines), to avoid the higher cost of having to catch many thrown exceptions.

 

 

public static class CurveExtensions
{
   public static bool TryGetLength( this Curve curve, out double result )
   {
      try
      {
         result = curve.GetDistanceAtParameter( curve.EndParam ) -
            curve.GetDistanceAtParameter( curve.StartParam );
         return true;
      }
      catch( Autodesk.AutoCAD.Runtime.Exception )
      {
         result = 0.0;
         return false;
      }
   }
}

 

 

Distinguished Mentor
gasty1001
Posts: 582
Registered: ‎04-11-2010
Message 4 of 10 (583 Views)

Re: Spline Length

05-20-2013 05:34 PM in reply to: DiningPhilosopher

Hi Tony,

 

You are right, just had the link to spiderman in my references without checking. Nice way to go with that Tony.

 

Regards,

 

Gaston Nunez

*Expert Elite*
Alfred.NESWADBA
Posts: 9,573
Registered: ‎06-29-2007
Message 5 of 10 (547 Views)

Re: Spline Length

05-21-2013 11:13 AM in reply to: DiningPhilosopher

Hi,

 

>> curve.GetDistanceAtParameter( curve.StartParam )

May I ask what condition has to exist that this calculation will result in a value n.e. 0.0?

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 6 of 10 (542 Views)

Re: Spline Length

05-21-2013 12:09 PM in reply to: Alfred.NESWADBA

Try calling it on an Arc whose start angle is != 0.0

*Expert Elite*
Alfred.NESWADBA
Posts: 9,573
Registered: ‎06-29-2007
Message 7 of 10 (539 Views)

Re: Spline Length

05-21-2013 12:32 PM in reply to: DiningPhilosopher

Hi,

 

>> Try calling it on an Arc whose start angle is != 0.0

Well, tried, my result is 0.0 as you can see in the screenshot (that shows the debug-info from an arc with start-angle and end-angle != 0.0)

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 8 of 10 (511 Views)

Re: Spline Length

05-21-2013 07:06 PM in reply to: Alfred.NESWADBA

It is the StartParameter that is not always 0.0.  

 

The result of GetDistanceAtParameter() with the curve's start parameter is usually 0.0, but I have seen cases where it isn't, which involved custom objects derived from AcDbCurve, that return a non-zero value, and getting the actual length requires the algorithm shown, so it is designed to ensure that the actual distance is calculated, even if the distance at the start parameter is non-zero, which is an assumption I don't recommend relying on if dealing with custom objects.

 

 

*Expert Elite*
Alfred.NESWADBA
Posts: 9,573
Registered: ‎06-29-2007
Message 9 of 10 (507 Views)

Re: Spline Length

05-21-2013 07:45 PM in reply to: DiningPhilosopher

Hi,

 

>> The result of GetDistanceAtParameter() with the curve's start parameter is usually 0.0

That was my question, when this result gives != 0.0.

So the arc you suggested does return 0.0 for you too?

 

>> but I have seen cases where it isn't, which involved custom objects derived from AcDbCurve

In that case the custom objects returns a wrong value. And if it returns a defect length at the startparam I also would not trust any other calculation returned by GetDistanceAtParameter(), so also for the endparam. With other words the length calculation in that case might be wrong anyway and not to be trusted.

 

At least I never have seen that this return not 0.0 and so I'm wondering about that calculation within your function.

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 10 of 10 (503 Views)

Re: Spline Length

05-21-2013 08:00 PM in reply to: Alfred.NESWADBA

alfred.neswadba wrote:

Hi,

 

>> The result of GetDistanceAtParameter() with the curve's start parameter is usually 0.0

That was my question, when this result gives != 0.0.

So the arc you suggested does return 0.0 for you too?

 

- alfred -


No, actually I mistakenly thought you were asking about the value of the StartParameter, which is non-zero for arcs and some other objects.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.