""" Single Rule L-System Generator with DXF Output Jordan Perr - 2010 - All Rights Reserved Version 1 """ ############### DEPENDENCIES ################ from math import cos, sin, pi import sdxf ############### CONFIGURATION ################ # Path to output file OFILE = "fern.dxf" # Start position [xcor, ycor, theta] START = [0,0,pi/2] # Segment of depth 0 will be this long SCALE = 20 # How many times should RULES be applied? # (Warning: Large DEPTH may freeze your machine.) DEPTH = 4 # L-System configuration (See ISBN:0262561271) ANGLE = 0.13962634 DS = 0.4 AXIOM = "F" RULE = "F=|[5+F][7-F]-|[4+F][6-F]-|[3+F][5-F]-|F" ################## CODE ##################### # Apply rule DEPTH times. Create turtle's roadmap. rule = RULE.split('=') commands = AXIOM[:] for step in range(DEPTH): commands = commands.replace(rule[0], '('+rule[1]+')') # Set Up sdxf AutoCad dxf library. CAD = sdxf.Drawing() CAD.styles.append(sdxf.Style()) CAD.views.append(sdxf.View('Normal')) CAD.views.append(sdxf.ViewByWindow('Window',leftBottom=(0,0),rightTop=(1,1))) #idem # Precompute line segment distances. Prepare to draw. dst = [SCALE*(DS**l) for l in range(1,DEPTH+2)] stack = [START] coef = 1 dep = 0 # Draw the L-System using a virtual turtle. for c in commands: if c in ['1','2','3','4','5','6','7','8','9']: coef = int(c) elif c == '(': dep = dep + 1 elif c == ')': dep = dep - 1 elif c == '[': stack.append(list(stack[-1])) elif c == ']': stack.pop() elif c == 'F' or c == '|': top = stack[-1] point1 = list(top) top[0] = top[0] + dst[dep]*cos(top[2]) top[1] = top[1] + dst[dep]*sin(top[2]) CAD.append(sdxf.Line(points=[point1[:2], list(top[:2])])) elif c == '+': top = stack[-1] top[2] = top[2] + ANGLE*coef coef = 1 elif c == '-': top = stack[-1] top[2] = top[2] - ANGLE*coef coef = 1 # Save the DXF file CAD.saveas(OFILE)