Change Linear Pattern spacing

Dear Solidworks API Experts,

I have a component, whose holes are defined via Linear Pattern.
I would like to control the spacing between those holes, via Solidworks API.

Is it possible to get a VBA/VB.NET/C# code, which changes the spacing of the Linear Pattern in an assembly file (.SLDASM)?

 

The problem I have, is that I can't select the Linear Pattern, inside the Component.
I tried recording a macro, and the macro shows the following:

boolstatus = Part.Extension.SelectByID2("D3@LPattern1@Part1^holes with bolts2-1@HB2", "DIMENSION", 0.233189692745758, -4.63590286780184E-02, -0.203375327910249, False, 0, Nothing, 0)

When I try to replicate it to C#, it didn't work. The "LPattern1" is not selected.


Then I found a C# example:
https://help.solidworks.com/2023/english/api/sldworksapi/Get_Linear_Pattern_Feature_Data_Example_CSharp.htm
It selects the Linear Pattern via:

status = swModelDocExt.SelectByID2("LPattern1", "BODYFEATURE", 0, 0, 0, false, 0, null, 0);

This didn't work as well. It didn't select the "LPattern1".

Do I need to select the Linear Pattern - in order to control the spacing between those holes (via API)?

 

I would welcome any kind of help. As I am desperate at the moment.
Thank you in advance for any input.
 

Kind regards,
Didier

Answers 5
TS
TS 2025-06-19 Best Answer

Hi, @DB san.

I think you can change the spacing by selecting the LPattern1 feature and changing the parameters of the linear pattern as shown below.


Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssembly As SldWorks.AssemblyDoc
Dim swExtension As SldWorks.ModelDocExtension
Dim swSelMgr As SldWorks.SelectionMgr
Dim swComp As SldWorks.Component2
Dim swFeature As SldWorks.Feature
Dim swLinearPatternFeatureData As SldWorks.LinearPatternFeatureData
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swAssembly = swModel
Set swExtension = swModel.Extension
Set swSelMgr = swModel.SelectionManager
boolstatus = swExtension.SelectByID2("LPattern1@Part1^holes with bolts2-1@holes with bolts2", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)
Set swComp = swSelMgr.GetSelectedObjectsComponent4(1, -1)
Set swFeature = swSelMgr.GetSelectedObject6(1, -1)
Set swLinearPatternFeatureData = swFeature.GetDefinition
boolstatus = swLinearPatternFeatureData.AccessSelections(swModel, swComp)
swLinearPatternFeatureData.D1Spacing = 0.005
boolstatus = swFeature.ModifyDefinition(swLinearPatternFeatureData, swModel, swComp)
swAssembly.EditAssembly

End Sub
 

 

If you select a dimension and want to change the spacing, change the dimension value as shown below.

 

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swExtension As SldWorks.ModelDocExtension
Dim swSelMgr As SldWorks.SelectionMgr
Dim swDispDim As SldWorks.DisplayDimension
Dim swDimension As SldWorks.Dimension
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swExtension = swModel.Extension
Set swSelMgr = swModel.SelectionManager
boolstatus = swExtension.SelectByID2("D3@LPattern1@Part1^holes with bolts2-1@holes with bolts2", "DIMENSION", 0, 0, 0, False, 0, Nothing, 0)
Set swDispDim = swSelMgr.GetSelectedObject6(1, -1)
Set swDimension = swDispDim.GetDimension2(0)
longstatus = swDimension.SetSystemValue3(0.01, swSetValueInConfiguration_e.swSetValue_InThisConfiguration, Nothing)
swModel.EditRebuild3

End Sub
 

DB
DB 2025-06-19

Hi @TS ,
I tried the first example you posted, and it worked without issues!
I have no doubt your second example would work too.
Thank you very much for the help!!

By the way, I am always puzzled why Solidworks API requires so much information, just to select a feature.
For example, we have to define, even the name of the currently opened assembly file ("holes with bolts2"):

boolstatus = swExtension.SelectByID2("LPattern1@Part1^holes with bolts2-1@holes with bolts2", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)

JS
JS 2025-06-29

You do not need to use full names, just create a loop for components and an inner loop for features to get the job done.
On a side note, you only need to use a boolean if you need to debug or if you need it further in your code.

This macro works in assemblies and in parts.

'by John R. Sitek
'28-JUN-2025
'This macro loops features to edit linear pattern spacing in parts and assemblies

Sub Main()

Dim swApplication As SldWorks.SldWorks
Dim swAssembly As SldWorks.AssemblyDoc
Dim swModel As SldWorks.ModelDoc2
Dim swActivateDocErrors As swActivateDocError_e
Dim swLinearPatternFeatureData As SldWorks.LinearPatternFeatureData
Dim vbPartModel As Boolean
Dim vbComponents As Variant
Dim vbComponent As Variant
Dim vbFeatures As Variant
Dim vbFeature As Variant

Set swApplication = Application.SldWorks
Set swModel = swApplication.ActiveDoc

'If model is an assembly
If swModel.GetType = swDocASSEMBLY Then
   vbPartModel = False
   Set swAssembly = swApplication.ActiveDoc
ElseIf swModel.GetType = swDocPART Then
   vbPartModel = True
   GoTo PartModel
ElseIf swModel.GetType = swDocDRAWING Then
   MsgBox "This macro is not for drawings", vbOKOnly, "Not For Drawings"
   Exit Sub
End If

'Loop assembly
vbComponents = swAssembly.GetComponents(False)
For Each vbComponent In vbComponents
   'Skip assembly
   If UCase(vbComponent.GetPathName) Like "*.SLDASM" Then
   'Save part as STL
   ElseIf UCase(vbComponent.GetPathName) Like "*.SLDPRT" Then
       swApplication.ActivateDoc3 vbComponent.GetModelDoc2.GetTitle, False, swDontRebuildActiveDoc, swActivateDocErrors
       Set swModel = vbComponent.GetModelDoc2
PartModel:
       'Loop features
       vbFeatures = swModel.FeatureManager.GetFeatures(False)
       For Each vbFeature In vbFeatures
           If vbFeature.GetTypeName2 = "LPattern" Then
               Set swLinearPatternFeatureData = vbFeature.GetDefinition
               swLinearPatternFeatureData.D1Spacing = InputBox("Enter new spacing in mm: ", "Pattern Spacing", swLinearPatternFeatureData.D1Spacing * 1000) / 1000
               vbFeature.ModifyDefinition swLinearPatternFeatureData, swModel, Nothing
           End If
       Next vbFeature
       If vbPartModel = True Then
           Exit Sub
       End If
       swApplication.CloseDoc vbComponent.GetModelDoc2.GetTitle
   End If
Next vbComponent

End Sub

DB
DB 2025-06-29

Hi @J ,
Thank you very much for the example and the video!
I was totally ignorant that the line:
vbFeature.ModifyDefinition swLinearPatternFeatureData, swModel, Nothing

would work, even when feature component is set to "Nothing" (last input in the line above).

DB
DB 2025-07-03

Thank you very much for the help and your knowledge, both of you @TS and @J !!