<aside> <img src="https://s3-us-west-2.amazonaws.com/secure.notion-static.com/01f9b8a6-3c36-442b-bed3-ec193007d66f/ic_01_idea.png" alt="https://s3-us-west-2.amazonaws.com/secure.notion-static.com/01f9b8a6-3c36-442b-bed3-ec193007d66f/ic_01_idea.png" width="40px" /> Start with the short introduction to scripts at Create Your First Command and Create Your First .NET Command

</aside>

<aside> <img src="https://s3-us-west-2.amazonaws.com/secure.notion-static.com/844cd1c9-d253-44c5-9200-dc026df980f5/ic_01_idea.png" alt="https://s3-us-west-2.amazonaws.com/secure.notion-static.com/844cd1c9-d253-44c5-9200-dc026df980f5/ic_01_idea.png" width="40px" /> See .NET Script Facilities for information about the script support module

</aside>

Accessing Script Data


pyRevit provides access to some data about the .NET script being executed. To gain access to this data, import the PyRevitLabs.PyRevit.Runtime in your script, and define a public field on your command class with the type ExecParams

At execution time, pyRevit will fill this field. The ExecParams is defined as

public class ExecParams {
    public string ScriptPath { get; set; }            // path to command script
    public string ConfigScriptPath { get; set; }      // path to command config script
    public string CommandUniqueId { get; set; }       // command unique id
    public string CommandName { get; set; }           // command name
    public string CommandBundle { get; set; }         // command bundle full name with type
    public string CommandExtension { get; set; }      // command extention name
    public string HelpSource { get; set; }            // command help source
    public bool RefreshEngine { get; set; }           // if a new engine was requested thought UI
    public bool ConfigMode { get; set; }              // if command is in config mode
    public bool DebugMode { get; set; }               // if command is in debug mode
    public bool ExecutedFromUI { get; set; }          // if command has been executed from UI
}

Here is an example of accessing this data in C# and VB.NET

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

using PyRevitLabs.PyRevit.Runtime;

namespace HelloWorld
{
   public class MyCSharpCommand : IExternalCommand
   {
      // define the execparams field
      public ExecParams execParams;

      public Result Execute(ExternalCommandData revit, ref string message, ElementSet elements)
      {
         Console.WriteLine(execParams.ScriptPath); 
         return Result.Succeeded;
      }
   }
}
Imports System
Imports Microsoft.VisualBasic

Imports Autodesk.Revit.UI
Imports Autodesk.Revit.DB

Imports PyRevitLabs.PyRevit.Runtime

Public Class MyVBCommand
    Implements IExternalCommand

    ' define the execparams field
    Public execParams As ExecParams

    Public Function Execute(ByVal revit As ExternalCommandData, _
                            ByRef message As String, _
                            ByVal elements As ElementSet) As Autodesk.Revit.UI.Result _
                                Implements IExternalCommand.Execute
            Console.WriteLine(execParams.ScriptPath)
            Return Autodesk.Revit.UI.Result.Succeeded
    End Function
End Class

Accessing Logger


pyRevit provides access to an internal logger instance to help the script log data to the output window. To gain access to this data, import the pyRevitLabs.NLog in your script, and define a field on your command class with the type Logger

Here is an example of accessing this logger in C# and VB.NET

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

using pyRevitLabs.NLog;

namespace HelloWorld
{
   public class MyCSharpCommand : IExternalCommand
   {
      // define the logger field
      private Logger logger = LogManager.GetCurrentClassLogger();

      public Result Execute(ExternalCommandData revit, ref string message, ElementSet elements)
      {
         logger.Info("Logger works...");
         logger.Debug("Logger works...");
         return Result.Succeeded;
      }
   }
}
Imports System
Imports Microsoft.VisualBasic

Imports Autodesk.Revit.UI
Imports Autodesk.Revit.DB

Imports pyRevitLabs.NLog

Public Class MyVBCommand
    Implements IExternalCommand

    ' define the logger field
    Private logger As Logger = LogManager.GetCurrentClassLogger()

    Public Function Execute(ByVal revit As ExternalCommandData, _
                            ByRef message As String, _
                            ByVal elements As ElementSet) As Autodesk.Revit.UI.Result _
                                Implements IExternalCommand.Execute
            logger.Info("Logger works...")
            logger.Debug("Logger works...")
            Return Autodesk.Revit.UI.Result.Succeeded
    End Function
End Class