Link multiple DWG

Hi!
I’m trying to use the node “LinkDwg.AtOrigin” of Morpheus package for a link multiple DWG in diferent views. I’m a beginner user of dynamo.

I’ve prepared an excel with all views on first column and all file paths in the second one.
So, I’ve imported data to dynamo, I’ve create two lists, file paths and vides, but when I run the rutine I get an error, and I don’t know why.

If I punt only one file path anda one view, rutine works perfectly. But I need to link several DWG…

Could you help me!?
Thank you!

Hi @ariviti6,

Try with the longest lacing
(Right click on LinkDWG node and change the lacing).

Don’t work. Then the error is:


AG_dwg to revit.dyn (24.9 KB)

Hello…not sure but you feed in a string in your view have tried with views ?

try view by name from genius loci…

1 Like

Thank you @sovitek, using “view by name”, rutine links all dwg file into the revit model, and put them into selected views.

Thank you! I have so much for learn!

@sovitek This is seems to be broken in Revit 2026. Is there another node that does link to view ?

Just to confirm it works fine R22; 23; 24 it doesnt work in 25 or 26

Hi @Tim.r it should work in 25-26 as well, but need ironpyhon installed , work both with 2.7 and 3.0

1 Like

and with pythonnet 3..

import clr
import traceback
import os

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

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

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

doc = DocumentManager.Instance.CurrentDBDocument
TransactionManager.Instance.EnsureInTransaction(doc)

def linkdwg(fpath, doc, view, pin_link, this_view_only, black_and_white, units_str):
    if not fpath or not os.path.exists(fpath):
        return None, f"Invalid path: {fpath}"

    options = DWGImportOptions()
    options.AutoCorrectAlmostVHLines = True
    options.ColorMode = ImportColorMode.BlackAndWhite if black_and_white else ImportColorMode.Preserved
    options.OrientToView = True
    options.ThisViewOnly = not this_view_only
    options.VisibleLayersOnly = True

    units = str(units_str).lower() if units_str else ""
    if units == "foot": options.Unit = ImportUnit.Foot
    elif units == "inch": options.Unit = ImportUnit.Inch
    elif units == "meter": options.Unit = ImportUnit.Meter
    elif units == "centimeter": options.Unit = ImportUnit.Centimeter
    elif units == "millimeter": options.Unit = ImportUnit.Millimeter
    else: options.Unit = ImportUnit.Default

    options.Placement = ImportPlacement.Origin

    try:
        link_instance, link_result = ImportInstance.Create(doc, view, fpath, options)
        if link_instance is None or not link_instance.IsValidObject:
            return None, f"Failed to link: {fpath}"
        link_instance.Pinned = pin_link
        return link_instance, f"Linked successfully: {fpath}"
    except Exception as e:
        return None, f"Error linking {fpath}: {str(e)}"

try:
    dwg_files = IN[0] if isinstance(IN[0], list) else [IN[0]]
    dwg_files = [f for f in dwg_files if f]
    if not dwg_files:
        OUT = "No valid DWG file paths provided"
    else:
        views_input = IN[1] if isinstance(IN[1], list) else [IN[1]]
        views = [v for v in UnwrapElement(views_input) if v]
        if not views:
            OUT = "No valid views provided"
        else:
            pin_link = bool(IN[2]) if IN[2] is not None else False
            this_view_only = bool(IN[4]) if IN[4] is not None else True
            black_and_white = bool(IN[5]) if IN[5] is not None else False
            units_str = IN[6] if IN[6] else ""

            if len(views) == 1 and len(dwg_files) > 1:
                views = views * len(dwg_files)
            elif len(dwg_files) == 1 and len(views) > 1:
                dwg_files = dwg_files * len(views)

            if len(dwg_files) != len(views):
                OUT = f"Error: number of views ({len(views)}) does not match number of DWG files ({len(dwg_files)})"
            else:
                links = []
                logs = []

                if IN[3]:
                    wstable = doc.GetWorksetTable()
                    activewsid = wstable.GetActiveWorksetId()
                    wsid = WorksetId(IN[3].Id)
                    WorksetTable.SetActiveWorksetId(wstable, wsid)
                else:
                    activewsid = None

                for fpath, view in zip(dwg_files, views):
                    link, log = linkdwg(fpath, doc, view, pin_link, this_view_only, black_and_white, units_str)
                    if link: links.append(link)
                    logs.append(log)

                if activewsid:
                    WorksetTable.SetActiveWorksetId(wstable, activewsid)

                OUT = (links, logs)

except Exception:
    OUT = traceback.format_exc()

finally:
    TransactionManager.Instance.TransactionTaskDone()

1 Like

I have this Simpson Doh! moment now, :grinning_face: Thanks @sovitek

yeah i always have that here, thats why the forum is so great :wink: :wink:

Thank you, this is even better. Regarding the previous script; If I have a list of levels and I would like to add cad to each correspoding level, could I create a similar list in codeblock and point to File path, instead of manually pointing to them?

1 Like

not sure havent tried, please create a new post with that ,s

Great, will do, thanks

1 Like