Ray intersection, intersection point

Ray intersection, intersection point

Anonymous
Not applicable
2,453 Views
3 Replies
Message 1 of 4

Ray intersection, intersection point

Anonymous
Not applicable

 

I'm trying to get the floor to floor/roof height (depending on whether there's another floor above the room or a roof) of rooms in Revit, I've tried to implement ray intersections following the reference intersector class documentation: https://www.revitapidocs.com/2015/d28155ae-817b-1f31-9c3f-c9c6a28acc0d.htm

 

However whilst debugging with breakpoints, I've found the reference.global point returned on all my intersections has a z coordinate of 0.  Is there something I'm doing wrong here, is there a way to get this z coordinate?

 

One workaround I've thought of so far would to be to find the element and use its bounding box. But in the case of a sloping roof this would not give me the true value.

 

public double GetFloorToRoofHeight()
        {
            // Find a 3D view to use for the ReferenceIntersector constructor
            FilteredElementCollector collector = new FilteredElementCollector(room.Document);
            Func<View3D, bool> isNotTemplate = v3 => !(v3.IsTemplate);
            View3D view3D = collector.OfClass(typeof(View3D)).Cast<View3D>().First<View3D>(isNotTemplate);


            // Use the center of the room bounding box as the start point.                   
            XYZ center = boundingBox.Min.Add(boundingBox.Max).Multiply(0.5);


            XYZ up = new XYZ(0, 0, 1);
            XYZ down = new XYZ(0, 0, -1);


            ElementClassFilter filter = new ElementClassFilter(typeof(Floor));

            ReferenceIntersector refIntersector = new ReferenceIntersector(filter, FindReferenceTarget.Face, view3D);
            ReferenceWithContext referenceWithContext = refIntersector.FindNearest(center, down);

            Reference reference = referenceWithContext.GetReference();
            XYZ intersectionFloor = reference.GlobalPoint;


            filter = new ElementClassFilter(typeof(Floor));

            refIntersector = new ReferenceIntersector(filter, FindReferenceTarget.Face, view3D);
            referenceWithContext = refIntersector.FindNearest(center, up);
            
            


            //if we found floor above we use it for height otherwise we need to search for a roof
            if (referenceWithContext != null)
            {
                Reference reference2 = referenceWithContext.GetReference();
                XYZ intersectionFloorUp = reference.GlobalPoint;
                Debug.WriteLine(Line.CreateBound(center, intersectionFloorUp).Length);
                return intersectionFloorUp.Z - intersectionFloor.Z; 
            }

            filter = new ElementClassFilter(typeof(RoofBase));

            refIntersector = new ReferenceIntersector(filter, FindReferenceTarget.Face, view3D);
            referenceWithContext = refIntersector.FindNearest(center, up);

            if (referenceWithContext == null)
            {
                return 0;
            }

            Reference reference3 = referenceWithContext.GetReference();
            XYZ intersectionRoof = reference.GlobalPoint;
            Debug.WriteLine(Line.CreateBound(center, intersectionRoof).Length);
            return intersectionRoof.Z - intersectionFloor.Z;
        }

 

0 Likes
Accepted solutions (1)
2,454 Views
3 Replies
Replies (3)
Message 2 of 4

RPTHOMAS108
Mentor
Mentor

What global point the up direction raycast or the down direction raycast?

 

What Z value were you expecting? For example I don't know anything in relation to the levels of things within your test model. It also gives you a distance.

0 Likes
Message 3 of 4

Anonymous
Not applicable

Capture.PNG

@RPTHOMAS108  here's a section of the first room it looks at,  at level 0 there is a floor and level 1.  I'm expecting a height of around 3.5 metres 

The centre is the following coordinate:

center = {(-1147.609231629, 2179.627095675, 4.000000000)}

 

intersectionRoof = {(-1147.609231629, 2179.627095675, 0.000000000)}

intersectionFloor = {(-1147.609231629, 2179.627095675, 0.000000000)}

 

Edit:

Sorry, I gave the coordinates for a different room

 

center = {(-1141.137652285, 2197.339355559, 5.823490814)}

intersectionFloorUp = {(-1141.137652285, 2197.339355559, 0.000000000)}

intersectionFloor = {(-1141.137652285, 2197.339355559, 0.000000000)}

0 Likes
Message 4 of 4

RPTHOMAS108
Mentor
Mentor
Accepted solution

Well your lower floor is at 0 isn't it, so going down from mid point of room looks right.

 

In terms of going back up in the other direction the ray will often hit the element it starts closest to so you have to start it with a slightly higher Z than the surface of the floor or exclude the floor.

 

Is centre point of room as you expect i.e. is room enclosed at right levels?

 

If you do Find instead of FindNearest you can look up from just above floor finding floors, ceilings and roofs. In one ray i.e. you don't have to stop at nearest. From the distances returned you can work out their relations.

 

Note also (unrelated) that when you get a 3D view you have to ensure the visibility is as you expect for the intersector to work as expected. Often the discipline may be wrong hiding certain elements or categories are hidden.

0 Likes