我在HelixViewport3D中添加了不同类型的3D对象。我的问题是如何将3D对象/模型保存在文件中并稍后打开,就像典型的CAD应用程序一样。
HelixViewport3D是用户在控件UserControl.xaml中定义的。
UserControl如下所示:
<UserControl x:Class="SubWindows.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:HelixToolkit="clr-namespace:HelixToolkit.Wpf;assembly=HelixToolkit.Wpf"
Height="350" Width="525" MouseRightButtonDown="OnMouseRightButtonDown" TabIndex="0" MouseMove="UserControl_MouseMove" Opacity="1">
<Grid>
<HelixToolkit:HelixViewport3D x:Name="ViewPort" ZoomExtentsWhenLoaded="True" ItemsSource="{Binding}">
<HelixToolkit:SunLight Brightness="0.4" Ambient="0.1" />
<ModelVisual3D Content="{Binding Model}"/>
<ModelVisual3D x:Name="SolidModels"/>
<ModelVisual3D x:Name="ScreenLineModels"/>
<ModelVisual3D x:Name="DimLinesModels"/>
<ModelVisual3D x:Name="TagsModels"/>
</HelixToolkit:HelixViewport3D>
<Canvas Name="TagsOverlay" IsHitTestVisible="False" ClipToBounds="False"/>
<Canvas Name="DimsOverlay" IsHitTestVisible="False" ClipToBounds="False" />
</Grid>
保存文件事件如下:
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
saveFileDialog.Filter = "pvp Files (*.pvp)|*.pvp|Text Files (*.xaml)|*.xaml|All Files (*.*)|*.*";
if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
{
string FileName = saveFileDialog.FileName;
string fileNameExtention = System.IO.Path.GetExtension(FileName);
fileNameExtention = fileNameExtention.ToLower();
if (fileNameExtention == ".xaml")
{
uc.ViewPort.Export(FileName);
}
}
}
&#34;我们&#34;是UserControl1的一个实例,定义为UserControl1 uc = new UserControl1();当我saveToolStripMenuItem_Click保存文件时,下面有一个错误快照。
但ViewPort的一部分以xaml格式保存。
任何解决问题的意见都将受到赞赏。
答案 0 :(得分:0)
首先,您需要使用Export()导出viewport3D [例如Helix Vieport3D的viewport.Export(/sample.xaml)方法,它将为您提供一个XAML文件(如果您将模型导出为.xaml),其中包含键值, 稍后将导出的XAML文件添加到项目目录中,并使用下面的代码。
UserConrol.xaml文件代码
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="<Path towards your exported xaml file>\sample.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
导入XAML文件的代码
HelixViewport3D viewport; //Extra viewport to store the childrens
viewport= (HelixViewport3D)FindResource("Model");
// "Model" is the key value of my exported XAML file
List<Visual3D> items = new List<Visual3D>(); // List is use to store the childrens of ResourceDirectories xaml.
//Adds the children inside List
for (int i=(viewport.Children.Count - 1);i>=0;i--)
{
items.Add(viewport.Children[i]);
viewport.Children.RemoveAt(i);
}
//viewport3D.Children.Clear();
foreach(Visual3D item in items)
{
viewport3D.Children.Add(item);
}