Skip to content

Commit e13eb2f

Browse files
committed
Initial commit
0 parents  commit e13eb2f

File tree

9 files changed

+289
-0
lines changed

9 files changed

+289
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.vs
2+
/packages
3+
/GcodeThumbnailExtension/obj
4+
/GcodeThumbnailExtension/bin

GcodeThumbnailExtension.sln

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.25420.1
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GcodeThumbnailExtension", "GcodeThumbnailExtension\GcodeThumbnailExtension.csproj", "{0BAFF894-FC5B-43D1-B09B-AB66EF63F8E7}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{0BAFF894-FC5B-43D1-B09B-AB66EF63F8E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{0BAFF894-FC5B-43D1-B09B-AB66EF63F8E7}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{0BAFF894-FC5B-43D1-B09B-AB66EF63F8E7}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{0BAFF894-FC5B-43D1-B09B-AB66EF63F8E7}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
using System;
2+
using System.Drawing;
3+
using SharpShell.SharpThumbnailHandler;
4+
using System.IO;
5+
using System.Drawing.Imaging;
6+
using System.Drawing.Text;
7+
using System.Text;
8+
using System.Runtime.InteropServices;
9+
using SharpShell.Attributes;
10+
11+
namespace GcodeThumbnailExtension
12+
{
13+
[ComVisible(true)]
14+
[COMServerAssociation(AssociationType.FileExtension, ".gcode", ".gco")]
15+
public class CgodeThumbnailHandler : SharpThumbnailHandler
16+
{
17+
protected override Bitmap GetThumbnailImage(uint width)
18+
{
19+
// Create a stream reader for the selected item stream
20+
try
21+
{
22+
using (var reader = new StreamReader(SelectedItemStream))
23+
{
24+
// Now return a preview from the gcode or error when none present
25+
return GetThumbnailForGcode(reader, width);
26+
}
27+
}
28+
catch (Exception exception)
29+
{
30+
// Log the exception and return null for failure
31+
LogError("An exception occurred opening the file.", exception);
32+
return null;
33+
}
34+
}
35+
36+
private Bitmap GetThumbnailForGcode(StreamReader reader, uint width)
37+
{
38+
// Create the bitmap dimensions
39+
var thumbnailSize = new Size((int)width, (int)width);
40+
41+
// Create the bitmap
42+
var bitmap = new Bitmap(thumbnailSize.Width, thumbnailSize.Height,
43+
PixelFormat.Format32bppArgb);
44+
45+
// Get pre-generated thumbnail from gcode file or error if none found
46+
Image gcodeThumbnail = ReadThumbnailFromGcode(reader);
47+
48+
// Create a graphics object to render to the bitmap
49+
using (var graphics = Graphics.FromImage(bitmap))
50+
{
51+
// Set the rendering up for anti-aliasing
52+
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
53+
graphics.DrawImage(gcodeThumbnail, 0, 0, thumbnailSize.Width, thumbnailSize.Height);
54+
}
55+
56+
// Return the bitmap
57+
return bitmap;
58+
}
59+
60+
private Image ReadThumbnailFromGcode(StreamReader reader)
61+
{
62+
int thumbnailSize = 0;
63+
int counter = 0, maxCounter = 1000;
64+
String line = "";
65+
66+
// locate first thumbnail block in the header
67+
while ((counter < maxCounter) && !reader.EndOfStream) {
68+
counter++;
69+
line = reader.ReadLine();
70+
if (line.Contains("thumbnail begin"))
71+
{
72+
break;
73+
}
74+
}
75+
76+
// if no thumbnail was found in first maxCounter lines then throw error
77+
if ((counter == maxCounter) || reader.EndOfStream)
78+
{
79+
throw new Exception();
80+
}
81+
82+
String[] thumbnailMeta = line.Split(' ');
83+
thumbnailSize = Int32.Parse(thumbnailMeta[thumbnailMeta.Length - 1]);
84+
85+
StringBuilder thumbnailData = ReadBase64Data(reader);
86+
byte[] imageBytes = Convert.FromBase64String(thumbnailData.ToString());
87+
88+
// try to find additional thumbnails in the file (PrusaSlicer seems to order them by ascending resolution)
89+
try
90+
{
91+
Image otherImg = ReadThumbnailFromGcode(reader);
92+
return otherImg;
93+
}
94+
catch (Exception exception)
95+
{
96+
// do nothing, we got the previous thumbnail anyway
97+
}
98+
99+
using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
100+
{
101+
Image image = Image.FromStream(ms, true);
102+
return image;
103+
}
104+
}
105+
106+
private StringBuilder ReadBase64Data(StreamReader reader)
107+
{
108+
StringBuilder res = new StringBuilder(20000);
109+
String line = reader.ReadLine();
110+
while ((!line.Contains("thumbnail end")) && !reader.EndOfStream)
111+
{
112+
res.Append(line.Trim("; \n".ToCharArray()));
113+
line = reader.ReadLine();
114+
}
115+
116+
// no thumbnail end found, file is damaged or invalid
117+
if (reader.EndOfStream)
118+
{
119+
throw new Exception();
120+
}
121+
return res;
122+
}
123+
}
124+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{0BAFF894-FC5B-43D1-B09B-AB66EF63F8E7}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>GcodeThumbnailExtension</RootNamespace>
11+
<AssemblyName>GcodeThumbnailExtension</AssemblyName>
12+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\Debug\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<OutputPath>bin\Release\</OutputPath>
28+
<DefineConstants>TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<PropertyGroup>
33+
<SignAssembly>true</SignAssembly>
34+
</PropertyGroup>
35+
<PropertyGroup>
36+
<AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile>
37+
</PropertyGroup>
38+
<ItemGroup>
39+
<Reference Include="Apex.WinForms, Version=1.6.0.0, Culture=neutral, PublicKeyToken=98d06957926c086d, processorArchitecture=MSIL">
40+
<HintPath>..\packages\SharpShellTools.2.2.0.0\lib\Apex.WinForms.dll</HintPath>
41+
<Private>True</Private>
42+
</Reference>
43+
<Reference Include="ServerManager, Version=2.2.0.0, Culture=neutral, processorArchitecture=x86">
44+
<HintPath>..\packages\SharpShellTools.2.2.0.0\lib\ServerManager.exe</HintPath>
45+
<Private>True</Private>
46+
</Reference>
47+
<Reference Include="ServerRegistrationManager, Version=2.7.2.0, Culture=neutral, PublicKeyToken=68bd4561cc3495fc, processorArchitecture=MSIL">
48+
<HintPath>..\packages\ServerRegistrationManager.2.7.2\lib\net45\ServerRegistrationManager.exe</HintPath>
49+
<Private>True</Private>
50+
</Reference>
51+
<Reference Include="SharpShell, Version=2.2.0.0, Culture=neutral, PublicKeyToken=f14dc899472fe6fb, processorArchitecture=MSIL">
52+
<HintPath>..\packages\SharpShellTools.2.2.0.0\lib\SharpShell.dll</HintPath>
53+
<Private>True</Private>
54+
</Reference>
55+
<Reference Include="System" />
56+
<Reference Include="System.Core" />
57+
<Reference Include="System.Drawing" />
58+
<Reference Include="System.Windows.Forms" />
59+
<Reference Include="System.Xml.Linq" />
60+
<Reference Include="System.Data.DataSetExtensions" />
61+
<Reference Include="Microsoft.CSharp" />
62+
<Reference Include="System.Data" />
63+
<Reference Include="System.Net.Http" />
64+
<Reference Include="System.Xml" />
65+
</ItemGroup>
66+
<ItemGroup>
67+
<Compile Include="CgodeThumbnailHandler.cs" />
68+
<Compile Include="Properties\AssemblyInfo.cs" />
69+
</ItemGroup>
70+
<ItemGroup>
71+
<None Include="app.config">
72+
<SubType>Designer</SubType>
73+
</None>
74+
<None Include="key.snk" />
75+
<None Include="packages.config" />
76+
</ItemGroup>
77+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
78+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
79+
Other similar extension points exist, see Microsoft.Common.targets.
80+
<Target Name="BeforeBuild">
81+
</Target>
82+
<Target Name="AfterBuild">
83+
</Target>
84+
-->
85+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("GcodeThumbnailExtension")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("GcodeThumbnailExtension")]
13+
[assembly: AssemblyCopyright("Copyright © 2020")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("0baff894-fc5b-43d1-b09b-ab66ef63f8e7")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

GcodeThumbnailExtension/app.config

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<runtime>
4+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
5+
<dependentAssembly>
6+
<assemblyIdentity name="SharpShell" publicKeyToken="f14dc899472fe6fb" culture="neutral" />
7+
<bindingRedirect oldVersion="0.0.0.0-2.2.0.0" newVersion="2.2.0.0" />
8+
</dependentAssembly>
9+
</assemblyBinding>
10+
</runtime>
11+
</configuration>

GcodeThumbnailExtension/key.snk

596 Bytes
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="ServerRegistrationManager" version="2.7.2" targetFramework="net452" />
4+
<package id="SharpShell" version="2.7.2" targetFramework="net452" />
5+
<package id="SharpShellTools" version="2.2.0.0" targetFramework="net452" />
6+
</packages>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ServerRegistrationManager.exe install GcodeThumbnailExtension.dll -codebase

0 commit comments

Comments
 (0)