Set Elevation of NewFamilyInstance Hosted Family

Set Elevation of NewFamilyInstance Hosted Family

evan.dinelli
Participant Participant
1,004 Views
3 Replies
Message 1 of 4

Set Elevation of NewFamilyInstance Hosted Family

evan.dinelli
Participant
Participant

I've read other forum posts that discuss the troubles with NewFamilyInstance(Reference, XYZ, XYZ, FamilySymbol)

 

I've been able to place a family instance, but am unable to set the element at the proper elevation. I'm able to set an already placed element's elevation parameter like this:

 

ElementId tmpID = new ElementId(5901554);
Element tmpE = document.GetElement(tmpID);
Parameter paramtmp_height = tmpE.get_Parameter(BuiltInParameter.INSTANCE_ELEVATION_PARAM);
double tmpD = 4.9321;
paramtmp_height.Set(tmpD);

 

When I place the hosted element and try to set the elevation parameter, the code breaks. Any ideas on a workaround?

// NewFamilyInstance params
XYZ p1 = null;
Util.GetFamilyInstanceLocation(out p1, familyInstance.Key);
XYZ p2 = new XYZ(p1.X, p1.Y, p1.Z);
Reference r = familyInstance.Key.HostFace;
ElementId linkDocElementID = new ElementId(1152842);
Element linkedDocElement = document.GetElement(linkDocElementID);
RevitLinkInstance rvtlink = linkedDocElement as RevitLinkInstance;
r = r.CreateLinkReference(rvtlink);

// create instance
var instance = creation_doc.NewFamilyInstance(r, p2, new XYZ(0, 0, 0), pSelection.FamilySymbolObject);

// get main model parameters to update
ElementId placed_id = instance.Id;
Element placed = document.GetElement(placed_id);

// modify placed instance elevation
Parameter paramMain_height = placed.get_Parameter(BuiltInParameter.INSTANCE_ELEVATION_PARAM);
double tmp = 4.3;
paramMain_height.Set(tmp); // CODE BREAKS HERE :-(
0 Likes
1,005 Views
3 Replies
Replies (3)
Message 2 of 4

jeremytammik
Autodesk
Autodesk

What exactly do you mean by saying 'the code breaks'?

 

I am trying to imagine the concrete situation... 

 

By the way, you are doing a couple of slightly weird things in your code.

 

For example, p1 and p2 are identical, so one asks oneself, why introduce the second?

 

Similarly, `instance` and `placed` are identical, so why introduce the second?

 

Back to your main problem:

 

Maybe you are lacking an intermediate regeneration of the model?

 

Cf.,

 

https://thebuildingcoder.typepad.com/blog/about-the-author.html#5.33

 

 

 



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

0 Likes
Message 3 of 4

evan.dinelli
Participant
Participant

Thanks Jeremy!

 

I've updated the code and I am still not able to modify the elevation of the placed hosted family with the API. The element is successfully placed after the code runs, ending the transaction. Then in the Revit UI, I am also not able to modify the elevation of the placed element. The elevation parameter ReadOnly=True.

 

In the Revit UI, after I click on the element and select "Pick New" to choose to reassign the surface host, I am able to modify the elevation and the elevation parameter ReadOnly=False. What would be causing the ReadOnly parameter to be True after placement and how can I work around that in the API?

 

Capture.JPG

// NewFamilyInstance params
XYZ p1 = null;
Util.GetFamilyInstanceLocation(out p1, familyInstance.Key);
Reference r = familyInstance.Key.HostFace;
ElementId linkDocElementID = new ElementId(1152842);
Element linkedDocElement = document.GetElement(linkDocElementID);
RevitLinkInstance rvtlink = linkedDocElement as RevitLinkInstance;
r = r.CreateLinkReference(rvtlink);

// create instance
var instance = creation_doc.NewFamilyInstance(r, p1, new XYZ(0, 0, 0), pSelection.FamilySymbolObject);

instance.Document.Regenerate(); // added

// modify placed instance elevation
Parameter paramMain_height = instance.get_Parameter(BuiltInParameter.INSTANCE_ELEVATION_PARAM);
double tmp = 4.3;
MessageBox.Show("not set");
paramMain_height.Set(tmp);
MessageBox.Show("set");

 

Thanks,

Evan

0 Likes
Message 4 of 4

jeremytammik
Autodesk
Autodesk

Probably your manual placement is generating a different placement type than the programmatic one.

 

The NewFamilyInstance method comes in many different flavours known as overloads, taking different arguments:

 

https://www.revitapidocs.com/2020/0c0d640b-7810-55e4-3c5e-cd295dede87b.htm

 

It can be tricky to know which one to use:

 

https://thebuildingcoder.typepad.com/blog/2011/01/newfamilyinstance-overloads.html

 

You need to find the right overload to generate the placement type that you need, the placement type generated by your manual insertion.

 

Systematic trial and error of the various overloads might help move forward with this.

 

Good luck!

 

Cheers,

 

Jeremy

 



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

0 Likes