Files
MsAddIns/QuickModeling/Models/Parameter.cs

128 lines
3.9 KiB
C#
Raw Normal View History

2026-02-28 21:01:57 +08:00
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using Bentley.DgnPlatformNET;
using Bentley.GeometryNET;
using CommunityToolkit.Mvvm.ComponentModel;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace QuickModeling.Models
{
/// <summary>
/// 项参数
/// </summary>
public partial class Parameter : ObservableValidator, ICloneable
{
/// <summary>
/// 对应CustomProperty的数据类型
/// </summary>
[ObservableProperty]
private TypeKind dataType;
/// <summary>
/// 对应ItemType
/// </summary>
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(FullName))]
private string group;
/// <summary>
/// 对应CustomProperty的名称
/// </summary>
[ObservableProperty]
[Required(ErrorMessage = "属性名称必填")]
[MinLength(1, ErrorMessage = "参数名称不能为空")]
[MaxLength(20, ErrorMessage = "参数名称不能超过20个字符")]
[NotifyDataErrorInfo]
[NotifyPropertyChangedFor(nameof(FullName))]
private string name;
/// <summary>
/// 对应CustomProperty的值
/// </summary>
[ObservableProperty]
[NotifyDataErrorInfo]
[CustomValidation(typeof(Parameter), nameof(ValidateValue))]
private object value;
///// <summary>
///// 工作单位
///// </summary>
//[ObservableProperty]
//private WorkingUnit unit = WorkingUnit.Unspecified;
/// <summary>
/// 对应CustomProperty的单位
/// </summary>
//[ObservableProperty]
//private DgnECUnit dgnUnit;
partial void OnDataTypeChanged(TypeKind kind)
{
if (string.IsNullOrEmpty(Value?.ToString()))
{
switch (kind)
{
case TypeKind.Boolean:
Value = false;
break;
case TypeKind.Double:
Value = 0.0;
break;
case TypeKind.Integer:
Value = 0;
break;
case TypeKind.String:
Value = string.Empty;
break;
}
}
}
public object Clone()
{
var copy = (Parameter)MemberwiseClone();
//copy.DgnUnit = new DgnECUnit(Unit);
return copy;
}
public static ValidationResult ValidateValue(string value, ValidationContext context)
{
if (string.IsNullOrEmpty(value))
{
return new ValidationResult("参数值为空");
}
Parameter instance = (Parameter)context.ObjectInstance;
switch (instance.DataType)
{
case TypeKind.Boolean:
if (bool.TryParse(value, out _))
{
return ValidationResult.Success;
}
break;
case TypeKind.Double:
if (double.TryParse(value, out _))
{
return ValidationResult.Success;
}
break;
case TypeKind.Integer:
if (int.TryParse(value, out _))
{
return ValidationResult.Success;
}
break;
case TypeKind.String:
// 字符串类型不需要额外验证
return ValidationResult.Success;
}
return new("参数值与值类型不匹配");
}
//public string Name { get => name; set => SetProperty(ref name, value, true); }
public string FullName => $"{Group}-{Name}";
}
}