3 Replies Latest reply on Mar 28, 2015 11:34 AM by Kasun Udayanga

    How to access selected edges respectively?

    Kasun Udayanga

      Number of edges in a model are selected before run the macro. When the macro is run ,it should iterate through each and every edge separately. I will include another  program in that loop which will create points on that edges. Sample model which have selected edges is attached herewith too.

      Great help.problem 2.JPG

        • Re: How to access selected edges respectively?
          Santosh Pawar

          You need to use GetSelectedObjectCount2 Method (ISelectionMgr) in combination with GetSelectedObject6 Method (ISelectionMgr) to process all selected edges.

           

          Links to sample macros can be found here,

           

          2015 SOLIDWORKS API Help - GetSelectedObjectCount2 Method (ISelectionMgr)

            • Re: How to access selected edges respectively?
              Kasun Udayanga

              Thanks for your information. But i do not get clear picture about how to do it. BR.

              • Re: How to access selected edges respectively?
                Kasun Udayanga

                I have tried with two Methods which you have mentioned. But it failed to give required functionality. I have inserted the VBcode used for the macro too.

                Best Regards.

                 

                Private Sub CommandButton1_Click()

                 

                 

                create_points

                 

                 

                End Function

                 

                 

                 

                 

                ' Preconditions:

                '          (1) Part or assembly document is open.

                '          (2) Edge is selected.

                ' Postconditions: Six, evenly spaced, reference points are created on the

                '          selected edge.

                '------------------------------------

                 

                Private Function create_points()

                 

                    Dim swApp           As SldWorks.SldWorks

                    Dim swmodel         As SldWorks.ModelDoc2

                    Dim swFeatMgr       As SldWorks.FeatureManager

                    Dim vFeatArr        As Variant

                    Dim vFeat           As Variant

                    Dim swFeat          As SldWorks.Feature

                    Dim swRefPt         As SldWorks.RefPoint

                    Dim swRefPtData     As SldWorks.RefPointFeatureData

                    Dim swMathPt        As SldWorks.MathPoint

                    Dim nStatus         As Long

                    Dim bRet            As Boolean

                    Dim pointData       As String 'store the point coordinates data of the relevant point

                    Dim noOfPoints      As Integer 'stores number of points in a edge

                    'to get edge length

                    Dim swEdge          As Object

                    Dim swCurve         As Object

                    Dim vcurveparam     As Variant

                    Dim edgeLength      As Double

                    Dim noOfEdges       As Integer

                    Dim obj             As Object

                       

                    Set swApp = Application.SldWorks

                    Set swmodel = swApp.ActiveDoc

                    Set swFeatMgr = swmodel.FeatureManager

                    ' for edge length

                    Set swSelMgr = swmodel.SelectionManager

                    

                    'Get number of selected edge count

                    noOfEdges = swSelMgr.GetSelectedObjectCount2(0)

                    MsgBox ("no of selected edges : " & noOfEdges)

                 

                    'Open the text file

                    Open "D:\test.txt" For Append As #1 ' modify starting from end of the file

                   

                    'Line number for the code

                    Dim N As Integer

                    N = 100 'initialized

                   

                    Dim i As Integer

                    For i = 1 To noOfEdges

                   

                        Set swEdge = swSelMgr.GetSelectedObject6(i, 1)

                        Set swCurve = swEdge.GetCurve

                       

                        'get the curve parameters

                        vcurveparam = swEdge.GetCurveParams2

                        'get edge length

                        edgeLength = swCurve.GetLength2(vcurveparam(6), vcurveparam(7))

                        'calculate number of points

                        noOfPoints = edgeLength * 10 / cb_distance.Value

                       

                        'An edge must be preselected

                        vFeatArr = swFeatMgr.InsertReferencePoint(swRefPointAlongCurve, swRefPointAlongCurveEvenlyDistributed, 0#, noOfPoints)

                       

                        For Each vFeat In vFeatArr

                            Set swFeat = vFeat

                            Set swRefPt = swFeat.GetSpecificFeature2

                            Set swRefPtData = swFeat.GetDefinition

                            Set swMathPt = swRefPt.GetRefPoint

                                   

                            'Get point coordinates and print to the text file

                            pointData = "N" & N & " G01 " & " X" & swMathPt.ArrayData(0) * 1000# & " Y" & swMathPt.ArrayData(1) * 1000# & " Z" & swMathPt.ArrayData(2) * 1000#

                       

                            'override to txt document

                            Print #1, pointData

                           

                            N = N + 2

                                 

                        Next

                        Debug.Print i

                    Next

                 

                    'close the text document

                    Close #1

                    MsgBox ("Points created ")

                       

                End Function