Hi PyRevit team
Are there any instructions on how to port a plugin developed in C# as a plugin to PyRevit? I am particularly confused about where to place external references that I’m using in my C# plugin and how to load them in PyRevit.
Thanks.
Hi PyRevit team
Are there any instructions on how to port a plugin developed in C# as a plugin to PyRevit? I am particularly confused about where to place external references that I’m using in my C# plugin and how to load them in PyRevit.
Thanks.
Hi @dadandroid ,
May I ask you why do you want to do that?
I see pyRevit as a tool for rapid prototyping the functionality you want; if you already have a proper revit plugin, why bother moving it to pyrevit?
Anyway, i think the answer your looking for is here: place your dll dependencies inside a bin
folder of your panel/tab.
@sanzoghenzo thanks for the answer. I need that mainly to integrate it with other tools that have been developed with pyRevit in python. So no way out of that.
Do you know if there is a way to load assemblies dynamically from code?
Oh, of course the invokebutton is the way!
My solution refers to using third party dependencies inside pyrevit scripts (I assumed this was the question and you already knew about invokebuttons )
Happy coding!
@sanzoghenzo @revitislife I’m still running some issues when loading a .dll from PyRevit.
Say - for the sake of simplicity- I want to just parse a json file using Newtonsoft.Json.dll
.
As you can see, I’ve placed the .dll
in the bin folder, on the parent of the button:
Here is what script.cs
looks like (I’ve tested it and works well on a simple console app):
using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Newtonsoft.Json.Linq;
namespace HelloWorld {
public class Test2 : IExternalCommand {
public Result Execute(ExternalCommandData revit,
ref string message, ElementSet elements) {
string js = @"
{
'ParentGuid': '16daadd7-0366-4739-a044-a736be12a6ea-007621df',
'SelfGuid': '6efbef1d-de53-4dea-960b-c79a57a0c74b-0085bcd5',
'SelfTypeId': 8054113,
'TimeStamp': 1676966647.3828125
}";
JObject jsonObj = JObject.Parse(js);
JToken parent = jsonObj["ParentGuid"];
Console.WriteLine(parent.ToString());
return Result.Succeeded;
}
}
}
And the error in the PyRevit console when I run the button:
C# Traceback:
Define: REVIT2022;REVIT2022_1_3
Reference: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.dll
Reference: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\WindowsFormsIntegration\v4.0_4.0.0.0__31bf3856ad364e35\WindowsFormsIntegration.dll
Reference: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xaml\v4.0_4.0.0.0__b77a5c561934e089\System.Xaml.dll
Reference: C:\Users\User\AppData\Roaming\pyRevit-Master\bin\pyRevitLabs.PyRevit.dll
Reference: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll
Reference: C:\Users\User\AppData\Roaming\pyRevit-Master\bin\pyRevitLabs.NLog.dll
Reference: C:\Program Files\Autodesk\Revit 2022\RevitAPIUI.dll
Reference: C:\Program Files\Autodesk\Revit 2022\RevitAPI.dll
Reference: C:\Users\User\AppData\Roaming\pyRevit-Master\bin\pyRevitLabs.Emojis.dll
Reference: C:\Users\User\AppData\Roaming\pyRevit-Master\bin\engines\IPY2710\IronPython.dll
Reference: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll
Reference: C:\Users\User\AppData\Roaming\pyRevit-Master\bin\engines\IPY2710\pyRevitLoader.dll
Reference: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll
Reference: C:\Program Files\Autodesk\Revit 2022\UIFramework.dll
Reference: C:\WINDOWS\Microsoft.NET\Framework64\v4.0.30319\System.Web.dll
Reference: C:\Users\User\AppData\Roaming\pyRevit-Master\bin\pyRevitLabs.MahAppsMetro.dll
Reference: C:\Users\User\AppData\Roaming\pyRevit-Master\bin\engines\IPY2710\Microsoft.Scripting.dll
Reference: C:\Program Files\Autodesk\Revit 2022\AdWindows.dll
Reference: C:\Users\User\AppData\Roaming\pyRevit-Master\bin\pyRevitLabs.Json.dll
Reference: C:\Users\User\AppData\Roaming\pyRevit-Master\bin\engines\IPY2710\Microsoft.Dynamic.dll
Reference: C:\Users\User\AppData\Roaming\pyRevit-Master\bin\pyRevitLabs.CommonWPF.dll
Reference: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Numerics\v4.0_4.0.0.0__b77a5c561934e089\System.Numerics.dll
Reference: C:\Program Files\Autodesk\Revit 2022\Xceed.Wpf.AvalonDock.dll
Reference: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework\v4.0_4.0.0.0__31bf3856ad364e35\PresentationFramework.dll
Reference: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\WindowsBase\v4.0_4.0.0.0__31bf3856ad364e35\WindowsBase.dll
Reference: C:\Users\User\AppData\Roaming\pyRevit-Master\bin\engines\IPY2710\IronPython.Modules.dll
Reference: C:\Users\User\AppData\Roaming\pyRevit-Master\bin\pyRevitLabs.TargetApps.Revit.dll
Reference: C:\Users\User\AppData\Roaming\pyRevit-Master\bin\pyRevitLabs.Common.dll
Reference: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll
Reference: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll
Reference: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.CSharp\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.CSharp.dll
Reference: C:\Users\User\AppData\Roaming\pyRevit-Master\bin\engines\CPY385\pyRevitLabs.PythonNet.dll
Reference: C:\WINDOWS\Microsoft.Net\assembly\GAC_64\PresentationCore\v4.0_4.0.0.0__31bf3856ad364e35\PresentationCore.dll
Reference: C:\Users\User\AppData\Roaming\pyRevit\2022\pyRevit_2022_478ffab196b92918_PyRevitLabs.PyRevit.Runtime.dll
C:\Users\User\Documents\MyExtensions\MyFirstExtension.extension\MyTools.tab\test.panel\SharpTest.pushbutton\script.cs(4,7): error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)
C:\Users\User\Documents\MyExtensions\MyFirstExtension.extension\MyTools.tab\test.panel\SharpTest.pushbutton\script.cs(24,10): error CS0246: The type or namespace name 'JObject' could not be found (are you missing a using directive or an assembly reference?)
C:\Users\User\Documents\MyExtensions\MyFirstExtension.extension\MyTools.tab\test.panel\SharpTest.pushbutton\script.cs(24,28): error CS0103: The name 'JObject' does not exist in the current context
C:\Users\User\Documents\MyExtensions\MyFirstExtension.extension\MyTools.tab\test.panel\SharpTest.pushbutton\script.cs(26,10): error CS0246: The type or namespace name 'JToken' could not be found (are you missing a using directive or an assembly reference?)
What am I doing wrong?? Do I need to load the assembly in any specific way? I appreciate your help.
Hi @dadandroid,
I’m sorry you still have problems with that.
To be honest, this is an uncharted territory for me, I use pyRevit just for revit script and create proper revit addins if I need “more power”.
The suggestion I gave you was based on the documentation, but after trying to read the source code it could that the bin directory is not detected…
Stupid question: have you reloaded pyRevit (or the entire revit app) after creating the bin folder?
I did tried reloading it, and tried restarting Revit. Doesn’t work
I doubt it but I am not skilled enough in C# and the like to help you on this.
check this bundle file in the dev tools extension
and also the CS file. I t might help you
what I generally do is compile the c# command addin as a DLL once compiles I then use the invokebutton to call\invoke the desired class. in my experience that has guaranteed the most consistent results