hello i mtrying to make a script that detects the mouse cursor hovering on a revit active view and creates a point right on my cursor in revit active view , something like this , Parakeet on Grashopper/rhino,ive tried creating a python script that does that , but the point is offseted from the cursor
Cotation par clique.dyn (25.4 KB)
It can work, however there will be a translation of the window rectangle and mouse position to the 3d view.
Look at using the ReferenceIntersector, uiview.GetWindowRectangle() and uiview.GetZoomCorners()
Working with the UIDocument and UIDocument.Selection can add quite a bit of overhead
Here is an example in C# from Jeremy Tammik’s GitHub [link]
You can use python ctypes more effectively like this
from ctypes import byref, windll, wintypes
from time import sleep
def get_mouse_positon():
point = wintypes.POINT()
windll.user32.GetCursorPos(byref(point))
return point
def is_key_pressed(*args):
def key_pressed(key):
return bool(windll.user32.GetAsyncKeyState(key) & 0xFF)
return any(map(key_pressed, args))
# Windows key codes
MSE_RBUTTON = 0x02
ESC = 0x1B
while IN[0]: # noqa: F821
if is_key_pressed(ESC, MSE_RBUTTON):
print("Exiting.")
break
point = get_mouse_positon()
... # Your code goes here
sleep(0.5)
I am curious for the use case
.
Hi,
a workaround with ISelectionFilter class
import sys
import clr
import System
from System.Collections.Generic import List
from System import Array
clr.AddReference('ProtoGeometry')
import Autodesk.DesignScript.Geometry as DS
from Autodesk.DesignScript.Geometry import *
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
clr.AddReference('RevitAPIUI')
import Autodesk.Revit.UI as RUI
from Autodesk.Revit.UI import *
from Autodesk.Revit.UI.Selection import *
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
uidoc = uiapp.ActiveUIDocument
import traceback
class Custom_SelectionElem1(ISelectionFilter):
__namespace__ = "CustomSelectionElem_78de78"
def AllowElement(self, e):
return True
def AllowReference(self, ref, point):
print(point)
return True
try:
RUI.TaskDialog.Show("Info", "Move cursor on a face, tap ESC to quit")
ref = uidoc.Selection.PickObject(ObjectType.Face, Custom_SelectionElem1())
except Exception as ex:
print(ex)
another example involving transient solids
hi thanks for the reply! i actually need to use this in a plan view and not 3D view, does the script work for 2D plan views or only 3d views?
hi sir, thanks for the reply , its getting me this error for some reason 
Just in case you guys are wondering what would the use for this , i actually did it on grashooper inside rhino using Parakeet plugin for grasshopper , but im trying to figure out a way on how to actively detect mouse cursor position in dynamo on the active view and actively detect the closest edge to my cursor and out of that i offset it and then i can extend the line by pressing on some keyboard keys , and then when i click on LMB the line is created and then i turn them into Revit Dimension lines,now for Dynamo i just need to find a way to actively detect and draw the blue point on my cursor on my current Revit active view , i can do the rest i just need this part! hopefully this videos explains what i mean! thanks guys!
The example in C# can use plan views, however it does a translation of the plan view to the default 3d view for nearest inference and then uses that for selection. This can cause issues because if you have a slab floor it will not select anything underneath (no tabbing).
You can set the inferencing to edges with FindReferenceTarget enum
need to use the PythonNet3 engine
can you please tell me how do i download it? is it a plugin for dynamo?
Search it on Dynamo package manager. Be sure to get the package version which aligns to your Revit and Dynamo version.
hi thanks , but for some reason its still showing the same error even after installing Pythonnet3 engine
You have to change the engine - right click on the Python node and look at your options. A restart of Revit may be required first.
should i change the version? i use Revit 2025 , which pythonnet 3 engine should i download?
You’ve downloaded and installed it. Now you have to tell the Python node to use it. Right click on the node, go into the Python Engine Version context menu, select PythonNet3 instead of CPython3.
it shows only these two , thers no other engine ?
Looks like you installed the wrong version of the package. Sadly the version information isn’t setup correctly for the package (team has been notified), so you’ll have to do a bit of install/uninstall/install/uninstall/install until you get the version you need.
The specific version will depend on on which version of Revit and Dynamo you’re in. I think you might be in Revit 2025, so perhaps Dynamo 3.3, so perhaps you need PythonNet3 version 1.1.1.
i have PythonNet3 version 1.1.1 and still same error , @c.poupin could you please adapt the script to work with cpython3 if possible? and will it work in plan view? thanks so much!
Might be a limitation of Revit 2025. The PythonNet3 engine was experimental back then. The CPython3 engine isn’t viable for this type of work usually (I’ll let Cyril confirm), so you may have to either move to C# or remove support for 2025.
is there a way to change the python script into C# please how ?