Files
ShrlAlgoToolkit/ShrlAlgo.Addin.Test/ModifySystemTypeCmd.cs

176 lines
6.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
namespace ShrlAlgo.Addin.Test;
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
internal class ModifySystemTypeCmd : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
//程序UI界面
var uiapp = commandData.Application;
//获取元素(选择) 显示元素 视图(活动视图)管理(对象)
var uidoc = uiapp.ActiveUIDocument;
//程序
var app = uiapp.Application;
//获取位置和场地 视图(多个视图)管理 获取元素Revit 项目里的全部元素)
var doc = uidoc.Document;
//获取所有打开文档
var docset = uiapp.Application.Documents;
//当前视图
var view = doc.ActiveView;
//var views = doc.OfClass<View>().Cast<View>().Where(v => !v.IsTemplate);
//foreach (var v in views)
//{
// if (v.GetParameter("子视图分类")?.AsString() == "01 平面")
// {
// MessageBox.Show(v.Id.ToString());
// break;
// }
//}
#region RegionName
//var ins = doc.OfClass<FamilyInstance>().Cast<FamilyInstance>();
//var pipes = doc.OfClass<Pipe>().Cast<Pipe>();
//using (var trans = new Transaction(doc, nameof(ModifySystemTypeCmd)))
//{
// trans.Start();
// var level = doc.OfClass<Level>().Cast<Level>().FirstOrDefault(l => l.Name == "站厅层");
// foreach (var pipe in pipes)
// {
// if (pipe.ReferenceLevel.Name == "站厅层.")
// {
// try
// {
// pipe.ReferenceLevel = doc.GetElement(level.Id) as Level;
// }
// catch (Exception ex)
// {
// MessageBox.Show(pipe.Name);
// }
// }
// }
// foreach (var instance in ins)
// {
// var e = doc.GetElement(instance.LevelId);
// if (e != null && e.Name == "站厅层.")
// {
// try
// {
// instance.get_Parameter(BuiltInParameter.FAMILY_LEVEL_PARAM)?.Set(level.Id);
// }
// catch (Exception ex)
// {
// MessageBox.Show(e.Name);
// }
// }
// }
// trans.Commit();
//}
#endregion
//var templates = doc.OfClass<View>().Cast<View>().Where(v => v.IsTemplate);
//foreach (var template in templates)
//{
// var filters = template.GetFilters();
// foreach (var id in filters)
// {
// var filterElement = doc.GetElement(id);
// if (filterElement is ParameterFilterElement filter)
// {
// if (filter.Name.Contains("生产生活给水系统"))
// {
// var ogs = new OverrideGraphicSettings();
// ogs.SetProjectionLineColor(new Color(0, 255, 0));
// ogs.SetProjectionLineColor(new Color(0, 255, 0));
// ogs.SetProjectionLineColor(new Color(0, 255, 0));
// ogs.SetProjectionLineColor(new Color(0, 255, 0));
// template.SetFilterOverrides(id, ogs);
// }
// }
// }
//}
//var maps = doc.ParameterBindings;
//var li = maps.ForwardIterator();
//try
//{
// using (var trans = new Transaction(doc, "修改"))
// {
// trans.Start();
// var pipeCate = Category.GetCategory(doc, BuiltInCategory.OST_PipeCurves);
// var cabtrayCate = Category.GetCategory(doc, BuiltInCategory.OST_CableTray);
// var conduitCate = Category.GetCategory(doc, BuiltInCategory.OST_Conduit);
// while (li.MoveNext())
// {
// var k = li.Key;
// if (Regex.IsMatch(k.Name, @"^[A-Z][A-Z](-)\d(00-)"))
// {
// if (li.Current is InstanceBinding binding && binding.Categories.Contains(pipeCate))
// {
// //binding.Categories.Insert(cabtrayCate);
// //binding.Categories.Insert(conduitCate);
// //binding.Categories.Erase(pipeCate);
// doc.ParameterBindings.ReInsert(k, binding, BuiltInParameterGroup.PG_ADSK_MODEL_PROPERTIES);
// }
// }
// }
// trans.Commit();
// }
//}
//catch (Exception)
//{
// throw;
//}
#region RegionName
#endregion
try
{
//var r = uidoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element);
var ids = uidoc.Selection.GetElementIds();
foreach (var id in ids)
{
var elem = doc.GetElement(id);
if (elem is not Pipe)
{
continue;
}
var systemType = new FilteredElementCollector(doc).OfClass(typeof(PipingSystemType))
.FirstOrDefault(p => p.Name == "气体灭火管网系统") as PipingSystemType;
using (var trans = new Transaction(doc, nameof(ModifySystemTypeCmd)))
{
trans.Start();
elem.get_Parameter(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM).Set(systemType.Id);
//foreach (var id in uidoc.Selection.GetElementIds())
//{
// var elem = doc.GetElement(id);
// elem.ChangeTypeId(systemType.Id);
//}
trans.Commit();
}
}
}
catch (Exception)
{
}
return Result.Succeeded;
}
}