Macro for repetitive surface loft operation

Heya;

Facing an interesting challenge; I have a series of 3D spline curves that I need to turn into a series of lofts. Each loft is between two adjacent curves in the series. I have code that generates over a thousand curves which I can load using the ScanTo3D utility. I can then manually create each loft, but given the snail-like performance when the massive amount of data is present, it takes an unreasonable amount of to do it by hand.

So, I figure its high time to jump into the world of writing macros! I have yet to find one that does this task.

From mucking about, I have code that kind of works, in that it selects the curves in the correct order. However, I have so far been unsucessful in creating a loft feature. Has anyone created surface lofts or similar features before? I am sure its something obvious that I am missing, but I am not familiar with either VBA or the Solidworks API...

Attached is part with a few curves as a generic representation of the problem, as well as the code I have so far. The code has two sections, one that uses InsertLoftRefSurface2 and one that uses CreateLoftSurface. Neither one works. Currently just attempting one loft operation, will expand it to the full series once that is working. Any ideas or pointers?

<p><span style="font-family: 'courier new', courier;">Dim swApp As SldWorks.SldWorks</span></p><p><span style="font-family: 'courier new', courier;">Dim swDoc As SldWorks.ModelDoc2</span></p><p><span style="font-family: 'courier new', courier;">Dim swSketch As SldWorks.Sketch</span></p><p><span style="font-family: 'courier new', courier;">Dim vSegs As Variant</span></p><p><span style="font-family: 'courier new', courier;">Dim vSeg As Variant</span></p><p><span style="font-family: 'courier new', courier;">Dim swSeg As SldWorks.SketchSegment</span></p><p><span style="font-family: 'courier new', courier;">Dim swModel As Object</span></p><p></p><p></p><p><span style="font-family: 'courier new', courier;">Sub main()</span></p><p></p><p><span style="font-family: 'courier new', courier;">    ' Initialize Solidworks objects</span></p><p><span style="font-family: 'courier new', courier;">    Set swApp = Application.SldWorks</span></p><p><span style="font-family: 'courier new', courier;">    Set swDoc = swApp.ActiveDoc</span></p><p><span style="font-family: 'courier new', courier;">    Set swSketch = swDoc.SketchManager.ActiveSketch</span></p><p><span style="font-family: 'courier new', courier;">    Set swModel = swApp.ActiveDoc</span></p><p><span style="font-family: 'courier new', courier;">    Dim swSelMgr As SldWorks.SelectionMgr</span></p><p><span style="font-family: 'courier new', courier;">    Set swSelMgr = swModel.SelectionManager</span></p><p></p><p></p><p><span style="font-family: 'courier new', courier;">    ' Throw an error & exit if not in sketch</span></p><p><span style="font-family: 'courier new', courier;">    If swSketch Is Nothing Then</span></p><p><span style="font-family: 'courier new', courier;">        MsgBox "No sketch active!"</span></p><p><span style="font-family: 'courier new', courier;">        Exit Sub</span></p><p><span style="font-family: 'courier new', courier;">    End If</span></p><p></p><p><span style="font-family: 'courier new', courier;">    ' Clear selection & get segments from sketch</span></p><p><span style="font-family: 'courier new', courier;">    swDoc.ClearSelection2 True</span></p><p><span style="font-family: 'courier new', courier;">    vSegs = swSketch.GetSketchSegments</span></p><p></p><p><span style="font-family: 'courier new', courier;">    ' Create points from beginnings of spline curves</span></p><p><span style="font-family: 'courier new', courier;">    Dim Curve As Object</span></p><p><span style="font-family: 'courier new', courier;">    Dim PointsObject As Variant</span></p><p><span style="font-family: 'courier new', courier;">    Dim LocStart0 As Object</span></p><p><span style="font-family: 'courier new', courier;">    Dim LocStart1 As Object</span></p><p><span style="font-family: 'courier new', courier;">    Dim boolstatus As Boolean</span></p><p></p><p><span style="font-family: 'courier new', courier;">    ' Select curve 0</span></p><p><span style="font-family: 'courier new', courier;">    Set Curve = vSegs(0).GetCurve()</span></p><p><span style="font-family: 'courier new', courier;">    PointsObject = Curve.Evaluate(0)</span></p><p><span style="font-family: 'courier new', courier;">    Set LocStart0 = swDoc.CreatePoint2(PointsObject(0), PointsObject(1), PointsObject(2))</span></p><p><span style="font-family: 'courier new', courier;">    boolstatus = swModel.Extension.SelectByID2("", "SKETCHSEGMENT", PointsObject(0), PointsObject(1), PointsObject(2), False, 1, Nothing, 1)</span></p><p></p><p><span style="font-family: 'courier new', courier;">    ' Select curve 1</span></p><p><span style="font-family: 'courier new', courier;">    Set Curve = vSegs(1).GetCurve()</span></p><p><span style="font-family: 'courier new', courier;">    PointsObject = Curve.Evaluate(0)</span></p><p><span style="font-family: 'courier new', courier;">    Set LocStart1 = swDoc.CreatePoint2(PointsObject(0), PointsObject(1), PointsObject(2))</span></p><p><span style="font-family: 'courier new', courier;">    boolstatus = swModel.Extension.SelectByID2("", "SKETCHSEGMENT", PointsObject(0), PointsObject(1), PointsObject(2), True, 1, Nothing, 1)</span></p><p></p><p></p><p><span style="font-family: 'courier new', courier;">    ' Create surface loft</span></p><p><span style="font-family: 'courier new', courier;">    swModel.InsertLoftRefSurface2 False, True, False, 1, 0, 0</span></p><p></p><p><span style="font-family: 'courier new', courier;">    ' variables for CreateLoftSurface</span></p><p><span style="font-family: 'courier new', courier;">    Dim swModeler As IModeler</span></p><p><span style="font-family: 'courier new', courier;">    Dim CurveArray(2) As Object</span></p><p><span style="font-family: 'courier new', courier;">    Dim BBlendClosed As Boolean</span></p><p><span style="font-family: 'courier new', courier;">    Dim BForceCubic As Boolean</span></p><p><span style="font-family: 'courier new', courier;">    Dim GuideCrvArray As Object</span></p><p><span style="font-family: 'courier new', courier;">    Dim StartMatchingType As Integer</span></p><p><span style="font-family: 'courier new', courier;">    Dim EndMatchingType As Integer</span></p><p><span style="font-family: 'courier new', courier;">    Dim NormalAtStartSection As Object</span></p><p><span style="font-family: 'courier new', courier;">    Dim NormalAtEndSection As Object</span></p><p><span style="font-family: 'courier new', courier;">    Dim StartMatchingFaceList As Object</span></p><p><span style="font-family: 'courier new', courier;">    Dim EndMatchingFaceList As Object</span></p><p><span style="font-family: 'courier new', courier;">    Dim DegeneratedStart As Boolean</span></p><p><span style="font-family: 'courier new', courier;">    Dim DegeneratedEnd As Boolean</span></p><p><span style="font-family: 'courier new', courier;">    Dim StartPointOfStartSection As Object</span></p><p><span style="font-family: 'courier new', courier;">    Dim StartPointOfEndSection As Object</span></p><p><span style="font-family: 'courier new', courier;">    Dim SectionIndexStart As Integer</span></p><p><span style="font-family: 'courier new', courier;">    Dim SectionIndexEnd As Integer</span></p><p><span style="font-family: 'courier new', courier;">    Dim GuideIndexStart As Integer</span></p><p><span style="font-family: 'courier new', courier;">    Dim GuideIndexEnd As Integer</span></p><p><span style="font-family: 'courier new', courier;">    Dim LoftSurface As Object</span></p><p></p><p></p><p><span style="font-family: 'courier new', courier;">    Set CurveArray(0) = vSegs(0)    ' Sets first curve</span></p><p><span style="font-family: 'courier new', courier;">    Set CurveArray(1) = vSegs(1)    ' Sets second curve</span></p><p><span style="font-family: 'courier new', courier;">    BBlendClosed = False            ' False for non-closed</span></p><p><span style="font-family: 'courier new', courier;">    BForceCubic = False             ' False for not forcing cubic surface</span></p><p><span style="font-family: 'courier new', courier;">    Set GuideCrvArray = Nothing     ' Don't have a guide curve</span></p><p><span style="font-family: 'courier new', courier;">    StartMatchingType = 0           ' Match none</span></p><p><span style="font-family: 'courier new', courier;">    EndMatchingType = 0             ' Match none</span></p><p><span style="font-family: 'courier new', courier;">    Set NormalAtStartSection = Nothing  ' Not used</span></p><p><span style="font-family: 'courier new', courier;">    Set NormalAtEndSection = Nothing    ' Not used</span></p><p><span style="font-family: 'courier new', courier;">    Set StartMatchingFaceList = Nothing ' Not used</span></p><p><span style="font-family: 'courier new', courier;">    Set EndMatchingFaceList = Nothing   ' Not used</span></p><p><span style="font-family: 'courier new', courier;">    DegeneratedStart = False            ' Not used</span></p><p><span style="font-family: 'courier new', courier;">    DegeneratedEnd = False              ' Not used</span></p><p><span style="font-family: 'courier new', courier;">    Set StartPointOfStartSection = LocStart0    'Start point of curve 0</span></p><p><span style="font-family: 'courier new', courier;">    Set StartPointOfEndSection = LocStart1      'Start point of curve 1</span></p><p><span style="font-family: 'courier new', courier;">    SectionIndexStart = 0</span></p><p><span style="font-family: 'courier new', courier;">    SectionIndexEnd = 1</span></p><p><span style="font-family: 'courier new', courier;">    GuideIndexStart = -1</span></p><p><span style="font-family: 'courier new', courier;">    GuideIndexEnd = -1</span></p><p></p><p><span style="font-family: 'courier new', courier;">    ' Create the surface loft</span></p><p><span style="font-family: 'courier new', courier;">    Set swModeler = swApp.GetModeler</span></p><p><span style="font-family: 'courier new', courier;">    Set LoftSurface = swModeler.CreateLoftSurface(CurveArray, BBlendClosed, BForceCubic, GuideCrvArray, StartMatchingType, EndMatchingType, NormalAtStartSection, NormalAtEndSection, StartMatchingFaceList, EndMatchingFaceList, DegeneratedStart, DegeneratedEnd, StartPointOfStartSection, StartPointOfEndSection, SectionIndexStart, SectionIndexEnd, GuideIndexStart, GuideIndexEnd)</span></p><p></p><p></p><p><span style="font-family: 'courier new', courier;">End Sub</span></p>
SolidworksApi macros
Answers 3
SF
SF 2012-03-24 Best Answer

Hi

If your splines are in one 3DSketch, then they should be treated as SketchContours. Try following code, which I tested on one 3DSketch:

<p>Dim swApp As SldWorks.SldWorks</p><p>Dim swModel As SldWorks.ModelDoc2</p><p>Dim swSelMgr As SldWorks.SelectionMgr</p><p>Dim bRet As Boolean</p><p></p><p></p><p>Sub main()</p><p>    Set swApp = Application.SldWorks</p><p>    Set swModel = swApp.ActiveDoc</p><p>    Set swSelMgr = swModel.SelectionManager</p><p></p><p>    Dim swFeat As SldWorks.Feature</p><p>    Dim swSketch As SldWorks.Sketch</p><p>    Dim swCont As SketchContour</p><p></p><p>    Dim vCont As Variant</p><p>    Dim vConts As Variant</p><p></p><p>    'Get selected sketch and its contours:</p><p>    Set swFeat = swSelMgr.GetSelectedObject6(1, -1)</p><p>    Set swSketch = swFeat.GetSpecificFeature2()</p><p>    vConts = swSketch.GetSketchContours()</p><p>    Dim swConts() As SldWorks.SketchContour</p><p>    ReDim swConts(swSketch.GetSketchContourCount - 1)</p><p>    Dim i As Integer</p><p>    For Each vCont In vConts</p><p>        Set swCont = vCont</p><p>        Set swConts(i) = swCont</p><p>        i = i + 1</p><p>    Next</p><p></p><p>    'Select two contours with mark 1</p><p>    bRet = swConts(0).Select(False, 1)</p><p>    bRet = swConts(1).Select(True, 1)</p><p></p><p>    'Create Loft:</p><p>    swModel.InsertLoftRefSurface2 False, False, False, 1#, 0, 0</p><p></p><p>End Sub</p><p></p>

Regards!

------------

Krzysztof Polkowski

SF
SF 2012-03-24

CreateLoftSurface gives you more flexibility meaning access to all Loft Feature options. This function needs an array of Curves. To get curves you must get SketchSegments instead of SketchContours and for each segment call GetCurve().

SF
SF 2012-03-26

Excellent! That works great! Added loop to run through the array.

<p>  ' Iterate through array of contours</p><p>    For i = 0 To UBound(vConts) - 1</p><p>        'Select two contours with mark 1</p><p>        bRet = swConts(i).Select(False, 1)</p><p>        bRet = swConts(i + 1).Select(True, 1)</p><p>        'Create loft:</p><p>        swModel.InsertLoftRefSurface2 False, False, False, 1#, 0, 0</p><p>    Next i</p>

Many thanks for your speedy reply, Krzysztof. I can't tell you how much monotonous clicking you've saved me, it is much appreciated.