From 15c422d76dea555e7f2ba789ca50f809629b96c3 Mon Sep 17 00:00:00 2001 From: tfarcraw <123211521@163.com> Date: Tue, 21 Jul 2026 12:26:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=85=8D=E6=96=B9bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PLCSharpWpf/Models/GlobalModel.Recipes.cs | 58 +++++++++++++++-------- PLCSharpWpf/Models/GlobalModel.cs | 1 + PLCSharpWpf/VVMs/Recipe/Recipe.cs | 7 +-- 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/PLCSharpWpf/Models/GlobalModel.Recipes.cs b/PLCSharpWpf/Models/GlobalModel.Recipes.cs index a84fa85..39d73fb 100644 --- a/PLCSharpWpf/Models/GlobalModel.Recipes.cs +++ b/PLCSharpWpf/Models/GlobalModel.Recipes.cs @@ -110,8 +110,9 @@ namespace PLCSharp.Models } else { - var camera = _DatasContext.Recipes.Where(c => c.ID == item.ID).FirstOrDefault(); - camera.Comment = item.Comment; + var recipe = _DatasContext.Recipes.Where(c => c.ID == item.ID).FirstOrDefault(); + recipe.Name = item.Name; + recipe.Comment = item.Comment; } } RecipeSelected.Prompt = ""; @@ -126,21 +127,36 @@ namespace PLCSharp.Models break; if (RecipeSelected.ID != CurrentRecipe.ID) { + var recipeId = RecipeSelected.ID; + + // 级联删除配方关联的所有数据 + _DatasContext.Variables.RemoveRange(_DatasContext.Variables.Where(v => v.RecipeID == recipeId)); + _DatasContext.SystemVariables.RemoveRange(_DatasContext.SystemVariables.Where(v => v.RecipeID == recipeId)); + _DatasContext.Workflows.RemoveRange(_DatasContext.Workflows.Where(w => w.RecipeID == recipeId)); + _DatasContext.ImageDatas.RemoveRange(_DatasContext.ImageDatas.Where(i => i.RecipeID == recipeId)); + _DatasContext.AxisPoints.RemoveRange(_DatasContext.AxisPoints.Where(a => a.RecipeID == recipeId)); + _DatasContext.Matrices.RemoveRange(_DatasContext.Matrices.Where(m => m.RecipeID == recipeId)); + _DatasContext.InterpolationGroups.RemoveRange(_DatasContext.InterpolationGroups.Where(g => g.RecipeID == recipeId)); + _DatasContext.RobotPoints.RemoveRange(_DatasContext.RobotPoints.Where(r => r.RecipeID == recipeId)); + _DatasContext.RobotMatrices.RemoveRange(_DatasContext.RobotMatrices.Where(m => m.RecipeID == recipeId)); + _DatasContext.CanvasConfigs.RemoveRange(_DatasContext.CanvasConfigs.Where(c => c.RecipeID == recipeId)); + _DatasContext.CustomControls.RemoveRange(_DatasContext.CustomControls.Where(c => c.RecipeID == recipeId)); + // VisionFunction 有关联的 LocalImageData(通过 FuncID),需先收集 ID 再级联删除 + var visionFuncIds = _DatasContext.VisionFunctions.Where(v => v.RecipeID == recipeId).Select(v => v.ID).ToList(); + _DatasContext.LocalImageDatas.RemoveRange(_DatasContext.LocalImageDatas.Where(l => visionFuncIds.Contains(l.FuncID))); + _DatasContext.VisionFunctions.RemoveRange(_DatasContext.VisionFunctions.Where(v => v.RecipeID == recipeId)); + + var recipe = _DatasContext.Recipes.FirstOrDefault(r => r.ID == recipeId); + if (recipe != null) { - var recipe = _DatasContext.Recipes.Where - (r => r.ID == RecipeSelected.ID).FirstOrDefault(); - - if (recipe != null) - { - _DatasContext.Recipes.Remove(recipe); - } - - var name = RecipeSelected.Name; - Recipes.Remove(RecipeSelected); - SendInfoDialog($"已删除:{name}"); - RecipeSelected = null; - _DatasContext.Save(); + _DatasContext.Recipes.Remove(recipe); } + + var name = RecipeSelected.Name; + Recipes.Remove(RecipeSelected); + RecipeSelected = null; + _DatasContext.Save(); + SendInfoDialog($"已删除配方:{name}"); } else { @@ -156,10 +172,16 @@ namespace PLCSharp.Models { item.Current = false; } + foreach (var item in Recipes) + { + item.Current = false; + } var current = _DatasContext.Recipes.Where(r => r.ID == RecipeSelected.ID).FirstOrDefault(); if (current != null) { current.Current = true; + var uiCurrent = Recipes.FirstOrDefault(r => r.ID == RecipeSelected.ID); + if (uiCurrent != null) uiCurrent.Current = true; if (current != CurrentRecipe) { CurrentRecipe = current; @@ -774,12 +796,6 @@ namespace PLCSharp.Models CustomControls.Add(item.DeepCopy()); } - var config = _DatasContext.CanvasConfigs.Where(c => c.RecipeID == CurrentRecipe.ID).FirstOrDefault(); - if (config != null) - { - CurrentCanvasConfig.Rows = config.Rows; - CurrentCanvasConfig.Columns = config.Columns; - } _EventAggregator.GetEvent().Publish(new() { diff --git a/PLCSharpWpf/Models/GlobalModel.cs b/PLCSharpWpf/Models/GlobalModel.cs index d4b253f..7e8f4ae 100644 --- a/PLCSharpWpf/Models/GlobalModel.cs +++ b/PLCSharpWpf/Models/GlobalModel.cs @@ -77,6 +77,7 @@ namespace PLCSharp.Models RecipeSelected = CurrentRecipe; ExecuteRecipeManage("Save"); } + RecipeSelected ??= CurrentRecipe; } VariablesModel = container.Resolve(); diff --git a/PLCSharpWpf/VVMs/Recipe/Recipe.cs b/PLCSharpWpf/VVMs/Recipe/Recipe.cs index 81a414a..87fa471 100644 --- a/PLCSharpWpf/VVMs/Recipe/Recipe.cs +++ b/PLCSharpWpf/VVMs/Recipe/Recipe.cs @@ -23,11 +23,10 @@ namespace PLCSharp.VVMs.Recipe get { return _Name; } set { - if (_Name != value) + if (SetProperty(ref _Name, value)) { Prompt = "已修改,请保存"; } - SetProperty(ref _Name, value); } } private string _Comment; @@ -35,16 +34,14 @@ namespace PLCSharp.VVMs.Recipe /// 备注 /// public string Comment - { get { return _Comment; } set { - if (_Comment != value) + if (SetProperty(ref _Comment, value)) { Prompt = "已修改,请保存"; } - SetProperty(ref _Comment, value); } }