Skip to content

Instantly share code, notes, and snippets.

@hideyukisaito
Created December 12, 2019 17:58
Show Gist options
  • Save hideyukisaito/aa3042558e58ec58f597b152e29b5622 to your computer and use it in GitHub Desktop.
Save hideyukisaito/aa3042558e58ec58f597b152e29b5622 to your computer and use it in GitHub Desktop.
import sys
import os
import copy
class Section():
def __init__(self):
self.headers = []
self.tables = []
self.blocks = []
self.inserts = []
self.entities = []
class Block():
def __init__(self):
self.name = ''
self.layer_name = ''
self.block_type = ''
self.x = 0
self.y = 0
self.entities = []
class BlockInsert():
def __init__(self):
self.name = ''
self.layer_name = ''
self.block_name = ''
self.x = 0
self.y = 0
class Entity():
def __init__(self):
self.type = ''
self.name = ''
self.full = list([])
def main():
if 2 > len(sys.argv):
print('no input file')
sys.exit()
filename = sys.argv[1]
lines = []
scangroup = []
with open(os.path.join(os.getcwd(), 'sample/data/result.txt'), 'wt', encoding='shift_jis') as fout:
with open(os.path.abspath(filename), 'rt', encoding='shift_jis') as fin:
line_num = 0
offset = 0
while True:
line = fin.readline()
if not line:
break
line = line.strip()
if line == '0':
scangroup.append([])
key = line
line = fin.readline()
value = line.strip()
scangroup[-1].append([key, value])
fout.write(key.rjust(3, ' ') + ' ' + value + '\n')
print(len(scangroup))
sections = make_section(scangroup)
exportImage(sections[0])
def make_section(groups):
is_blocks = False
is_entities = False
copied_groups = copy.deepcopy(groups)
sections = []
section = Section()
block = Block()
entity = Entity()
for group in copied_groups:
if '0' == group[0][0]:
if group[0][1] in { 'SECTION' }:
section = Section()
for rows in group:
if '2' == rows[0]:
if 'BLOCKS' in rows[1]:
is_blocks = True
is_entities = False
elif 'ENTITIES' in rows[1]:
is_blocks = False
is_entities = True
elif 'HEADER' in rows[1]:
is_blocks = False
is_entities = False
section.headers = group
elif 'TABLES' in rows[1]:
is_blocks = False
is_entities = False
section.tables = group
sections.append(section)
elif group[0][1] in { 'VERTEX', 'LINE', 'POLYLINE', 'SEQEND', 'TEXT' }:
entity = Entity()
entity.type = group[0][1]
entity.full = group
if is_blocks:
block.entities.append(entity)
elif is_entities:
sections[-1].entities.append(entity)
elif group[0][1] in { 'INSERT' }:
block_insert = BlockInsert()
for rows in group:
if '8' == rows[0]:
block_insert.layer_name = rows[1]
if '2' == rows[0]:
block_insert.block_name = rows[1]
if '10' == rows[0]:
block_insert.x = float(rows[1])
if '20' == rows[0]:
block_insert.y = float(rows[1])
sections[-1].inserts.append(block_insert)
elif group[0][1] in { 'BLOCK' }:
block = Block()
for rows in group:
if '8' == rows[0]:
block.layer_name = rows[1]
if '2' == rows[0]:
block.name = rows[1]
if '70' == rows[0]:
block.block_type = rows[1]
if '10' == rows[0]:
block.x = float(rows[1])
if '20' == rows[0]:
block.y = float(rows[1])
elif group[0][1] in { 'ENDBLK' }:
sections[-1].blocks.append(block)
elif group[0][1] in { 'EOF' }:
print('end of file')
else:
print('other', group[0][1])
return sections
def exportImage(section):
for block in section.blocks:
for entity in block.entities:
if 'SEQEND' == entity.type:
print('end of sequence')
if 'VERTEX' == entity.type:
for rows in entity.full:
if '10' == rows[0]:
print('x coord:', rows[1])
elif '20' == rows[1]:
print('y coord:', rows[1])
if 'LINE' == entity.type:
for rows in entity.full:
if '10' == rows[0]:
print('x coord of line starts:', rows[1])
elif '11' == rows[1]:
print('y coord of line starts:', rows[1])
elif '20' == rows[1]:
print('x coord of line ends:', rows[1])
elif '21' == rows[1]:
print('y coord of line ends:', rows[1])
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment