drawing

LEGO component for 3d printing

在Rhino3D軟體裡用python程式語言 創造客制化的樂高原件。 樂高元件設計 LEGO customized components developed through Python script using Rhino 3D software. We can design our own lego components via 3d printer. Mouse left click => make LEGO up part. Mouse left click => make LEGO down part.

drawing

Method:

Basically, the methodology is quite simple. 1. Replace any surface with lego unit. We can find its dimensions here. 2. Trim a hole. 3. Extrude surfaces. 4. Join all faces.

LegoFaceLeft UP v0.001

import rhinoscriptsyntax as rs

objSelect = rs.GetObject("Object", 16)
faces = rs.ExplodePolysurfaces(objSelect)
faceSelect = rs.GetObject("face", 8, rs.filter.surface)

border = rs.DuplicateSurfaceBorder(faceSelect)
borderLines = rs.ExplodeCurves(border, True)

U = rs.GetObject("UV select_U", 4)
V = rs.GetObject("UV select_v", 4)

uLen = rs.CurveLength(U)
vLen = rs.CurveLength(V)

udomain = rs.SurfaceDomain(faceSelect, 0)
vdomain = rs.SurfaceDomain(faceSelect, 1)

head = []

rs.EnableRedraw(False)

u = udomain[0]+4
while u<=udomain[1]-2.75:
    v = vdomain[0]+4
    while v<=vdomain[1]-2.75:
        pt = rs.EvaluateSurface(faceSelect, u, v)
        if rs.Distance(pt, rs.BrepClosestPoint(faceSelect, pt)[0])<0.1:
            srf_frame = rs.SurfaceFrame(faceSelect, [u, v])
            circle = rs.AddCircle(srf_frame, 2.45)
            head.append(circle)
        v+=8
    u+=8

UendPt = rs.CurveEndPoint(U)
UStPt = rs.CurveStartPoint(U)
UVector = rs.VectorCreate(UendPt, UStPt)

VendPt = rs.CurveEndPoint(V)
VStPt = rs.CurveStartPoint(V)
VVector = rs.VectorCreate(VendPt, VStPt)
path = rs.VectorUnitize(rs.VectorCrossProduct(UVector, VVector))

extrudeFace = []

cut = rs.AddLine( [0, 0 ,0 ], [ path[0]*1.7, path[1]*1.7, path[2]*1.7 ]   )

for i in range(len(head)):

    cutter = rs.ExtrudeCurve(head[i], cut)
    rs.MoveObject(cutter, [ -path[0]*1.7, -path[1]*1.7, -path[2]*1.7 ] )
    extrudeFace.append(cutter)
    f = rs.TrimBrep(faceSelect, cutter)

body = rs.JoinSurfaces(faces, f)
extrudeFace.append(body)
preFinal = rs.JoinSurfaces(extrudeFace)
rs.CapPlanarHoles(preFinal)

rs.DeleteObjects(faces)
rs.DeleteObjects(border)
rs.DeleteObject(f)
rs.DeleteObject(body)
rs.DeleteObject(cut)
rs.DeleteObjects(circle)
rs.DeleteObjects(objSelect)
rs.DeleteObjects(borderLines)

for j in range(len(extrudeFace)):
    rs.DeleteObject(extrudeFace[j])

for k in range(len(head)):
    rs.DeleteObject(head[k])

rs.EnableRedraw(True)

LegoFaceRight DOWN v0.001

import rhinoscriptsyntax as rs

objSelect = rs.GetObject("Object", 16)
faces = rs.ExplodePolysurfaces(objSelect)
faceSelect = rs.GetObject("face", 8, rs.filter.surface)

border = rs.DuplicateSurfaceBorder(faceSelect)
rs.HideObject(border)
borderLines = rs.ExplodeCurves(border, True)

U = rs.GetObject("UV select_U", 4)
V = rs.GetObject("UV select_v", 4)

uLen = rs.CurveLength(U)
vLen = rs.CurveLength(V)

udomain = rs.SurfaceDomain(faceSelect, 0)
vdomain = rs.SurfaceDomain(faceSelect, 1)

headIn = []
headOut = []
headSrf = []
head = []

rs.EnableRedraw(False)

UendPt = rs.CurveEndPoint(U)
UStPt = rs.CurveStartPoint(U)
UVector = rs.VectorCreate(UendPt, UStPt)

VendPt = rs.CurveEndPoint(V)
VStPt = rs.CurveStartPoint(V)
VVector = rs.VectorCreate(VendPt, VStPt)
path = rs.VectorUnitize(rs.VectorCrossProduct(UVector, VVector))

inBoxLine = rs.OffsetCurveOnSurface(border, faceSelect, 1.5)  #1.6
cutter = rs.AddLine(  [0, 0 ,0 ], [ path[0]*1.7, path[1]*1.7, path[2]*1.7 ] ) #3.2
inBox = rs.ExtrudeCurve(inBoxLine, cutter)

inBoxCap = rs.CapPlanarHoles(inBox)

body = rs.BooleanDifference( objSelect , inBox)

u = udomain[0]+8
while u<=udomain[1]-(8-1.6):
    v = vdomain[0]+8
    while v<=vdomain[1]-(8-1.6):
        pt = rs.EvaluateSurface(faceSelect, u, v)
        if rs.Distance(pt, rs.BrepClosestPoint(faceSelect, pt)[0])<0.1:
            srf_frame = rs.SurfaceFrame(faceSelect, [u, v])
            circleIn = rs.AddCircle(srf_frame, 3.1)  #3.1568 , 3.25
            circleOut = rs.AddCircle(srf_frame, 2.45)    #2.4
            headIn.append(rs.ExtrudeCurve( circleIn , cutter ))
            headOut.append(rs.ExtrudeCurve( circleOut , cutter ))
            lo = rs.AddLoftSrf([circleIn, circleOut])
            headSrf.append(lo)
            rs.DeleteObject(circleIn)
            rs.DeleteObject(circleOut)
        v+=8
    u+=8

final = []

for i in range(len(headSrf)):
    head.append(headIn[i])
    head.append(headOut[i])
    head.append(headSrf[i])
    headId = rs.JoinSurfaces(head)
    head.remove(headIn[i])
    head.remove(headOut[i])
    head.remove(headSrf[i])
    rs.CapPlanarHoles(headId)
    final.append(headId)

final.append(body)
rs.BooleanUnion(final)

rs.DeleteObjects(cutter )
rs.DeleteObjects(faces)
rs.DeleteObject(faceSelect)
rs.DeleteObject(border)
rs.DeleteObjects(borderLines)
rs.DeleteObject(inBoxLine)

for j in range(len(headIn)):
    rs.DeleteObject(headIn[j])

for j in range(len(headOut)):
    rs.DeleteObject(headOut[j])

for j in range(len(headSrf)):
    rs.DeleteObject(headSrf[j])

rs.EnableRedraw(True)