def voxel_to_TopoDS(model, voxel_length, voxel_width, voxel_height):
    (position_x, position_y, position_z) = np.where(model.data)
    voxel = TopoDS_Compound()
    counter = TopoDS_Builder()
    counter.MakeCompound(voxel)

    for i in range(position_x.size):
        voxel1 = TopoDS_Shape(
            BRepPrimAPI_MakeBox(voxel_length, voxel_width,
                                voxel_height).Shape())
        transmat = gp_Trsf()
        x, y, z = position_x[i] * voxel_length, position_y[
            i] * voxel_width, position_z[i] * voxel_height
        transmat.SetTranslation(gp_Vec(float(x), float(y), float(z)))
        location = TopLoc_Location(transmat)
        voxel1.Location(location)
        counter.Add(voxel, voxel1)

    return voxel
예제 #2
0
    def Create_Bk(self, filename="SFU01204-4", ss="BKBF10", L=1000):
        pass
        "获取选择零件名称  获取路径"
        # 获取零件名称
        try:
            pass
            new_shape = TopoDS_Shape()
            #filename = "SFU2005-4"
            # 获取相应零件的路径***************************************
            self.partpath = os.getcwd()
            self.partpath = self.partpath + "\\3Ddata" + "\\STP" + "\\" + filename + ".stp"
            self.shape = read_step_file(self.partpath)
            self.shape.Free(True)  # 先释放shape
            # self.new_build.Add(self.aCompound,shape123)#将shaoe添加入复合体
            self.new_build.Add(self.aCompound, self.shape)
            #绘制丝杆**************************************************
            if int(filename[3:6]) == 12 or int(filename[3:6]) == 14 or int(
                    filename[3:6]) == 15:
                ss = "BKBF10"
            elif int(filename[3:6]) == 14 or int(filename[3:6]) == 15 or int(
                    filename[3:6]) == 16 or int(filename[3:6]) == 18:
                pass
                ss = "BKBF12"
            elif int(filename[3:6]) == 18 or int(filename[3:6]) == 20:
                ss = "BKBF15"
            elif int(filename[3:6]) == 20 or int(filename[3:6]) == 25:
                ss = "BKBF17"
            elif int(filename[3:6]) == 25 or int(filename[3:6]) == 28:
                ss = "BKBF20"
            elif int(filename[3:6]) == 32 or int(filename[3:6]) == 36:
                ss = "BKBF25"
            elif int(filename[3:6]) == 36 or int(filename[3:6]) == 40:
                ss = "BKBF30"
            elif int(filename[3:6]) == 40 or int(filename[3:6]) == 45 or int(
                    filename[3:6]) == 50:
                ss = "BKBF35"
            elif int(filename[3:6]) == 50 or int(filename[3:6]) == 55:
                ss = "BKBF40"
            self.BK_serise_dict[ss]["D3"] = int(filename[3:6])  #重新设置丝杆直径

            #print(filename[3:6])
            #PL = (L - 15 - 39 - 10) / 2
            '''
            PL = (L - self.BK_serise_dict[ss]["L1"] - self.BK_serise_dict[ss]["L3"]
                  - self.BK_serise_dict[ss]["L4"]) / 2
            '''
            #Center_point=filename[0:]
            PL = L / 2
            #P1 = [0, 0, PL + 39 + 15]
            P1 = [
                0, 0, PL + self.BK_serise_dict[ss]["L3"] +
                self.BK_serise_dict[ss]["L1"]
            ]
            #P2 = [0, 4, PL + 39 + 15]
            P2 = [
                0, self.BK_serise_dict[ss]["D1"] / 2, PL +
                self.BK_serise_dict[ss]["L3"] + self.BK_serise_dict[ss]["L1"]
            ]
            #P3 = [0, 4, PL + 39]
            P3 = [
                0, self.BK_serise_dict[ss]["D1"] / 2,
                PL + self.BK_serise_dict[ss]["L3"]
            ]
            #P4 = [0, 5, PL + 39]
            P4 = [
                0, self.BK_serise_dict[ss]["D2"] / 2,
                PL + self.BK_serise_dict[ss]["L3"]
            ]
            #P5 = [0, 5, PL]
            P5 = [0, self.BK_serise_dict[ss]["D2"] / 2, PL]
            #P6 = [0, 6, PL]
            P6 = [0, self.BK_serise_dict[ss]["D3"] / 2, PL]
            #P7 = [0, 6, -PL]
            P7 = [0, self.BK_serise_dict[ss]["D3"] / 2, -PL]
            #P8 = [0, 4, -PL]
            P8 = [0, self.BK_serise_dict[ss]["D4"] / 2, -PL]
            #P9 = [0, 4, -PL - 7.9]
            P9 = [
                0, self.BK_serise_dict[ss]["D4"] / 2,
                -PL - self.BK_serise_dict[ss]["L5"]
            ]
            #P10 = [0, 4 - 0.2, -PL - 7.9]
            P10 = [
                0, self.BK_serise_dict[ss]["D5"] / 2,
                -PL - self.BK_serise_dict[ss]["L5"]
            ]
            #P11 = [0, 4 - 0.2, -PL - 7.9 - 0.8]
            P11 = [
                0, self.BK_serise_dict[ss]["D5"] / 2, -PL -
                self.BK_serise_dict[ss]["L5"] - self.BK_serise_dict[ss]["L6"]
            ]
            #P12 = [0, 4, -PL - 7.9 - 0.8]
            P12 = [
                0, self.BK_serise_dict[ss]["D4"] / 2, -PL -
                self.BK_serise_dict[ss]["L5"] - self.BK_serise_dict[ss]["L6"]
            ]
            #P13 = [0, 4, -PL - 10]
            P13 = [
                0, self.BK_serise_dict[ss]["D4"] / 2,
                -PL - self.BK_serise_dict[ss]["L4"]
            ]
            #P14 = [0, 0, -PL - 10]
            P14 = [0, 0, -PL - self.BK_serise_dict[ss]["L4"]]
            E11 = BRepBuilderAPI_MakeEdge(gp_Pnt(P1[0], P1[1], P1[2]),
                                          gp_Pnt(P2[0], P2[1], P2[2])).Edge()
            E12 = BRepBuilderAPI_MakeEdge(gp_Pnt(P2[0], P2[1], P2[2]),
                                          gp_Pnt(P3[0], P3[1], P3[2])).Edge()
            E13 = BRepBuilderAPI_MakeEdge(gp_Pnt(P3[0], P3[1], P3[2]),
                                          gp_Pnt(P4[0], P4[1], P4[2])).Edge()
            E14 = BRepBuilderAPI_MakeEdge(gp_Pnt(P4[0], P4[1], P4[2]),
                                          gp_Pnt(P5[0], P5[1], P5[2])).Edge()
            E15 = BRepBuilderAPI_MakeEdge(gp_Pnt(P5[0], P5[1], P5[2]),
                                          gp_Pnt(P6[0], P6[1], P6[2])).Edge()
            E16 = BRepBuilderAPI_MakeEdge(gp_Pnt(P6[0], P6[1], P6[2]),
                                          gp_Pnt(P7[0], P7[1], P7[2])).Edge()
            E17 = BRepBuilderAPI_MakeEdge(gp_Pnt(P7[0], P7[1], P7[2]),
                                          gp_Pnt(P8[0], P8[1], P8[2])).Edge()
            E18 = BRepBuilderAPI_MakeEdge(gp_Pnt(P8[0], P8[1], P8[2]),
                                          gp_Pnt(P9[0], P9[1], P9[2])).Edge()
            E19 = BRepBuilderAPI_MakeEdge(gp_Pnt(P9[0], P9[1], P9[2]),
                                          gp_Pnt(P10[0], P10[1],
                                                 P10[2])).Edge()
            E20 = BRepBuilderAPI_MakeEdge(gp_Pnt(P10[0], P10[1], P10[2]),
                                          gp_Pnt(P11[0], P11[1],
                                                 P11[2])).Edge()
            E21 = BRepBuilderAPI_MakeEdge(gp_Pnt(P11[0], P11[1], P11[2]),
                                          gp_Pnt(P12[0], P12[1],
                                                 P12[2])).Edge()
            E22 = BRepBuilderAPI_MakeEdge(gp_Pnt(P12[0], P12[1], P12[2]),
                                          gp_Pnt(P13[0], P13[1],
                                                 P13[2])).Edge()
            E23 = BRepBuilderAPI_MakeEdge(gp_Pnt(P13[0], P13[1], P13[2]),
                                          gp_Pnt(P14[0], P14[1],
                                                 P14[2])).Edge()
            E24 = BRepBuilderAPI_MakeEdge(gp_Pnt(P14[0], P14[1], P14[2]),
                                          gp_Pnt(P1[0], P1[1], P1[2])).Edge()

            new_charme = ChFi2d_ChamferAPI()
            new_charme.Init(E11, E12)
            new_charme.Perform()
            E25 = new_charme.Result(E11, E12, self.BK_serise_dict[ss]["C1"],
                                    self.BK_serise_dict[ss]["C1"])  #倒角1

            new_charme.Init(E13, E14)
            new_charme.Perform()
            E26 = new_charme.Result(E13, E14, self.BK_serise_dict[ss]["C2"],
                                    self.BK_serise_dict[ss]["C2"])  #倒角2

            new_charme.Init(E15, E16)
            new_charme.Perform()
            E27 = new_charme.Result(E15, E16, self.BK_serise_dict[ss]["C3"],
                                    self.BK_serise_dict[ss]["C3"])  #倒角3

            new_charme.Init(E16, E17)
            new_charme.Perform()
            E28 = new_charme.Result(E16, E17, self.BK_serise_dict[ss]["C3"],
                                    self.BK_serise_dict[ss]["C3"])  # 倒角4

            new_charme.Init(E22, E23)
            new_charme.Perform()
            E29 = new_charme.Result(E22, E23, self.BK_serise_dict[ss]["C1"],
                                    self.BK_serise_dict[ss]["C1"])  # 倒角5

            #print(type(E11))
            #print(E29.IsNull())

            W1 = BRepBuilderAPI_MakeWire(E11, E25, E12).Wire()
            W2 = BRepBuilderAPI_MakeWire(E13, E26, E14).Wire()
            W3 = BRepBuilderAPI_MakeWire(E15, E27, E16).Wire()
            W4 = BRepBuilderAPI_MakeWire(E16, E28, E17).Wire()
            W5 = BRepBuilderAPI_MakeWire(E18, E19, E20, E21).Wire()
            W6 = BRepBuilderAPI_MakeWire(E22, E29, E23, E24).Wire()
            #print("succeed")

            mkWire = BRepBuilderAPI_MakeWire()
            mkWire.Add(W1)
            mkWire.Add(W2)
            mkWire.Add(W3)
            mkWire.Add(W4)
            mkWire.Add(W5)
            mkWire.Add(W6)
            Rob = BRepPrimAPI_MakeRevol(
                BRepBuilderAPI_MakeFace(mkWire.Wire()).Face(),
                gp_Ax1(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1))).Shape()
            #倒角-----------------------------
            #MF=BRepFilletAPI_MakeChamfer(Rob)
            #MF.Add()
            #移动
            ls_filename = filename[0:8] + "_4"
            move_distance = 0.5 * L - (
                L - float(self.SFU_serise_dict[ls_filename]["L"])) / 2
            cone = TopoDS_Shape(Rob)
            T = gp_Trsf()
            T.SetTranslation(gp_Vec(0, 0, -move_distance))
            loc = TopLoc_Location(T)
            cone.Location(loc)
            self.new_build.Add(self.aCompound, cone)
            print(type(self.aCompound))
            return self.aCompound

        except:
            return False