How to export draft images

Have you ever had a need to export vector graphics or raster graphics from a draft file? Over my 15 years of working with Solid Edge, I have found myself in that position many, many times. In this post, I’ll cover two possible approaches to accomplishing this task.

Background

When a Solid Edge draft file is saved, an Enhanced Metafile (EMF) is embedded into the file for each working sheet. The reason Solid Edge does this is so that their lightweight viewer can easily and quickly render a graphical view of the draft. The EMF format is ideal in this scenario as it is lightweight, vector based and scales perfectly when resized rather than distorting like a raster image would.

revision-manager.png

If we can aquire these vector images ourselves, a lot of doors open to what we can accomplish. The question is, how do we access these embedded EMF images for our own purposes.

For technical details about the EMF format, see the Enhanced Metafile Format Specification.

Option 1 – Solid Edge API + a touch of open source

If we examine the Solid Edge API, in this case draft.tlb, we will find a CopyEMFToClipboard() method available for a Sheet object.

// Generated .IDL file (by the OLE/COM Object Viewer)
//
// typelib filename: draft.tlb
[
uuid(3E2B3BDC-F0B9-11D1-BDFD-080036B4D502),
version(1.0),
helpstring(“Solid Edge Draft Type Library”),
]
library SolidEdgeDraft
{
[
uuid(FFB20FA0-339B-11CE-956A-08003601DFE5),
helpstring(“Represents a sheet in a document.”),
helpcontext(0x00007537)
]
dispinterface Sheet {
properties:
methods:
[id(0x000000de), helpstring(“Copies an enhanced metafile to the operating system clipboard.”), helpcontext(0x0000756c)]
void CopyEMFToClipboard();
};
};

The CopyEMFToClipboard() method creates an in-memory EMF and places it on the clipboard in CF_ENHMETAFILE format. In order to access the EMF data, we use the GetClipboardData() function which returns a pointer to a HMETAFILE. For most (non C++) programmers, everything comes to a screeching halt right there. Unless you have ever dabbled with C++, you likely have no idea how to proceed.

Fortunately, it’s 2015 and people actually share code. If you don’t want to dig into the gory details of how to access the EMF on the clipboard, you can easily install the SolidEdge.Community NuGet package and leverage pre-built extension methods to gain access to the actual EMF. If you’re not familiar with NuGet, no worries, I’ve got you covered in my How to use NuGet for Solid Edge custom development post. If you’re not comfortable using an open source library, no problem! You can view the full source code for SolidEdge.Community on GitHub. Specifically for this article, you’ll want to reference SheetExtensions.cs and look for the SaveAsEnhancedMetafile() extension method. Extension methods like this enhance the base Solid Edge API by overlaying additional methods to classes like SolidEdgeDraft.Sheet.

For an example of how to use the SaveAsEnhancedMetafile() extension method, head over to the Solid Edge Community on GitHub. There is a C# sample and Visual Basic sample available. The following is a short snippet demonstrating the usage.

foreach (SolidEdgeDraft.Sheet sheet in workingSection.Sheets)
{
// Note: SaveAsEnhancedMetafile() is an extension method from SolidEdge.Community.dll.
sheet.SaveAsEnhancedMetafile(emfFileName);
}

I should also mention that the GetEnhancedMetafile() extension method is also available. This will return a .NET friendly System.Drawing.Imaging.Metafile.

Note that in order to enabled these extension methods, you must use the C# using keyword or the Visual Basic Imports keyword.

using SolidEdgeCommunity.Extensions;

 or

Imports SolidEdgeCommunity.Extensions

Option 2 – Open source only

As an Applications Architect, I often find myself with project requirements that don’t always agree with existing software APIs. For example, I’ve needed to process and export 11,000+ draft files as fast as possible. I’ve also needed to perform this export process in an automated server environment. Automating Solid Edge as shown in Option 1 was not really a (good) option as Solid Edge is a UI application.

Considering these requirements, the SolidEdge.Community.Reader NuGet package was born. Full source code can be found on the SolidEdge.Community.Reader GitHub page. Installing this NuGet package into your project adds a reference to the SolidEdge.Community.Reader.dll assembly. The assembly has the ability to read Solid Edge files natively and perform the same export operation without the need to have Solid Edge running.

For an example of how to use the SolidEdge.Community.Reader NuGet package, head over to the Solid Edge Community on GitHub. There is a C# sample and Visual Basic sample available. The following is a short snippet demonstrating the usage.

// Open the file.
using (var draftDocument = DraftDocument.Open(options.FileName))
{
// Process each sheet.
foreach (var sheet in draftDocument.Sheets)
{
// Save EMF.
sheet.SaveAsEmf(emfFileName);
}
}

In my particular project, using the SolidEdge.Community.Reader NuGet package yielded the following results.

  • DFT count: 11,450 (65 files processed sec)
  • EMF count: 24,202 (137 files exported sec)
  • Duration: 2 minutes 57 seconds

65 DFT files per second! 137 EMF files per second! Now that’s what I’m talking about. Honestly, I was a bit mind blown when I saw the results. It goes without saying that a lot of hard work has gone into the SolidEdge.Community.Reader NuGet package. It is an extremely efficient and fast bit of code. 

Conclusion

In this post, I demonstrated 2 methods of how to access and export an EMF for each working sheet in a draft. I focused on the EMF format but the C# sample and Visual Basic sample also show you how to convert the EMF to other image types.

Reasons for the ability to do this include but not limited to:

  • Displaying draft files outside of Solid Edge
  • Converting EMFs to other formats for other purposes.
  • Batch printing. *You can render EMFs directly to printers 😉

More than anything, I want you all to be aware of the usefullness and power of open source. In my opinion, if you’re not leveraging andor contributing to what open source has to offer, you’re doing it wrong.

valve_house.jpg

Topics

Leave a Reply