@@ -319,45 +319,56 @@ def convert_bspline(e):
319319
320320 # get the section which contains the header in the DXF file
321321 endsec = np .nonzero (blob [:, 1 ] == 'ENDSEC' )[0 ]
322- header_start = np .nonzero (blob [:, 1 ] == 'HEADER' )[0 ][0 ]
323- header_end = endsec [np .searchsorted (endsec , header_start )]
324- header_blob = blob [header_start :header_end ]
325322
326323 # get the section which contains entities in the DXF file
327324 entity_start = np .nonzero (blob [:, 1 ] == 'ENTITIES' )[0 ][0 ]
328325 entity_end = endsec [np .searchsorted (endsec , entity_start )]
329326 entity_blob = blob [entity_start :entity_end ]
330327
331- # store some properties from the DXF header
332- metadata = {'DXF_HEADER' : {}}
333-
334- for key in ['$DIMSCALE' , '$DIMUNIT' , '$INSUNITS' , '$LUNITS' ]:
335- value = get_key (header_blob ,
336- key ,
337- '70' )
338- if value is not None :
339- metadata ['DXF_HEADER' ][key ] = value
340-
341- # store unit data pulled from the header of the DXF
342- # prefer LUNITS over INSUNITS
343- # I couldn't find a table for LUNITS values but they
344- # look like they are 0- indexed versions of
345- # the INSUNITS keys, so for now offset the key value
346- for offset , key in [(- 1 , '$LUNITS' ),
347- (0 , '$INSUNITS' )]:
348- # get the key from the header blob
349- units = get_key (header_blob , key , '70' )
350- # if it exists add the offset
351- if units is None :
352- continue
353- metadata [key ] = units
354- units += offset
355- # if the key is in our list of units store it
356- if units in _DXF_UNITS :
357- metadata ['units' ] = _DXF_UNITS [units ]
358- # warn on drawings with no units
359- if 'units' not in metadata :
360- log .warning ('DXF doesn\' t have units specified!' )
328+ # store metadata
329+ metadata = {}
330+
331+ # try reading the header, which may be malformed
332+ header_start = np .nonzero (blob [:, 1 ] == 'HEADER' )[0 ]
333+ if len (header_start ) > 0 :
334+ header_end = endsec [np .searchsorted (endsec , header_start [0 ])]
335+ header_blob = blob [header_start [0 ]:header_end ]
336+
337+ # store some properties from the DXF header
338+ metadata ['DXF_HEADER' ] = {}
339+ for key , group in [('$ACADVER' , '1' ),
340+ ('$DIMSCALE' , '40' ),
341+ ('$DIMALT' , '70' ),
342+ ('$DIMALTF' , '40' ),
343+ ('$DIMUNIT' , '70' ),
344+ ('$INSUNITS' , '70' ),
345+ ('$LUNITS' , '70' )]:
346+ value = get_key (header_blob ,
347+ key ,
348+ group )
349+ if value is not None :
350+ metadata ['DXF_HEADER' ][key ] = value
351+
352+ # store unit data pulled from the header of the DXF
353+ # prefer LUNITS over INSUNITS
354+ # I couldn't find a table for LUNITS values but they
355+ # look like they are 0- indexed versions of
356+ # the INSUNITS keys, so for now offset the key value
357+ for offset , key in [(- 1 , '$LUNITS' ),
358+ (0 , '$INSUNITS' )]:
359+ # get the key from the header blob
360+ units = get_key (header_blob , key , '70' )
361+ # if it exists add the offset
362+ if units is None :
363+ continue
364+ metadata [key ] = units
365+ units += offset
366+ # if the key is in our list of units store it
367+ if units in _DXF_UNITS :
368+ metadata ['units' ] = _DXF_UNITS [units ]
369+ # warn on drawings with no units
370+ if 'units' not in metadata :
371+ log .warning ('DXF doesn\' t have units specified!' )
361372
362373 # find the start points of entities
363374 group_check = entity_blob [:, 0 ] == '0'
0 commit comments