Selecting in Linked Documents

Selecting in Linked Documents

cwaluga
Advocate Advocate
7,165 Views
23 Replies
Message 1 of 24

Selecting in Linked Documents

cwaluga
Advocate
Advocate

Hi everybody,

 

As it is possible to select in linked documents (Tab-Tab-Tab...) via the GUI, and since Revit can react to such selection changes, I wonder why this is so non-obvious to do via the API. I have two related questions which I try to keep as short as possible:

 

1) is it possible to select/show elements in linked documents?

 

I tried out two ideas which unfortunately did not seem to work:

 

The first one is to instantiate a UIDocument for the linked doc, which seems to be illegal in the API:

 

var uiDoc = new UIDocument(linkedDoc); // <- not allowed
uiDoc.ShowElements(elementId);
 
The second one is to hope that uiDoc.ShowElements is able to handle linked elements:
 
var uiDoc = uiApp.ActiveUIDocument;
// var element = linkedDoc.GetElement(elementId); // we may need to get the element first
uiDoc.ShowElements(element); <- use element here instead of ID

 

Here, there is no exception thrown, but Revit comes back with the message that it could not find a suitable view.

 

 

2) can one obtain the set of selected elements in linked documents?

 

UIDocument.Selection does not seem to work here, since it is ElementId-based. Moreover, as stated above, the instantiation of linked UIDocuments is prohibited.

 

 

Would be great if anyone had some advice! Note: I'm aware that picking elements in linked documents works since 2014 or so. I wrote this in bold because I am not interested in picking and don't want anybody to spend his/her precious time on a picking-related answer. Picking is a totally different story and does not fit the intended workflow.

 

In case there is no hope in the 2017 edition of the API, would it be possible to request these two crucial features for enhancing user interaction? 

 

Thanks for reading and any comment that may follow! 🙂

 

Have a good day/weekend!

Christian

7,166 Views
23 Replies
Replies (23)
Message 2 of 24

Anonymous
Not applicable

Hi ! 

To Select linked elements I use this :

 

IList<Element> Allelems = new List<Element>();
IList<Element> elems = collector .OfCategory(BuiltInCategory.OST_RvtLinks) .OfClass(typeof(RevitLinkType)).ToElements(); foreach (Element e in elems) { if (e.Name == arc) { Allelems.Add(e); } }
 foreach (Element e in Allelems)
                {
                    RevitLinkType linkType = e as RevitLinkType;
    foreach (Document linkedDoc in CachedUiApp.Application.Documents)
                    {
                        if (linkedDoc.Title.Equals(linkType.Name))
                        {
                            FilteredElementCollector collLinked = new FilteredElementCollector(linkedDoc);
                            IList<Element> linkedWalls = collLinked.OfClass(typeof(Wall)).WherePasses(filter).ToElements();
                            if (linkedWalls.Count != 0)
                            {
                                foreach (Element eleWall in linkedWalls)
                                {
                                    walls.Add(eleWall);
                                }
                            }
                        }
                    }
                }

Here you can Select every walls in linked projects

Hope this help !

0 Likes
Message 3 of 24

cwaluga
Advocate
Advocate
Hi, thanks for answering. I honestly do not see where the selection happens. I mean literally 'select' in the sense of the object being highlighted (and possibly shown) in the editor window. As far as I can tell your code steps through all linked documents and collects the walls it can find there. This is not the question here.
0 Likes
Message 4 of 24

louisC6AEK
Participant
Participant

Did you ever get this working?
I now have exactly the same problem!

0 Likes
Message 5 of 24

cwaluga
Advocate
Advocate

To my knowledge there still seems to be no way to accomplish this using the API. One could draw auxiliary objects for highlighting, but this is neither trivial nor useful for our purpose. Picking linked elements seems to work though which leads me to the conclusion that it is in principle no big deal to implement this. I guess the problem is just that this feature on the very bottom of the todo list. There was a wish list item for this if I'm not mistaken. I can only vote once, but you can try to find it and add some weight to this issue :-).

Message 6 of 24

yekose
Participant
Participant

I am using that code to select the linked element. While picking the element you shall use the tab to select linked element over the model so that you can get the linked element id.

 

Selection choices = uidoc.Selection;

Reference hasPickOne = choices.PickObject(ObjectType.LinkedElement);
if (hasPickOne != null)
{

TaskDialog.Show("Revit", string.Format("{0}", hasPickOne.LinkedElementId.IntegerValue));

}

0 Likes
Message 7 of 24

louisC6AEK
Participant
Participant
I have the ElementId.
What I want to do is to select the element programmatically and to bring it into view.
Revit also can't do this with the Find by Id tool. It shows a message that the ElementId is invalid and can't be used.
0 Likes
Message 8 of 24

yekose
Participant
Participant

 Hi @louisC6AEK

 

Just to make sure, are you aware that the element id and the linked element id s are different if you are using the linked documents? 

0 Likes
Message 9 of 24

