wenxiangchao
2026-11-02 9d1660270272180cd71147d6ddd53d1382643d2e
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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;
            string name = $"DUTSetting.{data.Name}";
 
            if (data.IsArray)
            {
                PropertyValueTypes type;
                if (data.Type == "Bool")
                {
                    type = PropertyValueTypes.PropValType_Boolean;
                }
                else if (data.Type == "Int")
                {
                    type = PropertyValueTypes.PropValType_Number;
                }
                else if (data.Type == "Double")
                {
                    type = PropertyValueTypes.PropValType_Number;
                }
                else
                {
                    type = PropertyValueTypes.PropValType_String;
                }
                string[] arr = data.Value?.Split(',') ?? new string[0];
                if (arr.Length > 0)
                {
                    context.FileGlobals.DeleteSubProperty(name, 2);
 
                    context.FileGlobals.NewSubProperty(name, type, true, null, 1);
                    context.FileGlobals.GetPropertyObject(name, 0).SetNumElements(arr.Length);
                    for (int i = 0; i < arr.Length; i++)
                    {
                        if (type == PropertyValueTypes.PropValType_String)
                        {
                            context.FileGlobals.SetValString($"{name}[{i}]", 0, arr[i]);
                        }
                        if (data.Type == "Int")
                        {
                            context.FileGlobals.SetValNumber($"{name}[{i}]", 0, int.Parse(arr[i]));
                        }
                        if (data.Type == "Double")
                        {
                            context.FileGlobals.SetValNumber($"{name}[{i}]", 0, double.Parse(arr[i]));
                        }
                        if (data.Type == "Bool")
                        {
                            context.FileGlobals.SetValBoolean($"{name}[{i}]", 0, bool.Parse(arr[i]));
                        }
                    }
                }
            }
            else
            {
                if (data.Type == "Bool")
                {
                    context.FileGlobals.SetValBoolean(name, 1, bool.Parse(data.Value));
                }
                else if (data.Type == "Int")
                {
                    context.FileGlobals.SetValNumber(name, 1, int.Parse(data.Value));
                }
                else if (data.Type == "Double")
                {
                    context.FileGlobals.SetValNumber(name, 1, double.Parse(data.Value));
                }
                else
                {
                    context.FileGlobals.SetValString(name, 1, data.Value);
                }
            }
 
 
        }
 
        private static string GetDataFilePath()
        {
            string path = ParameterUtil.GetCommonParameterValue("ShareConfig", out _);
            if (path == null)
            {
                path = Path.Combine(RegUtil.GetLocaltion(), "Config");
            }
            return path;
        }
 
        /// <summary>
        /// 根据产品型号写配置
        /// </summary>
        /// <param name="model">产品型号</param>
        /// <param name="instance">TestStand上下文</param>
        /// <param name="errMsg">异常信息</param>
        /// <returns>是否成功</returns>
        public static bool WriteConfigToContextByModel(string model, object instance, out string errMsg)
        {
            errMsg = string.Empty;
            try
            {
                string path = Path.Combine(GetDataFilePath(), "Product", "Parameter", $"Parameter_{model}.data");
                return WriteConfigToContextByPath(path, instance, out errMsg);
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                return false;
            }
        }
 
        /// <summary>
        /// 根据路径写配置
        /// </summary>
        /// <param name="path">绝对路径</param>
        /// <param name="instance">TestStand上下文</param>
        /// <param name="errMsg">异常信息</param>
        /// <returns>是否成功</returns>
        public static bool WriteConfigToContextByPath(string path, object instance, out string errMsg)
        {
            errMsg = string.Empty;
            try
            {
                if (!File.Exists(path))
                {
                    throw new Exception($"未找到该配置[{path}],请检查");
                }
                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;
            }
        }
    }
}