C# to Python

Does anyone have a Rosetta Stone for converting C# to Python? or suggest a replacement node? It duplicates views based on template and level.

using Revit.Elements;
using RevitServices.Persistence;
using RevitServices.Transactions;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

public static class ViewTemplates
{
    /// <summary>
    /// Templates should start with 'Temp-' and end with a level number.
    /// this script duplicates multiple views across multiple levels.
    /// </summary>
    /// <param name="TemplatesAndLevelNumbers">Should be a list of lists like this, [ [template list], ["Level 01", "Level 02"] ]</param>
    /// <returns name="view">List of View templates</returns>
    public static List<Revit.Elements.Views.View> TDDuplicateMultiple(List<List<object>> TemplatesAndLevelNumbers)
    {
        var viewTemplates = TemplatesAndLevelNumbers[0];
        var levelNumbers = TemplatesAndLevelNumbers[1];

        Regex rx = new Regex(@"[0-9]+",
          RegexOptions.Compiled | RegexOptions.IgnoreCase);

        var doc = DocumentManager.Instance.CurrentDBDocument;
        List<Revit.Elements.Views.View> outputList = new List<Revit.Elements.Views.View> { };

        foreach (Revit.Elements.Views.View viewTemplate in viewTemplates)
        {
            foreach (string levelNumber in levelNumbers)
            {
                TransactionManager.Instance.ForceCloseTransaction();
                TransactionManager.Instance.EnsureInTransaction(doc);
                string levelNum = rx.Match(levelNumber).Value;
                var name = viewTemplate.Name.Replace("XX", levelNum).Replace("Temp-", "");

                var v = viewTemplate.InternalElement as Autodesk.Revit.DB.View;
                var newId = Autodesk.Revit.DB.ElementTransformUtils.CopyElement(doc, v.Id, Autodesk.Revit.DB.XYZ.Zero);
                Autodesk.Revit.DB.View newView = (Autodesk.Revit.DB.View)doc.GetElement(newId.FirstOrDefault());
                newView.Name = name;
                TransactionManager.Instance.TransactionTaskDone();

                var outputView = newView.ToDSType(true) as Revit.Elements.Views.View;
                outputView.SetParameterByName("View Group", levelNumber);

                outputList.Add(outputView);
            }
        }
        return outputList;
    }
}
1 Like

Hey,

Unfortunately not… Though it is a useful learning experience :slight_smile:

Given how fragile coding is, it would need to be a rosetta stone exactly for C# for Dynamo and Revit…

The example you’ve given isn’t too complex though, I would make it as a graph using a lot of Archi-Lab nodes (thanks Konrad)…

Then when that was working, I might ‘node to code’ to compress it and give it a tidy up…

Hopefully that helps,

Mark

1 Like