예제 #1
0
파일: mesh.py 프로젝트: pcon-world/pcon_db
    def _write_faces(self, output, pov_file, identifier):
        """
        Triangulates all faces and writes them to *output* (javascript) and *pov_file* (POVRay).
        """
        BRepMesh_Mesh(self.topo_shape, self._precision)

        _points = kjDict()

        faces_iterator = Topo(self.topo_shape).faces()
        index = 0

        for F in faces_iterator:
            face_location = TopLoc_Location()
            triangulation = BRep_Tool_Triangulation(F, face_location)

            if triangulation.IsNull() == False:
                facing = triangulation.GetObject()
                tab = facing.Nodes()
                tri = facing.Triangles()
                the_normal = TColgp_Array1OfDir(tab.Lower(), tab.Upper())
                StdPrs_ToolShadedShape_Normal(F,
                                              Poly_Connect(facing.GetHandle()),
                                              the_normal)

                for i in range(1, facing.NbTriangles() + 1):

                    trian = tri.Value(i)

                    if F.Orientation() == TopAbs_REVERSED:
                        index1, index3, index2 = trian.Get()
                    else:
                        index1, index2, index3 = trian.Get()

                    P1 = tab.Value(index1).Transformed(
                        face_location.Transformation())
                    P2 = tab.Value(index2).Transformed(
                        face_location.Transformation())
                    P3 = tab.Value(index3).Transformed(
                        face_location.Transformation())
                    p1_coord = P1.XYZ().Coord()
                    p2_coord = P2.XYZ().Coord()
                    p3_coord = P3.XYZ().Coord()
                    if self._triangle_is_valid(P1, P2, P3):
                        for point in (p1_coord, p2_coord, p3_coord):
                            if not _points.has_key(point):
                                _points.add(point, index)
                                output.write(
                                    vertice_fmt %
                                    (identifier, point[0], point[1], point[2]))
                                index += 1

                        n1 = the_normal(index1)
                        n2 = the_normal(index2)
                        n3 = the_normal(index2)
                        output.write(face_fmt % (
                            identifier,
                            _points.neighbors(p1_coord)[0],
                            _points.neighbors(p2_coord)[0],
                            _points.neighbors(p3_coord)[0],
                            n1.X(),
                            n1.Y(),
                            n1.Z(),
                            n2.X(),
                            n2.Y(),
                            n2.Z(),
                            n3.X(),
                            n3.Y(),
                            n3.Z(),
                        ))
                        pov_file.write(triangle_fmt % (
                            p1_coord[0],
                            p1_coord[1],
                            p1_coord[2],
                            n1.X(),
                            n1.Y(),
                            n1.Z(),
                            p2_coord[0],
                            p2_coord[1],
                            p2_coord[2],
                            n2.X(),
                            n2.Y(),
                            n2.Z(),
                            p3_coord[0],
                            p3_coord[1],
                            p3_coord[2],
                            n3.X(),
                            n3.Y(),
                            n3.Z(),
                        ))
                        self.triangle_count += 1
