Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Set Elevation of NewFamilyInstance Hosted Family

3 REPLIES 3
Reply
Message 1 of 4
evan.dinelli
755 Views, 3 Replies

Set Elevation of NewFamilyInstance Hosted Family

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 :-(
3 REPLIES 3
Message 2 of 4
jeremytammik
in reply to: evan.dinelli

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

Message 3 of 4
evan.dinelli
in reply to: jeremytammik

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

Message 4 of 4
jeremytammik
in reply to: evan.dinelli

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


Rail Community