Selecting in Linked Documents

Selecting in Linked Documents

cwaluga
Advocate Advocate
7,168 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,169 Views
23 Replies
Replies (23)
Message 21 of 24

KahnSir
Participant
Participant

@Anonymous 

Thanks Sanjay.
This is what I am seeking. Your code help to solve my problem smoothly.

0 Likes
Message 22 of 24

AGGilliam
Collaborator
Collaborator

I think @Anonymous was on the right track, I placed an element in another document and linked it and was able to select that element in the host document through the api. Here's what I've got:

public Result Execute(ExternalCommandData data, ref string message, ElementSet elements)
        {
            UIDocument uidoc = data.Application.ActiveUIDocument;
            Document doc = uidoc.Document;

            using (Transaction t = new Transaction(doc, "Filter"))
            {
                t.Start();

                FilteredElementCollector collector = new FilteredElementCollector(doc);
                collector = new FilteredElementCollector(doc);
                List<Element> elems = collector.OfCategory(BuiltInCategory.OST_RvtLinks).ToElements().ToList();
                List<Element> allElems = new List<Element>();
                List<ElementId> selection = new List<ElementId>();
                foreach (Element e in elems)
                {
                    allElems.Add(e);
                }
                foreach (Element e in allElems)
                {
                    if (e is RevitLinkInstance)
                    {
                        RevitLinkInstance linkInstance = e as RevitLinkInstance;
                        selection.Add(e.Id);
                    }
                }

                uidoc.Selection.SetElementIds(selection);

                t.Commit();
            }

            return Result.Succeeded;
        }

This would select all of the linked elements in the host document, but that's essentially what you're looking for, correct?

0 Likes
Message 23 of 24

VeevDesignAutomation
Explorer
Explorer
We are also struggling with the same issue.
Is there any open case for Autodesk?
0 Likes
Message 24 of 24

VanQuyet.Doan
Enthusiast
Enthusiast

I have a code to copy element in link file

https://forums.autodesk.com/t5/revit-api-forum/elementtransformutils-copyelements-from-linked-docume...

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiApp = commandData.Application;
UIDocument uidoc = uiApp.ActiveUIDocument;
Document hostDoc = uidoc.Document;

// Get the link
FilteredElementCollector links = new FilteredElementCollector(hostDoc).OfClass(typeof(RevitLinkInstance));
Document linkedDoc = links.Cast<RevitLinkInstance>().FirstOrDefault().GetLinkDocument();

//// Get familys in link
ISelectionFilter filter = new WallSelectionFilterLink(hostDoc);
IList<Reference> walls = uidoc.Selection.PickObjects(ObjectType.LinkedElement, filter, "Select walls");

List<ElementId> ids = new List<ElementId>();
foreach (Reference reference in walls)
ids.Add(reference.LinkedElementId);

if (ids.Count == 0)
{
TaskDialog.Show("Copy Element", "No Wall selected.");
}
else
{
using (Transaction targetTrans = new Transaction(hostDoc))
{
CopyPasteOptions copyOptions = new CopyPasteOptions();

copyOptions.SetDuplicateTypeNamesHandler(new CopyUseDestination());

targetTrans.Start("Copy and paste linked families");

ElementTransformUtils.CopyElements(linkedDoc, ids, hostDoc, null, copyOptions);

//hostDoc.Regenerate();
targetTrans.Commit();
}

TaskDialog.Show("Copy Element", "Have copied data.");
}
return Result.Succeeded;
}

0 Likes