| | | 1 | | using System; |
| | | 2 | | using System.IO; |
| | | 3 | | using System.Runtime.InteropServices; |
| | | 4 | | |
| | | 5 | | namespace SolidEdgeCommunity.Extensions; |
| | | 6 | | |
| | | 7 | | public static partial class SheetExtensions |
| | | 8 | | { |
| | | 9 | | [LibraryImport("user32.dll")] |
| | | 10 | | [return: MarshalAs(UnmanagedType.Bool)] |
| | | 11 | | private static partial bool CloseClipboard(); |
| | | 12 | | |
| | | 13 | | [LibraryImport("user32.dll")] |
| | | 14 | | [UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvStdcall)])] |
| | | 15 | | private static partial IntPtr GetClipboardData(uint format); |
| | | 16 | | |
| | | 17 | | [LibraryImport("user32.dll")] |
| | | 18 | | private static partial IntPtr GetClipboardOwner(); |
| | | 19 | | |
| | | 20 | | [LibraryImport("user32.dll")] |
| | | 21 | | [return: MarshalAs(UnmanagedType.Bool)] |
| | | 22 | | private static partial bool IsClipboardFormatAvailable(uint format); |
| | | 23 | | |
| | | 24 | | [LibraryImport("user32.dll")] |
| | | 25 | | [return: MarshalAs(UnmanagedType.Bool)] |
| | | 26 | | private static partial bool OpenClipboard(IntPtr hWndNewOwner); |
| | | 27 | | |
| | | 28 | | [LibraryImport("gdi32.dll")] |
| | | 29 | | [return: MarshalAs(UnmanagedType.Bool)] |
| | | 30 | | private static partial bool DeleteEnhMetaFile(IntPtr hemf); |
| | | 31 | | |
| | | 32 | | [LibraryImport("gdi32.dll")] |
| | | 33 | | private static partial uint GetEnhMetaFileBits(IntPtr hemf, uint cbBuffer, [Out] byte[] lpbBuffer); |
| | | 34 | | |
| | | 35 | | private const uint _cf_ENHMETAFILE = 14; |
| | | 36 | | |
| | | 37 | | public static System.Drawing.Imaging.Metafile GetEnhancedMetafile(this SolidEdgeDraft.Sheet sheet) |
| | | 38 | | { |
| | | 39 | | try |
| | | 40 | | { |
| | | 41 | | // Copy the sheet as an EMF to the windows clipboard. |
| | 1 | 42 | | sheet.CopyEMFToClipboard(); |
| | | 43 | | |
| | 1 | 44 | | if (OpenClipboard(IntPtr.Zero)) |
| | | 45 | | { |
| | 1 | 46 | | if (IsClipboardFormatAvailable(_cf_ENHMETAFILE)) |
| | | 47 | | { |
| | | 48 | | // Get the handle to the EMF. |
| | 0 | 49 | | IntPtr henhmetafile = GetClipboardData(_cf_ENHMETAFILE); |
| | | 50 | | |
| | 0 | 51 | | return new System.Drawing.Imaging.Metafile(henhmetafile, true); |
| | | 52 | | } |
| | | 53 | | else |
| | | 54 | | { |
| | 1 | 55 | | throw new Exception("CF_ENHMETAFILE is not available in clipboard."); |
| | | 56 | | } |
| | | 57 | | } |
| | | 58 | | else |
| | | 59 | | { |
| | 0 | 60 | | throw new Exception("Error opening clipboard."); |
| | | 61 | | } |
| | | 62 | | } |
| | 1 | 63 | | catch (Exception) |
| | | 64 | | { |
| | 1 | 65 | | throw; |
| | | 66 | | } |
| | | 67 | | finally |
| | | 68 | | { |
| | 1 | 69 | | CloseClipboard(); |
| | 1 | 70 | | } |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | public static void SaveAsEnhancedMetafile(this SolidEdgeDraft.Sheet sheet, string filename) |
| | | 74 | | { |
| | | 75 | | try |
| | | 76 | | { |
| | | 77 | | // Copy the sheet as an EMF to the windows clipboard. |
| | 1 | 78 | | sheet.CopyEMFToClipboard(); |
| | | 79 | | |
| | 1 | 80 | | if (OpenClipboard(IntPtr.Zero)) |
| | | 81 | | { |
| | 1 | 82 | | if (IsClipboardFormatAvailable(_cf_ENHMETAFILE)) |
| | | 83 | | { |
| | | 84 | | // Get the handle to the EMF. |
| | 0 | 85 | | IntPtr hEMF = GetClipboardData(_cf_ENHMETAFILE); |
| | | 86 | | |
| | | 87 | | // Query the size of the EMF. |
| | 0 | 88 | | uint len = GetEnhMetaFileBits(hEMF, 0, null); |
| | 0 | 89 | | byte[] rawBytes = new byte[len]; |
| | | 90 | | |
| | | 91 | | // Get all of the bytes of the EMF. |
| | 0 | 92 | | GetEnhMetaFileBits(hEMF, len, rawBytes); |
| | | 93 | | |
| | | 94 | | // Write all of the bytes to a file. |
| | 0 | 95 | | File.WriteAllBytes(filename, rawBytes); |
| | | 96 | | |
| | | 97 | | // Delete the EMF handle. |
| | 0 | 98 | | DeleteEnhMetaFile(hEMF); |
| | | 99 | | } |
| | | 100 | | else |
| | | 101 | | { |
| | 1 | 102 | | throw new Exception("CF_ENHMETAFILE is not available in clipboard."); |
| | | 103 | | } |
| | | 104 | | } |
| | | 105 | | else |
| | | 106 | | { |
| | 0 | 107 | | throw new Exception("Error opening clipboard."); |
| | | 108 | | } |
| | 0 | 109 | | } |
| | 1 | 110 | | catch (Exception) |
| | | 111 | | { |
| | 1 | 112 | | throw; |
| | | 113 | | } |
| | | 114 | | finally |
| | | 115 | | { |
| | 1 | 116 | | CloseClipboard(); |
| | 1 | 117 | | } |
| | 0 | 118 | | } |
| | | 119 | | } |