Top Banner
#SEU12 Basics of Automating Solid Edge with .NET Greg Chasteen Siemens PLM
43

Solid Edge API: Automating Solid Edge Basics with .net

Dec 18, 2014

Download

Technology

Solid Edge API: Automating Solid Edge Basics with .net
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Solid Edge API: Automating Solid Edge Basics with .net

#SEU12

Basics of Automating Solid Edge with .NET

Greg ChasteenSiemens PLM

Page 2: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 2

Getting Started

All major Windows programming tools that support Active X Automation or COM

Visual Basic, C++,.NET, etc.

Automation typlibs delivered with Solid Edge Physically located in program folder Just Add Reference to the typlib required

Just pick your favorite programming language and away you go.

This presentation will mainly use Visual Basic delivered with Visual Studio 2010.

Page 3: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 3

What is .NET Framework?

Google says:

A programming infrastructure created by Microsoft for building, deploying, and running applications and services that use .NET technologies, such as desktop applications and Web services.

The .NET Framework contains three major parts: the Common Language Runtime

The Common Language Runtime (CLR) is the execution engine of the .NET Framework. All .NET programs execute under the supervision of the CLR, guaranteeing certain properties and behaviors in the areas of memory management, security, and exception handling.

the Framework Class Library The .NET Framework includes a set of standard class libraries. The class

library is organized in a hierarchy of namespaces. Most of the built-in APIs are part of either System.* or Microsoft.* namespaces. These class libraries implement a large number of common functions, such as file reading and writing, graphic rendering, database interaction, and XML document manipulation, among others.

ASP.NET.

Page 4: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 4

COM Interop – Important Classes

System.GC Class Controls the system garbage collector, a service that automatically

reclaims unused memory GC.Collect() method forces garbage collectionSystem.Runtime.InteropServices Namespace that provides a wide variety of members that support COM

Interop and platform invoke services System.Runtime.InteropServices.Marshall Class Provides a collection of methods for allocating unmanaged memory,

copying unmanaged memory blocks, and converting managed to unmanaged types, as well as other miscellaneous methods used when interacting with unmanaged code.

Marshal.ReleaseComObject() method Decrements the reference count of the supplied runtime callable wrapper

Page 5: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 5

Common .NET Framework Terminology

Assembly A collection of one or more files that are versioned and deployed as a

unit. An assembly is the primary building block of a .NET Framework application.

Global Assembly Cache (GAC) A machine-wide code cache that stores assemblies specifically installed to be

shared by many applications on the computer. Applications deployed in the global assembly cache must have a strong name.

Primary Interop Assembly

• An assembly containing definitions of COM types that is distributed and digitally signed by the author of the COM component.

Page 6: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 6

Common .NET Framework Terminology

COM Interop Component Object Model (COM) is a binary-interface standard for

software componentry introduced by Microsoft in 1993. It is used to enable interprocess communication and dynamic object creation in a large range of programming languages. The term COM is often used in the Microsoft software development industry as an umbrella term that encompasses the OLE, OLE Automation, ActiveX, COM+ and DCOM technologies.

Why is COM Interop important? It is the only means by which a .NET application can communicate with

a COM application and vice-versaSolid Edge is COM based application. Therefore, API’s are Majority of applications that you will interact with are currently COM

based COM is going to be around for long, long time so better get used to it!

Page 7: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 7

Common .NET Framework Terminology

Garbage Collection (GC) The .NET Garbage Collector (GC) is a non-deterministic, compacting,

mark-and-sweep garbage collector. The GC runs only when a certain amount of memory has been used or there is enough pressure for memory on the system. Since it is not guaranteed when the conditions to reclaim memory are reached, the GC runs are non-deterministic.

So exactly what does this mean to the average Solid Edge wannbe programmer like ME! This was a much bigger issue for ST2 and previous Solid Edge versions

and .NET programs. Later SE versions especially ST4 and ST5 do a much better job determining when to FORCE this Garbage Collection. The result is ALL the responsibility for memory management is NOT on me the .NET application programmer as before in previous Solid Edge versions!

That said it is still a good idea as a programmer to appropriate Garbage Collection when necessary. This provides simply and extra “layer” or protection against crashes and memory corruption as a result of running your program.

Page 8: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 8

Confused enough!!!!

.NET Framework available versions

