[PythonCAD] Improved DWG reader, take 2 ...

Art Haas ahaas at airmail.net
Sun Oct 5 19:54:00 EDT 2003

On Sun, Oct 05, 2003 at 05:02:55PM -0500, Eric Wilhelm wrote:
> [ ... snip my code snippet ... ]
> As butchered as the code gets when quoted here, I like this design for having 
> abstracted the file format to an interface which is independent of the 
> version.  


> The only thing I could ask for beyond that is that the entities not have
> to be processed as a list.  This may be prohibitive given the nature of
> the dwg format, but is there a way to randomly access entities based on
> properties like layer and entity type?  

I don't see it if there is. The entites are all stored in the object map
section of the file, and there is no telling what type of object the
entity represents until you seek to the correct offset in the file, read
in the right amount of bits/bytes, and start decoding them. I don't
think the DWG file format lends itself to the sort of access you are
looking. If someone out there can correct me, please do, as it would be
nice to be able to read only certain entity types from the file, or
maybe entities that are on a specific layer.

> Maybe this is premature optimization.  I'll try digging through the code some 
> more and see if there is a simple and robust way to do it (such as storing 
> the offset to an entity as it is loaded (slight cost in memory, but might 
> enable a much faster access if you only want part of the data.))
> If the simplest method is to let them be a list, what about a method which 
> steps through the list, returning a handle to one entity at a time (similar 
> to read() and seek() functions?)

The way reading the entities works, at least with the R13, R14, and R15
readers, is the object map section is read in first, creating a list of
file offsets where the objects are stored. At this point the number of
entities in the file is known. So there can certainly be something
written where each each entity is individually read in. In the code I've
made, this is kinda-sorta done in the read_objects() functions, This
function just saves all the entities it reads in as a list then returns
the list. Some sort of generator-type code could be created to where
each entry taken from the object map processed one "object" at a time.

Take a peek at the code and see if you can come up with what you are
looking for, or write some pseudo-code and send it to the list. I'd bet
something can be cooked up ...

Man once surrendering his reason, has no remaining guard against absurdities
the most monstrous, and like a ship without rudder, is the sport of every wind.

-Thomas Jefferson to James Smith, 1822

More information about the PythonCAD mailing list