Expression to count nos. of item in List

Need help, or want to share a macro? Post here!
Post Reply
paullee
Posts: 3809
Joined: Wed May 04, 2016 3:58 pm

Expression to count nos. of item in List

Post by paullee »

Just testing to reference to a PlacementList - <<VoxelPart>>.pList

And also need to find the number of item of the PlacementList, below not working:-
- Count(<<VoxelPart>>.pList) : return 0


Any idea ? Thanks :)

Screenshot from 2021-09-18 23-41-25.png
Screenshot from 2021-09-18 23-41-25.png (21.09 KiB) Viewed 718 times
Screenshot from 2021-09-19 00-04-19.png
Screenshot from 2021-09-19 00-04-19.png (9.55 KiB) Viewed 718 times
chrisb
Posts: 37826
Joined: Tue Mar 17, 2015 9:14 am

Re: Expression to count nos. of item in List

Post by chrisb »

Not all functions are supported by expressions. I couldn't find "count" on the Expressions page, but it could be hidden in one of the collapsed paragragraphs.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
paullee
Posts: 3809
Joined: Wed May 04, 2016 3:58 pm

Re: Expression to count nos. of item in List

Post by paullee »

chrisb wrote: Sat Sep 18, 2021 8:30 pm Not all functions are supported by expressions. I couldn't find "count" on the Expressions page, but it could be hidden in one of the collapsed paragragraphs.
Thanks @chrisb.

It is hidden in the Statistics section and need to click the 'Expand' to read it :)

But it seem count does not work on a list / PropertyList :-

- count(list(1,2,3)) = 0
- count(1,2,3) = 3
- count(LinkToPropertyList) = 0

See if anyone has an idea if something works to count the numbers of item in a List in Expression.

Thanks !
TheMarkster
Posts: 2715
Joined: Thu Apr 05, 2018 1:53 am

Re: Expression to count nos. of item in List

Post by TheMarkster »

As a workaround you could make a feature python object with this ability.

Code: Select all

import FreeCAD,FreeCADGui
class Counter:
    def __init__(self,obj):
        obj.addProperty("App::PropertyString","PropertyToCount")
        obj.addProperty("App::PropertyLink","ObjectWithPropertyToCount")
        obj.addProperty("App::PropertyInteger","Count")
        obj.Proxy = self
    def execute(self,fp):
        if fp.ObjectWithPropertyToCount:   
            if fp.PropertyToCount:
                fp.Count = len(getattr(fp.ObjectWithPropertyToCount,fp.PropertyToCount))
counter = FreeCAD.ActiveDocument.addObject("App::FeaturePython","counter")
Counter(counter)
sel = FreeCADGui.Selection.getSelection()
if len(sel) == 1:
    counter.ObjectWithPropertyToCount = sel[0]
    if hasattr(sel[0],"pList"):
        counter.PropertyToCount = "pList"
FreeCAD.ActiveDocument.recompute()
As written the object will not survive saving to a document and reloading the document. It will be there, but the Count will not be updated. For this you need to place the class definition in a separate file with the .py extension and import that file. The following should work: (untested)

CounterClass.py

Code: Select all

class Counter:
    def __init__(self,obj):
        obj.addProperty("App::PropertyString","PropertyToCount")
        obj.addProperty("App::PropertyLink","ObjectWithPropertyToCount")
        obj.addProperty("App::PropertyInteger","Count")
        obj.Proxy = self
    def execute(self,fp):
        if fp.ObjectWithPropertyToCount:   
            if fp.PropertyToCount:
                fp.Count = len(getattr(fp.ObjectWithPropertyToCount,fp.PropertyToCount))
counter.FCMacro

Code: Select all

import FreeCAD,FreeCADGui
import CounterClass as CC

counter = FreeCAD.ActiveDocument.addObject("App::FeaturePython","counter")
CC.Counter(counter)
sel = FreeCADGui.Selection.getSelection()
if len(sel) == 1:
    counter.ObjectWithPropertyToCount = sel[0]
    if hasattr(sel[0],"pList"):
        counter.PropertyToCount = "pList"
FreeCAD.ActiveDocument.recompute()
Edit: Usage: select the voxel object with the pList placement list property and run the macro. Else you need to edit the ObjectWithPropertyToCount link so it points to the voxel object after the counter object is created.
My FreeCAD video series on youtube: https://www.youtube.com/c/mwganson
paullee
Posts: 3809
Joined: Wed May 04, 2016 3:58 pm

Re: Expression to count nos. of item in List

Post by paullee »

TheMarkster wrote: Sun Sep 19, 2021 3:57 pm As a workaround you could make a feature python object with this ability.
Many thanks for the codes @TheMarkster :D

Indeed if there is no support in expression to count a list, count it in a FeaturePython object is probably best workflow. I integrate that in the original object 'VoxelPart'.
Post Reply