Hi community,
I just started learning Speckle, so I hope you can point me in the right direction.
I am a C# developer, and I am currently working on creating an add-in for Revit (2024).
My goal is to send particular elements (or the whole current document) to a Speckle Server/stream. This works fine when I use the User interface of the provided Revit connector.
Now I would like to send elements not using the UI of the connector, but doing this programmatically inside of a Revit add-in (using C#).
Revit allows to access most of their stuff (elements, current project, linked documents, etc…) using the RevitAPI. So I was thinking I could create a new Revit converter kit instance inside the add-in codee, and then use this to convert the Revit elements into Speckle objects.
Here is what I got so far:
//Create a revit converter kit instance
var kit = KitManager.GetDefaultKit();
var converter = kit.LoadConverter(Objects.Converter.Revit.ConverterRevit.RevitAppName);
converter.SetContextDocument(revit_doc);
//Example for converting a particular element - fails with a
//NullReferenceException - the private variable revitDocumentAggregateCache
//is null inside ConverterRevit
var collector = new FilteredElementCollector(Project.MainDocument);
var rev_el = collector.OfCategory(BuiltInCategory.OST_MEPSpaces)
.FirstOrDefault();
var sp_el = converter.ConvertToSpeckle(rev_el);
//Example for the whole document - the conversion result does not contain any elements
var speckle_doc = converter.ConvertToSpeckle(revit_doc);
//Finally, send the document or the element to Speckle
Helpers.Send("https://speckle.xyz/streams/1225379f6a", speckle_doc);
//Helpers.Send("https://speckle.xyz/streams/1225379f6a", sp_el);
I have two issues here. When I try to convert the whole Autodesk.Revit.DB.Document into a Speckle object, I don’t get an exception, but also the document does not contain any elements.
When I try to convert a particular element (in this case a MEPSpace, but I get the same issue for a Room or a Wall etc.), I get a NullReferenceException.
I tried to debug the issue in the Speckle connector source code - it looks like the ConverterRevit class has a private variable called revitDocumentAggregateCache, which is null in my case.
Judging on the implementation of the current Revit connector (which is thankfully open source), it looks like that variable is somehow injected when the UI is created. I must have missed something here.
So my question is basically: Is there a way to programmatically convert Revit objects (or the whole document) into Speckle objects inside a Revit plugin?
Thanks for your support!