alielie
2025-06-12 c1c3772a11b1f3fbad8846cc714120771d85fa53
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using NationalInstruments.TestStand.Interop.API;
using System;
using System.Collections.Generic;
using System.IO;
 
namespace HMI.Simple.Util
{
    public class ProductUtil
    {
        private static void WriteParameterData(object instance, ParameterData data)
        {
            SequenceContext context = (SequenceContext)instance;
            if (data.Type == "Bool")
            {
                context.FileGlobals.SetValBoolean($"DUTSetting.{data.Name}", 1, bool.Parse(data.Value));
            }
            else if (data.Type == "Int")
            {
                context.FileGlobals.SetValInteger64($"DUTSetting.{data.Name}", 1, int.Parse(data.Value));
            }
            else if (data.Type == "Double")
            {
                context.FileGlobals.SetValNumber($"DUTSetting.{data.Name}", 1, double.Parse(data.Value));
            }
            else
            {
                context.FileGlobals.SetValString($"DUTSetting.{data.Name}", 1, data.Value);
            }
        }
 
        public static bool WriteConfigToContextByModel(string model, object instance, out string errMsg)
        {
            errMsg = string.Empty;
            try
            {
                string dir = RegUtil.GetLocaltion();
                string path = Path.Combine(dir, "Config", "Product", $"Parameter_{model}.data");
                return WriteConfigToContextByPath(path, instance, out errMsg);
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                return false;
            }
        }
 
        public static bool WriteConfigToContextByPath(string path, object instance, out string errMsg)
        {
            errMsg = string.Empty;
            try
            {
                if (!File.Exists(path))
                {
                    throw new Exception("未找到该配置,请检查");
                }
                List<ParameterData> datas = XmlHelper.DeserializeFromXml<List<ParameterData>>(path);
                foreach (var data in datas)
                {
                    WriteParameterData(instance, data);
                }
                return true;
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                return false;
            }
        }
    }
}