Page 9: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 9

Solid Edge Setup Delivers .NET Framework prerequisite

Solid Edge Version Microsoft .NET Framework

V20 2.0

ST 2.0

ST2 3.5

ST3 3.5

ST4 3.5, 4.0

ST5 3.5, 4.0

Page 10: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 10

.NET Framework Version Compatibility

Microsoft says:

Most times, applications will ask for a particular version of the framework to be installed. We'd recommend avoiding installing that particular version, and trying instead to install the most up-to-date version of .NET, assuming your Windows OS supports it. Most .NET packages have backwards compatibility, so an app asking for the 2.0 framework can usually get by with what's packaged into the latest version: .NET Framework 4.

Another common problem involves older versions of .NET and, perhaps, their misbehavior on your system. Head into your Add/Remove Programs section in Control Panel (or "Uninstall Programs" in newer Windows setups) and search for any installations related to ".NET Framework," or something very similar. Try removing them from here, through the standard uninstall procedure, then try installing your newer .NET framework again. If that still fails, it's time to turn to the .NET Framework Cleanup Tool, which was made by Microsoft itself to tidy up and set things straight following tricky .NET installations.

Page 11: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 11

Which Microsoft .NET Framework version?

Know your customer… Who will be using your application? What version of Solid Edge will they be using? What type of machines?

What Visual Studio version are you using? VS 2010 -> 4.0 .NET Framework VS 2008 -> 3.5 .NET Framework

Page 12: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 12

New Automation Project

Need an automation solution to mark dimensions on a solid Edge draft file that are critical, major and minor with diamond, square and round respectively. The idea is to automate this to help increase productivity.

REQUIREMENTS Macro that runs only in the draft environment

User selects the desired dimensions depending on which type of dimension. Select all critical dimensions then run the macro to mark the selected dimensions. Repeat process for major and minor dimensions.

Critical dimensions marked with ♦ Major dimensions marked with ■ Minor dimensions marked with ●

Page 13: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 13

Writing .NET Applications for Solid Edge

Seven General Steps To Create A Program Open a new Project Add References Create Forms Place Controls on Forms Set Properties for Forms and Controls Write code Test and Debug code Create executable and deploy

Page 14: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 14

Create a new project

Page 15: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 15

Writing .NET Applications for Solid Edge

Configuring Visual Studio .NET for Solid Edge Programming What’s involved?

Not much actually We still have to reference Solid Edge .dll’s, just in a different way

What happens when we reference a Solid Edge .dll from Visual Studio (VB).NET? Visual Studio .NET uses the Type Library Importer (Tlbimp.exe), to create a

new .NET .dll that you can use in your project This new .NET .dll contains the Runtime Callable Wrappers (RCW) that we

will use when writing .NET code Depending on Solid Edge COM API changes, this .NET interop dll may or

may not work from version to version The .NET version of the Solid Edge .dll must be deployed with your

application

Page 16: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 16

Adding the Solid Edge References

Page 17: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 17

Adding the Solid Edge References

Page 18: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 18

Result of adding the Solid Edge References

After building the solution. Here is what you get….

So where are the Interop DLLs previously discussed?

Page 19: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 19

New “feature” in VS 2010

VB 2010 has the ability to

embed the Interops

This is “handy” but is it a good

Idea?

Development says still a good idea

to generate unique interops and

reference those and to not embed!

Page 20: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 20

Generating Unique Interops

Tool delivered with Solid

Edge

I generate a bat file for each Solid Edge macro I write and save it to the solution folder.

Page 21: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 21

Generating Unique Interops

Delivered in the Solid Edge SDK\tools folder GenEdgeInteropAssemblies.bat

First argument "C:\Program Files\Solid Edge ST5\Program“ - where you have SE loaded

Second argument "C:\_Work\Events\Dev Day SE University 2012\Automating SE basics

with .NET\Sample\Sample\Interops“ - the location were the generated interop DLLs will be placed

Third argument Sample - This should be a unique label. I typically match the macro name

Page 22: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 22

Generating Unique Interops

Resulting uniquely name Interop DLLs

Page 23: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 23

How to reference the unique Interop DLLs

Run the Add Reference command and select the necessary interops

Page 24: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 24

Results

Page 25: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 25

Creating Forms

Page 26: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 26

Add Code

