Page 1 of 1
Total number of component as custom property...
Posted: Fri Mar 26, 2021 1:51 am
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...
Re: Total number of component as custom property...
Posted: Fri Mar 26, 2021 6:41 am
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?
Re: Total number of component as custom property...
Posted: Fri Mar 26, 2021 8:51 am
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.
Re: Total number of component as custom property...
Posted: Fri Mar 26, 2021 9:04 am
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".
Re: Total number of component as custom property...
Posted: Fri Mar 26, 2021 9:37 am
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.
Re: Total number of component as custom property...
Posted: Fri Mar 26, 2021 10:23 am
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?
Re: Total number of component as custom property...
Posted: Fri Mar 26, 2021 11:36 am
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.
Re: Total number of component as custom property...
Posted: Fri Mar 26, 2021 11:41 am
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!
Re: Total number of component as custom property...
Posted: Fri Mar 26, 2021 12:08 pm
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.
Re: Total number of component as custom property...
Posted: Fri Mar 26, 2021 12:27 pm
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?
Re: Total number of component as custom property...
Posted: Fri Mar 26, 2021 12:40 pm
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.
Re: Total number of component as custom property...
Posted: Sat Mar 27, 2021 3:32 am
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..
Re: Total number of component as custom property...
Posted: Sun Oct 15, 2023 4:15 pm
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.
Not sure what that is about.