Selecting objects does not work when application.visible set to false

Pioneer
Pioneer

I find that selecting objects in SE only seems to work when application.visible is set to true. Is this a known problem? Or am I missing something? I would really like to let this code run as a windows service, so I would like to keep SE invisible.

 

In JasonNewells Samples I saw he explicitly sets the application visibility to true, can SE automation run invisible?

 

A related problem is that when running my service, the application visibility seems to be stuck on false. So debugging is hard as I cant follow the changes within SE. What do I need to do to influence the application visibilty when running as a service? I get the feeling I am missing something obviousSmiley Very Happy

18 REPLIES

Re: Selecting objects does not work when application.visible set to false

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom

When you say "selecting objects in SE", what exactly do you mean? i.e What are you trying to accomplish?

 

Regarding trying to automate Solid Edge from a Windows Service, there is a lot to discuss.

 

In chapter 1 of sesetup.pdf, Siemens explicitly states "It is not recommended that you run Solid Edge on server operating systems." There are techincal reasons that they do not get into but you can deduce that many of the reasons are similar to Considerations for server-side Automation of Office. Beyond that, that statement in the install guide gives them an out if you try and ask for support specific to automating Solid Edge in a server environment which is typically why you would want to write a service.

 

Next, Windows services cannot have UI's directly as they are not allowed to interact with the desktop. Also, the operating system isolates services in Session 0 and runs applications in other sessions, so serv... I have read about ways around this but the point is to understand that windows services are handled differently by the OS so you can't expect them to work the same as a typical application. 

 

