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: 

C# and IronPython Hosting for Revit addins

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
daniel.gercak
1315 Views, 7 Replies

C# and IronPython Hosting for Revit addins

I try to run IronPython scripts via IronPython.Hosting in C# inspired by Nick Cosentino  in blog “Python, Visual Studio, and C#… So. Sweet

The purpose of such effort is to create easy distributable and accessible addins in our company which are developed by small group of non IT experts on IronPython platform due to it’s flexibility and lower demandings for coding skills. Yes I’have heard about RevitPythonShell, but this is not exactly what would make sufficient result.

I was able to run classes from references RevitAPI and RevitAPIU but when it came across RevitServices >:

from RevitServices.Persistence import DocumentManager

doc = DocumentManager.Instance.CurrentDBDocument

uiapp = DocumentManager.Instance.CurrentUIApplication

uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

 

>the result are empty objects

 

I wonder if this intention to run such scripts is useless effort due to some restrictions in source code or there is some way how to deal with this issue.

Any idea would be valuable.

 

Thanks,

 

Daniel Gerčák

 

I’m adding piece of code for better overview.

Labels (3)
7 REPLIES 7
Message 2 of 8

Dear Daniel,

 

As suggested in our prior personal email correspondence, you can take a look at the open source implementations of RevitPythonShell and pyRevit to discover how they have addressed this same issue.

 

Good luck solving your task, and please let us know how you do so. Thank you!

 

Best regards,

 

Jeremy

  

Jeremy Tammik, Developer Advocacy and Support, The Building Coder, Autodesk Developer Network, ADN Open
Message 3 of 8

Dear Jerremy,

 

after you advised me to look at RevitPythonShell source code I did a little research and I was succesfull.

The point was to create a new variable within IronPython engine with assigned commandData.Application object to it so that you could access to it from python environment. I named it the same way as it is in RPS "__revit__" in order to keep compatibility with scripts developed for RPS. Since I've tested it only by using a simple python code, I don't guarantee it's functionality with more complex scripts. Additional settings might be required.

The execute function looks like this.

 

public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
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__");
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("totalSelectedVolume.py");
}
catch (Exception ex)
{
TaskDialog myDialog = new TaskDialog("IronPython Error");
myDialog.MainInstruction = "Couldn't execute IronPython script totalSelectedVolume.py: ";
myDialog.ExpandedContent = ex.Message;
myDialog.Show();
}

return Result.Succeeded;

 

whole testing project as IExternalCommand see in attachment.

 

 

Message 4 of 8

Congratulations on solving this and glad I was able to help!

 

Happy weekend!

  

Jeremy Tammik, Developer Advocacy and Support, The Building Coder, Autodesk Developer Network, ADN Open
Message 5 of 8
george1985
in reply to: daniel.gercak

Hi @daniel.gercak ,
Thank you very much for sharing the source code and the files!
Do you mind if I ask, how can I compile the attached files? I need to open the IronPython_engine.csproj file in Visual Studio (by double clicking it), and then?
Do I need to have Python Tools for Visual Studio installed first? Anything else?

I would be grateful for any kind reply from you. Děkuji.

Message 6 of 8
jeremy_tammik
in reply to: george1985

You have two main simple options:

  

  

Jeremy Tammik, Developer Advocacy and Support, The Building Coder, Autodesk Developer Network, ADN Open
Message 7 of 8
george1985
in reply to: jeremy_tammik

Hi @jeremy_tammik ,
Thank you for the reply.
I not qualified enough to understand how to implement option 1, or modify option 2.
What makes @daniel.gercak  solution fantastic is that it basically enables making an actual revit addon button which by clicking on it, directly calls a .py file. Unlike RevitPythonShell, which requires user to open the .py file, and then run it.

Message 8 of 8
guru_s3LAHB
in reply to: daniel.gercak

Hey Hi, is it possible to run the code without clicking the addin from addin >>External Tools >> Particular_addin, after opening the revit file?

 

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community