2022-01-25 19:51:37 +08:00
|
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Runtime.InteropServices.ComTypes;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
using System.Windows.Media.Media3D;
|
|
|
|
|
using Common.Utilities;
|
|
|
|
|
using HelixToolkit.Wpf;
|
2022-08-24 04:45:27 +08:00
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Library;
|
2022-01-25 19:51:37 +08:00
|
|
|
|
using Bitmap = System.Drawing.Bitmap;
|
2022-12-14 20:37:23 +08:00
|
|
|
|
using Color = System.Windows.Media.Color;
|
|
|
|
|
using ColorConverter = System.Windows.Media.ColorConverter;
|
2022-01-25 19:51:37 +08:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.PowerToys.ThumbnailHandler.Stl
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Stl Thumbnail Provider.
|
|
|
|
|
/// </summary>
|
2022-12-14 20:37:23 +08:00
|
|
|
|
public class StlThumbnailProvider
|
2022-01-25 19:51:37 +08:00
|
|
|
|
{
|
2022-12-14 20:37:23 +08:00
|
|
|
|
public StlThumbnailProvider(string filePath)
|
|
|
|
|
{
|
|
|
|
|
FilePath = filePath;
|
|
|
|
|
Stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the file path to the file creating thumbnail for.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string FilePath { get; private set; }
|
|
|
|
|
|
2022-01-25 19:51:37 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the stream object to access file.
|
|
|
|
|
/// </summary>
|
2022-12-14 20:37:23 +08:00
|
|
|
|
public Stream Stream { get; private set; }
|
2022-01-25 19:51:37 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The maximum dimension (width or height) thumbnail we will generate.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private const uint MaxThumbnailSize = 10000;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Loads the Stl model into a Viewport3D and renders a bitmap of it.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="stream">The Stream instance for the Stl content.</param>
|
|
|
|
|
/// <param name="cx">The maximum thumbnail size, in pixels.</param>
|
|
|
|
|
/// <returns>A thumbnail rendered from the Stl model.</returns>
|
|
|
|
|
public static Bitmap GetThumbnail(Stream stream, uint cx)
|
|
|
|
|
{
|
|
|
|
|
if (cx > MaxThumbnailSize || stream == null || stream.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Bitmap thumbnail = null;
|
|
|
|
|
|
|
|
|
|
var stlReader = new StLReader
|
|
|
|
|
{
|
2022-08-24 04:45:27 +08:00
|
|
|
|
DefaultMaterial = new DiffuseMaterial(new SolidColorBrush(DefaultMaterialColor)),
|
2022-01-25 19:51:37 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var model = stlReader.Read(stream);
|
|
|
|
|
|
|
|
|
|
if (model.Bounds == Rect3D.Empty)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var viewport = new System.Windows.Controls.Viewport3D();
|
|
|
|
|
|
2022-12-14 20:37:23 +08:00
|
|
|
|
viewport.Measure(new System.Windows.Size(cx, cx));
|
2022-01-25 19:51:37 +08:00
|
|
|
|
viewport.Arrange(new Rect(0, 0, cx, cx));
|
|
|
|
|
|
|
|
|
|
var modelVisual = new ModelVisual3D()
|
|
|
|
|
{
|
|
|
|
|
Transform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), 180)),
|
|
|
|
|
};
|
|
|
|
|
viewport.Children.Add(modelVisual);
|
|
|
|
|
viewport.Children.Add(new DefaultLights());
|
|
|
|
|
|
|
|
|
|
var perspectiveCamera = new PerspectiveCamera
|
|
|
|
|
{
|
|
|
|
|
Position = new Point3D(1, 2, 1),
|
|
|
|
|
LookDirection = new Vector3D(-1, -2, -1),
|
|
|
|
|
UpDirection = new Vector3D(0, 0, 1),
|
|
|
|
|
FieldOfView = 20,
|
|
|
|
|
NearPlaneDistance = 0.1,
|
|
|
|
|
FarPlaneDistance = double.PositiveInfinity,
|
|
|
|
|
};
|
|
|
|
|
viewport.Camera = perspectiveCamera;
|
|
|
|
|
|
|
|
|
|
modelVisual.Content = model;
|
|
|
|
|
|
|
|
|
|
perspectiveCamera.ZoomExtents(viewport);
|
|
|
|
|
|
|
|
|
|
var bitmapExporter = new BitmapExporter
|
|
|
|
|
{
|
|
|
|
|
Background = new SolidColorBrush(Colors.Transparent),
|
|
|
|
|
OversamplingMultiplier = 1,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var bitmapStream = new MemoryStream();
|
|
|
|
|
|
|
|
|
|
bitmapExporter.Export(viewport, bitmapStream);
|
|
|
|
|
|
|
|
|
|
bitmapStream.Position = 0;
|
|
|
|
|
|
|
|
|
|
thumbnail = new Bitmap(bitmapStream);
|
|
|
|
|
|
|
|
|
|
return thumbnail;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-14 20:37:23 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Generate thumbnail bitmap for provided Gcode file/stream.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="cx">Maximum thumbnail size, in pixels.</param>
|
|
|
|
|
/// <returns>Generated bitmap</returns>
|
|
|
|
|
public Bitmap GetThumbnail(uint cx)
|
2022-01-25 19:51:37 +08:00
|
|
|
|
{
|
|
|
|
|
if (cx == 0 || cx > MaxThumbnailSize)
|
|
|
|
|
{
|
2022-12-14 20:37:23 +08:00
|
|
|
|
return null;
|
2022-01-25 19:51:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-26 21:02:31 +08:00
|
|
|
|
if (global::PowerToys.GPOWrapper.GPOWrapper.GetConfiguredStlThumbnailsEnabledValue() == global::PowerToys.GPOWrapper.GpoRuleConfigured.Disabled)
|
|
|
|
|
{
|
|
|
|
|
// GPO is disabling this utility.
|
2022-12-14 20:37:23 +08:00
|
|
|
|
return null;
|
2022-10-26 21:02:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-14 20:37:23 +08:00
|
|
|
|
using (var memStream = new MemoryStream())
|
2022-01-25 19:51:37 +08:00
|
|
|
|
{
|
2022-12-14 20:37:23 +08:00
|
|
|
|
this.Stream.CopyTo(memStream);
|
2022-01-25 19:51:37 +08:00
|
|
|
|
|
2022-12-14 20:37:23 +08:00
|
|
|
|
memStream.Position = 0;
|
2022-01-25 19:51:37 +08:00
|
|
|
|
|
2022-12-14 20:37:23 +08:00
|
|
|
|
using (Bitmap thumbnail = GetThumbnail(memStream, cx))
|
|
|
|
|
{
|
|
|
|
|
if (thumbnail != null && thumbnail.Size.Width > 0 && thumbnail.Size.Height > 0)
|
2022-01-25 19:51:37 +08:00
|
|
|
|
{
|
2022-12-14 20:37:23 +08:00
|
|
|
|
return (Bitmap)thumbnail.Clone();
|
2022-01-25 19:51:37 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-14 20:37:23 +08:00
|
|
|
|
|
|
|
|
|
return null;
|
2022-01-25 19:51:37 +08:00
|
|
|
|
}
|
2022-08-24 04:45:27 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a value indicating what color to use.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static Color DefaultMaterialColor
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var moduleSettings = new SettingsUtils();
|
|
|
|
|
|
|
|
|
|
var colorString = moduleSettings.GetSettings<PowerPreviewSettings>(PowerPreviewSettings.ModuleName).Properties.StlThumbnailColor.Value;
|
|
|
|
|
|
|
|
|
|
return (Color)ColorConverter.ConvertFromString(colorString);
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
// Couldn't read the settings.
|
|
|
|
|
// Assume default color value.
|
|
|
|
|
return Color.FromRgb(255, 201, 36);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-25 19:51:37 +08:00
|
|
|
|
}
|
|
|
|
|
}
|