Document.Create.NewFamilyInstance places element with wrong Z-coordinate

Document.Create.NewFamilyInstance places element with wrong Z-coordinate

ottosson_mathias
Advocate Advocate
1,248 Views
3 Replies
Message 1 of 4

Document.Create.NewFamilyInstance places element with wrong Z-coordinate

ottosson_mathias
Advocate
Advocate

I'm trying to place a new element next to another element.


To do this I have the target element location, which I use to create the new element in the exact same position, as a start... But here is where thing go wrong!

 

 

var targetLocation = targetElement.Location as LocationPoint;

var newElement = doc.Create.NewFamilyInstance(targetLocation.Point, familySymbol, targetElement.LevelId, StructuralType.NonStructural);

 


When I debug and set a break point after the element has been created newElement has position 0, 0, 0! This gets updated after the transaction has been committed, which is fine I guess...

 

After the commit the element X and Y-coordinates are correct, but not Z which now is a much larger (I've made sure the targetElement.LevelId is correct for the newElement).

 

Z-issue.png

 

Why has the element got correct coordinates in X and Y but not in Z?
Bonus question: Why is the new element position 0, 0, 0 after creation before transaction has been committed?

 

I'm currently testing my code with Revit 2018.3.3.


Thanks!

Accepted solutions (1)
1,249 Views
3 Replies
Replies (3)
Message 2 of 4

David_Robison
Advocate
Advocate

What's the elevation of the level you are inserting on?

 

I did a quick review of my code where I use NewFamilyInstance. I pass the Z-coordinate as 0 and my instance ends up at the elevation of my level. Try setting Z to 0 and see what happens.

0 Likes
Message 3 of 4

ottosson_mathias
Advocate
Advocate

No luck... then the Z-elevation is 0 from the current level... as you'd expect!

 

Maybe it has something to do with units being handled differently for Z-offset? Which feels extremely unintuitive...

0 Likes
Message 4 of 4

ottosson_mathias
Advocate
Advocate
Accepted solution

Ok, so I think I've figured out the problem...

 

When I get the point for the target element I get the absolute Z-offset not the floor offset! So I first have to get the parameter value and create a new XYZ like so:

 

 

var floorOffset = targetElement.get_Parameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM).AsDouble()
var offset = new XYZ(targetObjectPoint.X, targetObjectPoint.Y, floorOffset);

 

 

A little bit weird that you get the absolute offset value, not the floor offset value, but at the same time it makes sense. Maybe there should be a more intuitive way to get the floor offset.