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.
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Automating Analysis with the API
1. Amazing API Analysis Automation Applications, Analytical Approaches and Advanced Advice Paul Gimbel, Business Process Sherpa Razorleaf Corporation
2. 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
3. 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
4. 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
6. 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>piidcollector.exe Launch it with SolidWorks open Select an entity Click Copy PIDS to Clipboard Fetch them from the clipboard Use them with ModelDoc2.Extension.GetObjectByPersistReference3
7. The Basics of Every Simulation API Application Let’s “Git ‘Er Done”
8. Required Reference (VBA) Tools>>References Make sure you grab the SolidWorks Type and Constant Libraries, too
9. Required Reference (.NET) You need to add the reference manually Add Reference>Browse to> <installationDirectory>piedistolidWorks.Interop.cosworks.dll <install>piedist
12. Getting SolidWorks and The Simulation Object (VBA) ' Connect to SolidWorks DimswAppAsSldWorks.SldWorks SetswApp = CreateObject("SldWorks.Application") 'Get the SolidWorks Simulation object DimsimAddInAsCosmosWorksLib.CwAddincallback SetsimAddIn = swApp.GetAddInObject("CosmosWorks.CosmosWorks") 'Use the Add-In Object to get the Simulation Object DimsimObjectAsCosmosWorksLib.COSMOSWORKS SetsimObject = simAddIn.COSMOSWORKS NOTE!!! All error trapping and object presence testing has been removed to make the slides shorter. BE SURE TO CHECK EVERY OBJECT IF simObject Is Nothing Then ErrorReport(“No object!”)
13. Get the Simulation Document (VBA) 'Open the SolidWorks document and capture it as an object DimswDocAs SldWorks.ModelDoc2 SetswDoc = RLOpenSWDocSilent(swApp, Range("PartName").Value) 'Open and get the Simulation document DimsimDocAsCosmosWorksLib.CWModelDoc SetsimDoc = simObject.ActiveDoc
14. Get Simulation Study By Name (VBA) ‘Get the Study Manager object DimsimStudyMgrAsCosmosWorksLib.CWStudyManager SetsimStudyMgr = RLGetStudyManager(simDoc) 'Get the Simulation Study (from the Excel range “StudyName”) DimsimStudyAsCosmosWorksLib.CWStudy SetsimStudy = RLGetStudyByName(simStudyMgr, Range("StudyName").Value)
15. Cycling Through the Study Manager (VBA) PublicFunctionRLGetStudyByName(simStudyMgrAsCWStudyManager) _ AsCosmosWorksLib.CWStudy DimsimStudyAsCosmosWorksLib.CWStudy Dim n As Integer = 0 simStudyMgr.ActiveStudy = 0 'Activate the first Study Do 'Loop through the studies SetsimStudy = simStudyMgr.GetStudy(n) 'Increment the study number n = n + 1 Loop Until simStudy.Name = StudyName SetRLGetStudyByName = simStudy End Function ‘RLGetStudyByName NOTE!! Error trapping removed for presentation purposes.
17. Setting Material – Getting Solid Component (VBA) Public Function RLGetSimCompByName(simCompMgrAs _ CWSolidManager, CompNameAsString) AsCWSolidComponent DimsimCompAsCosmosWorksLib.CWSolidComponent DimerrCodeAs Long Dim n As Integer = 0 Do SetsimComp = simCompMgr.GetComponentAt(n, errCode) n = n + 1 Loop Until simComp.ComponentName = CompName SetRLGetSimCompByName = simComp End Function 'RLGetSimCompByName
18. Setting Material – Getting Solid Body (VBA) Public Function RLGetSimBodyByName(simCompAs _ CWSolidComponent, BodyNameAsString) AsCWSolidBody DimsimBodyAsCosmosWorksLib.CWSolidBody DimerrCodeAsLong Dim n AsInteger = 0 Do SetsimBody = simComp.GetSolidBodyAt(n, errCode) n = n + 1 LoopUntilsimBody.SolidBodyName = BodyName SetRLGetSimBodyByName = simBody End Function 'RLGetSimBodyByName Sample Solid Body Name: SolidBody 1(BreakFace)
19. Set Material (VBA) 'Set the material simSolidBody.SetLibraryMaterial "C:WLibustom Matls", "5052-H32" 'Build a Material DimsimMaterialAsCosmosWorksLib.CWMaterial SetsimMaterial = NewCosmosWorksLib.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.SetSolidBodyMaterialsimMaterial
21. Retrieving the Loads and Restraints Manager (VBA) 'Get the loads and restraints manager DimsimLoadMgrAsCWLoadsAndRestraintsManager SetsimLoadMgr = simStudy.LoadsAndRestraintsManager
22. 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
23. Difficulty in Driving a Force To change a directional (non-normal) force: simForce.ForceBeginEdit DimbXAsInteger, bYAs Integer, bZAs Integer Dim X As Double, Y As Double, Z As Double simForce.GetForceComponentValuesbX, bY, bZ, X, Y, Z simForce.SetForceComponentValuesbX, bY, bZ, X, Y, _ CDbl(Parameter.ParamValue) simForce.ForceEndEdit THIS DOES NOT WORK!!!!!
25. 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
36. Working with Sensors in Code Get the sensor object from the Sensor Feature (use MD2.GetFirstFeature) DimswSensorAsSensor = swFeature.GetSpecificFeature2 Critical sensor properties and methods SelectCaseswSensor.SensorType CaseswSensorType_e.swSensorSimulation CaseswSensorType_e.swSensorMassProperty CaseswSensorType_e.swSensorDimension CaseswSensorType_e.swSensorInterfaceDetection EndSelect 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
37. Using SolidWorks Events Create a class Declare a variable WITHEVENTS for the object that holds your events PublicWithEventsswPartAsSolidWorks.interop.sldworks.PartDoc Create a Function for whatever event you want to monitor FunctionswPart_SensorAlertPreNotify(ByValswSensorAsObject, ByVal _ SensorAlertTypeAsInteger)AsInteger Shell("http://www.twitter.com/?'#SW Sensor!'", AppWinStyle.Hide, False) EndFunction 'swPart_SensorAlertPreNotify In your main code – Activate the handler (RemoveHandler to discontinue) AddHandlerswPart.SensorAlertPreNotify, AddressOf _ Me.swPart_SensorAlertPreNotify Note who your event belongs to Check out all available events in API Help
38. 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
40. 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.” Cepolina Photo Larchaud Dance Project: Toronto 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.