using Autodesk.Revit.DB; using Autodesk.Revit.UI; using HStation.RevitDev.RevitDataExport.Common; using HStation.RevitDev.RevitDataExport.Service; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Windows; namespace HStation.RevitDev.RevitDataExport.Entity { public class ExternalEvent_CreateInstance : IExternalEventHandler { public string RfaPath { get; set; } = null; private bool m_isFirstTrigger = true; private UIApplication m_app = null; public void Execute(UIApplication app) { if (app == null || RfaPath == null || !File.Exists(RfaPath)) { return; } m_app = app; m_app.Idling += App_Idling; UIDocument uiDoc = app.ActiveUIDocument; Document doc = uiDoc.Document; Family family = null; FamilySymbol symbol = null; LoadFamilyFile(doc, ref family, ref symbol); if (family == null || symbol == null) { return; } PlacementFamilyInstance(uiDoc, symbol); } private void App_Idling(object sender, Autodesk.Revit.UI.Events.IdlingEventArgs e) { if (m_isFirstTrigger) { m_isFirstTrigger = false; } else { GlobalResource.PlacingType = Model.ModelEnum.RevitType.RFT_Unknown; m_app.Idling -= App_Idling; m_isFirstTrigger = true; } } private static void PlacementFamilyInstance(UIDocument uiDoc, FamilySymbol symbol) { if (uiDoc.CanPlaceElementType(symbol)) { try { uiDoc.PromptForFamilyInstancePlacement(symbol, new PromptForFamilyInstancePlacementOptions()); } catch (Autodesk.Revit.Exceptions.OperationCanceledException e) { return; } } else { MessageBox.Show("无法放置此类族!"); } } private void LoadFamilyFile(Document doc, ref Family family, ref FamilySymbol symbol) { using (Transaction trans = new Transaction(doc, "创建族实例")) { trans.Start(); try { if (!doc.LoadFamily(RfaPath, new ProjectLoadOption(), out family)) { //说明文档中已经存在, 需要搜索整个文档 var elems = SearchService.SearchElements(doc, typeof(Family), Path.GetFileNameWithoutExtension(RfaPath)); if (elems != null && elems.Count > 0) { family = elems[0] as Family; } } if (family == null) { trans.Commit(); return; } var symbolId = family.GetFamilySymbolIds().FirstOrDefault(); symbol = doc.GetElement(symbolId) as FamilySymbol; if (symbol == null) { trans.Commit(); return; } symbol.Activate(); } catch (Exception ex) { } trans.Commit(); } } public string GetName() { return "Create Family Instance"; } } public class ExternalEvent_CreatePipe : IExternalEventHandler { UIApplication m_app = null; bool m_isFirstTrigger = true; public void Execute(UIApplication app) { m_app = app; app.Idling += App_Idling; RevitCommandId id = RevitCommandId.LookupPostableCommandId(PostableCommand.Pipe); if (m_app.CanPostCommand(id)) { m_app.PostCommand(id); } } private void App_Idling(object sender, Autodesk.Revit.UI.Events.IdlingEventArgs e) { if (m_isFirstTrigger) { m_isFirstTrigger = false; } else { GlobalResource.PlacingType = Model.ModelEnum.RevitType.RFT_Unknown; m_app.Idling -= App_Idling; m_isFirstTrigger = true; } } public string GetName() { return "Create Pipe"; } } public class ExternalEvent_SelectElement : IExternalEventHandler { UIApplication m_uiapp = null; public static string m_id = null; public void Execute(UIApplication app) { m_uiapp = app; if (string.IsNullOrEmpty(m_id)) { return; } var id = new ElementId(int.Parse(m_id)); m_uiapp.ActiveUIDocument.Selection.SetElementIds(new List { id }); m_uiapp.ActiveUIDocument.ShowElements(id); } public string GetName() { return "Select Element"; } } public class ExternalEvent_Execute : IExternalEventHandler { public ExternalEvent_Execute(string name) { Name = name; } public string Name { get; private set; } public Action ExecuteAction { get; set; } public string GetName() { return Name; } public void Execute(UIApplication app) { if (ExecuteAction != null) { try { ExecuteAction(app); } catch { } } } } }