From 933558fcfc947d3325343d5c232cd3163e5f0afb Mon Sep 17 00:00:00 2001 From: Anthony Sokolowski Date: Wed, 25 Sep 2024 10:17:33 +1000 Subject: [PATCH 1/2] Added new section Iterating Over Compounds to Assembly documentation. --- docs/assemblies.rst | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/docs/assemblies.rst b/docs/assemblies.rst index 9d360cdf..eec7b56f 100644 --- a/docs/assemblies.rst +++ b/docs/assemblies.rst @@ -160,6 +160,28 @@ adds the following attributes to :class:`~topology.Shape`: .. _pack: +************************ +Iterating Over Compounds +************************ + +As Compounds are containers for shapes, Build123 can iterate over these as required. +Complex nested assemblies (compounds within compounds) do not need to be looped over with recursive functions. +In the example below, the variable total_volume holds the sum of all the volumes in each solid in an assembly. +Compare this to assembly3_volume which only results in the volume of the top level part. + +.. code:: python + + # [import] + from build123d import * + from ocp_vscode import * + + # Each assembly has a box and the previous assembly. + assembly1 = Compound(label='Assembly1', children=[Box(1, 1, 1),]) + assembly2 = Compound(label='Assembly2', children=[assembly1, Box(1, 1, 1)]) + assembly3 = Compound(label='Assembly3', children=[assembly2, Box(1, 1, 1)]) + total_volume = sum([part.volume for part in assembly3.solids()]) # 3 + assembly3_volume = assembly3.volume # 1 + ****** pack ****** From f1244a574c999bc9679f676f038411d5c8a798dd Mon Sep 17 00:00:00 2001 From: Anthony Date: Thu, 26 Sep 2024 13:53:38 +1000 Subject: [PATCH 2/2] Fixed project name and included generator expression rather than list comprehension. --- docs/assemblies.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/assemblies.rst b/docs/assemblies.rst index eec7b56f..8889c28a 100644 --- a/docs/assemblies.rst +++ b/docs/assemblies.rst @@ -164,7 +164,7 @@ adds the following attributes to :class:`~topology.Shape`: Iterating Over Compounds ************************ -As Compounds are containers for shapes, Build123 can iterate over these as required. +As Compounds are containers for shapes, build123d can iterate over these as required. Complex nested assemblies (compounds within compounds) do not need to be looped over with recursive functions. In the example below, the variable total_volume holds the sum of all the volumes in each solid in an assembly. Compare this to assembly3_volume which only results in the volume of the top level part. @@ -179,7 +179,7 @@ Compare this to assembly3_volume which only results in the volume of the top lev assembly1 = Compound(label='Assembly1', children=[Box(1, 1, 1),]) assembly2 = Compound(label='Assembly2', children=[assembly1, Box(1, 1, 1)]) assembly3 = Compound(label='Assembly3', children=[assembly2, Box(1, 1, 1)]) - total_volume = sum([part.volume for part in assembly3.solids()]) # 3 + total_volume = sum(part.volume for part in assembly3.solids()) # 3 assembly3_volume = assembly3.volume # 1 ******