I am currently trying to integrate an STL File Viewer in my WPF application. I have been using the HelixToolKit to do this and currently can display an STL/OBJ/3D file in the display, however, the location of the STL code is hard coded in the CS file. Here is my code
private const string MODEL_PATH = @"C:\FILE LOCATION ON DISK";
public MainWindow()
{
InitializeComponent();
ModelVisual3D device3D = new ModelVisual3D();
device3D.Content = Display3d(MODEL_PATH);
viewPort3d.Children.Add(device3D);
}
private Model3D Display3d(string model)
{
Model3D device = null;
try
{
//Adding a gesture here
viewPort3d.RotateGesture = new MouseGesture(MouseAction.LeftClick);
//Import 3D model file
ModelImporter import = new ModelImporter();
//Load the 3D model file
device = import.Load(model);
}
catch (Exception e)
{
// Handle exception in case can not file 3D model
MessageBox.Show("Exception Error : " e.StackTrace);
}
return device;
}
I want the user to be able to import their own STL files into the viewer and view them. I tried to implement a browse button and coded it as such
public void btnBrowse_Click(object sender, RoutedEventArgs e)
{
// Create OpenFileDialog
OpenFileDialog dlg = new OpenFileDialog();
// Set filter for file extension and default file extension
dlg.DefaultExt = ".stl";
dlg.Filter = "3D Objects (.stl)|*.stl";
// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
string filename = dlg.FileName;
}
}
but I do not know how to handle the MODEL_PATH with this button. Any help would be greatly appreciated.
Regards KillerSwitch
CodePudding user response:
So I figured out how to do it. Here's how incase it helps anyone else in the future.
private Model3D Display3d(string model)
{
Model3D device = null;
try
{
//Adding a gesture here
viewPort3d.RotateGesture = new MouseGesture(MouseAction.LeftClick);
//Import 3D model file
ModelImporter import = new ModelImporter();
//Load the 3D model file
device = import.Load(model);
}
catch (Exception e)
{
// Handle exception in case can not file 3D model
MessageBox.Show("Exception Error : " e.StackTrace);
}
return device;
}
public void btnBrowse_Click(object sender, RoutedEventArgs e)
{
string fileName;
var myDialog = new OpenFileDialog();
try
{
myDialog.Filter = "SE Model File|*.par;*.psm;*.asm;*.stl;*.obj";
if ((bool)myDialog.ShowDialog())
{
Mouse.OverrideCursor = Cursors.Wait;
fileName = myDialog.FileName;
Model3D newModel3D;
var newMOdelImporter = new ModelImporter();
var fileInf = new FileInfo(fileName);
string extn = fileInf.Extension;
if (extn == ".stl" | extn == ".STL")
{
newModel3D = newMOdelImporter.Load(fileName);
}
else
{
}
// Now launch the model in Viewport.
var device3D = new ModelVisual3D();
var lights = new DefaultLights();
device3D.Content = Display3d(fileName);
this.viewPort3d.RotateGesture = new MouseGesture(MouseAction.LeftClick);
this.viewPort3d.Children.Clear();
this.viewPort3d.Children.Add(device3D);
this.viewPort3d.Children.Add(lights);
this.viewPort3d.ShowCoordinateSystem = true;
this.viewPort3d.ZoomAroundMouseDownPoint = true;
this.viewPort3d.ZoomExtentsWhenLoaded = true;
this.viewPort3d.ResetCamera();
this.viewPort3d.Title = "SE File : " fileName;
// viewPort3d.ShowTriangleCountInfo = True
Mouse.OverrideCursor = Cursors.Arrow;
}
}
catch (Exception ex)
{
Mouse.OverrideCursor = Cursors.Arrow;
}
}