standard set of minor improvements, tho this is a good script and not much to change.

faster dict/list operations.
use layer flags ratehr then lists.
This commit is contained in:
Campbell Barton 2007-01-05 03:49:54 +00:00
parent ea71aeb254
commit 330efd46fa
3 changed files with 93 additions and 52 deletions

@ -162,8 +162,9 @@ class Line:
else:
self.color_index = BYLAYER
discard, self.layer = get_layer(obj.data)
obj.data.remove(discard)
discard, self.layer, discard_index = get_layer(obj.data)
del obj.data[discard_index]
self.points = self.get_points(obj.data)
@ -238,8 +239,8 @@ class LWpolyline:
self.flags = 0
self.closed = self.flags&1 # byte coded, 1 = closed, 128 = plinegen
discard, self.layer = get_layer(obj.data)
obj.data.remove(discard)
discard, self.layer, discard_index = get_layer(obj.data)
del obj.data[discard_index]
self.points = self.get_points(obj.data)
self.extrusion = self.get_extrusion(obj.data)
@ -341,8 +342,8 @@ class Polyline:
self.closed = self.flags&1 # byte coded, 1 = closed, 128 = plinegen
discard, self.layer = get_layer(obj.data)
obj.data.remove(discard)
discard, self.layer, discard_index = get_layer(obj.data)
del obj.data[discard_index]
self.extrusion = self.get_extrusion(obj.data)
@ -531,8 +532,8 @@ class Text:
else:
self.valignment = self.valignment[0]
discard, self.layer = get_layer(obj.data)
obj.data.remove(discard)
discard, self.layer, discard_index = get_layer(obj.data)
del obj.data[discard_index]
self.loc = self.get_loc(obj.data, self.halignment, self.valignment)
self.extrusion = self.get_extrusion(obj.data)
@ -638,8 +639,8 @@ class Mtext:
else:
self.line_space = self.line_space[0]
discard, self.layer = get_layer(obj.data)
obj.data.remove(discard)
discard, self.layer, discard_index = get_layer(obj.data)
del obj.data[discard_index]
self.loc = self.get_loc(obj.data)
self.extrusion = self.get_extrusion(obj.data)
@ -730,8 +731,8 @@ class Circle:
else:
self.color_index = BYLAYER
discard, self.layer = get_layer(obj.data)
obj.data.remove(discard)
discard, self.layer, discard_index = get_layer(obj.data)
del obj.data[discard_index]
self.loc = self.get_loc(obj.data)
self.extrusion = self.get_extrusion(obj.data)
@ -805,8 +806,8 @@ class Arc:
else:
self.color_index = BYLAYER
discard, self.layer = get_layer(obj.data)
obj.data.remove(discard)
discard, self.layer, discard_index = get_layer(obj.data)
del obj.data[discard_index]
self.loc = self.get_loc(obj.data)
self.extrusion = self.get_extrusion(obj.data)
@ -923,8 +924,8 @@ class Block:
else:
self.discription = ''
discard, self.layer = get_layer(obj.data)
obj.data.remove(discard)
discard, self.layer, discard_index = get_layer(obj.data)
del obj.data[discard_index]
self.loc = self.get_loc(obj.data)
@ -985,8 +986,8 @@ class Insert:
else:
self.color_index = BYLAYER
discard, self.layer = get_layer(obj.data)
obj.data.remove(discard)
discard, self.layer, discard_index = get_layer(obj.data)
del obj.data[discard_index]
self.loc = self.get_loc(obj.data)
self.scale = self.get_scale(obj.data)
self.rows, self.columns = self.get_array(obj.data)
@ -1097,8 +1098,8 @@ class Ellipse:
else:
self.color_index = BYLAYER
discard, self.layer = get_layer(obj.data)
obj.data.remove(discard)
discard, self.layer, discard_index = get_layer(obj.data)
del obj.data[discard_index]
self.loc = self.get_loc(obj.data)
self.major = self.get_major(obj.data)
self.extrusion = self.get_extrusion(obj.data)
@ -1185,8 +1186,8 @@ class Face:
else:
self.color_index = BYLAYER
discard, self.layer = get_layer(obj.data)
obj.data.remove(discard)
discard, self.layer, discard_index = get_layer(obj.data)
del obj.data[discard_index]
self.points = self.get_points(obj.data)
@ -1242,7 +1243,6 @@ class Face:
return "%s: layer - %s, points - %s" %(self.__class__.__name__, self.layer, self.points)
def get_name(data):
"""Get the name of an object from its object data.
@ -1251,23 +1251,23 @@ def get_name(data):
name not None before using the returned values!
"""
value = None
for item in data:
for i, item in enumerate(data):
if item[0] == 2:
value = item[1]
break
return item, value
return item, value, i
def get_layer(data):
"""Expects object data as input.
Returns (entry, layer_name) where entry is the data item that provided the layer name.
Returns (entry, layer_name, entry_index) where entry is the data item that provided the layer name.
"""
value = None
for item in data:
for i, item in enumerate(data):
if item[0] == 8:
value = item[1]
break
return item, value
return item, value, i
# type to object map

