Last active
November 25, 2018 21:37
-
-
Save i5ar/7ff846a00a68431d61bb0edc61dbf7f5 to your computer and use it in GitHub Desktop.
Get data from DWG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import itertools | |
import pdb | |
def find_sentinel(file, sentinel): | |
counter = 0 | |
pattern = [] | |
for i in itertools.count(): | |
buffer = file.read(1) | |
# NOTE: buffer[0] is 48 where buffer is equal to b'0\x00'. | |
if buffer[0] == sentinel[counter]: | |
# pdb.set_trace() | |
pattern.append(buffer) | |
counter += 1 | |
if counter >= len(sentinel): | |
print('Index:', i) | |
break | |
else: | |
if 0 < len(pattern) < len(sentinel): | |
pattern.clear() | |
counter = 0 | |
def get_data(file, size): | |
pattern = [] | |
for i in range(size): | |
buffer = file.read(1) | |
pattern.append(buffer) | |
return b''.join(pattern) | |
def main(): | |
try: | |
raw = sys.argv[1] | |
except IndexError: | |
print('Usage:', 'python main.py draw.dwg') | |
sys.exit(1) | |
try: | |
with open(raw, "rb") as file: | |
# NOTE: Data section AcDb:Preview (R13C3 AND LATER) [pag. 93] | |
sentinel_start = [ | |
0x1F, 0x25, 0x6D, 0x07, 0xD4, 0x36, 0x28, 0x28, | |
0x9D, 0x57, 0xCA, 0x3F, 0x9D, 0x44, 0x10, 0x2B] | |
sentinel_end = [ | |
0xE0, 0xDA, 0x92, 0xF8, 0x2B, 0xc9, 0xD7, 0xD7, | |
0x62, 0xA8, 0x35, 0xC0, 0x62, 0xBB, 0xEF, 0xD4] | |
find_sentinel(file, sentinel_start) | |
# overall size (overall size of image area) | |
data = get_data(file, 4) | |
# imagespresent (counter indicating what is present here) | |
data = get_data(file, 1) | |
# code (code indicating what follows) | |
data = get_data(file, 1) | |
# header data start if code == 1 (start of header data) | |
data = get_data(file, 4) | |
# header data size if code == 1 (size of header data) | |
data = get_data(file, 4) | |
print(data.hex()) | |
# TODO: Summary info Address in stream | |
# R2007 Meta Data [pag. 38] | |
# Data section AcDb:SummaryInfo Section [pag. 91] | |
# buffer = file.seek(int('20', 16)) | |
except IOError: | |
print('Could not open {}'.format(raw)) | |
sys.exit(1) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Following the specification from OpenDesign.