File Path for Linked CAD

Hi,

I’m trying to create a tool that re-path Linked CAD. However, I am stuck on getting the file path of the import instances (it’s part of the workflow), which confuses me because I use the same method in Dynamo Python and it works…

Here is my code:

dwgs = DB.FilteredElementCollector(revit.doc)\
             .OfClass(DB.ImportInstance)\
             .WhereElementIsNotElementType()\
             .ToElements()

for dwg in dwgs:
    if dwg.IsLinked:
        Ref = dwg.GetExternalFileReference()

and the error I got is “This Element does not represent an external file.”

Hi
Would you mind posting the whole code and the stack trace to make sure where the error is.

I believe your collector is incorrect -
I had written this code a while back - check if this works for you

allcadlinks = list(FilteredElementCollector(doc).\
                   OfClass(CADLinkType).\
                   WhereElementIsElementType().ToElements())
for cadlink in allcadlinks:
    file_path = cadlink.GetExternalFileReference().GetPath()
    file_path = ModelPathUtils.ConvertModelPathToUserVisiblePath(file_path)
1 Like

Awesome! Works perfectly! Thanks you!