Batch Save As PDF

I have a folder full of .asm, .par, & .psm files. I need to export all these Solid Edge files to .pdf so that I can use them in a web application.

I need a Visual Basic .NET or C# script that will:1) Open each file one at a time2) Change the view orientation to ISO view (optional)3) Change the view style to Visible Edges (or possibly another view)4) Save this file as PDF using the same filename (example partname.par ->partname.pdf)

5) Close file and then open the next.

I am able to do this manually in Solid Edge, by going to Save As-> Save As Translated -> .pdf, so I'm assuming there is a way to do this using a script. (?)

I am a PHP web developer who is new to both Visual Basic .NET and Solid Edge, and I've attempted to make my way through this forum and the "Solid Edge .NET Programmer’s Guide ". But I'm striking out and am possibly approaching this all wrong.

So if anyone can help point me in the right direction (on how to do any of the steps above), that would be very much appreciated.





Posted by: Denver Steiner
Post date: 11/1/2010 8:55:38 AM


RE: Batch Save As PDF

Update on previous post, I came across SESpy, and this is helping.Imports System.Runtime.InteropServicesImports SolidEdgeFramework Module Module1 Sub Main() Dim objApplication As SolidEdgeFramework.Application = Nothing Dim objDocument As SolidEdgeFramework.SolidEdgeDocument = Nothing Try ' Connect to a running instance of Solid Edge. objApplication = Marshal.GetActiveObject("SolidEdge.Application") objDocument = objApplication.Documents.Open("C:\projects\SE_test\test.psm") objDocument.SaveCopyAs("C:\projects\SE_test\test.pdf") objDocument.Close Catch ex As Exception Console.WriteLine(ex.Message) Finally If Not (objDocument Is Nothing) Then Marshal.ReleaseComObject(objDocument) objDocument = Nothing End If If Not (objApplication Is Nothing) Then Marshal.ReleaseComObject(objApplication) objApplication = Nothing End If End Try End SubEnd Module

Using SaveCopyAs produced this error: C:\projects\SE_test\test.psm could not be saved because of a file translation error.

What am I missing?

I still need to figure out how to switch views orientation and views, but I need to get past the "Save to PDF" step.


Posted by: Denver Steiner
Post date: 11/4/2010 10:28:21 AM

RE: Batch Save As PDF

I came across these two posts:


And I realized I don't have "Solid Edge Velocity PS Printer 2.0" installed on the computer I'm testing this on, so that may be my SaveAs problems. How do I install this?


In the mean time, if anybody can help me with my view orientation and view style changes, that would be appreciated.




Posted by: Denver Steiner
Post date: 11/5/2010 7:46:14 AM

RE: Batch Save As PDF

I'm slowly but surely figuring this out (and answering my own post).


FYI, if you don't have "Solid Edge Velocity PS Printer 2.0" installed, you need to install Solid Edge without UAC enabled.


This will switch my view style (written in C#):

_objWin = (SolidEdgeFramework.Window) _solidEdge.ActiveWindow;


_objWin.View.RenderModeType = SeRenderModeType.seRenderModeVHL;


When I have complete solution available I'll post it. (Still haven't seen how to change view orientation, but I'm sure that's buried somewhere).


Posted by: Denver Steiner
Post date: 11/5/2010 9:32:49 AM

RE: Batch Save As PDF

Hey man,


I did something similar a while back, without changing the orientation though. I actually used SaveAsImage and then used iTextSharp to create a pdf. This allowed me to add overlays, text etc to the pdf.


The first thing I did was to create a new invisible window so that I could apply the view without changing the users current window. I then looked for a view style in the file, and if I didn't find it then I copied it from a template file. I did this all a while ago, but if you've got any questions I should be abled to figure out why I did things the way I did.


Here's the code I used to save the image:

