How to use RevitLinkType LoadFrom?

How to use RevitLinkType LoadFrom?

BIM.Frankliang
Collaborator Collaborator
1,967 Views
4 Replies
Message 1 of 5

How to use RevitLinkType LoadFrom?

BIM.Frankliang
Collaborator
Collaborator

Dear Friends,

          I want to manage link file worksets open/close, thus I use WorksetConfiguration and RevitLinkType.LoadFrom() to Reload link files in current rvt, but it always makes Revit crash 😞

          I try some methods according to previous Forum records, but all failed...

          Here is my codes:

    foreach (var key in result.Keys)
    {
          var linkdoc = key;
          var openids = result[key].OpenedList.Select(x => x.Id).ToList();
          var closeids = result[key].ClosedList.Select(x => x.Id).ToList();

          var config = new WorksetConfiguration();
          if (openids != null && openids.Any())
          {
                config.Open(openids);
          }
          if (closeids != null && closeids.Any())
          {
                config.Close(closeids);
          }

          var mp = ModelPathUtils.ConvertUserVisiblePathToModelPath(linkdoc.PathName);
          var rlt = rvtlinktypes.FirstOrDefault(x => x.Name.Equals(linkdoc.Title)) as RevitLinkType;
          rlt.LoadFrom(mp, config);
  }

         I hope that you can help me,  many thanks!

 

Best Regards,

Frank Liang

TJAD BIM Software Dev Lead

0 Likes
Accepted solutions (3)
1,968 Views
4 Replies
Replies (4)
Message 2 of 5

BIM.Frankliang
Collaborator
Collaborator
Accepted solution

I solved this issue by myself, haha ~~

I found that RevitLinkType.LoadFrom() is not always successful to change link file workset open/close.

 

Thus, I use this way:

foreach (var key in result.Keys)
{
    var linkpath = key;
    var openids = result[key].OpenedList.Select(x => x.Id).ToList();
    var closeids = result[key].ClosedList.Select(x => x.Id).ToList();

    var config = new WorksetConfiguration(WorksetConfigurationOption.CloseAllWorksets);
    if (openids != null && openids.Any())
    {
          config.Open(openids);
    }

    var filePath = new FileInfo(linkpath);
    var mp = ModelPathUtils.ConvertUserVisiblePathToModelPath(filePath.ToString());
    var linktitle = Path.GetFileName(linkpath);
    var rlt = rvtlinktypes.FirstOrDefault(x => x.Name.Equals(linktitle)) as RevitLinkType;

    doc.Delete(rlt.Id);
    doc.Regenerate();
    RevitLinkOptions option = new RevitLinkOptions(true);
    option.SetWorksetConfiguration(config);
    RevitLinkType.Create(doc, mp, option);
}

Remember that it should run in transaction

 

Wish it is helpful for everybody

 

Best Regards

Frank Liang

TJAD BIM Software Dev Lead

0 Likes
Message 3 of 5

BIM.Frankliang
Collaborator
Collaborator
Accepted solution

I recommend that a Autodesk Help Link:Autodesk Revit 2014 Help

it gave me a lot of Inspirations about link file workset configuration operation.

 

Wish it is helpful for everyone 🙂

 

Best Regards

Frank Liang

TJAD BIM Software Dev Lead

0 Likes
Message 4 of 5

PerryLackowski
Advocate
Advocate

Thank you for posting this, I'm struggling with this same issue.

 

I'm having a bit of trouble understanding the linkpath that you sent to the mp. Is this the path of the RevitLinkType? How do you access it? I have a filtered element collector of my RevitLinkTypes, and I'm accessing each element with the iterator 'e'. Can I just use FileInfo(e)?

0 Likes
Message 5 of 5

PerryLackowski
Advocate
Advocate
Accepted solution

For the record, I managed to get this working in Python using the following code:

'''
Goes to Manage > Manage Links > Manage Worksets and opens the Shared Levels and Grids workset for each LOADED link.

TESTED REVIT API: 2020

Author: Robert Perry Lackowski

'''
from pyrevit import DB
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB import FilteredElementCollector
from Autodesk.Revit.DB import Transaction
from Autodesk.Revit.DB import WorksharingUtils
from Autodesk.Revit.DB import WorksetConfiguration
from Autodesk.Revit.DB import RevitLinkType
from Autodesk.Revit.UI import TaskDialog

from pyrevit import script
output = script.get_output()

import traceback

doc = __revit__.ActiveUIDocument.Document
app = __revit__.ActiveUIDocument.Application.Application

# Set the name of the workset you would like to hide. Decide if you want to hide it or unhide it.
hide_this_workset = "Shared Levels and Grids"
hide_workset = False

for link_doc in app.Documents:
    
    link_doc_title = link_doc.Title
    print link_doc_title

    # Exclude the project file itself
    if not link_doc.IsLinked:
        print "this one isn't linked, skip the rest of the for loop \n\n "
        continue
    
    # Get a list of the RevitLinkType elements in the active document.
    revit_link_types = FilteredElementCollector(doc).OfClass(DB.RevitLinkType).ToElements()
 
    # This finds the RevitLinkType element that matches the link_doc.
    rlt = next((rlt for rlt in revit_link_types if rlt.LookupParameter("Type Name").AsString().rstrip(".rvt") == link_doc_title), None)
    if not rlt:
        print "No matching RevitLinkType for the link_doc."
    else:
        temp = "Updating workset visibility on this file: " + str(link_doc.Title)
        print temp

        # generate a list of the worksets in the link_doc
        workset_table = link_doc.GetWorksetTable()
        model_path = link_doc.GetWorksharingCentralModelPath()
        lst_preview = WorksharingUtils.GetUserWorksetInfo(model_path)

        # Populate list of worksets to enable
        open_this_list_of_worksets = []
        
        for objWorksetPreview in lst_preview:
            wkset = workset_table.GetWorkset(objWorksetPreview.Id)
            
            if hide_workset:
                #gather a list of all the open worksets, EXCLUDING the workset we want to hide
                if wkset.IsOpen and (wkset.Name != hide_this_workset):
                    open_this_list_of_worksets.append(wkset.Id)
            else:
                #gather a list of all the open worksets, INCLUDING the one we hid.
                if wkset.IsOpen or wkset.Name == hide_this_workset:
                    open_this_list_of_worksets.append(wkset.Id)
        
        # Set the WorksetConfiguration file to close all worksets, then open the ones from the list
        workset_config = WorksetConfiguration(WorksetConfigurationOption.CloseAllWorksets)
        workset_config.Open(open_this_list_of_worksets)
        
        #reload the link with the updated workset configuration
        rlt.LoadFrom(model_path, workset_config)
        
        #delete the workset configuration after use
        workset_config.Dispose()

print "Script complete."
0 Likes