-2

I am trying to convert a C# code (for revit API) to python but to no luck. The C# code looks:

 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); I use pyrevit. can anyone suggest how to do it? thank you!

4
  • Does this answer your question? array filter in python? Commented Jul 29, 2020 at 5:34
  • i will try, but not so sure. i'll reply later Commented Jul 29, 2020 at 15:18
  • You can use list comprehensions in python [x for x in FilteredElementCollector(doc).OfClass(typeof(FamilyInstance)) if x.Host.Id == r.ElementId ] Commented Jul 31, 2020 at 2:33
  • 1
    @EhsanIran-Nejad thank you! this way I could. Commented Aug 5, 2020 at 10:36

1 Answer 1

0

You can conceivably convert C# code into python in places like csharp-to-python or using SharpDevelop Macro Editor.

The conversion will not run because certain classes are constructed differently in Python scripts

If you want to write what you have there in python first need some different declarations. 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:

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. For python (unless i'm mistaken) you would need something like this LINQ in Python.

For transactions usually:

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

Assemble the code and try to run it in RevitPythonShell.

Just remember you can use C# assemblies directly in PyRevit here is a link

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.