Useful Functions

The following are all functions that you might find useful to copy and paste into a preamble Python rule in your rule sequence or DrawScript program. A preamble rule is a Python rule that executes once in order to define variables and functions that will be accessible across the entire program.

You can modify these functions however you would like in order to cater their behavior to your use case, as needed.

Output Geometry

By default, outputting geometry requires some extra details, and this helper function keeps you from having to manually perform the extra steps. The outputted geometry will be centered at the centroid of the initial design. This means a shapemachine.geometry.point.Point at (0, 0, 0) will be at the initial design’s centroid. In all likelihood, you will find this function most useful if you create geometry relative to other geometry in the design, instead of manually choosing positions.

output_shape(shape: shapemachine.geometry.shape.Shape, include_in_current_design: bool = True) None

Output a shapemachine.geometry.shape.Shape into the model. If include_in_current_design is True, the outputted geometry will also be included in shapemachine.core.engine.ShapeMachine.current_design, which allows it to be used in future queries performed by engine.

Parameters:
  • shape – The shapemachine.geometry.shape.Shape to output.

  • include_in_current_design – If True, shape will be usable for future queries by engine. If False, the outputted geometry will only be aesthetic. If it isn’t needed for future queries within the same program, setting this to False can speed up future queries.

Source Code

def output_shape(shape, include_in_current_design = True)
  communication_layer.output_shape(new_shape, engine.to_design)
  if include_in_current_design:
    engine.current_design += new_shape

Example Usage

import numpy as np
from shapemachine.geometry.point import Point
from shapemachine.geometry.shape import Shape

# Generate 10 random points within (-100, -100, -100) and (100, 100, 100)
points = np.random.rand(10, 3) * 200 - 100
# Flatten the points to 2D
points[:, -1] = 0

attr = make_attributes("Blue")

to_output = Shape(
  [],  # no lines
  [],  # no arcs
  [Point(location, attr) for location in points]
)
output_shape(to_output)

Replace Entire Design

If you’d like to output a new shapemachine.geometry.shape.Shape to replace the entirety of the current design, this function is for you.

replace_design(shape: shapemachine.geometry.shape.Shape) None

Replace the current design with a new shapemachine.geometry.shape.Shape. The newly outputted shape will be used for future queries.

Parameters:

shape – The shapemachine.geometry.shape.Shape to replace the current design with.

Source Code

def replace_design(new_shape)
  engine.current_design = new_shape
  communication_layer.replace_current_design(new_shape, engine.to_design)

Example Usage

import numpy as np
from shapemachine.geometry.point import Point
from shapemachine.geometry.shape import Shape

# Generate 10 random points within (-100, -100, -100) and (100, 100, 100)
points = np.random.rand(10, 3) * 200 - 100
# Flatten the points to 2D
points[:, -1] = 0

attr = make_attributes("Blue")

to_output = Shape(
  [],  # no lines
  [],  # no arcs
  [Point(location, attr) for location in points]
)
replace_design(to_output)