Creating equally spaced points on a curve

Document created by Emily Potter on Dec 29, 2009
Version 1Show Document
  • View in full screen mode

Below is an example of how to create equally spaced points on a curve in VB


Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swCurve As SldWorks.Curve
Dim swSelMgr As SldWorks.SelectionMgr
Dim swEdge As SldWorks.Edge
Dim swSketch As SldWorks.Sketch
Dim swSketchMgr As SldWorks.SketchManager


Dim vStartPt As Variant
Dim ret As Boolean
Dim uvStartPt As Double
Dim uvEndPt As Double
Dim x As Integer


Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc


Set swSelMgr = swModel.SelectionManager
x = swSelMgr.GetSelectedObjectType3(1, -1)
Set swEdge = swSelMgr.GetSelectedObject6(1, -1)
Set swSketchMgr = swModel.SketchManager


If Not swEdge Is Nothing Then
    Set swCurve = swEdge.GetCurve


    ret = swCurve.GetEndParams(uvStartPt, uvEndPt, False, False): Debug.Assert ret
End If


If ret Then
    'Grab the number of points and make sure it's greater than 0
    If UserForm1.NumberOfPoints > 0 Then
        Dim uvLength As Double
        Dim distance As Double
        Dim point As Double
        Dim i As Integer
        Dim swSkPoint As SldWorks.SketchPoint
        Dim newSketch As SldWorks.Sketch
        'Create a new sketch to add the points to
        swSketchMgr.Insert3DSketch (False)
        swSketchMgr.AddToDB = True
        Set swSketch = swSketchMgr.ActiveSketch
        i = 0
        'Get the length
        uvLength = uvEndPt - uvStartPt
        'Divide by the number of points to get the distance between the points
        distance = uvLength / UserForm1.NumberOfPoints
        'Set where the first point should be placed
        point = uvStartPt + distance
        Do While i < UserForm1.NumberOfPoints
            'Evaluate where the point should be placed on the curve
            swCurve.Evaluate (point)
            'Add the point
            Set swSkPoint = swSketchMgr.CreatePoint(vStartPt(0), vStartPt(1), vStartPt(2))
            'Increment the loop counter and the point distance
            i = i + 1
            point = point + distance
    swSketchMgr.Insert3DSketch (True)
    End If
End If