summaryrefslogtreecommitdiff
path: root/src/helpers_solid.py
blob: 2557e010719eea8b2d88cfa73a4345cc7d893a03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import solid as sl

debug_trace = False

def debugprint(info):
    if debug_trace:
        print(info)

def box(width, height, depth):
    return sl.cube([width, height, depth], center=True)


def cylinder(radius, height, segments=100):
    return sl.cylinder(r=radius, h=height, segments=segments, center=True)


def sphere(radius):
    return sl.sphere(radius)


def cone(r1, r2, height):
    return sl.cylinder(r1=r1, r2=r2, h=height)  # , center=True)


def rotate(shape, angle):
    if shape is None:
        return None
    return sl.rotate(angle)(shape)


def translate(shape, vector):
    if shape is None:
        return None
    return sl.translate(tuple(vector))(shape)


def mirror(shape, plane=None):
    debugprint('mirror()')
    planes = {
        'XY': [0, 0, 1],
        'YX': [0, 0, -1],
        'XZ': [0, 1, 0],
        'ZX': [0, -1, 0],
        'YZ': [1, 0, 0],
        'ZY': [-1, 0, 0],
    }
    return sl.mirror(planes[plane])(shape)


def union(shapes):
    debugprint('union()')
    shape = None
    for item in shapes:
        if item is not None:
            if shape is None:
                shape = item
            else:
                shape += item
    return shape


def add(shapes):
    debugprint('union()')
    shape = None
    for item in shapes:
        if item is not None:
            if shape is None:
                shape = item
            else:
                shape += item
    return shape


def difference(shape, shapes):
    debugprint('difference()')
    for item in shapes:
        if item is not None:
            shape -= item
    return shape


def intersect(shape1, shape2):
    if shape2 is not None:
        return sl.intersection()(shape1, shape2)
    else:
        return shape1

def hull_from_points(points):
    return sl.hull()(*points)


def hull_from_shapes(shapes, points=None):
    hs = []
    if points is not None:
        hs.extend(points)
    if shapes is not None:
        hs.extend(shapes)
    return sl.hull()(*hs)


def tess_hull(shapes, sl_tol=.5, sl_angTol=1):
    return sl.hull()(*shapes)


def triangle_hulls(shapes):
    debugprint('triangle_hulls()')
    hulls = []
    for i in range(len(shapes) - 2):
        hulls.append(hull_from_shapes(shapes[i: (i + 3)]))

    return union(hulls)



def bottom_hull(p, height=0.001):
    debugprint("bottom_hull()")
    shape = None
    for item in p:
        proj = sl.projection()(p)
        t_shape = sl.linear_extrude(height=height, twist=0, convexity=0, center=True)(
            proj
        )
        t_shape = sl.translate([0, 0, height / 2 - 10])(t_shape)
        if shape is None:
            shape = t_shape
        shape = sl.hull()(p, shape, t_shape)
    return shape

def polyline(point_list):
    return sl.polygon(point_list)


# def project_to_plate():
#     square = cq.Workplane('XY').rect(1000, 1000)
#     for wire in square.wires().objects:
#         plane = cq.Workplane('XY').add(cq.Face.makeFromWires(wire))

def extrude_poly(outer_poly, inner_polys=None, height=1):
    if inner_polys is not None:
        return sl.linear_extrude(height=height, twist=0, convexity=0, center=True)(outer_poly, *inner_polys)
    else:
        return sl.linear_extrude(height=height, twist=0, convexity=0, center=True)(outer_poly)


def import_file(fname, convexity=2):
    print("IMPORTING FROM {}".format(fname))
    return sl.import_stl(fname + ".stl", convexity=convexity)


def export_file(shape, fname):
    print("EXPORTING TO {}".format(fname))
    sl.scad_render_to_file(shape, fname + ".scad")


def export_dxf(shape, fname):
    print("NO DXF EXPORT FOR SOLID".format(fname))
    pass