Exemple #1
0
def label(a_str:str, width:float=15, halign:str="left", valign:str="baseline", 
          size:int=10, depth:float=0.5, lineSpacing:float=1.15, 
          font:str="MgOpen Modata:style=Bold", segments:int=40, spacing:int=1) -> OpenSCADObject:
    """Renders a multi-line string into a single 3D object.
    
    __author__    = 'NerdFever.com'
    __copyright__ = 'Copyright 2018-2019 NerdFever.com'
    __version__   = ''
    __email__     = '*****@*****.**'
    __status__    = 'Development'
    __license__   = Copyright 2018-2019 NerdFever.com
    """

    lines = a_str.splitlines()

    texts = []

    for idx, l in enumerate(lines):
        t = text(text=l, halign=halign, valign=valign, font=font, spacing=spacing).add_param('$fn', segments)
        t = linear_extrude(height=1)(t)
        t = translate([0, -size * idx * lineSpacing, 0])(t)

        texts.append(t)

    result = union()(texts)
    result = resize([width, 0, depth])(result)
    result = translate([0, (len(lines)-1)*size / 2, 0])(result)

    return result
Exemple #2
0
def nameplate(id):
    #create the plate
    bottom = cube([xlen, ylen, 0.01], center=True)
    top = cube([xlen-zlen-zlen, ylen-zlen, 0.1], center=True)
    top = translate([0, zlen/2, zlen-0.1])(top)

    plate = hull()([bottom, top])

    # define the text
    id_str = name_str.replace('$ID',f'{id:03d}')
    msg = text(id_str,
               size=text_size,
               font=text_font,
               spacing = text_spacing,
               halign='center',
               valign='center')
    msg = linear_extrude(zlen+1)(msg)
    msg = resize([text_width,text_height,0])(msg)
    msg = translate([0, zlen/2, -0.5])(msg)
    
    #add text to the plate
    plate = plate - msg

    #generate output files
    scad_file = Path.cwd() / 'scad_files' / f'plate_{id:03d}.scad'
    if scad_file.parent.exists() is False:
        scad_file.parent.mkdir()
    scad_render_to_file(plate, str(scad_file))

    stl_file = Path.cwd() / 'stl_files' / f'plate_{id:03d}.stl'
    if stl_file.parent.exists() is False:
        stl_file.parent.mkdir()
    subprocess.run(['openscad', '-o', str(stl_file), str(scad_file)])
Exemple #3
0
def rcube(size, rnd=0, center=True):
    ''' primitive for a cube with rounded edges on 4 sides '''
    if rnd == 0: return solid.cube(size, center=center)
    round_ratio = (1 - rnd) * 1.1 + 0.5
    #TODO fix rounded cube for detent use-case
    c = solid.cube(size, center=center) * solid.resize(
        (size[0] * round_ratio, 0, 0))(solid.cylinder(
            h=size[2], d=size[1] * round_ratio, center=center))
    return c
Exemple #4
0
    def inset_text(self):
        """
        """
        text = s.text(
            text=self.text,
            font=self.font,
            size=self.font_size,
        )

        text = s.resize([self.top_dims[0] / 2, 0, 0])(text)
        text = s.translate([-self.top_dims[0] / 4, 0, 0])(text)
        text = s.resize([0, self.top_dims[1] / 2, 0])(text)
        # text = s.translate([0, -self.top_dims[1] / 4, 0])(text)
        text = s.linear_extrude(height=5)(text)
        text = s.translate([0, -1, -5])(text)
        text = s.translate([0, 0, self.z])(text)

        return text
Exemple #5
0
def rcube(size, rnd=0, center=True):
    ''' primitive for a cube with rounded edges on 4 sides '''
    if rnd == 0: return solid.cube(size, center=center)
    round_ratio = (1 - rnd) * 1.1 + 0.5
    #TODO fix rounded cube for detent use-case
    c = solid.cube(size, center=center) * solid.resize(
        (size[0] * round_ratio, 0, 0))(solid.cylinder(
            h=size[2], d=size[1] * round_ratio, center=center))
    return c


#SolidTools hacks to make functions easily done inline
solid.OpenSCADObject.mod = (lambda self, t: self.set_modifier(t))
solid.OpenSCADObject.translate = (lambda self, t: solid.translate(t)(self))
solid.OpenSCADObject.rotate = (lambda self, t: solid.rotate(t)(self))
solid.OpenSCADObject.resize = (lambda self, t: solid.resize(t)(self))


def iterable(obj):
    ''' test if object is iterable '''
    try:
        if isinstance(obj, str): return False
        iter(obj)
        return True
    except TypeError:
        return False


def diff(val1, val2):
    ''' get difference between numbers '''
    return max(val1, val2) - min(val1, val2)
Exemple #6
0
def label_size(
    a_str: str,
    width: float = 15,
    halign: str = "left",
    valign: str = "baseline",
    size: int = 10,
    depth: float = 0.5,
    lineSpacing: float = 1.4,
    font: str = "Impact:style=Bold",
    segments: int = 48,
    spacing: int = 1.2,
    direction: str = "ltr",
    center: bool = False,
    do_resize=True,
) -> Tuple[OpenSCADObject, Tuple[float, float, float]]:
    """Renders a multi-line string into a single 3D object.

    __author__    = 'NerdFever.com'
    __copyright__ = 'Copyright 2018-2019 NerdFever.com'
    __version__   = ''
    __email__     = '*****@*****.**'
    __status__    = 'Development'
    __license__   = Copyright 2018-2019 NerdFever.com

    """

    lines = a_str.splitlines()

    texts = []

    for idx, l in enumerate(lines):
        t = text(
            text=l,
            halign=halign,
            valign=valign,
            font=font,
            spacing=spacing,
            size=size,
            direction=direction,
        ).add_param("$fn", segments)
        t = linear_extrude(height=depth, center=center)(t)
        tvals = (0, -size * idx * lineSpacing, 0)
        if any(tvals):
            t = translate(tvals)(t)
        texts.append(t)

    if len(texts) > 1:
        result = union()(texts)
    else:
        result = texts[0]
    resize_vals = (
        width,
        0,
        depth,
    )
    if do_resize:
        result = resize(resize_vals)(result)
    restvals = (0, (len(lines) - 1) * size / 2, 0)
    if any(restvals):
        result = translate(restvals)(result)
    return result, resize_vals