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
Expression to count nos. of item in List
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
Re: Expression to count nos. of item in List
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.
Re: Expression to count nos. of item in List
Thanks @chrisb.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.
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 !
-
- Veteran
- Posts: 5505
- Joined: Thu Apr 05, 2018 1:53 am
Re: Expression to count nos. of item in List
As a workaround you could make a feature python object with this ability.
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
counter.FCMacro
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.
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()
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))
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()
Re: Expression to count nos. of item in List
Many thanks for the codes @TheMarksterTheMarkster wrote: ↑Sun Sep 19, 2021 3:57 pm As a workaround you could make a feature python object with this ability.
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'.