0

Import Model From FileStream or Stream... [Solved]

Anonymous 5 years ago 0
This discussion was imported from CodePlex

ysinotelodigo wrote at 2012-05-01 03:11:

Hey,

I want to load or import model from stream in C#... I have played with this library and I did it.

 

        <helix:HelixViewport3D Background="AliceBlue"  ShowViewCube="False" Name="Pantallazo">
                <helix:DefaultLights/>
          <helix:FileModelVisual3D Source="Ferarri40.3ds" >
                <helix:FileModelVisual3D.Transform>
                    <ScaleTransform3D ScaleY="0.01" ScaleX="0.01" ScaleZ="0.01"/>
                </helix:FileModelVisual3D.Transform>
            </helix:FileModelVisual3D>
        </helix:HelixViewport3D>

In code-behind is this...

FileModelVisual3D a = new FileModelVisual3D();
a.Source = "Ferarri40.3ds";
ScaleTransform3D b = new ScaleTransform3D(0.05, 0.05, 0.05);
a.Transform = b;
Pantallazo.Children.Add(a);
//Pantallazo is the name of HelixViewPort3D
 

But I need read the 3ds from stream because the model will come from DataBase I did it...
But I don't know how I go on...

FileStream c = new FileStream("Ferarri40.3ds", FileMode.Open, FileAccess.Read, FileShare.Read);  
FileModelVisual3D d = new FileModelVisual3D();
d.Source = ¿¿?? //HELP!!
Pantallazo.Children.Add(d);

 

This library is nice!


objo wrote at 2012-05-02 21:08:

The FileModelVisual3D cannot be bound to a stream.

Create a "StudioReader" instance, and use the Read(Stream) method. The result is a Model3D you can bind to the Content of a ModelVisual3D!

http://msdn.microsoft.com/en-us/library/system.windows.media.media3d.modelvisual3d.content.aspx


ysinotelodigo wrote at 2012-05-06 19:01:

Hello,

Thank you, very much for your answer. But I have several questions, I'm sorry for I am new and I don't speak english well.

My final objective is that my application can put and remove 3d model. This model will be in 3ds or obj format. (Perphaps en 3ds because it has a texture). So that I decided add and remove children...

I understand that I have create a "StudioReader" class with a method called Read(stream) for example. And the result will be a System.Windows.Media.Media3D.Model3D But, How could I transform this stream (in 3ds) to Model3D?

Thank you!


objo wrote at 2012-05-07 17:57:

Yes, you have to create an instance of the StudioReader class, and use the read method.

If your 3d viewport contains the following element (defined in xaml)

<ModelVisual3D Content="{Binding MyModel}"/>

you can import your stream by

var reader = new StudioReader();
this.MyModel = reader.Read(myStream);

this requires the MyModel property to be defined in your data context (ViewModel)

public Model3D MyModel { get; set; }

Your data context (ViewModel) should also implement INotifyPropertyChanged, then you can simply write

this.MyModel = null;

 to remove the model again.

http://msdn.microsoft.com/en-us/library/system.windows.media.media3d.modelvisual3d.aspx

http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx


objo wrote at 2012-05-07 17:58:

Note that if you want to use textures, the StudioReader only supports reading these from files at the moment - not from streams...


ysinotelodigo wrote at 2012-05-16 00:36:

Sorry to disturb again.

I tried to follow your instrutions but I don't know how i go on...

I created this .xaml

 

<Window x:Class="WpfApplication7.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:local="clr-namespace:WpfApplication7"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:MainWindowViewModel/>
    </Window.DataContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Label Content="{Binding Texto}"/>
        <Viewport3D Name="myViewport" Grid.Row="1">
            <Viewport3D.Camera>
                <PerspectiveCamera Position="0,0,500" />
            </Viewport3D.Camera>
            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <DirectionalLight Color="#FFFffFFF" Direction="-3,-4,-5" />
                </ModelVisual3D.Content>
            </ModelVisual3D>
            <ModelVisual3D Content="{Binding MyModel}"/>
        </Viewport3D>

    </Grid>
</Window>

 

And this ViewModel:

 

using System.Text;
using System.Windows.Media.Media3D;
using HelixToolkit;
using System.IO;

namespace WpfApplication7
{
    class MainWindowViewModel
    {
        public MainWindowViewModel()
        {
           Texto = "Prueba";

           var reader = new StudioReader();
           // This works well.
           //MyModel = reader.Read("Ferarri40.3ds");

           // But, I need that (Stream come from DataBase)
           // Why does it work?
            Stream myStream = new FileStream("Ferarri40.3ds", FileMode.Open, FileAccess.Read, FileShare.Read);
            MyModel = reader.Read(myStream);
        }

        public String Texto { set; get; }
        public Model3D MyModel { get; set; }
    }
}

 

I don´t know why it doesn't work?

I want to add that my application has to add and remove several model... I''ve been learned MVVM... if you could say the steps, I would appreciate it.

Thank you very much



objo wrote at 2012-05-31 22:35:

I see, the reader didn't know where to look for the textures and was throwing an exception since TexturePath was null. Add the following line

            reader.TexturePath = @"directoryofyour3dsmodel";

I am checking in a fix! 


ysinotelodigo wrote at 2012-06-01 11:07:

Thank you, very much!

Your indications have been userful.

Here, I have copied my code for other persons....

MainWindows.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:helix="clr-namespace:HelixToolkit;assembly=HelixToolkit"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Window.DataContext>
        <local:MainWindowViewModel/>
    </Window.DataContext>
    <Grid>
        <helix:HelixViewport3D Background="AliceBlue"  ShowViewCube="False" Name="Pantallazo">
            <helix:DefaultLights/>
            <ModelVisual3D Content="{Binding MyModel}">
                <ModelVisual3D.Transform>
                    <ScaleTransform3D ScaleY="0.05" ScaleX="0.05" ScaleZ="0.05"/>
                </ModelVisual3D.Transform>
            </ModelVisual3D>
        </helix:HelixViewport3D>
    </Grid>
</Window>

MainWindowViewModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PFG;
using System.Windows.Media.Media3D;
using System.IO;
using HelixToolkit;

namespace WpfApplication1
{
    public class MainWindowViewModel : BaseINPC
    {
        public MainWindowViewModel()
        {
            FileStream c = new FileStream("Ferarri40.3ds", FileMode.Open, FileAccess.Read, FileShare.Read);
            var reader = new StudioReader();
            reader.TexturePath = ".";
            MyModel = reader.Read(c);
        }

        private Model3D mymodel;
        public Model3D MyModel
        {
            get
            {
                return mymodel;
            }
            set
            {
                mymodel = value;
            OnPropertyChanged("MyModel");
            }
        }
    }
}

Sorry to be disturb.