Using elements selected before a command is started

Using elements selected before a command is started

stever66
Advisor Advisor
2,578 Views
5 Replies
Message 1 of 6

Using elements selected before a command is started

stever66
Advisor
Advisor

How can I turn a list of ElementId's into a list of References to selected elements??

 

I want my command to work so that if elements are selected when the command is started, it uses the selected elements.  If no elements are selected,  the program should prompt the user to select the elements.  I can't quite get this to work.  The only way I can find to get the pre-selected elements returns a list of ElementId's:

 

         ICollection<ElementId> selectedIds = uidoc.Selection.GetElementIds();

 

But I need element references to run the rest of my code, like I get with the PickObjects method:

 

        IList<Reference> selected = new List<Reference>();

        selected = uidoc.Selection.PickObjects(ObjectType.Element, "Select keynotes");

 

Here is the portion of code I have so far, that works for the post-selected elements, but not preselected elements;

 

ICollection<ElementId> selectedIds = uidoc.Selection.GetElementIds();
IList<Reference> selected = new List<Reference>();


 if (0 == selectedIds.Count)
            {
               //If no elements selected, prompt the user to select some
                selected = uidoc.Selection.PickObjects(ObjectType.Element, "Select keynotes");

             }

//get the parameter "MyParameter" from the first element in "selected".

//Is there a way to do this using ElementId's?

Element pickedID = doc.GetElement(selected[0]);
Parameter myparam = pickedID.LookupParameter("MyParameter");

 

 

0 Likes
Accepted solutions (1)
2,579 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable

Something like this ?

 

ElementID pickedID = new ElementID();

 if (0 == selectedIds.Count)
            {
               //If no elements selected, prompt the user to select some
                selected = uidoc.Selection.PickObjects(ObjectType.Element, "Select keynotes");

                pickedID = selected[0];  

           }

else

{

               pickedID = selectedIds[0];  

}

Element pickedelem = doc.GetElement(pickedID);
Parameter myparam = pickedelem.LookupParameter("MyParameter");

 

Message 3 of 6

stever66
Advisor
Advisor

I'm still not having any luck.  The first error I keep getting is:

 

Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.ICollection<Autodesk.Revit.DB.ElementId>'

 

 

I think this is because its a Collection of objects, and I'm trying to treat it as a list.  How do I access individual objects in a collection?

 

The second error is:

 

cannot convert from 'Autodesk.Revit.DB.Element' to 'Autodesk.Revit.DB.ElementId'

 

So I just need to find the ElementId given an element.  Sounds easy enough, but I can't really try doing this until I figure out the first error.

 

0 Likes
Message 4 of 6

stever66
Advisor
Advisor

I think I've figured out how to iterate through the collection to get each ElementId. 


But I'm still having problems getting this error message:

 

Argument 1: cannot convert from 'Autodesk.Revit.DB.ElementId' to 'Autodesk.Revit.DB.Reference'.

 

Is there a way to get the element reference given the ElementId?   Or can I do some type of cast?

 

This is my best guess, and it doesn't work:

 

     foreach (ElementId id in selectedIds)
                {
                    selected.Add(id as Reference);
                }

It gives this error:

 

Cannot convert type 'Autodesk.Revit.DB.ElementId' to 'Autodesk.Revit.DB.Reference' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

 

Steve

0 Likes
Message 5 of 6

Dale.Bartlett
Collaborator
Collaborator
Accepted solution

Any good? Dale

 

foreach (ElementId id in selectedIds)

{

  Element e = doc.GetElement(id);

}

 




______________
Yes, I'm Satoshi.
Message 6 of 6

stever66
Advisor
Advisor

 

Yes, that works!!  Thank you. 

 

But why does it work?  doc.GetElement(Id) is supposed to take a Reference type, not an ElementID type.  (At least that's what it says when I hover my mouse over the GetElement command).

 

Also, my head is spinning.  There seems to be 3 ways to get a handle to a element: 

  1.  The element itself.

  2.  A reference to the element.

  3.  The ElementId.

 

Just curious - now I know how to get the element given either 2 or 3, is there a way to get 2 or 3 given the element itself?  Or a shortcut between the reference and ElementId?

 

It would be nice if Autodesk would summerize the ways to access an element somewhere for us beginners.

 

Things get even more complicated when I start looking at Element Types and Families, and type parameters vs. instance parameters.

 

Steve

0 Likes