If you automate Solid Edge from an external (out-of-proc) application, you may receive one of the following errors:

Application is busy (RPC_E_CALL_REJECTED 0x80010001)

Call was rejected by callee (RPC_E_SERVERCALL_RETRYLATER 0x8001010A)

These errors occur due to threading contention issues between automation clients

and Solid Edge. They can be handled by implementing the OleMessageFilter error

handlers in your automation application.

Page 27: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 27

Add Code

Making your macro connect to or start Solid Edge

Page 28: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 28

Add Code

Page 29: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 29

Page 30: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 30

Tips

Try, Catch & Finally

VB now supports structured exception handling, which helps you create and maintain programs with robust, comprehensive error handlers

Page 31: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 31

Tips

An executable created with VB .NET 2003 - 2008 because of .NET security issues can not be run across a network drive because of trust.

• Must tell each client the macro is trusted!

• With VB .NET 2010 this has been lifted provided you are Framework 4.0 based

You can again store all your SE macros on a mapped network drive to simplify deployment of fixes or enhancements to your macros to all your customers!

Page 32: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 32

Tips

In automation situations where a client is keeping Solid Edge extremely busy, Solid Edge will not have an opportunity to perform background tasks. Depending on the automation scenario, this could mean that certain objects may not be in an expected state.

One of the most important tasks that DoIdle() performs is to ensure that a SolidEdgeDocument gets fully released after SolidEdgeDocument.Close is called. This is significant because even though SolidEdgeDocument.Close is called, the calling client is still holding a reference to the COM object via IUnknown.AddRef(). It is recommended that DoIdle() be called after any document is closed if the client that closed the document is going to do more processing before returning control to Solid Edge.

Page 33: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 33

Tips

General rule of thumb which reference to use Solid Edge Framework Type Library- All APIs for functionality common across

all environments at the document level Solid Edge FrameworkSupport Type Library- All APIs for functionality common

across all environments for 2D elements (lines, dimensons, boundaries,etc) Solid Edge Geometry Type Library - All APIs for functionality regarding both 2D

and 3D elements ( body,face,curve,etc.) Solid Edge Part Type Library – All APIs for functionality specific to the Part and

Sheetmetal environments Solid Edge Assembly Type Library - All APIs for functionality specific to the

Assembly environment Solid Edge Draft Type Library - All APIs for functionality specific to the DRAFT

environment Solid Edge Constants Type Library - All constants example igRight,

igUnitDistance, etc Solid Edge Revision Manager Object Library - All APIs for functionality specific

to insight connect/revision manager

Page 34: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 34

Tips

General rule of thumb which reference to use - Continued Solid Edge Install Data Library - lightweight API to determine Solid Edge

information such as path version, etc. Solid Edge File Properties Object Library – Lightweight API to get read/write

access to a Solid Edge document’s file properties Solid Edge Part Viewer Control – This is an OCX control that can be used to

view SE document Solid Edge Web Parts 1.0 Type Library – This is an API to get access to certain

Sharepoint related functionality

Page 35: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 35

Tips

Keep commonly used functions and sub routines in your SE macro template….

http://www.codeproject.com/Articles/88957/Write-Templates-for-Visual-Studio-2010

This is very useful… As I write little apps, I come across things I find myself

having to code over and over again. As I encounter these, I’ll update my

VB 2010 .NET template as shown in the above link!!!

Write Templates for Visual Studio 2010 - CodeProject.mht - Shortcut.lnk

Page 36: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 36

Tips

Keep common subroutines and functions in a dll 1 common source…. If need to make a change then ALL your customization

that references this DLL can automatically recognize any changes/enhancements.

Add the code to a class library project, build it and then reference it so all

Functions and subroutines can be called from your other customization projects

Page 37: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 37

Tips

Add a reference to your dll housing your common functions and subroutines

Page 38: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 38

Tips

Page 39: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 39

Tips

Add your functions to your solution

Page 40: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 40

Tips

How to call your DLL

Page 41: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 41

Tips

Page 42: Solid Edge API: Automating Solid Edge Basics with .net

© Siemens AG 2012. All Rights Reserved.

Siemens PLM Software Page 42

Tips

Online templates

Page 43: Solid Edge API: Automating Solid Edge Basics with .net

#SEU12

Thank You!Questions?