16 Replies Latest reply on Jan 12, 2018 6:05 PM by Dan Pihlaja

# How to get spline length assigned to equation variable or custom property?

I have a sweep that follows a 3D Spline. How can I assign the value returned for the length of the spline path displayed using the TOOLS>MEASURE to an equation variable? I want to be able to get the length of the 3D spline path and display this value in a custom properties cell to use in a drawing.
• ###### How to get spline length assigned to equation variable or custom property?
There is no way currently to drive the length of a spline or to get its length into a custom property except by using a macro.
• ###### How to get spline length assigned to equation variable or custom property?
One thing you could try is to create a regular line from thebeginning to the end of the spline, and then drive that length,making the end points coincident.  This would only work if youneed an approximate length.

If you want to have the spline follow an exact route, driven by anequation, you are out of luck.  Although Matt does have amacro on his webpage that creates splines from polynomial andsinusoidal equations.  It would be very difficult to constrainthat, however, unless you wanted to constrain many points along thecurve.  I would guess that you could do it the same way youset up a finite integral if you were really interested in doing it,but I'll leave such a time consuming task up to you, I won't betrying it out for fun!
• ###### How to get spline length assigned to equation variable or custom property?
I am vexed by the thought that I will be forced to extract the information through an API macro populate the custom property cells with the 3D spline length. Especially since I can select the TOOLS>MEASURE and get the data for the length of the 3D spline, but this data is not "exposed" where it can be of a useful nature. (used in a drawing to affect or to drive other dimensions) Its not like I am attempting to constrain the length of the spline. I set a start point and an endpoint and set the 3D spline to go through points P1, P2, P3 etc. If I was a SW developer, I would set up something like "MyLength"="SW-Length@@@Spline1@3DSketch1@@MyConfig@MyPart.sldprt) I should be able to extract any basic dimensional data related to a part or sketch already. (OK, im through venting!) Back to the API board!!
• ###### How to get spline length assigned to equation variable or custom property?
OK, here's my solution. I wrote this VBA macro to get the length of a 3D Spline and assign the value to a custom configuration property. Im adding this for those who may run into the same dilema. Just CUT>PASTE into a new macro and you should be ready to go!

'JHILL 1/22/07 SW 2006 SP5.0
'this macro traverses throught the configurations of a part file
'and gets the length of the specified 3D spline given and then
'places the value returned into a custom property cell call "Length"
'
'requirements:
'a) a solidoworks part file is open
'b) a spline with the name specified exists
'
'notes:
'change the sSplineName and sCustPropName below to suit your needs
'(excuse the amount of extra detail in the notes...I included them for those who may not be familiar with VBA and SW API)

Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swConfig As SldWorks.Configuration
Dim swSelMgr As SldWorks.SelectionMgr
Dim swSketchSegment As SldWorks.SketchSegment
Dim sSplineName As String
Dim length As Double
Dim sLength As String
Dim vNameArr As Variant
Dim vName As Variant
Dim boolstatus As Boolean
Dim DoNextPart As String
Dim lSaveAsOptions As Long
Dim sCustPropName As String
Dim longerrors As Long
Dim longwarnings As Long
Dim vConfNames As Variant
Dim sConfName As String
Dim sConfDesc As String
Dim nStart As Single
Dim i As Long

Sub main()

'set the name of your 3D Spline here
sSplineName = "Spline1@3DSketchPath"
'set the name of the custom property here, if it does not exist, it will be created
sCustPropName = "Length"

'set a pointer to the application object
Set swApp = Application.SldWorks
'get the currently active document
Set swModel = swApp.ActiveDoc
'check to see if a document is loaded
If (swModel Is Nothing) Then
MsgBox " No document is open"
Exit Sub
End If
'set a pointer to the selection manager
Set swSelMgr = swModel.SelectionManager
'get the document type, make sure it is the right document type
'I use this as a generic filter to trap macros being applied to wrong file types
Select Case swModel.GetType
Case Is = 1 'part

Case Is = 2 'assembly
MsgBox " Open document is not the correct type for this operation"
Exit Sub
Case Is = 3 'drawing
MsgBox " Open document is not the correct type for this operation"
Exit Sub
Case Else 'not a SW document
MsgBox " Open document is not valid"
Exit Sub
End Select
'get a list of the configuration names
vConfNames = swModel.GetConfigurationNames
'set the timer, I use this to see how long a macro takes to parse through all configurations
nStart = Timer
'loop through each configuration
For i = 0 To UBound(vConfNames)
'get the current configuration name
sConfName = vConfNames(i)
'set and show the active configuration
boolstatus = swModel.ShowConfiguration2(sConfName)
If boolstatus = False Then
MsgBox " Configuration not activated successfully " & sConfName
End If
'show the current active configuration
swModel.ShowConfiguration sConfName
'rebuild features that need a rebuild if any
boolstatus = swModel.EditRebuild3()
If boolstatus = False Then
MsgBox " Configuration rebuild error" & sConfName
End If
'force a rebuild whether needed or not, true=top level assm only, false all sub assm
DoNextPart = "N"
If DoNextPart = "Y" Then
boolstatus = swModel.ForceRebuild3(False)
If boolstatus = False Then
MsgBox " Configuration rebuild error" & sConfName
End If
End If
'after rebuilds, get the active configuration
Set swConfig = swModel.GetActiveConfiguration

'select the 3D spline to measure, get the length in meters, convert to inches, round to three places
boolstatus = swModel.Extension.SelectByID2(sSplineName, "EXTSKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, 0)
If boolstatus = False Then
MsgBox " 3D Spline not selected successfully " & sConfName
Exit Sub
End If
Set swSketchSegment = swSelMgr.GetSelectedObject6(1, -1)
length = swSketchSegment.GetLength
length = length * 39.37007874016
length = Round(length, 3)

'get a list of the custom properties for this configuration
vNameArr = swModel.GetCustomInfoNames2(sConfName)

'check the list for a custom property named sCustPropName, if it exists update the value
DoNextPart = "Y"
For Each vName In vNameArr
If vName = sCustPropName Then
swModel.CustomInfo2(sConfName, sCustPropName) = length
DoNextPart = "N"
End If
Next vName

'if no custom property with matching name was found then
If DoNextPart = "Y" Then
boolstatus = swModel.AddCustomInfo3(sConfName, sCustPropName, swCustomInfoNumber, length)
If boolstatus = False Then
MsgBox " Custom configuration property not added successfully " & sConfName
Exit Sub
End If
End If

'save the document after editing configuration?
DoNextPart = "Y"
If DoNextPart = "Y" Then
'set the save options
lSaveAsOptions = 0
'swSaveAsOptions_Silent
'swSaveAsOptions_Copy
'swSaveAsOptions_SaveReferenced \\Supports parts, assemblies, and drawings. This setting indicates to save all components (sub-assemblies and parts) in both assemblies and drawings. If a part has an external reference, then this setting indicates to save the external reference.
'swSaveAsOptions_AvoidRebuildOnSave
'swSaveAsOptions_UpdateInactiveViews \\ Not a valid option for PartDoc::SaveToFile2. This setting is only applicable for a drawing that has one or more sheets. This setting updates the views on inactive sheets.
'swSaveAsOptions_OverrideSaveEmodel \\Saves eDrawings-related information into a section of the file being saved. Specifying this setting overrides the Tools, Options, System Options, General, Save eDrawings data in SolidWorks document setting. Not a valid option for PartDoc::SaveToFile2.
'swSaveAsOptions_SaveEmodelData \\Not a valid option for PartDoc::SaveToFile2
'swSaveAsOptions_DetachedDrawing \\Not a valid option for PartDoc::SaveToFile2

'do you want to return errors found during save3 method?
DoNextPart = "Y"
If DoNextPart = "Y" Then
longerrors = 0
longwarnings = 0
Else
longerrors = Null
longwarnings = Null
End If

'save the document
'retval = ModelDoc2.Save3 ( Options, &Errors, &Warnings )
boolstatus = swModel.Save3(lSaveAsOptions, longerrors, longwarnings)

'check for a save error(s)?
'DoNextPart = "Y"
If DoNextPart = "Y" Then
If boolstatus = False Then
'get the error
MsgBox " Save Error: " & "Long Error = " & longerrors
End If
End If
'check for long warnings?
'DoNextPart = "Y"
If DoNextPart = "Y" Then
If longwarnings <> 0 Then
'get the error
MsgBox " Save Error: " & "Long Warnings = " & longwarnings
' 1 swFileSaveWarning_RebuildError
' 2 swFileSaveWarning_NeedsRebuild
' 4 swFileSaveWarning_ViewsNeedUpdate
' 8 swFileSaveWarning_AnimatorNeedToSolve
'16 swFileSaveWarning_AnimatorFeatureEdits
End If
End If
End If

Next i

MsgBox " Setting of 3D Spline length completed" & " Time = " & Timer - nStart & " s"

End Sub

• ###### Re: How to get spline length assigned to equation variable or custom property?

My code is

---

'-------------------------------------------------
'
' Preconditions:
' (1) Sketch is active.
' (2) Spline is selected.
'
' Postconditions: None
'
'--------------------------------------------------
Option Explicit

' Define two types
Type DoubleRec
dValue As Double
End Type
Type Long2Rec
iLower As Long
iUpper As Long
End Type
' Extract two integer values from a single double value
' by assigning a DoubleRec to the double value,
' copying the value to a Long2Rec, and
' extracting the integer values
Function ExtractFields(ByVal dValue As Double, _
iLower As Long, iUpper As Long)
Dim dr As DoubleRec
Dim i2r As Long2Rec
' Set the double value
dr.dValue = dValue
' Copy the values
LSet i2r = dr
' Extract the values
iLower = i2r.iLower
iUpper = i2r.iUpper
End Function
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swSketchSeg As SldWorks.SketchSegment
Dim swCurve As SldWorks.Curve
Dim nStartParam As Double
Dim nEndParam As Double
Dim bIsClosed As Boolean
Dim bIsPeriodic As Boolean
Dim vStart As Variant
Dim vEnd As Variant
Dim nDummy As Long
Dim nStartSuccess As Long
Dim nEndSuccess As Long
Dim i As Long
Dim bRet As Boolean
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
Set swSketchSeg = swSelMgr.GetSelectedObject5(1)
Set swCurve = swSketchSeg.GetCurve
''
bRet = swCurve.GetEndParams(nStartParam, nEndParam, bIsClosed, bIsPeriodic)
Debug.Assert bRet
vStart = swCurve.Evaluate(nStartParam)
vEnd = swCurve.Evaluate(nEndParam)
''
ExtractFields vStart(6), nStartSuccess, nDummy
ExtractFields vEnd(6), nEndSuccess, nDummy
Debug.Assert nStartSuccess
Debug.Assert nEndSuccess
''
Debug.Print "Length = " & swCurve.GetLength2(nStartParam, nEndParam) * 1000# & " mm"
'Debug.Print " Start Pt = (" & vStart(0) * 1000# & ", " & vStart(1) * 1000# & ", " & vStart(2) * 1000# & " mm"
'Debug.Print " tangent = (" & vStart(3) & ", " & vStart(4) & ", " & vStart(5) & ""
'Debug.Print " End Pt = (" & vEnd(0) * 1000# & ", " & vEnd(1) * 1000# & ", " & vEnd(2) * 1000# & " mm"
'Debug.Print " tangent = (" & vEnd(3) & ", " & vEnd(4) & ", " & vEnd(5) & ""
End Sub

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

'Get All Sketch Segments in Drawing Template Example (VB)
'This example shows how to get all of the sketch segments in a drawing template.

'----------------------------------
'
' Preconditions: Drawing document is open.
'
' Postconditions: All sketch segments in the drawing template are selected.
'
'-----------------------------------
Option Explicit
Public Enum swSketchSegments_e
swSketchLINE = 0
swSketchARC = 1
swSketchELLIPSE = 2
swSketchSPLINE = 3
swSketchTEXT = 4
swSketchPARABOLA = 5
End Enum
Sub main()
Dim swApp                       As SldWorks.SldWorks
Dim swModel                     As SldWorks.ModelDoc2
Dim swDraw                      As SldWorks.DrawingDoc
Dim swView                      As SldWorks.View
Dim swSketch                    As SldWorks.Sketch
Dim swSketchSeg                 As SldWorks.SketchSegment
Dim swSelMgr                    As SldWorks.SelectionMgr
Dim swSelData                   As SldWorks.SelectData
Dim vSketchSeg                  As Variant
Dim vSketchSegID                As Variant
Dim i                           As Long
Dim bRet                        As Boolean
Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
Set swSelData = swSelMgr.CreateSelectData
Set swDraw = swModel

swDraw.EditTemplate

' This is the drawing template
Set swSketch = swModel.GetActiveSketch2

Debug.Print "File = " & swModel.GetPathName

swModel.ClearSelection2 (True)

vSketchSeg = swSketch.GetSketchSegments
For i = 0 To UBound(vSketchSeg)
Set swSketchSeg = vSketchSeg(i)

vSketchSegID = swSketchSeg.GetID

Debug.Print "    SketchSegID(" & i & ") = [" & vSketchSegID(0) & ", " & vSketchSegID(1) & "]"
Debug.Print "      Type       = " & swSketchSeg.GetType
Debug.Print "      Length     = " & swSketchSeg.GetLength

bRet = swSketchSeg.Select4(True, swSelData)
Debug.Assert bRet
Next
End Sub

• ###### How to get spline length assigned to equation variable or custom property?

Hi John, I'm currently trying to form a 3D spline that goesthrough a number of points with defined x, y, z co-ords similar toyou setting a start point and an endpoint and set the 3D spline togo through points P1, P2, P3 etc. I was wondering if you used anequation to define the x, y, z co-ords in the 3D spline and if so,how did you define these co-ords for the point in the 3D spline?Technically, I'm just trying to find the syntax for defining the x,y, z co-ords for the points on the 3D spline, something similar to:"x@point1@spline@3Dsketch" =..... Any help on this would be muchappreciated.

• ###### How to get spline length assigned to equation variable or custom property?
Mark, I think there are 3 ways to solve your problem, hopefully oneof them will be what you are really trying for.

1.) Matt Lombard's website ( http://mysite.verizon.net/mjlombard/)has a macro called eqcurve.  It will take equations(polynomial and sinusoidals) and convert them to a spline.

2.) Create an approximate free-form spline where you want it. After it's creation, click on all the handles (points) and anX/Y parameter will show up in the feature manager.  You canthen modify each handle's X/Y coordinates.

3.) You can use Insert>Curve>Through XYZ Points... to inserta curve using specified coordinates.  You can also createpoints, then use the option just below that, to insert the curvethrough already specified points.

And if neither of those satisfy your needs, Matt Lombard's websitehas another macro that will import XYZ points as a point cloud. You can then take that data (imported from a CSV file) andthen "connect the dots" with a spline in a separate 3D/2DSketch.
• ###### How to get spline length assigned to equation variable or custom property?

Hi Charles,

Thanks forthe points above, I've tried these options and I have been usingimporting a point cloud (using "Scanto3D" in office permium) andthen joining a spline to the points. This is ok for a low numbersof points, however, I've now got more than a 100 points and thereare multiple groups than need to be updated every so often into thespline, which is time consuming. So I was thinking of using theequation folder and the syntax for a spline and defining the multipoints in this. I just cant find the equation syntax for a splineand its points something similar to:"x@point1@spline@3Dsketch" =.....

• ###### How to get spline length assigned to equation variable or custom property?
Unfortunately, I don't know of any way to define points withoutsetting up equations for each coordinate.  So, for 100's ofpoints it wouldn't be much of a time saver, but you would have todraw a dimension from the origin to each point, in the X, Y, and Zcoordinates.  Also, when defining 3D splines (2D as well, justnot as bad) with an abundance of control points (15+) once youstart constraining all of them, it will start to get very finickyif you want to change their values.
• ###### Re: How to get spline length assigned to equation variable or custom property?

