c# - Revit API - 错误的完整类名

标签 c# revit-api revit revit-2015

我对 C# 和编码非常陌生。如果可能的话,我正在寻求一些帮助来弄清楚如何修复这段代码以使其正常工作。

他们单独工作。我可以在功能区上创建一个新按钮并执行标准的 hello world。我还有一个宏,可以在其中成功删除所有工作表和 View ,但尝试将两者结合起来会造成很大的困难!代码构建正常,但我在 revit 中收到此错误:

'Failed to initialize the add-in "Delete Views" because the class "DeleteViews" cannot be found in the add-in assembly.The FullClassName provides the enrty point for Revit to call add-in application. For Revit to run the add-in, you must ensure this class implements the "Autodesk.Revit.UI.ExternalCommand" interface.'

我很确定我的问题是在第二段代码中引用的。我知道我没有正确调用它,但一直无法找到任何解决方案。

如果这是一个愚蠢的问题,我深表歉意,但非常感谢任何帮助我学习的帮助!

感谢您给我的任何帮助

代码:

namespace BGPanel
{
public class CsBGPanel : IExternalApplication
{
    public UIDocument ActiveUIDocument { get; private set; }
    public Result OnStartup(UIControlledApplication application)
    {
        RibbonPanel ribbonPanel = application.CreateRibbonPanel("Tools");

        string thisAssemblyPath = Assembly.GetExecutingAssembly().Location;
        PushButtonData buttonData = new PushButtonData("cmdDeleteViews",
           "Delete Views", thisAssemblyPath, "BGPanel.DeleteViews");

        PushButton pushButton = ribbonPanel.AddItem(buttonData) as PushButton;

        pushButton.ToolTip = "Delete all sheets, schedules & views except structural plans";

        Uri uriImage = new Uri(@"C:\Revit_API\Revit_2015\32px-Broom.png");
        BitmapImage largeImage = new BitmapImage(uriImage);
        pushButton.LargeImage = largeImage;

        return Result.Succeeded;
    }

         public void DeleteViews()
    {
        UIDocument uidoc = this.ActiveUIDocument;
        Document doc = uidoc.Document;

        FilteredElementCollector collector = new FilteredElementCollector(doc);
        ICollection<Element> collection = collector.OfClass(typeof(View)).ToElements();

        using (Transaction t = new Transaction(doc, "Delete Views"))
        {
            t.Start();

            int x = 0;

            foreach (Element e in collection)
            {
                try
                {
                    View view = e as View;

                    switch (view.ViewType)
                    {
                        case ViewType.FloorPlan:
                            break;
                        case ViewType.EngineeringPlan:
                            break;
                        case ViewType.ThreeD:
                            break;
                        default:
                            doc.Delete(e.Id);
                            x += 1;
                            break;
                    }
                }
                catch (Exception ex)
                {
                    View view = e as View;
                    TaskDialog.Show("Error", e.Name + "\n" + "\n" + ex.Message);
                    TaskDialog.Show("Error", ex.Message);
                }
            }
            t.Commit();

            TaskDialog.Show("BG_API DeleteViews", "Views Deleted: " + x.ToString());
        }

    }
    public Result OnShutdown(UIControlledApplication application)
    {
        return Result.Succeeded;
    }
}
}

最佳答案

首先,考虑使用反射,而不是自己输入类名:

typeof(YourClassName).FullName

其次,Revit 中的每个命令都需要有自己的类来实现 IExternalCommand。我没有测试过,但你的代码应该类似于以下内容:

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Media.Imaging;

using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace BGPanel
{
  public class CsBGPanel : IExternalApplication
  {
    public Result OnStartup(UIControlledApplication application)
    {
      RibbonPanel ribbonPanel = application.CreateRibbonPanel("Tools");

      string thisAssemblyPath = Assembly.GetExecutingAssembly().Location;
      PushButtonData buttonData = new PushButtonData("cmdDeleteViews",
         "Delete Views", thisAssemblyPath, typeof(DeleteViews).FullName);

      PushButton pushButton = ribbonPanel.AddItem(buttonData) as PushButton;

      pushButton.ToolTip = "Delete all sheets, schedules & views except structural plans";

      Uri uriImage = new Uri(@"C:\Revit_API\Revit_2015\32px-Broom.png");
      BitmapImage largeImage = new BitmapImage(uriImage);
      pushButton.LargeImage = largeImage;

      return Result.Succeeded;
    }

    public Result OnShutdown(UIControlledApplication application)
    {
      return Result.Succeeded;
    }
  }

  public class DeleteViews : IExternalCommand
  {
    // this will not work...
    //public UIDocument ActiveUIDocument { get; private set; }

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

      FilteredElementCollector collector = new FilteredElementCollector(doc);
      ICollection<Element> collection = collector.OfClass(typeof(View)).ToElements();

      using (Transaction t = new Transaction(doc, "Delete Views"))
      {
        t.Start();

        int x = 0;

        foreach (Element e in collection)
        {
          try
          {
            View view = e as View;

            switch (view.ViewType)
            {
              case ViewType.FloorPlan:
                break;
              case ViewType.EngineeringPlan:
                break;
              case ViewType.ThreeD:
                break;
              default:
                doc.Delete(e.Id);
                x += 1;
                break;
            }
          }
          catch (Exception ex)
          {
            View view = e as View;
            TaskDialog.Show("Error", e.Name + "\n" + "\n" + ex.Message);
            TaskDialog.Show("Error", ex.Message);
          }
        }
        t.Commit();

        TaskDialog.Show("BG_API DeleteViews", "Views Deleted: " + x.ToString());
      }
      return Result.Succeeded; // must return here
    }
  }
}

关于c# - Revit API - 错误的完整类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42218031/

相关文章:

c# - 使用 Windows 窗体创建 Revit 插件

c# - 如何使用 C# 在 API 中加载 Revit 文件?

c# - 客户端通过 Azure AD 应用程序代理发出的请求中缺少授权 header

revit-api - 无法提示用户使用 Revit Python Shell 选择房间

c# - .NET MVC 使用模型属性更新 View

revit-api - 如何通过revit API访问所有族类型?

c# - 如何使用c#获取revit中所有元素的列表

c# - Revit API : How to create Element Keynote for a wall and Material Keynote?

c# - 在面板内的控件之上绘图 (C# WinForms)

c# - 当用 [] 中的数字赋值时,无法将类型字符串 [] 转换为字符串