How to make a simple 3D Ifc Viewer in WPF with Xbim

881 views Asked by At

I’m a C# and a WPF beginner and I’m trying for days to make an Ifc Viewer in my app. I tried many ways to do it but I did not succeed and I turn around.

I got inspired by an other topic on the forum : “How to use Xbim in WPF to visualise an .IFC in 3D”. Currently my xaml file seems like this :

xmlns:presentation="http://schemas.Xbim.com/Presentation"

<Window.Resources>
    <ObjectDataProvider x:Key="ModelProvider"  IsInitialLoadEnabled="False" />
</Window.Resources>

<Grid Name="MainFrame" DataContext="{StaticResource ModelProvider}">
    <presentation:DrawingControl3D x:Name="test3D" Model="{Binding ObjectInstance}"/>
</Grid>

And my C# file :

using System.Windows;
using System.Windows.Data;
using Xbim.Ifc;
using Xbim.ModelGeometry.Scene;


namespace okokokok
{

    public partial class MainWindow : Window
    {
        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:\Users\e.mazzone\Desktop\Mission_C#\Test_Ifc_1.ifc");
            var context = new Xbim3DModelContext(model);
            context.CreateContext();
            ModelProvider.ObjectInstance = model;
        }
    }
}

This open the Drawing control but we cannot see the IFC. I tried every IFC type but it didn't work. I think there is probably a binding problem between the xaml file and the C# file.

Would anyone be able to advise me or point me towards a good tutorial?

Thanks!

1

There are 1 answers

1
György Müller On

I did create a simple IFC viewer not long ago, my DrawingControl3D is called DrawingControl, and the simplified code looks something like this:

xaml:

<xbim:DrawingControl3D
            Grid.Column="1"
            Grid.Row="1"
            x:Name="DrawingControl"
            SelectedEntity="{Binding Path=SelectedItem, ElementName=MainWindow, Mode=TwoWay}"
            Model="{Binding}"
            Focusable="True"
            MouseMove="DrawingControl_MouseMove">
            <xbim:DrawingControl3D.Background>
                <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
                    <GradientStop Color="#D1CBC4" Offset="1"/>
                    <GradientStop Color="#C8FCFF"/>
                </LinearGradientBrush>
            </xbim:DrawingControl3D.Background>
        </xbim:DrawingControl3D>

cs:

    IfcStore ifcModel = IfcStore.Open("ifcFile.ifc");

    var context = new Xbim3DModelContext(ifcModel);
    context.CreateContext();

    DrawingControl.Model = ifcModel;
    DrawingControl.LoadGeometry(ifcModel);

For me it works as expected.