WPF实现电机选型功能
开发这样一个选型工具的原因有几点:
1、工作需要,虽然是开发电机驱动的工程师,但是在很多情况下需要了解和知道电机选型,如果糊里糊涂的拿个电机就上机器,很有可能电机参数调好了,可就是在机器上不能正常工作;
2、对一些新的工具的探索,该工具使用了WPFMath工具,可以很好的显示我需要显示的Latex公式,这样就可以很直观的显示计算过程,虽然还是有缺点(无法在公式后面显示单位,可能是本人使用方式不太对),个人喜欢将公式直接显示出来的效果,哈哈;
3、由于时间较短,目前只开发了丝杆无传动机构部分的计算和选型,基本实现选型功能,整体框架基本搭好;
4、关于参考和图片,主要参考https://www.orientalmotor.com.cn/guide/motorsizingtool/
5、关于源码,我这里会直接将源码贴出,仅作参考使用,暂不直接上传工程;
主页面UI(View)
<Window x:Class="MotorSelectionTools.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MotorSelectionTools"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
mc:Ignorable="d"
ResizeMode="NoResize"
WindowStyle="ThreeDBorderWindow"
Background="Transparent"
WindowStartupLocation="CenterScreen"
Title="YJH Motor Selection Tools V1.0.0" Height="700" Width="1200">
<Window.Resources>
<ResourceDictionary>
<Style x:Key="TabControlWithUnderLineStyle" TargetType="{x:Type TabControl}">
<Setter Property="Padding" Value="2" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Background" Value="LightGray" />
<Setter Property="BorderBrush" Value="#FFACACAC" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabControl}">
<Grid
x:Name="templateRoot"
ClipToBounds="True"
KeyboardNavigation.TabNavigation="Local"
SnapsToDevicePixels="True">
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="ColumnDefinition0" />
<ColumnDefinition x:Name="ColumnDefinition1" Width="0" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition x:Name="RowDefinition0" Height="Auto" />
<RowDefinition x:Name="RowDefinition1" Height="*" />
</Grid.RowDefinitions>
<TabPanel
x:Name="HeaderPanel"
Grid.Row="0"
Grid.Column="0"
Margin="0"
HorizontalAlignment="Left"
Panel.ZIndex="1"
Background="White"
IsItemsHost="True"
KeyboardNavigation.TabIndex="1" />
<Line
Margin="0,0,0,1"
VerticalAlignment="Bottom"
SnapsToDevicePixels="True"
Stroke="Black"
StrokeThickness="0.1"
X1="0"
X2="{Binding ActualWidth, RelativeSource={RelativeSource Self}}" />
<Border
x:Name="ContentPanel"
Grid.Row="1"
Grid.Column="0"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
KeyboardNavigation.DirectionalNavigation="Contained"
KeyboardNavigation.TabIndex="2"
KeyboardNavigation.TabNavigation="Local">
<ContentPresenter
x:Name="PART_SelectedContentHost"
Margin="0"
Content="{TemplateBinding SelectedContent}"
ContentSource="SelectedContent"
ContentStringFormat="{TemplateBinding SelectedContentStringFormat}"
ContentTemplate="{TemplateBinding SelectedContentTemplate}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="TabStripPlacement" Value="Bottom">
<Setter TargetName="HeaderPanel" Property="Grid.Row" Value="1" />
<Setter TargetName="ContentPanel" Property="Grid.Row" Value="0" />
<Setter TargetName="RowDefinition0" Property="Height" Value="*" />
<Setter TargetName="RowDefinition1" Property="Height" Value="Auto" />
</Trigger>
<Trigger Property="TabStripPlacement" Value="Left">
<Setter TargetName="HeaderPanel" Property="Grid.Row" Value="0" />
<Setter TargetName="ContentPanel" Property="Grid.Row" Value="0" />
<Setter TargetName="HeaderPanel" Property="Grid.Column" Value="0" />
<Setter TargetName="ContentPanel" Property="Grid.Column" Value="1" />
<Setter TargetName="ColumnDefinition0" Property="Width" Value="Auto" />
<Setter TargetName="ColumnDefinition1" Property="Width" Value="*" />
<Setter TargetName="RowDefinition0" Property="Height" Value="*" />
<Setter TargetName="RowDefinition1" Property="Height" Value="0" />
</Trigger>
<Trigger Property="TabStripPlacement" Value="Right">
<Setter TargetName="HeaderPanel" Property="Grid.Row" Value="0" />
<Setter TargetName="ContentPanel" Property="Grid.Row" Value="0" />
<Setter TargetName="HeaderPanel" Property="Grid.Column" Value="1" />
<Setter TargetName="ContentPanel" Property="Grid.Column" Value="0" />
<Setter TargetName="ColumnDefinition0" Property="Width" Value="*" />
<Setter TargetName="ColumnDefinition1" Property="Width" Value="Auto" />
<Setter TargetName="RowDefinition0" Property="Height" Value="*" />
<Setter TargetName="RowDefinition1" Property="Height" Value="0" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="templateRoot" Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="TabItemExWithUnderLineStyle" TargetType="{x:Type TabItem}">
<Setter Property="Foreground" Value="Black" />
<Setter Property="Width" Value="100" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="#FFACACAC" />
<Setter Property="Margin" Value="0" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid
x:Name="templateRoot"
Background="Transparent"
SnapsToDevicePixels="True">
<Border
x:Name="_underline"
Margin="{TemplateBinding Margin}"
BorderBrush="#002870"
BorderThickness="0" />
<Grid>
<TextBlock
x:Name="txt"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Foreground="{TemplateBinding Foreground}"
Text="{TemplateBinding Header}"
TextTrimming="CharacterEllipsis"
ToolTip="{TemplateBinding Header}"
Visibility="Visible" />
</Grid>
</Grid>
<ControlTemplate.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="true" />
<Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Top" />
</MultiDataTrigger.Conditions>
<Setter TargetName="txt" Property="Foreground" Value="Black" />
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false" />
<Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Left" />
</MultiDataTrigger.Conditions>
<Setter TargetName="templateRoot" Property="Opacity" Value="0.56" />
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false" />
<Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Bottom" />
</MultiDataTrigger.Conditions>
<Setter TargetName="templateRoot" Property="Opacity" Value="0.56" />
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false" />
<Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Right" />
</MultiDataTrigger.Conditions>
<Setter TargetName="templateRoot" Property="Opacity" Value="0.56" />
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false" />
<Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Top" />
</MultiDataTrigger.Conditions>
<Setter TargetName="templateRoot" Property="Opacity" Value="0.56" />
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="true" />
<Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Top" />
</MultiDataTrigger.Conditions>
<Setter Property="Panel.ZIndex" Value="1" />
<Setter TargetName="txt" Property="Foreground" Value="Black" />
<Setter TargetName="_underline" Property="BorderThickness" Value="0,0,0,2" />
</MultiDataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Window.Resources>
<Grid x:Name="border_bootom">
<TabControl
Background="Transparent"
BorderBrush="Transparent"
Style="{DynamicResource TabControlWithUnderLineStyle}" Cursor="Hand">
<TabItem
Name="Tab_OutInfo"
Width="100"
Margin="5,0"
Cursor="Hand"
Header="选型原则"
Style="{DynamicResource TabItemExWithUnderLineStyle}">
<StackPanel
Orientation="Horizontal">
<RichTextBox Margin="2" FontSize="16" Background="LightGray" VerticalAlignment="Top" HorizontalAlignment="Left" IsReadOnly="True" BorderThickness="2" BorderBrush="Black" Width="{Binding ElementName=border_bootom, Path=ActualWidth}" >
<FlowDocument>
<Paragraph>
<Italic>
<Run FontWeight="Bold" >选型原则:</Run>
</Italic>
</Paragraph>
<List MarkerStyle="Square">
<ListItem>
<Paragraph>
<Italic>
<Run Foreground="Blue">1、电机额定扭矩 > 连续工作扭矩</Run>
</Italic>
</Paragraph>
</ListItem>
<ListItem>
<Paragraph>
<Italic>
<Run Foreground="Blue">2、电机加速时最大扭矩 > 瞬时最大扭矩</Run>
</Italic>
</Paragraph>
</ListItem>
<ListItem>
<Paragraph>
<Italic>
<Run Foreground="Blue">3、电机规定惯量比 > 实际惯量比</Run>
</Italic>
</Paragraph>
</ListItem>
<ListItem>
<Paragraph>
<Italic>
<Run Foreground="Blue">4、电机额定转速 > 连续工作速度</Run>
</Italic>
</Paragraph>
</ListItem>
</List>
</FlowDocument>
</RichTextBox>
</StackPanel>
</TabItem>
<TabItem
Width="100"
Margin="5,0"
Cursor="Hand"
Header="选型步骤"
Style="{DynamicResource TabItemExWithUnderLineStyle}">
<RichTextBox x:Name="rtbServoMotor" FontSize="16" Background="LightGray" Height="290" Margin="2" VerticalAlignment="Top" HorizontalAlignment="Left" IsReadOnly="True" BorderThickness="2" BorderBrush="Black" Width="{Binding ElementName=border_bootom, Path=ActualWidth}"/>
</TabItem>
<TabItem
Width="100"
Margin="5,0"
Cursor="Hand"
Header="选型计算"
Style="{DynamicResource TabItemExWithUnderLineStyle}">
<Grid Background="LightGray">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="0.5*"/>
<RowDefinition Height="60"/>
</Grid.RowDefinitions>
<GroupBox Header="工作条件" Grid.Column="0" Grid.Row="0" BorderThickness="2" BorderBrush="Black" Foreground="Black">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="机构类型:" VerticalAlignment="Center" HorizontalAlignment="Right"/>
<NbComboBox x:Name="MechanismSelection" Grid.Row="0" Grid.Column="1" Style="{DynamicResource DefaultComboBoxStyle}" Margin="5" IsEditable="False" EnabledSearchItems="True" EnabledSearchIgnoreCase="True" ShowClearButton="False" Height="20" Width="100" SelectedIndex="{Binding MechanismSelectionSelectedIndex}">
<NbComboBoxItem Content="丝杆"/>
<NbComboBoxItem Content="齿轮"/>
<NbComboBoxItem Content="皮带" />
<NbComboBoxItem Content="机械臂"/>
</NbComboBox>
<ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="4" Grid.RowSpan="2">
<ScrollViewer.Content>
<Frame x:Name="MyPageContext" Cursor="Hand" Background="Transparent" BorderBrush="Transparent" NavigationUIVisibility="Hidden" x:FieldModifier="public"/>
</ScrollViewer.Content>
</ScrollViewer>
</Grid>
</GroupBox>
<GroupBox Header="电机规格" Grid.Column="0" Grid.Row="1" BorderThickness="2" BorderBrush="Black" Foreground="Black">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition />
<ColumnDefinition Width="1.2*"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="额定功率【kW】:" VerticalAlignment="Center" HorizontalAlignment="Right"/>
<TextBox Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Width="120" Text="{Binding Motor_RatedPower}" VerticalContentAlignment="Center" HorizontalContentAlignment="Right"></TextBox>
<TextBlock Grid.Row="0" Grid.Column="2" Text="额定电压【V】:" VerticalAlignment="Center" HorizontalAlignment="Right"/>
<TextBox Grid.Row="0" Grid.Column="3" VerticalAlignment="Center" HorizontalAlignment="Left" Width="120" Text="{Binding Motor_RatedVoltage}" VerticalContentAlignment="Center" HorizontalContentAlignment="Right"></TextBox>
<TextBlock Grid.Row="1" Grid.Column="0" Text="额定转矩【N·m】:" VerticalAlignment="Center" HorizontalAlignment="Right"/>
<TextBox Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Width="120" Text="{Binding Motor_RatedTorque}" VerticalContentAlignment="Center" HorizontalContentAlignment="Right"></TextBox>
<TextBlock Grid.Row="1" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Right">
<Run Text="转动惯量【x10⁻³Kg·m²】" />
</TextBlock>
<TextBox Grid.Row="1" Grid.Column="3" VerticalAlignment="Center" HorizontalAlignment="Left" Width="120" Text="{Binding Motor_MomentInertia}" VerticalContentAlignment="Center" HorizontalContentAlignment="Right"></TextBox>
<TextBlock Grid.Row="2" Grid.Column="0" Text="额定电流【A】:" VerticalAlignment="Center" HorizontalAlignment="Right"/>
<TextBox Grid.Row="2" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Width="120" Text="{Binding Motor_RatedCurrent}" VerticalContentAlignment="Center" HorizontalContentAlignment="Right"></TextBox>
<TextBlock Grid.Row="2" Grid.Column="2" Text="峰值电流【A】:" VerticalAlignment="Center" HorizontalAlignment="Right"/>
<TextBox Grid.Row="2" Grid.Column="3" VerticalAlignment="Center" HorizontalAlignment="Left" Width="120" Text="{Binding Motor_PeakCurrent}" VerticalContentAlignment="Center" HorizontalContentAlignment="Right"></TextBox>
<TextBlock Grid.Row="3" Grid.Column="0" Text="额定转速【rpm】:" VerticalAlignment="Center" HorizontalAlignment="Right"/>
<TextBox Grid.Row="3" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Width="120" Text="{Binding Motor_RatedVelocity}" VerticalContentAlignment="Center" HorizontalContentAlignment="Right"></TextBox>
<TextBlock Grid.Row="3" Grid.Column="2" Text="最高转速【rpm】:" VerticalAlignment="Center" HorizontalAlignment="Right"/>
<TextBox Grid.Row="3" Grid.Column="3" VerticalAlignment="Center" HorizontalAlignment="Left" Width="120" Text="{Binding Motor_MaxVelocity}" VerticalContentAlignment="Center" HorizontalContentAlignment="Right"></TextBox>
<TextBlock Grid.Row="4" Grid.Column="0" Text="减速比:" VerticalAlignment="Center" HorizontalAlignment="Right"/>
<TextBox Grid.Row="4" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Width="120" Text="{Binding Motor_ReductionRatio}" ToolTip="比值结果,eg:30:1=30,1:2=0.5" VerticalContentAlignment="Center" HorizontalContentAlignment="Right"></TextBox>
</Grid>
</GroupBox>
<Button Grid.Row="2" Grid.Column="0" Content="开始选型计算" Command="{Binding StartCommand}" Margin="50,5,50,30" Foreground="Black" Background="LightCyan">
<Button.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="CornerRadius" Value="8"/>
</Style>
</Button.Resources>
</Button>
<RichTextBox x:Name="rtbClac" Grid.Column="1" Grid.Row="0" Height="635" Grid.RowSpan="3" Margin="2" VerticalAlignment="Top" HorizontalAlignment="Left" IsReadOnly="True" BorderThickness="2" BorderBrush="Black" />
</Grid>
</TabItem>
<TabItem
Width="120"
Margin="5,0"
Cursor="Hand"
Header="负载转矩计算查询"
Style="{DynamicResource TabItemExWithUnderLineStyle}">
<!--<Image Grid.Column="0" Grid.Row="4" Grid.RowSpan="3" Margin="2" Source="../Image/inertia.png" />-->
<Grid Background="LightGray">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="120"/>
<RowDefinition Height="200"/>
<RowDefinition Height="120"/>
<RowDefinition Height="200"/>
</Grid.RowDefinitions>
<Image Grid.Column="0" Grid.Row="0" Margin="2" HorizontalAlignment="Left" Source="../Image/ScrewMomentCalculation.png" />
<Image Grid.Column="0" Grid.Row="1" Margin="2" HorizontalAlignment="Left" Source="../Image/PulleyMomentCalculation.png" />
<Image Grid.Column="0" Grid.Row="2" Margin="2" HorizontalAlignment="Left" Source="../Image/GearMomentCalculation.png" />
<Image Grid.Column="0" Grid.Row="3" Margin="2" HorizontalAlignment="Left" Source="../Image/TorqueDeviceMeasurement.png" />
<Image Grid.Column="1" Grid.Row="1" Margin="2" Grid.RowSpan="2" Source="../Image/TorqueCalculationParameter.png" />
</Grid>
</TabItem>
<TabItem
Width="120"
Margin="5,0"
Cursor="Hand"
Header="转动惯量计算查询"
Style="{DynamicResource TabItemExWithUnderLineStyle}">
<!--<Image Grid.Column="0" Grid.Row="4" Grid.RowSpan="3" Margin="2" Source="../Image/inertia.png" />-->
<Grid Background="LightGray">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.8*"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="145"/>
<RowDefinition Height="150"/>
<RowDefinition Height="115"/>
<RowDefinition Height="132"/>
<RowDefinition Height="85"/>
</Grid.RowDefinitions>
<Image Grid.Column="0" Grid.Row="0" Margin="2" HorizontalAlignment="Left" Source="../Image/CylindricalInertia.png" />
<Image Grid.Column="0" Grid.Row="1" Margin="2" HorizontalAlignment="Left" Source="../Image/HollowCylinderInertia.png" />
<Image Grid.Column="0" Grid.Row="2" Margin="2" HorizontalAlignment="Left" Source="../Image/IrregularInertia.png" />
<Image Grid.Column="0" Grid.Row="3" Margin="2" HorizontalAlignment="Left" Source="../Image/PrismInertia.png" />
<Image Grid.Column="0" Grid.Row="4" Margin="2" HorizontalAlignment="Left" Source="../Image/LineRunInertia.png" />
<Image Grid.Column="1" Grid.Row="0" Margin="2" Grid.RowSpan="2" Source="../Image/SpecificInertiaOfDeceleration.png" />
<Image Grid.Column="1" Grid.Row="2" Margin="2" Grid.RowSpan="2" Source="../Image/InertiaCalculationParameters.png" />
</Grid>
</TabItem>
<TabItem
Width="100"
Margin="5,0"
Cursor="Hand"
Header="常用公式查询"
Style="{DynamicResource TabItemExWithUnderLineStyle}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<RichTextBox x:Name="torquAndVel" Background="LightGray" Height="290" Margin="2" VerticalAlignment="Top" HorizontalAlignment="Left" IsReadOnly="True" BorderThickness="2" BorderBrush="Black" Width="{Binding ElementName=border_bootom, Path=ActualWidth}"/>
</Grid>
</TabItem>
</TabControl>
<Border>
<ContentPresenter/>
</Border>
</Grid>
</Window>
主页.cs
using MotorSelectionTools.View;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfMath.Controls;
using static System.Net.Mime.MediaTypeNames;
namespace MotorSelectionTools
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MyPage("丝杆");
// 添加文本到 RichTextBox
AddTextToRichTextBox();
ShowAllFormula();
this.DataContext = new MainWindowViewModel() {
MyPageNavigateAction=MyPage,
UpdataRichText=ShowRichText,
UpdataRichTextPlus=ShowRichTextPlus,
UpdataRichFormula=ShowRichFormula,
UpdataRichFormulaPlus=ShowRichFormulaPlus,
ClearRichText=ClearText
};
}
private void AddTextToRichTextBox()
{
// 创建 FlowDocument
FlowDocument flowDocument = new FlowDocument();
// 创建选型步骤段落
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(new Bold(new Run("电机选型步骤")));
flowDocument.Blocks.Add(paragraph);
// 创建选型原则列表
List list = new List();
list.MarkerStyle = TextMarkerStyle.Box;
// 添加列表项
list.ListItems.Add(new ListItem(new Paragraph(new Italic(new Run("1. 确定机构\n常用机构有:丝杆、皮带、齿条与齿轮\n确定机构细节(丝杆长度、导程等)")) { Foreground = Brushes.Blue })));
list.ListItems.Add(new ListItem(new Paragraph(new Italic(new Run("2. 确定运转模式\n主要确定加减速时间、匀速时间、停止时间、移动距离等")) { Foreground = Brushes.Blue })));
list.ListItems.Add(new ListItem(new Paragraph(new Italic(new Run("3. 计算负载惯量和惯量比")) { Foreground = Brushes.Blue })));
list.ListItems.Add(new ListItem(new Paragraph(new Italic(new Run("4. 计算转速\n根据移动距离、加减速时间等条件计算电机运行需要满足最大转速")) { Foreground = Brushes.Blue })));
list.ListItems.Add(new ListItem(new Paragraph(new Italic(new Run("5. 计算转矩\n根据负载惯量、加减速时间等条件计算电机运行需要满足最大转据")) { Foreground = Brushes.Blue })));
list.ListItems.Add(new ListItem(new Paragraph(new Italic(new Run("6. 选择电机\n选择满足以上条件的电机")) { Foreground = Brushes.Blue })));
// 添加列表到 FlowDocument
flowDocument.Blocks.Add(list);
// 将 FlowDocument 设置为 RichTextBox 的文档
rtbServoMotor.Document = flowDocument;
}
private void Border_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
DragMove();
}
private void ShowAllFormula()
{
// 创建 FlowDocument
FlowDocument flowDocument = new FlowDocument();
// 创建选型步骤段落
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(new Bold(new Run("扭矩与转速公式:")));
flowDocument.Blocks.Add(paragraph);
//string mathFormulas = " T = \\frac{9550P}{n} \n T = \\frac{973P}{n}";
//string[] formulas = mathFormulas.Split('\n');
//foreach (string formula in formulas)
//{
// var formulaControl = new FormulaControl();
// formulaControl.Formula = formula;
// var container = new BlockUIContainer(formulaControl);
// flowDocument.Blocks.Add(container);
//}
string mathFormulas = " T = \\frac{9550P}{n}";
var formulaControl = new FormulaControl();
formulaControl.Formula = mathFormulas;
var container = new BlockUIContainer(formulaControl);
flowDocument.Blocks.Add(container);
List list = new List();
list.MarkerStyle = TextMarkerStyle.Box;
// 添加列表项
list.ListItems.Add(new ListItem(new Paragraph(new Italic(new Run("T:扭矩 N·m")) { Foreground = Brushes.Blue })));
list.ListItems.Add(new ListItem(new Paragraph(new Italic(new Run("P:输出功率 Kw")) { Foreground = Brushes.Blue })));
list.ListItems.Add(new ListItem(new Paragraph(new Italic(new Run("n:转速 r/min")) { Foreground = Brushes.Blue })));
flowDocument.Blocks.Add(list);
torquAndVel.Document= flowDocument;
}
FlowDocument Doc = new FlowDocument();
private void ShowRichText(Brush brush, string txt,bool isBold=false)
{
var p = new Paragraph(); // Paragraph 类似于 html 的 P 标签
var r = new Run(txt); // Run 是一个 Inline 的标签
if (isBold== false)
{
p.Inlines.Add(r);
}
else
{
p.Inlines.Add(new Bold(r));
}
p.LineHeight = 1;
p.Foreground = brush;//设置字体颜色
p.FontSize = 15; // 设置字体大小
Doc.Blocks.Add(p);
rtbClac.Document = Doc;
ScrollToBottom(rtbClac);
}
private void ShowRichTextPlus(Brush brush, string txt, bool isBold = false, string additionalText = "")
{
var p = new Paragraph();
var r = new Run(txt);
if (isBold == false)
{
p.Inlines.Add(r);
}
else
{
p.Inlines.Add(new Bold(r));
}
if (!string.IsNullOrEmpty(additionalText))
{
p.Inlines.Add(new Run(additionalText));
}
p.LineHeight = 1;
p.Foreground = brush;
p.FontSize = 15; // 设置字体大小
Doc.Blocks.Add(p);
rtbClac.Document = Doc;
ScrollToBottom(rtbClac);
}
private void ShowRichFormula(Brush brush, string mathFormulas, bool isBold = false)
{
//string Formulas = " T = \\frac{9550P}{n}";
if (string.IsNullOrEmpty(mathFormulas))
{
HandyControl.Controls.MessageBox.Error("公式错误!");
}
var formulaControl = new FormulaControl();
formulaControl.Formula = mathFormulas;
// 设置公式控件的字体大小
formulaControl.FontSize = 12; // 设置您想要的字体大小
if (isBold)
{
formulaControl.FontWeight = FontWeights.Bold;// 设置公式控件的粗体
}
var container = new BlockUIContainer(formulaControl);
Doc.Blocks.Add(container);
rtbClac.Document = Doc;
ScrollToBottom(rtbClac);
//var formulaControl = new FormulaControl();
//formulaControl.Formula = mathFormulas;
//var container = new BlockUIContainer(formulaControl);
//Doc.Blocks.Add(container);
//rtbClac.Document = Doc;
}
private void ShowRichFormulaPlus(Brush brush, string mathFormulas, bool isBold = false, string additionalText = "")
{
//string Formulas = " T = \\frac{9550P}{n}";
if (string.IsNullOrEmpty(mathFormulas))
{
HandyControl.Controls.MessageBox.Error("公式错误!");
}
var formulaControl = new FormulaControl();
formulaControl.FontSize = 12;
formulaControl.Formula = mathFormulas;
var p = new Paragraph();
if (isBold)
{
p.Inlines.Add(new Bold(new Run(mathFormulas)));
}
else
{
p.Inlines.Add(new Run(mathFormulas));
}
if (!string.IsNullOrEmpty(additionalText))
{
p.Inlines.Add(new Run(additionalText));
}
p.LineHeight = 1;
p.Foreground = brush;
Doc.Blocks.Add(new BlockUIContainer(formulaControl));
rtbClac.Document = Doc;
ScrollToBottom(rtbClac);
}
private void ClearText(bool obj)
{
try
{
if (obj)
{
FlowDocument doc = rtbClac.Document;
doc.Blocks.Clear();
//rtbClac.Document.Blocks.Clear();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private void ScrollToBottom(System.Windows.Controls.RichTextBox richTextBox)
{
richTextBox.ScrollToEnd();
richTextBox.CaretPosition = richTextBox.Document.ContentEnd;
richTextBox.Focus();
}
private void MyPage(string pageName)
{
if (pageName == null) HandyControl.Controls.MessageBox.Error("页面为空");
switch (pageName)
{
case "丝杆": // "丝杆"
//frame = FindName("MyPageContext") as Frame;
MyPageContext?.Navigate(new ScrewMechanismPage());
break;
case "齿轮": // "齿轮":
//frame = FindName("MyPageContext") as Frame;
MyPageContext?.Navigate(new GearedMechanismPage());
break;
default:
break;
}
}
}
}
主页面后台文件(ViewModel)
主要计算和显示计算过程的功能在以下代码中
using MotorSelectionTools.Model;
using MVVM;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
namespace MotorSelectionTools
{
public class MainWindowViewModel:ViewModelBase
{
#region 变量
public Action<string> MyPageNavigateAction { get; set; }
public Action<bool> ClearRichText { get; set; }
public Action<Brush, string,bool> UpdataRichText { get; set; }
public Action<Brush, string,bool, string> UpdataRichTextPlus { get; set; }
public Action<Brush, string,bool> UpdataRichFormula { get; set; }
public Action<Brush, string,bool, string> UpdataRichFormulaPlus { get; set; }
private int ClcUnm = 0;
#endregion
#region 构造函数
public MainWindowViewModel()
{
Init();
StartCommand = new RelayCommand(Startcalculate);
}
#endregion
#region 命令
public RelayCommand StartCommand { get; set; }
#endregion
#region 属性
#region 通讯方式
private int _MechanismSelectionSelectedIndex;
public int MechanismSelectionSelectedIndex
{
get { return _MechanismSelectionSelectedIndex; }
set
{
_MechanismSelectionSelectedIndex = value;
if (_MechanismSelectionSelectedIndex!=-1)
{
switch (_MechanismSelectionSelectedIndex)
{
case 0:
MyPageNavigateAction.Invoke("丝杆");
break;
case 1:
MyPageNavigateAction.Invoke("齿轮");
break;
default:
MyPageNavigateAction.Invoke("丝杆");
break;
}
}
else
{
MyPageNavigateAction.Invoke("丝杆");
}
RaisePropertyChanged(() => MechanismSelectionSelectedIndex);
}
}
#endregion
#region 额定功率
private string _Motor_RatedPower;
public string Motor_RatedPower
{
get { return _Motor_RatedPower; }
set
{
_Motor_RatedPower = value;
RaisePropertyChanged(() => Motor_RatedPower);
}
}
#endregion
#region 额定电压
private string _Motor_RatedVoltage;
public string Motor_RatedVoltage
{
get { return _Motor_RatedVoltage; }
set
{
_Motor_RatedVoltage = value;
RaisePropertyChanged(() => Motor_RatedVoltage);
}
}
#endregion
#region 额定转矩
private string _Motor_RatedTorque;
public string Motor_RatedTorque
{
get { return _Motor_RatedTorque; }
set
{
_Motor_RatedTorque = value;
RaisePropertyChanged(() => Motor_RatedTorque);
}
}
#endregion
#region 转动惯量
private string _Motor_MomentInertia;
public string Motor_MomentInertia
{
get { return _Motor_MomentInertia; }
set
{
_Motor_MomentInertia = value;
RaisePropertyChanged(() => Motor_MomentInertia);
}
}
#endregion
#region 额定电流
private string _Motor_RatedCurrent;
public string Motor_RatedCurrent
{
get { return _Motor_RatedCurrent; }
set
{
_Motor_RatedCurrent = value;
RaisePropertyChanged(() => Motor_RatedCurrent);
}
}
#endregion
#region 峰值电流
private string _Motor_PeakCurrent;
public string Motor_PeakCurrent
{
get { return _Motor_PeakCurrent; }
set
{
_Motor_PeakCurrent = value;
RaisePropertyChanged(() => Motor_PeakCurrent);
}
}
#endregion
#region 额定转速
private string _Motor_RatedVelocity;
public string Motor_RatedVelocity
{
get { return _Motor_RatedVelocity; }
set
{
_Motor_RatedVelocity = value;
RaisePropertyChanged(() => Motor_RatedVelocity);
}
}
#endregion
#region 最高转速
private string _Motor_MaxVelocity;
public string Motor_MaxVelocity
{
get { return _Motor_MaxVelocity; }
set
{
_Motor_MaxVelocity = value;
RaisePropertyChanged(() => Motor_MaxVelocity);
}
}
#endregion
#region 减速比
private string _Motor_ReductionRatio;
public string Motor_ReductionRatio
{
get { return _Motor_ReductionRatio; }
set
{
_Motor_ReductionRatio = value;
RaisePropertyChanged(() => Motor_ReductionRatio);
}
}
#endregion
#endregion
#region 方法
private void Init()
{
Motor_RatedPower = "200";
Motor_RatedVoltage = "24";
Motor_RatedTorque = "10";
Motor_MomentInertia = "0.2";
Motor_RatedCurrent = "8";
Motor_PeakCurrent = "16";
Motor_RatedVelocity = "3000";
Motor_MaxVelocity = "3600";
Motor_ReductionRatio = "15";
ClcUnm = 0;
}
private void Startcalculate()
{
string strshow = "";
string PI = "\\pi";
//ClearRichText.Invoke(true);
if (Systemglobal.Instance.ScrewMechaninismPageViewModel.CheckData() && CheckData())
{
ClcUnm++;
UpdataRichText.Invoke(Brushes.GreenYellow, $"第{ClcUnm}次计算", true);
if (Systemglobal.Instance.ScrewMechaninismPageViewModel.IsDrivingGear)
{
//有传动
if (!Systemglobal.Instance.ScrewMechaninismPageViewModel.IsKnoewWeight)
{
//知道质量
}
else
{
//不知道质量
}
if (Systemglobal.Instance.ScrewMechaninismPageViewModel.IsCalculatedReductionRatio)
{
//计算减速比
}
}
else
{
//无传动
//转动惯量J[kg·m2]的确认
UpdataRichText.Invoke(Brushes.Red, "1、转动惯量J[kg·m²]的计算", true);
UpdataRichText.Invoke(Brushes.Black, "滚珠螺杆的转动惯量[kg·m²]:", true);
var density = Convert.ToDouble(Systemglobal.Instance.ScrewMechaninismPageViewModel.DrivingGear_Screw_Shaft_Density);
var length = Convert.ToDouble(Systemglobal.Instance.ScrewMechaninismPageViewModel.DrivingGear_Screw_Shaft_Length);
var diameter = Convert.ToDouble(Systemglobal.Instance.ScrewMechaninismPageViewModel.DrivingGear_Screw_Shaft_Diameter);
var g = 9.807;//9.807
var j1 = Math.PI * density* 1000 * length * 0.001 * Math.Pow(diameter*0.001, 4)/ 32.0;
var unit = "kg·m²";
var JB_Data = new Data() { Value= j1 ,Unit= unit };
strshow = $"J_B= \\frac{PI}{{32}} \\times \\rho \\times L_B \\times D_B^4";
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
strshow = $"= \\frac{PI}{{32}} \\times {density} \\times 10^3 \\times {length} \\times 10^{{-3}} \\times \\({diameter} \\times 10^{{-3}}\\)^4";
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
strshow= Systemglobal.Instance.FormatJ1(ref JB_Data, Systemglobal.Instance.ranges_1);
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
UpdataRichText.Invoke(Brushes.Black, "工作台与工作物的转动惯量[kg·m²]:", true);
var m= Convert.ToDouble(Systemglobal.Instance.ScrewMechaninismPageViewModel.DrivingGear_Work_Weight);
var A= Convert.ToDouble(Systemglobal.Instance.ScrewMechaninismPageViewModel.DrivingGear_Screw_Shaft_Lead);
var jm = m * (Math.Pow(A * 0.001 / (2 * Math.PI), 2));
var JM_Data = new Data() { Value = jm, Unit = unit };
strshow = $"J_m= m \\times ( \\frac{{A}}{{2{PI}}} )^2";
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
strshow = $"= {m} \\times ( \\frac{{{A} \\times 10^{{-3}}}}{{2{PI}}} )^2";
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
strshow = Systemglobal.Instance.FormatJ1(ref JM_Data, Systemglobal.Instance.ranges_1);
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
UpdataRichText.Invoke(Brushes.Black, "求出减速机输出轴的转动惯量J[kg·m²]:", true);
var J = JB_Data.Value* (Math.Pow(10, JB_Data.Power)) + JM_Data.Value* (Math.Pow(10, JM_Data.Power));
var J_Data=new Data() { Value = J, Unit = unit };
strshow = $"J= J_B+J_m={JB_Data.Value.ToString("F4", CultureInfo.InvariantCulture)} \\times 10^{{{JB_Data.Power}}}+{JM_Data.Value.ToString("F4", CultureInfo.InvariantCulture)} \\times 10^{{{JM_Data.Power}}}";
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
strshow = Systemglobal.Instance.FormatJ1(ref J_Data, Systemglobal.Instance.ranges_1);
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
var motor_reduction_ratio = Convert.ToDouble(Motor_ReductionRatio);
UpdataRichText.Invoke(Brushes.Black, "计算电机需要的转动惯量JL[kg·m²]:", true);
var JI = J / (motor_reduction_ratio * motor_reduction_ratio);
var JI_Data = new Data() { Value = JI, Unit = unit };
strshow = $"J_L= \\frac{{J}}{{i^2}}=\\frac{{{J_Data.Value.ToString("F4", CultureInfo.InvariantCulture)} \\times 10^{{{J_Data.Power}}}}}{{{motor_reduction_ratio}^2}}";
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
strshow = Systemglobal.Instance.FormatJ1(ref JI_Data, Systemglobal.Instance.ranges_1);
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
var JIM= Convert.ToDouble(Motor_MomentInertia)*0.001;
var JIM_Data = new Data() { Value = JIM, Unit = unit };
strshow = Systemglobal.Instance.FormatJ1(ref JIM_Data, Systemglobal.Instance.ranges_1);
if (JI> JIM)
{
var temp = Systemglobal.Instance.SimplifyDecimalHandle(JIM_Data);
var temp1 = Systemglobal.Instance.SimplifyDecimalHandle(JI_Data);
UpdataRichText.Invoke(Brushes.DarkRed, $"所选电机转动惯量{temp}x10^{JIM_Data.Power}kg·m²运行时所需的转动惯量{temp1}x10^{JI_Data.Power}kg·m²,不匹配!", true);
return;
}
else
{
UpdataRichText.Invoke(Brushes.DarkRed, $"所选电机转动惯量匹配!", true);
}
UpdataRichText.Invoke(Brushes.Red, "2、负载转矩TL[N·m]的计算", true);
UpdataRichText.Invoke(Brushes.Black, "运行方向负载[N]:", true);
var FA = Convert.ToDouble(Systemglobal.Instance.ScrewMechaninismPageViewModel.DrivingGear_External_Force);
var angle= Convert.ToDouble(Systemglobal.Instance.ScrewMechaninismPageViewModel.DrivingGear_Inclination_Angle);
var u = Convert.ToDouble(Systemglobal.Instance.ScrewMechaninismPageViewModel.DrivingGear_Guideway_Friction);
unit = "N";
var F = FA + m * g * (Math.Sin(angle * Math.PI / 180)+u* Math.Cos(angle * Math.PI / 180));
var F_Data = new Data() { Value = F, Unit = unit };
strshow = $"F= F_A + mg( sin \\theta + \\mu cos \\theta )";
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
strshow = $"= {FA} + {m} \\times {g} \\times ( sin( {angle}^ \\circ) + {u} cos ({angle}^ \\circ) )";
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
strshow = $"= {F}";
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
UpdataRichText.Invoke(Brushes.Black, "滚珠螺杆的预负载[N·m]:", true);
var F0= Convert.ToDouble(Systemglobal.Instance.ScrewMechaninismPageViewModel.DrivingGear_Preload);
strshow = $"F_0= {F0}";
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
UpdataRichText.Invoke(Brushes.Black, "负载转矩[N·m]:", true);
var efficiency= Convert.ToDouble(Systemglobal.Instance.ScrewMechaninismPageViewModel.DrivingGear_Efficiency);
var u0 = Convert.ToDouble(Systemglobal.Instance.ScrewMechaninismPageViewModel.DrivingGear_Preload_Nut_Friction);
var Tl=F* A*0.001/(2*Math.PI* efficiency)+u0*F0*A*0.001/(2*Math.PI);
var Tl_Data = new Data() { Value = Tl, Unit = unit };
strshow = $"T_l= \\frac{{F \\cdot A}} {{2{PI} \\cdot \\eta }} + \\frac{{\\mu_0 \\cdot F_0 \\cdot A}} {{2{PI}}} ";
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
strshow = $"= \\frac{{{F.ToString("F4", CultureInfo.InvariantCulture)} \\times {A} \\times 10^{{-3}}}} {{2{PI} \\times {efficiency} }} + \\frac{{{u0} \\times {F0} \\times {A} \\times 10^{{-3}}}} {{2{PI}}} ";
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
strshow = Systemglobal.Instance.FormatJ1(ref Tl_Data, Systemglobal.Instance.ranges_1);
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
UpdataRichText.Invoke(Brushes.Black, "考虑安全系数:", true);
var sf = 2;
strshow = $"S_f=2 ";
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
var TL = Tl * sf;
var TL_Data = new Data() { Value = TL, Unit = unit };
UpdataRichText.Invoke(Brushes.Black, "负载转矩[N·m]:", true);
strshow = $"T_L=T_l \\cdot S_f = {Tl.ToString("F4", CultureInfo.InvariantCulture)} \\times 2";
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
strshow = Systemglobal.Instance.FormatJ1(ref TL_Data, Systemglobal.Instance.ranges_1);
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
//var motor_reduction_ratio = Convert.ToDouble(Motor_ReductionRatio);
var motor_rate_torque= Convert.ToDouble(Motor_RatedTorque); ;
UpdataRichText.Invoke(Brushes.Black, "计算电机需要输出的转矩[N·m]:", true);
var TM = TL / (motor_reduction_ratio);
var TM_Data = new Data() { Value = TM, Unit = unit };
strshow = $"T_M= \\frac{{T_L}}{{i}}=\\frac{{{TL_Data.Value.ToString("F4", CultureInfo.InvariantCulture)}}}{{{motor_reduction_ratio}}}";
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
strshow = Systemglobal.Instance.FormatJ1(ref TM_Data, Systemglobal.Instance.ranges_1);
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
if (TM> motor_rate_torque)
{
UpdataRichText.Invoke(Brushes.DarkRed, $"所选电机额定力矩{motor_rate_torque}N·m小于持续负载时的负载力矩{TM}N·m,不匹配!", true);
return;
}
else
{
UpdataRichText.Invoke(Brushes.DarkRed, $"所选电机额定力矩匹配!", true);
}
if (Systemglobal.Instance.ScrewMechaninismPageViewModel.IsSpecifiedSpeed)
{
//指定转速时
UpdataRichText.Invoke(Brushes.Red, "3、计算输出轴最大转速[r/min]:", true);
strshow = $"N_G= \\frac{{V \\cdot 60}}{{{A}}}";
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
var maxVel = Convert.ToDouble(Systemglobal.Instance.ScrewMechaninismPageViewModel.DrivingGear_MaxVelocity);
var NG = maxVel * 60 / A;
var NG_Data= new Data() { Value = NG, Unit = unit };
strshow = Systemglobal.Instance.FormatJ1(ref NG_Data, Systemglobal.Instance.ranges_1);
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
UpdataRichText.Invoke(Brushes.Black, "计算电机需要的转速[r/min]:", true);
strshow = $"N_M= N_G \\times i={NG} \\times {motor_reduction_ratio}";
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
var motor_rate_vel = Convert.ToDouble(Motor_RatedVelocity);
//var motor_reduction_ratio = Convert.ToDouble(Motor_ReductionRatio);
var tempVel = NG * motor_reduction_ratio;
var TV_Data = new Data() { Value = tempVel, Unit = unit };
strshow = Systemglobal.Instance.FormatJ1(ref TV_Data, Systemglobal.Instance.ranges_1);
UpdataRichFormula.Invoke(Brushes.Black, strshow, false);
if (motor_rate_vel < tempVel)
{
UpdataRichText.Invoke(Brushes.DarkRed, $"所选电机额定转速{motor_rate_vel}rpm小于运行速度{NG}x{motor_reduction_ratio}={tempVel}rpm,不匹配!", true);
return;
}
else
{
UpdataRichText.Invoke(Brushes.DarkRed, $"所选电机额定转速匹配!", true);
}
UpdataRichText.Invoke(Brushes.Red, $"综上所述,所选电机满足工作运行要求!", true);
}
else
{
//指定位移量时
}
}
}
}
private bool CheckData()
{
bool res = false;
#region 额定功率
if (String.IsNullOrEmpty(Motor_RatedPower))
{
HandyControl.Controls.MessageBox.Error("额定功率数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(Motor_RatedPower))
{
HandyControl.Controls.MessageBox.Error("额定功率非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
#region 额定电压
if (String.IsNullOrEmpty(Motor_RatedVoltage))
{
HandyControl.Controls.MessageBox.Error("额定电压数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(Motor_RatedVoltage))
{
HandyControl.Controls.MessageBox.Error("额定电压非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
#region 额定转矩
if (String.IsNullOrEmpty(Motor_RatedTorque))
{
HandyControl.Controls.MessageBox.Error("额定转矩数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(Motor_RatedTorque))
{
HandyControl.Controls.MessageBox.Error("额定转矩非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
#region 转动惯量
if (String.IsNullOrEmpty(Motor_MomentInertia))
{
HandyControl.Controls.MessageBox.Error("转动惯量数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(Motor_MomentInertia))
{
HandyControl.Controls.MessageBox.Error("转动惯量非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
#region 额定电流
if (String.IsNullOrEmpty(Motor_RatedCurrent))
{
HandyControl.Controls.MessageBox.Error("额定电流数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(Motor_RatedCurrent))
{
HandyControl.Controls.MessageBox.Error("额定电流非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
#region 峰值电流
if (String.IsNullOrEmpty(Motor_PeakCurrent))
{
HandyControl.Controls.MessageBox.Error("峰值电流数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(Motor_PeakCurrent))
{
HandyControl.Controls.MessageBox.Error("峰值电流非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
#region 额定转速
if (String.IsNullOrEmpty(Motor_RatedVelocity))
{
HandyControl.Controls.MessageBox.Error("额定转速数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(Motor_RatedVelocity))
{
HandyControl.Controls.MessageBox.Error("额定转速非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
#region 最高转速
if (String.IsNullOrEmpty(Motor_MaxVelocity))
{
HandyControl.Controls.MessageBox.Error("最高转速数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(Motor_MaxVelocity))
{
HandyControl.Controls.MessageBox.Error("最高转速非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
#region 减速比
if (String.IsNullOrEmpty(Motor_ReductionRatio))
{
HandyControl.Controls.MessageBox.Error("减速比数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(Motor_ReductionRatio))
{
HandyControl.Controls.MessageBox.Error("减速比非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
return res;
}
#endregion
}
}
然后就是几种机构不同类型显示的界面和后台,目前只实现了丝杆结构基本功能;
丝杆机构页面UI(View)
丝杆机构.cs文件很简单,添加DataContext后台绑定即可。
<Page
x:Class="MotorSelectionTools.View.ScrewMechanismPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MotorSelectionTools.View"
xmlns:converter="clr-namespace:MotorSelectionTools.Converter"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="ScrewMechanismPage"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<Page.Resources>
<converter:InverseBoolConverter x:Key="InverseBoolConverter"/>
</Page.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="150" />
<RowDefinition Height="150" />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="0.8*" />
</Grid.ColumnDefinitions>
<Image
Grid.Row="0"
Grid.Column="1"
Margin="2"
VerticalAlignment="Top"
Source="../Image/screw.png"
Stretch="Fill" />
<Image
Grid.Row="1"
Grid.Column="1"
Margin="2"
VerticalAlignment="Top"
Source="../Image/ExternalForce.png"
Stretch="Fill" />
<Grid
Grid.Row="0"
Grid.RowSpan="4"
Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.5*" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<CheckBox
x:Name="isDrivingGear"
Grid.ColumnSpan="2"
Margin="2"
Content="有传动装置(勾选:有)"
IsChecked="{Binding IsDrivingGear}" />
<TextBlock
Grid.Row="1"
Grid.Column="0"
Margin="4"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Text="电机侧带轮或齿轮直径【mm】:"
Visibility="{Binding IsDrivingGearVisibility}" />
<TextBox
Grid.Row="1"
Grid.Column="1"
Width="100"
Margin="4"
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
Text="{Binding DrivingGear_Diameter_Motor}"
Visibility="{Binding IsDrivingGearVisibility}" />
<TextBlock
Grid.Row="2"
Grid.Column="0"
Margin="4"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Text="负载侧带轮或齿轮直径【mm】:"
Visibility="{Binding IsDrivingGearVisibility}" />
<TextBox
Grid.Row="2"
Grid.Column="1"
Width="100"
Margin="4"
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
Text="{Binding DrivingGear_Diameter_Vice}"
Visibility="{Binding IsDrivingGearVisibility}" />
<CheckBox
Grid.Row="3"
Grid.Column="0"
Grid.ColumnSpan="2"
Margin="4"
Content="质量是否清楚(勾选:不清楚)"
IsChecked="{Binding IsKnoewWeight}"
Visibility="{Binding IsDrivingGearVisibility}" />
<TextBlock
Grid.Row="4"
Grid.Column="0"
Margin="4"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Text="电机侧带轮或齿轮质量【mm】:"
Visibility="{Binding IsKnoewWeightVisibility}" />
<TextBox
Grid.Row="4"
Grid.Column="1"
Width="100"
Margin="4"
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
Text="{Binding DrivingGear_Weight_Motor}"
Visibility="{Binding IsKnoewWeightVisibility}" />
<TextBlock
Grid.Row="5"
Grid.Column="0"
Margin="4"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Text="负载侧带轮或齿轮质量【mm】:"
Visibility="{Binding IsKnoewWeightVisibility}" />
<TextBox
Grid.Row="5"
Grid.Column="1"
Width="100"
Margin="4"
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
Text="{Binding DrivingGear_Weight_Vice}"
Visibility="{Binding IsKnoewWeightVisibility}" />
<TextBlock
Grid.Row="6"
Grid.Column="0"
Margin="4"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Text="电机侧带轮或齿轮宽度【mm】:"
Visibility="{Binding IsKnoewWeightNotVisibility}" />
<TextBox
Grid.Row="6"
Grid.Column="1"
Width="100"
Margin="4"
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
Text="{Binding DrivingGear_Width_Motor}"
Visibility="{Binding IsKnoewWeightNotVisibility}" />
<TextBlock
Grid.Row="7"
Grid.Column="0"
Margin="4"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Text="负载侧带轮或齿轮宽度【mm】:"
Visibility="{Binding IsKnoewWeightNotVisibility}" />
<TextBox
Grid.Row="7"
Grid.Column="1"
Width="100"
Margin="4"
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
Text="{Binding DrivingGear_Width_Vice}"
Visibility="{Binding IsKnoewWeightNotVisibility}" />
<TextBlock
Grid.Row="8"
Grid.Column="0"
Margin="4"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Visibility="{Binding IsKnoewWeightNotVisibility}">
<Run Text="电机侧材质密度【x10³Kg/m³】:" />
</TextBlock>
<TextBox
Grid.Row="8"
Grid.Column="1"
Width="100"
Margin="4"
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
Text="{Binding DrivingGear_Density_Motor}"
ToolTip="材质密度请查询【转动惯量计算】页面密度项"
Visibility="{Binding IsKnoewWeightNotVisibility}" />
<TextBlock
Grid.Row="9"
Grid.Column="0"
Margin="4"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Visibility="{Binding IsKnoewWeightNotVisibility}">
<Run Text="负载侧材质密度【x10³Kg/m³】:" />
</TextBlock>
<TextBox
Grid.Row="9"
Grid.Column="1"
Width="100"
Margin="4"
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
Text="{Binding DrivingGear_Density_Vice}"
ToolTip="材质密度请查询【转动惯量计算】页面密度项"
Visibility="{Binding IsKnoewWeightNotVisibility}" />
<CheckBox
Grid.Row="10"
Grid.Column="0"
Grid.ColumnSpan="2"
Margin="4"
Content="根据齿数计算减速比(勾选:是)"
IsChecked="{Binding IsCalculatedReductionRatio}"
Visibility="{Binding IsDrivingGearVisibility}" />
<TextBlock
Grid.Row="11"
Grid.Column="0"
Margin="4"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Text="电机侧齿数:"
Visibility="{Binding IsCalculatedReductionRatioVisibility}" />
<TextBox
Grid.Row="11"
Grid.Column="1"
Width="100"
Margin="4"
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
Text="{Binding DrivingGear_NumberOfTeeth_Motor}"
Visibility="{Binding IsCalculatedReductionRatioVisibility}" />
<TextBlock
Grid.Row="12"
Grid.Column="0"
Margin="4"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Text="负载侧齿数:"
Visibility="{Binding IsCalculatedReductionRatioVisibility}" />
<TextBox
Grid.Row="12"
Grid.Column="1"
Width="100"
Margin="4"
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
Text="{Binding DrivingGear_NumberOfTeeth_Vice}"
Visibility="{Binding IsCalculatedReductionRatioVisibility}" />
<TextBlock
Grid.Row="13"
Grid.Column="0"
Margin="4"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Text="皮带重量【kg】:"
Visibility="{Binding IsDrivingGearVisibility}" />
<TextBox
Grid.Row="13"
Grid.Column="1"
Width="100"
Margin="4"
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
Text="{Binding DrivingGear_Belt_Weight}"
Visibility="{Binding IsDrivingGearVisibility}" />
<TextBlock
Grid.Row="14"
Grid.Column="0"
Margin="4"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Text="传动效率:"
Visibility="{Binding IsDrivingGearVisibility}" />
<TextBox
Grid.Row="14"
Grid.Column="1"
Width="100"
Margin="4"
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
Text="{Binding DrivingGear_Transmission_Efficiency}"
Visibility="{Binding IsDrivingGearVisibility}" />
<Border
Grid.Row="15"
Grid.Column="0"
Grid.ColumnSpan="2"
Height="6"
Background="LightBlue" />
<TextBlock
Grid.Row="16"
Grid.Column="0"
Margin="4"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Text="工作物质量【kg】:" />
<TextBox
Grid.Row="16"
Grid.Column="1"
Width="100"
Margin="4"
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
Text="{Binding DrivingGear_Work_Weight}" />
<TextBlock
Grid.Row="17"
Grid.Column="0"
Margin="4"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Text="导轨的摩擦系数:" />
<TextBox
Grid.Row="17"
Grid.Column="1"
Width="100"
Margin="4"
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
Text="{Binding DrivingGear_Guideway_Friction}" />
<Border
Grid.Row="18"
Grid.Column="0"
Grid.ColumnSpan="2"
Height="6"
Background="LightBlue" />
<TextBlock
Grid.Row="19"
Grid.Column="0"
Margin="4"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Text="丝杆轴径【mm】:" />
<TextBox
Grid.Row="19"
Grid.Column="1"
Width="100"
Margin="4"
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
Text="{Binding DrivingGear_Screw_Shaft_Diameter}" />
<TextBlock
Grid.Row="20"
Grid.Column="0"
Margin="4"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Text="丝杆总长【mm】:" />
<TextBox
Grid.Row="20"
Grid.Column="1"
Width="100"
Margin="4"
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
Text="{Binding DrivingGear_Screw_Shaft_Length}" />
<TextBlock
Grid.Row="21"
Grid.Column="0"
Margin="4"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Text="丝杆导程【mm】:" />
<TextBox
Grid.Row="21"
Grid.Column="1"
Width="100"
Margin="4"
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
Text="{Binding DrivingGear_Screw_Shaft_Lead}" />
<TextBlock
Grid.Row="22"
Grid.Column="0"
Margin="4"
HorizontalAlignment="Right"
VerticalAlignment="Center">
<Run Text="丝杆负载侧密度【x10³Kg/m³】:" />
</TextBlock>
<TextBox
Grid.Row="22"
Grid.Column="1"
Width="100"
Margin="4"
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
Text="{Binding DrivingGear_Screw_Shaft_Density}"
ToolTip="材质密度请查询【转动惯量计算】页面密度项" />
<TextBlock
Grid.Row="23"
Grid.Column="0"
Margin="4"
HorizontalAlignment="Right"
VerticalAlignment="Center">
<Run Text="效率:" />
</TextBlock>
<TextBox
Grid.Row="23"
Grid.Column="1"
Width="100"
Margin="4"
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
Text="{Binding DrivingGear_Efficiency}">
<TextBox.ToolTip>
<TextBlock>
<Run Text="研磨滚珠丝杆效率:0.9" />
<Run Text="滚压滚珠丝杆效率:0.8" />
<Run Text="梯形丝杆效率:0.3" />
<Run Text="其它类型丝杆效率可自行输入" />
</TextBlock>
</TextBox.ToolTip>
</TextBox>
<TextBlock
Grid.Row="24"
Grid.Column="0"
Margin="4"
HorizontalAlignment="Right"
VerticalAlignment="Center">
<Run Text="预负载【N】:" />
</TextBlock>
<TextBox
Grid.Row="24"
Grid.Column="1"
Width="100"
Margin="4"
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
Text="{Binding DrivingGear_Preload}">
<TextBox.ToolTip>
<TextBlock>
<Run Text="1、一般情况为 F/3" />
<Run Text="2、自行输入" />
<Run Text="3、无预负载 0" />
</TextBlock>
</TextBox.ToolTip>
</TextBox>
<TextBlock
Grid.Row="25"
Grid.Column="0"
Margin="4"
HorizontalAlignment="Right"
VerticalAlignment="Center">
<Run Text="预压螺母内部摩擦系数:" />
</TextBlock>
<TextBox
Grid.Row="25"
Grid.Column="1"
Width="100"
Margin="4"
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
Text="{Binding DrivingGear_Preload_Nut_Friction}">
<TextBox.ToolTip>
<TextBlock>
<Run Text="1、一般情况为 0.3" />
</TextBlock>
</TextBox.ToolTip>
</TextBox>
<Border
Grid.Row="26"
Grid.Column="0"
Grid.ColumnSpan="2"
Height="6"
Background="LightBlue" />
<TextBlock
Grid.Row="27"
Grid.Column="0"
Margin="4"
HorizontalAlignment="Right"
VerticalAlignment="Center">
<Run Text="外力FA【N】:" />
</TextBlock>
<TextBox
Grid.Row="27"
Grid.Column="1"
Width="100"
Margin="4"
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
Text="{Binding DrivingGear_External_Force}" />
<TextBlock
Grid.Row="28"
Grid.Column="0"
Margin="4"
HorizontalAlignment="Right"
VerticalAlignment="Center">
<Run Text="倾斜角α【°】:" />
</TextBlock>
<TextBox
Grid.Row="28"
Grid.Column="1"
Width="100"
Margin="4"
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
Text="{Binding DrivingGear_Inclination_Angle}" />
<Border
Grid.Row="29"
Grid.Column="0"
Grid.ColumnSpan="2"
Height="6"
Background="LightBlue" />
</Grid>
<Grid Grid.Row="4" Grid.ColumnSpan="2">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="700"/>
</Grid.RowDefinitions>
<Grid Grid.ColumnSpan="2" >
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<RadioButton
x:Name="cb1"
Grid.Column="0"
Margin="4"
Content="指定转速"
HorizontalAlignment="Center"
IsChecked="{Binding IsSpecifiedSpeed}"/>
<RadioButton
Grid.Column="1"
Margin="4"
Content="指定移动量"
HorizontalAlignment="Center"
IsChecked="{Binding IsSpecifiedPosition}"/>
</Grid>
<Image
Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="2"
Margin="0,-280,0,0"
Height="700"
Width="500"
VerticalAlignment="Top"
Source="../Image/Velocity.png"
Stretch="Uniform" />
<Grid>
<StackPanel VerticalAlignment="Top" HorizontalAlignment="Center" Orientation="Horizontal" Visibility="Hidden">
<TextBlock Margin="100 50 0 0" Text="移动时间【s】:" Foreground="Black"/>
<TextBox Width="100" Margin="0 50 0 0" HorizontalContentAlignment="Right" VerticalContentAlignment="Center"/>
</StackPanel>
<StackPanel VerticalAlignment="Top" HorizontalAlignment="Left" Orientation="Vertical" Visibility="{Binding IsSpecifiedSpeedVisibility}">
<TextBlock Margin="40 80 0 0" Text="最大转速【mm/s】:" Foreground="Black" />
<TextBox Width="100" Margin="40 0 0 0" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Text="{Binding DrivingGear_MaxVelocity}"/>
</StackPanel>
<StackPanel VerticalAlignment="Top" HorizontalAlignment="Left" Orientation="Horizontal" Visibility="{Binding IsSpecifiedPositionVisibility}">
<TextBlock Margin="230 140 0 0" Text="移动量【mm】:" Foreground="Black" />
<TextBox Width="100" Margin="0 140 0 0" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Text="{Binding DrivingGear_Position}"/>
</StackPanel>
<StackPanel VerticalAlignment="Top" HorizontalAlignment="Left" Orientation="Horizontal" Visibility="Hidden">
<TextBlock Margin="150 290 0 0" Text="加速时间【mm】:" Foreground="Black" />
<TextBox Width="100" Margin="0 290 0 0" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" />
</StackPanel>
</Grid>
</Grid>
</Grid>
</Page>
丝杆机构后台(ViewModel)
using MVVM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using HandyControl.Controls;
using System.Globalization;
namespace MotorSelectionTools.ViewModel
{
public class ScrewMechaninismPageViewModel:ViewModelBase
{
#region 变量
#endregion
#region 构造函数
public ScrewMechaninismPageViewModel()
{
Init();
}
#endregion
#region 命令
#endregion
#region 属性
#region CheckBox
private bool _IsDrivingGear;
public bool IsDrivingGear
{
get { return _IsDrivingGear; }
set
{
_IsDrivingGear = value;
if (_IsDrivingGear)
{
IsDrivingGearVisibility = Visibility.Visible;
//IsKnoewWeightVisibility=Visibility.Visible;
//IsKnoewWeightNotVisibility=Visibility.Visible;
//IsCalculatedReductionRatioVisibility=Visibility.Visible;
IsKnoewWeight = false;
IsCalculatedReductionRatio = false;
}
else
{
IsDrivingGearVisibility = Visibility.Collapsed;
IsKnoewWeightVisibility = Visibility.Collapsed;
IsKnoewWeightNotVisibility = Visibility.Collapsed;
IsCalculatedReductionRatioVisibility = Visibility.Collapsed;
}
RaisePropertyChanged(() => IsDrivingGear);
}
}
private bool _IsKnoewWeight;
public bool IsKnoewWeight
{
get { return _IsKnoewWeight; }
set
{
_IsKnoewWeight = value;
if (_IsKnoewWeight)
{
if (!IsDrivingGear)
{
IsKnoewWeightVisibility = Visibility.Collapsed;
IsKnoewWeightNotVisibility = Visibility.Collapsed;
}
else
{
IsKnoewWeightVisibility = Visibility.Collapsed;
IsKnoewWeightNotVisibility = Visibility.Visible;
}
}
else
{
if (!IsDrivingGear)
{
IsKnoewWeightVisibility = Visibility.Collapsed;
IsKnoewWeightNotVisibility = Visibility.Collapsed;
}
else
{
IsKnoewWeightVisibility = Visibility.Visible;
IsKnoewWeightNotVisibility = Visibility.Collapsed;
}
}
RaisePropertyChanged(() => IsKnoewWeight);
}
}
private bool _IsCalculatedReductionRatio;
public bool IsCalculatedReductionRatio
{
get { return _IsCalculatedReductionRatio; }
set
{
_IsCalculatedReductionRatio = value;
if (_IsCalculatedReductionRatio)
{
if (!IsDrivingGear)
{
IsCalculatedReductionRatioVisibility = Visibility.Collapsed;
}
else
{
IsCalculatedReductionRatioVisibility = Visibility.Visible;
}
}
else
{
IsCalculatedReductionRatioVisibility = Visibility.Collapsed;
}
RaisePropertyChanged(() => IsCalculatedReductionRatio);
}
}
private bool _IsSpecifiedSpeed;
public bool IsSpecifiedSpeed
{
get { return _IsSpecifiedSpeed; }
set
{
_IsSpecifiedSpeed = value;
if (_IsSpecifiedSpeed)
{
IsSpecifiedSpeedVisibility=Visibility.Visible;
}else
{
IsSpecifiedSpeedVisibility = Visibility.Hidden;
}
IsSpecifiedPosition = !_IsSpecifiedSpeed;
RaisePropertyChanged(() => IsSpecifiedSpeed);
}
}
private bool _IsSpecifiedPosition;
public bool IsSpecifiedPosition
{
get { return _IsSpecifiedPosition; }
set
{
_IsSpecifiedPosition = value;
//IsSpecifiedSpeed = !_IsSpecifiedPosition;
if (_IsSpecifiedPosition)
{
IsSpecifiedPositionVisibility = Visibility.Visible;
}
else
{
IsSpecifiedPositionVisibility = Visibility.Hidden;
}
RaisePropertyChanged(() => IsSpecifiedPosition);
}
}
#endregion
#region Visility
private Visibility _IsDrivingGearVisibility;
public Visibility IsDrivingGearVisibility
{
get { return _IsDrivingGearVisibility; }
set
{
_IsDrivingGearVisibility = value;
RaisePropertyChanged(() => IsDrivingGearVisibility);
}
}
private Visibility _IsKnoewWeightVisibility;
public Visibility IsKnoewWeightVisibility
{
get { return _IsKnoewWeightVisibility; }
set
{
_IsKnoewWeightVisibility = value;
RaisePropertyChanged(() => IsKnoewWeightVisibility);
}
}
private Visibility _IsKnoewWeightNotVisibility;
public Visibility IsKnoewWeightNotVisibility
{
get { return _IsKnoewWeightNotVisibility; }
set
{
_IsKnoewWeightNotVisibility = value;
RaisePropertyChanged(() => IsKnoewWeightNotVisibility);
}
}
private Visibility _IsCalculatedReductionRatioVisibility;
public Visibility IsCalculatedReductionRatioVisibility
{
get { return _IsCalculatedReductionRatioVisibility; }
set
{
_IsCalculatedReductionRatioVisibility = value;
RaisePropertyChanged(() => IsCalculatedReductionRatioVisibility);
}
}
private Visibility _IsSpecifiedSpeedVisibility;
public Visibility IsSpecifiedSpeedVisibility
{
get { return _IsSpecifiedSpeedVisibility; }
set
{
_IsSpecifiedSpeedVisibility = value;
if (_IsSpecifiedSpeedVisibility==Visibility.Visible)
{
IsSpecifiedPositionVisibility = Visibility.Hidden;
}else if(_IsSpecifiedSpeedVisibility == Visibility.Visible)
{
IsSpecifiedPositionVisibility = Visibility.Visible;
}
RaisePropertyChanged(() => IsSpecifiedSpeedVisibility);
}
}
private Visibility _IsSpecifiedPositionVisibility;
public Visibility IsSpecifiedPositionVisibility
{
get { return _IsSpecifiedPositionVisibility; }
set
{
_IsSpecifiedPositionVisibility = value;
if (_IsSpecifiedPositionVisibility == Visibility.Visible)
{
IsSpecifiedSpeedVisibility = Visibility.Hidden;
}
else if (_IsSpecifiedPositionVisibility == Visibility.Visible)
{
IsSpecifiedSpeedVisibility = Visibility.Visible;
}
RaisePropertyChanged(() => IsSpecifiedPositionVisibility);
}
}
#endregion
#region 电机侧带轮或齿轮直径
private string _DrivingGear_Diameter_Motor;
public string DrivingGear_Diameter_Motor
{
get { return _DrivingGear_Diameter_Motor; }
set
{
_DrivingGear_Diameter_Motor = value;
RaisePropertyChanged(() => DrivingGear_Diameter_Motor);
}
}
#endregion
#region 负载侧带轮或齿轮直径
private string _DrivingGear_Diameter_Vice;
public string DrivingGear_Diameter_Vice
{
get { return _DrivingGear_Diameter_Vice; }
set
{
_DrivingGear_Diameter_Vice = value;
RaisePropertyChanged(() => DrivingGear_Diameter_Vice);
}
}
#endregion
#region 电机侧带轮或齿轮质量
private string _DrivingGear_Weight_Motor;
public string DrivingGear_Weight_Motor
{
get { return _DrivingGear_Weight_Motor; }
set
{
_DrivingGear_Weight_Motor = value;
RaisePropertyChanged(() => DrivingGear_Weight_Motor);
}
}
#endregion
#region 负载侧带轮或齿轮质量
private string _DrivingGear_Weight_Vice;
public string DrivingGear_Weight_Vice
{
get { return _DrivingGear_Weight_Vice; }
set
{
_DrivingGear_Weight_Vice = value;
RaisePropertyChanged(() => DrivingGear_Weight_Vice);
}
}
#endregion
#region 电机侧带轮或齿轮宽度
private string _DrivingGear_Width_Motor;
public string DrivingGear_Width_Motor
{
get { return _DrivingGear_Width_Motor; }
set
{
_DrivingGear_Width_Motor = value;
RaisePropertyChanged(() => DrivingGear_Width_Motor);
}
}
#endregion
#region 负载侧带轮或齿轮宽度
private string _DrivingGear_Width_Vice;
public string DrivingGear_Width_Vice
{
get { return _DrivingGear_Width_Vice; }
set
{
_DrivingGear_Width_Vice = value;
RaisePropertyChanged(() => DrivingGear_Width_Vice);
}
}
#endregion
#region 电机侧材质密度
private string _DrivingGear_Density_Motor;
public string DrivingGear_Density_Motor
{
get { return _DrivingGear_Density_Motor; }
set
{
_DrivingGear_Density_Motor = value;
RaisePropertyChanged(() => DrivingGear_Density_Motor);
}
}
#endregion
#region 负载侧材质密度
private string _DrivingGear_Density_Vice;
public string DrivingGear_Density_Vice
{
get { return _DrivingGear_Density_Vice; }
set
{
_DrivingGear_Density_Vice = value;
RaisePropertyChanged(() => DrivingGear_Density_Vice);
}
}
#endregion
#region 电机侧齿数
private string _DrivingGear_NumberOfTeeth_Motor;
public string DrivingGear_NumberOfTeeth_Motor
{
get { return _DrivingGear_NumberOfTeeth_Motor; }
set
{
_DrivingGear_NumberOfTeeth_Motor = value;
RaisePropertyChanged(() => DrivingGear_NumberOfTeeth_Motor);
}
}
#endregion
#region 负载侧齿数
private string _DrivingGear_NumberOfTeeth_Vice;
public string DrivingGear_NumberOfTeeth_Vice
{
get { return _DrivingGear_NumberOfTeeth_Vice; }
set
{
_DrivingGear_NumberOfTeeth_Vice = value;
RaisePropertyChanged(() => DrivingGear_NumberOfTeeth_Vice);
}
}
#endregion
#region 皮带重量
private string _DrivingGear_Belt_Weight;
public string DrivingGear_Belt_Weight
{
get { return _DrivingGear_Belt_Weight; }
set
{
_DrivingGear_Belt_Weight = value;
RaisePropertyChanged(() => DrivingGear_Belt_Weight);
}
}
#endregion
#region 传动效率
private string _DrivingGear_Transmission_Efficiency;
public string DrivingGear_Transmission_Efficiency
{
get { return _DrivingGear_Transmission_Efficiency; }
set
{
_DrivingGear_Transmission_Efficiency = value;
RaisePropertyChanged(() => DrivingGear_Transmission_Efficiency);
}
}
#endregion
#region 工作物质量
private string _DrivingGear_Work_Weight;
public string DrivingGear_Work_Weight
{
get { return _DrivingGear_Work_Weight; }
set
{
_DrivingGear_Work_Weight = value;
RaisePropertyChanged(() => DrivingGear_Work_Weight);
}
}
#endregion
#region 导轨的摩擦系数
private string _DrivingGear_Guideway_Friction;
public string DrivingGear_Guideway_Friction
{
get { return _DrivingGear_Guideway_Friction; }
set
{
_DrivingGear_Guideway_Friction = value;
RaisePropertyChanged(() => DrivingGear_Guideway_Friction);
}
}
#endregion
#region 丝杆轴径
private string _DrivingGear_Screw_Shaft_Diameter;
public string DrivingGear_Screw_Shaft_Diameter
{
get { return _DrivingGear_Screw_Shaft_Diameter; }
set
{
_DrivingGear_Screw_Shaft_Diameter = value;
RaisePropertyChanged(() => DrivingGear_Screw_Shaft_Diameter);
}
}
#endregion
#region 丝杆总长
private string _DrivingGear_Screw_Shaft_Length;
public string DrivingGear_Screw_Shaft_Length
{
get { return _DrivingGear_Screw_Shaft_Length; }
set
{
_DrivingGear_Screw_Shaft_Length = value;
RaisePropertyChanged(() => DrivingGear_Screw_Shaft_Length);
}
}
#endregion
#region 丝杆导程
private string _DrivingGear_Screw_Shaft_Lead;
public string DrivingGear_Screw_Shaft_Lead
{
get { return _DrivingGear_Screw_Shaft_Lead; }
set
{
_DrivingGear_Screw_Shaft_Lead = value;
RaisePropertyChanged(() => DrivingGear_Screw_Shaft_Lead);
}
}
#endregion
#region 丝杆负载侧密度
private string _DrivingGear_Screw_Shaft_Density;
public string DrivingGear_Screw_Shaft_Density
{
get { return _DrivingGear_Screw_Shaft_Density; }
set
{
_DrivingGear_Screw_Shaft_Density = value;
RaisePropertyChanged(() => DrivingGear_Screw_Shaft_Density);
}
}
#endregion
#region 效率
private string _DrivingGear_Efficiency;
public string DrivingGear_Efficiency
{
get { return _DrivingGear_Efficiency; }
set
{
_DrivingGear_Efficiency = value;
RaisePropertyChanged(() => DrivingGear_Efficiency);
}
}
#endregion
#region 预负载
private string _DrivingGear_Preload;
public string DrivingGear_Preload
{
get { return _DrivingGear_Preload; }
set
{
_DrivingGear_Preload = value;
RaisePropertyChanged(() => DrivingGear_Preload);
}
}
#endregion
#region 预压螺母内部摩擦系数
private string _DrivingGear_Preload_Nut_Friction;
public string DrivingGear_Preload_Nut_Friction
{
get { return _DrivingGear_Preload_Nut_Friction; }
set
{
_DrivingGear_Preload_Nut_Friction = value;
RaisePropertyChanged(() => DrivingGear_Preload_Nut_Friction);
}
}
#endregion
#region 外力Fa
private string _DrivingGear_External_Force;
public string DrivingGear_External_Force
{
get { return _DrivingGear_External_Force; }
set
{
_DrivingGear_External_Force = value;
RaisePropertyChanged(() => DrivingGear_External_Force);
}
}
#endregion
#region 倾斜角α
private string _DrivingGear_Inclination_Angle;
public string DrivingGear_Inclination_Angle
{
get { return _DrivingGear_Inclination_Angle; }
set
{
_DrivingGear_Inclination_Angle = value;
RaisePropertyChanged(() => DrivingGear_Inclination_Angle);
}
}
#endregion
#region 转速
private string _DrivingGear_MaxVelocity;
public string DrivingGear_MaxVelocity
{
get { return _DrivingGear_MaxVelocity; }
set
{
_DrivingGear_MaxVelocity = value;
RaisePropertyChanged(() => DrivingGear_MaxVelocity);
}
}
#endregion
#region 位移
private string _DrivingGear_Position;
public string DrivingGear_Position
{
get { return _DrivingGear_Position; }
set
{
_DrivingGear_Position = value;
RaisePropertyChanged(() => DrivingGear_Position);
}
}
#endregion
#endregion
#region 方法
private void Init()
{
IsDrivingGear = false;//默认无传动装置
IsKnoewWeight = false;//默认知道质量
IsCalculatedReductionRatio = false;//默认无减速比
IsSpecifiedSpeed = true;
DrivingGear_Diameter_Motor = "10.01";
DrivingGear_Diameter_Vice = "30.01";
DrivingGear_Weight_Motor= "10.01";
DrivingGear_Weight_Vice = "10.01";
DrivingGear_Width_Motor = "10.01";
DrivingGear_Width_Vice = "10.01";
DrivingGear_Density_Motor = "7.9";
DrivingGear_Density_Vice= "7.9";
DrivingGear_NumberOfTeeth_Motor = "10";
DrivingGear_NumberOfTeeth_Vice="10";
DrivingGear_Belt_Weight = "0.01";
DrivingGear_Transmission_Efficiency = "1";
DrivingGear_Work_Weight= "45";
DrivingGear_Guideway_Friction = "0.05";
DrivingGear_Screw_Shaft_Diameter= "20";
DrivingGear_Screw_Shaft_Length="800";
DrivingGear_Screw_Shaft_Lead= "5";
DrivingGear_Screw_Shaft_Density = "7.9";
DrivingGear_Efficiency = "0.9";
DrivingGear_Preload_Nut_Friction = "0.3";
DrivingGear_External_Force = "0";
DrivingGear_Inclination_Angle = "90";
var FA = Convert.ToDouble(DrivingGear_External_Force);
var angle = Convert.ToDouble(DrivingGear_Inclination_Angle);
var u = Convert.ToDouble(DrivingGear_Guideway_Friction);
var m = Convert.ToDouble(DrivingGear_Work_Weight);
var g = 9.807;//9.807
var F = (FA + m * g * (Math.Sin(angle*Math.PI/180) + u * Math.Cos(angle * Math.PI / 180)))/3.0;
DrivingGear_Preload = F.ToString("F2", CultureInfo.InvariantCulture);
DrivingGear_MaxVelocity = "17.00";
DrivingGear_Position = "0 ";
}
#region 数据校验
public bool CheckData()
{
bool res=false;
if (IsDrivingGear)
{
#region 电机侧带轮或齿轮直径数据
if (String.IsNullOrEmpty(DrivingGear_Diameter_Motor))
{
HandyControl.Controls.MessageBox.Error("电机侧带轮或齿轮直径数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(DrivingGear_Diameter_Motor))
{
HandyControl.Controls.MessageBox.Error("电机侧带轮或齿轮直径数据非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
#region 负载侧带轮或齿轮直径数据
if (String.IsNullOrEmpty(DrivingGear_Diameter_Vice))
{
HandyControl.Controls.MessageBox.Error("负载侧带轮或齿轮直径数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(DrivingGear_Diameter_Vice))
{
HandyControl.Controls.MessageBox.Error("负载侧带轮或齿轮直径数据非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
if (!IsKnoewWeight)
{
#region 电机侧带轮或齿轮质量
if (String.IsNullOrEmpty(DrivingGear_Weight_Motor))
{
HandyControl.Controls.MessageBox.Error("电机侧带轮或齿轮质量数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(DrivingGear_Weight_Motor))
{
HandyControl.Controls.MessageBox.Error("电机侧带轮或齿轮质量数据非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
#region 负载侧带轮或齿轮质量
if (String.IsNullOrEmpty(DrivingGear_Weight_Vice))
{
HandyControl.Controls.MessageBox.Error("负载侧带轮或齿轮质量数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(DrivingGear_Weight_Vice))
{
HandyControl.Controls.MessageBox.Error("负载侧带轮或齿轮质量数据非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
}else
{
#region 电机侧带轮或齿轮宽度
if (String.IsNullOrEmpty(DrivingGear_Width_Motor))
{
HandyControl.Controls.MessageBox.Error("电机侧带轮或齿轮宽度数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(DrivingGear_Width_Motor))
{
HandyControl.Controls.MessageBox.Error("电机侧带轮或齿轮宽度数据非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
#region 负载侧带轮或齿轮宽度
if (String.IsNullOrEmpty(DrivingGear_Width_Vice))
{
HandyControl.Controls.MessageBox.Error("负载侧带轮或齿轮宽度数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(DrivingGear_Width_Vice))
{
HandyControl.Controls.MessageBox.Error("负载侧带轮或齿轮宽度数据非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
#region 电机侧材质密度
if (String.IsNullOrEmpty(DrivingGear_Density_Motor))
{
HandyControl.Controls.MessageBox.Error("电机侧材质密度数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(DrivingGear_Density_Motor))
{
HandyControl.Controls.MessageBox.Error("电机侧材质密度数据非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
#region 负载侧材质密度
if (String.IsNullOrEmpty(DrivingGear_Density_Vice))
{
HandyControl.Controls.MessageBox.Error("负载侧材质密度数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(DrivingGear_Density_Vice))
{
HandyControl.Controls.MessageBox.Error("负载侧材质密度数据非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
}
if (IsCalculatedReductionRatio)
{
#region 电机侧齿数
if (String.IsNullOrEmpty(DrivingGear_NumberOfTeeth_Motor))
{
HandyControl.Controls.MessageBox.Error("电机侧齿数数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(DrivingGear_NumberOfTeeth_Motor))
{
HandyControl.Controls.MessageBox.Error("电机侧齿数数据非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
#region 负载侧齿数
if (String.IsNullOrEmpty(DrivingGear_NumberOfTeeth_Vice))
{
HandyControl.Controls.MessageBox.Error("负载侧齿数数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(DrivingGear_NumberOfTeeth_Vice))
{
HandyControl.Controls.MessageBox.Error("负载侧齿数数据非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
}
#region 皮带重量
if (String.IsNullOrEmpty(DrivingGear_Belt_Weight))
{
HandyControl.Controls.MessageBox.Error("皮带重量数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(DrivingGear_Belt_Weight))
{
HandyControl.Controls.MessageBox.Error("皮带重量数据非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
#region 传动效率
if (String.IsNullOrEmpty(DrivingGear_Transmission_Efficiency))
{
HandyControl.Controls.MessageBox.Error("传动效率数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatStringOne(DrivingGear_Transmission_Efficiency))
{
HandyControl.Controls.MessageBox.Error("传动效率数据非0.00-1.00范围!");
return false;
}
res = true;
}
#endregion
}
#region 工作物质量
if (String.IsNullOrEmpty(DrivingGear_Work_Weight))
{
HandyControl.Controls.MessageBox.Error("工作物质量数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(DrivingGear_Work_Weight))
{
HandyControl.Controls.MessageBox.Error("工作物质量非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
#region 导轨的摩擦系数
if (String.IsNullOrEmpty(DrivingGear_Guideway_Friction))
{
HandyControl.Controls.MessageBox.Error("导轨的摩擦系数数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(DrivingGear_Guideway_Friction))
{
HandyControl.Controls.MessageBox.Error("导轨的摩擦系数非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
#region 丝杆轴径
if (String.IsNullOrEmpty(DrivingGear_Screw_Shaft_Diameter))
{
HandyControl.Controls.MessageBox.Error("丝杆轴径数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(DrivingGear_Screw_Shaft_Diameter))
{
HandyControl.Controls.MessageBox.Error("丝杆轴径非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
#region 丝杆总长
if (String.IsNullOrEmpty(DrivingGear_Screw_Shaft_Length))
{
HandyControl.Controls.MessageBox.Error("丝杆总长数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(DrivingGear_Screw_Shaft_Length))
{
HandyControl.Controls.MessageBox.Error("丝杆总长非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
#region 丝杆导程
if (String.IsNullOrEmpty(DrivingGear_Screw_Shaft_Lead))
{
HandyControl.Controls.MessageBox.Error("丝杆导程数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(DrivingGear_Screw_Shaft_Lead))
{
HandyControl.Controls.MessageBox.Error("丝杆导程非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
#region 丝杆负载侧密度
if (String.IsNullOrEmpty(DrivingGear_Screw_Shaft_Density))
{
HandyControl.Controls.MessageBox.Error("丝杆负载侧密度数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(DrivingGear_Screw_Shaft_Density))
{
HandyControl.Controls.MessageBox.Error("丝杆负载侧密度非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
#region 效率
if (String.IsNullOrEmpty(DrivingGear_Efficiency))
{
HandyControl.Controls.MessageBox.Error("效率数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatStringOne(DrivingGear_Efficiency))
{
HandyControl.Controls.MessageBox.Error("效率非0.00-1.00范围!");
return false;
}
res = true;
}
#endregion
#region 预负载
if (String.IsNullOrEmpty(DrivingGear_Preload))
{
HandyControl.Controls.MessageBox.Error("预负载数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(DrivingGear_Preload))
{
HandyControl.Controls.MessageBox.Error("预负载非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
#region 预压螺母内部摩擦系数
if (String.IsNullOrEmpty(DrivingGear_Preload_Nut_Friction))
{
HandyControl.Controls.MessageBox.Error("预压螺母内部摩擦系数数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatStringOne(DrivingGear_Preload_Nut_Friction))
{
HandyControl.Controls.MessageBox.Error("预压螺母内部摩擦系数非0.00-1.00范围!");
return false;
}
res = true;
}
#endregion
#region 外力Fa
if (String.IsNullOrEmpty(DrivingGear_External_Force))
{
HandyControl.Controls.MessageBox.Error("外力Fa数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(DrivingGear_External_Force))
{
HandyControl.Controls.MessageBox.Error("外力Fa非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
#region 倾斜角α
if (String.IsNullOrEmpty(DrivingGear_Inclination_Angle))
{
HandyControl.Controls.MessageBox.Error("倾斜角α数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(DrivingGear_Inclination_Angle))
{
HandyControl.Controls.MessageBox.Error("倾斜角α非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
if (IsSpecifiedSpeed)
{
#region 转速
if (String.IsNullOrEmpty(DrivingGear_MaxVelocity))
{
HandyControl.Controls.MessageBox.Error("转速数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(DrivingGear_MaxVelocity))
{
HandyControl.Controls.MessageBox.Error("转速非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
}
else
{
#region 位移
if (String.IsNullOrEmpty(DrivingGear_Position))
{
HandyControl.Controls.MessageBox.Error("位移数据为空!");
return false;
}
else
{
if (!Systemglobal.Instance.IsValidTwoDigitFloatString(DrivingGear_Position))
{
HandyControl.Controls.MessageBox.Error("位移非0.00-9999.99范围!");
return false;
}
res = true;
}
#endregion
}
return res;
}
#endregion
#endregion
}
}
全局单例类
该类主要统管全局,该类还实现数据校验和科学计数法显示功能。
using MotorSelectionTools.Model;
using MotorSelectionTools.ViewModel;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MotorSelectionTools
{
public class Systemglobal
{
#region 单例
private static readonly Lazy<Systemglobal> lazy = new Lazy<Systemglobal>(() => new Systemglobal());
public static Systemglobal Instance => lazy.Value;
#endregion
#region 变量
public readonly (double lowerBound, double upperBound, int exponent, bool divideBy10000)[] ranges_1 = new[]
{
(10000.0, 99999.9, 4, true),
(1000.0, 9999.9, 3, true),
(100.0, 999.9, 2, true),
(10.0, 99.9, 1, true),
(1.0, 9.9, 0, true),
(0.1, 0.9, -1, false),
(0.01, 0.99, -2, false),
(0.001, 0.999, -3, false),
(0.0001, 0.9999, -4, false),
(0.00001, 0.09999, -5, false),
(0.000001, 0.009999, -6, false),
(0.0000001, 0.0009999, -7, false),
(0.00000001, 0.00009999, -8, false),
(0.000000001, 0.000009999, -9, false),
(0.0000000001, 0.0000009999, -10, false),
(0.00000000001, 0.00000009999, -11, false),
(0.000000000001, 0.000000009999, -12, false),
(0.0000000000001, 0.0000000009999, -13, false),
(0.00000000000001, 0.00000000009999, -14, false),
(0.000000000000001, 0.000000000009999, -15, false),
(0.0000000000000001, 0.0000000000009999, -16, false),
(0.00000000000000001, 0.00000000000009999, -17, false),
(0.000000000000000001, 0.000000000000009999, -18, false),
(0.0000000000000000001, 0.0000000000000009999, -19, false),
(0.00000000000000000001, 0.00000000000000009999, -20, false),
};
#endregion
#region 构造函数
public Systemglobal()
{ }
#endregion
#region View
#endregion
#region Model
#endregion
#region ViewModel
public ScrewMechaninismPageViewModel ScrewMechaninismPageViewModel { get; set; }
#endregion
#region 方法
public bool IsValidTwoDigitFloatString(string input)
{
if (string.IsNullOrEmpty(input))
{
return false;
}
// 尝试将输入字符串解析为浮点数
if (float.TryParse(input, out float floatValue))
{
// 检查浮点数是否在有效范围内(0.00 到 9999.99)
if (floatValue >= 0.00f && floatValue <= 9999.99f)
{
检查字符串是否恰好包含两位小数
//int decimalIndex = input.IndexOf('.');
//if (decimalIndex >= 0 && input.Length - decimalIndex - 1 == 2)
//{
// return true;
//}
return true;
}
}
return false;
}
public bool IsValidTwoDigitFloatStringOne(string input)
{
if (string.IsNullOrEmpty(input))
{
return false;
}
// 尝试将输入字符串解析为浮点数
if (float.TryParse(input, out float floatValue))
{
// 检查浮点数是否在有效范围内(0.00 到 9999.99)
if (floatValue >= 0.00f && floatValue <= 1.00f)
{
检查字符串是否恰好包含两位小数
//int decimalIndex = input.IndexOf('.');
//if (decimalIndex >= 0 && input.Length - decimalIndex - 1 == 2)
//{
// return true;
//}
return true;
}
}
return false;
}
public bool IsValidOneDigitFloatString(string input)
{
if (string.IsNullOrEmpty(input))
{
return false;
}
// 尝试将输入字符串解析为浮点数
if (float.TryParse(input, out float floatValue))
{
// 检查浮点数是否在有效范围内(0.00 到 9999.99)
if (floatValue >= 0.0f && floatValue <= 9999.9f)
{
检查字符串是否恰好包含两位小数
//int decimalIndex = input.IndexOf('.');
//if (decimalIndex >= 0 && input.Length - decimalIndex - 1 == 2)
//{
// return true;
//}
return true;
}
}
return false;
}
public bool IsInRange(double value, double lowerBound, double upperBound)
{
return value >= lowerBound && value <= upperBound;
}
public string FormatJ1(ref Data j1, (double lowerBound, double upperBound, int exponent, bool divideBy10000)[] ranges_1)
{
double formattedValue = 0;
foreach (var range in ranges_1)
{
if (IsInRange(j1.Value, range.lowerBound, range.upperBound))
{
if (range.divideBy10000)
{
formattedValue = j1.Value / (double)Math.Pow(10, range.exponent);
}else
{
formattedValue = j1.Value * Math.Pow(10, -range.exponent);
}
j1.Value = formattedValue;
j1.Power = range.exponent;
string temp = "";
if (j1.Power==0)
{
temp = SimplifyDecimalHandle(j1);
return $"= {temp}";
}else
{
if (j1.Power == -1)
{
formattedValue = j1.Value / 10.0;
j1.Value = formattedValue;
j1.Power = 0;
temp = SimplifyDecimalHandle(j1);
return $"= {temp}";
}
else if (j1.Power == 1)
{
formattedValue = j1.Value * 10.0;
j1.Value = formattedValue;
j1.Power = 0;
temp = SimplifyDecimalHandle(j1);
return $"= {temp}";
}
else if (j1.Power == 2)
{
formattedValue = j1.Value * 100.0;
j1.Value = formattedValue;
j1.Power = 0;
temp = SimplifyDecimalHandle(j1);
return $"= {temp}";
}
else if (j1.Power == 3)
{
formattedValue = j1.Value * 1000.0;
j1.Value = formattedValue;
j1.Power = 0;
temp = SimplifyDecimalHandle(j1);
return $"= {temp}";
}
//temp = $"{j1.Value.ToString("F4", CultureInfo.InvariantCulture)}";
//if (decimal.TryParse(temp, NumberStyles.Any, CultureInfo.InvariantCulture, out decimal value1))
//{
// temp = SimplifyDecimal(value1);
//}
temp =SimplifyDecimalHandle(j1);
return $"= {temp} \\times 10^{{{range.exponent}}}";
//return $"= {j1.Value.ToString("F4", CultureInfo.InvariantCulture)} \\times 10^{{{range.exponent}}}";
}
}
}
return "Out of Range";
}
public string SimplifyDecimal(decimal value)
{
string decimalPart = (value - Math.Floor(value)).ToString("F4", CultureInfo.InvariantCulture).Substring(2);
if (decimalPart.Length > 1 && decimalPart.Substring(1).All(c => c == '0'))
{
return Math.Floor(value).ToString(CultureInfo.InvariantCulture) +"."+ decimalPart[0];
}
return value.ToString(CultureInfo.InvariantCulture);
}
public string SimplifyDecimalHandle(Data j1)
{
var temp = $"{j1.Value.ToString("F4", CultureInfo.InvariantCulture)}";
if (decimal.TryParse(temp, NumberStyles.Any, CultureInfo.InvariantCulture, out decimal value1))
{
temp = Systemglobal.Instance.SimplifyDecimal(value1);
}
return temp ;
}
#endregion
}
}