AnsweredAssumed Answered

VBA Macro not working for SpringConnector

Question asked by Leonardo Presciuttini on Nov 5, 2015

The following Macro crashes SW when entering in the statement SpringConnectorBeginEdit.

If I put a stop on the statement AddSpingConnector, and I execute it by step, I can verify that the statemnt is successfully executed, but few seconds after SW crashes.

Some suggestions?

 

Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim SelMgr  As SelectionMgr
Dim feature, feat0 As feature

Sub main()
Dim varArr1 As Variant
Dim varArr2 As Variant
Dim pDisp1 As Object
Dim pDisp2 As Object
Dim SpringConnector As CWSpringConnector
Dim lrMngr As CWLoadsAndRestraintsManager
Dim StudyMngr As CWStudyManager
Dim Study As CWStudy
Dim ActDoc As CWModelDoc
Dim errCode As Long
Dim COSMOSObject As CwAddincallback
Dim COSMOSWORKS As COSMOSWORKS
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set SelMgr = Part.SelectionManager
Set COSMOSObject = swApp.GetAddInObject("SldWorks.Simulation")
Set COSMOSWORKS = COSMOSObject.COSMOSWORKS
Set ActDoc = COSMOSWORKS.ActiveDoc()
Set StudyMngr = ActDoc.StudyManager()
Set Study = StudyMngr.GetStudy(0)
Set lrMngr = Study.LoadsAndRestraintsManager
boolstatus = Part.Extension.SelectByID2("", "FACE", 2.05380000000008, -0.83628465239574, -0.175168744587097, True, 0, Nothing, 0)
If boolstatus Then
   Set pDisp2 = SelMgr.GetSelectedObject6(1, 0)
   varArr2 = Array(pDisp2)
Else
   Stop
End If
For i = 1 To 10
boolstatus = Part.Extension.SelectByID2("", "FACE", 5.17999999999574E-02, -0.791226651704349, _
              1.36487019516096E-02 + i * 0.048, True, 0, Nothing, 0)
If boolstatus Then
    Set pDisp1 = SelMgr.GetSelectedObject6(2, 0)
    varArr1 = Array(pDisp1)
Else
    Stop
End If
Set SpringConnector = lrMngr.AddSpringConnector(swsSpringConnectorType_e.swsSpringConnectoryTypeFlatParallelFaces, _
(varArr1), (varArr2), errCode)
     SpringConnector.SpringConnectorBeginEdit
     SpringConnector.Unit = swsUnit_e.swsUnitSI
     SpringConnector.NormalRadialStiffnessValue = 10000000#
     SpringConnector.SpringConnectorEndEdit
Part.ClearSelection2 True
Next

End Sub

Outcomes