コード例 #1
0
def from_assembly(cad_obj,
                  top,
                  loc=None,
                  render_mates=False,
                  mate_scale=1,
                  default_color=None):
    loc = Location()
    render_loc = cad_obj.loc

    if cad_obj.color is None:
        if default_color is None:
            color = Color(get_default("default_color"))
        else:
            color = Color(default_color)
    else:
        color = Color(get_rgb(cad_obj.color))

    # Special handling for edge lists in an MAssembly
    is_edges = [isinstance(obj, Edge) for obj in cad_obj.shapes]
    if is_edges and all(is_edges):
        if cad_obj.color is None:
            if default_color is None:
                color = Color(get_default("default_edgecolor"))
            else:
                color = Color(default_color)
        else:
            color = Color(get_rgb(cad_obj.color))

        workplane = Workplane()
        workplane.objects = cad_obj.shapes
        parent = [Edges(
            workplane,
            name="%s_0" % cad_obj.name,
            color=color,
        )]
    else:
        if cad_obj.color is None:
            if default_color is None:
                color = Color(get_default("default_color"))
            else:
                color = Color(default_color)
        else:
            color = Color(get_rgb(cad_obj.color))
        parent = [
            Part(
                Workplane(shape),
                "%s_%d" % (cad_obj.name, i),
                color=color,
            ) for i, shape in enumerate(cad_obj.shapes)
        ]

    if render_mates and cad_obj.mates is not None:
        rgb = (Color((255, 0, 0)), Color((0, 128, 0)), Color((0, 0, 255)))
        pg = PartGroup(
            [
                Edges(to_edge(mate_def.mate, scale=mate_scale),
                      name=name,
                      color=rgb) for name, mate_def in top.mates.items()
                if mate_def.assembly == cad_obj
            ],
            name="mates",
            loc=Location(
            ),  # mates inherit the parent location, so actually add a no-op
        )
        if pg.objects:
            parent.append(pg)

    children = [
        from_assembly(c, top, loc, render_mates, mate_scale)
        for c in cad_obj.children
    ]
    return PartGroup(parent + children, cad_obj.name, loc=render_loc)
コード例 #2
0
def _from_sketch(cad_obj, obj_id, show_parent=True, show_selection=True):

    result = []

    locs = cad_obj.locs if cad_obj.locs else [Location()]

    workplane = Workplane()
    if cad_obj._faces:
        for loc in locs:
            workplane.objects += cad_obj._faces.moved(loc).Faces()
        result += _from_facelist(workplane,
                                 obj_id,
                                 name="Faces",
                                 show_parent=show_parent)
    elif cad_obj._edges:
        workplane.objects = [
            edge.moved(loc) for edge in cad_obj._edges for loc in locs
        ]
        result += _from_edgelist(workplane,
                                 obj_id,
                                 name="Edges",
                                 show_parent=show_parent)

    if show_selection and cad_obj._selection:
        workplane = Workplane()
        if isinstance(cad_obj._selection[0], Location):
            workplane.objects = [
                Vertex.makeVertex(0, 0, 0).moved(loc * obj)
                for obj in cad_obj._selection for loc in locs
            ]
            sel = _from_vertexlist(workplane,
                                   obj_id,
                                   name="Locations",
                                   show_parent=show_parent)

        elif isinstance(cad_obj._selection[0], Face):
            for loc in locs:
                workplane.objects += flatten([
                    obj._faces.moved(loc).Faces() for obj in cad_obj._selection
                ])
            sel = _from_facelist(workplane,
                                 obj_id,
                                 name="Faces",
                                 show_parent=show_parent)

        elif isinstance(cad_obj._selection[0], (Edge, Wire)):
            workplane.objects = [
                edge.moved(loc) for edge in cad_obj._selection for loc in locs
            ]
            sel = _from_edgelist(workplane,
                                 obj_id,
                                 name="Edges",
                                 show_parent=show_parent)

        elif isinstance(cad_obj._selection[0], Vertex):
            workplane.objects = [
                vertex.moved(loc) for vertex in cad_obj._selection
                for loc in locs
            ]
            sel = _from_vertexlist(workplane,
                                   obj_id,
                                   name="Vertices",
                                   show_parent=show_parent)

        result.append(PartGroup(sel, name=f"Selection_{obj_id}"))

    return result