using System; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Windows; using System.Windows.Interop; using Autodesk.Revit.DB; using Autodesk.Revit.DB.Architecture; using Autodesk.Revit.UI; using Microsoft.Win32; using Nice3point.Revit.Toolkit.External; namespace GeologyToolkit { [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)] [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)] internal class CreateBoreholeCmd : ExternalCommand { public override void Execute() { var view = SingletonViewUtil.GetInstance(out var isNewCreate); if (isNewCreate) { view.DataContext = new CreatorViewModel(); ShowAhead(view); } view.Activate(); } private static void ShowAhead(Window window) { _ = new WindowInteropHelper(window) { Owner = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle }; window.Show(); } } public sealed record SingletonViewUtil where T : Window, new() { private static T instance; private static readonly object padlock = new(); private SingletonViewUtil() { } public static T GetInstance(out bool isNewCreate) { isNewCreate = false; //double-check locking if (instance == null) { isNewCreate = true; lock (padlock) { instance ??= new T(); instance.Closed += OnWindowClosed; } } return instance; } private static void OnWindowClosed(object sender, EventArgs e) { instance = null; } } public class LoadFamilyOptions : IFamilyLoadOptions { //族已载入 public bool OnFamilyFound(bool familyInUse, out bool overwriteParameterValues) { overwriteParameterValues = true; return true; } //存在共享族 public bool OnSharedFamilyFound( Family sharedFamily, bool familyInUse, out FamilySource source, out bool overwriteParameterValues ) { source = FamilySource.Project; overwriteParameterValues = true; return true; } } }