@ -26,6 +26,9 @@
# ***** END GPL LICENCE BLOCK *****
# --------------------------------------------------------------------------
# development
#import dxfImportObjects
#reload(dxfImportObjects)
from dxfImportObjects import *
@ -141,9 +144,9 @@ def handleObject(infile):
def handleTable(table, infile):
"""Special handler for dealing with nested table objects."""
item, name = get_name(table.data)
item, name, item_index = get_name(table.data)
if name: # We should always find a name
table.data.remove(item)
del table.data[item_index]
table.name = name.lower()
# This next bit is from handleObject
# handleObject should be generalized to work with any section like object
@ -162,9 +165,9 @@ def handleTable(table, infile):
def handleBlock(block, infile):
"""Special handler for dealing with nested table objects."""
item, name = get_name(block.data)
item, name, item_index = get_name(block.data)
if name: # We should always find a name
block.data.remove(item)
del block.data[item_index]
block.name = name.lower()
# This next bit is from handleObject
# handleObject should be generalized to work with any section like object
@ -302,9 +305,9 @@ def readDXF(filename):
if drawing:
drawing.name = filename
for obj in drawing.data:
item, name = get_name(obj.data)
item, name, item_index = get_name(obj.data)
if name:
obj.data.remove(item)
del obj.data[item_index]
obj.name = name.lower()
setattr(drawing, name.lower(), obj)
# Call the objectify function from dxfImportObjects to cast

