Get Elements in Active Selection

Hi guys,

How can I access selected elements in Grasshopper? I am planning to create a script and distribute it in my company. So users can select some elements and modify them throug Revit Interface.

1 Like

Like Grasshopper Player! I know that’s been discussed but not seeing the Feature Request. I’ll get more info on this and get back to you.

Possible workarounds…
It could be parameter driven, if the idea is that a user just opens a particular script to ‘do something’ on the 4 walls you could have a Text Parameter that they add a value to, which the script would be able to gather the Elements ‘do something’ and clear the parameter.

For instance, I have rooms in the floorplan and I want the user to only select rooms and click the script. Then script will generate floor from room boundaries.

I am making the selection before running the script.

Other way around is also works. First start the script then select elements. Script will do its thing.

Here’s what i was thinking.

You select the Elements and add a Yes to the parameter, then use grasshopper player to run the script.

Re_RhinoInsidePlayer.rvt (1.5 MB) Revit 2022
RhinoInsidePlayer.gh (8.6 KB)

1 Like

This is a hacky way to do. It works you are right but I would definitely prefer just making a selection then running it.

First post here, Hi,
you might want to try something in Python, the syntax below is Dynamo4Revit related but should be easily adaptable

import clr

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

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager

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

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

ueWrapper = None
wrappers = clr.GetClrType(Revit.Elements.ElementWrapper).GetMethods()
for w in wrappers:
	if w.ToString().startswith("Revit.Elements.UnknownElement"):
		ueWrapper = w
		break
		
out = []
selectElemId = uidoc.Selection.GetElementIds()
selectElem = [doc.GetElement(xId) for xId in selectElemId]

for i in selectElem:
	try:
		check = Revit.Elements.ElementWrapper.Wrap(i, True)
	except: check = None

	if check is None:
		out.append(ueWrapper.Invoke(None, (i, False)))
	else:
		out.append(i) 

OUT = out
2 Likes

Oh ok. I will give it a try when I get a chance. Either way, native component would be great.

I ended up creating a Python component to get Active Selection from Revit.
Get Active Selection.gh (2.8 KB)

__author__ = "mbgoker"
__version__ = "2021.09.09"

import clr
clr.AddReference('System.Core')
clr.AddReference('RhinoInside.Revit')
clr.AddReference('RevitAPI') 
clr.AddReference('RevitAPIUI')

from System import Enum, Action

import rhinoscriptsyntax as rs
import Rhino
import RhinoInside
import Grasshopper
from Grasshopper.Kernel import GH_RuntimeMessageLevel as RML
from RhinoInside.Revit import Revit, Convert
# add extensions methods as well
# this allows calling .ToXXX() convertor methods on Revit objects
clr.ImportExtensions(Convert.Geometry)
from Autodesk.Revit import DB
from Autodesk.Revit import UI

# access the active document object
doc = Revit.ActiveDBDocument
uidoc = Revit.ActiveUIDocument

# a few utility methods
def show_warning(msg):
    ghenv.Component.AddRuntimeMessage(RML.Warning, msg)

def show_error(msg):
    ghenv.Component.AddRuntimeMessage(RML.Error, msg)

def show_remark(msg):
    ghenv.Component.AddRuntimeMessage(RML.Remark, msg)
element = []

selection = uidoc.Selection
elementid = selection.GetElementIds()
for id in elementid:
    e = doc.GetElement(id)
    element.append(e)
2 Likes

Hi @mucahitbgoker,

Any ‘Graphical Element’ can be used for this purpose as well.

It also allows you to save your selections in the document using ‘Externalise selection’ or even reference Rule-Based Filters stored on your document.

3 Likes

How did I miss that? :sweat_smile: thanks @kike, I’ll replace my Python node with this one.

1 Like

hey @kike,

I think Active Selection doesn’t work for Linked Elements.

2 Likes

Hi Kike this is an very elegant and minimal way to implement it.
I have to admit that I could also only discover it because of this post here.
It might make sense to add a separate component for it, so that users can discover it when searching for “active” together with the other components like active -workset, -document, -graphical view and -design option.
And like Mucahit I’m also missing a way to reference the active selection of linked elements. Is it just not possible right now, or am I missing out on something.
Thanks in advance, and thanks for your great developement in general!
image

This is still not an option (preselect linked elements). I’ll create a feature request for a new component named Active Selection with that functionality, would definitely be useful. Thanks.

2 Likes

Hi @mucahitbgoker, I’m interested, did you find a way to get the active selection of linked elements?

This will be hard, even Revit struggles with Linked elements, good luck @Japhy

There are several exciting additions to Revit API with linked Elements, unfortunately this particular selection request doesn’t appear to be available.

image
Here’s what happened when I tried to launch Player with an Active Selection button saved in the script. Revit did not like that.

Can you share your script? I see you’re on Revit 2022, what release of Rhino.Inside.Revit might be relevant as well. Thanks.

image
image
Active Selection bug.gh (91.2 KB)
There you go

What type of Element do you have selected that is going to Face > Brep?

Are the metahopper components required in this workflow?

There also appears to be something refreshing the grasshopper constantly…