Resolve camera and scoped vision image references in workflow scripts

This commit is contained in:
wanglei 2026-12-05 22:31:22 +08:00
parent 30e669fccb
commit 2e03f3b617

View File

@ -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<object>();
var diagnostics = new List<object>();
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<InvocationExpressionSyntax>())
@ -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,