Problem with showing script results

Hi, i am new on this forum (and in pyrevit). I watched all pyRevit tutorials and now i try to write my first “button”. So i start from easy project (but not easy for me…). I want to pick elements and linked elements in model, and then show the lists of them (i know there are few programs for this but this is learning project). I spent a lot of time (written few scripts) but nothing is change… I can’t extract and print the data. My script looks:

from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Architecture import *
from Autodesk.Revit.DB.Analysis import *

import clr
import System.Windows
import rpw
from rpw import revit, DB
from rpw.db.element import Element

uidoc =rpw.uidoc
doc = rpw.doc

from Autodesk.Revit.UI import *


def alert(msg):
    TaskDialog.Show('xxx', msg)

def quit():
    __widow__.Close

el = [rpw.uidoc.Selection.pick_element(multiple=True)]
ellink =[rpw.uidoc.Selection.pick_linked_element(multiple=True)]

for x in el:
    print("Elements in model"+x._revit_object.ElementId)

for x in ellink:
    print("Elements in linked model"+x._revit_object.LinkedElementId)

Could you help a little bit? What i need to change?

[Nice, now after rerun i get error]

Ok so i rewrite my script. Now it works but i have another question. What i need to change in ISelectionFilter to use it in pick linked object. No i can’t pic anything…

import clr
import sys
import os

from rpw import revit
from Autodesk.Revit.UI.Selection import *
from pyrevit import forms


doc = revit.doc
uidoc = revit.uidoc


###Thanks to Cyril Waechter https://pythoncvc.net/?p=116 custom ISelectionFilter
class CustomISelectionFilter(ISelectionFilter):
    def __init__(self, nom_categorie):
        self.nom_categorie = nom_categorie
    def AllowElement(self, e):
        if e.Category.Name == self.nom_categorie:
            return True
        else:
            return False
    def AllowReference(self, ref, point):
        return true
###

# Pick model elements
try:
    with forms.WarningBar(title="Pick elements in model"):
        wall_collector = uidoc.Selection.PickObjects(ObjectType.Element, CustomISelectionFilter("Walls"))

except:
    print("No elements")
    
# Pick linked elements
try:
    with forms.WarningBar(title="Pick elements in linked model"):
        wall_collector_link = uidoc.Selection.PickObjects(ObjectType.LinkedElement,CustomISelectionFilter("Walls"))

except:
    print("No linked elements")

# Print Ids
try:
    for i in wall_collector:
            print("Model element "+str(i.ElementId))
except:
    print("No linked elements")
try:
    for i in wall_collector_link:
            print("Linked element "+str(i.ElementId))
except:
    print("No linked elements")

probably sth like this (not tested):

with forms.WarningBar(title="Pick elements in model"):
       linked_reference = uidoc.Selection.PickObjects(ObjectType.LinkedElement, CustomISelectionFilter("Walls"))
       element = doc.GetElement(linked_reference.ElementId)
       linked_doc = element.GetLinkDocument()
       linked_element = linked_doc.GetElement(linked_reference.LinkedElementId)

2 Likes

Thank you for your time! But my PickObjects don’t work with linked elements (it means that in my case when i run script i can’t pick any “wall”(and other elements) from linked model)

Have you tried these @PanPawel_1:

from pyrevit import revit

revit.pick_linked()
# or, if many
revit.pick_linkeds()

or you could look at the way this module is built

also, check out all the code samples ApiDocs.co clicking on the <> sign at the top right

1 Like

a bit of code that works and that I use:

##Select linked elements
with forms.WarningBar(title='Select linked elements and then press Finish'):
    try:
    	elem_refs = tolist(uidoc.Selection.PickObjects(UI.Selection.ObjectType.LinkedElement, "Select linked elements"))
    except:
    	script.exit()
1 Like