控制器:型号选定后下拉框只读,新增删除按钮

- Controllers.xaml: 型号 ComboBox 默认禁用,仅 Type=Undefined 时可选;新增删除按钮
- Controller: Type setter 允许重置为 Undefined;后台循环加 SDK null 保护;新增 Reset()(停轴→关SDK→置null→清Axes/DI/DQ→复位状态)
- ControllersModel: ControllerManage 新增 Delete 命令(确认框→Reset→清运行时集合与DB全部相关表→型号重置为Undefined)
This commit is contained in:
tfarcraw 2026-10-16 03:25:14 +08:00
parent 45f505cec3
commit 9085c9b178
3 changed files with 85 additions and 1 deletions

View File

@ -54,6 +54,11 @@ namespace PLCSharp.VVMs.MotionController
SetProperty(ref _Type, value);
}
}
else if (value == ControllerType.Undefined)
{
// 允许重置为Undefined(删除控制器)
SetProperty(ref _Type, value);
}
else if (_Type != value)
{
MessageBox.Show("选定型号后不可改变!");
@ -147,6 +152,12 @@ namespace PLCSharp.VVMs.MotionController
}
}
if (SDK == null)
{
Thread.Sleep(1000);
continue;
}
if (Connected == false)
{
Thread.Sleep(1000);
@ -469,6 +480,29 @@ namespace PLCSharp.VVMs.MotionController
SDK?.Close();
}
/// <summary>
/// 删除控制器:停轴、关闭SDK、复位型号为Undefined并清空运行时数据
/// </summary>
public void Reset()
{
foreach (var axis in Axes)
{
axis.Stop();
}
Thread.Sleep(100);
Type = ControllerType.Undefined;
Close();
SDK = null;
Connected = false;
Online = false;
Prompt = "";
Axes.Clear();
DI.Clear();
DQ.Clear();
}
/// <summary>
/// 初始化
/// </summary>

View File

@ -55,7 +55,18 @@
<TextBlock Grid.Row="0" Grid.Column="0" Margin="6" Text="型号" />
<ComboBox Grid.Row="0" Grid.Column="1" Margin="6"
ItemsSource="{Binding Source={StaticResource ControllerType}}"
SelectedItem="{Binding ControllerModel.Controller.Type, UpdateSourceTrigger=PropertyChanged}" />
SelectedItem="{Binding ControllerModel.Controller.Type, UpdateSourceTrigger=PropertyChanged}">
<ComboBox.Style>
<Style TargetType="ComboBox">
<Setter Property="IsEnabled" Value="False" />
<Style.Triggers>
<DataTrigger Binding="{Binding ControllerModel.Controller.Type}" Value="{x:Static local:ControllerType.Undefined}">
<Setter Property="IsEnabled" Value="True" />
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
<TextBlock Grid.Row="1" Grid.Column="0" Margin="6" Text="IP" />
<TextBox Grid.Row="1" Grid.Column="1" Margin="6" Text="{Binding ControllerModel.Controller.IP, UpdateSourceTrigger=LostFocus}" />
<TextBlock Grid.Row="2" Grid.Column="0" Margin="6" Text="备注" />
@ -67,6 +78,7 @@
<TextBlock Margin="0,0,12,0" Text="连接" />
<Button Margin="12,0,0,0" Padding="8,2" Content="保存" Command="{Binding ControllerModel.ControllerManage}" CommandParameter="Save" />
<Button Margin="6,0,0,0" Padding="8,2" Content="配置" Command="{Binding ControllerModel.ControllerManage}" CommandParameter="Config" />
<Button Margin="6,0,0,0" Padding="8,2" Content="删除" Command="{Binding ControllerModel.ControllerManage}" CommandParameter="Delete" />
</StackPanel>
<TextBlock Grid.Row="4" Grid.Column="1" Margin="6" Text="{Binding ControllerModel.Controller.Prompt}" />
</Grid>

View File

@ -433,6 +433,44 @@ namespace PLCSharp.VVMs.MotionController
{
switch (cmd)
{
case "Delete":
if (Controller == null || Controller.Type == ControllerType.Undefined)
{
SendInfoDialog("当前没有已配置的控制器");
break;
}
if (System.Windows.MessageBox.Show("确认删除当前控制器?\n将同时删除所有轴、IO、点位、矩阵和插补配置!", "确认删除", System.Windows.MessageBoxButton.YesNo, System.Windows.MessageBoxImage.Warning) != System.Windows.MessageBoxResult.Yes)
break;
Controller.Reset();
// 清理运行时集合
Axes.Clear();
DI.Clear();
DQ.Clear();
AxisPoints.Clear();
Matrices.Clear();
InterpolationGroups.Clear();
SelectedAxis = null;
SelectedAxisPoint = null;
SelectedMatrix = null;
SelectedInterpolationGroup = null;
ControllerConfig = null;
AxisConfig = null;
// 清理数据库
_DatasContext.Controllers.RemoveRange(_DatasContext.Controllers.ToList());
_DatasContext.Axes.RemoveRange(_DatasContext.Axes.ToList());
_DatasContext.DI.RemoveRange(_DatasContext.DI.ToList());
_DatasContext.DQ.RemoveRange(_DatasContext.DQ.ToList());
_DatasContext.AxisPoints.RemoveRange(_DatasContext.AxisPoints.ToList());
_DatasContext.Matrices.RemoveRange(_DatasContext.Matrices.ToList());
_DatasContext.InterpolationGroups.RemoveRange(_DatasContext.InterpolationGroups.ToList());
_DatasContext.Save();
SendInfoDialog("控制器已删除,型号已重置为Undefined");
break;
case "Save":
if (!_DatasContext.Controllers.Any(h => h.ID == Controller.ID))
{