/* Declare a variable for the view styles so we don't have to do a switch on the * document type again when applying the styles */ViewStyles viewStyles = null;/* Make sure this is the correct document type */switch (document.Type){ case DocumentTypeConstants.igAssemblyDocument: case DocumentTypeConstants.igWeldmentAssemblyDocument: viewStyles = ((AssemblyDocument)document).ViewStyles; break; case DocumentTypeConstants.igPartDocument: viewStyles = ((PartDocument)document).ViewStyles; break; case DocumentTypeConstants.igSheetMetalDocument: viewStyles = ((SheetMetalDocument)document).ViewStyles; break; case DocumentTypeConstants.igWeldmentDocument: viewStyles = ((WeldmentDocument)document).ViewStyles; break; case DocumentTypeConstants.igUnknownDocument: case DocumentTypeConstants.igDraftDocument: default: throw new InvalidOperationException(string.Format("Invalid document type {0} passed to SaveImage", document.Type));}Application solidEdge = document.Application;try{ View view = null; try { /* Get the View object for the document */ if (document.Windows.Count > 1) { bool multipleVisibleWindows = false; foreach (Window window in (IEnumerable)document.Windows) { try { if (window == solidEdge.ActiveWindow) { if (view != null) Marshal.ReleaseComObject(view); view = window.View; multipleVisibleWindows = false; break; } else if (window.Visible) { if (view != null) { multipleVisibleWindows = true; } else { view = window.View; } } } finally { Marshal.ReleaseComObject(window); } } if (multipleVisibleWindows && (view != null)) { Marshal.ReleaseComObject(view); view = null; } } else if (document.Windows.Count == 1) { view = document.Windows.Item(1).View; } if (view == null) { throw new InvalidOperationException(Resources.CouldNotGetWindowForImage); } /* If the part is in-place activated then we need to activate the * hidden parent window and call this method recursively */ Window tempWindow = null; if (document.InPlaceActivated) { /* Get the parent document */ SolidEdgeDocument parent = document; while (parent.InPlaceActivated) { object temp = null; Array matrix = new double[] { }; switch (parent.Type) { case DocumentTypeConstants.igAssemblyDocument: case DocumentTypeConstants.igWeldmentAssemblyDocument: ((AssemblyDocument)parent).GetContainerDocumentAndMatrixOfIPADoc(out temp, ref matrix); break; case DocumentTypeConstants.igPartDocument: ((PartDocument)parent).GetContainerDocumentAndMatrixOfIPADoc(out temp, ref matrix); break; case DocumentTypeConstants.igSheetMetalDocument: ((SheetMetalDocument)parent).GetContainerDocumentAndMatrixOfIPADoc(out temp, ref matrix); break; case DocumentTypeConstants.igWeldmentDocument: ((WeldmentDocument)parent).GetContainerDocumentAndMatrixOfIPADoc(out temp, ref matrix); break; case DocumentTypeConstants.igUnknownDocument: case DocumentTypeConstants.igDraftDocument: default: break; } if (temp != null) { parent = (SolidEdgeDocument)temp; } else { /* If we get here then we couldn't generate a pdf */ throw new ApplicationException(Resources.CouldNotGenerateImage); } } /* The part window caption should always be the same as the * assembly name - this is a bit dodgy but I couldn't come * up with anything better. Just in case we perform a check * against the parent document. */ string windowCaption = view.Window.Caption; for (int i = 1; i


Posted by: Calum McLellan
Post date: 11/6/2010 6:47:29 AM

RE: Batch Save As PDF

Calum, Thanks for the code. I ended up going with something a bit simpler. For others looking for a solution, this is the code that seems to work:string sFile = @"08.2150";string sDestinationFile = sFile + ".psm";string sPDFfile = sFile + ".pdf";string sDir=@"W:\Solid_Edge\Released\"; string sDestination=@"W:\Solid_Edge\Released\PDF\";SolidEdgeFramework.Application _solidEdge = null;SolidEdgeFramework.SolidEdgeDocument _objDoc = null;SolidEdgeFramework.Window _objWin = null;Type type = null;type = Type.GetTypeFromProgID("SolidEdge.Application");_solidEdge = (SolidEdgeFramework.Application) Activator.CreateInstance(type);_solidEdge.Visible = true;_solidEdge.DisplayFullScreen = true; Object oDocRelationAutoServer = "";Object oAltPath = "";bool bRecognizeFeaturesIfPartTemplate = true;RevisionManager.RevisionRuleType rroRevisionRuleType = RevisionManager.RevisionRuleType.LastSavedType;bool bStopFileOpenIfRevisionRuleNotApplicable = false;_objDoc = (SolidEdgeFramework.SolidEdgeDocument)_solidEdge.Documents.Open( sDir+sDestinationFile , oDocRelationAutoServer, oAltPath , bRecognizeFeaturesIfPartTemplate , rroRevisionRuleType , bStopFileOpenIfRevisionRuleNotApplicable);_objWin = (SolidEdgeFramework.Window) _solidEdge.ActiveWindow;_objWin.Activate();//fit drawing to the window (otherwise it may cut part of it off when exported to PDF)_objWin.View.Fit();//change the view style to Visible Edges_objWin.View.RenderModeType = SeRenderModeType.seRenderModeVHL; _objDoc.SaveCopyAs(sDestination+sPDFfile);_objDoc.Close(false, null, null);

Also attached (if I did this correctly) is the source code for the Console application that I built that loops through a directory of files. I'm sure there is better ways to do things, but this was at least an amateur stab at it. It works pretty well for what I need it to do.

A bug I noticed, it occasionally will export a blank PDF, but work the next run through.

My other wish is that I wish the PDF would stretch the width of the document. It seems to add a white box behind the image, and this box tricks programs that look at the image into thinking the image is wider than it really is resulting in a lot of wasted white space when I wanted to blow the image up larger. I'm not sure if there is a setting I can change or if is just the way Solid Edge is.


Posted by: Denver Steiner
Post date: 11/9/2010 10:12:25 AM

RE: Batch Save As PDF

There is one piece I'm still missing if anybody could give me pointers, I would be appreciative.


In Solid Edge when I go to "print", I have the option of change my Print Range to: "Print area", then I can manually draw a box around what I want printed. This allows me to crop out excess white space above and below an object. Otherwise it will export the Current display which depends on my window size. 


I'm using _objWin.View.Fit(); to fiit the drawing to my view before exporting, but I need the Print Area to FIT to only the visible model (excluding any hidden reference planes).


The problem I'm having with both exporting to PDF and Images is that there is a lot of excess "white space" surrounding my model, and I need my PDF and image to crop to just the model. It seems to be using the "Current Display" and not give me any other options. This especially sucks if have a model that is taller than it is wide (ends up with alot of horizontal white space).


How can I approach a fix this to eliminating excess white space? Is there away of specifying "print area" using scripting?


With images, can I detect width and height of my view of a 3D object? And then save images with pixels that fit that? Even if I did that, how do I get rid of excess white space?


Or is there a simple command that I'm missing that does this?



Posted by: Denver Steiner
Post date: 11/11/2010 3:04:43 AM

RE: Batch Save As PDF

FYI: My workaround for the problem of whitespace was to load my Black & White PDF drawings in Adobe Illustrator, and perform a Illustrator script that does this:

1) Increase line width across the board

2) Detect edges of visible lines

3) Crop art box to that area detected above

4) Export file as a .png, .jpg, .swf, .svg, or anything I want


I also export a color pdf drawing at the same time. This however is exported as an image, so detecting visible lines isn't possible. But because I've just detected the artbox of the B&W drawing (and the color version is the exact same proportion), I crop to the exact same artbox, and export a .jpg.


Unfortunately adding the Illustrator steps makes this a two step process, but its actually worked out pretty well.


In just a few days, I've plugged through over 3500 solid images, and have bw and color images off all them cropped to just the image. Our CAD guys are extremely happy they didn't have to do all this by hand.


Few other notes:

(1) If you minimize the SE window when you are running the script, it will generate a BLANK PDF.

(2) I use Illustrator to detect if an image is blank and flag that I need to regenerate the image.






Posted by: Denver Steiner
Post date: 12/9/2010 5:02:50 AM