Exemplo n.º 1
0
def display_shape(shape, clr=None, viewer_handle=None):
    if viewer_handle is None:
        viewer_handle = handle

    if isinstance(shape, shape_tuple):
        shape, representation = shape.geometry, shape
    else:
        representation = None

    material = Graphic3d.Graphic3d_MaterialAspect(
        Graphic3d.Graphic3d_NOM_PLASTER)

    if representation and not clr:
        if len(set(representation.styles)) == 1:
            clr = representation.styles[0]
            if min(clr) < 0. or max(clr) > 1.:
                clr = DEFAULT_STYLES.get(representation.data.type,
                                         DEFAULT_STYLES["DEFAULT"])

    if clr:
        ais = AIS.AIS_Shape(shape)
        ais.SetMaterial(material)

        if isinstance(clr, str):
            qclr = getattr(
                Quantity, "Quantity_NOC_%s" % clr.upper(),
                getattr(Quantity, "Quantity_NOC_%s1" % clr.upper(), None))
            if qclr is None:
                raise Exception("No color named '%s'" % clr.upper())
        elif isinstance(clr, Iterable):
            clr = tuple(clr)
            if len(clr) < 3 or len(clr) > 4:
                raise Exception("Need 3 or 4 color components. Got '%r'." %
                                len(clr))
            qclr = Quantity.Quantity_Color(clr[0], clr[1], clr[2],
                                           Quantity.Quantity_TOC_RGB)
        elif isinstance(clr, Quantity.Quantity_Color):
            qclr = clr
        else:
            raise Exception("Object of type %r cannot be used as a color." %
                            type(clr))

        ais.SetColor(qclr)
        if isinstance(clr, tuple) and len(clr) == 4 and clr[3] < 1.:
            ais.SetTransparency(1. - clr[3])

    elif representation and hasattr(AIS, "AIS_MultipleConnectedShape"):
        default_style_applied = None

        ais = AIS.AIS_MultipleConnectedShape(shape)

        subshapes = list(yield_subshapes(shape))
        lens = len(representation.styles), len(subshapes)
        if lens[0] != lens[1]:
            warnings.warn(
                "Unable to assign styles to subshapes. Encountered %d styles for %d shapes."
                % lens)
        else:
            for shp, stl in zip(subshapes, representation.styles):
                subshape = AIS.AIS_Shape(shp)
                if min(stl) < 0. or max(stl) > 1.:
                    default_style_applied = stl = DEFAULT_STYLES.get(
                        representation.data.type, DEFAULT_STYLES["DEFAULT"])
                subshape.SetColor(
                    Quantity.Quantity_Color(stl[0], stl[1], stl[2],
                                            Quantity.Quantity_TOC_RGB))
                subshape.SetMaterial(material)
                if len(stl) == 4 and stl[3] < 1.:
                    subshape.SetTransparency(1. - stl[3])
                ais.Connect(subshape.GetHandle())

        # For some reason it is necessary to set transparency here again
        # in order for transparency to be rendered on the subshape.
        applied_styles = representation.styles
        if default_style_applied:
            if len(default_style_applied) == 3:
                default_style_applied += (1., )
            applied_styles += (default_style_applied, )

        if len(applied_styles):
            # The only way for this not to be true if is the entire shape is NULL
            min_transp = min(map(operator.itemgetter(3), applied_styles))
            if min_transp < 1.:
                ais.SetTransparency(1.)

    else:
        ais = AIS.AIS_Shape(shape)
        ais.SetMaterial(material)

        def r():
            return random.random() * 0.3 + 0.7

        clr = Quantity.Quantity_Color(r(), r(), r(), Quantity.Quantity_TOC_RGB)
        ais.SetColor(clr)

    ais_handle = ais.GetHandle() if USE_OCCT_HANDLE else ais
    viewer_handle.Context.Display(ais_handle, False)

    return ais_handle
Exemplo n.º 2
0
def random_colored_material_aspect():
    clrs = [i for i in dir(Graphic3d) if i.startswith('Graphic3d_NOM_')]
    color = random.sample(clrs, 1)[0]
    print("color", color)
    return Graphic3d.Graphic3d_MaterialAspect(getattr(Graphic3d, color))
Exemplo n.º 3
0
from OCC.Core import TopoDS, Graphic3d, AIS, Bnd, BRepBndLib
from OCC.Core.TopoDS import TopoDS_Wire
import ifcopenshell.geom

from OCCUtils import Topology
import OCC
material = Graphic3d.Graphic3d_MaterialAspect(Graphic3d.Graphic3d_NOM_PLASTER)

settings = ifcopenshell.geom.settings()
settings.set(settings.USE_PYTHON_OPENCASCADE, True)

file = ifcopenshell.open("Mur.ifc")
walls = file.by_type("IfcWall")
bbox = OCC.Core.Bnd.Bnd_Box()
for wall in walls:
    product = ifcopenshell.geom.create_shape(settings, wall)
    shape = TopoDS.TopoDS_Iterator(product.geometry).Value()
    occ_display = ifcopenshell.geom.utils.initialize_display()
    ifcopenshell.geom.utils.display_shape(shape)
    shape_gpXYZ = shape.Location().Transformation().TranslationPart()
    print(shape_gpXYZ.X(), shape_gpXYZ.Y(), shape_gpXYZ.Z())
    break
    # wire = TopoDS.topods_Wire(product)
    # explorer = Topology.WireExplorer(wire)
    # vertices = explorer.ordered_vertices()
    # for vertex in vertices:
    #     print vertex
raw_input()