Get linked Revit document

Hi everyone,

i am trying to get the document of a linked model using python. the code works but im getting the linked document double!
anyone have an idea what is the reason and hou to get only one document?

import System
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

from System.Collections.Generic import *


clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Analysis import *

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)


rlinkName = IN[0]


linkedDocument = []
collector = Autodesk.Revit.DB.FilteredElementCollector(doc)
linkInstances = collector.OfClass(Autodesk.Revit.DB.RevitLinkInstance)

linkDoc, linkName = [], []
requiredView = []
for i in linkInstances:
    linkDoc.append(i.GetLinkDocument())
    linkName.append(i.Name)


OUT = linkDoc, linkName

image

hi @Mohamad_Kayali , you can write a function to get the unique items from a list like this

def unique_items(items, keys):
    unique_keys = set()
    unique_items = []
    
    for ind, key in enumerate(keys):
        if key not in unique_keys:
            unique_keys.add(key)
            unique_items.append(items[ind])
    
    return unique_items

the items list is the linkDoc and the keys list is the linkName.

Hi Luffy, thank you for your reply i know that i even can have unique items by getting the first element of each list (OUT = linkDoc[0], linkName[0]) . but im trying to understand why the collector is returning two instances while i have one model linked, what is the diference between them and what is the best way to get only one instance using the class RevitLinkInstance.

Can you share the Revit model? Because I don’t see any problem with your code.

You’re right. the problem is in my model. whan i made a new one I got only one instance. also when when i have removed the linked model and insert it again I get only one instance. unfortunately I cant share the big model. I will get the first index for now and try to findout what is causing the double instances in my model.
thank you anyway.

1 Like