automating analysis with the api

36
Amazing API Analysis Automation Applications, Analytical Approaches and Advanced Advice Paul Gimbel, Business Process Sherpa Razorleaf Corporation

Upload: razorleaf-corporation

Post on 21-Jan-2015

1.373 views

Category:

Technology


1 download

DESCRIPTION

The SolidWorks API can automate your design simulations as easily as the building of the models. Your automation tools (even 3rd party apps) can integrate SolidWorks Simulation into the process with just a little bit of code and forward thinking. It's easier than you think.

TRANSCRIPT

Page 1: Automating Analysis with the API

Amazing API Analysis Automation Applications, Analytical Approaches and Advanced AdvicePaul Gimbel, Business Process Sherpa

Razorleaf Corporation

Page 2: Automating Analysis with the API

Slide 2 of 2.8 e+57

BACKGROUND

• Razorleaf Corporation SolidWorks Service Partner

Services ONLY (we’re not trying to sell you any products, we’re neutral)

Data Management (EPDM, Enovia, SmarTeam, Aras, V6, MatrixOne)

Design Automation (DriveWorks, Tacton, Custom Programmed API)

Workflow Automation (Microsoft SharePoint and Tools)

• Paul Gimbel (aka “The Sherpa”)

Mechanical Engineer, SolidWorks Demojock, Automation Implementer

All Razorleaf presentations will be available at www.razorleaf.com and on www.slideshare.net

Page 3: Automating Analysis with the API

Slide 3 of 2.8 e+57

DISCLAIMER

We will be discussing

• Automating BASIC SolidWorks Simulation programmatically

• One or two tools within SolidWorks to validate designs

This an ADVANCED session that assumes:

• you know how to do this stuff with the SolidWorks user interface

• you know the basics of VBA and SolidWorks API programming

ALL problems simplified for presentation purposes

ALL SolidWorks CODE EXAMPLES IN VBA and VB.NET (Excel uses VBA)

• Presentation will be available at www.razorleaf.com and on www.slideshare.net

Page 4: Automating Analysis with the API

Slide 4 of 2.8 e+57

Realistic Applications of Code

• Batch running simulations

Changing geometry

Changing boundary conditions

Changing loading conditions

Changing mesh/study options

• Output data to another system

Write to Excel for further analysis

Output to XML for import

• Iterative Solving

Export to a system that determines new inputs based on outputs

Page 5: Automating Analysis with the API

Slide 5 of 2.8 e+57

What To Use the API For

Drive.

Don’t

Build.

Page 6: Automating Analysis with the API

Slide 6 of 2.8 e+57

A Note About Selecting Model Entities

• SolidWorks uses internal identifiers that persist across SolidWorks sessions

• Persistent Reference Identifiers aka “PIDs”

• SolidWorks provides a tool to find them: <install>\api\pidcollector.exe1. Launch it with SolidWorks open2. Select an entity3. Click Copy PIDS to Clipboard4. Fetch them from the clipboard5. Use them with ModelDoc2.Extension.GetObjectByPersistReference3

Page 7: Automating Analysis with the API

THE BASICS OF EVERY SIMULATION API APPLICATION

Let’s “Git ‘Er Done”

Page 8: Automating Analysis with the API

Slide 8 of 2.8 e+57

Required Reference (VBA)

• Tools>>References

Make sure you grab the SolidWorks Type and Constant Libraries, too

Page 9: Automating Analysis with the API

Slide 9 of 2.8 e+57

Required Reference (.NET)

• You need to add the reference manually

Add Reference>Browse to> <installationDirectory>\api\redist\SolidWorks.Interop.cosworks.dll

<install>\api\redist

Page 10: Automating Analysis with the API

Slide 10 of 2.8 e+57

Add cosworks interop reference

SolidWorks Simulation Add-In

SolidWorks Simulation

Active Document

Study Manager

Study (Create)

Solid Manager Solid Component Solid Body Set Material

Loads and Restraints Manager

Loads

Force

Pressure

RestraintsRigid Connector

RestraintMesh

Run (Method)

Results Result Value (Array)

Follow The Trail of Objects

Page 11: Automating Analysis with the API

Slide 11 of 2.8 e+57

Another “Reference” to Keep Handy

Page 12: Automating Analysis with the API

Slide 12 of 2.8 e+57

Getting SolidWorks and The Simulation Object (VBA)

' Connect to SolidWorks

Dim swApp As SldWorks.SldWorks

Set swApp = CreateObject("SldWorks.Application")

'Get the SolidWorks Simulation object

Dim simAddIn As CosmosWorksLib.CwAddincallback

Set simAddIn = swApp.GetAddInObject("CosmosWorks.CosmosWorks")

'Use the Add-In Object to get the Simulation Object

Dim simObject As CosmosWorksLib.COSMOSWORKS

Set simObject = simAddIn.COSMOSWORKS

NOTE!!! All error trapping and object presence testing has been removed to make the slides shorter.

BE SURE TO CHECK EVERY OBJECTIF simObject Is Nothing Then ErrorReport(“No object!”)