I know this is kind of an old thread but I'm trying to get this macro to work in SolidWorks 2009 SP4.1 and can't seem to figure out how to get it to work.  Where do you name the spline at?  I know this is an old macro but was wondering if it still works with SolidWorks 2009.  I cannot seem to figure out where to rename the spline.  I have a 3DSketch1 with 2 lines 1.5"s long and a small little spline about 5"s connecting them.  If I click on the dimensions of the lines I get a box where I can fill in a "Primary Value" and I've used that before for design tables but can't seem to figure out where or how to name the spline.  When ever I run the macro I get "Configuration not activated successfully Default" followed by a "3D Spline not selected successfully Default"  If someone could point me in the right direction that would be greatly appreciated.

Thanks,

Jason

• ###### Re: How to get spline length assigned to equation variable or custom property?

There are two lines in the macro as shown below:

'set the name of your 3D Spline here
sSplineName = "Spline1@3DSketchPath"

You need to change the "Spline1@3DSketchPath" to whatever the name of that line is and the name of that sketch is. "Spline1@3DSketch1" would be the default for the first spline in the first 3DSketch. I don't believe it is possible to rename sketch segments, so you just need to rewrite that line in the macro to match the name of the spline as assigned by solidworks.

I'm not sure why the configuration would be giving you and error, but try the above and see if that fixes the problem.

• ###### Re: How to get spline length assigned to equation variable or custom property?

I also asked this question recently in another post (see here). Pro-E has this capability through "Evaluate Feature" (see here and here) to calculate the length of a curve. I would suspect this could be added to SW functionality as well.

• ###### Re: How to get spline length assigned to equation variable or custom property?

I know this post was a long time ago, but since it just came up for me in a google search... this has been available since version 2014 as a 'fixed length spline', and it can be referenced as a variable.

• ###### Re: How to get spline length assigned to equation variable or custom property?

Also, the path length dimension is now available. (just needs to be 2 or more elements).

Alternatively, you can match, "equal length" for a spline and a straight line.....so you could put your spline down, then put a straight line down and add a dimension to the length of the straight line.  Then make the spline and line equal length.

Then you can link your dimension either to a global variable or a custom property.

• ###### Re: How to get spline length assigned to equation variable or custom property?

Dan,

Do you know what version they added that ability?

• ###### Re: How to get spline length assigned to equation variable or custom property?

Path was in SW 2014 or 2015.

Equal length was 2016.