12 Replies Latest reply on Mar 2, 2018 4:01 AM by Murali B,B.

    Macro to divide a spline in points

    Alex Reds

      Hi guys, I have a macro that divide a spline in a number of points (100 in my macro). But there is a problem: my macro creates these points OUTSIDE the sketch. I need instead that my macro creates points INSIDE MY SKETCH.

       

      The code of my macro is:

       

       

       

      Option Explicit

       

       

      Dim swApp As SldWorks.SldWorks

       

      Dim swModel As SldWorks.ModelDoc2

       

      Dim swSelMgr As SldWorks.SelectionMgr

       

      Dim vFeatArr As Variant

       

      Dim swFeatMgr As SldWorks.FeatureManager

       

      Dim swModelDocExt As SldWorks.ModelDocExtension

       

      Dim swSpline As SldWorks.SketchSpline

       

      Dim swFeat As SldWorks.Feature

       

      Dim myRefPlane As SldWorks.RefPlane

       

      Dim boolstatus As Boolean

       

      Dim i As Long

       

       

      Sub main()

       

       

          Set swApp = Application.SldWorks

       

          Set swModel = swApp.ActiveDoc

       

          Set swSelMgr = swModel.SelectionManager

       

          Set swFeatMgr = swModel.FeatureManager

       

          Set swModelDocExt = swModel.Extension

       

       

          If Not swSelMgr.GetSelectedObjectType3(1, -1) = swSelSKETCHSEGS Then

              MsgBox "select spline"

              Exit Sub

          End If

       

       

          Dim Spline As Object

          Set Spline = swSelMgr.GetSelectedObject6(1, -1)

       

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

        

      End Sub

       

       

       

      May you help me?

      I post the orginal macro too.

       

       

      Thanks a lot

        • Re: Macro to divide a spline in points
          Deepak Gupta

          Alex this macro creates reference points along the curve and they can't be inside the sketch.

            • Re: Macro to divide a spline in points
              Simon Turner

              I suppose you could examine the results of InsertReferencePoint, create a 3d sketch and create points at the correct coordinates.

              Then you could delete the original reference points.

              • Re: Macro to divide a spline in points
                Alex Reds

                And if I would create sketch points instead of reference points, how could I do?

                  • Re: Macro to divide a spline in points
                    Simon Turner

                    My suggestion is to create both. Create the rereference points as you are doing now. Then interrogate the results using RefPoint.GetRefPoint and then create 3d sketch points using those coordinates.

                    There is an example in the api help for creating refpoints and getting the coordinates. Search for Insert Reference Points Example (VBA)

                      • Re: Macro to divide a spline in points
                        Alex Reds

                        Ok now I think to have created a macro that creates 100 ref points along a spline and interrogates the results using GetRefPoint. But I don't know how I could create 3D sketch using those coordinates. May you help me to write this part of code?

                         

                        The code is:

                         

                         

                        Option Explicit

                         

                         

                        Dim swApp                       As SldWorks.SldWorks

                         

                        Dim swModel                     As SldWorks.ModelDoc2

                         

                        Dim swSelMgr                    As SldWorks.SelectionMgr

                         

                        Dim vFeatArr                    As Variant

                         

                        Dim swFeatMgr                   As SldWorks.FeatureManager

                         

                        Dim swModelDocExt               As SldWorks.ModelDocExtension

                         

                        Dim swSpline                    As SldWorks.SketchSpline

                         

                        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 boolstatus                  As Boolean

                         

                        Dim i                           As Long

                         

                         

                        Sub main()

                         

                         

                            Set swApp = Application.SldWorks

                         

                            Set swModel = swApp.ActiveDoc

                         

                            Set swSelMgr = swModel.SelectionManager

                           

                            Set swFeatMgr = swModel.FeatureManager

                         

                            Set swModelDocExt = swModel.Extension

                         

                           

                            If Not swSelMgr.GetSelectedObjectType3(1, -1) = swSelEXTSKETCHSEGS Then

                                MsgBox "select spline"

                                Exit Sub

                            End If

                         

                            Dim Spline As Object

                            Set Spline = swSelMgr.GetSelectedObject6(1, -1)

                          

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

                          

                         

                            For Each vFeat In vFeatArr

                         

                         

                                Set swFeat = vFeat

                         

                                Set swRefPt = swFeat.GetSpecificFeature2

                         

                                Set swRefPtData = swFeat.GetDefinition

                         

                                Set swMathPt = swRefPt.GetRefPoint

                         

                         

                                Debug.Print "  " & swFeat.Name

                         

                                Debug.Print "    Pt = (" & swMathPt.ArrayData(0) * 1000# & ", " & swMathPt.ArrayData(1) * 1000# & ", " & swMathPt.ArrayData(2) * 1000# & ") mm"

                         

                         

                                Debug.Print "    AlongCurveOption   = " & swRefPtData.AlongCurveOption

                         

                                Debug.Print "    Distance           = " & swRefPtData.Distance * 1000# & " mm"

                         

                                Debug.Print "    Type               = " & swRefPtData.Type

                         

                            Next

                           

                        End Sub

                          • Re: Macro to divide a spline in points
                            Simon Turner

                            Here you go:

                             

                            Option Explicit

                             

                            Dim swApp                       As SldWorks.SldWorks

                            Dim swModel                     As SldWorks.ModelDoc2

                            Dim swSelMgr                    As SldWorks.SelectionMgr

                            Dim vFeatArr                    As Variant

                            Dim swFeatMgr                   As SldWorks.FeatureManager

                            Dim swModelDocExt               As SldWorks.ModelDocExtension

                            Dim swSpline                    As SldWorks.SketchSpline

                            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 swSkPt as SketchPoint

                            Dim swMgr as SketchManager

                            Dim nStatus                     As Long

                            Dim bRet                        As Boolean

                            Dim boolstatus                  As Boolean

                            Dim i                           As Long

                             

                            Sub main()

                             

                                Set swApp = Application.SldWorks

                                Set swModel = swApp.ActiveDoc

                                Set swSelMgr = swModel.SelectionManager

                                Set swFeatMgr = swModel.FeatureManager

                                Set swModelDocExt = swModel.Extension

                             

                                If Not swSelMgr.GetSelectedObjectType3(1, -1) = swSelEXTSKETCHSEGS Then

                                    MsgBox "select spline"

                                    Exit Sub

                                End If

                             

                                Dim Spline As Object

                             

                                Set Spline = swSelMgr.GetSelectedObject6(1, -1)

                             

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

                             

                             

                                Set swMgr=swModel.SketchManager

                                swModel.ClearSelection2 True

                                swModel.Insert3DSketch2 False

                                swMgr.AddToDB = True

                             

                                For Each vFeat In vFeatArr

                                    Set swFeat = vFeat

                                    Set swRefPt = swFeat.GetSpecificFeature2

                                    Set swRefPtData = swFeat.GetDefinition

                                    Set swMathPt = swRefPt.GetRefPoint

                             

                                    Set swSkPt = swMgr.CreatePoint(swMathPt.ArrayData(0),swMathPt.ArrayData(1),swMathPt.ArrayData(2))

                                Next

                                swMgr.AddToDB = False

                                swModel.ClearSelection2 True

                                swModel.Insert3DSketch2 True

                            End Sub

                    • Re: Macro to divide a spline in points
                      Alex Reds

                      Thank you very much for your help!!Now it works very well!

                      • Re: Macro to divide a spline in points
                        Korbi Anis

                        Hello my friends , I have a question about your subject ! its very intersting for me ! . If I create for example a  face curves . its means I will select surface and divide it with spline like in the picture enclosed. after I will select everytime two lines , compare them if similar or not and if not similar and intersect , then get the instersection points like you lool in the second picture in attachement . how can I do this with macro . I found a macro but using C# .I want somthing with VBA solidworks.thanks in advance Capture.JPGCapture2.JPG