Total number of component as custom property...
Total number of component as custom property...
Is it possible to have custom property that capture the total component inserted in assembly (parametrically)?
Eg: inside the assembly file, there are 5 components, the custom property should show 5, when user insert or remove components, the custom property will update accordingly...
Eg: inside the assembly file, there are 5 components, the custom property should show 5, when user insert or remove components, the custom property will update accordingly...
Here is the code to paste into the "Code" custom property if you want only unsuppressed components:
1
Dim vComps As Variant
Dim Cnt As Long
Dim i As Long
vComps = Assembly.GetComponents(False)
For i = 0 To UBound(vComps)
If vComps(i).GetSuppression2 <> 0 Then
Cnt = Cnt + 1
End If
Next i
Assembly.CustomInfo2("", "NumberOfComponents") = Cnt
As I mentioned before, this code actually sets the "NumberOfComponents" custom property directly, so you don't need the step where you link this property to the global variable. For this to work, the "NumberOfComponents" custom property must already exist. It will not create it for you. Also, the global variable will no longer contain the actual number. It will always display a value of 1.
So... I guess I should just paste an updated list of steps:
1. Create two Text type custom properties. One called "Code" (or whatever you want), and one called "NumberOfComponents" (or again, whatever you want).
2. Give each property a value of 1.
3. Add a global variable called "NumComps". Link it to the custom property "Code". You should get an evaluated value of 1.
4. Go back to the Custom Properties. Paste the block of code above into the "Code" custom property.
5. Exit the Custom Properties window.
Number of unsuppressed components will be written to the custom property with every rebuild.
As far as how I figured it out.... Well... VBA commands in equations was already sort of documented (iif etc). I just wondered exactly how far you could take it. You can take it pretty far.
One feature that's asked for quite frequently is a custom property in each part that lists its qty in the assembly. Of course, parts don't "know" that they belong to an assembly, so that's not exactly straightforward. I wrote a block of code you could put in the assembly so that with every assembly rebuild, it would write a certain custom property to all the parts with the qty in the assembly.
Go to full post1
Dim vComps As Variant
Dim Cnt As Long
Dim i As Long
vComps = Assembly.GetComponents(False)
For i = 0 To UBound(vComps)
If vComps(i).GetSuppression2 <> 0 Then
Cnt = Cnt + 1
End If
Next i
Assembly.CustomInfo2("", "NumberOfComponents") = Cnt
As I mentioned before, this code actually sets the "NumberOfComponents" custom property directly, so you don't need the step where you link this property to the global variable. For this to work, the "NumberOfComponents" custom property must already exist. It will not create it for you. Also, the global variable will no longer contain the actual number. It will always display a value of 1.
So... I guess I should just paste an updated list of steps:
1. Create two Text type custom properties. One called "Code" (or whatever you want), and one called "NumberOfComponents" (or again, whatever you want).
2. Give each property a value of 1.
3. Add a global variable called "NumComps". Link it to the custom property "Code". You should get an evaluated value of 1.
4. Go back to the Custom Properties. Paste the block of code above into the "Code" custom property.
5. Exit the Custom Properties window.
Number of unsuppressed components will be written to the custom property with every rebuild.
As far as how I figured it out.... Well... VBA commands in equations was already sort of documented (iif etc). I just wondered exactly how far you could take it. You can take it pretty far.
One feature that's asked for quite frequently is a custom property in each part that lists its qty in the assembly. Of course, parts don't "know" that they belong to an assembly, so that's not exactly straightforward. I wrote a block of code you could put in the assembly so that with every assembly rebuild, it would write a certain custom property to all the parts with the qty in the assembly.
Far too many items in the world are designed, constructed and foisted upon us with no understanding-or even care-for how we will use them.
- Roasted By John
- Posts: 366
- Joined: Mon Mar 08, 2021 3:21 pm
- Location: Lebanon PA USA
- x 268
- x 583
- Contact:
Re: Total number of component as custom property...
Not sure where to find it on the old Forum, but I think there are macros out there that total your components in an assembly. Where do you want it shown? How do you want it shown?
www.martinsroastapig.com
Pig Roast Your Way
Pig Roast Your Way
Re: Total number of component as custom property...
It is possible. You have to bury API code in a custom property, link an equation to that property, then link another property to that equation.
Steps to achieve (Please follow closely):
1. Create two Text type custom properties. One called "Code" (or whatever you want), and one called "NumberOfComponents" (or again, whatever you want).
2. Give each property a value of 1.
3. Add a global variable called "NumComps". Link it to the custom property "Code". You should get an evaluated value of 1.
4. Go back to the Custom Properties. Change the value of "Code" to "Assembly.GetComponentCount(False)" (no quotes). Link "NumberOfComponents" to the Global Variable "NumComps".
5. Exit the Custom Properties window.
From this point on, the Global Variable "NumComps", and therefore the custom property "NumberOfComponents" will update with every rebuild.
Steps to achieve (Please follow closely):
1. Create two Text type custom properties. One called "Code" (or whatever you want), and one called "NumberOfComponents" (or again, whatever you want).
2. Give each property a value of 1.
3. Add a global variable called "NumComps". Link it to the custom property "Code". You should get an evaluated value of 1.
4. Go back to the Custom Properties. Change the value of "Code" to "Assembly.GetComponentCount(False)" (no quotes). Link "NumberOfComponents" to the Global Variable "NumComps".
5. Exit the Custom Properties window.
From this point on, the Global Variable "NumComps", and therefore the custom property "NumberOfComponents" will update with every rebuild.
Re: Total number of component as custom property...
A couple of additional notes... This will get the total number of components, including components of subassemblies. If you want top-level only, change "False" to "True".
- mattpeneguy
- Posts: 1386
- Joined: Tue Mar 09, 2021 11:14 am
- x 2489
- x 1899
Re: Total number of component as custom property...
Has this changed since they implemented equations in custom properties. I don't have 2021, but I believe it's in that release. It may make this a lot easier.
Re: Total number of component as custom property...
Amazing...josh wrote: ↑Fri Mar 26, 2021 8:51 am It is possible. You have to bury API code in a custom property, link an equation to that property, then link another property to that equation.
Steps to achieve (Please follow closely):
1. Create two Text type custom properties. One called "Code" (or whatever you want), and one called "NumberOfComponents" (or again, whatever you want).
2. Give each property a value of 1.
3. Add a global variable called "NumComps". Link it to the custom property "Code". You should get an evaluated value of 1.
4. Go back to the Custom Properties. Change the value of "Code" to "Assembly.GetComponentCount(False)" (no quotes). Link "NumberOfComponents" to the Global Variable "NumComps".
5. Exit the Custom Properties window.
From this point on, the Global Variable "NumComps", and therefore the custom property "NumberOfComponents" will update with every rebuild.
Never thought we can make it work like that...
However, GetComponentCount seem to return total component, regardless whether part is suppressed or not.
Not sure is it too much to ask... but is it possible to get only total of "active" component, excluding suppressed component?
Far too many items in the world are designed, constructed and foisted upon us with no understanding-or even care-for how we will use them.
Re: Total number of component as custom property...
It is possible, but SW is much less happy with it. You have to paste a whole multiline macro into the "Code" custom property that actually sets the NumberOfComponents property because you can't get the value you want in a single line of code.
- DanPihlaja
- Posts: 862
- Joined: Thu Mar 11, 2021 9:33 am
- Location: Traverse City, MI
- x 815
- x 993
Re: Total number of component as custom property...
I did not know that you could just add API text like that to Custom Properties!! Wow!josh wrote: ↑Fri Mar 26, 2021 8:51 am It is possible. You have to bury API code in a custom property, link an equation to that property, then link another property to that equation.
Steps to achieve (Please follow closely):
1. Create two Text type custom properties. One called "Code" (or whatever you want), and one called "NumberOfComponents" (or again, whatever you want).
2. Give each property a value of 1.
3. Add a global variable called "NumComps". Link it to the custom property "Code". You should get an evaluated value of 1.
4. Go back to the Custom Properties. Change the value of "Code" to "Assembly.GetComponentCount(False)" (no quotes). Link "NumberOfComponents" to the Global Variable "NumComps".
5. Exit the Custom Properties window.
From this point on, the Global Variable "NumComps", and therefore the custom property "NumberOfComponents" will update with every rebuild.
-Dan Pihlaja
Solidworks 2022 SP4
2 Corinthians 13:14
Solidworks 2022 SP4
2 Corinthians 13:14
Re: Total number of component as custom property...
Well... You're not actually adding API text to the custom properties... You're hiding API text from the equation editor interface (just the stuipid interface!) by burying it in custom properties. It still gets evaluated by the Equation.
All equations in SW are evaluated through the VBA engine. Several years ago, you could just type VBA into the Equations interface. I even did a whole presentation at SWW about it. Then, they changed the interface's syntax checking to reject all API stuff. It will not allow you to create an equation it thinks is invalid. Once you get it past the syntax checker, it all still evaluates just fine, the interface just rejects it while you're trying to create it. That's why the order of steps I gave is so important. You have to create the custom property with the value of 1. Then create the equation that links to the custom property. The interface sees the link, sees the value of 1, and accepts it. Once that has been done, you can go back and change the custom property to contain useful code. Because the interface has already syntax checked and approved our "dummy" value of 1, it doesn't check it anymore and sends it straight to the equation evaluator, which runs the line of code that's now in the custom property. If you try to put code in the custom property first, then create the equation, the syntax checker will reject it.
- DanPihlaja
- Posts: 862
- Joined: Thu Mar 11, 2021 9:33 am
- Location: Traverse City, MI
- x 815
- x 993
Re: Total number of component as custom property...
ummm.....wow.josh wrote: ↑Fri Mar 26, 2021 12:08 pmWell... You're not actually adding API text to the custom properties... You're hiding API text from the equation editor interface (just the stuipid interface!) by burying it in custom properties. It still gets evaluated by the Equation.
All equations in SW are evaluated through the VBA engine. Several years ago, you could just type VBA into the Equations interface. I even did a whole presentation at SWW about it. Then, they changed the interface's syntax checking to reject all API stuff. It will not allow you to create an equation it thinks is invalid. Once you get it past the syntax checker, it all still evaluates just fine, the interface just rejects it while you're trying to create it. That's why the order of steps I gave is so important. You have to create the custom property with the value of 1. Then create the equation that links to the custom property. The interface sees the link, sees the value of 1, and accepts it. Once that has been done, you can go back and change the custom property to contain useful code. Because the interface has already syntax checked and approved our "dummy" value of 1, it doesn't check it anymore and sends it straight to the equation evaluator, which runs the line of code that's now in the custom property. If you try to put code in the custom property first, then create the equation, the syntax checker will reject it.
Talk about a work around.
Thank you for sharing.....
And dare I ask.......how you found all this out?
-Dan Pihlaja
Solidworks 2022 SP4
2 Corinthians 13:14
Solidworks 2022 SP4
2 Corinthians 13:14
Re: Total number of component as custom property...
Here is the code to paste into the "Code" custom property if you want only unsuppressed components:
1
Dim vComps As Variant
Dim Cnt As Long
Dim i As Long
vComps = Assembly.GetComponents(False)
For i = 0 To UBound(vComps)
If vComps(i).GetSuppression2 <> 0 Then
Cnt = Cnt + 1
End If
Next i
Assembly.CustomInfo2("", "NumberOfComponents") = Cnt
As I mentioned before, this code actually sets the "NumberOfComponents" custom property directly, so you don't need the step where you link this property to the global variable. For this to work, the "NumberOfComponents" custom property must already exist. It will not create it for you. Also, the global variable will no longer contain the actual number. It will always display a value of 1.
So... I guess I should just paste an updated list of steps:
1. Create two Text type custom properties. One called "Code" (or whatever you want), and one called "NumberOfComponents" (or again, whatever you want).
2. Give each property a value of 1.
3. Add a global variable called "NumComps". Link it to the custom property "Code". You should get an evaluated value of 1.
4. Go back to the Custom Properties. Paste the block of code above into the "Code" custom property.
5. Exit the Custom Properties window.
Number of unsuppressed components will be written to the custom property with every rebuild.
As far as how I figured it out.... Well... VBA commands in equations was already sort of documented (iif etc). I just wondered exactly how far you could take it. You can take it pretty far.
One feature that's asked for quite frequently is a custom property in each part that lists its qty in the assembly. Of course, parts don't "know" that they belong to an assembly, so that's not exactly straightforward. I wrote a block of code you could put in the assembly so that with every assembly rebuild, it would write a certain custom property to all the parts with the qty in the assembly.
1
Dim vComps As Variant
Dim Cnt As Long
Dim i As Long
vComps = Assembly.GetComponents(False)
For i = 0 To UBound(vComps)
If vComps(i).GetSuppression2 <> 0 Then
Cnt = Cnt + 1
End If
Next i
Assembly.CustomInfo2("", "NumberOfComponents") = Cnt
As I mentioned before, this code actually sets the "NumberOfComponents" custom property directly, so you don't need the step where you link this property to the global variable. For this to work, the "NumberOfComponents" custom property must already exist. It will not create it for you. Also, the global variable will no longer contain the actual number. It will always display a value of 1.
So... I guess I should just paste an updated list of steps:
1. Create two Text type custom properties. One called "Code" (or whatever you want), and one called "NumberOfComponents" (or again, whatever you want).
2. Give each property a value of 1.
3. Add a global variable called "NumComps". Link it to the custom property "Code". You should get an evaluated value of 1.
4. Go back to the Custom Properties. Paste the block of code above into the "Code" custom property.
5. Exit the Custom Properties window.
Number of unsuppressed components will be written to the custom property with every rebuild.
As far as how I figured it out.... Well... VBA commands in equations was already sort of documented (iif etc). I just wondered exactly how far you could take it. You can take it pretty far.
One feature that's asked for quite frequently is a custom property in each part that lists its qty in the assembly. Of course, parts don't "know" that they belong to an assembly, so that's not exactly straightforward. I wrote a block of code you could put in the assembly so that with every assembly rebuild, it would write a certain custom property to all the parts with the qty in the assembly.
Re: Total number of component as custom property...
Amazing!josh wrote: ↑Fri Mar 26, 2021 12:40 pm Here is the code to paste into the "Code" custom property if you want only unsuppressed components:
1
Dim vComps As Variant
Dim Cnt As Long
Dim i As Long
vComps = Assembly.GetComponents(False)
For i = 0 To UBound(vComps)
If vComps(i).GetSuppression2 <> 0 Then
Cnt = Cnt + 1
End If
Next i
Assembly.CustomInfo2("", "NumberOfComponents") = Cnt
As I mentioned before, this code actually sets the "NumberOfComponents" custom property directly, so you don't need the step where you link this property to the global variable. For this to work, the "NumberOfComponents" custom property must already exist. It will not create it for you. Also, the global variable will no longer contain the actual number. It will always display a value of 1.
So... I guess I should just paste an updated list of steps:
1. Create two Text type custom properties. One called "Code" (or whatever you want), and one called "NumberOfComponents" (or again, whatever you want).
2. Give each property a value of 1.
3. Add a global variable called "NumComps". Link it to the custom property "Code". You should get an evaluated value of 1.
4. Go back to the Custom Properties. Paste the block of code above into the "Code" custom property.
5. Exit the Custom Properties window.
Number of unsuppressed components will be written to the custom property with every rebuild.
As far as how I figured it out.... Well... VBA commands in equations was already sort of documented (iif etc). I just wondered exactly how far you could take it. You can take it pretty far.
One feature that's asked for quite frequently is a custom property in each part that lists its qty in the assembly. Of course, parts don't "know" that they belong to an assembly, so that's not exactly straightforward. I wrote a block of code you could put in the assembly so that with every assembly rebuild, it would write a certain custom property to all the parts with the qty in the assembly.
Thanks alot..
Far too many items in the world are designed, constructed and foisted upon us with no understanding-or even care-for how we will use them.
-
- Posts: 51
- Joined: Wed Mar 01, 2023 6:55 pm
- x 36
- x 5
Re: Total number of component as custom property...
I have added two lines of to skip sub-assemblies as I am only interested in individual parts.
1
Dim vComps As Variant
Dim Cnt As Long
Dim i As Long
vComps = Assembly.GetComponents(False)
For i = 0 To UBound(vComps)
If vComps(i).GetSuppression2 <> 0 Then
If vComps(i).GetModelDoc2.GetType = swDocPART Then
Cnt = Cnt + 1
End If
End If
Next i
Assembly.CustomInfo2("", "NumberOfComponents") = Cnt
Apart from it is all good. I did notice that the "Global Variable" throws up an error. Not sure what that is about.
1
Dim vComps As Variant
Dim Cnt As Long
Dim i As Long
vComps = Assembly.GetComponents(False)
For i = 0 To UBound(vComps)
If vComps(i).GetSuppression2 <> 0 Then
If vComps(i).GetModelDoc2.GetType = swDocPART Then
Cnt = Cnt + 1
End If
End If
Next i
Assembly.CustomInfo2("", "NumberOfComponents") = Cnt
Apart from it is all good. I did notice that the "Global Variable" throws up an error. Not sure what that is about.