Utils functions

Utility functions provides useful functions that are used by both the Shape() class and the Reactor class. They can also be used directly.

utils

class EdgeLengthSelector(length, tolerance=0.1)

Bases: cadquery.selectors.Selector

A custom CadQuery selector the selects edges based on their length with a tolerance. The following useage example will fillet the inner edge of a rotated triangular shape. paramak.RotateStraightShape(points=[(1,1),(2,1), (2,2)]).solid.edges(paramak.EdgeLengthSelector(6.28)).fillet(0.1)

Parameters
  • length (float) – The length of the edge to select.

  • tolerance (float, optional) – The allowable tolerance of the length (+/-) while still being selected by the custom selector.

filter(object_list)

Loops through all the edges in the object checking if the edge meets the custom selector requirements or not.

Parameters

object_list (cadquery) – The object to filter the edges from.

Returns

The edge that match the selector length

within the specified tolerance.

Return type

object_list (cadquery)

class FaceAreaSelector(area, tolerance=0.1)

Bases: cadquery.selectors.Selector

A custom CadQuery selector the selects faces based on their area with a tolerance. The following useage example will fillet the faces of an extrude shape with an area of 0.5. paramak.ExtrudeStraightShape(points=[(1,1), (2,1), (2,2)], distance=5).solid.faces(FaceAreaSelector(0.5)).fillet(0.1)

Parameters
  • area (float) – The area of the surface to select.

  • tolerance (float, optional) – The allowable tolerance of the length (+/-) while still being selected by the custom selector.

filter(object_list)

Loops through all the faces in the object checking if the face meets the custom selector requirements or not.

Parameters

object_list (cadquery) – The object to filter the faces from.

Returns

The face that match the selector area within

the specified tolerance.

Return type

object_list (cadquery)

add_thickness(x, y, thickness, dy_dx=None)

Computes outer curve points based on thickness

Parameters
  • x (list) – list of floats containing x values

  • y (list) – list of floats containing y values

  • thickness (float) – thickness of the magnet

  • dy_dx (list) – list of floats containing the first order derivatives

Returns

R and Z lists for outer curve points

Return type

Tuple[list, list]

calculate_wedge_cut(self)

Calculates a wedge cut with the given rotation_angle

coefficients_of_line_from_points(point_a, point_b)

Computes the m and c coefficients of the equation (y=mx+c) for a straight line from two points.

Parameters
  • point_a – point 1 coordinates

  • point_b – point 2 coordinates

Returns

m coefficient and c coefficient

Return type

Tuple[float, float]

convert_circle_to_spline(p_0, p_1, p_2, tolerance=0.1)

Converts three points on the edge of a circle into a series of points on the edge of the circle. This is done by creating a circle edge from the the points provided (p_0, p_1, p_2), facets the circle with the provided tolerance to extracts the points on the faceted edge and returns them.

Parameters
  • p_0 – coordinates of the first point

  • p_1 – coordinates of the second point

  • p_2 – coordinates of the third point

  • tolerance – the precision of the faceting.

Returns

The new points

Return type

List[Tuple[float, float, str]]

cut_solid(solid, cutter)

Performs a boolean cut of a solid with another solid or iterable of solids.

Parameters
  • Shape (cutter) – The Shape that you want to cut from

  • Shape – The Shape(s) that you want to be the cutting object

Returns

The original shape cut with the cutter shape(s)

Return type

Shape

diff_between_angles(angle_a, angle_b)

Calculates the difference between two angles angle_a and angle_b

Parameters
  • angle_a (float) – angle in degree

  • angle_b (float) – angle in degree

Returns

difference between the two angles in degree.

Return type

float

Return type

float

distance_between_two_points(point_a, point_b)

Computes the distance between two points.

Parameters
  • point_a (float, float) – X, Y coordinates of the first point

  • point_b (float, float) – X, Y coordinates of the second point

Returns

distance between A and B

Return type

float

Return type

float

export_wire_to_html(wires, filename=None, view_plane='RZ', facet_splines=True, facet_circles=True, tolerance=0.001, title=None, mode='markers+lines')

Creates a html graph representation of the points within the wires. Edges of certain types (spines and circles) can optionally be faceted. If filename provided doesn’t end with .html then .html will be added. Viewed from the XZ plane

Parameters
  • wires (CadQuery.Wire) – the wire (edge) or list of wires to plot points from and to optionally facet.

  • filename – the filename used to save the html graph. If None then no html file will saved but a ploty figure will still be returned. Defaults to None.

  • view_plane – The axis to view the points and faceted edges from. The options are ‘XZ’, ‘XY’, ‘YZ’, ‘YX’, ‘ZY’, ‘ZX’, ‘RZ’ and ‘XYZ’. Defaults to ‘RZ’

  • facet_splines – If True then spline edges will be faceted. Defaults to True.

  • facet_circles – If True then circle edges will be faceted. Defaults to True.

  • tolerance – faceting toleranceto use when faceting cirles and splines. Defaults to 1e-3.

  • title – the title of the plotly plot.

  • mode – the plotly trace mode to use when plotting the data. Options include ‘markers+lines’, ‘markers’, ‘lines’. Defaults to ‘lines’.

