Search code examples
c#revit-apirevit

Revit API: Use String as Button Assembly Name with Current Path


I'm not entirely sure at all why this is happening... So I have a ExternalCommand and an application for making a ribbon tab and button. These two programs are in the same solution and under the same namespace, which allows me to have fewer files to deal with. When I create a button for my command, I want to put in the current path of the application that is currently running. I do this with Directory.GetCurrentDirectory() + \AddInsAll\Ribbon17.dll (where AddInsAll is the folder and Ribbon17 is the dll, obviously). I use @ when necessary to avoid escape sequences. This string contains the exact assembly name needed, but Revit tells me "Assembly does not exist." If I replace this String variable with the hard coded C:\ProgramData\Autodesk\Revit\Addins\2017\AddInsAll\Ribbon17.dll it works. I want it obviously more robust than that. My code will be below, thanks in advance.

FYI: I have a TaskDialog showing when it first runs, and the fullPath that it returns is exacly the same as the hard coded path. I have to do a replace (Program Files to ProgramData) due to some weird bug with the get directory. Also, I add "\AddInsAll\Ribbon17.dll" to the end of the string because the CurrentDirectory goes only to Addins\2017. Finally, if you think the problem is due to the @'s, I have already tried putting it and taking it off of variables and none of the attempts work. But if you think of them is the problem, I welcome the advice. Thanks.

public class RibApp : IExternalApplication
{
    public Result OnStartup(Autodesk.Revit.UI.UIControlledApplication application)
    {
        // Create a custom ribbon tab
        String tabName = "Add-Ins";
        String fakeFullPath = @Directory.GetCurrentDirectory() + @"\AddInsAll\Ribbon17.dll";
        String fullPath = fakeFullPath.Replace(@"\Program Files\", @"\ProgramData\");
        TaskDialog.Show("Hi", @fullPath);
        application.CreateRibbonTab(tabName);

        //Create buttons and panel
            // Create two push buttons
            PushButtonData CommandButton = new PushButtonData("Command17", "Command",
                @fullPath, "Ribbon17.Command");

Solution

  • I suggest you skip the @ and replace each backslash \ by a forward slash /.

    KISS!

    Better still, use an approach similar to the CreateRibbonTab implementation in the HoloLens Escape Path Waypoint JSON Exporter.