IExternalCommandAvailability in the same class as IExternalCommand

IExternalCommandAvailability in the same class as IExternalCommand

rhanzlick
Advocate Advocate
540 Views
2 Replies
Message 1 of 3

IExternalCommandAvailability in the same class as IExternalCommand

rhanzlick
Advocate
Advocate

Am I able to implement both of these interfaces in the same class?

The provided sample does not seem to work, and I can't quite figure out why. I haven't set the PushButton property to a reference to itself, could that be the problem? Why isn't availability implemented in this way?

 

The full implementation for availability seems to be very cumbersome, so I have resorted to returning Result.Canceled with a taskdialog within the Execute method of the normal class (but allowing the button to always be available)

 

Any info would be much appreciated. Thanks!
    

public class ExampleClass : IExternalCommand, IExternalCommandAvailability
{
    public override Result Execute(ExternalCommand commandData)
    {
        TaskDialog.Show("AvailableCommand","This is a floorplan!");
        return Result.Succeeded;
    }

    public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
    {
        if(applicationData.ActiveUIDocument.ActiveView.ViewType == ViewType.FloorPlan) return true;
        else return false;
    }
}


}

0 Likes
Accepted solutions (1)
541 Views
2 Replies
Replies (2)
Message 2 of 3

RPTHOMAS108
Mentor
Mentor

It is an interface and any class can implement an interface. Revit shouldn't care where it looks for it as long as it is told where to find it and it can see it from outside.

 

The above should work if you point to it correctly when the button was created. I believe you still have to specify the availability class even if it is the same as the class that implements IExternalCommand. Would use class viewer or ildasm to check that the class sits at the namespace level given in the availability class name string.

 

It often makes more sense to have it on it's own if you want many buttons to have similar availability checks but again that has nothing to do with it being able to be called or not (just good practice from a code understanding perspective).

0 Likes
Message 3 of 3

rhanzlick
Advocate
Advocate
Accepted solution

As a follow-up, I tested this and it worked in a single class. The key was to add the AvailabilityClass reference as itself. This will be useful as many of our add-ins have unique criteria in which they are enabled/disabled, so being able to control this within the class itself makes referencing much easier.

 

@RPTHOMAS108 Thanks for the response!

 

Regards,

Ryan

0 Likes