def test_simple_glue(self): s1 = cm.box(2.0, 2.0, 2.0) s1.translate((-2.0, -1.0, -1.0)) s2 = cm.box(2.0, 2.0, 2.0) s2.translate((0.0, -1.0, -1.0)) f1 = s1.nearest('face', [(0.0, 0.0, 0.0)])[0] f2 = s2.nearest('face', [(0.0, 0.0, 0.0)])[0] s3 = cm.glue(s1, s2, [(f1, f2)]) v3 = s3.volume() self.assert_(close(v3, 2 * 2.0 * 2.0 * 2.0))
def test_chamfer_cut(self): # sphere had trouble s1 = cm.box(2.0, 2.0, 2.0) s1.translate((-1.0, -1.0, -1.0)) s2 = cm.box(4.0, 4.0, 4.0) s2.translate((0.0, -2.0, -2.0)) s3 = cm.chamfer_cut(s1, s2, 0.25) v3 = s3.volume() #print v3, 0.5*2.0*2.0*2.0 # empirical self.assert_(close(v3, 0.5 * 2.0 * 2.0 * 2.0 - 0.229, 0.1))
def boolean_glue(): s1 = cm.box(1.0, 2.0, 3.0) s2 = cm.box(1.0, 1.0, 1.0) s2.translate((1.0, 0.5, 1.0)) faces_s1 = s1.subshapes('face') faces_s2 = s2.subshapes('face') i1 = s1.nearest('face', [(1.0, 1.0, 1.5)])[0] i2 = s2.nearest('face', [(1.0, 1.0, 1.5)])[0] print 'glue faces', i1, i2 s3 = cm.glue(s1, s2, [(i1, i2)]) save_iso(s3, 'boolean_glue.png')
def logging_solid_fillet(): s1 = cm.box(1.0, 1.0, 1.0) s1.fillet(0.25, [(1.0, 0.5, 0.0), (1.0, 0.5, 1.0), (1.0, 0.0, 0.5), (1.0, 1.0, 0.5)]) save_iso(s1, 'logging_solid_fillet.png')
def logging_solid_chamfer(): s1 = cm.box(1.0, 1.0, 1.0) s1.chamfer(0.25, [(1.0, 0.5, 0.0), (1.0, 0.5, 1.0), (1.0, 0.0, 0.5), (1.0, 1.0, 0.5)]) save_iso(s1, 'logging_solid_chamfer.png')
def cube_face(): s1 = cm.box(1.0, 1.0, 1.0) subs = s1.subshapes('face') #_cube_subs(s1, subs, 'cube_face.png') _cube_sub(s1, subs[s1.nearest('face', [(1.0, 0.5, 0.5)])[0]], 'cube_face.png')
def cube_wire(): s1 = cm.box(1.0, 1.0, 1.0) faces = s1.subshapes('face') face = faces[s1.nearest('face', [(1.0, 0.5, 0.5)])[0]] w1 = face.subshapes('wire')[0] #_cube_subs(s1, subs, 'cube_wire.png') _cube_sub(s1, w1, 'cube_wire.png')
def cube_vertex(): s1 = cm.box(1.0, 1.0, 1.0) subs = s1.subshapes('vertex') #_cube_subs(s1, subs, 'cube_vertex.png') _cube_sub(s1, subs[s1.nearest('vertex', [(1.0, 0.0, 1.0)])[0]], 'cube_vertex.png')
def test_subshapes(self): s1 = cm.box(1.0, 2.0, 3.0).subshapes('shell')[0] fs = s1.subshapes('face') ws = s1.subshapes('wire') es = s1.subshapes('edge') vs = s1.subshapes('vertex') self.assert_(len(vs) == 8 and len(es) == 12 and len(ws) == 6 and len(fs) == 6)
def main(): s1 = cm.sphere(1.0) s2 = cm.box(1.0, 2.0, 3.0) s2.translate((2.0, 0.0, 0.0)) v1 = cd.view() v1.display(s1) v1.display(s2) cd.start()
def test_to_iges(self): s1 = cm.box(1.0, 2.0, 3.0).subshapes('shell')[0] r1 = s1.area() s1.to_iges('tmp.igs', brep_mode=1) s2 = cm.from_iges('tmp.igs') r2 = s2.area() self.assert_(close(r1, 22.0) and close(r2, 22.0))
def test_to_iges(self): s1 = cm.box(1.0, 2.0, 3.0) r1 = s1.volume() s1.to_iges('tmp.igs', brep_mode=1) s2 = cm.from_iges('tmp.igs') r2 = s2.volume() self.assert_(close(r1, 6.0) and close(r2, 6.0))
def test_to_step(self): s1 = cm.box(1.0, 2.0, 3.0).subshapes('shell')[0] r1 = s1.area() s1.to_step('tmp.stp') s2 = cm.from_step('tmp.stp') r2 = s2.area() self.assert_(close(r1, 22.0) and close(r2, 22.0))
def test_to_step(self): s1 = cm.box(1.0, 2.0, 3.0) r1 = s1.volume() s1.to_step('tmp.stp') s2 = cm.from_step('tmp.stp') r2 = s2.volume() self.assert_(close(r1, 6.0) and close(r2, 6.0))
def test_slice(self): b1 = cm.box(10.0, 10.0, 10.0) b1.translate((-5.0, -5.0, 0.0)) c1 = cm.cylinder(2.5, 20.0) c1.translate((0.0, 0.0, -5.0)) s1 = b1 - c1 f1 = cm.slice(s1, z=1.0)[0] self.assert_(close(100.0 - math.pi * 2.5 ** 2, f1.area(), 0.001))
def trms_translate(): s1 = cm.box(1.0, 2.0, 3.0) s2 = cm.translated(s1, (2.0, -6.0, 4.0)) v.viewstandard(viewtype='iso') v.display(s1, (1.0, 0.0, 0.0)) v.display(s2, (0.0, 0.0, 1.0)) v.fit() v.save('trms_translate.png') v.clear()
def display_display(): s1 = cm.sphere(1.0) + cm.box(1.0, 2.0, 2.0) v.display(s1, color=(1.0, 0.0, 0.0), material='metallized', transparency=0.5) v.fit() v.save('display_display.png') v.clear()
def test_simplify(self): s1 = cm.box(1.0, 1.0, 1.0) s2 = s1.copy() s2.translate((1.0, 0.5, 0.5)) s3 = s1 - s2 count1 = len(s3.subshapes('face')) s3.simplify() count2 = len(s3.subshapes('face')) self.assert_(count1 == 7 and count2 == 6)
def trms_mirror(): s1 = cm.box(1.0, 2.0, 3.0) s2 = cm.mirrored(s1, (0.0, 0.0, 0.0), (1.0, 0.0, 0.0)) v.viewstandard(viewtype='iso') v.display(s1, (1.0, 0.0, 0.0)) v.display(s2, (0.0, 0.0, 1.0)) v.fit() v.save('trms_mirror.png') v.clear()
def test_subcenters(self): s1 = cm.box(1.0, 2.0, 3.0) cs = s1.subcenters('face') center = [0.0, 0.0, 0.0] for c in cs: center[0] = center[0] + c[0] center[1] = center[1] + c[1] center[2] = center[2] + c[2] self.assert_(close(center, (6.0 * 0.5, 6.0 * 1.0, 6.0 * 1.5)))
def trms_rotate(): s1 = cm.box(1.0, 2.0, 3.0) s2 = cm.rotated(s1, (0.0, 0.0, 0.0), (0.0, 0.0, 1.0), math.pi / 2) v.viewstandard(viewtype='iso') v.display(s1, (1.0, 0.0, 0.0)) v.display(s2, (0.0, 0.0, 1.0)) v.fit() v.save('trms_rotate.png') v.clear()
def logging_solid_draft(): s1 = cm.box(1.0, 1.0, 1.0) s1.translate((-0.5, -0.5, 0.0)) face_centers = s1.subcenters('face') to_draft = [] for count, face_center in enumerate(face_centers): if abs(face_center[2] - 0.5) < 0.1: to_draft.append(count) s1.draft(math.radians(5.0), (0.0, 0.0, 1.0), (0.0, 0.0, 0.0), to_draft) save_iso(s1, 'logging_solid_draft.png')
def test_fillet_common(self): s1 = cm.sphere(1.0) s2 = cm.box(4.0, 4.0, 4.0) s2.translate((0.0, -2.0, -2.0)) s3 = cm.fillet_common(s1, s2, 0.25) v3 = s3.volume() #print v3, 0.5*4.0/3.0*math.pi*1.0**3 # empirical self.assert_(close(v3, 0.5 * 4.0 / 3.0 * math.pi * 1.0 ** 3 - 0.127, 0.1))
def logging_solid_simplify(): s1 = cm.box(1.0, 1.0, 1.0) s2 = s1.copy() s2.translate((1.0, 0.5, 0.5)) s3 = s1 - s2 print len(s3.subshapes('face')) save_iso(s3, 'logging_solid_simplify1.png') s3.simplify() print len(s3.subshapes('face')) save_iso(s3, 'logging_solid_simplify2.png')
def test_chamfer_fuse(self): s1 = cm.sphere(1.0) s2 = cm.box(4.0, 4.0, 4.0) s2.translate((0.0, -2.0, -2.0)) s3 = cm.chamfer_fuse(s1, s2, 0.25) v3 = s3.volume() #print v3, 4.0*4.0*4.0 + 0.5*4.0/3.0*math.pi*1.0**3 # empirical self.assert_(close(v3, 0.2 + 4.0 * 4.0 * 4.0 + 0.5 * 4.0 / 3.0 * math.pi * 1.0 ** 3, 0.1))
def test_draft(self): s1 = cm.box(1.0, 1.0, 1.0) s1.translate((-0.5, -0.5, 0.0)) face_centers = s1.subcenters('face') to_draft = [] for count, face_center in enumerate(face_centers): if abs(face_center[2] - 0.5) < 0.1: to_draft.append(count) s1.draft(math.radians(5.0), (0.0, 0.0, 1.0), (0.0, 0.0, 0.0), to_draft) #print 'draft', s1.volume() # empirical self.assert_(close(s1.volume(), 0.835, eps=0.001))
def derived_slice(): b1 = cm.box(10.0, 10.0, 10.0) b1.translate((-5.0, -5.0, 0.0)) c1 = cm.cylinder(2.5, 20.0) c1.translate((0.0, 0.0, -5.0)) s1 = b1 - c1 f1 = cm.slice(s1, z=1.0)[0] v.viewstandard(viewtype='top') v.display(f1) v.fit() v.save('derived_slice.png') v.clear()
def test_offset(self): w1 = cm.ngon(8.0, 6) f1 = cm.offset(cm.plane(w1), 1.0)[0] b1 = cm.box(10.0, 10.0, 10.0) b1.translate((-5.0, -5.0, 0.0)) c1 = cm.cylinder(2.5, 20.0) c1.translate((0.0, 0.0, -5.0)) s1 = b1 - c1 s2 = cm.offset(s1, 1.0)[0] # empirical self.assert_(close(217.418, f1.area(), 0.001) and close(1608.966, s2.volume(), 0.001))
def test_cut(self): s1 = cm.sphere(1.0) s2 = cm.box(2.0, 2.0, 2.0) s2.translate((0.0, -1.0, -1.0)) s3 = cm.cut(s1, s2) v3new = s3.volume() s3 = cm.old_cut(s1, s2) v3old = s3.volume() s3 = s1 - s2 v3op = s3.volume() value = 0.5 * 4.0 / 3.0 * math.pi * 1.0 ** 3 self.assert_(close(v3new, value) and close(v3old, value) and close(v3op, value))
def derived_offset_solid(): b1 = cm.box(10.0, 10.0, 10.0) b1.translate((-5.0, -5.0, 0.0)) c1 = cm.cylinder(2.5, 20.0) c1.translate((0.0, 0.0, -5.0)) s1 = b1 - c1 s2 = cm.offset(s1, 1.0)[0] v.viewstandard(viewtype='iso') v.display(s1, (1.0, 0.0, 0.0)) edges = s1.subshapes('edge') for edge in edges: v.display(edge, (0.0, 0.0, 0.0)) v.display(s2, transparency=0.5) v.fit() v.save('derived_offset_solid.png') v.clear()
def test_subtolerance(self): s1 = cm.box(1.0, 2.0, 3.0).subshapes('Shell')[0] subtols = s1.subtolerance() self.assert_(close(subtols, (1e-7, 1e-7, 1e-7), eps=1e-9))
def test_dump(self): s1 = cm.box(1.0, 2.0, 3.0).subshapes('Shell')[0] s1.dump() self.assert_(True)
def example1(): import numpy as np unit = 5.0 height = 10.0 draft = 1.0 # degrees of draft on faces for plastic ejection knob_rad = 1.8 # radius of the brick knob for mating with other bricks knob_height = 2.0 knob_draft = 5.0 # degrees of draft for the knob wall_thickness = 1.0 # plastic wall thickness fillet_rad = 0.4 # the default radius to use for rounded edges xsize = 4 ysize = 2 outerbrick = solidbrick(xsize, ysize, 0.0, unit, height, draft, knob_rad, knob_height, knob_draft, save_images=1) to_fillet = [] for count, edge_center in enumerate(outerbrick.subcenters('edge')): if (abs(edge_center[2]) < 0.1 or (abs(edge_center[2] - height) < 0.1 and abs(edge_center[0] - (0.5 * unit)) % unit < 0.1 and abs(edge_center[1] - (0.5 * unit)) % unit < 0.1)): pass else: to_fillet.append(count) outerbrick.fillet(fillet_rad, to_fillet) v.clear() v.display(outerbrick) v.fit() v.save('example1_outerbrick.png') innerbrick = solidbrick(xsize, ysize, wall_thickness, unit, height - wall_thickness, draft, knob_rad - wall_thickness, knob_height, knob_draft) base = cm.box(2 * unit * xsize, 2 * unit * ysize, 1.0) base.translate((-0.5 * unit * xsize, -0.5 * unit * ysize, -1.0)) innerbrick = innerbrick + base v.clear() v.display(innerbrick) v.fit() v.save('example1_innerbrick.png') to_fillet = [] for count, edge_center in enumerate(innerbrick.subcenters('edge')): if (abs(edge_center[2]) < 0.1 or (abs(edge_center[2] - (height - wall_thickness)) < 0.1 and abs(edge_center[0] - (0.5 * unit)) % unit < 0.1 and abs(edge_center[1] - (0.5 * unit)) % unit < 0.1)): to_fillet.append(count) innerbrick.fillet(fillet_rad, to_fillet) v.clear() v.display(innerbrick) v.fit() v.save('example1_innerbrickfillet.png') brick = outerbrick - innerbrick v.clear() v.display(brick) v.set_projection((0.0, 0.0, 0.0), (math.sqrt(0.45), -math.sqrt(0.1), -math.sqrt(0.45)), (0.0, -1.0, 0.0)) v.fit() v.save('example1_brick.png') post_rad = (math.sqrt(2.0) * unit - 2 * knob_rad) / 2.0 drad = (height - 0.5 * wall_thickness) * math.tan(math.radians(draft)) post_base_rad = post_rad + drad post = cm.cone(post_rad, post_base_rad, height - 0.5 * wall_thickness) post.fillet(fillet_rad, [(0.0, 0.0, 0.0)]) v.clear() v.display(post) v.viewstandard(viewtype='iso') v.fit() v.save('example1_post.png') post.translate((unit, unit, 0.0)) for x in range(xsize - 1): for y in range(ysize - 1): brick = brick + cm.translated(post, (x * unit, y * unit, 0.0)) v.clear() v.display(brick) v.viewstandard(viewtype='bottom') v.fit() v.save('example1_brickpost.png') brick_images.generate_images(4, 2)
def test_fix(self): s1 = cm.box(1.0, 2.0, 3.0) s1.fix() self.assert_(close(s1.center(), (0.5, 1.0, 1.5)))
def test_to_stl(self): s1 = cm.box(1.0, 2.0, 3.0) s1.to_stl('tmp.stl') self.assert_(True)
def test_nearest(self): s1 = cm.box(1.0, 2.0, 3.0) i1 = s1.nearest('Vertex', [(1.0, 2.0, 3.0)])[0] self.assert_( close(s1.subshapes('Vertex')[i1].center(), (1.0, 2.0, 3.0)))
# e = 15 # l = 20 # w = 30 input = {"e": 5, "l": 20, "w": 30} e = input["e"] l = input["l"] w = input["w"] hole_d = 2 hole_positions = ((l / 4, -w / 4), (l / 4, w / 4), (-l / 4, -w / 4), (-l / 4, w / 4)) plate = translated(box(l, w, e), (-l / 2, -w / 2, 0)) cylinders = list() for (x, y) in hole_positions: cylinders.append(translated(cylinder(hole_d / 2., e), (x, y, 0))) for c in cylinders: plate -= c __shape__ = plate.shape __anchors__ = dict() for i, (x, y) in enumerate(hole_positions, 1): __anchors__[str(i)] = { "p": (x, y, e),
def cube_edge(): s1 = cm.box(1.0, 1.0, 1.0) subs = s1.subshapes('edge') #_cube_subs(s1, subs, 'cube_edge.png') _cube_sub(s1, subs[s1.nearest('edge', [(1.0, 0.5, 1.0)])[0]], 'cube_edge.png')
def test_bounds(self): s1 = cm.box(1.0, 2.0, 3.0) self.assert_( close(s1.bounds(), (0.0, 0.0, 0.0, 1.0, 2.0, 3.0), eps=0.1))
def solid_box(): s1 = cm.box(1.0, 2.0, 3.0) save_iso(s1, 'solid_box.png')
def test_check(self): s1 = cm.box(1.0, 2.0, 3.0) self.assert_(s1.check())
def boolean_cut(): s1 = cm.sphere(1.0) s2 = cm.box(1.0, 2.0, 3.0) s3 = s1 - s2 save_iso(s3, 'boolean_cut.png')
def test_dump(self): s1 = cm.box(1.0, 2.0, 3.0) s1.dump() self.assert_(True)
def test_bounding_box(self): s1 = cm.box(2.0, 3.0, 4.0) # bbs1 = s1.bounding_box() _ = s1.bounding_box()
def test_subtolerance(self): s1 = cm.box(1.0, 2.0, 3.0) subtols = s1.subtolerance() self.assert_(close(subtols, (1e-7, 1e-7, 1e-7), eps=1e-9))
def test_box(self): s1 = cm.box(1.0, 2.0, 3.0) self.assert_(close(6.0, s1.volume()))
def cube_vertex(): s1 = cm.box(1.0, 1.0, 1.0) subs = s1.subshapes('vertex') # _cube_subs(s1, subs, 'cube_vertex.png') _cube_sub(s1, subs[s1.nearest('vertex', [(1.0, 0.0, 1.0)])[0]], 'cube_vertex.png')
def test_scaledz(self): s1 = cm.box(1.0, 2.0, 3.0) s2 = cm.scaledz(s1, 2.0) v2 = (1.0 * 1.0) * (2.0 * 1.0) * (3.0 * 2.0) self.assert_(close(v2, s2.volume()))
def boolean_fuse(): s1 = cm.sphere(1.0) s2 = cm.box(1.0, 2.0, 3.0) s3 = s1 + s2 save_iso(s3, 'boolean_fuse.png')
def test_bounds(self): s1 = cm.box(1.0, 2.0, 3.0).subshapes('Shell')[0] self.assert_( close(s1.bounds(), (0.0, 0.0, 0.0, 1.0, 2.0, 3.0), eps=0.1))
def boolean_common(): s1 = cm.sphere(1.0) s2 = cm.box(1.0, 2.0, 3.0) s3 = s1 & s2 save_iso(s3, 'boolean_common.png')
def test_check(self): s1 = cm.box(1.0, 2.0, 3.0).subshapes('Shell')[0] self.assert_(s1.check())
def test_fix(self): s1 = cm.box(1.0, 2.0, 3.0).subshapes('Shell')[0] s1.fix() self.assert_(close(s1.center(), (0.5, 1.0, 1.5)))
def cube_solid(): s1 = cm.box(1.0, 1.0, 1.0) # _cube_subs(s1, [], 'cube_solid.png') _cube_sub(s1, None, 'cube_solid.png')
def test_copy(self): s1 = cm.box(1.0, 2.0, 3.0) s2 = s1.copy() s1.translate((1.0, 1.0, 1.0)) self.assert_(close(s2.center(), (0.5, 1.0, 1.5)) and close(s1.center(), (1.5, 2.0, 2.5)))