Get Link Element by Category with Python

Hi guys,
For some reason in the attached script I won’t be able to use custom nodes so I have to somehow make a work around to replace the use of bimorph and orchid nodes with python script.
The attached script is prepared for Revit2022 + Dynamo 2.12.
It moves coordination link by comparison of current document coordination grids intersection point with analogous grids in linked coordination model. Than acquires shared site from that link.

The first issue is the Bimorph nodes group.
I would like to achieve exactly the same thing. Select elements in link by chosen category then convert them to “standard elements” but with a Python script. Would anyone be willing to help me with this?

The second one, I know it’s not exactly related with this topic title - I’m sorry for that, is to get link insertion point like I did with orchid node. I think this node reads insert point by link’s internal origin, what is fine for me.

Could you please help me with those two?

Cheers,
Jacek



Coordinates_Adjustment_2022.dyn (144.0 KB)
Site.rvt (3.3 MB)
XXXOB1-XXX-XXX-XX-XXXXX-XXX-XX.rvt (3.4 MB)

That’s a lot of work to duplicate the efforts of those package authors. Certainly less effort to use the packages directly, and a better practice overall.

2 Likes

Hi Jacob, thank you for the answer!
Yes, I know it ain’t the best way to go. Unluckily in this case I have no other option.
Seems like I’ll have to pass this task to some python developers :slight_smile:

Hi,

Here is something to get you there quickly if you really need it, but as Jacob said better to use custom packages that have been tried and tested.

GET LINK INSTANCES

import clr
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
import Autodesk
from Autodesk.Revit.DB import *


doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication 
app = uiapp.Application 
uidoc = uiapp.ActiveUIDocument

linked_instances = FilteredElementCollector(doc).OfClass(RevitLinkInstance).ToElements()


OUT = linked_instances

GET ELEMENTS OF CATEGORY

import clr
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
import Autodesk
from Autodesk.Revit.DB import *

def get_linked_elements_of_category(linked_instance, category):
    linked_doc = linked_instance.GetLinkDocument()
    # Use a filtered element collector to get elements of a specific category
    linked_collector = FilteredElementCollector(linked_doc)
    linked_elements = linked_collector.OfCategory(category).WhereElementIsNotElementType().ToElements()
    
    return linked_elements
    
linked_instance = UnwrapElement(IN[0])
cat = UnwrapElement(IN[1])

built_in_category = cat.BuiltInCategory
linked_elems = get_linked_elements_of_category(linked_instance, built_in_category)

OUT = linked_elems

Hope it helps

2 Likes

Wow, that’s awesome! Thanks Josip!
I know, I know, but when it comes to running scripts with VDI, custom packages are not so handy.

For some reason I receive an error with the categories script.

About the “GET LINK INSTANCES” script, well I’m sorry to say that, but it was already solved in the script with Design Script code block :sweat_smile: What I need as a second script is to get an insert location of Link instance.

Anyway, great job! Thank you so much for helping me :slight_smile:

Oh,

you are using an older version of Revit.

Ok than just take at this to find which BuiltInCategory you need and you can enter it manually.

import clr
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
import Autodesk
from Autodesk.Revit.DB import *

def get_linked_elements_of_category(linked_instance, category):
    linked_doc = linked_instance.GetLinkDocument()
    # Use a filtered element collector to get elements of a specific category
    linked_collector = FilteredElementCollector(linked_doc)
    linked_elements = linked_collector.OfCategory(category).WhereElementIsNotElementType().ToElements()
    
    return linked_elements
    
linked_instance = UnwrapElement(IN[0])
category = BuiltInCategory.OST_Grids #Change to the desired BuiltInCategory

linked_elems = get_linked_elements_of_category(linked_instance, category)

OUT = linked_elems

It is possible to use OfCategoryId() too


import clr
import sys
import System
#
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import Autodesk.DesignScript.Geometry as DS

#import Revit API
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
import Autodesk.Revit.DB as DB

#import transactionManager and DocumentManager (RevitServices is specific to Dynamo)
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

#Preparing input from dynamo to revit
linkInstance = UnwrapElement(IN[0])
category = UnwrapElement(IN[1])

OUT = FilteredElementCollector(linkInstance.GetLinkDocument())\
                    .OfCategoryId(category.Id)\
                    .WhereElementIsNotElementType()\
                    .ToElements()
1 Like