예제 #2
0
def getSubShapes(lab, loc):
    global cnt, lvl
    cnt += 1
    print("\n[%d] level %d, handling LABEL %s\n" %
          (cnt, lvl, get_label_name(lab)))
    print()
    print(lab.DumpToString())
    print()
    print("Is Assembly    :", shape_tool.IsAssembly(lab))
    print("Is Free        :", shape_tool.IsFree(lab))
    print("Is Shape       :", shape_tool.IsShape(lab))
    print("Is Compound    :", shape_tool.IsCompound(lab))
    print("Is Component   :", shape_tool.IsComponent(lab))
    print("Is SimpleShape :", shape_tool.IsSimpleShape(lab))
    print("Is Reference   :", shape_tool.IsReference(lab))

    users = TDF_LabelSequence()
    users_cnt = shape_tool.GetUsers(lab, users)
    print("Nr Users       :", users_cnt)

    l_subss = TDF_LabelSequence()
    shape_tool.GetSubShapes(lab, l_subss)
    print("Nb subshapes   :", l_subss.Length())
    l_comps = TDF_LabelSequence()
    shape_tool.GetComponents(lab, l_comps)
    print("Nb components  :", l_comps.Length())
    print()

    if shape_tool.IsAssembly(lab):
        l_c = TDF_LabelSequence()
        shape_tool.GetComponents(lab, l_c)
        for i in range(l_c.Length()):
            label = l_c.Value(i + 1)
            if shape_tool.IsReference(label):
                print("\n########  reference label :", label)
                label_reference = TDF_Label()
                shape_tool.GetReferredShape(label, label_reference)
                loc = shape_tool.GetLocation(label)
                print("    loc          :", loc)
                trans = loc.Transformation()
                print("    tran form    :", trans.Form())
                rot = trans.GetRotation()
                print("    rotation     :", rot)
                print("    X            :", rot.X())
                print("    Y            :", rot.Y())
                print("    Z            :", rot.Z())
                print("    W            :", rot.W())
                tran = trans.TranslationPart()
                print("    translation  :", tran)
                print("    X            :", tran.X())
                print("    Y            :", tran.Y())
                print("    Z            :", tran.Z())

                locs.append(loc)
                print(">>>>")
                lvl += 1
                getSubShapes(label_reference, loc)
                lvl -= 1
                print("<<<<")
                locs.pop()

    elif shape_tool.IsSimpleShape(lab):
        print("\n########  simpleshape label :", lab)
        shape = shape_tool.GetShape(lab)
        print("    all ass locs   :", locs)

        loc = TopLoc_Location()
        for i in range(len(locs)):
            print("    take loc       :", locs[i])
            loc = loc.Multiplied(locs[i])

        trans = loc.Transformation()
        print("    FINAL loc    :")
        print("    tran form    :", trans.Form())
        rot = trans.GetRotation()
        print("    rotation     :", rot)
        print("    X            :", rot.X())
        print("    Y            :", rot.Y())
        print("    Z            :", rot.Z())
        print("    W            :", rot.W())
        tran = trans.TranslationPart()
        print("    translation  :", tran)
        print("    X            :", tran.X())
        print("    Y            :", tran.Y())
        print("    Z            :", tran.Z())
        shape = BRepBuilderAPI_Transform(shape, loc.Transformation()).Shape()

        c = Quantity_Color()
        colorSet = False
        if (color_tool.GetInstanceColor(shape, 0, c)
                or color_tool.GetInstanceColor(shape, 1, c)
                or color_tool.GetInstanceColor(shape, 2, c)):
            for i in (0, 1, 2):
                color_tool.SetInstanceColor(shape, i, c)
            colorSet = True
            n = c.Name(c.Red(), c.Green(), c.Blue())
            print('    instance color Name & RGB: ', c, n, c.Red(), c.Green(),
                  c.Blue())

        if not colorSet:
            if (color_tool.GetColor(lab, 0, c)
                    or color_tool.GetColor(lab, 1, c)
                    or color_tool.GetColor(lab, 2, c)):
                for i in (0, 1, 2):
                    color_tool.SetInstanceColor(shape, i, c)

                n = c.Name(c.Red(), c.Green(), c.Blue())
                print('    shape color Name & RGB: ', c, n, c.Red(), c.Green(),
                      c.Blue())

        # n = c.Name(c.Red(), c.Green(), c.Blue())
        # print('    color Name & RGB: ', c, n, c.Red(), c.Green(), c.Blue())
        # Display shape
        display.DisplayColoredShape(shape, c)

        for i in range(l_subss.Length()):
            lab = l_subss.Value(i + 1)
            print("\n########  simpleshape subshape label :", lab)
            shape = shape_tool.GetShape(lab)

            c = Quantity_Color()
            colorSet = False
            if (color_tool.GetInstanceColor(shape, 0, c)
                    or color_tool.GetInstanceColor(shape, 1, c)
                    or color_tool.GetInstanceColor(shape, 2, c)):
                for i in (0, 1, 2):
                    color_tool.SetInstanceColor(shape, i, c)
                colorSet = True
                n = c.Name(c.Red(), c.Green(), c.Blue())
                print('    instance color Name & RGB: ', c, n, c.Red(),
                      c.Green(), c.Blue())

            if not colorSet:
                if (color_tool.GetColor(lab, 0, c)
                        or color_tool.GetColor(lab, 1, c)
                        or color_tool.GetColor(lab, 2, c)):
                    for i in (0, 1, 2):
                        color_tool.SetInstanceColor(shape, i, c)

                    n = c.Name(c.Red(), c.Green(), c.Blue())
                    print('    shape color Name & RGB: ', c, n, c.Red(),
                          c.Green(), c.Blue())

            # n = c.Name(c.Red(), c.Green(), c.Blue())
            # print('    color Name & RGB: ', c, n, c.Red(), c.Green(), c.Blue())
            # Display shape
            display.DisplayColoredShape(shape, c)