Search Element in multiple open document

Search Element in multiple open document

markjvlampad
Advocate Advocate
602 Views
5 Replies
Message 1 of 6

Search Element in multiple open document

markjvlampad
Advocate
Advocate

I have opened multiple documents and have my element id.

 

What I want to do is to search the ids in the documents. So the api will look which document contains the Id and select.

 

Currently the below code is working only for single opened document but it return me an error when the selected id does not exist.

 

Selection selection = uidoc.Selection;
ICollection<ElementId> selectedIds = uidoc.Selection.GetElementIds();
Element el = null;
foreach (ElementId id in selectedIds)
{
el = doc.GetElement(id);
uidoc.ShowElements(e);
}

0 Likes
603 Views
5 Replies
Replies (5)
Message 2 of 6

MarryTookMyCoffe
Collaborator
Collaborator

I see a logic error here.
why do you thing that there will be the same element with equal id ?
items get first free id when created.
if you looking for specific element or family
first get all documents (you should have Document instance for every open document in revit)
use for every document

FilteredElementCollector

that set what class and category you are looking for
searching for equalc id is not good idea.

-------------------------------------------------------------
--------------------------------|\/\/|------------------------
do not worry it only gonna take Autodesk 5 years to fix bug
0 Likes
Message 3 of 6

mhannonQ65N2
Collaborator
Collaborator

I think you want to use the UIDocument.Document property.

Document doc = uidoc.Document;
Selection selection = uidoc.Selection;
ICollection<ElementId> selectedIds = uidoc.Selection.GetElementIds();
Element el = null;
foreach (ElementId id in selectedIds)
{
    el = doc.GetElement(id);
    uidoc.ShowElements(e);
}

It will give you the document that the selection belongs to. The UIDocument corresponds to a single database document, its Selection cannot contain elements that are in other documents.

0 Likes
Message 4 of 6

stefanome
Collaborator
Collaborator

@markjvlampad wrote:

Currently the below code is working only for single opened document but it return me an error when the selected id does not exist.

Three considerations:

  • I don't understand your statement, because your code only deals with one document, it doesn't even know if you have multiple open documents
  • It cannot "return me an error when the selected id does not exist", because (as far as I know) uidoc.Selection.GetElementIds() only returns existing ids
  • You can't cycle through all your documents looking for the one where an id is valid, because that id could be valid in more than one document

Can you give us more details?

0 Likes
Message 5 of 6

markjvlampad
Advocate
Advocate

Hi Stefanomenci,

 

Thank you for your response. First of all I would like to clarify what is happening.

 

1. I have 2 models, which is old and new. And some elements which are deleted from the old model and a new elements save in the latest file.

 

2. I manage to save the element Ids for all the deleted and new elements in excel files. When I copy and paste the id to my code it will only check the id within that 1 document. which mean what if the element is deleted so basically it will return element = null.

 

what I want is that when I paste the id, the api will automatically search the element for both file and shows the selected id where the element is present.

 

 

0 Likes
Message 6 of 6

MarryTookMyCoffe
Collaborator
Collaborator

Hi,
You make assumption that revit don't reuse ids, I'm not sure if this is true.

But  you can try using this:

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            var uidoc = commandData.Application.ActiveUIDocument;


            int yourId = 83; //<--get some id and insert it here
            ElementId elementId = new ElementId(yourId);
            
            //all open documents in revit
            var documents = commandData.Application.Application.Documents;
            //filter for elementId value
            ParameterValueProvider pid= new ParameterValueProvider(new ElementId(BuiltInParameter.ID_PARAM));
            FilterNumericEquals numericEquals = new FilterNumericEquals();
            FilterElementIdRule rule = new FilterElementIdRule(pid, numericEquals, elementId);
            ElementParameterFilter filter = new ElementParameterFilter(rule);

            foreach (Document document in documents)
            {
                FilteredElementCollector collector = new FilteredElementCollector(document)
.WherePasses(filter);

                //if (new FilteredElementCollector(document).Any(e => e.Id == elementId)) //<-- or you can use this if using filter is overwhelming 
                if (collector.Count() > 0)
                {
                    var element = document.GetElement(elementId);
                    uidoc.ShowElements(element);
                }
            }
            return Result.Succeeded;
        }

 i didn't test it so i could make a mistake somewhere in code

-------------------------------------------------------------
--------------------------------|\/\/|------------------------
do not worry it only gonna take Autodesk 5 years to fix bug
0 Likes