Documentation

This commit is contained in:
Elod Csirmaz 2024-03-15 12:00:13 +00:00
parent 593581aee2
commit bce3d3cbc0
3 changed files with 32 additions and 11 deletions

View file

@ -13,10 +13,10 @@ It also contains convenience functions to define a wider range of primitives, as
## Example ## Example
``` ```
Cube([1, 1, 2]).move([0, 0, 1]) Cube([1, 1, 2]).move([0, 0, 1]).render()
``` ```
becomes returns
``` ```
@ -25,11 +25,29 @@ translate(v=[0, 0, 1]) { cube(size=[1, 1, 2], center=false); }
## Notable convenience functions ## Notable convenience functions
- Usual computational geometry functions on the `Point` class that work in an arbitrary number of dimensions. Overloads for algebraic operators Usual computational geometry functions on the `Point` class that work in an arbitrary number of dimensions. Overloads for algebraic operators.
- `Cylinder.from_ends` constructs a cylinder between two given points in space ```
distance = (Point((0, 0, 1)) - Point((1, 0, 1))).length()
angle_between = Point((0, 0, 1) * 2).angle(Point((1, 0, 1)))
```
- `Polyhedron.tube` creates a tube-like or toroid polyhedron from a 2D array of points `Cylinder.from_ends()` constructs a cylinder between two given points in space
- `PathTube` creates a tube-like or toroid polyhedron from an arbitrary path ```
openscad_code = Cylinder.from_ends(radius=2, p1=(0, 0, 1), p2=(1, 0, 2)).render()
```
`Polyhedron.tube()` creates a tube-like or toroid polyhedron from a 2D array of points
`PathTube` creates a tube-like or toroid polyhedron from an arbitrary path
```
PathTube(
points=[(0,0,0), (0,0,1), (1,0,2), (1,1,0), (0,.5,0)],
radius=.2,
fn=4
)
```
![PathTube example](https://raw.github.com/csirmaz/openscad-py/master/images/pathtube.png)

BIN
images/pathtube.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

View file

@ -9,14 +9,14 @@ NP_TYPE = np.float_
class Point: class Point:
"""Represents a point of vector in arbitrary dimensions""" """Represents a point or vector in arbitrary dimensions"""
def __init__(self, coords): def __init__(self, coords):
self.c = np.array(coords, dtype=NP_TYPE) self.c = np.array(coords, dtype=NP_TYPE)
@classmethod @classmethod
def c(cls, coords: TUnion[list, 'Point']) -> 'Point': def c(cls, coords: TUnion[list, 'Point']) -> 'Point':
"""Ensure coords is an instance of Point""" """Ensure coords is an instance of Point (idempotent)"""
if isinstance(coords, Point): if isinstance(coords, Point):
return coords return coords
return Point(coords) return Point(coords)
@ -177,10 +177,12 @@ class Object:
return Difference(subject=self, tool=tool) return Difference(subject=self, tool=tool)
def union(self, objects: TUnion[list, 'Object']) -> 'Object': def union(self, objects: TUnion[list, 'Object']) -> 'Object':
"""Form the union of self and an object or list of objects"""
return Union(child=Collection.c(objects)._add(self)) return Union(child=Collection.c(objects)._add(self))
class Header: class Header:
"""Render a header (setting global values) of an OpensCAD file"""
def __init__(self, quality: str = 'draft'): def __init__(self, quality: str = 'draft'):
self.quality = quality self.quality = quality
@ -451,13 +453,14 @@ class Polygon(Object):
class Collection(Object): class Collection(Object):
"""Represents a collection of objects"""
def __init__(self, coll: list): def __init__(self, coll: list):
self.collection = coll self.collection = coll
@classmethod @classmethod
def c(cls, coll: TUnion[list, Object]) -> Object: def c(cls, coll: TUnion[list, Object]) -> Object:
"""Cast lists to collections""" """Ensure the list of objects is a Collection (idempotent)"""
if isinstance(coll, Object): if isinstance(coll, Object):
return coll return coll
return cls(coll) return cls(coll)
@ -494,8 +497,8 @@ class Rotate(Object):
class Scale(Object): class Scale(Object):
def __init__(self, v: TUnion[list, Point, float], child: Object): def __init__(self, v: TUnion[list, Point, float, int], child: Object):
if isinstance(v, float): if isinstance(v, float) or isinstance(v, int):
v = [v, v, v] v = [v, v, v]
self.v = Point.c(v) self.v = Point.c(v)
self.child = child self.child = child