gpt4 book ai didi

c# - Revit API - 全类名错误

转载 作者:太空宇宙 更新时间:2023-11-03 23:03:25 28 4
gpt4 key购买 nike

我对 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com