refactor: lazy database creation - match path planning pattern
- TryConnectToDatabase: createIfMissing=false (only connect, don't create) - New BeforeSaveAlias event + EnsureDatabaseCreated: creates DB on first save - Matches MainPlugin.TryConnectPathDatabase pattern
This commit is contained in:
parent
af7099d682
commit
4da6669707
@ -21,6 +21,8 @@ namespace NavisworksTransport.Core.AliasTree
|
||||
|
||||
var eh = new ElementHost { AutoSize = true };
|
||||
_control = new AliasTreeControl();
|
||||
_control.OnDatabaseNeeded += TryConnectToDatabase;
|
||||
_control.BeforeSaveAlias += EnsureDatabaseCreated;
|
||||
eh.Child = _control;
|
||||
eh.CreateControl();
|
||||
|
||||
@ -48,6 +50,8 @@ namespace NavisworksTransport.Core.AliasTree
|
||||
{
|
||||
base.OnLoaded();
|
||||
NavisApplication.ActiveDocumentChanged += OnDocumentChanged;
|
||||
// 面板自动恢复时文档可能已加载,主动尝试连接
|
||||
TryConnectToDatabase();
|
||||
}
|
||||
|
||||
protected override void OnUnloading()
|
||||
@ -71,9 +75,9 @@ namespace NavisworksTransport.Core.AliasTree
|
||||
var doc = NavisApplication.ActiveDocument;
|
||||
if (doc == null || string.IsNullOrEmpty(doc.FileName)) return;
|
||||
|
||||
// 强制初始化数据库(首次保存别名也会触发建库)
|
||||
// 只连接,不创建(和路径规划一致)
|
||||
var pathManager = PathPlanningManager.GetActivePathManager();
|
||||
pathManager?.DatabaseInitialize(createIfMissing: true);
|
||||
pathManager?.DatabaseInitialize(createIfMissing: false);
|
||||
|
||||
var db = pathManager?.GetPathDatabase();
|
||||
if (db?.Connection == null) return;
|
||||
@ -88,5 +92,16 @@ namespace NavisworksTransport.Core.AliasTree
|
||||
LogManager.Error($"[别名树] 连接数据库失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 确保数据库已创建(保存别名时调用)
|
||||
/// </summary>
|
||||
private void EnsureDatabaseCreated()
|
||||
{
|
||||
if (_control?.GetDataStore() != null) return;
|
||||
var pathManager = PathPlanningManager.GetActivePathManager();
|
||||
pathManager?.DatabaseInitialize(createIfMissing: true);
|
||||
TryConnectToDatabase();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -23,6 +23,11 @@ namespace NavisworksTransport.UI.WPF.Views
|
||||
// ============================================================
|
||||
|
||||
private AliasDataStore _dataStore;
|
||||
|
||||
/// <summary>当控件检测到模型就绪但数据存储未连接时触发</summary>
|
||||
internal event Action OnDatabaseNeeded;
|
||||
/// <summary>保存别名前触发(确保数据库已创建)</summary>
|
||||
internal event Action BeforeSaveAlias;
|
||||
private Dictionary<string, AliasNodeViewModel> _nodeMap = new Dictionary<string, AliasNodeViewModel>();
|
||||
private bool _isInternalSelection = false;
|
||||
private Autodesk.Navisworks.Api.ModelItem _currentSelectionItem;
|
||||
@ -44,6 +49,16 @@ namespace NavisworksTransport.UI.WPF.Views
|
||||
NavisApplication.ActiveDocumentChanged += (s, e) => { _built = false; RebuildTree(); };
|
||||
NavisApplication.ActiveDocumentChanging += (s, e) => { _built = false; };
|
||||
|
||||
// 模型加载事件:面板启动时文档已存在但模型未加载时触发
|
||||
var doc = NavisApplication.ActiveDocument;
|
||||
if (doc != null)
|
||||
doc.Models.CollectionChanged += (s, e) =>
|
||||
{
|
||||
_built = false;
|
||||
RebuildTree();
|
||||
OnDatabaseNeeded?.Invoke();
|
||||
};
|
||||
|
||||
HookSelectionEvents();
|
||||
|
||||
// 立即构建:面板打开时如果文档已存在,不走事件也要构建
|
||||
@ -638,6 +653,7 @@ namespace NavisworksTransport.UI.WPF.Views
|
||||
{
|
||||
if (e.Key == Key.Enter)
|
||||
{
|
||||
BeforeSaveAlias?.Invoke();
|
||||
if (_currentSelectionItem == null || _dataStore == null) return;
|
||||
string input = TxtAliasInput.Text?.Trim();
|
||||
if (string.IsNullOrEmpty(input)) return;
|
||||
@ -757,6 +773,7 @@ namespace NavisworksTransport.UI.WPF.Views
|
||||
string newAlias = textBox.Text?.Trim();
|
||||
if (string.IsNullOrEmpty(newAlias) || newAlias == node.OriginalName)
|
||||
newAlias = null;
|
||||
BeforeSaveAlias?.Invoke();
|
||||
LogManager.Info($"[别名树] 内联编辑: {newAlias}, NodeKey={node.NodeKey}");
|
||||
if (_dataStore != null)
|
||||
{
|
||||
|
||||
Loading…
Reference in New Issue
Block a user