@ -78,10 +78,17 @@ except ImportError:
import sys
curdir = Sys.dirname(Blender.Get('filename'))
sys.path.append(curdir)
# development
#import dxfReader
#reload(dxfReader)
from dxfReader import readDXF
from dxfColorMap import color_map
from math import *
try:
import os
if os.name:# != 'mac':
@ -137,7 +144,7 @@ class MatColors:
self.map[color] = layer
color = 0
color = abs(color)
if color not in self.colors.keys():
if color not in self.colors: # .keys()
self.add(color)
return self.colors[color]
@ -179,7 +186,7 @@ class Blocks:
"""
if not name:
return self.blocks
if name not in self.blocks.keys():
if name not in self.blocks: # .keys():
self.add(name)
return self.blocks[name]
@ -223,17 +230,17 @@ class Settings:
# The header section may be omited
if self.optimization <= self.MID:
if 'header' in sections.keys():
if 'header' in sections: #.keys():
print "Found header!"
else:
print "File contains no header!"
# The tables section may be partialy or completely missing.
if 'tables' in sections.keys():
if 'tables' in sections: # .keys():
if self.optimization <= self.MID:
print "Found tables!"
tables = dict([(item.name, item) for item in sections["tables"].data])
if 'layer' in tables.keys():
if 'layer' in tables: # .keys():
if self.optimization <= self.MID:
print "Found layers!"
# Read the layers table and get the layer colors
@ -251,7 +258,7 @@ class Settings:
self.colors = MatColors({})
# The blocks section may be omited
if 'blocks' in sections.keys():
if 'blocks' in sections: #.keys():
if self.optimization <= self.MID:
print "Found blocks!"
# Read the block definitions and build our block object
@ -347,11 +354,11 @@ class Drawer:
# Set the visability
if settings.isOff(entity.layer):
ob.layers = [20]
ob.Layers = 1<<19 # [20]
elif block_def:
ob.layers = [19]
ob.Layers = (1<<18) # [19]
else:
ob.layers = [i+1 for i in range(20)]
ob.Layers = (1<<20)-1 # [i+1 for i in xrange(20)] # all layers
# # Set the visability
# if settings.isOff(entity.layer) or block_def:
@ -500,7 +507,7 @@ def drawDrawing(drawing):
drawEntities(drawing.entities, settings)
# Set the visable layers
SCENE.setLayers([i+1 for i in range(18)])
SCENE.setLayers([i+1 for i in xrange(18)]) # SCENE.Layers = 262143 or (1<<18)
Blender.Redraw(-1)
if settings.optimization <= settings.MID:
print "Done!"
@ -509,7 +516,7 @@ def drawEntities(entities, settings, group=None):
If provided 'group' is the Blender group new entities are to be added to.
"""
for _type, drawer in type_map.items():
for _type, drawer in type_map.iteritems():
# for each known type get a list of that type and call the associated draw function
drawer(entities.get_type(_type), settings, group)
@ -539,7 +546,7 @@ def drawLWpolyline(pline, curves=False):
"""Do all the specific things needed to import plines into Blender."""
# Generate the geometery
points = []
for i in range(len(pline.points)):
for i in xrange(len(pline.points)):
point = pline.points[i]
if not point.bulge:
points.append(point.loc)
@ -552,7 +559,7 @@ def drawLWpolyline(pline, curves=False):
if point.bulge >= 0:
verts.reverse()
points.extend(verts)
edges = [[num, num+1] for num in range(len(points)-1)]
edges = [[num, num+1] for num in xrange(len(points)-1)]
if pline.closed:
edges.append([len(pline.points)-1, 0])
@ -575,7 +582,7 @@ def drawPolyline(pline, curves=False):
"""Do all the specific things needed to import plines into Blender."""
# Generate the geometery
points = []
for i in range(len(pline.points)):
for i in xrange(len(pline.points)):
point = pline.points[i]
if not point.bulge:
points.append(point.loc)
@ -588,7 +595,7 @@ def drawPolyline(pline, curves=False):
if point.bulge >= 0:
verts.reverse()
points.extend(verts)
edges = [[num, num+1] for num in range(len(points)-1)]
edges = [[num, num+1] for num in xrange(len(points)-1)]
if pline.closed:
edges.append([len(pline.points)-1, 0])
@ -817,14 +824,14 @@ def drawArc(center, radius, start, end, step=0.5):
stepmatrix = Mathutils.RotationMatrix(step, 3, "Z")
point = Mathutils.Vector(startpoint)
for i in range(int(pieces)):
for i in xrange(int(pieces)):
point = stepmatrix * point
points.append(point)
points.append(endpoint)
if center:
points = [[point[0]+center[0], point[1]+center[1], point[2]+center[2]] for point in points]
edges = [[num, num+1] for num in range(len(points)-1)]
edges = [[num, num+1] for num in xrange(len(points)-1)]
return points, edges
drawEllipses = Drawer()
@ -1003,3 +1010,34 @@ type_map = {
if __name__ == "__main__":
Window.FileSelector(main, 'Import a DXF file', '*.dxf')
"""
# For testing compatibility
if 1:
# DEBUG ONLY
TIME= Blender.sys.time()
import os
print 'Searching for files'
os.system('find /metavr/ -iname "*.dxf" > /tmp/tempdxf_list')
print '...Done'
file= open('/tmp/tempdxf_list', 'r')
lines= file.readlines()
file.close()
def between(v,a,b):
if v <= max(a,b) and v >= min(a,b):
return True
return False
for i, _file in enumerate(lines):
if between(i, 50,60):
_file= _file[:-1]
print 'Importing', _file, '\nNUMBER', i, 'of', len(lines)
_file_name= _file.split('/')[-1].split('\\')[-1]
newScn= Scene.New(_file_name)
newScn.makeCurrent()
main(_file)
print 'TOTAL TIME: %.6f' % (Blender.sys.time() - TIME)
"""