def readdxf(infile): infile = infile.readlines() file_index = 0 block_list = [] block_section_start_flag = False block_section_end_flag = False block_start_flag = False block_id_flag = False acbdblockbegin_flag = False block_end_flag = False line_flag = False polyline_flag = False while block_section_end_flag == False: infile[file_index] = infile[file_index].strip() if infile[file_index] == "BLOCKS": block_section_start_flag = True elif block_section_start_flag == True and infile[file_index] == "BLOCK": block_start_flag = True bloc = Block() elif block_start_flag == True and infile[file_index] == "330": block_id_flag = True elif block_start_flag == True and block_id_flag == True: bloc.id = infile[file_index] block_id_flag = False elif ( infile[file_index] == "AcDbBlockBegin" and block_start_flag == True and infile[file_index + 1].strip() == "2" ): acbdblockbegin_flag = True bloc.name = infile[file_index + 2].strip() elif infile[file_index] == "AcDbLine" and acbdblockbegin_flag == True and block_start_flag == True: line_flag = True elif line_flag == True and acbdblockbegin_flag == True and block_start_flag == True: if infile[file_index] == "10" and infile[file_index + 2].strip() == "20": lin = Line(bloc.name) lin.x_start = infile[file_index + 1].strip() lin.y_start = infile[file_index + 3].strip() lin.x_end = infile[file_index + 7].strip() lin.y_end = infile[file_index + 9].strip() bloc.object_list["line"].append(lin) line_flag = False elif infile[file_index] == "AcDbPolyline" and acbdblockbegin_flag == True and block_start_flag == True: polyline_flag = True elif polyline_flag == True and acbdblockbegin_flag == True and block_start_flag == True: if infile[file_index] == "10" and infile[file_index + 2].strip() == "20": poly = Polyline(bloc.name) x_coordinates = infile[file_index + 1 : file_index + 14 : 4] for item in x_coordinates: item.strip() y_coordinates = infile[file_index + 3 : file_index + 16 : 4] for item in y_coordinates: item.strip() x_coordinates = [float(item) for item in x_coordinates] y_coordinates = [float(item) for item in y_coordinates] poly.x_bl = min(x_coordinates) poly.y_bl = min(y_coordinates) poly.x_tr = max(x_coordinates) poly.y_tr = max(y_coordinates) bloc.object_list["poly"].append(poly) polyline_flag = False elif infile[file_index] == "AcDbBlockEnd" and acbdblockbegin_flag == True and block_start_flag == True: acbdblockbegin_flag = False block_start_flag = False block_list.append(bloc) elif block_section_start_flag == True and infile[file_index] == "ENDSEC": block_section_end_flag = True block_id_flag = False file_index += 1 return block_list
def readdxf(infile): infile = infile.readlines() file_index = 0 block_record = [] block_record_start_flag = False block_record_id_flag = False block_record_end_flag = False #block record collection program start #goes through dxf file and collects the unique ids of the blocks while block_record_end_flag == False: infile[file_index] = infile[file_index].strip() if infile[file_index] == 'BLOCK_RECORD': block_record_start_flag = True bloc_rec = Block_Record() elif infile[file_index] == '5': block_record_id_flag = True elif block_record_start_flag == True and block_record_id_flag == True: bloc_rec.name = infile[file_index] #l = copy.copy(bloc_rec) block_record.append(bloc_rec) #when using outside of class, you can append directly without the danger of overwriting block_record_start_flag = False block_record_id_flag = False elif infile[file_index] == 'ENDTAB': if infile[file_index+2].strip() == 'ENDSEC': block_record_start_flag = False block_record_id_flag = False block_record_end_flag = True file_index += 1 #block record collection program end block_list = [] block_section_end_flag = False #block collection program start while block_section_end_flag == False: block_id_flag = False block_acbdblockbegin_flag = False block_end_flag = False line_flag = False polyline_flag = False infile[file_index] = infile[file_index].strip() if infile[file_index] == 'BLOCK': block_start_flag = True bloc = Block() elif block_start_flag == True and infile[file_index] == '330': block_id_flag = True elif block_start_flag == True and block_id_flag == True: bloc.id = infile[file_index] block_id_flag = False elif infile[file_index] == 'AcDbBlockBegin' and block_start_flag == True: acbdblockbegin_flag = True elif infile[file_index] == '2' and acbdblockbegin_flag == True and block_start_flag == True: bloc.name = infile[file_index+1].strip() elif infile[file_index] == 'AcDbLine' and acbdblockbegin_flag == True and block_start_flag == True: line_flag = True elif line_flag == True and acbdblockbegin_flag == True and block_start_flag == True: if infile[file_index] == '10' and infile[file_index+2].strip() == '20': lin = Line(bloc.name) lin.x_start = entities[index+1].strip() lin.y_start = entities[index+3].strip() lin.x_end = entities[index+7].strip() lin.y_end = entities[index+9].strip() bloc.object_list['line'].append(lin) line_flag = False elif infile[file_index] == 'AcDbPolyline' and acbdblockbegin_flag == True and block_start_flag == True: polyline_flag = True elif polyline_flag == True and acbdblockbegin_flag == True and block_start_flag == True: if infile[file_index] == '10' and infile[file_index+2].strip() == '20': poly = Polyline(bloc.name) x_coordinates = entities[file_index+1:file_index+14:4].strip() y_coordinates = entities[file_index+3:file_index+16:4].strip() x_coordinates = [float(item) for item in self.x_coordinates] y_coordinates = [float(item) for item in self.y_coordinates] poly.x_bl = min(self.x_coordinates) poly.y_bl = min(self.y_coordinates) poly.x_tr = max(self.x_coordinates) poly.y_tr = max(self.y_coordinates) bloc.object_list['poly'].append(poly) polyline_flag = False elif infile[file_index] == 'AcDbBlockEnd' and acbdblockbegin_flag == True and block_start_flag == True: acbdblockbegin_flag = False block_start_flag = False block_list.append(bloc) elif infile[file_index] == 'ENDSEC': block_section_end_flag = True block_id_flag = False file_index += 1
def readdxf(infile, xmlfile): file = ET.parse('file.xml') tree = file.getroot() infile = infile.readlines() file_index = 0 block_list = [] block_section_start_flag = False block_section_end_flag = False block_start_flag = False block_id_flag = False acbdblockbegin_flag = False block_end_flag = False line_flag = False polyline_flag = False while block_section_end_flag == False: infile[file_index] = infile[file_index].strip() if infile[file_index] == 'BLOCKS': block_section_start_flag = True elif block_section_start_flag == True and infile[file_index] == 'BLOCK': block_start_flag = True bloc = Block() elif block_start_flag == True and infile[file_index] == '330': block_id_flag = True elif block_start_flag == True and block_id_flag == True: bloc.id = infile[file_index] block_id_flag = False elif infile[file_index] == 'AcDbBlockBegin' and block_start_flag == True and infile[file_index+1].strip() == '2': acbdblockbegin_flag = True bloc.name = infile[file_index+2].strip() elif infile[file_index] == 'AcDbLine' and acbdblockbegin_flag == True and block_start_flag == True: line_flag = True elif line_flag == True and acbdblockbegin_flag == True and block_start_flag == True: if infile[file_index] == '10' and infile[file_index+2].strip() == '20': lin = Line(bloc.name) lin.x_start = infile[file_index+1].strip() lin.y_start = infile[file_index+3].strip() lin.x_end = infile[file_index+7].strip() lin.y_end = infile[file_index+9].strip() bloc.object_list['line'].append(lin) line_flag = False elif infile[file_index] == 'AcDbPolyline' and acbdblockbegin_flag == True and block_start_flag == True: polyline_flag = True elif polyline_flag == True and acbdblockbegin_flag == True and block_start_flag == True: if infile[file_index] == '10' and infile[file_index+2].strip() == '20': poly = Polyline(bloc.name) x_coordinates = infile[file_index+1:file_index+14:4] for item in x_coordinates: item.strip() y_coordinates = infile[file_index+3:file_index+16:4] for item in y_coordinates: item.strip() x_coordinates = [float(item) for item in x_coordinates] y_coordinates = [float(item) for item in y_coordinates] poly.x_bl = min(x_coordinates) poly.y_bl = min(y_coordinates) poly.x_tr = max(x_coordinates) poly.y_tr = max(y_coordinates) bloc.object_list['poly'].append(poly) polyline_flag = False elif infile[file_index] == 'AcDbBlockEnd' and acbdblockbegin_flag == True and block_start_flag == True: acbdblockbegin_flag = False block_start_flag = False block_list.append(bloc) elif block_section_start_flag == True and infile[file_index] == 'ENDSEC': block_section_end_flag = True block_id_flag = False file_index += 1 return block_list