简体   繁体   English

如何在 Revit API 中将 C# code.where(…) 转换为 python

[英]how to convert from C# code.where(…) to python in Revit API

I am trying to convert a C# code (for revit API) to python but to no luck.我正在尝试将 C# 代码(用于 revit API)转换为 python 但没有运气。 The C# code looks: C# 代码看起来:

 public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        Document doc = commandData.Application.ActiveUIDocument.Document;

        Reference r = commandData.Application.ActiveUIDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, "please select wall");

        IEnumerable<Element> associate = new FilteredElementCollector(doc).OfClass(typeof(FamilyInstance)).Where(m=>(m as FamilyInstance).Host.Id  == r.ElementId);

        return Result.Succeeded;
    }

what I am having problem with is the part .Where(m=>(m as FamilyInstance).Host.Id == r.ElementId);我遇到的问题是部分.Where(m=>(m as FamilyInstance).Host.Id == r.ElementId); I use pyrevit.我用pyrevit。 can anyone suggest how to do it?谁能建议怎么做? thank you!谢谢你!

You can conceivably convert C# code into python in places likecsharp-to-python or using SharpDevelop Macro Editor .您可以在csharp-to-python或使用SharpDevelop Macro Editor之类的地方将 C# 代码转换为 python 。

The conversion will not run because certain classes are constructed differently in Python scripts转换不会运行,因为某些类在 Python 脚本中的构造不同

If you want to write what you have there in python first need some different declarations.如果你想写你在 python 中的内容,首先需要一些不同的声明。 Usings example:使用示例:

import clr
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

Then Document & UIApp as an example need to be created using Document Manager:然后需要使用 Document Manager 创建 Document & UIApp 作为示例:

app = DocumentManager.Instance.CurrentUIApplication
doc = DocumentManager.Instance.CurrentDBDocument

For Current Selection this article should do the trick对于当前选择,这篇文章应该可以解决问题

For the portion of the code you have a problem with:对于您遇到问题的代码部分:

IEnumerable<Element> associate = new FilteredElementCollector(doc).
OfClass(typeof(FamilyInstance)).
Where(m=>(m as FamilyInstance).Host.Id  == r.ElementId);

Remember you are using LINQ.请记住,您使用的是 LINQ。 For python (unless i'm mistaken) you would need something like this LINQ in Python .对于 python (除非我弄错了),您将需要Python 中的 LINQ 之类的东西。

For transactions usually:对于交易通常:

TransactionManager.Instance.EnsureInTransaction(doc)  
  {Do Something}
TransactionManager.Instance.TransactionTaskDone()

Assemble the code and try to run it in RevitPythonShell.组装代码并尝试在 RevitPythonShell 中运行它。

Just remember you can use C# assemblies directly in PyRevit here is a link请记住,您可以直接在 PyRevit 中使用 C# 程序集,这是一个链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM