Skip to content

Instantly share code, notes, and snippets.

@5263
Created May 31, 2012 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 5263/2842792 to your computer and use it in GitHub Desktop.
Save 5263/2842792 to your computer and use it in GitHub Desktop.
WIP: ISO261 Thread script for FreeCAD
import Part, FreeCAD, math
iso261pitchregular={1.0:0.25, 1.1:0.25, 1.2:0.25,
1.4:0.30,
1.6:0.35, 1.8:0.35,
2.0:0.4,
2.2:0.45, 2.5:0.45,
3.0:0.5,
3.5:0.6,
4.0:0.7,
4.5:0.75,
5.0:0.8,
6.0:1.0, 7.0:1.0,
8.0:1.25, 9.0:1.25,
10.0:1.5, 11.0:1.5,
12.0:1.75,
14.0:2.0, 16.0:2.0,
18.0:2.5, 20.0:2.5, 22.0:2.5,
24.0:3.0, 27.0:3.0,
30.0:3.5, 33.0:3.5,
36.0:4.0, 39.0:4.0,
42.0:4.5, 45.0:4.5,
48.0:5.0, 52.0:5.0,
56.0:5.5, 60.0:5.5,
64.0:6.0, 68.0:6.0}
unctpi= {0.073:64,0.086:56.0,0.099:48.0,0.112:40.0,0.125:40.0,
0.138:32.0,0.164:32.0,0.19:24.0,0.216:24.0,0.25:20.0,
0.3125:18.0,0.375:16.0,0.4375:14.0,0.5:13.0,0.5625:12.0,
0.625:11.0,0.75:10.0,0.875:9.0,1.0:8.0,1.125:7.0,1.25:7.0,
1.375:6.0,1.5:6.0,1.75:5.0,2.0:4.5,2.25:4.5,2.5:4.0,2.75:4.0,
3.0:4.0,3.25:4.0,3.5:4.0,3.75:4.0,4.0:4.0}
unftpi= {0.06:80.0,0.073:72.0,0.086:64.0,0.099:56.0,.112:48.0,
0.125:44.0,0.138:40.0,0.164:36.0,0.19:32.0,0.216:28.0,
0.25:28.0,0.3125:14.0,0.375:24.0,0.4375:20.0,0.5:20.0,
0.5625:18.0,0.625:18.0,0.75:16.0,0.875:14.0,1.0:12.0,
1.125:12.0,1.250:12.0,1.375:12.0,1.5:12.0}
uneftpi= {0.216:32.0,0.25:32.0,0.3125:32.0,0.375:32.0,0.4375:28.0,0.5:28.0,
0.5625:24.0,0.625:24.0,0.75:20.0,0.875:20.0,1.0:20.0,1.0625:18.0,
1.125:18.0,1.25:18.0,1.3125:18.0,1.375:18.0,1.4375:18.0,1.5:18.0,
1.5625:18.0,1.625:18.0,1.6875:18}
def imperial2metric(dia,tpi):
return dia*25.4,25.4/tpi
def makethreadiso261(len,d,p=None,external=True):
p=p or iso261pitchregular[d]
h=p*math.cos(math.radians(30))
r=d/2.0
pm2=(r-h*5.0/8.0,0,-p*3.0/8.0)
pm2i=(r-h*11.0/16.0,0,-p*7.0/16.0) #prevent coincident geometry
pm1=(r,0,-p/8.0)
pm1arc=(r+h*(1-math.cos(math.radians(30)))*0.12,0,0) #third point of the arc
pm1tri=(r+h/8.0,0,0.0) #reference point #needed to make a sharp edge instead of a arc
p0=(r,0,p/8.0)
p0i=(r+h/16.0,0,p/16.0) #between d and d+h/8 to prevent coincident geometry
p1=(r-h*5.0/8.0,0,p*3.0/8.0)
p1i=(r-h*11.0/16.0,0,p*7.0/16.0)
p1arc=(r-h*(5.0/8.0+(1-math.cos(math.radians(30)))*0.6),0,p/2.0) #third point of the arc
p1tri=(r-h*7.0/8.0,0,p/2.0) #needed to make a sharp edge instead of a arc
p2=(r-h*5.0/8.0,0,p*5.0/8.0)
p3= (r,0,p*7.0/8.0)
p3i=(r+h/16.0,0,p*15/16.0) #between d and d+h/8 to prevent coincident geometry
if external:
edge1 = Part.makeLine(p0i,p1)
#edge2 = Part.makeLine(p1,p2) #line instead of arc (part of internal thread shape)
edge2 = Part.Arc(FreeCAD.Vector(p1),FreeCAD.Vector(p1arc),FreeCAD.Vector(p2)).toShape()
edge3 = Part.makeLine(p2,p3i)
edge4 = Part.makeLine(p3i,p0i)
else: #internal
#todo replace coincitent points pm2,p1
edge1 = Part.makeLine(pm2i,pm1)
edge2 = Part.Arc(FreeCAD.Vector(pm1),FreeCAD.Vector(pm1arc),FreeCAD.Vector(p0)).toShape()
edge3 = Part.makeLine(p0,p1i)
edge4 = Part.makeLine(p1i,pm2i)
helix = Part.makeHelix(p,len,d,0)
section = Part.Wire([edge1,edge2,edge3,edge4])
makeSolid=1
isFrenet=1
pipe = Part.Wire(helix).makePipeShell([section],makeSolid,isFrenet)
return pipe
#Part.show(pipe)
#Part.show(helix)
#Part.show(section)
if __name__ == '__main__':
l,d,p=10,6,1
negextthreadshape=makethreadiso261(l,d,p,True)
doc=FreeCAD.ActiveDocument
thread=doc.addObject('Part::Feature','NegExtThread')
thread.Shape=negextthreadshape
#thread.Placement = App.Placement(App.Vector(0,0,p/2.0),App.Rotation())
cyl=doc.addObject("Part::Cylinder","Bolt")
cyl.Radius = d/2.0
cyl.Height = l
cut=doc.addObject("Part::Cut","ThreadedBolt")
cut.Base = cyl
cut.Tool = thread
cyl.ViewObject.hide()
thread.ViewObject.hide()
doc.recompute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment