import re class Parser: def __init__(self): self.all = [] self.op = [] def parser(self): pattern1 = re.compile(r"m_pData\[\d+\]\t\{x=(.*?) y=(.*?) z=(.*?) \}\tOdGePoint3d", re.VERBOSE) pattern2 = re.compile(r"m_pData\[\d+\]\t(.*?)\tdouble", re.VERBOSE) pattern3 = re.compile(r"\{x=(.*?) y=(.*?) z=(.*?) \}") pattern4 = re.compile(r"m_pt\[\d+\]\t\{x=(.*?) y=(.*?) z=(.*?) \}\tOdGePoint3d", re.VERBOSE) pattern5 = re.compile(r"m_dBugle\[\d+\]\t(.*?)\tdouble", re.VERBOSE) begin_pattern = re.compile(r"体(\d+):(createCone)?") op_pattern = re.compile(r"(?:循环体|扣减体|体)(\d+)和(?:循环体|扣减体|体)(\d+)布尔([并交差])") offset_pattern = re.compile(r"体(\d+) ([XYZ])轴偏移(-?\d+)") split_pattern = re.compile(r"体(\d+)被切割") with open("/home/xiaolong/HeteQuadrature/wxl/data_1.txt") as f: points = [] bulges = [] extusion = [] index = 1 end = False for line in f: mathc0 = begin_pattern.findall(line) if mathc0 != []: index = mathc0[0][0] if mathc0[0][1] != "": next_line = next(f) temp_pattern = re.compile(r"topPt\t\{x=(.*?) y=(.*?) z=(.*?) \}\tOdGePoint3d") temp_match1 = temp_pattern.findall(next_line) next_line = next(f) temp_pattern = re.compile(r"downPt\t\{x=(.*?) y=(.*?) z=(.*?) \}\tOdGePoint3d") temp_match2 = temp_pattern.findall(next_line) next_line = next(f) temp_pattern = re.compile(r"dRadius1\t(.*?)\tdouble") temp_match3 = temp_pattern.findall(next_line) next_line = next(f) temp_pattern = re.compile(r"dRadius2\t(.*?)\tdouble") temp_match4 = temp_pattern.findall(next_line) self.all.append([4, [index, temp_match1[0], temp_match2[0], temp_match3[0], temp_match4[0]]]) continue match1 = pattern1.findall(line) if match1 != []: points.append(match1) continue match4 = pattern4.findall(line) if match4 != []: points.append(match4) continue match2 = pattern2.findall(line) if match2 != []: bulges.append(match2) continue match5 = pattern5.findall(line) if match5 != []: bulges.append(match5) continue match3 = pattern3.findall(line) if match3 != []: extusion = match3 self.all.append([0, index, points, bulges, extusion]) points = [] bulges = [] extusion = [] continue op_match = op_pattern.findall(line) if op_match != []: self.all.append([1, op_match[0]]) continue offset_match = offset_pattern.findall(line) if offset_match != []: self.all.append([2, offset_match[0]]) continue split_match = split_pattern.findall(line) if split_match != []: next_line = next(f) temp_pattern1 = re.compile(r"topPt\t\{x=(.*?) y=(.*?) z=(.*?) \}\tOdGePoint3d") temp_match1 = temp_pattern1.findall(next_line) next_line = next(f) temp_pattern2 = re.compile(r"k([XYZ])Axis") temp_match2 = temp_pattern2.findall(next_line) self.all.append([3, [split_match[0], temp_match1[0], temp_match2[0]]]) continue def generate(self): result = " Loader loader;\n\n" result += " std::vector points;\n" result += " std::vector bulges;\n" result += " Vector3D extusion;\n\n" for value in self.all: if (value[0] == 0): _, index, points, bulges, extusion = value result += " /* 体{} */\n".format(index) if points == []: continue result += " points.clear();\n" result += " bulges.clear();\n" for point in points: result += " points.push_back(Point3D{" + point[0][0] + "," + point[0][1] + "," + point[0][2] +"});\n" for bulge in bulges: result += " bulges.push_back(" + bulge[0] + ");\n" if extusion != []: result += " extusion = Vector3D{" + extusion[0][0] + "," + extusion[0][1] + "," + extusion[0][2] + "};\n" result += " auto tag" + index + " = loader.addExtrude(points, bulges, extusion);\n" result += "\n" if (value[0] == 1): op = value[1] result += " /* 体{}和体{}布尔{} */\n".format(op[0], op[1], op[2]) if (op[2] == "并"): result += " loader.unionNode(tag" + op[0] + ", tag" + op[1] + ");\n\n" elif (op[2] == "交"): result += " loader.intersectNode(tag" + op[0] + ", tag" + op[1] + ");\n\n" elif (op[2] == "差"): result += " loader.differentNode(tag" + op[0] + ", tag" + op[1] + ");\n\n" if (value[0] == 2): offset =value[1] result += " /* 体{} {}轴偏移 {} */\n".format(offset[0], offset[1], offset[2]) if (offset[1] == "X"): result += " direction = Direction3D{1, 0, 0};\n" elif (offset[1] == "Y"): result += " direction = Direction3D{0, 1, 0};\n" elif (offset[1] == "Z"): result += " direction = Direction3D{0, 0, 1};\n" result += " offset = " + offset[2] + ";\n" result += " loader.offset(tag" + offset[0] + ", direction, offset);\n\n" if (value[0] == 3): split = value[1] result += " /* 体{}被切割 */\n".format(split[0]) if (split[2] == "X"): result += " direction = Direction3D{1, 0, 0};\n" elif (split[2] == "Y"): result += " direction = Direction3D{0, 1, 0};\n" elif (split[2] == "Z"): result += " direction = Direction3D{0, 0, 1};\n" result += " basePoint = Point3D{" + split[1][0] + "," + split[1][1] + "," + split[1][2] +"};\n" result += " loader.split(tag" + split[0] + ", basePoint, direction);\n\n" if (value[0] == 4): cone = value[1] index, top, bottom, radius1, radius2 = cone result += " /* 体{} */\n".format(index) result += " topPoint = Point3D{" + top[0] + "," + top[1] + "," + top[2] +"};\n" result += " bottomPoint = Point3D{" + bottom[0] + "," + bottom[1] + "," + bottom[2] +"};\n" result += " radius1 = " + radius1 + ";\n" result += " radius2 = " + radius2 + ";\n" result += " auto tag" + index + " = loader.addCone(topPoint, bottomPoint, radius1, radius2);\n\n" print(result) p = Parser() p.parser() # print(p.all) p.generate()