DaveGrohl
DaveGrohl

Reputation: 35

Autodesk Revit Room Name Spell Check

I am trying to implement a spell check for room names in Autodesk Revit using the LanguageTool API. However, Revit freezes as soon as I run the script. It seems like the problem starts when executing the following part of the code.

Questions:

Why is Revit freezing when I run this script? Are there any better ways to implement spell checking in Revit for room names? Any suggestions on getting Hunspell to work with the de_CH locale?

I appreciate any help or suggestions!

private async Task<(bool isCorrect, List<string> suggestions)> CheckSpelling(string word)
{
    string url = $"https://api.languagetool.org/v2/check?text={word}&language=de-CH";

    using (HttpClient client = new HttpClient())
    {
        HttpResponseMessage response = await client.GetAsync(url);
        response.EnsureSuccessStatusCode();

        string responseBody = await response.Content.ReadAsStringAsync();
        JObject json = JObject.Parse(responseBody);

        var matches = json["matches"];
        if (matches != null && matches.HasValues)
        {
            List<string> suggestions = new List<string>();
            foreach (var match in matches)
            {
                var replacements = match["replacements"];
                if (replacements != null && replacements.HasValues)
                {
                    suggestions.AddRange(replacements.Select(r => r["value"].ToString()));
                }
            }
            return (false, suggestions);
        }
        else
        {
            return (true, new List<string>());
        }
    }
}

Upvotes: 0

Views: 51

Answers (2)

Jeremy Tammik
Jeremy Tammik

Reputation: 8339

A more complete version of the question including sample code providing the necessary context is shared in the Revit API discussion forum thread on Spell Check for Room Names, and, therefore, a more complete answer is provided there as well.

Upvotes: 0

Jeremy Tammik
Jeremy Tammik

Reputation: 8339

It depends a lot on the context in which you are trying to run this method. Please be aware of the following fundamental principles underlying the Revit API:

  • The Revit API is single-threaded and can only be used within a valid Revit API context. Such a context is only provided by the Revit API event handlers. So, your add-in needs to subscribe to certain Revit API events. Within the handler, you have access to Revit API functionality.

The code you share above does not explicitly make any use whatsoever of any Revit API functionality, afaict.

However, the async may or may not break the execution context. I cannot tell without more context.

Upvotes: 0

Related Questions