Distance between two points (XYZ) along a given vector

Distance between two points (XYZ) along a given vector

mhillis
Advocate Advocate
14,182 Views
13 Replies
Message 1 of 14

Distance between two points (XYZ) along a given vector

mhillis
Advocate
Advocate

Simple question, does the Revit API have a method of returning the distance between two points along a given vector?

 

I realize there are ways to manually do this, but I was wondering if the Revit API had something natively to make my code a little cleaner.

 

Thanks!

0 Likes
Accepted solutions (2)
14,183 Views
13 Replies
Replies (13)
Message 2 of 14

GonçaloFeio1321
Enthusiast
Enthusiast

You can use a Autodesk.Revit.DB.Line

There is a double Length property that it inherits from Curve.

 

 

0 Likes
Message 3 of 14

matthew_taylor
Advisor
Advisor

Hi @mhillis

Where pointA and pointB are XYZ points:

dim distance as double = pointA.DistanceTo(pointB)

The XYZ class also has .multiply and .divide functions if you want to go a proportion of that distance along a vector.

 

 

Cheers,

 

-Matt


Cheers,

-Matt
_______________________________________________________________________________
Marking a post as a 'solution' helps the community. Giving a post 'Kudos' is as good as saying thanks. Why not do both?
0 Likes
Message 4 of 14

GonçaloFeio1321
Enthusiast
Enthusiast

Shame on me, Mr. Taylor, shame on me for missing that! 

Message 5 of 14

matthew_taylor
Advisor
Advisor

Haha. I did a similar thing just yesterday, so don't feel bad! Smiley Very Happy


Cheers,

-Matt
_______________________________________________________________________________
Marking a post as a 'solution' helps the community. Giving a post 'Kudos' is as good as saying thanks. Why not do both?
0 Likes
Message 6 of 14

FAIR59
Advisor
Advisor
Accepted solution

If you need the distance of the points projected onto the given vector:

 

XYZ p1 = new XYZ(0, 10, 0);

XYZ p2 = new XYZ(10, 60, 0);

XYZ direction = new XYZ(30, 60, 0);

double distance = direction.Normalize().DotProduct(p1.Subtract(p2));

// double distance = Math.Abs( direction.Normalize().DotProduct(p1.Subtract(p2)));

 

Message 7 of 14

jeremytammik
Autodesk
Autodesk
Accepted solution

Dear Everybody,

 

I would award the main prize and cigar to Fair59.

 

Partly for providing what I guess might be the right answer.

 

Above all for guessing what may or may not be the right intended question.

 

Dear @mhillis, you might want to hone your question asking skills a bit.

 

Is this an accurate description of your needs?

point_dist_along_vector.png

 

 

Question: Given points p1 and p2, what is the distance between them, measured along the line L?

 

If that is indeed what you need, you are actually not asking about the distance between the points at all.

 

These two points, together with the direction w of L, define two planes.

 

You are asking about the distance between those two planes.

 

Are you?

 

If so, Fair's answer is absolutely accurate and can be reformulated as:

 

Answer:

 

  v = p2 - p1
  w = L.direction.normalise
  distance_along_w = v.dotProduct( w )

 

Unfortunately, the Revit geometry API is not full-fledged and therefore lacks a method to return the distance between two planes.

 

Otherwise, that would probably provide an even more straightforward path.

 

The suggested one is very direct and efficient, though, and hard to beat in those respects.

 

I hope this helps and that we collectively succeeded at nailing your intention.

 

Best regards,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 8 of 14

jeremytammik
Autodesk
Autodesk

I promoted this thread to a blog post for better legibility and future reference:

 

http://thebuildingcoder.typepad.com/blog/2017/01/distances-switches-kiss-ing-and-a-dino.html#2

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 9 of 14

Anonymous
Not applicable

It seems to me that everyone went a little too hard on this.  If you need a distance between two points:

double rDist = fLocation1.DistanceTo(fLocation2);

where `fLocation1` and `fLocation2` are `XYZ` data types (Autodesk.Revit.DB namespace)

Message 10 of 14

jeremytammik
Autodesk
Autodesk

Too hard or not... depends on whether you want the shortest distance between two points, or the distance along a given direction.

 

The latter is actually not the distance between two points, but the distance between two planes.

 

For safety's sake, I added this clarification to the existing blog post:

 

http://thebuildingcoder.typepad.com/blog/2017/01/distances-switches-kiss-ing-and-a-dino.html#2.1

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 11 of 14

mhillis
Advocate
Advocate

@Anonymous wrote:

Too hard or not... depends on whether you want the shortest distance between two points, or the distance along a given direction.

 

The latter is actually not the distance between two points, but the distance between two planes.

 

For safety's sake, I added this clarification to the existing blog post:

 

http://thebuildingcoder.typepad.com/blog/2017/01/distances-switches-kiss-ing-and-a-dino.html#2.1

 

Cheers,

 

Jeremy


Jeremy,

 

I am surprised to see this particular forum post has so much activity.   For further clarification for those who may travel here in the future, and upon retrospect, yes, you're absolutely correct in your initial assumption when I first posed this question.  I was looking for the distance between two planes, my initial question was a very round-a-bout way of getting to that point, but I am glad we were able to get there either way.

 

I didn't make a comment, but I did mark you and FAIR's response as an answer because they gave me exactly what I was looking for.  Putting together a small routine utilizing the information I was given gave me A) a nice, recallable routine, B) something that I can tailor to my needs.

 

As an aside, I realize that measuring between planes isn't something 'native' to Revit as you described, but being sort of pigeon-holed into making your own routines has the benefit of allowing me to make methods for simple functions that I can expand/modify as needed.

 

If it is, in some way, possible to edit my initial post for clarification to those in the future, that would be nice. 

0 Likes
Message 12 of 14

jeremytammik
Autodesk
Autodesk

Dear Mhillis,

 

Thank you very much for your confirmation and appreciation.

 

I am glad we were able to help and contribute to a clean, reliable and reusable solution.

 

I am not aware of a possibility to edit your original post, and I can imagine some reasons why that should be avoided...

 

That is one reason why I like summarising discussions like these on The Building Coder 🙂

 

... So, I added your new comment to the discussion there as well...

 

Cheers,

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 13 of 14

Eoghan_Hynes45Z5Y
Community Visitor
Community Visitor

What units does this solution produce?

 

From two ends of a Pipe segment of length 6.2 meters, I consistently get a distance value of 0.217 ?

0 Likes
Message 14 of 14

jeremy_tammik
Alumni
Alumni

The internal Revit database unit for length is imperial feet:

  

  

So, 6.2 metres will produce an XYZ distance of ca. 20.34:

  

 

one_inch = 25.4 # millimetres
one_foot = 12 * one_inch # imperial feet
mm_to_feet = 1 / one_foot
m_to_feet = 1000 * mm_to_feet
m_to_feet
  --> 3.280839895013124
6.2 * m_to_feet
  --> 20.34120734908137

 

     

I cannot explain where your value of 0.217 is coming from.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes