'special note goto tools references and make sure GXExt8.0 Type Library is available
'It many be v7 v8 or v9 depending an what version you are running
Private Const PI = 3.1415927
Private Const degtorad = 0.0174532
Sub Thread()
'default 1/2 inch UNC 13 threads per inch
Dim strP As String
strP = InputBox("Pitch", "Screw Thread", 0.076923, 100, 100)
If strP = "" Then Exit Sub
Dim strD As String
strD = InputBox("Blank Diameter", "Screw Thread", 0.5, 100, 100)
If strD = "" Then Exit Sub
Dim strFA As String
strFA = InputBox("Flank Angle DEG", "Screw Thread", 30, 100, 100)
If strFA = "" Then Exit Sub
draw_screw True, 1#, CDbl(strP), CDbl(strD), CDbl(strFA)
End Sub
Sub HexNut()
'default 1/2 inch UNC 13 threads per inch
Dim strAF As String
strAF = InputBox("Nut Head A/F", "Screw Thread", 0.75, 100, 100)
If strAF = "" Then Exit Sub
Dim strH As String
strH = InputBox("Nut Head Thickness", "Screw Thread", 0.432, 100, 100)
If strH = "" Then Exit Sub
Dim strP As String
strP = InputBox("Pitch", "Screw Thread", 0.076923, 100, 100)
If strP = "" Then Exit Sub
Dim strD As String
strD = InputBox("Blank Diameter", "Screw Thread", 0.5, 100, 100)
If strD = "" Then Exit Sub
Dim strFA As String
strFA = InputBox("Flank Angle DEG", "Screw Thread", 30, 100, 100)
If strFA = "" Then Exit Sub
MinD = calcMinDia(CDbl(strP), CDbl(strD), CDbl(strFA))
Dim screwID As Long
Dim cylinID As Long
Dim headID As Long
screwID = draw_screw(True, CDbl(strH * 2), CDbl(strP), CDbl(strD), CDbl(strFA))
cylinID = draw3D_cylinder((MinD * 0.5) * 1.01, CDbl(strH * 2) + CDbl(strP))
Dim screwgraphic As Graphic
Set screwgraphic = ActiveDrawing.Graphics.GraphicFromID(screwID)
Dim cylingraphic As Graphic
Set cylingraphic = ActiveDrawing.Graphics.GraphicFromID(cylinID)
headID = draw3D_BoltHead(CDbl(strAF), -CDbl(strH))
Dim headgraphic As Graphic
Set headgraphic = ActiveDrawing.Graphics.GraphicFromID(headID)
headgraphic.MoveRelative 0, 0, CDbl(strH)
Dim nutter As New Boolean3D
Dim nutG As Graphic
Set nutG = nutter.Subtract(headgraphic, cylingraphic) '
ActiveDrawing.Graphics.AddGraphic nutter.Subtract(nutG, screwgraphic) '
screwgraphic.Delete
cylingraphic.Delete
headgraphic.Delete
Set screwgraphic = Nothing
Set cylingraphic = Nothing
Set headgraphic = Nothing
End Sub
Sub HexBolt()
'default 1/2 inch UNC 13 threads per inch
Dim headID As Long
Dim cylinID As Long
Dim screwID As Long
Dim MinD As Double
Dim headgraphic As Graphic
Dim screwgraphic As Graphic
Dim cylingraphic As Graphic
Dim strAF As String
strAF = InputBox("Hex Head A/F", "Screw Thread", 0.75, 100, 100)
If strAF = "" Then Exit Sub
Dim strH As String
strH = InputBox("Hex Head Thickness", "Screw Thread", 0.313, 100, 100)
If strH = "" Then Exit Sub
Dim strP As String
strP = InputBox("Pitch", "Screw Thread", 0.076923, 100, 100)
If strP = "" Then Exit Sub
Dim strD As String
strD = InputBox("Blank Diameter", "Screw Thread", 0.5, 100, 100)
Dim strFA As String
strFA = InputBox("Flank Angle DEG", "Screw Thread", 30, 100, 100)
If strFA = "" Then Exit Sub
Dim strL As String
strL = InputBox("Length", "Screw Thread", 1, 100, 100)
If strL = "" Then Exit Sub
MinD = calcMinDia(CDbl(strP), CDbl(strD), CDbl(strFA))
headID = draw3D_BoltHead(CDbl(strAF), CDbl(strH))
Set headgraphic = ActiveDrawing.Graphics.GraphicFromID(headID)
screwID = draw_screw(True, CDbl(strL), CDbl(strP), CDbl(strD), CDbl(strFA))
Set screwgraphic = ActiveDrawing.Graphics.GraphicFromID(screwID)
cylinID = draw3D_cylinder(MinD * 0.5, CDbl(strL) + CDbl(strP))
Set cylingraphic = ActiveDrawing.Graphics.GraphicFromID(cylinID)
Dim Bolter As New Boolean3D
Dim BoltG As Graphic
Set BoltG = Bolter.Add(headgraphic, cylingraphic) '
ActiveDrawing.Graphics.AddGraphic Bolter.Add(BoltG, screwgraphic) '
screwgraphic.Delete
cylingraphic.Delete
headgraphic.Delete
Set screwgraphic = Nothing
Set cylingraphic = Nothing
Set headgraphic = Nothing
Set Bolter = Nothing
Set BoltG = Nothing
End Sub
Private Function draw3D_cylinder(r As Double, L As Double) As Long
Dim App As Application
Dim ActDr As Drawing
Dim Grs As Graphics
Dim gr As Graphic
Dim Gr1 As Graphic
Dim Ver As Vertex
Set App = IMSIGX.Application
Set ActDr = App.ActiveDrawing
Set Grs = ActDr.Graphics
Set gr = Grs.Add(, "TCW40EXTRUDE") ' add the graphic of type extrude
gr.Properties("$PIPE") = 1 ' set this property to 1 to define normal path for extrude
Set Gr1 = Grs.AddCircleCenterAndPoint(0, 0, 0, r, 0, 0)
Set Gr1 = Grs.Remove(Gr1.Index) ' remove the base graphic from Graphics collection of the drawing
gr.Graphics.AddGraphic Gr1 ' add the base grahic to graphics collection of extrude
Set Ver = gr.Vertices.Add(0, 0, 0)
Set Ver = Nothing
Set Ver = gr.Vertices.Add(0, 0, L)
Set Ver = Nothing
draw3D_cylinder = gr.ID
Set Gr1 = Nothing
Set gr = Nothing
Set Grs = Nothing
Set ActDr = Nothing
Set App = Nothing
End Function
Private Function draw3D_BoltHead(af As Double, h As Double) As Long
Dim r As Double
r = 0.5513289 * af
Dim App As Application
Dim ActDr As Drawing
Dim Grs As Graphics
Dim gr As Graphic
Dim Gr1 As Graphic
Dim Ver As Vertex
Set App = IMSIGX.Application
Set ActDr = App.ActiveDrawing
Set Grs = ActDr.Graphics
Set gr = Grs.Add(, "TCW40EXTRUDE") ' add the graphic of type extrude
gr.Properties("$PIPE") = 1 ' set this property to 1 to define normal path for extrude
Set Gr1 = Grs.AddLinePolygon(0, 0, 0, r, 0, 0, 6)
Set Gr1 = Grs.Remove(Gr1.Index) ' remove the base graphic from Graphics collection of the drawing
gr.Graphics.AddGraphic Gr1 ' add the base grahic to graphics collection of extrude
Set Ver = gr.Vertices.Add(0, 0, 0)
Set Ver = Nothing
Set Ver = gr.Vertices.Add(0, 0, -h)
Set Ver = Nothing
draw3D_BoltHead = gr.ID
Set Gr1 = Nothing
Set gr = Nothing
Set Grs = Nothing
Set ActDr = Nothing
Set App = Nothing
End Function
Private Function calcMinDia(Pitch As Double, OD As Double, flankAngle As Double) As Double
Dim h As Double
h = Pitch / (2 * Tan(flankAngle * degtorad))
calcMinDia = OD - 2 * h
End Function
Private Function draw_screw(RightHanded As Boolean, Length As Double, Pitch As Double, OD As Double, flankAngle As Double) As Long
Dim rotang As Double
Dim P As Double 'pitch
Dim theta As Double 'flank angle
Dim S2 As Double 'Depth of truncation of triangular height at root
Dim S1 As Double 'Depth of truncation of triangular height at crest
Dim r2 As Double 'Root radius
Dim r1 As Double 'crest radius
Dim E As Double 'Effective Diameter
Dim n As Double 'Number of starts
Dim D As Double 'Major Diameter
Dim dmin As Double 'Minor Diameter
Dim h As Double 'height of thread
'D = E + 0.5 * P * cot(theta) - 2 * S1
'dmin = E - 0.5 * P * cot(theta) + 2 * S2
'h = 0.5 * P * cot(theta) - (S1 + S2)
D = OD
theta = flankAngle
P = Pitch
h = P / (2 * Tan(theta * degtorad))
dmin = D - 2 * h
rotang = (Length / P) * 360
Dim App As Application
Dim ActDr As Drawing
Dim Grs As Graphics
Dim gr As Graphic
Dim Gr1 As Graphic
Dim Ver As Vertex
Set App = IMSIGX.Application
Set ActDr = App.ActiveDrawing
Set Grs = ActDr.Graphics
Set gr = Grs.Add(, "TCW40SPIN") '
gr.Properties("$ROTATIONANGLE") = rotang 'deg
gr.Properties("$STEP") = P + P * 0.0001 'pitch
If RightHanded = True Then gr.Properties("$HANDINESS") = 0 Else gr.Properties("$HANDINESS") = 1
gr.Properties("$COILNUMBER") = rotang / 360 'number of coils
'screw axis
Set Ver = gr.Vertices.Add(0, 0, 0)
Set Ver = Nothing
Set Ver = gr.Vertices.Add(100, 0, 0)
Set Ver = Nothing
Set Gr1 = Grs.Add(, "polyline")
Set Ver = Gr1.Vertices.Add(0, dmin * 0.5, 0)
Set Ver = Nothing
Set Ver = Gr1.Vertices.Add(P * 0.5, D * 0.5, 0)
Set Ver = Nothing
Set Ver = Gr1.Vertices.Add(P, dmin * 0.5, 0)
Set Ver = Nothing
Set Ver = Gr1.Vertices.AddClose
Set Ver = Nothing
Set Gr1 = Grs.Remove(Gr1.Index)
gr.Graphics.AddGraphic Gr1
gr.RotateAxis PI / 2, 0, -1000, 0, 0, 1000, 0
draw_screw = gr.ID
Set Gr1 = Nothing
Set gr = Nothing
Set Grs = Nothing
Set ActDr = Nothing
Set App = Nothing
End Function