How to change the instance ID number in assemblies

In an assembly I have had to delete and reinsert parts, this causes the instance count to be incorrect. For example I have only two parts, but they are labelled 6 and 7. Is there any way of changing this without restarting the session?

SolidworksAssemblies
Answers 10
CM
CM 2015-03-18 Best Answer

What version are you running?

I'm still running 2012, and if you are referring to the number in the <>.  No, the only way to get them to "reset" is to remove all of them ( or up to where the numbers start to skip ) then CLOSE solidworks completely and then relaunch and re insert the parts.  This will force solidworks to "forget" that you had inserted those other components and start number the instance id where the last one left off at.

IT'S SUPER ANNOYING!!!!!  And has been brought MANY times before.  (I'm not sure if it has been changed in newer versions or not)

KL
KL 2015-03-18

The number is just that, a number used to identify a specific instance of a component.

It is not supposed to be used for an accurate count of the number of instances.

GS
GS 2015-03-18

I agree with Kelvin.  Ignore that number.  If you need a count of parts, that's what a BOM is for.

CM
CM 2015-03-18

When you want to start to use macros and the API to handle things having those instance counts go in order REALLY helps out.  So there are times where having them make sense is worth it.

KL
KL 2015-03-18

I would have thought having them not change when an instance is removed would be even more important when using macros and the API.

SF
SF 2015-03-19

I think what Chris is referring to is if you make an assembly and then run a macro, it's more elegant to have instant counts [1,2,3] than to have a work around with a clause saying:

<p>If instance doesn't exist</p><p>     i=i+1</p>

Luckily It's not an important issue for me, I just find it incredibly annoying. It's not worth the time to manually reset them by closing and reopening, nor is it worth wasting time writing a macro (which I assume must be possible). I just wanted to know if there was a simple workaround. Looks like I'm just going to have to live with it :L.

AC
AC 2015-05-13

I have run into this being an issue when using Macros. You may want to post this in the API section and see if anyone has a solution.

KR
KR 2015-05-13

In the API, when you get a component name the instance number is included, but when you change the name, it doesn't affect the instance number.

Keith

SolidWorks API Tutorials

BO
BO 2016-01-14

Hello,

you might have found this solution already but here is what I do now. While in the assembly in question, create a subassembly containing all the components you want to renumber (identical or diff. configurations of the same part) and then dissolve the subassembly again. If you leave SW open while doing this the instance IDs will start with the next highest number but will be consecutive. If you save the file first and close SW and then re-open and then dissolve the subassembly the instance IDs will start with 1... . Props go to danuberacer, member at CAD.de (post 05.Dez.2011, Subject: "Kennung in Baugruppe manuell ändern? "

Hope this helps,

Brendan

TG
TG 2018-09-12

I wrote a VBA function a few years ago that returns the full part name of a part in an assembly with the Solidworks instance ID number. 

You can use the  "WhichInstanceToGet" parameter to choose which "instance number" in the assembly to get the name for.

You can also use the "GetIterationNumberOnly" parameter to just return the Solidworks instance ID number instead of the part name.

Here are a couple of examples of using this function to get the part name in an assembly named "Assembly1":

(If I was wanting to get the part name in the assembly for the 1st part named "Lifting Lug")

Dim strSWPartName as String

strSWPartName = strPartNameWithSWIterationNumber("Lifting Lug-xxx@Assembly1")

And if I was wanting to get the part name in the assembly for the 3rd part named "Lifting Lug")

Dim strSWPartName as String

strSWPartName = strPartNameWithSWIterationNumber("Lifting Lug-xxx@Assembly1", , , , , 3)

The function returns the part name with the correct SW instance number in place of "xxx"

You can also pass in "yyy" and "zzz" placeholders with the strPartName parameter to get parts within sub-assemblies

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

