def drawnObject(points, mode='point'): """Return the geometric object resulting from draw2D points""" minor = None if '_' in mode: mode, minor = mode.split('_') closed = minor == 'closed' if mode == 'point': return points elif mode == 'polyline': if points.ncoords() < 2: return None closed = obj_params.get('closed', None) return PolyLine(points, closed=closed) elif mode == 'curve' and points.ncoords() > 1: curl = obj_params.get('curl', None) closed = obj_params.get('closed', None) return BezierSpline(points, curl=curl, closed=closed) elif mode == 'nurbs': degree = obj_params.get('degree', None) if points.ncoords() <= degree: return None closed = obj_params.get('closed', None) return NurbsCurve(points, degree=degree, closed=closed) elif mode == 'circle' and points.ncoords() % 3 == 0: R, C, N = triangleCircumCircle(points.reshape(-1, 3, 3)) circles = [circle(r=r, c=c, n=n) for r, c, n in zip(R, C, N)] if len(circles) == 1: return circles[0] else: return circles else: return None
def run(): clear() linewidth(2) flat() F = Formex([[[1., 0., 0.]], [[1., 1., 0.]]]).rosette(4, 90.) draw(F) drawNumbers(F) zoomAll() setDrawOptions(bbox=None) showDoc() pts = F.coords.reshape(-1, 3) draw(simple.circle(2, 4), color=yellow, linewidth=4) for degree, c in zip(list(range(1, 4)), [black, red, green]): N = NurbsCurve(pts, degree=degree, closed=True) draw(N, color=c) drawThePoints(N, 16, color=c) for w, c in zip([sqrt(2.), sqrt(2.) / 2., 0.25, 0.], [blue, cyan, magenta, white]): wts = array([1., w] * 4).reshape(8, 1) pts4 = Coords4(pts) pts4.deNormalize(wts) pts4 = Coords4(concatenate([pts4, pts4[:1]], axis=0)) N = NurbsCurve(pts4, degree=2, closed=False, blended=False) draw(N, color=c) drawThePoints(N, 16, color=c)
def drawCircles(sections, ctr, diam): """Draw circles as approximation of Formices.""" circle = simple.circle().rotate(-90, 1) cross = Formex(simple.Pattern['plus']).rotate(-90, 1) circles = [] n = len(sections) for i in range(n): C = cross.translate(ctr[i]) B = circle.scale(diam[i] / 2).translate(ctr[i]) S = sections[i] clear() draw(S, view='left', wait=False) draw(C, color='red', bbox=None, wait=False) draw(B, color='blue', bbox=None) circles.append(B) return circles
def run(): reset() clear() flat() line1 = circle(30.) line2 = line1.trl(0, 0.4) # Find the intersection of the segments X, w1, w2 = intersection(line1, line2) # Change the color of the intersecting segments line1.setProp(1).prop[w1] = 5 line2.setProp(3).prop[w2] = 4 draw([line1, line2], linewidth=3) draw(X, marksize=10, ontop=True)
def run(): resetAll() delay(1) linewidth(1) for i in [3, 4, 5, 6, 8, 12, 20, 60, 180]: #print "%s points" % i clear() draw(circle(360. / i, 360. / i), bbox=None) clear() draw(circle(360. / i, 2 * 360. / i), bbox=None) clear() draw(circle(360. / i, 360. / i, 180.), bbox=None) # Example of the use clear() n = 40 h = 0.5 line = Formex('l:' + '1' * n).scale(2. / n).translate([-1., 0., 0.]) curve = line.bump(1, [0., h, 0.], lambda x: 1. - x**2) curve.setProp(1) draw(line) draw(curve) # Create circles in the middle of each segment, with normal along the segment # begin and end points of segment A = curve.coords[:, 0, :] B = curve.coords[:, 1, :] # midpoint and segment director C = 0.5 * (B + A) D = B - A # vector initially normal to circle defined above nuc = array([0., 0., 1.]) # rotation angles and vectors ang, rot = rotationAngle(nuc, D) # diameters varying linearly with the |x| coordinate diam = 0.1 * h * (2. - abs(C[:, 0])) # finally, here are the circles: circles = [ circle().scale(d).rotate(a, r).translate(c) for d, r, a, c in zip(diam, rot, ang, C) ] F = Formex.concatenate(circles).setProp(3) draw(F) # And now something more fancy: connect 1 out of 15 points of the circles res = askItems([ ('Connect circles', True), ('Create Triangles', True), ('Fly Through', True), ]) if res: if res['Connect circles'] or res['Create Triangles']: conn = arange(0, 180, 15) if res['Connect circles']: G = Formex.concatenate([ connect([c1.select(conn), c2.select(conn)]) for c1, c2 in zip(circles[:-1], circles[1:]) ]) draw(G) if res['Create Triangles']: conn1 = concatenate([conn[1:], conn[:1]]) G = Formex.concatenate([ connect([c1.select(conn), c2.select(conn), c2.select(conn1)]) for c1, c2 in zip(circles[:-1], circles[1:]) ]) smooth() draw(G) if res['Fly Through']: flyAlong(curve, sleeptime=0.1) clear() flat() draw(line) draw(curve) draw(F)
def circle_example(): return simple.circle(5., 5.)
def draw_circles(circles, color=red): for r, c, n in circles: C = simple.circle(r=r, n=n, c=c) draw(C, color=color)
def run(): smoothwire() # GEOMETRICAL PARAMETERS FOR HE200B wide flange beam h = 200. #beam height b = 200. #flange width tf = 15. #flange thickness tw = 9. #body thickness l = 400. #beam length r = 18. #filling radius # MESH PARAMETERS el = 20 #number of elements along the length etb = 2 #number of elements over half of the thickness of the body ehb = 5 #number of elements over half of the height of the body etf = 5 #number of elements over the thickness of the flange ewf = 8 #number of elements over half of the width of the flange er = 6 #number of elements in the circular segment Body = simple.rectangle(etb, ehb, tw / 2., h / 2. - tf - r) Flange1 = simple.rectangle(er // 2, etf - etb, tw / 2. + r, tf - tw / 2.).translate( [0., h / 2. - (tf - tw / 2.), 0.]) Flange2 = simple.rectangle(ewf, etf - etb, b / 2. - r - tw / 2., tf - tw / 2.).translate( [tw / 2. + r, h / 2. - (tf - tw / 2.), 0.]) Flange3 = simple.rectangle(ewf, etb, b / 2. - r - tw / 2., tw / 2.).translate([tw / 2. + r, h / 2. - tf, 0.]) c1a = simple.line([0, h / 2 - tf - r, 0], [0, h / 2 - tf + tw / 2, 0], er // 2) c1b = simple.line([0, h / 2 - tf + tw / 2, 0], [tw / 2 + r, h / 2 - tf + tw / 2, 0], er // 2) c1 = c1a + c1b c2 = simple.circle(90. / er, 0., 90.).reflect(0).scale(r).translate( [tw / 2 + r, h / 2 - tf - r, 0]) Filled = simple.connectCurves(c2, c1, etb) Quarter = Body + Filled + Flange1 + Flange2 + Flange3 Half = Quarter + Quarter.reflect(1).reverse() Full = Half + Half.reflect(0).reverse() Section = Full.toMesh() clear() draw(Section, name='Section', color=red) #pause() method = ask("Choose extrude method:", [ 'Cancel', 'Sweep', 'Connect', 'Extrude', 'ExtrudeQuadratic', 'RevolveLoop', 'Revolve' ]) from pyformex import timer t = timer.Timer() if method == 'Sweep': path = simple.line([0, 0, 0], [0, 0, l], el).toCurve() Beam = Section.sweep(path, normal=[0., 0., 1.], upvector=[0., 1., 0.]) elif method == 'Connect': Section1 = Section.trl([0, 0, l]) Beam = Section.connect(Section1, el) elif method == 'Extrude': Beam = Section.extrude(el, dir=2, length=l) elif method == 'ExtrudeQuadratic': Section = Section.convert('quad9') Beam = Section.extrude(el, dir=2, length=l, degree=2) elif method == 'Revolve': Beam = Section.revolve(el, axis=1, angle=60., around=[-l, 0., 0.]) elif method == 'RevolveLoop': Beam = Section.revolve(el, axis=1, angle=240., around=[-l, 0., 0.], loop=True) else: return print("Computing: %s seconds" % t.seconds()) #print Beam.prop #print Beam.elems.shape t.reset() #clear() #draw(Beam,name='Beam',color='red',linewidth=2) draw(Beam.getBorderMesh(), name='Beam', color='red', linewidth=2) print("Drawing: %s seconds" % t.seconds()) export({'Beam': Beam})
def createGeometry(): global F # Construct a triangle of an icosahedron oriented with a vertex in # the y-direction, and divide its edges in n parts n = 3 # Add a few extra rows to close the gap after projection nplus = n+3 clear() smoothwire() view('front') # Start with an equilateral triangle in the x-y-plane A = simple.triangle() A.setProp(1) draw(A) # Modular size a, b, c = A.sizes() print("Cell width: %s; height: %s" % (a, b)) # Create a mirrored triangle B = A.reflect(1) B.setProp(2) draw(B) # Replicate nplus times in 2 directions to create triangular pattern F = A.replic2(1, nplus, a, -b, 0, 1, bias=-a/2, taper=1) G = B.replic2(1, nplus-1, a, -b, 0, 1, bias=-a/2, taper=1) clear() F += G draw(F) # Get the top vertex and make it the origin P = F[0, -1] draw(Formex([P]), bbox='last') F = F.translate(-P) draw(F) # Now rotate around the x axis over an angle so that the projection on the # x-y plane is an isosceles triangle with top angle = 360/5 = 72 degrees. # The base angles thus are (180-72)/2 = 54 degrees. # Ratio of the height of the isosceles triangle over the icosaeder edge length. c = 0.5*tand(54.) angle = arccosd(tand(54.)/sqrt(3.)) print("Rotation Ratio: %s; Angle: %s degrees" % (c, angle)) F = F.rotate(angle, 0) clear() draw(F, colormap=['black', 'magenta', 'yellow', 'black']) # Project it on the circumscribing sphere # The sphere has radius ru golden_ratio = 0.5 * (1. + sqrt(5.)) ru = 0.5 * a * sqrt(golden_ratio * sqrt(5.)) print("Radius of circumscribed sphere: %s" % ru) ru *= n C = [0., 0., -ru] F = F.projectOnSphere(ru, center=C) draw(F) hx, hy, h = F.sizes() print("Height of the dome: %s" % h) # The base circle goes through bottom corner of n-th row, # which will be the first point of the first triangle of the n-th row. # Draw the point to check it. i = (n-1)*n//2 P = F[i][0] draw(Formex([P]), marksize=10, bbox='last') # Get the radius of the base circle from the point's coordinates x, y, z = P rb = sqrt(x*x+y*y) # Give the base points a z-coordinate 0 F = F.translate([0., 0., -z]) clear() draw(F) # Draw the base circle H = simple.circle().scale(rb) draw(H) # Determine intersections with base plane P = [0., 0., 0.] N = [0., 0., 1.] newprops = [ 5, 6, 6, None, 4, None, None ] F = F.cutWithPlane(P, N, side='+', newprops=newprops)#,atol=0.0001) #clear() draw(F) # Finally, create a rosette to make the circle complete # and rotate 90 degrees to orient it like in the paper clear() F = F.rosette(5, 72.).rotate(90) def cutOut(F, c, r): """Remove all elements of F contained in a sphere (c,r)""" d = F.distanceFromPoint(c) return F.select((d < r).any(axis=-1) == False) # Cut out the door: remove all members having a point less than # edge-length a away from the base point p1 = [rb, 0., 0.] F = cutOut(F, p1, 1.1*a*n/6) # a was a good size with n = 6 # Scale to the real geometry scale = 7000. / F.sizes()[2] print("Applying scale factor %s " % scale) print(F.bbox()) F = F.scale(scale) print(F.bbox()) clear() draw(F, alpha=0.4) export({'F':F})
dxf.line(ent.coords) elif isinstance(ent, curve.Arc): dxf.arc(ent.getCenter(), ent.radius, ent.getAngles()) elif isinstance(ent, curve.PolyLine): dxf.polyline(ent.coords) else: utils.warn('warn_dxf_export', data=type(ent)) dxf.endSection() dxf.close() def exportDxfText(filename, parts): """Export a set of dxf entities to a .dxftext file.""" fil = open(filename, 'w') for p in parts: fil.write(p.dxftext() + '\n') fil.close() # An example if __name__ == '__draw__': #chdir(__file__) from pyformex.simple import circle c = circle(360. / 20., 360. / 20., 360.) draw(c) exportDXF('circle1.dxf', c) #End