Create and display b-spline curve using SOLIDWORKS API

Edit ArticleEdit Article

Preview of b-spline curve
Preview of b-spline curve

This VBA example demonstrates the use of IModeler::CreateBsplineCurve method to create and preview b-spline curve using sample data.

Open part document and run the macro. Curve will be previewed and macro stops. Continue the macro to dispose the curve.

Follow the Get B-Spline Curve Parameters example for a guide of extracting the required data from the selected edge.

Type DoubleRec
    dValue As Double
End Type
Type Int2Rec
    iLower As Long
    iUpper As Long
End Type

Dim swApp As SldWorks.SldWorks
Dim swModeler As SldWorks.Modeler

Sub main()

    Set swApp = Application.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Set swModel = swApp.ActiveDoc
    Set swModeler = swApp.GetModeler
    Dim dProps(1) As Double
    dProps(0) = PackToDouble(3, 4)
    dProps(1) = PackToDouble(4, 0)
    Dim dKnots(7) As Double
    dKnots(0) = 0: dKnots(1) = 0: dKnots(2) = 0: dKnots(3) = 0
    dKnots(4) = 1: dKnots(5) = 1: dKnots(6) = 1: dKnots(7) = 1
    Dim dCtrlPts(11) As Double
    dCtrlPts(0) = 0: dCtrlPts(1) = 0: dCtrlPts(2) = 0
    dCtrlPts(3) = -0.15: dCtrlPts(4) = 0: dCtrlPts(5) = 0.05
    dCtrlPts(6) = 0.25: dCtrlPts(7) = 0: dCtrlPts(8) = 0.2
    dCtrlPts(9) = -0.05: dCtrlPts(10) = 0: dCtrlPts(11) = 0.25
    Dim swCurve As SldWorks.Curve
    Set swCurve = swModeler.CreateBsplineCurve(dProps, dKnots, dCtrlPts)
    Dim swCurveBody As SldWorks.Body2
    Set swCurveBody = swCurve.CreateWireBody
    swCurveBody.Display3 swModel, RGB(255, 255, 0), swTempBodySelectOptions_e.swTempBodySelectOptionNone
End Sub

Function PackToDouble(val1 As Long, val2 As Long) As Double
    Dim dr As DoubleRec
    Dim i2r As Int2Rec
    i2r.iLower = val1
    i2r.iUpper = val2
    LSet dr = i2r
    PackToDouble = dr.dValue
End Function


All articles and code at CodeStack are now open-source and hosted on GitHub. If you want to contribute by modifying existing articles and code snippets, submitting new ones, reporting errors and bugs etc. please follow this blog post for more information. We appreciate any contribution.

Product of Xarial Product of Xarial