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

Eric Wilhelm ewilhelm at sbcglobal.net
Sun Oct 5 18:02:55 EDT 2003


> The following was supposedly scribed by
> Art Haas
> on Sunday 05 October 2003 04:01 pm:

>>>> import dwgbase
>>>> dwg = dwgbase.Dwg("/path/to/the/file.dwg")
>[ ... the 'file.dwg' can be an R13, R14, or R15 file ... ]
>>>> objs = dwg.getObjects()
>[ ... the 'objs' will be a list of dwgEntity objects - we'll look at
>      the first one ... ]
>>>> obj = objs[0]
>>>> obj.getType()
>20
>>>> obj.getEntityKeys()
>['foo', 'bar', 'blah']
>>>> fooval = obj.getEntityData('foo')
>>>> barval = obj.getEntityData('bar')

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?  

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?)

--Eric



More information about the PythonCAD mailing list