Total number of component as custom property...

User avatar
zwei
Posts: 700
Joined: Mon Mar 15, 2021 9:17 pm
Answers: 18
Location: Malaysia
x 185
x 598

Total number of component as custom property...

Unread post by zwei »

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...
by josh » 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.
Go to full post
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.
User avatar
Roasted By John
Posts: 366
Joined: Mon Mar 08, 2021 3:21 pm
Answers: 1
Location: Lebanon PA USA
x 268
x 583
Contact:

Re: Total number of component as custom property...

Unread post by Roasted By John »

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
User avatar
josh
Posts: 253
Joined: Thu Mar 11, 2021 1:05 pm
Answers: 11
x 19
x 444

Re: Total number of component as custom property...

Unread post by josh »

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.
User avatar
josh
Posts: 253
Joined: Thu Mar 11, 2021 1:05 pm
Answers: 11
x 19
x 444

Re: Total number of component as custom property...

Unread post by josh »

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".
User avatar
mattpeneguy
Posts: 1380
Joined: Tue Mar 09, 2021 11:14 am
Answers: 4
x 2487
x 1888

Re: Total number of component as custom property...

Unread post by mattpeneguy »

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.
User avatar
zwei
Posts: 700
Joined: Mon Mar 15, 2021 9:17 pm
Answers: 18
Location: Malaysia
x 185
x 598

Re: Total number of component as custom property...

Unread post by zwei »

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.
Amazing...
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.
User avatar
josh
Posts: 253
Joined: Thu Mar 11, 2021 1:05 pm
Answers: 11
x 19
x 444

Re: Total number of component as custom property...

Unread post by josh »

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.
User avatar
DanPihlaja
Posts: 743
Joined: Thu Mar 11, 2021 9:33 am
Answers: 24
Location: Traverse City, MI
x 738
x 893

Re: Total number of component as custom property...

Unread post by DanPihlaja »

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.
I did not know that you could just add API text like that to Custom Properties!! Wow!
-Dan Pihlaja
Solidworks 2022 SP4

2 Corinthians 13:14
User avatar
josh
Posts: 253
Joined: Thu Mar 11, 2021 1:05 pm
Answers: 11
x 19
x 444

Re: Total number of component as custom property...

Unread post by josh »

dpihlaja wrote: Fri Mar 26, 2021 11:41 am
I did not know that you could just add API text like that to Custom Properties!! Wow!
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.
User avatar
DanPihlaja
Posts: 743
Joined: Thu Mar 11, 2021 9:33 am
Answers: 24
Location: Traverse City, MI
x 738
x 893

Re: Total number of component as custom property...

Unread post by DanPihlaja »

josh wrote: Fri Mar 26, 2021 12:08 pm
dpihlaja wrote: Fri Mar 26, 2021 11:41 am
I did not know that you could just add API text like that to Custom Properties!! Wow!
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.
ummm.....wow.

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
User avatar
josh
Posts: 253
Joined: Thu Mar 11, 2021 1:05 pm
Answers: 11
x 19
x 444

Re: Total number of component as custom property...

Unread post by josh »

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.
User avatar
zwei
Posts: 700
Joined: Mon Mar 15, 2021 9:17 pm
Answers: 18
Location: Malaysia
x 185
x 598

Re: Total number of component as custom property...

Unread post by zwei »

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.
Amazing!
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.
Jacomuller
Posts: 24
Joined: Wed Mar 01, 2023 6:55 pm
Answers: 0
x 18
x 2

Re: Total number of component as custom property...

Unread post by Jacomuller »

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.
image.png
Not sure what that is about.
Post Reply