Run C# script within pyRevit

Hi, pyRevit-ors)

After checking the forum & docs,

… I am still confused about the possibility to run C# inside pyRevit.
For example, can I run “Getinfo_View” script (ApiDocs.co) without conversion to Python?

Thank you for your attention,

Yes, you can. I was able to run “Getinfo_View” with the following cs file:

using System;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace TBS_Conversion_Tools
{
    [Transaction(TransactionMode.Manual)]
    public class Test : IExternalCommand
    {
        public Result Execute(ExternalCommandData data, ref string message, ElementSet elements)
        {
            // Get Application and Document objects
            UIApplication uiapp = data.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Document doc = uidoc.Document;

            using (Transaction t = new Transaction(doc, "View Info"))
            {
                t.Start();
                Getinfo_View(doc.ActiveView);
                t.Commit();
            }

            return Result.Succeeded;
        }

        private void Getinfo_View(Autodesk.Revit.DB.View view)
        {
            string message = "View: ";

            //get the name of the view
            message += "\nView name: " + view.Name;

            //The crop box sets display bounds of the view
            BoundingBoxXYZ cropBox = view.CropBox;
            XYZ max = cropBox.Max; //Maximum coordinates (upper-right-front corner of the box).
            XYZ min = cropBox.Min; //Minimum coordinates (lower-left-rear corner of the box).
            message += "\nCrop Box: ";
            message += "\nMaximum coordinates: (" + max.X + ", " + max.Y + ", " + max.Z + ")";
            message += "\nMinimum coordinates: (" + min.X + ", " + min.Y + ", " + min.Z + ")";


            //get the origin of the screen
            XYZ origin = view.Origin;
            message += "\nOrigin: (" + origin.X + ", " + origin.Y + ", " + origin.Z + ")";


            //The bounds of the view in paper space (in inches).
            BoundingBoxUV outline = view.Outline;
            UV maxUv = outline.Max; //Maximum coordinates (upper-right corner of the box).
            UV minUv = outline.Min; //Minimum coordinates (lower-left corner of the box).
            message += "\nOutline: ";
            message += "\nMaximum coordinates: (" + maxUv.U + ", " + maxUv.V + ")";
            message += "\nMinimum coordinates: (" + minUv.U + ", " + minUv.V + ")";

            //The direction towards the right side of the screen
            XYZ rightDirection = view.RightDirection;
            message += "\nRight direction: (" + rightDirection.X + ", " +
                           rightDirection.Y + ", " + rightDirection.Z + ")";

            //The direction towards the top of the screen
            XYZ upDirection = view.UpDirection;
            message += "\nUp direction: (" + upDirection.X + ", " +
                           upDirection.Y + ", " + upDirection.Z + ")";

            //The direction towards the viewer
            XYZ viewDirection = view.ViewDirection;
            message += "\nView direction: (" + viewDirection.X + ", " +
                           viewDirection.Y + ", " + viewDirection.Z + ")";

            //The scale of the view
            message += "\nScale: " + view.Scale;
            // Scale is meaningless for Schedules
            if (view.ViewType != ViewType.Schedule)
            {
                int testScale = 5;
                //set the scale of the view
                view.Scale = testScale;
                message += "\nScale after set: " + view.Scale;
            }

            TaskDialog.Show("Revit", message);
        }
    }
}

Follow the typical steps for creating a pushbutton, then create a script.cs instead of a script.py and paste the code inside. You will need a bundle.yaml as well. Here’s the bundle.yaml I used:

title: TestCommand2
tooltip: Test C# Script Tooltip
author: "AGG"
highlight: new
context:
  - doc-project
modules:

You can use these links for more info about the bundle.yaml settings
Bundle Metadata
Bundle Context

2 Likes