Regarding trying to test code in a windows service, I would write my automation logic in a separate class that the service can call. Since a windows service has a static void Main() method, you can control whether or not the service actually attempts to start. If you want to test the automation logic, call the automation class directly and don't start the service. Below is a small snippet of what I'm talking about. It's the call to ServiceBase.Run that causes Visual Studio to stop and tell you that it can't debug a service.

 

        static void Main()
        {
#if DEBUG
            // Test automation class.
#else
            ServiceBase[] ServicesToRun;
            
            ServicesToRun = new ServiceBase[] 
            { 
                new Service1() 
            };

            ServiceBase.Run(ServicesToRun);
#endif

All of that said, it is certainly (probably) possible to do what you want but it's not going to be easy. EDA spoke at Solid Edge University 2012 and demonstrated one of their products that is automating Solid Edge server side. I should also note that I'm pretty sure that I've heard SE developers mention that certain APIs may not work correctly from a service. I would imagine the reasons are similar to the Considerations for server-side Automation of Office.

 

Jason Newell
Applications Architect
www.jasonnewell.net

Re: Selecting objects does not work when application.visible set to false

Pioneer
Pioneer

Sorry I was unclear.

 

I have added or discovered an occurence in my drawing, this is an assembly, and I want to add parts to it, and put those in a pattern.

 

To do this I select the ooccurence , and open then for edit.:

 

occ.Select(true);

_app.StartCommand((SolidEdgeFramework.SolidEdgeCommandConstants)AssemblyCommandConstants.AssemblyEditActivateembeddedorlinkedobject);

 

(occ has the datatype SolidEdgeAssembly.Occurrence)

 

When the application is visible, this works fine. When the application is invisible the occurence/assembly opens in a new window, not in edit mode.

 

I am using the windows service in the background on a desktop. Engineers that do need to handle the resulting drawings have the same functionality in a tray application

 

Thanks for the quick reply.

Re: Selecting objects does not work when application.visible set to false

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom

StartCommand() was the main API I was thinking wouldn't work. As customers, other than what we can test, we're guessing what will work and won't work. You really need someone from development to explain WHY certain APIs don't work when Solid Edge is invisible and\or is being automated from a service. Assuming that I'm correct and StartCommand() won't work in this scenario, you're left with trying to find an alternative method of accomplishing the same task as the command you were trying to use.

 

I wish that I could give you better guidance but in the end, I'm just a customer like everyone else.

Jason Newell
Applications Architect
www.jasonnewell.net

Re: Selecting objects does not work when application.visible set to false

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom

Normally, you don't in-place-activate (IPA) the occurrence document. The occurrence object has an OccurrenceDocument property from which you can access the document and do your edits there.

If you are going to create an interpart-copy, you should use the Add2 method, which was exactly designed to support this without the need of IPA'ing an occurrence.

If you want to delete a suboccurrence, then I propose to transfer the suboccurrence first to the toplevel document and then to delete it there. This allows you perform this task without IPA.

Re: Selecting objects does not work when application.visible set to false

Pioneer
Pioneer

Do you have more information on the add2 method, the SDK does not seem want to reveal its secrets to me.

 

What I need to do is, add parts (asm, psm and par) as occurences, change location and Variables.

 

thanks

Re: Selecting objects does not work when application.visible set to false

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom

If you only need to assemble parts you won't need to Add2 method. This is only for interpart copies.

The following code shows you how to create a simple assembly (4 wheels of a car ;-) without inplace-activation, as you have access to all documents at any time.

'----------------------------------------------------------------------
' ASM: Create a 2-level simplified car assembly without IPA
' NOTE: Make sure that the folder "C:\TEMP\Car" exists and is empty 
'       on your computer!
'----------------------------------------------------------------------
Dim strAsmTemplate As String = objApp.GetDefaultTemplatePath(DocumentTypeConstants.igAssemblyDocument)
Dim strParTemplate As String = objApp.GetDefaultTemplatePath(DocumentTypeConstants.igPartDocument)
' Hide Solid Edge
objApp.Visible = False
objApp.Interactive = False
' Create the CAR assembly
Dim objCarDoc As AssemblyDocument = objApp.Documents.Add("SolidEdge.AssemblyDocument")
Dim objFrontAxleOcc As Occurrence = objCarDoc.Occurrences.AddByTemplate("C:\TEMP\Car\Axle.asm", strAsmTemplate)
objFrontAxleOcc.Move(1.5, 0, 0)
Dim objBackAxleOcc As Occurrence = objCarDoc.Occurrences.AddWithTransform("C:\TEMP\Car\Axle.asm", -1.5, 0, 0, 0, 0, 0)
' Create the AXLE assembly
Dim objAxleDoc As AssemblyDocument = objFrontAxleOcc.OccurrenceDocument
Dim objLeftWheelOcc As Occurrence = objAxleDoc.Occurrences.AddByTemplate("C:\TEMP\Car\Wheel.par", strParTemplate)
objLeftWheelOcc.Move(0, 1, 0)
Dim objRightWheelOcc As Occurrence = objAxleDoc.Occurrences.AddWithTransform("C:\TEMP\Car\Wheel.par", 0, -1, 0, 0, 0, 0)
' Create the WHEEL part (as a simple cylinder)
Dim objWheelDoc As PartDocument = objLeftWheelOcc.OccurrenceDocument
objWheelDoc.ModelingMode = ModelingModeConstants.seModelingModeOrdered
Dim objProfileSet As ProfileSet = objWheelDoc.ProfileSets.Add()
Dim objProfile As Profile = objProfileSet.Profiles.Add(objWheelDoc.RefPlanes.Item(3))
objProfile.Circles2d.AddByCenterRadius(0, 0.25, 0.25)
objProfile.End(ProfileValidationType.igProfileClosed)
objProfile.Visible = False
Dim objProfiles() As Profile = {objProfile}
objWheelDoc.Models.AddFiniteExtrudedProtrusion(1, objProfiles, igBoth, 0.2)
' Show Solid Edge again
objApp.Interactive = True
objApp.Visible = True
' Fit the view
objApp.StartCommand(SolidEdgeConstants.AssemblyCommandConstants.AssemblyViewFit)

 

Re: Selecting objects does not work when application.visible set to false

Pioneer
Pioneer

thank you very much. Ill do some testing when I get the time Smiley Happy

Re: Selecting objects does not work when application.visible set to false

Pioneer
Pioneer

It works ! ..... almost.

 

If I generate the assembly, with the AddByTemplate method, it works fine. But I dont have the sketches in my assembly that I use to create a pattern later on in my code. If I use AddByFilename and prefab assemblies (with the needed sketches) I do need to use IPA to add parts to my assembly with out errors.

 

 

 

 

Re: Selecting objects does not work when application.visible set to false

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom

If you use the AddByFilename() method to add your prepared assembly with the pattern sketches, then you will need to make the occurrence writeable BEFORE accessing it and continue to assemble your subparts.

'...Add the assembly with the pattern sketch
Dim objPatternOcc As Occurrence = objMainDoc.Occurrences.AddByFilename("C:\TEMP\Car\WheelPatternAsm.asm")
objPatternOcc.MakeWritable()
Dim objPatternDoc As AssemblyDocument = objPatternOcc.OccurrenceDocument
'...continue to create you assembly/parts