IronPython和Revit API如何在列表框(属性名称)中显示项属性?

2024-09-26 18:03:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一名建筑师,他逐渐喜欢通过Revit编写代码。但不幸的是,由于我仍然是一个绝顶聪明的人,我需要任何愿意加入的人的帮助。我也是一个无名氏,所以我不知道在社区里发布这样的问题是不是可以,也不例外,这些问题更多的是辅导问题,而不是解决问题。但不管怎样,事情是这样的: 我正在尝试创建一个能够同时从Revit族编辑器中删除多个参数的应用程序。我在C#方面已经取得了成功,但是由于作为初学者更容易入门,我想转到Python,所以我做了很多浏览工作,但由于OOP知识有限,没有任何帮助。 如何在列表框中显示族参数名称,或者如果列表框中有字符串名称,如何将所选项目与FamilyParameterSet中的实际参数进行比较?你知道吗

我有一个开始代码,我可以收集所有参数从家庭经理。我把它列入名单。然后一个选项是使用要在listbox中列出的参数的Name属性,但我不知道如何返回并签出列表,也不知道如何循环列表以将参数集中的名称与从listbox中选择的名称进行比较。所以我选择了另一个选项,直接将族参数放在列表框中,但是我无法显示实际的Name属性。 这段C代码可能对我有所帮助,但我不知道如何用Python重新创建它,因为我的OOP经验实在太差了。 Items on ListBox show up as a class name

doc = __revit__.ActiveUIDocument.Document               
mgr = doc.FamilyManager;                                
fps = mgr.Parameters;                                  

paramsList=list(fps)                                   
senderlist =[]                                          

class IForm(Form):

    def __init__(self):
        self.Text = "Remove multiple parameters"

        lb = ListBox()                                  
        lb.SelectionMode = SelectionMode.MultiSimple   

        for n in fps:                                   
        lb.Items.Add(n.Definition.Name)


        lb.Dock = DockStyle.Fill
        lb.SelectedIndexChanged += self.OnChanged       


        self.Size = Size(400, 400)                      
        self.CenterToScreen()                           

        button = Button()                               
        button.Text = "Delete Parameters"              
        button.Dock = DockStyle.Bottom                  
        button.Click += self.RemoveParameter                  
        self.Controls.Add(button)  

    def OnChanged(self, sender, event):
        senderlist.append(sender.SelectedItem)                        

    def RemoveParameter(self,sender,event):       
        for i in paramsList:
            if i.Definition.Name in senderlist:
            t = Transaction(doc, 'This is my new transaction')
            t.Start()  
            mgr.RemoveParameter(i.Id)
            t.Commit()
Application.Run(IForm())

我需要函数RemoveParameter具有所有的.Id比例的族参数,以便从族参数集中删除它们。在代码的开头(对于不了解Revit API的人)“fps”表示FamilyParameterSet,它被转换为Python列表“paramsList”。因此我需要从列表框中选择的项中删除fps的成员。你知道吗


Tags: 代码nameself名称列表参数docbutton
1条回答
网友
1楼 · 发布于 2024-09-26 18:03:38

您从Revit到代码的旅程非常熟悉!你知道吗

首先,看起来您的代码中有一些C#遗留问题。您需要记住从C#到Python有几个关键的转变—在您的示例中,有两行需要缩进,正如运行代码时的错误所示:

Syntax Error: expected an indented block (line 17)
Syntax Error: expected an indented block (line 39)

经验法则是在冒号:之后需要缩进,这也使得代码更具可读性。还有一些分号;在前几行-不需要它们!你知道吗

否则代码就差不多了,我在下面的代码中添加了注释:

# the Winforms library first needs to be referenced with clr
import clr
clr.AddReference("System.Windows.Forms")

# Then all the Winforms components youre using need to be imported
from System.Windows.Forms import Application, Form, ListBox, Label, Button, SelectionMode, DockStyle

doc = __revit__.ActiveUIDocument.Document               
mgr = doc.FamilyManager                         
fps = mgr.Parameters                          

paramsList=list(fps)                                   
# senderlist = [] moved this into the Form object - welcome to OOP!                                        

class IForm(Form):

    def __init__(self):
        self.Text = "Remove multiple parameters"

        self.senderList = []

        lb = ListBox()                                  
        lb.SelectionMode = SelectionMode.MultiSimple   

        lb.Parent = self # this essentially 'docks' the ListBox to the Form

        for n in fps:                                   
            lb.Items.Add(n.Definition.Name)

        lb.Dock = DockStyle.Fill
        lb.SelectedIndexChanged += self.OnChanged       

        self.Size = Size(400, 400)                      
        self.CenterToScreen()                           

        button = Button()                               
        button.Text = "Delete Parameters"              
        button.Dock = DockStyle.Bottom                  
        button.Click += self.RemoveParameter                  
        self.Controls.Add(button)  

    def OnChanged(self, sender, event):
        self.senderList.append(sender.SelectedItem)                        

    def RemoveParameter(self,sender,event):       
        for i in paramsList:
            if i.Definition.Name in self.senderList:
                t = Transaction(doc, 'This is my new transaction')
                t.Start()

                # wrap everything inside Transactions in 'try-except' blocks
                # to avoid code breaking without closing the transaction
                # and feedback any errors
                try:
                    name = str(i.Definition.Name) 
                    mgr.RemoveParameter(i) # only need to supply the Parameter (not the Id)
                    print 'Parameter deleted:',name # feedback to the user
                except Exception as e:
                    print '!Failed to delete Parameter:',e

                t.Commit()
                self.senderList = [] # clear the list, so you can re-populate 

Application.Run(IForm())

从这里开始,额外的功能只是为用户润色一下:

  • 添加一个弹出对话框,让他们知道成功/失败
  • 一旦用户删除了参数,刷新ListBox
  • 过滤掉无法删除的BuiltIn参数(尝试删除一个参数并查看它抛出的错误)

让我知道这是怎么回事!你知道吗

相关问题 更多 >

    热门问题