'How to use Xbim in WPF to visualise an .IFC in 3D
I'm trying to learn how to use Xbim in a WPF app to create a 3D view of a model that the user can spin around and view. I load my ifc file in the C# code as follows:
var model = IfcStore.Open("Images/test.ifc");
var context = new Xbim3DModelContext(model);
context.CreateContext();
I then set up a viewport in XAML as follows:
<xbim:DrawingControl3D Name="test3D"></xbim:DrawingControl3D>
I am struggling to work out how to link the two and display the loaded model in the viewport. Would anyone be able to advise / point be towards a good tutorial? If I run the app as stands I can see an empty viewport that I can move around, just need to get the model in there.
Thanks!
Solution 1:[1]
You need some more code.
private ObjectDataProvider ModelProvider
{
get
{
return MainFrame.DataContext as ObjectDataProvider;
}
}
public void openFile()
{
var model = IfcStore.Open("Images/test.ifc");
var context = new Xbim3DModelContext(model);
context.CreateContext();
ModelProvider.ObjectInstance = model;
ModelProvider.Refresh();
}
And on the xaml side :
<Window.Resources>
<ObjectDataProvider x:Key="ModelProvider" IsInitialLoadEnabled="False" />
</Window.Resources>
<Grid Name="MainFrame" DataContext="{StaticResource ModelProvider}">
<xbim:DrawingControl3D x:Name="test3D" Model="{Binding}" />
</Grid>
Solution 2:[2]
Thank you for all those comments. Sadly, this did not work for me. I had to move the line ModelProvider.Refresh(); to an event named
void MainWindow_Loaded( object sender, RoutedEventArgs e )
{
ModelProvider.Refresh();
}
also, i added in the xaml file follwing Loaded="MainWindow_Loaded">
Now, my two files look like this:
<Window x:Class="wpf2bim.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:wpf2bim"
xmlns:presentation="http://schemas.Xbim.com/Presentation"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525"
Loaded="MainWindow_Loaded">
<Window.Resources>
<ObjectDataProvider x:Key="ModelProvider" IsInitialLoadEnabled="False" />
</Window.Resources>
<Grid Name="MainFrame" DataContext="{StaticResource ModelProvider}">
<presentation:DrawingControl3D x:Name="test3D" Model="{Binding}" />
</Grid>
and the c# file
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
openFile();
}
void MainWindow_Loaded( object sender, RoutedEventArgs e )
{
ModelProvider.Refresh();
}
private ObjectDataProvider ModelProvider
{
get
{
return MainFrame.DataContext as ObjectDataProvider;
}
}
public void openFile()
{
var model = IfcStore.Open(@"C:\SampleHouse.ifc");
var context = new Xbim3DModelContext(model);
context.CreateContext();
ModelProvider.ObjectInstance = model;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rachid Benbrik |
| Solution 2 | Andru |
