1

This is my first question here. I'm a beginner in revit api programming, so I'm sorry if my question is too lame or missoriented. Hope someone can help me. I´m triying to implement the Iscommand available method in this simple learning example and I can´t undestand why it is not working, I mean the command is still available to use under any scenario. Thanks in advance!

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI.Selection;
using System.Windows.Forms;

namespace PruebasAPI
{
    [Autodesk.Revit.Attributes.Transaction(TransactionMode.Automatic)]
    class IExternalcommand_elements : IExternalCommand
{

    public bool IsCommandAvailable(Autodesk.Revit.UI.UIApplication applicationData,
        CategorySet selectedCategories)
    {
        //allow button click if there is no active selection
        if (selectedCategories.IsEmpty)
            return true;
        //allow button click if there is at least one wall selected
        foreach (Category c in selectedCategories)
            {
                if (c.Id.IntegerValue == (int)BuiltInCategory.OST_Walls)
                    return true;
            }
            return false;
        }


    public Result Execute(
                            ExternalCommandData commandData,
                                     ref string message,
                                     ElementSet elements)
    {
        try
        {
            Document doc = commandData.Application.ActiveUIDocument.Document;
            UIDocument uidoc = commandData.Application.ActiveUIDocument;

            //delete selected elements
            ICollection<Autodesk.Revit.DB.ElementId> ids = doc.Delete(uidoc.Selection.Elements);

            TaskDialog taskdialog = new TaskDialog("Revit");
            taskdialog.MainContent =
                ("click yes to return succeded.Selected members will be deleted. \n" +
                "click no to return failed.Selected members will not be deleted \n" +
                "click cancel to return cancelled. Selected members will not be deleted.");

            TaskDialogCommonButtons buttons = TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No | TaskDialogCommonButtons.Cancel;
            taskdialog.CommonButtons = buttons;
            TaskDialogResult taskdialogresult = taskdialog.Show();

            if (taskdialogresult == TaskDialogResult.Yes)
            {
                return Result.Succeeded;
            }
            else if (taskdialogresult == TaskDialogResult.No)
            {
                elements = uidoc.Selection.Elements;
                message = "failed to delete selection";
                return Result.Failed;
            }
            else
            {
                return Result.Cancelled;
            }
        }
        catch
        {
            message = "unespected dika";
            return Result.Failed;
        }
    }
}

}`

Linger
  • 14,942
  • 23
  • 52
  • 79

1 Answers1

3

The IsCommandAvailable should not be in your command class. You actually need to write a class that implements IExternalCommandAvailability. Here's the example from the API guide:

public class SampleAccessibilityCheck : IExternalCommandAvailability
{
    public bool IsCommandAvailable(Autodesk.Revit.UI.UIApplication applicationData,
         CategorySet selectedCategories)
    {
        // Allow button click if there is no active selection 
        if (selectedCategories.IsEmpty)
            return true;
        // Allow button click if there is at least one wall selected 
        foreach (Category c in selectedCategories)
        {
            if (c.Id.IntegerValue == (int)BuiltInCategory.OST_Walls)
                return true;
        }
        return false;
    }
}

Then you can specify this class name inside your Addin manifest file inside the tag AvailabilityClassName, such as:

<AvailabilityClassName>MyNamespace.SampleAccessibilityCheck</AvailabilityClassName>

if you have a button on the ribbon, the PushButton class also has a PushButton.AvailabilityClassName property where you can set the name of this class so that your command button enables/disables accordingly.

Hope this helps.

Bugra Barin
  • 246
  • 1
  • 4