using Autodesk.Revit.ApplicationServices; using Autodesk.Revit.DB; using Autodesk.Revit.DB.Events; using Autodesk.Revit.UI; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; namespace HYDragCurveJig { public class JigCreator { public JigCreator(ExternalCommandData commandData) { this.externalCommandData_0 = commandData; this.uiapplication_0 = commandData.Application; this.uidocument_0 = this.uiapplication_0.ActiveUIDocument; this.application_0 = this.uiapplication_0.Application; this.document_0 = this.uidocument_0.Document; } ~JigCreator() { if (this.keyboardHook_0 != null) { this.keyboardHook_0.HookStop(); } } public Result Initialize(List curveInfoList, List textInfoList, XYZ ptBase, ref string message) { Result result = this.method_4(ref message); try { this.jigFamilyCreator_0 = new JigFamilyCreator(this.externalCommandData_0, this.document_1); result = this.jigFamilyCreator_0.CreateJIGFamily(curveInfoList, textInfoList, ptBase, ref message); if (result != null) { this.CloseDoc(); } else { result = this.method_6(); if (result != null) { this.CloseDoc(); } else { } } } catch (Exception ex) { message = ex.Message; this.method_7(); result = Result.Failed; } return result; } public bool Do(ref XYZ ptInsert) { if (this.document_1 == null) { this.method_7(); return false; } FamilySymbol familySymbol = null; if (!this.method_0(ref familySymbol)) { this.keyboardHook_0.HookStop(); this.method_7(); } Transaction transaction = null; Element element = null; try { ElementId elementId = null; if (!this.method_10(familySymbol, ref elementId)) { this.method_7(); transaction = new Transaction(this.document_0); transaction.Start("recover"); this.document_0.Delete(familySymbol.Family.Id); transaction.Commit(); return false; } element = this.document_0.GetElement(elementId); LocationPoint locationPoint = element.Location as LocationPoint; ptInsert = locationPoint.Point; } catch (Exception) { this.keyboardHook_0.HookStop(); this.method_7(); if (transaction != null) { transaction.RollBack(); } return false; } if (!this.method_1(element, familySymbol.Family)) { this.keyboardHook_0.HookStop(); this.method_7(); } this.method_7(); return true; } private bool method_0(ref FamilySymbol familySymbol_0) { Transaction transaction = null; bool flag; try { transaction = new Transaction(this.document_0); transaction.Start("drag"); this.uiapplication_0.ActiveUIDocument.Document.LoadFamily(this.string_0); familySymbol_0 = this.method_9(this.string_1, this.jigFamilyCreator_0.SymbolName); transaction.Commit(); flag = true; } catch (Exception) { if (transaction != null) { transaction.RollBack(); } flag = false; } return flag; } private bool method_1(Element element_0, Family family_0) { Transaction transaction = null; bool flag; try { transaction = new Transaction(this.document_0); transaction.Start("recover"); this.document_0.Delete(element_0.Id); this.document_0.Delete(family_0.Id); transaction.Commit(); flag = true; } catch (Exception) { if (transaction != null) { transaction.RollBack(); } flag = false; } return flag; } private bool method_2(ref string string_2) { string text = this.method_3(); string versionNumber = this.externalCommandData_0.Application.Application.VersionNumber; string_2 = string.Concat(new string[] { string_2, text, "\\Templates\\", versionNumber, "\\JIGFamilyTemplate.rft" }); if (!File.Exists(string_2)) { throw new Exception("Unsupported version"); } return true; } private string method_3() { string location = Assembly.GetExecutingAssembly().Location; int num = location.LastIndexOf("\\"); string text = location.Substring(0, num); num = text.LastIndexOf("\\"); return text.Substring(0, num); } private Result method_4(ref string string_2) { string text = string.Empty; try { if (!this.method_2(ref text)) { string_2 = "缺少族模板."; return Result.Failed; } } catch (Exception ex) { string_2 = ex.Message; return Result.Failed; } this.document_1 = this.application_0.NewFamilyDocument(text); if (this.document_1 == null) { string_2 = "缺少族模板."; return Result.Failed; } return 0; } private void CloseDoc() { this.document_1.Close(false); } private Result method_6() { string tempPath = Path.GetTempPath(); this.string_1 = "HYJIG"; this.string_0 = tempPath + this.string_1 + ".rfa"; if (File.Exists(this.string_0)) { File.Delete(this.string_0); } SaveAsOptions saveAsOptions = new SaveAsOptions(); saveAsOptions.OverwriteExistingFile = true; document_1.SaveAs(this.string_0, saveAsOptions); this.document_1.Close(true); return 0; } private void method_7() { this.method_8(this.string_0); string tempPath = Path.GetTempPath(); foreach (string text in Directory.GetFiles(tempPath, "*.rfa", SearchOption.TopDirectoryOnly)) { if (text.IndexOf("HYJIG") != -1) { this.method_8(tempPath + Path.GetFileName(text)); } } } private bool method_8(string string_2) { bool flag; try { if (File.Exists(string_2)) { File.Delete(string_2); flag = true; } else { flag = false; } } catch (Exception) { flag = false; } return flag; } private FamilySymbol method_9(string string_2, string string_3) { FamilySymbol familySymbol = null; foreach (Element element in new FilteredElementCollector(this.document_0).OfClass(typeof(Family)).ToElements()) { Family family = element as Family; if (family != null) { Category familyCategory = family.Category; if (familyCategory != null && string_2.ToUpper().CompareTo(family.Name.ToUpper()) == 0) { foreach (FamilySymbol familySymbol2 in family.GetFamilySymbolIds().Select(id => document_0.GetElement(id))) { if (string_3.ToUpper().CompareTo(familySymbol2.Name.ToUpper()) == 0) { return familySymbol2; } } } } } return familySymbol; } private bool method_10(FamilySymbol familySymbol_0, ref ElementId elementId_0) { this.list_0.Clear(); this.method_11(); this.keyboardHook_0.HookStart(); if (this.uiapplication_0.ActiveUIDocument == null) { this.method_12(); this.keyboardHook_0.HookStop(); return false; } if (!this.eadSssotM(familySymbol_0)) { this.method_12(); this.keyboardHook_0.HookStop(); return false; } this.method_12(); if (this.list_0.Count == 0) { this.keyboardHook_0.HookStop(); return false; } elementId_0 = this.list_0[0]; this.keyboardHook_0.HookStop(); return true; } private bool eadSssotM(FamilySymbol familySymbol_0) { bool flag; try { this.uiapplication_0.ActiveUIDocument.PromptForFamilyInstancePlacement(familySymbol_0); flag = true; } catch (Autodesk.Revit.Exceptions.OperationCanceledException) { flag = true; } catch (Exception ex) { string message = ex.Message; this.method_12(); this.keyboardHook_0.HookStop(); flag = false; } return flag; } private void method_11() { this.application_0.DocumentChanged += this.method_13; this.application_0.DocumentClosing += this.method_14; this.application_0.DocumentCreating += this.method_15; this.application_0.DocumentOpening += this.method_16; } private void method_12() { this.application_0.DocumentChanged -= this.method_13; this.application_0.DocumentClosing -= this.method_14; this.application_0.DocumentCreating -= this.method_15; this.application_0.DocumentOpening -= this.method_16; } private void method_13(object sender, DocumentChangedEventArgs e) { if (e.GetAddedElementIds().Count == 1) { this.list_0.AddRange(e.GetAddedElementIds()); this.keyboardHook_0.HookStop(); this.method_17(); this.method_17(); } } private void method_14(object sender, DocumentClosingEventArgs e) { this.keyboardHook_0.HookStop(); this.method_17(); this.method_17(); } private void method_15(object sender, DocumentCreatingEventArgs e) { this.keyboardHook_0.HookStop(); this.method_17(); this.method_17(); } private void method_16(object sender, DocumentOpeningEventArgs e) { this.keyboardHook_0.HookStop(); this.method_17(); this.method_17(); } [DllImport("user32.dll")] private static extern void keybd_event(byte byte_0, byte byte_1, int int_0, int int_1); private void method_17() { byte b = 27; JigCreator.keybd_event(27, 0, 0, 0); JigCreator.keybd_event(b, 0, 2, 0); } private ExternalCommandData externalCommandData_0; private UIApplication uiapplication_0; private UIDocument uidocument_0; private Application application_0; private Document document_0; private Document document_1; private JigFamilyCreator jigFamilyCreator_0; private List list_0 = new List(); private string string_0 = string.Empty; private string string_1 = string.Empty; private KeyboardHook keyboardHook_0 = new KeyboardHook(); } }