[PythonCAD] Improved DWG Reader, Take 3
ahaas at airmail.net
Wed Oct 8 16:37:06 EDT 2003
After the exchange of e-mail regarding the previous DWG reader, I set
about to add a means of reading the entities from the DWG file one
entity at a time. Thanks to the neat new Python feature of generators
adding this ability has been easier than I'd hoped.
I've only changed the R15 reader to do handle this new reading
facitlity. The code to use this facility is simple ...
>>> dwg = dwgbase.Dwg("/path/to/file.dwg")
>>> for obj in dwg.getObject():
... [ ... do stuff here ... ]
Each object in the file is returned one is extracted, and the entities
are not cached in the Dwg instance. This should greatly reduce the
memory requirements when reading large files.
The previous method call getObjects(); which stores the entities, is
still present. I still think this can be useful in some situations, and
caching the entities is certainly faster if they list of entities needs
to be examined repeatedly.
Some of the earlier mail had perl code in them, so here's a perl-ish
representation of what the Dwg class now does. In the example code
pretend that the data in the file doesn't need processing in the way
that the DWG bitstream data does,.
Let's start with some variable setting code ...
my $file = '/path/to/some/file';
open(DWG, <$file) or die "Can't open '$file'! $!\n";
Using the entity-at-a-time getObject() call:
... do stuff with the data ...
In this case if you need to look at the contents of the file again you
have to re-read it. Likewise, the getObject() code has returned a
dwgEntity() object for you to use, but the Dwg class doesn't keep it
Using the getObjects() call as was seen previously:
@data = <DWG>
All the info from the file is stored in @data, and in the Dwg class all
the entities are saved away for quick retrieval.
If you only want to look at the info once then the getObject() approach
should work. If you need to repeatedly scan the data then use
The R15 code is now messier due to these changes. I've consolidated some
of the separate functions into single functions, and split some of the
large functions into several smaller ones. Aside from the fact that
there is now more unused code still in the R15 file, the direction
things are moving looks good.
If you want the code to play with it just mail me and I'll send it
along. I wish that the subversion repo was accessible now. It is
unlikely I'll get to work on get the subversion software installed on
that machine this week or weekend. Hopefully soon ...
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