From 2e03f3b617b4edee6610262189fca13dbce5a71e Mon Sep 17 00:00:00 2001 From: wanglei Date: Sat, 5 Dec 2026 22:31:22 +0800 Subject: [PATCH] Resolve camera and scoped vision image references in workflow scripts --- PLCSharpWpf/Models/WorkflowReferences.cs | 30 ++++++++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/PLCSharpWpf/Models/WorkflowReferences.cs b/PLCSharpWpf/Models/WorkflowReferences.cs index 2ebbbb3..abf059f 100644 --- a/PLCSharpWpf/Models/WorkflowReferences.cs +++ b/PLCSharpWpf/Models/WorkflowReferences.cs @@ -33,13 +33,17 @@ internal static class WorkflowReferences var targetType = kind switch { "Variables" => typeof(Variable), "SystemVariables" => typeof(SystemVariable), "VisionFunctions" => typeof(VisionFunction), "DI" => typeof(DI), "DQ" => typeof(DQ), "Axes" => typeof(Axis), "AxisPoints" => typeof(AxisPoint), "RobotPoints" => typeof(RobotPoint), "Cylinders" => typeof(Cylinder), "Robots" => typeof(Robot), "Connects" => typeof(Connect), + "Cameras" => typeof(PLCSharp.VVMs.Vision.Camera.CameraBase), "ImageDatas" => typeof(ImageData), "LocalImageDatas" => typeof(LocalImageData), "Workflows" => typeof(Workflow), _ => throw new ArgumentException("Unsupported reference target.") }; var target = ProjectInspection.Query(project, "get", new() { ["kind"] = kind, ["id"] = id }); - bool projectWide = kind is "Axes" or "DI" or "DQ" or "Cylinders" or "Robots" or "Connects"; + bool projectWide = kind is "Axes" or "DI" or "DQ" or "Cylinders" or "Robots" or "Connects" or "Cameras"; var recipeId = projectWide ? Guid.Empty : Guid.Parse((string)target["RecipeID"]); var name = (string)target["Name"]; var robotId = kind == "RobotPoints" ? Guid.Parse((string)target["RobotID"]) : Guid.Empty; var robots = kind == "RobotPoints" ? project.Data.Robots.Select(r => new { r.ID, r.Name }).ToList() : []; + var ownerId = kind == "LocalImageDatas" ? Guid.Parse((string)target["FuncID"]) : Guid.Empty; + var owners = kind == "LocalImageDatas" ? project.Data.VisionFunctions.Where(f => f.RecipeID == recipeId) + .Select(f => new { f.ID, f.Name }).ToList() : []; var hits = new List(); var diagnostics = new List(); foreach (var recipe in project.Recipes.Where(r => projectWide || r.ID == recipeId)) @@ -73,7 +77,18 @@ internal static class WorkflowReferences return matches.Length == 1 ? matches[0].ID == robotId : null; } - var lookup = kind switch { "Variables" => "GetVariable", "Workflows" => "GetTask", + bool? LocalOwner(ExpressionSyntax receiver) + { + if (receiver is not InvocationExpressionSyntax get || + model.GetSymbolInfo(get).Symbol is not IMethodSymbol { Name: nameof(GlobalModel.GetVisionFunction) } method || + method.ContainingType.ToDisplayString() != typeof(GlobalModel).FullName || + get.ArgumentList.Arguments.Count != 1 || + model.GetConstantValue(get.ArgumentList.Arguments[0].Expression) is not { HasValue: true, Value: string ownerName }) return null; + var matches = owners.Where(f => f.Name == ownerName).ToArray(); + return matches.Length == 1 ? matches[0].ID == ownerId : null; + } + + var lookup = kind switch { "ImageDatas" => nameof(GlobalModel.GetImageData), "Variables" => "GetVariable", "Workflows" => "GetTask", "Robots" => nameof(GlobalModel.GetRobot), "Connects" => nameof(GlobalModel.GetConnect), "VisionFunctions" => nameof(GlobalModel.GetVisionFunction), _ => null }; foreach (var call in root.DescendantNodes().OfType()) @@ -116,9 +131,9 @@ internal static class WorkflowReferences if (model.GetSymbolInfo(member).Symbol is IPropertySymbol property && property.Name == "Name" && property.ContainingType.ToDisplayString() == targetType.FullName) { - bool? bound = IsRecipePredicate(member, model, kind, IsTargetRobot); - if (bound != false || kind != "RobotPoints") - Record(value, comparison, bound == true || (!forRename && !projectWide && kind is not ("AxisPoints" or "RobotPoints" or "VisionFunctions"))); + bool? bound = IsRecipePredicate(member, model, kind, kind == "LocalImageDatas" ? LocalOwner : IsTargetRobot); + if (bound != false || kind is not ("RobotPoints" or "LocalImageDatas")) + Record(value, comparison, bound == true || (!forRename && !projectWide && kind is not ("AxisPoints" or "RobotPoints" or "VisionFunctions" or "ImageDatas" or "LocalImageDatas"))); } } } @@ -138,6 +153,11 @@ internal static class WorkflowReferences if (model.GetSymbolInfo(method.Expression).Symbol is not IPropertySymbol source) return null; return kind switch { + "Cameras" or "ImageDatas" when source.Name == kind && source.ContainingType.ToDisplayString() == typeof(VisionsModel).FullName => true, + "ImageDatas" when source.Name == kind && source.ContainingType.ToDisplayString() == typeof(VisionFunction).FullName => true, + "LocalImageDatas" when source.Name == kind && source.ContainingType.ToDisplayString() == typeof(VisionFunction).FullName && + method.Expression is MemberAccessExpressionSyntax collection => robotOwner(collection.Expression), + "LocalImageDatas" => null, "Cylinders" => source.Name == kind && source.ContainingType.ToDisplayString() == typeof(CylinderModel).FullName, "Robots" => source.Name == kind && source.ContainingType.ToDisplayString() == typeof(RobotModel).FullName, "Connects" => source.Name == kind && source.ContainingType.ToDisplayString() == typeof(ConnectsModel).FullName,