修改自动壁厚参数的自动计算
This commit is contained in:
parent
833b941b1c
commit
2ec899c282
@ -23,6 +23,15 @@ namespace CadParamPluging.UI
|
||||
private readonly RichTextBox _rtbNoteRendered;
|
||||
private Dictionary<int, NoteTemplateEngine.PlaceholderOccurrence> _occByIndex;
|
||||
|
||||
// 最小壁厚T自动计算相关
|
||||
private bool _canAutoCalcMinWallThickness;
|
||||
private Control _ctrlMinWallThickness;
|
||||
private const string KeyOuterDiameter1 = "OuterDiameter1";
|
||||
private const string KeyOuterDiameter1TolMinus = "OuterDiameter1TolMinus";
|
||||
private const string KeyInnerDiameter2 = "InnerDiameter2";
|
||||
private const string KeyInnerDiameter2TolPlus = "InnerDiameter2TolPlus";
|
||||
private const string KeyMinWallThickness = "MinWallThickness";
|
||||
|
||||
public ParamBag Result { get; private set; }
|
||||
|
||||
public DrawingParamsForm(ParamCatalog catalog, TemplateSchemaDefinition schema)
|
||||
@ -130,6 +139,7 @@ namespace CadParamPluging.UI
|
||||
split.Panel2MinSize = 280;
|
||||
EnsureValidSplitterDistance(split);
|
||||
UpdateNotePreviews();
|
||||
CheckMinWallThicknessAutoCalc();
|
||||
};
|
||||
}
|
||||
|
||||
@ -385,6 +395,7 @@ namespace CadParamPluging.UI
|
||||
_defsByControlKey[controlKey] = def;
|
||||
|
||||
AttachPreviewUpdate(ctrl);
|
||||
AttachMinWallThicknessUpdate(controlKey, ctrl);
|
||||
}
|
||||
|
||||
private void AttachPreviewUpdate(Control ctrl)
|
||||
@ -419,6 +430,137 @@ namespace CadParamPluging.UI
|
||||
}
|
||||
}
|
||||
|
||||
private void AttachMinWallThicknessUpdate(string paramKey, Control ctrl)
|
||||
{
|
||||
if (ctrl == null || string.IsNullOrWhiteSpace(paramKey))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var key = paramKey.Trim();
|
||||
if (!string.Equals(key, KeyOuterDiameter1, StringComparison.OrdinalIgnoreCase)
|
||||
&& !string.Equals(key, KeyOuterDiameter1TolMinus, StringComparison.OrdinalIgnoreCase)
|
||||
&& !string.Equals(key, KeyInnerDiameter2, StringComparison.OrdinalIgnoreCase)
|
||||
&& !string.Equals(key, KeyInnerDiameter2TolPlus, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (ctrl is NumericUpDown num)
|
||||
{
|
||||
num.ValueChanged += (_, __) => UpdateMinWallThickness();
|
||||
}
|
||||
else if (ctrl is TextBox tb)
|
||||
{
|
||||
tb.TextChanged += (_, __) => UpdateMinWallThickness();
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckMinWallThicknessAutoCalc()
|
||||
{
|
||||
_canAutoCalcMinWallThickness = false;
|
||||
|
||||
if (!_controlsByKey.ContainsKey(KeyMinWallThickness))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_ctrlMinWallThickness = _controlsByKey[KeyMinWallThickness];
|
||||
|
||||
var hasOuter = _controlsByKey.ContainsKey(KeyOuterDiameter1);
|
||||
var hasOuterTol = _controlsByKey.ContainsKey(KeyOuterDiameter1TolMinus);
|
||||
var hasInner = _controlsByKey.ContainsKey(KeyInnerDiameter2);
|
||||
var hasInnerTol = _controlsByKey.ContainsKey(KeyInnerDiameter2TolPlus);
|
||||
|
||||
if (hasOuter && hasOuterTol && hasInner && hasInnerTol)
|
||||
{
|
||||
_canAutoCalcMinWallThickness = true;
|
||||
UpdateMinWallThickness();
|
||||
|
||||
if (_ctrlMinWallThickness != null)
|
||||
{
|
||||
_toolTip.SetToolTip(_ctrlMinWallThickness, "自动计算: T=(Φ1-b1+Φ2-a2)/2\n(根据外径Φ1、外径下差b1、内径Φ2、内径上差a2)");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var missing = new List<string>();
|
||||
if (!hasOuter) missing.Add("外径Φ1");
|
||||
if (!hasOuterTol) missing.Add("外径下差b1");
|
||||
if (!hasInner) missing.Add("内径Φ2");
|
||||
if (!hasInnerTol) missing.Add("内径上差a2");
|
||||
|
||||
if (_ctrlMinWallThickness != null && missing.Count > 0)
|
||||
{
|
||||
var msg = string.Format("需人工填写(缺少参数: {0})", string.Join(", ", missing));
|
||||
_toolTip.SetToolTip(_ctrlMinWallThickness, msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateMinWallThickness()
|
||||
{
|
||||
if (!_canAutoCalcMinWallThickness || _ctrlMinWallThickness == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var phi1 = GetNumericValue(KeyOuterDiameter1);
|
||||
var b1 = GetNumericValue(KeyOuterDiameter1TolMinus);
|
||||
var phi2 = GetNumericValue(KeyInnerDiameter2);
|
||||
var a2 = GetNumericValue(KeyInnerDiameter2TolPlus);
|
||||
|
||||
// T = (Φ1 - b1 + Φ2 - a2) / 2
|
||||
// 注意:b1通常为负数(如-0.5),a2通常为正数(如0.5)
|
||||
// 实际最小壁厚 = ((外径最小) - (内径最大)) / 2
|
||||
// 外径最小 = Φ1 + b1,内径最大 = Φ2 + a2
|
||||
// 最小壁厚 = (Φ1 + b1 - Φ2 - a2) / 2
|
||||
// 但用户公式为 T=(Φ1-b1+Φ2-a2)/2,按此公式计算
|
||||
var t = (phi1 - b1 + phi2 - a2) / 2.0;
|
||||
|
||||
if (_ctrlMinWallThickness is NumericUpDown num)
|
||||
{
|
||||
var val = (decimal)t;
|
||||
if (val < num.Minimum) val = num.Minimum;
|
||||
if (val > num.Maximum) val = num.Maximum;
|
||||
num.Value = val;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignore calculation errors
|
||||
}
|
||||
}
|
||||
|
||||
private double GetNumericValue(string key)
|
||||
{
|
||||
if (!_controlsByKey.TryGetValue(key, out var ctrl) || ctrl == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ctrl is NumericUpDown num)
|
||||
{
|
||||
return (double)num.Value;
|
||||
}
|
||||
|
||||
if (ctrl is TextBox tb)
|
||||
{
|
||||
if (double.TryParse(tb.Text, NumberStyles.Float, CultureInfo.InvariantCulture, out var v))
|
||||
{
|
||||
return v;
|
||||
}
|
||||
if (double.TryParse(tb.Text, NumberStyles.Float, CultureInfo.CurrentCulture, out v))
|
||||
{
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private void UpdateNotePreviews()
|
||||
{
|
||||
if (_schema == null)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user