Hello Python programmers,
I am having an issue while using IExternalEventHandler. I have selected objects and want to perform get/set methods on them. But my code is giving an error. You can see the attached image and video. I hope someone can help me with this problem. Thanks all.
These are the libraries I have imported.
SeanP
May 8, 2023, 2:01am
3
Try replacing the double quote with single quotes on the imports. I have seen these be weird if you copy and paste.
1 Like
Thank you, i tried but it still doesn’t work
#here is my code
# -*- coding: utf-8 -*-
import clr
import os
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB import Transaction
clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *
from Autodesk.Revit.UI.Selection import *
from Autodesk.Revit.Attributes import *
clr.AddReference('PresentationCore')
clr.AddReference('PresentationFramework')
clr.AddReference('System')
import System
from System.Collections.Generic import *
from System.Windows import Window, MessageBox, MessageBoxButton, MessageBoxImage, RoutedEventHandler
from System.IO import FileStream, FileMode, FileAccess, File
from System.Windows.Markup import XamlReader
import System.Windows.Markup as Markup
from System.Windows.Controls import Button
#Get UIDocument, Document, UIApplication, Application
uidoc = __revit__.ActiveUIDocument
doc = uidoc.Document
uiapp = UIApplication(doc.Application)
app = uiapp.Application
#version_rvt = app.VersionName
# Get the directory path of the script.py file and .xaml file
dir_path = os.path.dirname(os.path.realpath(__file__))
xaml_file_path = os.path.join(dir_path, 'Window.xaml')
class TestEvent(IExternalEventHandler):
def __init__(self, wpfwindow):
self.myWindow = wpfwindow
def Execute(self, _uiapp):
_uidoc = _uiapp.ActiveUIDocument
_doc = _uidoc.Document
self.myWindow.Hide()
try:
select = _uidoc.Selection.PickElementsByRectangle()
t = Transaction(_doc,"Test")
t.Start()
for ele in select:
ele.LookupParameter('Comments').Set('test')
t.Commit()
except Exception as e:
print("Error: {}".format(str(e)))
self.myWindow.Show()
def GetName(self):
return "Tested"
#defind wpf window
class MyWindow:
def load_window (self):
#import window from .xaml file path
file_stream = FileStream(xaml_file_path, FileMode.Open, FileAccess.Read)
window = XamlReader.Load(file_stream)
#bindingdata
bt_Ok = window.FindName("Ok")
bt_Ok.AddHandler(Button.ClickEvent, RoutedEventHandler(self.OK_Click))
self.wpf_window = window
self.event = TestEvent(window)
self.external_event = ExternalEvent.Create(self.event)
window.Show()
def OK_Click(self, sender, e):
self.external_event.Raise()
MyWindow().load_window()
And here is the .xaml file for wpf window
xaml_window.txt (593 Bytes)
SeanP
May 8, 2023, 3:03am
7
With just the parts and peices I am not fully following the program logic, but are you sure your transaction is running from the main event within a valid context that has access to the Document and the imports?
1 Like
yes, that’s the problem I’m having, and I don’t know how to fix it, so I’m looking for help on the group
I updated the new code to show element name, but it still not work
Hi,
seem works correctly in Dynamo and Ipy2,
Which engine do you use in pyRevit ?
I used Ironpython 3.4, python 3.9.13, pyrevit 4.8.12
Looks good in Dynamo with Ipy3 (except “IronPython.Wpf”.dll seem missing in DSIronPython3 package)
maybe it’s a pyRevit bug…
1 Like
exactly, I can’t use the wpf library at the moment either. And it seems that pyRevit v4.8.12 and Ironpython 3.4 are conflicting
FYI, wpf work fine with Revit Python Shell (IPY3)
1 Like