louisC6AEK
Participant
Participant
No I didn't know.
How do I get the linked element id?
0 Likes
Message 10 of 24

louisC6AEK
Participant
Participant
Never mind...
0 Likes
Message 11 of 24

yekose
Participant
Participant

I am trying to help you, you do not need to be a smartass. 

 

Anyway,

 

here is i think what you are looking for;

 

The below code works only for 1 element, i assume you know how to find linked element ids, but the thing is you need to use Element ID on the linked file, not the linked element id, anyway if you test you can see the difference. 

 

When you add your selected ids to the selection list you will see that your elements will be highlighted.

 

int idInt = Convert.ToInt32("355014");
ElementId id = new ElementId(idInt);
Element element = doc.GetElement(id);

List<ElementId> icollection = new List<ElementId>();

icollection.Add(element.Id);

uidoc.Selection.SetElementIds(icollection);

 

I hope that helps.

 

 

0 Likes
Message 12 of 24

louisC6AEK
Participant
Participant

I meant: "Never mind - I know how to get the ElementId".

Anyway...

 

I have the ElementId of a door in the Linked model. (Actually, I have the UniqueId of it.)

 

I can get the Door Element in its linked file by using that ElementId.

 

In the active document the door Element is null. (selelem = doc.GetElement(dor_UniqueIDVal);)

 

In the Linked doc Door Element (selelem) is not null.

 

I now want to select that door in the current model.

 

  uidoc.ShowElements(selelem); //ERROR! - When I use Element: "No good view could be found"

 

  uidoc.ShowElements(selelemid); //ERROR! - When I use ElementId: "Element id is not valid for this document
                                                                                                          Parameter name: elementId"

If I search Revit for that ElementId - ERROR! "The following Element Ids are invalid and won't be used"

 

So I guess the question is:

What is the Element Id for my door in the current uidoc?

I can't select it and I don't know where it is. (The goal of my function is to open any view or sheet with that door on it, zoom to the door and select it.) 

 


  uidoc.Selection.SetElementIds(elementIds); 

0 Likes
Message 13 of 24

yekose
Participant
Participant

Actually,

 

you should use the id of the element in the host file, because linked element id and unique IDs are the real element ids but you want to select them on the host file.

 

The following code select all the elements in the linked files and add them to the selection list, this include the linked file itself, if you want you can exclude it from the filter. 

 

And you can get some ids from the below code and if you try to search in the revit you ll see that you can find those elements.

 

If you do this filter over the linked file you will get the Real id of the elements and if you try to search them you wont be finding them.

 

FilteredElementCollector collector = new FilteredElementCollector(doc);

IList<Element> elems = collector
.OfCategory(BuiltInCategory.OST_RvtLinks)
.ToElements();

List<ElementId> icollection = new List<ElementId>();
foreach (Element e in elems)
{
icollection.Add(e.Id);
}
sel.SetElementIds(icollection);

Message 14 of 24

Anonymous
Not applicable

I've tried to reproduce the answer provided by yekose but this only allows to select the element corresponding to the linked document, not the specific elements inside this linked document.

 

Is there any option to highlight only a specific element inside the linked document?

0 Likes
Message 15 of 24

louisC6AEK
Participant
Participant
You actually can't do that in Revit. Revit will only allow you to select elements in the current document. If you can't do it in Revit, you won't be able to do it via an addin.

0 Likes
Message 16 of 24

cwaluga
Advocate
Advocate

Well, you can actually do that in Revit. I do it all the time by enabling links in the selection filters and using Tab. The problem is just that there is still no API to control the selection set in linked document scenarios (which are a standard practice in non-toy projects), e.g., by using unique IDs.

0 Likes
Message 17 of 24

Anonymous
Not applicable

Hi,

 

Here You go:

  UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Application app = uiapp.Application;
            Document doc = uidoc.Document;
			ObjectType obt=ObjectType.LinkedElement;
			Reference refElemLinked;
			Element linkedelement = null;
			
			refElemLinked = uidoc.Selection.PickObject(obt, "Please pick an element in the linked model");
                        RevitLinkInstance elem = doc.GetElement(refElemLinked.ElementId) as RevitLinkInstance;
                        Document docLinked = elem.GetLinkDocument();
						
						linkedelement = docLinked.GetElement(refElemLinked.LinkedElementId);

Thanks & Regards

Sanjay Pandey

Message 18 of 24

Anonymous
Not applicable

Sorry, but the content above doesn't resolve the problem.

The question I asked previously is about selecting in the 3D view elements inside linked files based on the ids of those elements, no the other way around(picking elements with pickObject and getting the reference for the element picked).

 

thanks anyway

0 Likes
Message 19 of 24

Anonymous
Not applicable

@cwaluga Its almost 5 years now.!

did you find anything on how we can implement this now ? I beleive Autodesk hasn't exposed the API , is that true? 

0 Likes
Message 20 of 24

cwaluga
Advocate
Advocate

No, still waiting here. Better handling of link-interactions in the API is pretty much on top of my wishlist.