151 lines
4.5 KiB
C#
151 lines
4.5 KiB
C#
|
|
using System;
|
|||
|
|
|
|||
|
|
using Autodesk.Revit.Attributes;
|
|||
|
|
using Autodesk.Revit.DB;
|
|||
|
|
using Autodesk.Revit.UI;
|
|||
|
|
using Autodesk.Revit.UI.Selection;
|
|||
|
|
|
|||
|
|
using Nice3point.Revit.Toolkit.External;
|
|||
|
|
|
|||
|
|
|
|||
|
|
namespace ShrlAlgo.Addin.Test;
|
|||
|
|
[Transaction(TransactionMode.Manual)]
|
|||
|
|
[Regeneration(RegenerationOption.Manual)]
|
|||
|
|
public class AddWallTypeParam : ExternalCommand
|
|||
|
|
{
|
|||
|
|
public Result Executex(ExternalCommandData commandData, ref string message, ElementSet elements)
|
|||
|
|
{
|
|||
|
|
//程序UI界面
|
|||
|
|
UIApplication uiapp = commandData.Application;
|
|||
|
|
//获取元素(选择) 显示元素 视图(活动视图)管理(对象)
|
|||
|
|
UIDocument uidoc = uiapp.ActiveUIDocument;
|
|||
|
|
//程序
|
|||
|
|
Autodesk.Revit.ApplicationServices.Application app = uiapp.Application;
|
|||
|
|
//获取位置和场地 视图(多个视图)管理 获取元素(Revit 项目里的全部元素)
|
|||
|
|
Document doc = uidoc.Document;
|
|||
|
|
//获取所有打开文档
|
|||
|
|
DocumentSet docset = uiapp.Application.Documents;
|
|||
|
|
//当前视图
|
|||
|
|
View view = doc.ActiveView;
|
|||
|
|
|
|||
|
|
#region FilterExecute
|
|||
|
|
|
|||
|
|
var walls = new FilteredElementCollector(doc).OfClass(typeof(Wall)).ToElements();
|
|||
|
|
using (Transaction trans = new Transaction(doc, "default"))
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
trans.Start();
|
|||
|
|
//Do Something.
|
|||
|
|
trans.Commit();
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
message = ex.Message;
|
|||
|
|
if (trans.GetStatus() == TransactionStatus.Started)
|
|||
|
|
{
|
|||
|
|
trans.RollBack();
|
|||
|
|
}
|
|||
|
|
return Result.Failed;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion FilterExecute
|
|||
|
|
|
|||
|
|
#region RepeatExecute
|
|||
|
|
|
|||
|
|
bool isCoutine = true;
|
|||
|
|
using (Transaction trans = new Transaction(doc, "default"))
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
while (isCoutine)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
trans.Start();
|
|||
|
|
//do something.
|
|||
|
|
trans.Commit();
|
|||
|
|
}
|
|||
|
|
catch (Exception)
|
|||
|
|
{
|
|||
|
|
trans.Commit();
|
|||
|
|
return Result.Succeeded;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
|
|||
|
|
{
|
|||
|
|
if (trans.GetStatus() == TransactionStatus.Started)
|
|||
|
|
{
|
|||
|
|
trans.Commit();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
message = ex.Message;
|
|||
|
|
if (trans.GetStatus() == TransactionStatus.Started)
|
|||
|
|
{
|
|||
|
|
trans.Commit();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return Result.Succeeded;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion RepeatExecute
|
|||
|
|
|
|||
|
|
#region SelectExecute
|
|||
|
|
|
|||
|
|
using (Transaction trans = new Transaction(doc, "default"))
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
Reference refer = uidoc.Selection.PickObject(ObjectType.Element, "请选择XXX");
|
|||
|
|
Element e = uidoc.Document.GetElement(refer);
|
|||
|
|
|
|||
|
|
trans.Start();
|
|||
|
|
//Do Something.
|
|||
|
|
trans.Commit();
|
|||
|
|
}
|
|||
|
|
catch (Autodesk.Revit.Exceptions.OperationCanceledException ex)
|
|||
|
|
{
|
|||
|
|
if (trans.GetStatus() == TransactionStatus.Started)
|
|||
|
|
{
|
|||
|
|
trans.Commit();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return Result.Succeeded;
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
message = ex.Message;
|
|||
|
|
if (trans.GetStatus() == TransactionStatus.Started)
|
|||
|
|
{
|
|||
|
|
trans.RollBack();
|
|||
|
|
}
|
|||
|
|
return Result.Failed;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion SelectExecute
|
|||
|
|
|
|||
|
|
return Result.Succeeded;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public override void Execute()
|
|||
|
|
{
|
|||
|
|
Document.Invoke(
|
|||
|
|
ts =>
|
|||
|
|
{
|
|||
|
|
var file = ParameterAssist.OpenSharedParametersFile(Application);
|
|||
|
|
var group = file.Groups.get_Item("Atkore");
|
|||
|
|
var definition = group.Definitions.get_Item("test");
|
|||
|
|
var categories = Application.Create.NewCategorySet();
|
|||
|
|
categories.Insert(Category.GetCategory(Document, BuiltInCategory.OST_Walls));
|
|||
|
|
var newIB = Application.Create.NewTypeBinding(categories);
|
|||
|
|
var b = Document.ParameterBindings.ReInsert(definition, newIB);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|