Link DWG Files

Link DWG Files

Anonymous
Not applicable
2,910 Views
4 Replies
Message 1 of 5

Link DWG Files

Anonymous
Not applicable

Hey guys,

 

Should be a trivial question but I can't get this thing done and I can't understand why.

 

What I'm trying to do is to link some DWG files inside of Revit project. 

I've seen from the SDK that now from Revit 2018 there's the API LoadFrom(string Path) and that looked like what I needed, right?

 

But for some reason I can't use it in my program.

See the code:

            // Hardcode the path just for testing
            string path = @"C:\Users\atassera\ARC-2228-01.DWG[6].dwg";

            using (Transaction trans = new Transaction(doc))
            {
                trans.Start();

                CADLinkType.LoadFrom(path);
                
                trans.Commit();
            }

Ok the code is not much, I understand, but I thought this would have been pretty much it, considering that the LoadFrom method asks for a file path as a string...

The problem is that Visual Studio doesn't even give me .LoadFrom() as an option/suggestion and if I type it in, then it says "An object reference is required for the non-static field, method, or property....."

 

I've also seen there's the Link Method (String, DWGImportOptions, View, ElementId) from the Document class, but I don't want to link my DWG file to a view. I just want to normally link it as you would do from the Links Manager. That's why I didn't pursue that path. 

 

So it's clear there's something missing but I can' understand what. Any revelation to share?

 

Thanks everyone!

 

Andrea

0 Likes
2,911 Views
4 Replies
Replies (4)
Message 2 of 5

JimJia
Alumni
Alumni

Dear Andrea Tassera,

 

The LoadFrom method is not static, so you can not access from class CADLinkType directly.

 

If you want to call LoadFrom, you have to build one CADLinkType instance firstly.

 

Following code demonstrates how to import CAD firstly, and then use LoadFrom to load again.

 

// link CAD firstly
DWGImportOptions opt = new DWGImportOptions();
opt.Placement = ImportPlacement.Origin;
opt.AutoCorrectAlmostVHLines = true;
opt.ThisViewOnly = false; // not this view only
opt.Unit = ImportUnit.Default;
ElementId linkId = ElementId.InvalidElementId;
using (Transaction tran = new Transaction(RevitDoc, "Quick Link"))
{
	tran.Start();
    RevitDoc.Link(path , opt, RevitDoc.ActiveView, out linkId);
    tran.Commit();
}
//
// get the link instance and the CADLinkType
ImportInstance cadInst = RevitDoc.GetElement(linkId) as ImportInstance;
CADLinkType cadLinkType = RevitDoc.GetElement(cadInst.GetTypeId()) as CADLinkType;
cadLinkType.LoadFrom(new_path);

 

 

 

 


Jim Jia
Autodesk Forge Evangelist
https://forge.autodesk.com
Developer Technical Services
Autodesk Developer Network
Email: Jim.Jia@autodesk.com
0 Likes
Message 3 of 5

rodrigohbm
Advocate
Advocate

Hello
I found the loadfrom
in vb.net
attached code in image.

the code consists of searching all the cadlinks of Revit
to then select a DWG for later
Change the cadlink in the textbox.


regards

 

image.png

0 Likes
Message 4 of 5

Anonymous
Not applicable

How can we achieve the same function in API 2017 for backwards compatibility?

Understand that there is a method to use write transmission data.
https://thebuildingcoder.typepad.com/blog/2011/10/using-the-writetransmissiondata-method.html

However specifically for CAD files, the externalFileReferenceType does not show a CADLink, rather it shows a KeynoteTable / AssemblyCodeTable?

Are these the correct reference type for CAD files? 

Message 5 of 5

chinh.trieutien
Explorer
Explorer

You should use Document.Link() method to archive that.

 

http://www.revitapidocs.com/2018.2/f3112a35-91c2-7783-f346-8f21d7cb99b5.htm

 

https://www.youtube.com/watch?v=kPmDk2V1v9g

 

def link_em(view, ref):

    options = DB.DWGImportOptions()
    options.Unit = DB.ImportUnit.Millimeter
    options.Placement = DB.ImportPlacement.Origin
    options.ColorMode = DB.ImportColorMode.Preserved
    options.CustomScale = 0.0
    options.AutoCorrectAlmostVHLines = False
    options.ThisViewOnly = False
    options.OrientToView = False
    options.VisibleLayersOnly = True

    try:
        msg = 'Attemting to link file {} to view {}'\
            .format(op.split(ref)[-1], view.Name)
        print msg

        doc.Link(
            ref,
            options,
            view,
        )
    except Exception as e:
        msg = 'Fail when attemting to link file {} to view {}'\
            .format(op.split(ref)[-1], view.Name)
        boyexcept.INFO(msg)
0 Likes