1 Reply Latest reply on Feb 15, 2018 1:03 PM by Attilio Colangelo

    How to use macro to do simulation setting and export result?

    Marco Wu

      I want to use macro to do FEA setting (choose solver and enable/disable soft spring) and export the min safety factor to excel. How to do that? Note: the setting is also in an Excel

        • Re: How to use macro to do simulation setting and export result?
          Attilio Colangelo

          This should get you started. Excuse the formatting.


          Sub main()

          Dim SwApp                         As SldWorks.SldWorks

          Dim Part                           As SldWorks.ModelDoc2

          Dim COSMOSWORKS    As CosmosWorksLib.COSMOSWORKS

          Dim CWAddinCallBack      As CosmosWorksLib.CWAddinCallBack

          Dim ActDoc                        As CosmosWorksLib.CWModelDoc

          Dim StudyMngr                As CosmosWorksLib.CWStudyManager

          Dim Study                    As CosmosWorksLib.CWStudy

          Dim xlApp                As Excel.Application

          Dim xlBook                As Excel.Workbook

          Dim wSheet                As Excel.Worksheet

          Set xlApp = GetObject(, "Excel.Application")

          Set xlBook = xlApp.ActiveWorkbook

          Set wSheet = xlBook.Sheets("Sheet1")


          Set Study = StudyMngr.CreateNewStudy3("Static1", swsAnalysisStudyType_e.swsAnalysisStudyTypeStatic, 0, errCode)

          Set StaticOptions = Study.StaticStudyOptions

          StaticOptions.SolverType = 1

          StaticOptions.UseSoftSpring = 1

          errCode = Study.RunAnalysis

          Set Results = Study.Results

          'See the API help for details on GetMinMaxFactorOfSafety2

          '2018 SOLIDWORKS API Help - GetMinMaxFactorOfSafety2 Method (ICWResults)

          'resultsArray = Results.GetMinMaxFactorOfSafety2

          'This writes the maximum factor of safety to Excel

          wSheet.Range("A1").Value = resultsArray(1)

          End Sub