How to get XYZ direction of an Element in Revit ?

How to get XYZ direction of an Element in Revit ?

Anonymous
Not applicable
3,100 Views
7 Replies
Message 1 of 8

How to get XYZ direction of an Element in Revit ?

Anonymous
Not applicable

Hello ,

 

Can someone tell me how can i get a XYZ direction of an Element already created ?

 

this is my Code

  foreach (Element element in GetElement(doc))
                {
                    LocationPoint positionPoint = item.Location as LocationPoint;
                   
                    ReferencePlane refDir =element as ReferencePlane;

}
0 Likes
3,101 Views
7 Replies
Replies (7)
Message 2 of 8

jeremytammik
Autodesk
Autodesk

If the element has a location point, you can use the location point Rotation property:

 

http://www.revitapidocs.com/2017/43aea45e-dd1c-6a35-2fd5-1f99356708df.htm



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

0 Likes
Message 3 of 8

rsharpey
Explorer
Explorer

@jeremytammik So this gets the rotation value, is there an default direction that elements have when initially placed? Because that will be needed in order to find the XYZ direction from the angle

0 Likes
Message 4 of 8

jeremy_tammik
Alumni
Alumni

I believe this depends on the specific element and its placement type. For instance, some family instances are hosted on a plane, so their orientation is relative to the insertion direction on the plane, which in turn may depend on the plane U and V directions or other characteristics. The best (and probably the only reliable) way to find out is to explore your model and your specific elements using RevitLookup and see for yourself.

  

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

tuah_edu
Explorer
Explorer

Perhaps, inspect its Direction's Z value property? For some reason some of the Z values collected into the Python node will appear as 0.99999999 but they are actually 1s in Revit Lookup. 

 

 

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

# The inputs to this node will be stored as a list in the IN variables.
elements = UnwrapElement(IN[0])

validElements = []
absDirections = []
tolerance = 1e-6

# Place your code below this line

for element in elements:
    direction = element.Location.Curve.Direction.Z

    # Check if the absolute difference between direction and 1 is less than the tolerance
    if abs(1 - abs(direction)) > tolerance:
        validElements.append(element)
        absDirections.append(abs(direction))

# Assign your output to the OUT variable.
OUT = absDirections, validElements

 

 

0 Likes
Message 6 of 8

jeremy_tammik
Alumni
Alumni

0.99999999 but they are actually 1

  

Nope. Neither is correct. You are working with floating-point numbers on a digital computer, and the number cannot be exactly represented, so you need to add fuzz before you even try to think of the number:

  

https://www.google.com/search?q=fuzz&as_sitesearch=thebuildingcoder.typepad.com

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 7 of 8

tuah_edu
Explorer
Explorer
Thanks for articles, Jeremy!
0 Likes
Message 8 of 8

ctm_mka
Collaborator
Collaborator

Mathematicians feel free to prove me wrong, but a point (XYZ class in Revit's case) doesn't/can't have a direction. Certainly not within the Revit API. A Second point would be needed to define a curve/line, which would have a direction, which can be retrieved similar to this:

 

 Curve elemcurve = (element.Location as LocationCurve).Curve;
 Line elemline = elemcurve as Line;
 XYZ direction = elemline.Direction;

now then, if you really wanted to use that location point and the rotation, you would need to calculate a new point a certain distance away at that angle, which should look something like this, lets say the distance is 1 and the angle is 30 degrees:

double newx = origin.X + 1 * Math.Cos(30);
double newy = origin.Y + 1 * Math.Sin(30);
XYZ newpt = new XYZ(newx, newy, origin.Z);
Line testline = Line.CreateBound(origin, newpt);
 XYZ direction = testline.Direction;

my code for the new point is probably off, but it gets the idea across.

 

0 Likes