Products

How to open documents silently

By jnewell

Over in the Solid Edge Developer Forum, there has been an ongoing discussion titled Memory and handle leak in SolidEdge when opening and closing parts?. As always, it has been an interesting dicussion and there has been a lot of good knowledge sharing. Of particular interest though, on page 3 of the discussion, community member RDH shared the following tidbit of code with a very interesting trick.

case 1:
{
// This command demonstrates a little know method of opening a Part document without
// creating a window. It relies on setting the option input arg to an unsigned int. The
// value of the int is eight.
#define JDOCUMENTPROP_NOWINDOW 0x00000008
DocumentsPtr pDocs = GetApplicationPtr()->Documents;
_bstr_t FullName;

if( NULL != pDocs )
{
PartDocumentPtr pPartDoc;
VARIANT vOption;
V_VT( &vOption ) = VT_UI4;
V_UI4( &vOption ) = (unsigned long )JDOCUMENTPROP_NOWINDOW;
pPartDoc = pDocs->Open( “c:\temp\block.par”, vOption );

if( NULL != pPartDoc )
{
FullName = pPartDoc->FullName;
pPartDoc->Close();
}
}
break;
}


In his example C++ code, he is able to “silently” open a Solid Edge document. By silent, I mean that he is opening the document without any windows being created in the GUI. If you study the code closely, you’ll notice that he is passing a value of “8” to the 2nd parameter of the Documents.Open() method. If we examine the definition of that method,

[id(0x00000006), helpstring(“Opens a specified document.”), helpcontext(0x0000c39e)]
IDispatch* Open(
[in] BSTR Filename,
[in, optional] VARIANT DocRelationAutoServer,
[in, optional] VARIANT AltPath,
[in, optional] VARIANT RecognizeFeaturesIfPartTemplate,
[in, optional] VARIANT RevisionRuleOption,
[in, optional] VARIANT StopFileOpenIfRevisionRuleNotApplicable);

we will notice that the 2nd parameter is named “DocRelationAutoServer”. If you’re scratching your head right now, I am too. To say that this falls into the “Not obvious at all” category would be an understatement. Regardless, the trick exists and does work. I’ve tested it will all Solid Edge document types with success. In my test, I converted the code to C# as shown below. Note that the C++ ULONGLONG translates to C# uintint. Best that I can tell, the API is mostly only checking the value. I’ve tested with C# ulong,long,uint & int and all worked.

using SolidEdgeCommunity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SENoWindowTest
{
class Program
{
static void Main(string[] args)
{
//var filename = @”C:Program FilesSolid Edge ST8TrainingCoffee Pot.asm”;
//var filename = @”C:Program FilesSolid Edge ST8TrainingTest Drive_Part_QS1.dft”;
//var filename = @”C:Program FilesSolid Edge ST8Trainingbar.par”;
var filename = @”C:Program FilesSolid Edge ST8Trainingsimulationbrace.psm”;

var application = SolidEdgeUtils.Connect();
var documents = application.Documents;

// uint and int both work.
//uint JDOCUMENTPROP_NOWINDOW = 0x00000008;
int JDOCUMENTPROP_NOWINDOW = 0x00000008;

var document = (SolidEdgeFramework.SolidEdgeDocument)documents.Open(filename, JDOCUMENTPROP_NOWINDOW);
document.Close(false);
}
}
}


Taking a step back and looking at this objectively, my opinion is that while I’m grateful that this option exists, it’s usability rating is pretty low. If you didn’t read the original post or see this blog post, it’s likely you would have never known about this ability of the API. In my opinion, the API should be enhanced with something like Documents.OpenInBackground(). That is much more obvious and usable.

This post wouldn’t be complete if I didn’t mention that as a responsible community member, I immediately filed an Incident Report (IR) with GTAC. IR 7520370 was created and is titled “Add ability to open documents silently via API”. My hopes are that Siemens will consider enhancing the API to formally support this option. If you agree, you can call GTAC referencing IR 7520370 and request that your name be added. My understanding is that the more people that call in and add their name, the greater the weight of the IR during the prioritization process.

Jason Newell

Jason is a Solutions Architect for Charles Machine Works in Perry, Oklahoma. As a 21 year IT veteran, Jason is well versed in a broad range of software technologies and programming languages. In 2005, Jason started JasonNewell.NET, a website dedicated to expanding the Solid Edge developer community and offering consulting services. Through his website, he was able to acquire Siemens PLM Solution Partner status and soon began consulting for Siemens PLM directly. Through his consulting efforts with Siemens PLM, Jason takes a leading role in expanding the developer community by contributing to the Solid Edge SDK, authoring open source projects and speaking at conferences.

More from this author

Leave a Reply

This article first appeared on the Siemens Digital Industries Software blog at https://blogs.sw.siemens.com/solidedge/how-to-open-documents-silently/