Page 13: Automating Analysis with the API

Slide 13 of 2.8 e+57

Get the Simulation Document (VBA)

'Open the SolidWorks document and capture it as an object

Dim swDoc As SldWorks.ModelDoc2

Set swDoc = RLOpenSWDocSilent(swApp, Range("PartName").Value)

'Open and get the Simulation document

Dim simDoc As CosmosWorksLib.CWModelDoc

Set simDoc = simObject.ActiveDoc

Page 14: Automating Analysis with the API

Slide 14 of 2.8 e+57

Get Simulation Study By Name (VBA)

‘Get the Study Manager object

Dim simStudyMgr As CosmosWorksLib.CWStudyManager

Set simStudyMgr = RLGetStudyManager(simDoc)

'Get the Simulation Study (from the Excel range “StudyName”)

Dim simStudy As CosmosWorksLib.CWStudy

Set simStudy = RLGetStudyByName(simStudyMgr, Range("StudyName").Value)

Study (Create)

Solid Manager Solid Component Solid Body Set Material

Loads and Restraints Manager

LoadsForce

Pressure

RestraintsRigid Connector

RestraintMesh

Run (Method)

Results Result Value (Array)

Page 15: Automating Analysis with the API

Slide 15 of 2.8 e+57

Cycling Through the Study Manager (VBA)

Public Function RLGetStudyByName(simStudyMgr As CWStudyManager) _

As CosmosWorksLib.CWStudy

Dim simStudy As CosmosWorksLib.CWStudy

Dim n As Integer = 0

simStudyMgr.ActiveStudy = 0 'Activate the first Study

Do 'Loop through the studies

Set simStudy = simStudyMgr.GetStudy(n)

'Increment the study number

n = n + 1

Loop Until simStudy.Name = StudyName

Set RLGetStudyByName = simStudy

End Function ‘RLGetStudyByName

NOTE!! Error trapping removed for presentation purposes.

Page 16: Automating Analysis with the API

DRIVING MATERIALSLet’s Get Automating!

Page 17: Automating Analysis with the API

Slide 17 of 2.8 e+57

Setting Material – Getting Solid Component (VBA)

Public Function RLGetSimCompByName(simCompMgr As _

CWSolidManager, CompName As String) As CWSolidComponent

Dim simComp As CosmosWorksLib.CWSolidComponent

Dim errCode As Long

Dim n As Integer = 0

Do

Set simComp = simCompMgr.GetComponentAt(n, errCode)

n = n + 1

Loop Until simComp.ComponentName = CompName

Set RLGetSimCompByName = simComp

End Function 'RLGetSimCompByName

Page 18: Automating Analysis with the API

Slide 18 of 2.8 e+57

Setting Material – Getting Solid Body (VBA)

Public Function RLGetSimBodyByName(simComp As _

CWSolidComponent, BodyName As String) As CWSolidBody

Dim simBody As CosmosWorksLib.CWSolidBody

Dim errCode As Long

Dim n As Integer = 0

Do

Set simBody = simComp.GetSolidBodyAt(n, errCode)

n = n + 1

Loop Until simBody.SolidBodyName = BodyName

Set RLGetSimBodyByName = simBody

End Function 'RLGetSimBodyByName

Sample Solid Body Name:SolidBody 1(BreakFace)

Page 19: Automating Analysis with the API

Slide 19 of 2.8 e+57

Set Material (VBA)

'Set the material

simSolidBody.SetLibraryMaterial "C:\SWLib\Custom Matls", "5052-H32"

'Build a Material

Dim simMaterial As CosmosWorksLib.CWMaterial

Set simMaterial = New CosmosWorksLib.CWMaterial

simMaterial.Category = "Toys“

simMaterial.Description = "The One and Only Silly Putty“

simMaterial.MaterialName = "Silly Putty“

simMaterial.MaterialUnits = swsUnitSystemIPS

simMaterial.ModelType = swsMaterialModelTypeViscoElastic

simMaterial.SetPropertyByName "EX", 0.0014, 1

simSolidBody.SetSolidBodyMaterial simMaterial

Page 20: Automating Analysis with the API

DRIVING FORCESAnalysis Loads, that is…

Page 21: Automating Analysis with the API

Slide 21 of 2.8 e+57

Retrieving the Loads and Restraints Manager (VBA)

'Get the loads and restraints manager

Dim simLoadMgr As CWLoadsAndRestraintsManager

Set simLoadMgr = simStudy.LoadsAndRestraintsManager

Page 22: Automating Analysis with the API

Slide 22 of 2.8 e+57

Finding a Force by Name And Driving Magnitude (VBA)

Dim simForce As Object ‘(CosmosWorksLib.CWForce)

Dim LoadIndex As Long, errCode As Long

For LoadIndex = 0 To simLoadMgr.Count – 1

Set simForce = simLoadMgr.GetLoadsAndRestraints(LoadIndex, errCode)

If simForce.Name = Parameter.ParamName Then

