Calculate distance from object to level 0

Calculate distance from object to level 0

Anonymous
Not applicable
4,452 Views
9 Replies
Message 1 of 10

Calculate distance from object to level 0

Anonymous
Not applicable

Hello!
I want to find distance from every object of given category to level 0 in document. And then I'm setting this value like "Object Height" parameter.

I'm trying to get it in this way:

using (Transaction tx = new Transaction(doc))
{
   tx.Start("Set Parameter");
   foreach (Element elem in allElements)
   {
       double z = 0.0;
       Parameter p = elem.LookupParameter("Object Height");
       LocationPoint loc = elem.Location as LocationPoint;
       LocationCurve lc = elem.Location as LocationCurve;
       if (loc != null)
       {
           z = loc.Point.Z;
       }
       else
       {
           z = lc.Curve.GetEndPoint(0).Z;
       }
       z = (Math.Round(z / 10) )* 10;
       p.Set(z);  
    }
    tx.Commit();
}

but I get strange values after rounding. I feel like I'm doing something wrong. Maybe there's another way to get current object height from level 0?

ex.png

0 Likes
4,453 Views
9 Replies
Replies (9)
Message 2 of 10

RPTHOMAS108
Mentor
Mentor

By object height you mean underside level not top?

 

I would avoid LocationCurve/LocationPoint objects for this task considering the solid my be parametrically offset from such. Most level based objects also have 0 for Z in their location point.

 

Element.get_BoundingBox(Null) is probably a better option for this type of thing. Comparing max/min Z to a level elevation is then possible.

 

0 Likes
Message 3 of 10

sragan
Collaborator
Collaborator

When I look at:

z = (Math.Round(z / 10) )* 10;

You are making Z smaller, and then rounding to an integer, and scaling it back up.

 

That will make everything 0, 10, 20, 30.....


Is that what you want?  Or do you want to do the opposite?  Scale it up, then round off, and scale back down?

 

z = (Math.Round(z *10) )/ 10;

 
Then I think you will get things rounded to 0.1.   Like 9.8, 9.9, 10, 10.1.

 

Or do you want everything rounded to 10'?

0 Likes
Message 4 of 10

Anonymous
Not applicable

Yes, I have tried BoundingBox like that:

foreach (Element elem in allElements)
{
    double z = 0.0;
    Parameter p = elem.LookupParameter("Object Height");
    BoundingBoxXYZ bb = elem.get_BoundingBox(doc.ActiveView);
    z = Math.Round(bb.Min.Z, 0);
    p.Set(z);  
}

It returns the same values.

0 Likes
Message 5 of 10

Anonymous
Not applicable

I need make rounding like that: for example from 18321.4 to 18320.0.
In my case, this code 

z = bb.Min.Z;

 returns 18321.4, but 

z = Math.Round(bb.Min.Z, 0);

returns 18288.0. It should be 18321. Why is it so?

0 Likes
Message 6 of 10

Anonymous
Not applicable

I need make rounding like that: for example from 18321.4 to 18320.0.
In my case, this code 

 

z = bb.Min.Z;

 

 returns 18321.4, but 

 

z = Math.Round(bb.Min.Z, 0);

 

returns 18288.0. It should be 18321. Why is it so?

0 Likes
Message 7 of 10

sragan
Collaborator
Collaborator

It probably has to do with Revit's internal units, or the units revit is using for  Z.

 

Maybe try setting a double equal to the minimum, and then rounding:

 

z = bb.Min.Z;

z2 = Math.Round(z,0);

 

See if that makes a difference.

 

0 Likes
Message 8 of 10

RPTHOMAS108
Mentor
Mentor

I don't get that value using those numbers with that function (I get what you expected).

 

For bounding box I suggest you don't supply the active view as when you do the bounding box is calculated based on graphics within that view (this may have unexpected consequences).

 

I didn't suggest the use of BoundingBoxXYZ as being more correct than Location they both have their purpose. For measuring extents of solids however BoundingBoxXYZ is more appropriate. When extent of solid you want and location curve coincide it is correct, otherwise it is wrong.

0 Likes
Message 9 of 10

Anonymous
Not applicable

Unfortunately, it makes no difference. I get the same value after rounding 😞

0 Likes
Message 10 of 10

Anonymous
Not applicable

Thank you for tips, I'm doing like this:

foreach (Element elem in allElements)
{
    double z = 0.0;
    Parameter p = elem.LookupParameter("Object Height");
    BoundingBoxXYZ bb = elem.get_BoundingBox(null);
    z = bb.Min.Z;
    double z2 = Math.Round(z, 0);
    p.Set(z2);  
}

 but the same things anyway. 

0 Likes