我对C#和编码很新。如果可能的话,我会在寻求如何修复这段代码之后给予一些帮助。
他们单独工作。我可以在功能区上创建一个新按钮并执行标准的hello世界。我也有一个宏,它可以在我可以成功删除所有的工作表和视图,但尝试将两者结合起来会造成很大的困难!代码构建正常,但我在revit中收到此错误:
'无法初始化加载项“删除视图”,因为该类 在加载项程序集中找不到“DeleteViews”.FullClassName 为Revit调用加载项应用程序提供了enrty点。对于 要运行加载项,必须确保此类实现 “Autodesk.Revit.UI.ExternalCommand”界面。'
我很确定我的问题是在第二位代码中引用。我知道我没有正确地调用它,但却找不到任何解决方案。
道歉,如果这是一个愚蠢的问题,但非常感谢任何帮助我学习的帮助!
感谢您提供任何帮助
代码:
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;
}
}
}
答案 0 :(得分:1)
首先,自己输入类名,考虑使用反射,如:
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
}
}
}
答案 1 :(得分:1)
在完成任何其他操作之前,您应该先使用Revit API入门材料,尤其是 DevTV 和 My First Revit插件视频教程:
http://thebuildingcoder.typepad.com/blog/about-the-author.html#2
然后,这个问题和许多其他基本问题将得到预先解决,你将为自己和他人节省一些努力和头脑。
答案 2 :(得分:1)
由于单击按钮时发生事务错误,我需要在IExternalCommand之前添加一行。
[Transaction(TransactionMode.Manual)]