Various shape properties (Python)

Various shape properties (Python)
 1# The script assumes that the entities in the current block define one or more
 2# closed paths that can be used to define a shape. If there is just one closed path,
 3# then the shape will have SubShapeCount=1 and InternalCount=0. If there is more than one
 4# closed path and they are completely separate, then SubShapeCount will be greater than one.
 5# If some of the closed paths are completely within other paths, then these are treated
 6# as internals (voids within the shape) and InternalCount will be greater than zero.
 7
 8def DisplayShapeProperties( theShape ):
 9    ot = impact.gui.output_toolbox
10    ot.open()
11    ot.clear()
12    ot.add( "Shape properties: " )
13    conv = impact.system.convertor
14    ot.add( "   The area of the shape is: " + conv.area_as_string(theShape.area) )
15    ot.add( "   The shape consists of " + theShape.sub_shape_count + " sub-shapes" )
16
17    # Iterate through the sub-shapes and output their area
18    for count in range(0, theShape.sub_shape_count - 1 + 1):
19        subshape = None
20        try:
21            subshape = theShape.sub_shape( count )
22        except Exception as exc:
23            pass
24        impact.gui.output_toolbox.add(f"Failed to create object via the_shape.sub_shape(): {exc}")
25        ot.add( "      The area of sub-shape " + count + " is: " + conv.area_as_string( subShape.area ) )
26    ot.add( "   The shape has " + theShape.internal_count + " internal shapes (voids)" )
27    internalsShape = theShape.internals()
28    ot.add( "   The area of the internals (all the voids) is: " + conv.area_as_string(internalsShape.area) )
29
30    # Iterate through the internal shapes and output their area
31    for count in range(0, internalsShape.sub_shape_count - 1 + 1):
32        internalShape = None
33        try:
34            internalShape = internalsShape.sub_shape( count )
35        except Exception as exc:
36            pass
37        impact.gui.output_toolbox.add(f"Failed to create object via internals_shape.sub_shape(): {exc}")
38        ot.add( "      The area of internal (void) shape " + count + " is: " + conv.area_as_string( internalShape.area ) )
39    # Create another shape, the same as the original but without any voids
40    perimeterShape = theShape.remove_internals()
41    ot.add( "   The area of the shape without internals is: " + conv.area_as_string(perimeterShape.area) )
42
43    # Iterate through the sub-shapes and output their area
44    for count in range(0, perimeterShape.subshape_count - 1 + 1):
45        perimeterSubShape = None
46        try:
47            perimeterSubShape = perimeterShape.sub_shape( count )
48        except Exception as exc:
49            pass
50        impact.gui.output_toolbox.add(f"Failed to create object via perimeter_shape.sub_shape(): {exc}")
51        ot.add( "      The area of sub-shape " + count + " without internals is: " + conv.area_as_string( perimeterSubShape.area ) )
52
53if impact.active_drawing.isNone():
54    impact.gui.output_toolbox.add('Unable to continue: no active drawing')
55else:
56    # Create a shape using the entities from the active block
57    ShapeCreator = impact.creator.shape_creator()
58    ShapeCreator.entities = impact.active_block.entities
59    shape = None
60    try:
61        shape = ShapeCreator.perform( ipShapeCreationType.sctEntities )
62    except Exception as exc:
63        pass
64    impact.gui.output_toolbox.add(f"Failed to create object via shape_creator.perform(): {exc}")
65
66    if shape.sub_shape_count == 0:
67        impact.gui.output_toolbox.add('The entities do not seem to form a closed shape')
68    else:
69        DisplayShapeProperties( shape )