Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Error C# Excuted Python file.

3 REPLIES 3
Reply
Message 1 of 4
manhgt214
236 Views, 3 Replies

Error C# Excuted Python file.

Hi everyone!
I tried using C# run python file but when click to button on ribbon Revit show this error:

"Revit file or Assembly Microsoft.Scripting, Version = 1.3.4.0,, Culture = neutral, PuplicKeyToken = 7f709c5b713576e1, or can not read file. File path not find...

But that error will not show if I click another botton created Dynamo file (.dyn) first then comback click it.

1. The code run Python file by below:

public class Check : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            // Define the file path to your Python script
            string pythonScriptFilePath = @"C:\Test\Revit\Tool\script.py";

            UIApplication _revit = commandData.Application;
            UIDocument uidoc = _revit.ActiveUIDocument;
            Application app = _revit.Application;
            Document doc = uidoc.Document;

            var flags = new Dictionary<string, object>() { { "Frames", true }, { "FullFrames", true } };

            var py = IronPython.Hosting.Python.CreateEngine(flags);
            var scope = IronPython.Hosting.Python.CreateModule(py, "__main__");

            ScriptRuntime runtime = IronPython.Hosting.Python.CreateRuntime();
            ScriptEngine engine = runtime.GetEngine("py");

            scope.SetVariable("__commandData__", commandData);

            // Add special variable: __revit__ to be globally visible everywhere:
            var builtin = IronPython.Hosting.Python.GetBuiltinModule(py);
            builtin.SetVariable("__revit__", _revit);
            py.Runtime.LoadAssembly(typeof(Autodesk.Revit.DB.Document).Assembly);
            py.Runtime.LoadAssembly(typeof(Autodesk.Revit.UI.TaskDialog).Assembly);

            try
            {
                py.ExecuteFile(pythonScriptFilePath);
            }
            catch (Exception ex)
            {
                TaskDialog myDialog = new TaskDialog("IronPython Error");
                myDialog.MainInstruction = $"Couldn't execute IronPython script {pythonScriptFilePath}: ";
                myDialog.ExpandedContent = ex.Message;
                myDialog.Show();
            }

            return Result.Succeeded;
        }
    }
3 REPLIES 3
Message 2 of 4
jeremy_tammik
in reply to: manhgt214

So, it seems that a required assembly cannot be found and loaded. You can either load it yourself specifically up front or implement an assembly resolver to specify more exactly where it can be found in case of need. Since clicking the other button first helps, it obviously  exists somewhere.

   

Jeremy Tammik, Developer Advocacy and Support, The Building Coder, Autodesk Developer Network, ADN Open
Message 3 of 4
manhgt214
in reply to: jeremy_tammik

thank you Sir, but implement dynamo seem like same by run path.

 

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

string Dynamo_Script_Path = @"C:\Revit\Tool\script.dyn";
DynamoRevit dynamoRevit = new DynamoRevit();

DynamoRevitCommandData dynamoRevitCommandData = new DynamoRevitCommandData();
dynamoRevitCommandData.Application = uiapp;
IDictionary<string, string> journalData = new Dictionary<string, string>
{
{ Dynamo.Applications.JournalKeys.ShowUiKey, false.ToString() }, // don't show DynamoUI at runtime (run in background)
{ Dynamo.Applications.JournalKeys.AutomationModeKey, false.ToString() }, //run journal automatically (tested: trus is not run)
{ Dynamo.Applications.JournalKeys.DynPathKey, Dynamo_Script_Path }, //run node at this file path
{ Dynamo.Applications.JournalKeys.DynPathExecuteKey, true.ToString() },
// The journal file can specify if the Dynamo workspace opened from DynPathKey will be executed or not.
//If we are in automation mode the workspace will be executed regardless of this key.
{ Dynamo.Applications.JournalKeys.ForceManualRunKey, true.ToString() }, //set true to run in manual mode
{ Dynamo.Applications.JournalKeys.ModelShutDownKey, false.ToString() }, //increase speed when click to button on ribbon
{ Dynamo.Applications.JournalKeys.ModelNodesInfo, false.ToString() }
};

dynamoRevitCommandData.JournalData = journalData;
Result externalCommandResult = dynamoRevit.ExecuteCommand(dynamoRevitCommandData);
return externalCommandResult;
}
Message 4 of 4
mhannonQ65N2
in reply to: manhgt214

In Revit 2021 and 2022, Dynamo uses Microsoft.Scripting version 1.2.2.1000. You should probably reference the same version in your add-in if you use one of those versions. If it only works if you run Dynamo first, then you need to load the assembly yourself, maybe with an assembly resolver or maybe just place it in the same directory as your add-in. If you load a different version of the assembly than what Dynamo uses, it could cause problems for Dynamo.

 

In 2023 and 2024, Dynamo does not use Microsoft.Scripting. For these versions, you should probably try to use the libraries that Dynamo uses though you might be able to use Microsoft.Scripting if you include it with your add-in.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Customer Advisory Groups


Rail Community