Returns

figure object

Return type

plotly.Figure()

extend(point_a, point_b, L)

Creates a point C in (ab) direction so that |aC| = L

Parameters
  • point_a (float, float) – X, Y coordinates of the first point

  • point_b (float, float) – X, Y coordinates of the second point

  • L (float) – distance AC

Returns

point C coordinates

Return type

float, float

Return type

Tuple[float, float]

extract_points_from_edges(edges, view_plane='XZ')

Extracts points (coordinates) from a CadQuery Edge, optionally projects the points to a plane and returns the points.

Parameters
  • edges (CadQuery.Wires) – The edges to extract points (coordinates from).

  • view_plane – The axis to view the points and faceted edges from. The options are ‘XZ’, ‘XY’, ‘YZ’, ‘YX’, ‘ZY’, ‘ZX’, ‘RZ’ and ‘XYZ’. Defaults to ‘RZ’.

Returns

A list of tuples with float entries for every point

Return type

List of Tuples

Return type

Union[Tuple[float, float, float], Tuple[float, float]]

facet_wire(wire, facet_splines=True, facet_circles=True, tolerance=0.001)

Converts specified curved edge types from a wire into a series of straight lines (facetets) with the provided tol (tolerance).

Parameters
  • wire (cadquery.Wire) – The CadQuery wire to select edge from which will be redraw as a series of straight lines (facet).

  • facet_splines – If True then spline edges will be faceted. Defaults to True.

  • facet_splines – If True then circle edges will be faceted.Defaults to True.

  • tolerance – faceting toleranceto use when faceting cirles and splines. Defaults to 1e-3.

Returns

cadquery.Wire

find_center_point_of_circle(point_a, point_b, point_3)

Calculates the center of a circle passing through 3 points. :param point_a: point 1 coordinates :param point_b: point 2 coordinates :param point_3: point 3 coordinates

Returns

center of the circle coordinates or None if 3 points on a line are input and the radius

Return type

Optional[Tuple[float, float]]

find_radius_of_circle(center_point, edge_point)

Calculates the radius of a circle.

Parameters
  • center_point – x, y coordinates of the center of te circle

  • edge_point – x, y coordinates of a point on the edge of the circle

Returns

the radius of the circle

Return type

float

get_hash(shape, ignored_keys=None)

Computes a unique hash value for the shape.

Parameters
  • shape (list) – The paramak.Shape object to find the hash value for.

  • ignored_keys (list, optional) – list of shape.__dict__ keys to ignore when creating the hash.

Returns

R and Z lists for outer curve points

Return type

(list, list)

Return type

str

intersect_solid(solid, intersecter)

Performs a boolean intersection of a solid with another solid or iterable of solids. :param solid Shape: The Shape that you want to intersect :param intersecter Shape: The Shape(s) that you want to be the intersecting object

Returns

The original shape cut with the intersecter shape(s)

Return type

Shape

load_stp_file(filename, scale_factor=1.0)

Loads a stp file and makes the 3D solid and wires available for use.

Parameters
  • filename – the filename used to save the html graph.

  • scale_factor – a scaling factor to apply to the geometry that can be used to increase the size or decrease the size of the geometry. Useful when converting the geometry to cm for use in neutronics simulations.

Returns

solid and wires belonging to the object

Return type

CadQuery.solid, CadQuery.Wires

patch_workplane()

Going from CadQuery 2.1 to 2.2, the ‘distance’ arg to extrude was renamed ‘until’. This patch ensures that either version works fine using ‘until’.

plotly_trace(points, mode='markers+lines', name=None, color=None)

Creates a plotly trace representation of the points of the Shape object. This method is intended for internal use by Shape.export_html.

Parameters
  • points – A list of tuples containing the X, Z points of to add to the trace.

  • mode – The mode to use for the Plotly.Scatter graph. Options include “markers”, “lines” and “markers+lines”. Defaults to “markers+lines”

  • name – The name to use in the graph legend color

Returns

trace object

Return type

plotly trace

Return type

Union[Scatter, Scatter3d]

rotate(origin, point, angle)

Rotate a point counterclockwise by a given angle around a given origin. The angle should be given in radians.

Parameters
  • origin (float, float) – coordinates of origin point

  • point (float, float) – coordinates of point to be rotated

  • angle (float) – rotation angle in radians (counterclockwise)

Returns

rotated point coordinates.

Return type

float, float

transform_curve(edge, tolerance=0.001)

Converts a curved edge into a series of straight lines (facetets) with the provided tolerance.

Parameters
  • edge (cadquery.Wire) – The CadQuery wire to redraw as a series of straight lines (facet)

  • tolerance – faceting toleranceto use when faceting cirles and splines. Defaults to 1e-3.

Returns

cadquery.Wire

union_solid(solid, joiner)

Performs a boolean union of a solid with another solid or iterable of solids

Parameters
  • solid (Shape) – The Shape that you want to union from

  • joiner (Shape) – The Shape(s) that you want to form the union with the solid

Returns

The original shape union with the joiner shape(s)

Return type

Shape