[PythonCAD] seek to entity

Art Haas ahaas at airmail.net
Mon Oct 6 11:21:40 EDT 2003

On Sun, Oct 05, 2003 at 09:23:29PM -0500, Eric Wilhelm wrote:
> [ ... snip ... ]
> I'm not a fluent speaker of Python, so correct me if I'm reading this 
> incorrectly.
> It looks like the read_objects() function is storing the entity data in a data 
> structure and then adding this entire structure to the return list.
> What I had in mind (since it looks like you are going to run through yet 
> another level of code before getting this into your pythoncad data structure 
> anyway) is something more along the lines of:
> (if you don't mind me speaking in Perl)
> $dwg = Class::Whatever->new()
> $dwg->load("file/from/disk.dwg");
> $handle = $dwg->StartEntityGet("modelspace"); # rewinds the list
> while($object = $dwg->getObject($handle) ) {
> 	$type = $object->type();
> 	if($type eq "line") { # whatever, you get the point
> 		my %data = (
> 			"pts" => $object->getPoints(),
> 			"color" => $object->getColor(),
> 			);
> 		$self->addline(\%data);
> 		}
> 	} # end while
> See, I am going to loop over the entity list and load the data into a 
> structure of my own design (as I am guessing you plan to do as well.)

You can do this now with the current reading code as it stands now.
Start python and load in the 'dwgbase' module ...

>>> dwg = dwgbase.Dwg("/path/to/file.dwg")
>>> for obj in dwg.getObjects():
        type = obj.getType()
	if type == 34: # or whatever value floats your boat ...
             data = {}
	     for key in obj.getEntityKeys(): 
                 data[key] = obj.getEntityData[key]
        # use data in some fashion here ...

> For this reason, I think it makes sense to simply provide a way to get
> to the next entity (I agree that the format does not seem to lend itself
> to random access.)  If you set it up like this, it is similar to
> while($line = <FILE>) and you simply burn through the list, picking and
> choosing what you want.

I believe what you want is available now. My example code in earlier
mail messages demonstrated how the entire list of objects can be saved
in a list.

The one thing my current design does to is cache the objects after it
reads them, so there is a price in memory to be paid. My thinking was
that it would be good to save them so that multiple calls of
getObjects() would not require scanning the file again. Time will tell
if this choice is a good path to take.


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