pyrevitlabs / pyRevit

Rapid Application Development (RAD) Environment for Autodesk Revit®
http://wiki.pyrevitlabs.io
GNU General Public License v3.0
1.32k stars 335 forks source link

CPython objects passed to Revit cause halt #692

Closed eirannejad closed 1 year ago

eirannejad commented 5 years ago

Describe the bug CPython engine is unstable when an object that is created in cpython scope is passed to Revit API. Seems like Revit gets stuck when trying to garbage collect the passed object(?!)

The script below, will halt Revit after a couple of executions. The script does not cause any halts when rebars = select_objects() is used. (Uncomment the rebars = select_objects() line and comment out the rebars = select_objects_by_category('Walls') to change the test)

The issue seems to be related to passing an instance of CategoriesFilter to Revit API. Note that this might be related to #686

from pyrevit import EXEC_PARAMS
from pyrevit import revit, UI

class CategoriesFilter(UI.Selection.ISelectionFilter):
    __namespace__ = EXEC_PARAMS.exec_id

    def __init__(self, names):
        self.names = names

    def AllowElement(self, element):
        return element.Category.Name in self.names

    def AllowReference(self, refer, point):
        return False

def select_objects_by_category(*names):
    references = \
        revit.uidoc.Selection.PickObjects(
            UI.Selection.ObjectType.Element,
            CategoriesFilter(names),
            'Pick {}'.format(', '.join(names))
            )
    return [revit.doc.GetElement(reference) for reference in references]

def select_objects():
    references = \
        revit.uidoc.Selection.PickObjects(UI.Selection.ObjectType.Element)
    return [revit.doc.GetElement(reference) for reference in references]

def main():
    # rebars = select_objects()
    rebars = select_objects_by_category('Walls')
    print(rebars)

main()
eirannejad commented 5 years ago

@htlcnn

htlcnn commented 5 years ago

It might be related to an open issue of pythonnet https://github.com/pythonnet/pythonnet/issues/520

htlcnn commented 5 years ago

I ran the script and had this failure even when I'm not using CategoriesFilter. It happened after a few runs.

image OR this: image

pyrevit env:

C:\Users\HTL>pyrevit env
==> Registered Clones (full git repos)
==> Registered Clones (deployed from archive/image)
master | Deploy: "basepublic" | Branch: "master" | Version: "4.7-beta2" | Path: "C:\Users\HTL\AppData\Roaming\pyRevit-Master"
==> Attachments
master | Product: "2018 First Customer Ship" | Engine: 273 | Path: "C:\Users\HTL\AppData\Roaming\pyRevit-Master" | Manifest: "C:\Users\HTL\AppData\Roa
ming\Autodesk\Revit\Addins\2018\pyRevit.addin"
==> Installed Extensions
htl | Type: Unknown | Repo: "git@gitlab.com:hoangthanhlong/pyrevitscripts.git" | Installed: "E:\Setup\UCE\Autodesk\Revit\pyRevit extensions\htl.extens
ion"
==> Default Extension Search Path
C:\Users\HTL\AppData\Roaming\pyRevit\Extensions
==> Extension Search Paths
E:\Setup\UCE\Autodesk\Revit\pyRevit extensions
==> Extension Sources - Default
https://github.com/eirannejad/pyRevit/raw/master/extensions/extensions.json
==> Extension Sources - Additional
==> Installed Revits
2018 First Customer Ship | Version: 18.0.0.420 | Build: 20170223_1515(x64) | Language: 1033 | Path: "C:\Program Files\Autodesk\Revit 2018"
==> Running Revit Instances
PID: 12704 | 2018 First Customer Ship | Version: 18.0.0.420 | Build: 20170223_1515(x64) | Language: 0 | Path: "C:\Program Files\Autodesk\Revit 2018"
==> User Environment
Microsoft Windows 7 [Version 6.1.7601]
Executing User: HTL-PC\HTL
Active User: HTL-PC\HTL
Adming Access: Yes
%APPDATA%: "C:\Users\HTL\AppData\Roaming"
Latest Installed .Net Framework: 4.8
Installed .Net Target Packs: v3.5 v4.0 v4.5 v4.5.1 v4.5.2 v4.6 v4.6.1 v4.7 v4.7.1 v4.7.2 v4.X
Installed .Net-Core Target Packs: v2.2.203 v3.0.100-preview8-013656
pyRevit CLI 0.14.0.0

After this, I tried to reload pyRevit but it seemed to fail because I didn't see any output window. Other pyRevit commands (and other commands in my extension) still worked.

jmcouffin commented 1 year ago

Closing Issue older than a year. Thanks for pointing it out. If you feel it is still relevant, feel free to reopen the issue