Macro to create a bolt connector using circular edges

Can anyone please explain how to create a bolt connector using macro for a flange. and what is procedure to create that flange.

and My code is

////////////////////////////////////////////////////////////////////////////////////////////////////

Function boltconnectormain()

'pre conditions: the circular edges of flange plates were selected.(select an edge directly opposite to the selected an edge)

    Dim swApp           As SldWorks.SldWorks
    Dim Part            As SldWorks.ModelDoc2
    Dim selmgr          As SldWorks.SelectionMgr
    Dim COSMOSWORKS     As Object
    Dim COSMOSObject    As CosmosWorksLib.CwAddincallback
    Dim ActDoc          As CosmosWorksLib.CWModelDoc
    Dim StudyMngr       As CosmosWorksLib.CWStudyManager
    Dim Study           As CosmosWorksLib.CWStudy
    Dim LBCMgr          As CosmosWorksLib.CWLoadsAndRestraintsManager
    Dim CWBolt          As CosmosWorksLib.CWBoltConnector
    Dim CWPin           As CosmosWorksLib.CWPinConnector
    Dim errCode         As Long
    Dim var()           As Variant
    Dim obj             As Object
    Dim pDisp1          As Object
    Dim pDisp2          As Object
    Dim DispArray1      As Variant
    Dim DispArray2      As Variant
    Dim i               As Integer
    Dim m               As Integer
    Dim count           As Integer
    Dim lbcount1        As Integer
    Dim lbcount2        As Integer
    Dim swSelData       As SldWorks.SelectData
    Dim swFace          As SldWorks.Face2
   
   
    Set swApp = Application.SldWorks
    Set Part = swApp.ActiveDoc
    Set selmgr = Part.SelectionManager
    count = selmgr.GetSelectedObjectCount
    boltcreat = False
    If count = 0 Then ErrorMsg swApp, "Selection not done", True: GoTo Goout:
    If count = 1 Then ErrorMsg swApp, "Select two edges of a hole", True: GoTo Goout:
    Set COSMOSObject = swApp.GetAddInObject("SldWorks.Simulation")
    If COSMOSObject Is Nothing Then ErrorMsg swApp, "COSMOSObject object not found.", True
    Set COSMOSWORKS = COSMOSObject.COSMOSWORKS
    If COSMOSWORKS Is Nothing Then ErrorMsg swApp, "COSMOSWORKS object not found.", True
     Set ActDoc = COSMOSWORKS.ActiveDoc()
    Set StudyMngr = ActDoc.StudyManager()
    Set Study = StudyMngr.GetStudy(0)

    ReDim var(count - 1)
   
    For i = 0 To (count - 1)
        Set obj = selmgr.GetSelectedObject5(i + 1)
       var(i) = Part.Extension.GetPersistReference3(obj)
        Set obj = Nothing
    Next i
   
  
    For i = 0 To ((count / 2) - 1)
        Set pDisp1 = Part.Extension.GetObjectByPersistReference3((var((i * 2))), errCode)
        Set pDisp2 = Part.Extension.GetObjectByPersistReference3((var((i * 2) + 1)), errCode)
        DispArray1 = Array(pDisp1)
        DispArray2 = Array(pDisp2)
        Set LBCMgr = Study.LoadsAndRestraintsManager
        lbcount1 = LBCMgr.count
        Set CWBolt = LBCMgr.AddBoltConnector(swsBoltTypeStandardOrCounterboreNut, (DispArray1), (DispArray2), errCode)
        If CWBolt Is Nothing Then
            ErrorMsg swApp, "Failed to Create Bolt connector", True:
            swApp.SendMsgToUser2 "Wrong selection done by user ", 2, 2
            GoTo Goout:
        End If
        Set CWBolt = Nothing
        lbcount2 = LBCMgr.count
        For m = 0 To (lbcount2 - lbcount1 - 1)
            Set CWBolt = LBCMgr.GetLoadsAndRestraints((m + lbcount1), errCode)
            If CWBolt Is Nothing Then ErrorMsg swApp, "Failed to Get Loads and Restraints for bolt connector", True
            boltcreat = True
            CWBolt.BoltConnectorBeginEdit
            CWBolt.HeadDiameterUnit = 0
            CWBolt.BoltShankDiameterUnit = 0
            CWBolt.BoltShankDiameterValue = Bolt_Connector.TextBox1.Text  'form input

            CWBolt.HeadDiameterValue = Bolt_Connector.TextBox2.Text 'form input
            CWBolt.MaterialType = 1
            CWBolt.SetLibraryMaterial "c:\Program Files\SolidWorks Corp\SolidWorks\lang\english\sldmaterials\solidworks materials.sldmat", "Ductile Iron (SN)"
            CWBolt.BoltUnit = 0
            CWBolt.PreLoadForceType = 0
            CWBolt.PreLoadForceValue = 0.85
            CWBolt.FrictionValue = 0.2
            CWBolt.ThermalCoefficient = 0.5 'added
            errCode = CWBolt.BoltConnectorEndEdit
            Set CWBolt = Nothing
        Next m
    Next i
   
    If boltcreat = True Then
   
    Else
        swApp.SendMsgToUser2 "Wrong selection done by user " & vbCr & vbCr & "Select one edge of a hole and then select opposite edge of that hole" & vbCr & vbCr & "For to make the Bolt Connector", 2, 2
    End If
   
Goout:

End Function

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

While using this code I had a problem in that red colored code that is in " var(i)

If the count assigned in var(i) is same for both circular edges, then it will create a bolt connector

otherwise it will create a bolt connector.

Kindly help in this....

Regards,


M.Vijayakumar.

SolidworksApi macros