Nov 2, 2023

Open/Reload Selected CAD Links in Revit

Are you tired of looking for your linked CAD files in Revit? Use this tool to save time by selecting DWG in Revit and then clicking on Open/Reload/Copy Path of selected DWG.

Where is Waldo? I mean CAD Link…

There are times when we work in Revit and we need to open linked DWG file. And Revit doesn't make that easy.

We can see all links in Manage Links menu, but then we have to scroll through until we find the DWG file name and we can't even copy its path… We need to look at it and follow the path in our folder structure.

And if you work on a large project there is a chance that you have a ton of links already. So it can quickly become a nightmare. I often found myself feeling like I play Where is Waldo game because it's unnecessary complicated to find your Linked DWG's filepath.

So I made a solution that can save you time and nerves to find your Linked DWG files.

How to use it?

This tool is part of EF-Tools, and it's very simple to use:

  • Select Linked DWG file in Revit

  • Click on the Button

  • Choose one of the 4 options

And the tool will do exactly what you selected it to do.


How does it work?

The code is written in python and executed with pyRevit. If you are interested in Revit API and want to understand how I made it work, let's break it down.

There are a few things we need to cover:

  • How to Select Linked DWG?

  • How to Open Linked DWG?

  • How to Open Linked DWG's Folder?

  • How to Reload DWG?

  • How to copy filepath to Clipboard?

How to Select Linked DWG?

First of all we need to select a Linked DWG Element in Revit.

I chose a simple way of doing it:
I just collect ElementIds of all selected elements with uidoc.Selection.GetElementIds, and I iterate until I find ImportInstance element.

And if it's not found, then I create an menu with pyrevit.forms.alert to notify user that ImportInstance wasn't in the selection, please try again.

# -*- coding: utf-8 -*-
__title__ = "DWG: Open/Reload"
__author__ = "Erik Frits"

#⬇️ Imports
from Autodesk.Revit.DB import *
from pyrevit import forms

#📦 Variables
doc   = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument

#🧬 Functions
def get_selected_dwg_ImportInstance():
      """Try to get an ImportInstance from selected elements."""
      selected_elements = uidoc.Selection.GetElementIds()

      if selected_elements:
          for element_id in selected_elements:
              element = doc.GetElement(element_id)
    
              # FILTER SELECTION
              if type(element) == ImportInstance:
                  return element
      else:
          alert("Linked ImportInstance was not selected. "
                "\nPlease try again.", __title__, exitscript=True)

         
#🎯 Get Selected DWG Instance
selected_dwg = get_selected_dwg_ImportInstance()

How to Get DWG Filepath?

Once we have an ImportInstance we can find it's AbsolutePath, as long as it is a Linked DWG.

Let's create a function and break it down into steps:

import os

def get_import_instance_path(import_instance):
    # type:(ImportInstance) -> str
    """Function to get a path of the selected ImportInstance if it is linked."""

    if import_instance.IsLinked:
        cad_linktype_id = import_instance.get_Parameter(BuiltInParameter.ELEM_FAMILY_PARAM).AsElementId()
        cad_linktype = doc.GetElement(cad_linktype_id)
        efr = cad_linktype.GetExternalFileReference()
        dwg_path = ModelPathUtils.ConvertModelPathToUserVisiblePath(efr.GetAbsolutePath())
        return dwg_path
    else:
        forms.alert("Selected DWG instance is not linked.", __title__, exitscript=True)


dwg_filepath    = get_import_instance_path(import_instance)
dwg_folder_path = os.path.dirname(dwg_filepath)

How to Open Linked DWG's Folder/File?

To open a file or a folder we can use os package. There is a method called startfile, and it will work for both files and folders.

import os
  
def button_open_dwg(dwg_filepath):
    """Open DWG in a default application and close the GUI."""
    os.startfile(dwg_filepath)


def button_open_folder(folder_path):
    """Open DWG in a default application and close the GUI."""
    os.startfile(folder_path)

How to copy filepath to Clipboard?

Copying to clipboard is a little tricky. But we can do it with the shell command.

First of all we need to prepare the command, and then execute it with Popen, which will run it through cmd.

command = 'echo ' + dwg_filepath.strip() + '| clip'
os.Popen(command, shell=True)

How to Reload DWG?

Lastly, reloading is very simple once you get your CADLinkType. It has a method Reload.
Also don't forget to use Transaction.

t = Transaction(doc,'Reload DWG')
t.Start()
import_instance = self.selected_dwg_ImportInstance
if import_instance.IsLinked:
    cad_linktype_id = import_instance.get_Parameter(BuiltInParameter.ELEM_FAMILY_PARAM).AsElementId()
    cad_linktype = doc.GetElement(cad_linktype_id)
    cad_linktype.Reload()
t.Commit()