using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Media.Imaging;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Windows;
using AutoUpdaterDotNET;
using Nice3point.Revit.Toolkit.External;
using Nice3point.Revit.Toolkit.External.Handlers;
using Szmedi.RvKits.Civil;
using Szmedi.RvKits.Common;
using Szmedi.RvKits.DisplayTools;
using Szmedi.RvKits.DrawingTools;
using Szmedi.RvKits.FamilyTools;
using Szmedi.RvKits.InfoManager;
using Szmedi.RvKits.InfoManager.AFCA;
using Szmedi.RvKits.InfoManager.EAMTools;
using Szmedi.RvKits.InfoManager.PropEditor;
using Szmedi.RvKits.MEPTools;
using Szmedi.RvKits.Modeling;
using Szmedi.RvKits.ModelManager;
using Szmedi.RvKits.Properties;
using Szmedi.RvKits.RvScript;
using RibbonButton = Autodesk.Windows.RibbonButton;
using RibbonPanel = Autodesk.Revit.UI.RibbonPanel;
namespace Szmedi.RvKits.UIApp;
public class UIRibbon : ExternalApplication
{
private const string TabName = "SZMEDI工具";
private static readonly string AddInPath = GlobalVariables.AddInPath;
private Autodesk.Windows.RibbonPanel addInManagerPanel;
private AsyncEventHandler saveHandler;
private UIApplication uiapp;
//private void Updater()
//{
// Assembly assembly = Assembly.GetExecutingAssembly();
// //程序集版本
// AutoUpdater.Start("https://rbsoft.org/updates/AutoUpdaterTest.xml", assembly);
// //ftp服务器基本验证
// AutoUpdater.Start("ftp://rbsoft.org/updates/AutoUpdaterTest.xml", new NetworkCredential("FtpUserName", "FtpPassword"));
// AutoUpdater.Synchronous = true;
// AutoUpdater.ShowSkipButton = false;
// AutoUpdater.ShowRemindLaterButton = false;
// AutoUpdater.Mandatory = true;//忽略稍后提醒或跳过
// AutoUpdater.UpdateMode = Mode.Forced;//强制更新
// BasicAuthentication basicAuthentication = new("myUserName", "myPassword");
// AutoUpdater.BasicAuthXML = AutoUpdater.BasicAuthDownload = AutoUpdater.BasicAuthChangeLog = basicAuthentication;
// AutoUpdater.HttpUserAgent = "AutoUpdater";
// AutoUpdater.ReportErrors = true;
// AutoUpdater.RunUpdateAsAdmin = true;
// AutoUpdater.UpdateFormSize = new System.Drawing.Size(800, 600);
// AutoUpdater.TopMost = true;
// AutoUpdater.SetOwner(ComponentManager.ApplicationWindow);
// //定时检查更新
// DispatcherTimer timer = new() { Interval = TimeSpan.FromMinutes(2) };
// timer.Tick += delegate
// {
// AutoUpdater.Start("https://rbsoft.org/updates/AutoUpdaterTestWPF.xml");
// };
// timer.Start();
//}
// public static RadioButtonGroup ZoomRBGroup { private set; get; }
private ActionEventHandler zoomElementHandler;
//族文档插件不可见
private void Application_ViewActivating(object sender, Autodesk.Revit.UI.Events.ViewActivatingEventArgs e)
{
SetTabEnabled(TabName, !e.NewActiveView.Document.IsFamilyDocument);
}
///
/// 框选与视图中当前选择的图元类型一样的元素
///
private void BoxSelectTheSameElemsAtCurrentView()
{
try
{
var uidoc = uiapp.ActiveUIDocument;
var doc = uidoc.Document;
var sel = uidoc.Selection;
var selectedIds = sel.GetElementIds();
List setSelectedElemIds = [];
List setSelectedElems = [];
if (selectedIds.Count == 0)
{
return;
}
foreach (var elemId in selectedIds)
{
var selectedElement = doc.GetElement(elemId);
setSelectedElems.Add(selectedElement);
}
SelectTheSameElements selFilter = new(setSelectedElems);
var elements = uidoc.Selection.PickElementsByRectangle(selFilter, "请框选与当前选择元素相同的实例");
setSelectedElemIds.AddRange(elements.Select(e => e.Id));
uidoc.Selection.SetElementIds(setSelectedElemIds);
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException) { }
catch (Exception ex)
{
MessageBox.Show(ex.Message);
LogAssists.WriteLog(ex.StackTrace);
}
}
private static void CommonApp(RibbonPanel standardPanel)
{
PushButtonData addViewFilters =
new(nameof(AddViewFiltersCmd), "土建过滤", AddInPath, typeof(AddViewFiltersCmd).FullName)
{
LargeImage = Resources.ElementFilter_32px.ToBitmapSource(),
ToolTip = "在当前视图添加区分建筑、结构构件的默认过滤器"
};
PushButtonData resolveLevelPbd =
new(nameof(ResolveLevelCmd), "标高重设", AddInPath, typeof(ResolveLevelCmd).FullName)
{
LargeImage = Resources.LevelSet_32px.ToBitmapSource(),
ToolTip = "将构件按实际标高位置重设参照标高"
};
PushButtonData splittingPbd =
new(nameof(SplitComponentByLevelCmd), "拆分墙柱", AddInPath, typeof(SplitComponentByLevelCmd).FullName)
{
LargeImage = Resources.SplitCom_32px.ToBitmapSource(),
ToolTip = "按标高拆分墙、柱"
};
PushButtonData levelSeparatePbd =
new(nameof(LevelSeparateCmd), "构件划分", AddInPath, typeof(LevelSeparateCmd).FullName)
{
LargeImage = Resources.Floors_Separate_32px.ToBitmapSource(),
Image = Resources.Floors_Separate_16px.ToBitmapSource(),
ToolTip = "按标高或所属楼层对构件进行划分"
};
PushButtonData slopeFloorCreator =
new(nameof(SlopedFloorCmd), "坡度楼板", AddInPath, typeof(SlopedFloorCmd).FullName)
{
LargeImage = Resources.Inclination_32px.ToBitmapSource(),
ToolTip = "选择两个构件的两条平行的边线快速创建楼板"
};
PushButtonData resolveConnection =
new(nameof(ResolveCivilConnections), "连接构件", AddInPath, typeof(ResolveCivilConnections).FullName)
{
Image = Resources.union_16px.ToBitmapSource(),
LargeImage = Resources.union_32px.ToBitmapSource(),
ToolTip = "批量连接墙、梁、板、柱等构件"
};
PushButtonData room3D =
new(nameof(Room3D), "房间实体", AddInPath, typeof(Room3D).FullName)
{
Image = Resources.Room3D_16px.ToBitmapSource(),
LargeImage = Resources.Room3D_32px.ToBitmapSource(),
ToolTip = "根据房间的高差属性,创建房间的几何实体"
};
PushButtonData geologyByLoop =
new(nameof(CutGeologyByLoopCmd), "地质剖切", AddInPath, typeof(CutGeologyByLoopCmd).FullName)
{
Image = Resources.GeologyBlock_16px.ToBitmapSource(),
LargeImage = Resources.GeologyBlock_32px.ToBitmapSource(),
ToolTip = "根据框选的闭合曲线,对内建的地质体模型进行剖切"
};
PushButtonData roomNaming =
new(nameof(RoomNamingCmd), "车站房间", AddInPath, typeof(RoomNamingCmd).FullName)
{
Image = Resources.RoomManager_16px.ToBitmapSource(),
LargeImage = Resources.RoomManager_32px.ToBitmapSource(),
ToolTip = "快速修改房间名",
LongDescription = "将房间名修改为车站房间名,如:站台、候车室、售票处等"
};
PushButtonData roomNameAssign =
new(nameof(RoomNameAssignCmd), "房间命名", AddInPath, typeof(RoomNameAssignCmd).FullName)
{
Image = Resources.DWGRoomRename_16px.ToBitmapSource(),
LargeImage = Resources.DWGRoomRename_32px.ToBitmapSource(),
ToolTip = "根据CAD图纸的图层名称命名",
LongDescription = "布置好房间,在平面视图中,链接CAD图纸(注:房间名不是文字或多行文字需要炸开),对齐到模型,拾取文字,自动改名。",
AvailabilityClassName = typeof(EnableOnPlan).FullName
};
PushButtonData roomBatch =
new(nameof(RoomsBatchCmd), "房间设置", AddInPath, typeof(RoomsBatchCmd).FullName)
{
Image = Resources.RoomBatch_16px.ToBitmapSource(),
LargeImage = Resources.RoomBatch_32px.ToBitmapSource(),
ToolTip = "批量修改房间属性,统计",
LongDescription = "批量修改房间的标高,获取体积"
};
PushButtonData resetCoordinate =
new(nameof(ResetCoordinateCmd), "重置基点", AddInPath, typeof(ResetCoordinateCmd).FullName)
{
LargeImage = Resources.Reset_32px.ToBitmapSource(),
ToolTip = "批量处理项目基点清零"
};
PushButtonData moveModel =
new(nameof(MoveModelCmd), "移动模型", AddInPath, typeof(MoveModelCmd).FullName)
{
LargeImage = Resources.MoveModel_32px.ToBitmapSource(),
ToolTip = "在平、立面、剖面视图下移动全部模型",
LongDescription = "部分图元无法移动或移动会错位,需手动处理,如:基于面放置的族实例等,如果发生严重错误,如部分草图楼梯移动发生严重错误,可以尝试剪切粘贴后再使用此命令",
AvailabilityClassName = typeof(EnableOnPlanOrSection).FullName
};
PushButtonData getCoordinate =
new(nameof(GetBasePointInfoCmd), "获取基点", AddInPath, typeof(GetBasePointInfoCmd).FullName)
{
LargeImage = Resources.Coordinate_32px.ToBitmapSource(),
ToolTip = "获取项目基点"
};
PushButtonData materialProcess =
new(nameof(MaterialBatchCmd), "土建材质", AddInPath, typeof(MaterialBatchCmd).FullName)
{
LargeImage = Resources.Material_32px.ToBitmapSource(),
ToolTip = "土建构件的材质赋予、统一、查找发光材质"
};
PushButtonData renameType =
new(nameof(RenameTypeNameCmd), "重命名\n类型名", AddInPath, typeof(RenameTypeNameCmd).FullName)
{
LargeImage = Resources.Rename_32px.ToBitmapSource(),
ToolTip = "批量重命名族类型"
};
PushButtonData renameFamily =
new(nameof(RenameFamilyNameCmd), "重命名\n族名", AddInPath, typeof(RenameFamilyNameCmd).FullName)
{
LargeImage = Resources.RenameFull_32px.ToBitmapSource(),
ToolTip = "批量重命名族名称"
};
PushButtonData modelReviewPbd =
new(nameof(ModelReviewCmd), "模型审查", AddInPath, typeof(ModelReviewCmd).FullName)
{
LargeImage = Resources.model_review_32px.ToBitmapSource(),
ToolTip = "对项目文件,进行项目定位、命名、属性信息的审查,并出审核报告",
LongDescription = "对项目定位、命名、属性信息、机电管线问题进行审核"
};
PushButtonData modelCheckPbd =
new(nameof(ModelCheckCmd), "模型检查", AddInPath, typeof(ModelCheckCmd).FullName)
{
LargeImage = Resources.model_check_32px.ToBitmapSource(),
ToolTip = "对模型进行多项要求的检查"
};
PushButtonData dwgBlockModelCreator =
new(nameof(InstanceCreatorCmd), "实例布置", AddInPath, typeof(InstanceCreatorCmd).FullName)
{
//Image = ConvertFromBitmap(Resources.cmd_16px),
LargeImage = Resources.ModelCreator_32px.ToBitmapSource(),
ToolTip = "拾取dwg块布置族实例",
LongDescription = "拾取dwg块布置实例,可选填参数值",
AvailabilityClassName = typeof(EnableOnPlan).FullName
};
PushButtonData trackCreator =
new(nameof(TrackCreatorCmd), "曲线布置", AddInPath, typeof(TrackCreatorCmd).FullName)
{
Image = Resources.Track_16px.ToBitmapSource(),
LargeImage = Resources.Track_32px.ToBitmapSource(),
ToolTip = "沿曲线布置实例、创建放样,可用于轨道创建、轨枕、扣件的布置",
LongDescription = "1.选择合适的轮廓族(可选)或可载入的点定位族(可选)\n2.选择合适的材质(可选)\n3.设置基于放样线截面的相对位置关系\n4.选择连续的放样线,并完成选择"
};
PushButtonData pureModel =
new(nameof(PureModelCmd), "清理\n未使用项", AddInPath, typeof(PureModelCmd).FullName)
{
LargeImage = Resources.Pure_Model_32px.ToBitmapSource(),
ToolTip = "清理未使用项,并压缩保存项目文件"
};
PushButtonData setDisplay =
new(nameof(SetModelDisplayCmd), "清理\n分析图元", AddInPath, typeof(SetModelDisplayCmd).FullName)
{
LargeImage = Resources.Clear_32px.ToBitmapSource(),
ToolTip = "取消构件的分析模型和删除辅助模型线、参照线等图元"
};
PushButtonData parameterManager =
new(nameof(ParamManagerCmd), "参数管理", AddInPath, typeof(ParamManagerCmd).FullName)
{
//Image = ConvertFromBitmap(Resources.cmd_16px),
LargeImage = Resources.ParameterManager_32px.ToBitmapSource(),
ToolTip = "交付标准及自定义的参数批处理添加、修改参数、修改参数值",
AvailabilityClassName = typeof(OnActiveProjectDocument).FullName
};
PushButtonData eamManager =
new(nameof(EAMCodingCmd), "EAM设备表", AddInPath, typeof(EAMCodingCmd).FullName)
{
Image = Resources.EAM_Code_16px.ToBitmapSource(),
LargeImage = Resources.EAM_Code_32px.ToBitmapSource(),
ToolTip = "运营提供的EAM设备表(Excel)匹配及编码写入",
};
PushButtonData eamCodeManager =
new(nameof(EAMFacilityCodeCmd), "EAM编码填写", AddInPath, typeof(EAMFacilityCodeCmd).FullName)
{
Image = Resources.CodeList_16px.ToBitmapSource(),
LargeImage = Resources.CodeList_32px.ToBitmapSource(),
ToolTip = "运营提供的EAM设备表便捷填写、更新编码",
};
PushButtonData eamCheck =
new(nameof(EAMCodeCheckCmd), "EAM编码检查", AddInPath, typeof(EAMCodeCheckCmd).FullName)
{
Image = Resources.EAM_Code_Check_16px.ToBitmapSource(),
LargeImage = Resources.EAM_Code_Check_32px.ToBitmapSource(),
ToolTip = "检查模型中的EAM编码是否有误",
};
PushButtonData mappingManager =
new(nameof(MappingElementCodeCmd), "属性匹配", AddInPath, typeof(MappingElementCodeCmd).FullName)
{
Image = Resources.PropertyMatch_16px.ToBitmapSource(),
LargeImage = Resources.PropertyMatch_32px.ToBitmapSource(),
ToolTip = "根据目标属性,匹配属性值相同的族实例并写入Excel中属性的值,仅支持可载入族。",
LongDescription = "所有的属性会使用Excel中的值完全覆盖"
};
PushButtonData parameterWriter =
new(nameof(WriteParametersCmd), "参数填写", AddInPath, typeof(WriteParametersCmd).FullName)
{
LargeImage = Resources.WriteParameters_32px.ToBitmapSource(),
ToolTip = "修改族实例参数值,请在三维视图中使用",
LongDescription = "对三维视图可见(临时隐藏也算不可见)\n的三维元素进行修改实例参数值,请切换到三维视图使用",
AvailabilityClassName = typeof(EnableOnView3d).FullName
};
PushButtonData relacePropValue =
new(nameof(ReplaceParamValueCmd), "参数值替换", AddInPath, typeof(ReplaceParamValueCmd).FullName)
{
Image = Resources.Replace_Sub_16px.ToBitmapSource(),
LargeImage = Resources.Replace_Sub_32px.ToBitmapSource(),
ToolTip = "对选中元素的某个参数的参数值,查找字段并修改",
};
PushButtonData removeProps =
new(nameof(RemoveParamsCmd), "参数移除", AddInPath, typeof(RemoveParamsCmd).FullName)
{
Image = Resources.Remove_Params_16px.ToBitmapSource(),
LargeImage = Resources.Remove_Params_32px.ToBitmapSource(),
ToolTip = "对元素的某些参数进行移除",
};
PushButtonData writeFacilityCode =
new(nameof(WriteFacilityCodeCmd), "设备编号", AddInPath, typeof(WriteFacilityCodeCmd).FullName)
{
//Image = ConvertFromBitmap(Resources.cmd_16px),
LargeImage = Resources.WriteFacilityCode_32px.ToBitmapSource(),
ToolTip = "设备编号提取、写入",
};
PushButtonData writeFacilityInfo =
new(nameof(FacilityInfoProcessCmd), "设备信息", AddInPath, typeof(FacilityInfoProcessCmd).FullName)
{
//Image = ConvertFromBitmap(Resources.cmd_16px),
LargeImage = Resources.Facility_Info_Process_32px.ToBitmapSource(),
ToolTip = "给设备构件批量添加信息",
LongDescription = "Excel需要包含设备、设备参数、部署范围三列,设备参数列的每个单元格中每行一个参数,参数表达例如“品牌:日立”"
};
PushButtonData afcaMetroManager =
new(nameof(AFCAMetroCmd), "轨交交付", AddInPath, typeof(AFCAMetroCmd).FullName)
{
LargeImage = Resources.AFCAMetro_32px.ToBitmapSource(),
ToolTip = "按轨道交通交付标准要求,添加、填写、修改属性",
AvailabilityClassName = typeof(OnActiveProjectDocument).FullName
};
standardPanel.AddSplitButton(new("土建工具", "土建工具"), geologyByLoop, resolveLevelPbd, levelSeparatePbd, splittingPbd, slopeFloorCreator, resolveConnection, addViewFilters);
standardPanel.AddSplitButton(new("房间功能", "房间功能"), room3D, roomNaming, roomNameAssign);
standardPanel.AddSplitButton(new("基点功能", "基点功能"), resetCoordinate, getCoordinate, moveModel);
standardPanel.AddItem(materialProcess);
standardPanel.AddSplitButton(new("重命名功能", "重命名功能"), renameType, renameFamily);
//standardPanel.AddItem(resolveConnection);
standardPanel.AddSplitButton(new("模型检查", "模型检查"), modelReviewPbd, modelCheckPbd, eamCheck);
standardPanel.AddSplitButton(new("布置工具", "布置工具"), trackCreator, dwgBlockModelCreator);
//standardPanel.AddItem(dwgBlockModelCreator);
//standardPanel.AddItem(trackCreator);
standardPanel.AddSplitButton(new("清理工具", "清理工具"), pureModel, setDisplay);
standardPanel.AddSplitButton(new("参数管理", "参数管理"), parameterManager, afcaMetroManager, eamManager, eamCodeManager, mappingManager, writeFacilityCode, writeFacilityInfo);
standardPanel.AddSplitButton(new("参数修改", "参数修改"), parameterWriter, relacePropValue, removeProps);
//standardPanel.AddSplitButton(new("交付管理", "交付管理"), afcaMetroManager, afcaArchiManager);
//standardPanel.AddItem(afcaMetroManager);
}
private void ComponentManager_UIElementActivated(object sender, UIElementActivatedEventArgs e)
{
if (e is { Item.Id: not null })
{
if (e.Item.Id == "ID_IAV_BTN") //e.Item.Id这里绑定按钮Id执行相应事件
{
SelectTheSameElementsAtCurrentView();
}
else if (e.Item.Id == "ID_IIP_BTN")
{
SelectTheSameElementsInTheProject();
}
else if (e.Item.Id == "ID_BS_BTN")
{
BoxSelectTheSameElemsAtCurrentView();
}
}
}
private void ControlledApplication_ApplicationInitialized(object sender, Autodesk.Revit.DB.Events.ApplicationInitializedEventArgs e)
{
InsertQuickAccessAndHidePanel();
//Properties.Settings.Default.TimerSubscribed = false;
//Properties.Settings.Default.Save();
var currentVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
var savedVersion = Properties.Settings.Default.LastVersion;
if (currentVersion != savedVersion)
{
// 显示更新内容提示
ShowUpdateMessage();
// 更新存储的版本信息
Properties.Settings.Default.LastVersion = currentVersion;
Properties.Settings.Default.Save();
}
}
private void CreateModifyUI()
{
var tab = ComponentManager.Ribbon.Tabs.First(t => t.Id == "Modify");
if (tab.Id == "Modify") //将按钮嵌入到修改选项卡上
{
Autodesk.Windows.RibbonPanel selElesPanel = new();
//{
// FloatingOrientation = System.Windows.Controls.Orientation.Vertical
//}
RibbonPanelSource selSource = new() { Id = "ID_SSI_Panel", Title = "选择" };
selElesPanel.Source = selSource;
RibbonButton iavBtn =
new()
{
Image = Resources.Select_View_16px.ToBitmapSource(),
//LargeImage = ConvertFromBitmap(Resources.Select_View_32px),
ShowImage = true,
//Size = RibbonItemSize.Large,
ShowText = true,
Id = "ID_IAV_BTN",
Name = "选择视图中全部实例",
Text = "选择视图中全部实例",
IsEnabled = true,
IsVisible = true,
IsCheckable = true
//Orientation = System.Windows.Controls.Orientation.Horizontal
};
RibbonButton iipBtn =
new()
{
Image = Resources.Select_Project_16px.ToBitmapSource(),
//LargeImage = ConvertFromBitmap(Resources.Select_32px),
ShowImage = true,
//Size = RibbonItemSize.Large,
ShowText = true,
Id = "ID_IIP_BTN",
//下拉显示名
Name = "选择项目中全部实例",
//选中显示名
Text = "选择项目中全部实例",
IsEnabled = true,
IsVisible = true,
IsCheckable = true
//Orientation = System.Windows.Controls.Orientation.Horizontal
};
RibbonButton rSelBtn =
new()
{
Image = Resources.Select_Selected_16px.ToBitmapSource(),
//LargeImage = ConvertFromBitmap(Resources.Select_Object_32px),
ShowImage = true,
//Size = RibbonItemSize.Large,
ShowText = true,
Id = "ID_BS_BTN",
//下拉显示名
Name = "选取当前选择相同实例",
//选中显示名
Text = "选取当前选择相同实例",
IsEnabled = true,
IsVisible = true,
IsCheckable = true
//Orientation = System.Windows.Controls.Orientation.Horizontal
};
//面板行布局
RibbonRowPanel ribbonRowPanel = new();
ribbonRowPanel.Items.Add(iavBtn);
//行打断=新建行
ribbonRowPanel.Items.Add(new RibbonRowBreak());
ribbonRowPanel.Items.Add(iipBtn);
ribbonRowPanel.Items.Add(new RibbonRowBreak());
ribbonRowPanel.Items.Add(rSelBtn);
//面板按钮
Autodesk.Windows.RibbonButton selFilterBtn =
new()
{
//Image = new System.Windows.Media.Imaging.BitmapImage(new Uri(icopath)),
Image = Resources.select_filter_16px.ToBitmapSource(),
LargeImage = Resources.select_filter_32px.ToBitmapSource(),
Size = RibbonItemSize.Large,
ShowText = true,
Id = "ID_Filter_BTN",
Name = "构件明细",
ToolTip = "查看选中的元素",
Text = "构件明细",
IsCheckable = true,
Orientation = System.Windows.Controls.Orientation.Vertical,
CommandHandler = new RelayCommand(
() => WinDialogAssists.ShowOrActivate(UiApplication),
() =>
UiApplication.ActiveUIDocument?.Selection
.GetElementIds()
.Any(id => UiApplication.ActiveUIDocument.Document.GetElement(id).GetTypeId() != ElementId.InvalidElementId && UiApplication.ActiveUIDocument.Document.GetElement(id).Category != null)
== true
)
};
Autodesk.Windows.RibbonRowPanel selPanel = new Autodesk.Windows.RibbonRowPanel();
selPanel.Items.Add(selFilterBtn);
selSource.Items.Add(selPanel);
selSource.Items.Add(ribbonRowPanel);
ComponentManager.UIElementActivated += ComponentManager_UIElementActivated;
tab.Panels.Add(selElesPanel);
}
}
private void CreateRibbonUI(UIControlledApplication application)
{
application.CreateRibbonTab(TabName);
var familyUtilPanel = application.CreateRibbonPanel(TabName, "族工具");
//在面板上添加添加浏览族库按钮
PushButtonData localFamilyPbd =
new(nameof(LocalFamsLibCmd), "本地族库", AddInPath, typeof(LocalFamsLibCmd).FullName) { LargeImage = Resources.Library_32px.ToBitmapSource() };
PushButtonData familyTransmitPbData =
new(nameof(TransmitFamilyCmd), "族传递", AddInPath, typeof(TransmitFamilyCmd).FullName)
{
Image = Resources.One_Way_Transition_16px.ToBitmapSource(),
LargeImage = Resources.One_Way_Transition_32px.ToBitmapSource(),
ToolTip = "族文件在项目中传递"
};
PushButtonData familyMassSavePbData =
new(nameof(MassSaveFamiliesCmd), "批量保存", AddInPath, typeof(MassSaveFamiliesCmd).FullName)
{
Image = Resources.Save_16px.ToBitmapSource(),
LargeImage = Resources.Save_32px.ToBitmapSource(),
ToolTip = "将项目中的族保存在本地磁盘"
};
PushButtonData pbdFamilyInfoExport =
new(nameof(ComponentInfoCmd), "批量导出", AddInPath, typeof(ComponentInfoCmd).FullName)
{
Image = Resources.Export_16px.ToBitmapSource(),
LargeImage = Resources.Export_32px.ToBitmapSource(),
ToolTip = "将族图片、族参数进行导出"
};
PushButtonData pbdFamilyReplace = new(nameof(ReplaceInstanceCmd), "实例替换", AddInPath, typeof(ReplaceInstanceCmd).FullName)
{
Image = Resources.replace_instance_16px.ToBitmapSource(),
LargeImage = Resources.replace_instance_32px.ToBitmapSource(),
ToolTip = "将点布置的族实例替换,并保留原实例的参数值到新的实例(若存在)"
};
PushButtonData pbdQueryExport = new(nameof(QueryExportCmd), "查询导出", AddInPath, typeof(QueryExportCmd).FullName)
{
Image = Resources.query_export_16px.ToBitmapSource(),
LargeImage = Resources.query_export_32px.ToBitmapSource(),
ToolTip = "根据查询族实例属性值,并导出数据或导出选中明细表"
};
familyUtilPanel.AddItem(localFamilyPbd);
familyUtilPanel.AddStackedItems(familyTransmitPbData, familyMassSavePbData, pbdFamilyInfoExport);
familyUtilPanel.AddStackedItems(pbdFamilyReplace, pbdQueryExport);
//常规
var standardPanel = application.CreateRibbonPanel(TabName, "常规");
CommonApp(standardPanel);
var decorationPanel = application.CreateRibbonPanel(TabName, "装修");
DecorationApp(decorationPanel);
var ribbonPanel = application.CreateRibbonPanel(TabName, "机电工具");
MEPApp(ribbonPanel);
//standardPanel.AddStackedItems(addViewFilters, resolveLevelPbd, splittingPbd);
//standardPanel.AddSeparator();
//standardPanel.AddStackedItems(resolveConnection, setDisplay);
var dimensionPanel = application.CreateRibbonPanel(TabName, "注释");
AnnotationApp(dimensionPanel);
var displayPanel = application.CreateRibbonPanel(TabName, "外观");
DisplayApp(displayPanel);
var generalPanel = application.CreateRibbonPanel(TabName, "其他");
PushButtonData compareProject =
new(nameof(CompareProjectDiffCmd), "文档比较", AddInPath, typeof(CompareProjectDiffCmd).FullName)
{
Image = Resources.compare_project_16px.ToBitmapSource(),
LargeImage = Resources.compare_project_32px.ToBitmapSource(),
ToolTip = "对比两个文档之间的构件差异",
AvailabilityClassName = typeof(OnActiveProjectDocument).FullName
};
PushButtonData elementsControlPbData =
new(nameof(ElementsControlCmd), "对象控制", AddInPath, typeof(ElementsControlCmd).FullName)
{
LargeImage = Resources.Control_Panel_32px.ToBitmapSource()
};
PushButtonData categoryControlPbData =
new(nameof(CategoryDisplayCmd), "类别显示", AddInPath, typeof(CategoryDisplayCmd).FullName)
{
LargeImage = Resources.Category_Displayer_32px.ToBitmapSource(),
AvailabilityClassName = typeof(OnActiveProjectDocument).FullName
};
PushButtonData autoSave =
new(nameof(AutoSaveDocumentCmd), "自动保存", AddInPath, typeof(AutoSaveDocumentCmd).FullName)
{
LargeImage = Resources.Document_Autosave_32px.ToBitmapSource(),
ToolTip = "自动保存文件"
};
PushButtonData csharpScriptDock =
new(nameof(ScriptRunnerCmd), "C#脚本执行", AddInPath, typeof(ScriptRunnerCmd).FullName)
{
Image = Resources.csharp_16px.ToBitmapSource(),
LargeImage = Resources.csharp_32px.ToBitmapSource(),
ToolTip = "执行C#脚本"
};
//PushButtonData csharpScript =
// new(nameof(OpenScriptPadCommand), "C#脚本执行", AddInPath, typeof(OpenScriptPadCommand).FullName)
// {
// LargeImage = Resources.Document_Autosave_32px.ToBitmapSource(),
// ToolTip = "执行C#脚本"
// };
PushButtonData releaseNote =
new(nameof(ReleaseNotesCmd), "更新内容", AddInPath, typeof(ReleaseNotesCmd).FullName)
{
LargeImage = Resources.ReleaseNote_32px.ToBitmapSource(),
ToolTip = $"当前版本:{Assembly.GetExecutingAssembly().GetName().Version}"
};
var tab = RibbonControl.Tabs.First(t => t.Name == TabName);
SetTabControl(tab);
//ElementsControlPBData.LargeImage = ConvertFromBitmap(Resources.Control_Panel_32px);
//ZoomRBGroup = CreateViewToggleButton(generalPanel);
CreateToggleButton(tab, "其他");
generalPanel.AddItem(compareProject);
generalPanel.AddSplitButton(new("显示控制", "显示控制"), categoryControlPbData, elementsControlPbData);
//generalPanel.AddItem(categoryControlPbData);
generalPanel.AddItem(autoSave);
//generalPanel.AddItem(csharpScript);
generalPanel.AddItem(csharpScriptDock);
generalPanel.AddItem(releaseNote);
}
private void CreateToggleButton(Autodesk.Windows.RibbonTab rt, string panelName)
{
try
{
var panel = rt.Panels.FirstOrDefault(p => p.Source.Name == panelName);
var tog = new Autodesk.Windows.RibbonToggleButton()
{
LargeImage = Resources.zoom_32px.ToBitmapSource(),
Size = RibbonItemSize.Large,
Name = "ZoomElement",
Text = "最大化",
ShowText = true,
ToolTip = "根据视图的图元是否可见,在切换视图时,对当前选中的图元进行快速缩放定位",
IsCheckable = true,
Orientation = System.Windows.Controls.Orientation.Vertical
};
tog.CheckStateChanged += (s, e) =>
{
zoomElementHandler.Raise(
uiapp =>
{
if (tog.CheckState == true)
{
uiapp.ViewActivated -= UiApp_ViewActivated;
uiapp.ViewActivated += UiApp_ViewActivated;
}
else
{
uiapp.ViewActivated -= UiApp_ViewActivated;
}
});
};
//Autodesk.Windows.RibbonItemControl
panel.Source.Items.Add(tog);
tog.IsChecked = true;
}
catch (Exception)
{
throw;
}
}
private static void DecorationApp(RibbonPanel decorationPanel)
{
PushButtonData floorFinishesPbd =
new(nameof(FloorFinishesCmd), "房间饰面", AddInPath, typeof(FloorFinishesCmd).FullName)
{
LargeImage = Resources.tites_32px.ToBitmapSource(),
ToolTip = "创建房间饰面"
};
PushButtonData curtainFinishesPbd =
new(nameof(BricksFinishesCmd), "铺贴饰面", AddInPath, typeof(BricksFinishesCmd).FullName)
{
LargeImage = Resources.finishes_32px.ToBitmapSource(),
ToolTip = "创建铺贴饰面"
};
PushButtonData wallFinishesPbd =
new(nameof(WallFinishesCmd), "墙饰面", AddInPath, typeof(WallFinishesCmd).FullName)
{
LargeImage = Resources.bricks_32px.ToBitmapSource(),
ToolTip = "创建墙饰面"
};
decorationPanel.AddItem(floorFinishesPbd);
decorationPanel.AddItem(curtainFinishesPbd);
decorationPanel.AddItem(wallFinishesPbd);
}
static BitmapSource GetEmbeddedImage(string name)
{
try
{
Assembly a = Assembly.GetExecutingAssembly();
using (var s = a.GetManifestResourceStream(name))
{
return BitmapFrame.Create(s);
}
}
catch
{
return null;
}
}
private static bool IsVisible(ElementId viewId, Element elem)
{
if (FilteredElementCollector.IsViewValidForElementIteration(elem.Document, viewId)) // 某类视图不能使用 FilteredElementCollector
{
var fec = new FilteredElementCollector(elem.Document, viewId).ToElementIds();
return fec.Any(id => id == elem.Id);
}
return false;
}
private void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
{
saveHandler.RaiseAsync(app =>
{
var doc = app.ActiveUIDocument.Document;
#if REVIT2018
if (doc.IsModified && !string.IsNullOrEmpty(doc.PathName))
#else
if (!doc.IsBackgroundCalculationInProgress() && doc.IsModified && !string.IsNullOrEmpty(doc.PathName))
#endif
{
try
{
doc.Save();
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
{
MessageBox.Show("已手动取消本次保存", "提示");
}
catch (Exception)
{
var command = RevitCommandId.LookupPostableCommandId(PostableCommand.Save);
app.PostCommand(command);
//MessageBox.Show("本次自动保存失败,将重新计时", "提示");
}
}
});
}
private static void RegisterDockPaneAndEvent(UIControlledApplication application)
{
ElementsControlDock pane = new();
application.RegisterDockablePane(GlobalVariables.PaneId, "对象控制菜单", pane);
}
private void RibbonToggleButton_CheckStateChanged(object sender, EventArgs e)
{
if (sender is RibbonToggleButton toggleButton)
{
var tabName = toggleButton.Name;
var tab = RibbonControl.FindTab(tabName);
tab.IsVisible = toggleButton.IsChecked;
}
}
///
/// 当前视图实例
///
private void SelectTheSameElementsAtCurrentView()
{
try
{
var uidoc = uiapp.ActiveUIDocument;
var doc = uidoc.Document;
var selectedTypes = uidoc.Selection.GetElementIds().Select(doc.GetElement).GroupBy(e => e.GetTypeId());
if (!selectedTypes.Any())
{
return;
}
ICollection setSelIds = [];
var col = new FilteredElementCollector(doc, uidoc.ActiveView.Id).WhereElementIsNotElementType();
foreach (var group in selectedTypes)
{
foreach (var item in col)
{
if (group.Key == item.GetTypeId() && item.Category != null)
{
setSelIds.Add(item.Id);
}
}
}
uidoc.Selection.SetElementIds(setSelIds);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
LogAssists.WriteLog($"{ex.Source}:{ex.StackTrace}");
}
}
///
/// 项目中实例
///
private void SelectTheSameElementsInTheProject()
{
try
{
var uidoc = uiapp.ActiveUIDocument;
var doc = uidoc.Document;
var selectedTypes = uidoc.Selection.GetElementIds().Select(id => doc.GetElement(id)).GroupBy(e => e.GetTypeId());
if (!selectedTypes.Any())
{
return;
}
ICollection setSelIds = [];
var col = new FilteredElementCollector(doc).WhereElementIsNotElementType();
foreach (var group in selectedTypes)
{
foreach (var item in col)
{
if (group.Key == item.GetTypeId() && item.Category != null)
{
setSelIds.Add(item.Id);
}
}
}
uidoc.Selection.SetElementIds(setSelIds);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
LogAssists.WriteLog(ex.StackTrace);
}
}
///
/// 插件管理
///
///
private void SetTabControl(RibbonTab customRibbonTab)
{
addInManagerPanel = new Autodesk.Windows.RibbonPanel { FloatingOrientation = System.Windows.Controls.Orientation.Horizontal };
//面板资源
RibbonPanelSource panelSource = new() { Id = "ID_AddIn_Panel", Title = "插件管理" };
//面板源设置
addInManagerPanel.Source = panelSource;
RibbonChecklistButton checklist =
new()
{
Id = "Id_AddIns_ListBtn",
Text = "插件管理",
Name = "插件管理",
Description = "控制插件在Revit界面上的是否显示",
Image = Resources.Addin_16px.ToBitmapSource(),
//LargeImage = ConvertFromBitmap(Resources.Select_View_16px),
ShowText = true,
ShowImage = true,
ToolTip = "控制插件在Revit界面上的是否显示",
IsToolTipEnabled = true,
IsVisible = true,
IsEnabled = true,
Size = RibbonItemSize.Standard,
AllowInToolBar = true,
AllowInStatusBar = true,
IsCheckable = true
};
var ribbon = ComponentManager.Ribbon;
foreach (var tab in ribbon.Tabs)
{
if (tab.GetType() == typeof(RibbonTab) && tab.Title != TabName)
{
RibbonToggleButton ribbonToggleButton =
new()
{
//Image = ConvertFromBitmap(Resources.Select_View_16px),
//LargeImage = ConvertFromBitmap(Resources.Select_View_32px),
//ShowImage = true,
Size = RibbonItemSize.Standard,
ShowText = true,
Id = $"Id_{tab.Title}_Tog",
Name = tab.Title,
Text = tab.Title,
IsEnabled = true,
IsVisible = true,
IsCheckable = true,
IsThreeState = false,
IsChecked = true
//CheckStateBinding = new System.Windows.Data.Binding()
//{
// Source = tab,
// Path = new PropertyPath(tab.IsVisible),
//},
//CommandHandler = new RelayCommand