If simForce.Type = swsLoadsAndRestraintsTypeForce Then

simForce.ForceBeginEdit

simForce.NormalForceOrTorqueValue = CDbl(Parameter.ParamValue)

simForce.ForceEndEdit

End If 'this is a force object

End If 'the name matches

Next LoadIndex

Page 23: Automating Analysis with the API

Slide 23 of 2.8 e+57

Difficulty in Driving a Force

To change a directional (non-normal) force:simForce.ForceBeginEdit

Dim bX As Integer, bY As Integer, bZ As Integer

Dim X As Double, Y As Double, Z As Double

simForce.GetForceComponentValues bX, bY, bZ, X, Y, Z

simForce.SetForceComponentValues bX, bY, bZ, X, Y, _

CDbl(Parameter.ParamValue)

simForce.ForceEndEdit

THIS DOES NOT WORK!!!!!

Page 24: Automating Analysis with the API

THE SIMULATION AUTOTESTERA Practical Sample In Microsoft Excel/VBA

Page 25: Automating Analysis with the API

Slide 25 of 2.8 e+57

The Autotest Tool

• Enter model information at the top

• Fill in column 1 with test case names

• Fill in row 1 of the table with parameter and result names

• Choose types in row 3

• Fill in values

• Push the button

• Sit back

• Be amazed

• Go get some coffee

• Be amazed some more

• Call a supplier

• Be more amazed (and a bit tired)

• Use Excel to chart your results

Page 26: Automating Analysis with the API

Slide 26 of 2.8 e+57

The Process

Report Information Back To Excel

Extract Results Format Results Push Results to Cells

Run Analysis

Create Mesh Run Gather Results

Drive Model

Drive Dimensions Feature SuppressionParse Excel Information

Parameters to Drive Results to Return

Gather Simulation Objects

Add-In Simulation Document Study Manager Study

Page 27: Automating Analysis with the API

Slide 27 of 2.8 e+57

The Internal Structure

Page 28: Automating Analysis with the API

Slide 28 of 2.8 e+57

Classes, Methods and Properties

Page 29: Automating Analysis with the API

DEMO AND REVIEW OF CODEOff to the code!

Page 30: Automating Analysis with the API

SENSORS(Programatically Speaking, Of Course)

Page 31: Automating Analysis with the API

Slide 31 of 2.8 e+57

Sensors

(PART)

(ASSY)

• Sensor types

• Notice for interactive users

• Potential performance issues

• Suppress extraneous sensors

• Set notification frequency

Page 32: Automating Analysis with the API

Slide 32 of 2.8 e+57

Working with Sensors in Code

• Get the sensor object from the Sensor Feature (use MD2.GetFirstFeature)

Dim swSensor As Sensor = swFeature.GetSpecificFeature2

• Critical sensor properties and methods

Select Case swSensor.SensorType

Case swSensorType_e.swSensorSimulation

Case swSensorType_e.swSensorMassProperty

Case swSensorType_e.swSensorDimension

Case swSensorType_e.swSensorInterfaceDetection

End Select

• Be sure to Update the sensor with the UpdateSensor method

• Check the AlertState to see if the sensor has picked up on a problem

• Use the AlertType to see what kind of a problem

SW2009 SP2 and before only supported Dimension Sensors

Page 33: Automating Analysis with the API

Slide 33 of 2.8 e+57

Using SolidWorks Events

• Create a class

• Declare a variable WITHEVENTS for the object that holds your events

Public WithEvents swPart As SolidWorks.interop.sldworks.PartDoc

• Create a Function for whatever event you want to monitor

Function swPart_SensorAlertPreNotify(ByVal swSensor As Object, ByVal _

SensorAlertType As Integer) As Integer

Shell("http://www.twitter.com/?'#SW Sensor!'", AppWinStyle.Hide, False)

End Function 'swPart_SensorAlertPreNotify

• In your main code – Activate the handler (RemoveHandler to discontinue)

AddHandler swPart.SensorAlertPreNotify, AddressOf _

Me.swPart_SensorAlertPreNotify

Check out all available events in API Help

Note who your event belongs to

Page 34: Automating Analysis with the API

Slide 34 of 2.8 e+57

Review, Tips and Tricks

• Drive, Don’t Build

• Repurpose and genericize your code to grab objects

• Use Pervasive Reference Identifiers for bulletproof selections

• Evaluate standard functionaltiy before reinventing

• Look to use code to react to SolidWorks through events

Page 35: Automating Analysis with the API

QUESTIONS (AND HOPEFULLY ANSWERS)

Here’s the Audience Participation Portion of the Show

Page 36: Automating Analysis with the API

Slide 36 of 2.8 e+57

Still Open For Questions!!!

PLEASE!!Let’s see if they really read the evaluation forms…

In the comments section, after your comments………everyone write…

“A great blend of old skool and modern dance.”

For the complete version of the presentation, including presenter notes, full code and models, visit www.razorleaf.com after the show! Yes, it’s free.

Larchaud Dance Project: Toronto

Cepolina Photo