GetElement() returning null on selected ElementId within Family Document

GetElement() returning null on selected ElementId within Family Document

Anonymous
Not applicable
1,879 Views
2 Replies
Message 1 of 3

GetElement() returning null on selected ElementId within Family Document

Anonymous
Not applicable

Hello,

 

I'm having an issue, where I select an element, retrieve its ID, however when I try doc.GetElement() it returns null. I'm wondering if it is because I am running this in a family document (something I am not used to)

 

Document doc = this.Document;
if( !doc.IsFamilyDocument )
{
     TaskDialog.Show("Fam Doc", "Must be run in a family document");
}
			
FamilyManager mgr = doc.FamilyManager;
			
Selection selection = this.Application.ActiveUIDocument.Selection;
ICollection<ElementId> selectedIds = selection.GetElementIds();
ElementId id = selectedIds.First();
            
// is null
Element test = doc.GetElement(id);
// fails because test is null
ParameterSet pSet = test.Parameters;

Attached is the family file I am using, I selected element 3008 (the upper middle extrusion on the Ref. Level floor plan), however any element selection should yield the same failure. 

 

Thanks!

 

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

jeremytammik
Autodesk
Autodesk

In the code snippet you show above, there is no guarantee that 

 

  Document doc = this.Document;

is the same as

 

  this.Application.ActiveUIDocument

I suggest that you eliminate the latter and use one single `doc` variable to address both.

 

You are using the former document and an element id from the latter.

 

This will certainly fail if the two differ.

 

Cheers,

 

Jeremy



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

Message 3 of 3

Anonymous
Not applicable
Accepted solution

Jeremy's answer is correct I believe, just wanted to add what fixed it for me was in macro manager creating for "application" rather than the specific project. Good luck!

0 Likes