首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RevitPythonShell - IronPython.Runtime.UnboundNameException:未定义全局名称“doc”

RevitPythonShell - IronPython.Runtime.UnboundNameException:未定义全局名称“doc”
EN

Stack Overflow用户
提问于 2019-06-25 15:40:54
回答 1查看 824关注 0票数 1

我用RPS写了我的第一个按钮,但是看起来init文件似乎没有在启动时读取,也没有在单击按钮时读取。这是我试图遵循youtube上提供的教程时所遇到的错误。

代码语言:javascript
复制
IronPython.Runtime.UnboundNameException: global name 'doc' is not defined

在复制和粘贴从init脚本导入到我的按钮文件之前,我还收到了过滤元素收集器的类似错误。

是否有人有任何问题或找到任何解决办法?

下面是我的代码中错误的部分:

代码语言:javascript
复制
import os
import csv
import rpw
from rpw.ui.forms import Console
from rpw.ui.forms import SelectFromList

desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
filepath = os.path.join(desktop, 'RevisionClouds.csv')

cl = FilteredElementCollector(doc)
cl.OfCategory(BuiltInCategory.OST_RevisionClouds)
cl.WhereElementIsNotElementType()

我在Revit 2019,RPS运行python 2.7.7

这是我的init.py文件。我增加了几个导入到它,它可以很好地与RPS和它的控制台。但是,当涉及到pyRevit按钮表单时,当它已经在init脚本中定义时,它会在doc上出错。

init.py

代码语言:javascript
复制
# these commands get executed in the current scope
# of each new shell (but not for canned commands)

import clr
import rpw
from rpw import revit, db, ui, DB, UI

clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
clr.AddReferenceByPartialName('PresentationCore')
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName('System')
clr.AddReferenceByPartialName('System.Windows.Forms')

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

from Autodesk.Revit import DB
from Autodesk.Revit import UI


uidoc = __revit__.ActiveUIDocument
doc = __revit__.ActiveUIDocument.Document

from Autodesk.Revit.UI import TaskDialog
from Autodesk.Revit.UI import UIApplication


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


def quit():
    __window__.Close()
exit = quit


def get_selected_elements(doc):
    """API change in Revit 2016 makes old method throw an error"""
    try:
        # Revit 2016
        return [doc.GetElement(id)
                for id in __revit__.ActiveUIDocument.Selection.GetElementIds()]
    except:
        # old method
        return list(__revit__.ActiveUIDocument.Selection.Elements)
selection = get_selected_elements(doc)
# convenience variable for first element in selection
if len(selection):
    s0 = selection[0]

#------------------------------------------------------------------------------
import clr
from Autodesk.Revit.DB import ElementSet, ElementId

class RevitLookup(object):
    def __init__(self, uiApplication):
        '''
        for RevitSnoop to function properly, it needs to be instantiated
        with a reference to the Revit Application object.
        '''
        # find the RevitLookup plugin
        try:
            rlapp = [app for app in uiApplication.LoadedApplications
                     if app.GetType().Namespace == 'RevitLookup'
                     and app.GetType().Name == 'App'][0]
        except IndexError:
            self.RevitLookup = None
            return
        # tell IronPython about the assembly of the RevitLookup plugin
        clr.AddReference(rlapp.GetType().Assembly)
        import RevitLookup
        self.RevitLookup = RevitLookup
        # See note in CollectorExt.cs in the RevitLookup source:
        self.RevitLookup.Snoop.CollectorExts.CollectorExt.m_app = uiApplication
        self.revit = uiApplication

    def lookup(self, element):
        if not self.RevitLookup:
            print 'RevitLookup not installed. Visit https://github.com/jeremytammik/RevitLookup to install.'
            return
        if isinstance(element, int):
            element = self.revit.ActiveUIDocument.Document.GetElement(ElementId(element))
        if isinstance(element, ElementId):
            element = self.revit.ActiveUIDocument.Document.GetElement(element)
        if isinstance(element, list):
            elementSet = ElementSet()
            for e in element:
                elementSet.Insert(e)
            element = elementSet
        form = self.RevitLookup.Snoop.Forms.Objects(element)
        form.ShowDialog()
_revitlookup = RevitLookup(__revit__)
def lookup(element):
    _revitlookup.lookup(element)

#------------------------------------------------------------------------------

# a fix for the __window__.Close() bug introduced with the non-modal console
class WindowWrapper(object):
    def __init__(self, win):
        self.win = win

    def Close(self):
        self.win.Dispatcher.Invoke(lambda *_: self.win.Close())

    def __getattr__(self, name):
        return getattr(self.win, name)
__window__ = WindowWrapper(__window__)

任何和所有的帮助都是感激的!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-26 02:55:15

根据我的经验,“init”文件只适合在动态中使用控制台。当您将脚本部署到按钮(也称为“罐装命令”)时,您需要包含“init”文件中的所有预先定义。

这就解释了为什么当您从控制台运行脚本时,doc工作得很好,但是在部署到丝带按钮时却没有定义它。

这也是init文件的第一行所指的:

代码语言:javascript
复制
# these commands get executed in the current scope
# of each new shell (but not for canned commands)

尝试将所有的importclr.AddReference代码行添加到部署的按钮文件中,看看会发生什么。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56757650

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档