Missing Elements (Use "ConvertToSpeckle" to convert Revit selected elements to Speckle)

Hi all,

As topic stated, the uploaded commit only has BIM document info, while it doesn’t contain any of element. It seems my way to use “ConvertToSpeckle” doesn’t include element properly. I keen to understand how to properly use ConvertToSpeckle(Element). Thanks!

var kit = KitManager.GetDefaultKit();
var converter = kit.LoadConverter(HostApplications.Revit.GetVersion(HostAppVersion.v2022));

        converter.SetContextDocument(doc);
        var settings = new Dictionary<string, string>();
        converter.SetConverterSettings(settings);

        List<ApplicationObject> ApplicationObject = new List<ApplicationObject>();
        foreach (ElementId ei in selectedIds)
        {
            Element element = uidoc.Document.GetElement(ei);
            ApplicationObject.Add(new ApplicationObject(element.UniqueId, element.GetType().ToString()));
        }
        converter.SetContextObjects(ApplicationObject);
        var commitObject = converter.ConvertToSpeckle(doc) ?? new Collection();


        string url_ = "https://speckle.xyz/";
        Account account = new Account();
        account.token = token;
        account.serverInfo = new ServerInfo
        {
            url = url_
        };
        var transports = new List<Speckle.Core.Transports.ITransport>() { new ServerTransport(account, "58f0896602") };

        var objectId = await Operations.Send(
            @object: commitObject,
            transports: transports,
            disposeTransports: true
            ).ConfigureAwait(true);

        var actualCommit = new CommitCreateInput()
        {
            streamId = streamid,
            objectId = objectId,
            branchName = branchname,
            message = message,
            sourceApplication = "CoreKit",
        };

        var client = new Client(account);

        var response = await client.CommitCreate(actualCommit);
1 Like

Hey @archilles0203 ,

I’m less familiar with our logic nowadays, @Jedd or @connor should be able to provide you with more info. But essentially, you can have a look at what our connector does here:

The commitObject is expanded to include additional objects beyond the document itself.

You probably don’t need a to use the full commitObjectBuilder.BuildCommitObject but as long as you convert each element that you want to send using converter.ConvertToSpeckle and include it inside a Base object, you should be able to then see them online.

Thanks, @teocomi , @Jedd , and @connor. My core issue is how I can put a Revit element into “ConvertToSpeckle” as a Base so that send a commit object to Speckle. I still cannot get how “ConvertToSpeckle” works for Revit element convert. Appreciate.
Albert

Hi, just lock into the issue.
The error I got is “object reference not set to an instance of an object” when I use ConvertToSPeckle.

if (converter.CanConvertToSpeckle(element))
                {
                    Base result = converter.ConvertToSpeckle(element);

                }

“converter.ConvertToSpeckle(element)” cause that error, while I confirm converter is object and referred from default speckle converter for Revit.

Cheers,
Albert

Can you debug and find which object is null?
Maybe it’s the element variable, can you paste a screenshot from VS while you’re inspecting it during debugging?

HI @teocomi ,

My issue is identical to Sending data from Revit to Speckle programmatically

var kit = KitManager.GetDefaultKit();
var converter = kit.LoadConverter(Objects.Converter.Revit.ConverterRevit.RevitAppName);
converter.ConvertToSpeckle(element);

It seems the issue is due to the “converter” object

Thanks and cheers,
Albert

1 Like

@archilles0203, let’s take a moment to look closely at how our RevitConnector/RevitConverter works. Understanding this will help us solve the problem you’re facing.

For our converter to do its job correctly, it needs a bit of preparation. This involves a specific way of starting it up, which is essential for avoiding the errors you’ve encountered. Our Connector/Converter code explains the process of preparing the converter with SetContextDocument, using different pieces of information it needs to start. It’s a crucial step to make sure everything runs smoothly.

You can see the steps for this setup in detail here:

In particular, initialising the doc object will be relevant to your goal. With that in mind, if the specific readiness is not set in any of these contexts, then this explains why we are seeing NULL as a response.

While we are always looking for ways to improve, the approach outlined here is what we recommend for now. Following these instructions closely will help you progress and ensure your work with Speckle and Revit goes as planned.

We’d love to know what you are actually trying to build that isn’t possible with the existing connector.

2 Likes

Thanks @jonathon and @teocomi,

Sorry for the late reply.
After creating UIDocumentProvider and RevitDocumentAggregateCache, that works for me. Thanks for your both help.

var kit = KitManager.GetDefaultKit();
var converter = kit.LoadConverter(HostApplications.Revit.GetVersion(HostAppVersion.v2022));

    converter.SetContextDocument(doc);
    var settings = new Dictionary<string, string>();
    converter.SetConverterSettings(settings);

    List<ApplicationObject> ApplicationObject = new List<ApplicationObject>();
    foreach (ElementId ei in selectedIds)
    {
        Element element = uidoc.Document.GetElement(ei);
        ApplicationObject.Add(new ApplicationObject(element.UniqueId, element.GetType().ToString()));
    }
    converter.SetContextObjects(ApplicationObject);
    var commitObject = converter.ConvertToSpeckle(doc) ?? new Collection();


    string url_ = "https://speckle.xyz/";
    Account account = new Account();
    account.token = token;
    account.serverInfo = new ServerInfo
    {
        url = url_
    };
    var transports = new List<Speckle.Core.Transports.ITransport>() { new ServerTransport(account, "58f0896602") };

    var objectId = await Operations.Send(
        @object: commitObject,
        transports: transports,
        disposeTransports: true
        ).ConfigureAwait(true);

    var actualCommit = new CommitCreateInput()
    {
        streamId = streamid,
        objectId = objectId,
        branchName = branchname,
        message = message,
        sourceApplication = "CoreKit",
    };

    var client = new Client(account);

    var response = await client.CommitCreate(actualCommit);

Best regards,
Albert

2 Likes