.NET6: 开发基于WPF的摩登三维工业软件 (2)
在《.NET6: 开发基于WPF的摩登三维工业软件 (1)》我们创建了一个"毛坯"界面,距离摩登还差一段距离。本文将对上一阶段的成果进行深化,实现当下流行的暗黑风格UI。
1 设置暗黑主题
利用MergedDictionaries配置,在已有的通用风格基础上添加Dark.Blue主题。代码如下:
App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Fluent;Component/Themes/Generic.xaml" />
<ResourceDictionary Source="pack://application:,,,/Fluent;component/Themes/Themes/Dark.Blue.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
运行一下:
主题已经变成了暗黑模式。
2 给Button增加图标
2.1 引入图片
在项目目录下增加Resources目录,把circle.png复制到Resources目录下。
在项目浏览器中把图片添加到项目,选择Resources目录,右键菜单:
选择Resources/circle.png
2.2 设置图片属性
选择图片,在属性面板中设置:
- 复制到输出目录: 始终复制
- 生成操作:内容
按照上述步骤加入其他的图片。
2.3 Ribbon Button中使用图片
为Fluent:Button设置Icon属性,引用图片资源
<!--Tabs-->
<Fluent:RibbonTabItem Header="建模">
<Fluent:RibbonGroupBox Header="基本体" IsLauncherVisible="False">
<Fluent:Button Header="直线" Icon="/Resources/line.png" Size="Large"/>
<Fluent:Button Header="圆弧" Icon="/Resources/arc3pts.png" Size="Large"/>
<Fluent:Button Header="圆形" Icon="/Resources/circle.png" Size="Large"/>
</Fluent:RibbonGroupBox>
</Fluent:RibbonTabItem>
<Fluent:RibbonTabItem Header="设置">
</Fluent:RibbonTabItem>
运行一下:
3 增加Button响应消息
3.1 增加路由命令
定义RoutedCommand用于响应Button的点击事件。
MainWindow.xaml.cs
public partial class MainWindow
{
// 定义路由命令
public static readonly RoutedCommand ExecuteCommand = new RoutedCommand("Rapid", typeof(MainWindow));
public MainWindow()
{
InitializeComponent();
// 绑定响应函数
CommandBindings.Add(new CommandBinding(ExecuteCommand, OnExecuteCommand));
}
private void RibbonWindow_Loaded(object sender, RoutedEventArgs e)
{
}
// 处理点击命令
private void OnExecuteCommand(object sender, ExecutedRoutedEventArgs e)
{
}
}
3.2 XAML里绑定Button绑定事件
- Command: 绑定的命令
- CommandParameter: 命令参数
MainWindow.xaml
<Fluent:Button Header="直线" Icon="/Resources/line.png" Size="Large" Command="{x:Static local:MainWindow.ExecuteCommand}"
CommandParameter="line"/>
<Fluent:Button Header="圆弧" Icon="/Resources/arc3pts.png" Size="Large" Command="{x:Static local:MainWindow.ExecuteCommand}"
CommandParameter="arc"/>
<Fluent:Button Header="圆形" Icon="/Resources/circle.png" Size="Large" Command="{x:Static local:MainWindow.ExecuteCommand}"
CommandParameter="circle"/>
3.3 命令处理函数
MainWindow.xaml.cs
csharp // 处理点击命令 private void OnExecuteCommand(object sender, ExecutedRoutedEventArgs e) { switch (e.Parameter.ToString()) { case "line": { var shape = SketchBuilder.MakeLine(new GPnt(0, 0, 0), new GPnt(10, 10, 0)); mView3d.ShowShape(shape, ColorTable.AliceBlue); } break; case "arc": { var shape = SketchBuilder.MakeArcOfCircle(new GPnt(0, 0, 0), new GPnt(10, 10, 0), new GPnt(5,15,0)); mView3d.ShowShape(shape, ColorTable.AliceBlue); } break; case "circle": { var shape = SketchBuilder.MakeCircle(new GPnt(0, 0, 0), 5, GP.DZ()); mView3d.ShowShape(shape, ColorTable.AliceBlue); } break; } }
运行一下,挨个Button点一遍:
4 综合建模
爱心巧克力
一种实现方法:
- 由线生成爱心平面轮廓
- 轮廓填充成面
- 面拉伸成体
- 对体倒角
{
var arc1 = SketchBuilder.MakeArcOfCircle(new GPnt(0, 2, 0), new GPnt(10, 0, 0), new GPnt(5, 5, 0));
var arc2 = SketchBuilder.MakeArcOfCircle(new GPnt(0, 2, 0), new GPnt(-10, 0, 0), new GPnt(-5, 5, 0));
var bottomPt = new GPnt(0, -12, 0);
var line1 = SketchBuilder.MakeLine(new GPnt(-10, 0, 0), bottomPt);
var line2 = SketchBuilder.MakeLine(bottomPt, new GPnt(10, 0, 0));
var shapeList = new TopoShapeList();
shapeList.Add(arc1);
shapeList.Add(arc2);
shapeList.Add(line1);
shapeList.Add(line2);
var wire = SketchBuilder.MakeWire(shapeList);
var face = SketchBuilder.MakePlanarFace(wire);
var shape = FeatureTool.Extrude(face, 5, GP.DZ());
shape = FeatureTool.Fillet(shape, 1);
mView3d.ShowShape(shape, ColorTable.PaleVioletRed);
}
5 总结
本文通过简洁的示例实现了暗黑风格的程序界面,为Ribbon增加Icon图片,并通过WPF的命令路由机制实现了按钮消息处理方法。最后,通过AnyCAD的建模API创建复杂的形状:爱心巧克力。本文所有代码:Valentine's Day
最后,祝天下所有的程序员情人节快乐!
var mesh = FontManager.Instance().CreateMesh("情人节快乐!");
var material = MeshPhongMaterial.Create("love-material");
material.SetColor(ColorTable.OrangeRed);
var shape = new PrimitiveSceneNode(mesh, material);
mView3d.ShowSceneNode(shape);