Message 1 of 4
Error C# Excuted Python file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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;
}
}