Function strPartNameWithSWIterationNumber(strPartName As String, Optional swApp As SldWorks.SldWorks, Optional swModel As SldWorks.modelDoc2, Optional SelectingObjectInDrawingView As Boolean, Optional MaxNumberToLookThru As Integer = 10, Optional WhichInstanceToGet As Integer = 1, Optional GetNumberOnly As Boolean, Optional GetIterationNumberOnly As Boolean, Optional IterationNumberToStartAtForXXX As Integer = 1, Optional IterationNumberToStartAtForYYY As Integer = 1, Optional IterationNumberToStartAtForZZZ As Integer = 1) As String

'created 8/19/2015 by Tyler Greer

If swApp Is Nothing Then

    Set swApp = GetObject(, "SldWorks.Application")

    Set swModel = swApp.ActiveDoc

End If

If swModel Is Nothing Then

    Exit Function

End If

Dim SWIterationNumber As Integer

Dim SWIterationNumber2 As Integer

Dim SWIterationNumber3 As Integer

Dim boolstatus As Boolean

Dim swComp As Object ' SldWorks.Component2

Dim strPartName2 As String

Dim WhichInstanceLastFound As Integer

'WhichInstanceToGet    default is 1 - it will get the first one of this part

For SWIterationNumber = IterationNumberToStartAtForXXX To IIf(WhichInstanceToGet > MaxNumberToLookThru, WhichInstanceToGet, MaxNumberToLookThru)

    If InStr(1, strPartName, "yyy") > 0 Then

        For SWIterationNumber2 = IterationNumberToStartAtForYYY To MaxNumberToLookThru

            If InStr(1, strPartName, "zzz") > 0 Then

                For SWIterationNumber3 = IterationNumberToStartAtForZZZ To MaxNumberToLookThru

                    strPartName2 = Replace(Replace(Replace(strPartName, "yyy", CStr(SWIterationNumber2)), "xxx", CStr(SWIterationNumber)), "zzz", CStr(SWIterationNumber3))

               

                    boolstatus = swModel.Extension.SelectByID2(strPartName2, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)

               

                    If CBool(Abs(boolstatus)) = True Then

                        WhichInstanceLastFound = WhichInstanceLastFound + 1

                       

                        If WhichInstanceToGet = WhichInstanceLastFound Then

                            GoTo FoundPartName

                        End If

                    End If

                Next SWIterationNumber3

            Else

                strPartName2 = Replace(Replace(strPartName, "yyy", CStr(SWIterationNumber2)), "xxx", CStr(SWIterationNumber))

           

                boolstatus = swModel.Extension.SelectByID2(strPartName2, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)

           

                If CBool(Abs(boolstatus)) = True Then

                    WhichInstanceLastFound = WhichInstanceLastFound + 1

                   

                    If WhichInstanceToGet = WhichInstanceLastFound Then

                        GoTo FoundPartName

                    End If

                End If

            End If

        Next SWIterationNumber2

    Else

        strPartName2 = Replace(strPartName, "xxx", CStr(SWIterationNumber))

        boolstatus = swModel.Extension.SelectByID2(strPartName2, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)

    End If

       

    If CBool(Abs(boolstatus)) = True Then

'I added these lines 9/21/2015 - because if I am selecting an object in a drawing view, then it will select it no matter what iteration number it has

        WhichInstanceLastFound = WhichInstanceLastFound + 1

       

        If WhichInstanceToGet = WhichInstanceLastFound Then

            If SelectingObjectInDrawingView = True Then

                Set swComp = swModel.SelectionManager.GetSelectedObject6(1, -1)

                strPartName2 = swComp.Name

            End If

           

            GoTo FoundPartName

        Else

            strPartName2 = "" 'added 7/26/2016

        End If

    Else

        strPartName2 = ""

    End If

Next SWIterationNumber

FoundPartName:

If CBool(Abs(boolstatus)) = True Then

    If GetNumberOnly = True Then

        strPartNameWithSWIterationNumber = CStr(WhichInstanceLastFound)

    ElseIf GetIterationNumberOnly = True Then

        strPartNameWithSWIterationNumber = CStr(SWIterationNumber)

    Else

        strPartNameWithSWIterationNumber = strPartName2

    End If

End If

End Function