Second attempt editing a Python script

Hi all,

I found this Python code by @Kulkul

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

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument

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

import System
from System import Array
from System.Collections.Generic import *

cat = [BuiltInCategory.OST_ReferenceLines]
bic = List[BuiltInCategory](cat)
filter = ElementMulticategoryFilter(bic)
fec = FilteredElementCollector(doc).WherePasses(filter).ToElements()
OUT = fec

in this topic.

It works fine when you work inside a family document. But i would like it to work from the project environment on a list of Family Instances. So i tried to edit the Python code, this what i have so far.

import clr
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
import System
from System import Array
from System.Collections.Generic import *

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

elements = UnwrapElement(IN[0]) if isinstance(IN[0],list) else [UnwrapElement(IN[0])]

#Get ReferenceLines from Families
for element in elements:
	referencelines = []
	cat = [BuiltInCategory.OST_ReferenceLines]
	bic = List[BuiltInCategory](cat)
	filter = ElementMulticategoryFilter(bic)
	fec = FilteredElementCollector(doc).WherePasses(filter).ToElements()
	referencelines.append(fec)
OUT = referencelines

Can it be done this way? Any help is appreciated :slightly_smiling_face:

Nope.
Reference lines only exist inside families. They cannot be drawn directly in a Revit project, unlike Reference Planes that can.

So, you would need to dig into each family and pull out a reference to the nested reference line.

Hi @aaronrumple,

I use nodes from the ā€œGenius Lociā€ package to select the ā€œAutodesk.Revit.DB.Referenceā€ elements inside the families.
Only I canā€™t read any information from the mentioned DB elements (e.g. subcategory). So then I have a list of References but no information to sort them.
It is therefore not my intention to draw reference lines in the project environment. I would just like to consult the (unwrapped) reference lines within the families to read out the parameter values. I could then use these parameter values to sort the DB elements, otherwise I donā€™t know if Iā€™m selecting the correct reference as the basis for dimensioning.

1 Like

Thatā€™s the correct approach. But you canā€™t use a FilteredElementCollector at that level. It is going to be family instance by family instance.

Youā€™ll need to make a list of tuples to track what element has what reference list.
[[Family instance, Reference Line]
[Family instance, Reference Line]
etc]

@aaronrumple, Thanks for your tips so far :slightly_smiling_face:
It seems that I have chosen the wrong starting point (and I have gone far beyond my knowledge on this one :woozy_face:). After some research on: www.revitapi.com I think I can use the method below.
https://www.revitapidocs.com/2019/536b3d7a-ec8d-29f6-5957-751468c98dd0.htm

The steps I think I need to take.

  1. Input FamilyInstances
  2. Unwrap FamilyInstances
  3. Select References Inside the unwrapped FamilyInstances.
  4. Use the method above to get the geometry objects from the References (ModelLines/Planes).
  5. Get the subcategory parameter from the geometry objects.
  6. Write the references and corresponding subcategory parameters to the output.

But Iā€™m confused about the depth of the nested for loops and the intermediate writing of the data to Tuples.
Any advice to point me in the right direction (for the next step) is appreciated.

I have some bad experiences asking ChatGPT to create Python code, its not ready to do that. But it helps me a lot to get a better understanding if i ask ChatGPT advise for little steps. The steps above would result in the next ā€œBETAā€ code.

# inputs
inFamilies = IN[0]

# outputs
outReferences = []
outCategories = []

# iterate over the input FamilyInstances
for family in inFamilies:
    # unwrap the FamilyInstance
    element = UnwrapElement(family)
    # get the references from the element
    references = element.GetReferences(FamilyItemReferenceType.Both)

    # iterate over the references
    for reference in references:
        # get the geometry object from the reference
        geoObject = element.GetGeometryObjectFromReference(reference)
        # get the subcategory parameter from the geometry object
        subcategory = geoObject.GraphicsStyle.GraphicsStyleCategory.Subcategory

        # store the subcategory parameter in a list
        outCategories.append(subcategory.Name)
        # create a tuple of the reference and subcategory parameter
        outTuple = (reference, subcategory.Name)

        # append the tuple to the output list
        outReferences.append(outTuple)
# return the output lists
OUT = (outReferences, outCategories)

As i said there might be a lot wrong with this code, but for now i would like to know if this is the right structure to achieve what i have in mind.

Just run it and see what you get.

Hi,

It doesnā€™t exist in the API. Replace it with FamilyInstanceReferenceType.CenterLeftRight

Try it, but itā€™s probably a dead end. I donā€™t think you can return the GraphicsStyleId that doesnā€™t exist in your document context.

1 Like