[分享]激情奉献——齿轮的参数绘制!!
经常有人问,我也来搞搞!!!!可能有人会觉得用ACAD来做是自讨苦吃,没办法 !我只懂这个。利用VB.NET开发ACAD一实例改编而成,目前还不是很完善,欲打算添加斜齿轮,内齿轮等部分。希望大家一起学习和完善!!!!!'by gzy e-mail:gzy@mjtd.com
<BR> Dim 刀具 As Variant<BR> Dim Da, D0, D1, D2, D3, D4, n1, B, C As Double '齿数结构参数<BR> Dim Z, m, Af As Double '齿数、模数、压力角<BR> Const Pi = 3.141592 '圆周率
Private Sub CommandButton1_Click()<BR>clForm1.Hide
'设置三维视点<BR> Dim NewDirection(2) As Double<BR> NewDirection(0) = 1: NewDirection(1) = 0.5: NewDirection(2) = 0.5<BR> ThisDrawing.ActiveViewport.Direction = NewDirection<BR> ThisDrawing.ActiveViewport = ThisDrawing.ActiveViewport
ThisDrawing.Layers.Item(0).color = AutoCAD.AcColor.acBlue '颜色
ThisDrawing.SendCommand ("_Shademode" + vbCr + "_G" + vbCr) '着色 '
'齿轮输入参数<BR> Z = Val(Me.TextBox1.Text) '齿数<BR> m = Val(Me.TextBox2.Text) '模 数<BR> Af = Val(Me.TextBox3.Text) * Pi / 180 '压力角
'齿轮毛坯造型
Da = m * Z + 2 * m<BR> D4 = Val(Me.TextBox4.Text) '轴径<BR> D3 = Val(Me.TextBox7.Text)<BR> D0 = Val(Me.TextBox6.Text)<BR> D1 = (D0 + D3) / 2<BR> D2 = (D0 - D3) * 0.3
B = Val(Me.TextBox5.Text) '齿宽<BR> C = 0.2 * B<BR> n1 = 0.5 * m<BR> If Da <= 160 Then C = B: D0 = (Da + D4) / 2: D3 = D0: D2 = 0: Me.CheckBox1.Value = False
Dim plineObj(1) As AcadLWPolyline<BR> Dim points(19) As Double<BR> '定义2D多义线点<BR> points(0) = 0: points(1) = D4 / 2 '1点的X,Y坐标<BR> points(2) = B / 2 - n1: points(3) = points(1) '2点<BR> points(4) = points(2) + n1: points(5) = points(3) + n1 '3点<BR> points(6) = points(4): points(7) = D3 / 2 '4点<BR> points(8) = C / 2: points(9) = points(7) + n1 '5点<BR> points(10) = points(8): points(11) = D0 / 2 - n1 '6点<BR> points(12) = points(6): points(13) = points(11) + n1 '7点<BR> points(14) = points(12): points(15) = Da / 2 - n1 '8点<BR> points(16) = points(14) - n1: points(17) = points(15) + n1 '9点<BR> points(18) = 0: points(19) = points(17) '10点<BR> '创建AddLightWeightPolyline多义线<BR> Set plineObj(0) = ThisDrawing.ModelSpace.AddLightWeightPolyline(points)<BR> plineObj(0).Closed = True
'镜象1-10点围成的图形<BR> Dim point1(2) As Double<BR> Dim point2(2) As Double<BR> point1(0) = 0: point1(1) = 0: point1(2) = 0<BR> point2(0) = 0: point2(1) = 1: point2(2) = 0
Set plineObj(1) = plineObj(0).Mirror(point1, point2)
'创建为面域<BR> Dim regionObj As Variant<BR> regionObj = ThisDrawing.ModelSpace.AddRegion(plineObj)
'布尔加运算<BR> regionObj(0).Boolean AutoCAD.AcBooleanType.acUnion, regionObj(1)
'旋转面域<BR> Dim axisPt(2) As Double<BR> Dim axisDir(2) As Double<BR> Dim angle As Double<BR> axisPt(0) = 0: axisPt(1) = 0: axisPt(2) = 0<BR> axisDir(0) = 1: axisDir(1) = 0: axisDir(2) = 0<BR> angle = 2 * Pi
<BR> Dim solidObj As Acad3DSolid<BR> Set solidObj = ThisDrawing.ModelSpace.AddRevolvedSolid(regionObj(0), axisPt, axisDir, angle)
' ThisDrawing.ModelSpace.ZoomExtents
<BR> '为了造型方便,将得到的齿轮结构旋转体绕Y轴旋转90度。<BR> Dim rotatePt1(2) As Double<BR> Dim rotatePt2(2) As Double<BR> Dim rotateAngle As Double
rotatePt1(0) = 0: rotatePt1(1) = 0: rotatePt1(2) = 0<BR> rotatePt2(0) = 0: rotatePt2(1) = 1: rotatePt2(2) = 0<BR> rotateAngle = 90<BR> rotateAngle = rotateAngle * Pi / 180#
<BR> solidObj.Rotate3D rotatePt1, rotatePt2, rotateAngle
'键<BR> Dim boxObj As AutoCAD.Acad3DSolid<BR> Dim length As Double, width As Double, height As Double<BR> Dim center(2) As Double
center(0) = 0: center(1) = -D4 / 2: center(2) = 0<BR> length = D4 * 0.3: width = D4 * 0.3: height = B * 1.1
Set boxObj = ThisDrawing.ModelSpace.AddBox(center, length, width, height)<BR> solidObj.Boolean AutoCAD.AcBooleanType.acSubtraction, boxObj
Dim i As Integer
'腹板孔<BR> If Me.CheckBox1.Value = True Then<BR> Dim cylinderObj As Acad3DSolid<BR> Dim radius As Double<BR> center(0) = 0#: center(1) = D1 / 2: center(2) = 0#<BR> radius = D2 / 2<BR> height = C * 1.1<BR> Set cylinderObj = ThisDrawing.ModelSpace.AddCylinder(center, radius, height)
'环形阵列<BR> Dim retObj As Variant<BR> Dim basePnt(2) As Double<BR> basePnt(0) = 0: basePnt(1) = 0: basePnt(2) = 0#<BR> retObj = cylinderObj.ArrayPolar(7, 2 * Pi, basePnt)
For i = 0 To 5<BR> solidObj.Boolean AutoCAD.AcBooleanType.acSubtraction, retObj(i)<BR> Next i<BR> End If
Call 齿轮刀具
For i = 0 To Z - 1<BR> solidObj.Boolean AutoCAD.AcBooleanType.acSubtraction, 刀具(i)<BR> Next i
' 遍历模型空间的所有成员,删除非齿轮结构实体的一切实体<BR> For Each entry In ThisDrawing.ModelSpace<BR> If entry.ObjectID <> solidObj.ObjectID Then<BR> entry.Delete<BR> End If<BR> Next
solidObj.Update<BR> clForm1.Hide<BR>End Sub
Private Sub CommandButton2_Click()<BR>End<BR>End Sub
'齿轮结构造型<BR> Sub 齿轮刀具()<BR> Dim R, Rf, Rb, Ra As Single
R = m * Z / 2 '齿轮分度圆半径<BR> Rf = (R - 1.25 * m) '齿轮根圆半径<BR> Rb = R * Cos(Af) '齿轮基圆半径<BR> Ra = R + m ' 齿轮顶圆半径
'根据渐开线公式,计算渐开线上各点坐标<BR> Dim Sb, th(3)<BR> Sb = Cos(Af) * (3.14 * m / 2 + m * Z * (Tan(Af) - (Af))) ' 齿轮基圆齿厚<BR> th(1) = (3.14 * m * Cos(Af) - Sb) / (2 * Rb)<BR> th(0) = th(1) / 3<BR> th(2) = th(1) + Tan(Af) - Af<BR> th(3) = th(1) + Tan(Acos(Rb / Ra)) - Acos(Rb / Ra)
Dim curves(5) As AutoCAD.AcadEntity<BR> Dim points0(5) As Double<BR> Dim points1(8) As Double<BR> Dim points2(5) As Double
points0(0) = 0: points0(1) = Rf '第0点<BR> points0(2) = Rf * Sin(th(0)): points0(3) = Rf * Cos(th(0)) '第1点<BR> points0(4) = Rb * Sin(th(1)): points0(5) = Rb * Cos(th(1)) '第2点
Dim startTan(2) As Double<BR> Dim endTan(2) As Double<BR> startTan(0) = 0: startTan(1) = 0: startTan(2) = 0<BR> endTan(0) = 0.5: endTan(1) = 0.5: endTan(2) = 0
points1(0) = points0(4): points1(1) = points0(5): points1(2) = 0 '第2点<BR> points1(3) = R * Sin(th(2)): points1(4) = R * Cos(th(2)): points1(5) = 0 '第3点<BR> points1(6) = Ra * Sin(th(3)): points1(7) = Ra * Cos(th(3)): points1(8) = 0 '第4点
points2(0) = points1(6): points2(1) = points1(7) '第4点<BR> points2(2) = points1(6): points2(3) = points1(7) + 2.25 * m '第5点<BR> points2(4) = 0: points2(5) = points2(3) '第6点
'当基圆小于根圆,调整第1,第2点坐标,得到近似值<BR> If Rb < Rf Then<BR> points0(2) = points1(3) * 0.2: points0(3) = points0(1) + 0.25 * m * 0.03 '第1点<BR> points0(4) = points1(3) * 0.7: points0(5) = points0(1) + 0.25 * m * 0.8 '第2点<BR> points1(0) = points0(4): points1(1) = points0(5): points1(2) = 0 '第2点<BR> End If
'创建右部线段<BR> Set curves(0) = ThisDrawing.ModelSpace.AddLightWeightPolyline(points0) '由0,1,2点组成<BR> curves(0).SetBulge 1, 0.2 '第一点凸度为0.2<BR> Set curves(1) = ThisDrawing.ModelSpace.AddSpline(points1, startTan, endTan) '由2,3,4点组成<BR> Set curves(2) = ThisDrawing.ModelSpace.AddLightWeightPolyline(points2) '由4,5,6点组成
'镜像右部线段,得到左部线段<BR> Dim point1(2) As Double<BR> Dim point2(2) As Double<BR> point1(0) = 0: point1(1) = 0: point1(2) = 0<BR> point2(0) = 0: point2(1) = 1: point2(2) = 0<BR> Set curves(3) = curves(2).Mirror(point1, point2)<BR> Set curves(4) = curves(1).Mirror(point1, point2)<BR> Set curves(5) = curves(0).Mirror(point1, point2)
'创建面域<BR> 刀具 = ThisDrawing.ModelSpace.AddRegion(curves)<BR> '创建面域<BR> Dim taperAngle As Double<BR> taperAngle = 0<BR> Dim solidObj As AutoCAD.Acad3DSolid<BR> Set solidObj = ThisDrawing.ModelSpace.AddExtrudedSolid(刀具(0), B * 1.1, taperAngle)<BR> Dim center(2) As Double<BR> center(0) = 0: center(1) = solidObj.Centroid(1): center(2) = 0<BR> solidObj.Move solidObj.Centroid, center
'环形阵列<BR> Dim basePnt(2) As Double<BR> basePnt(0) = 0: basePnt(1) = 0: basePnt(2) = 0#<BR> 刀具 = solidObj.ArrayPolar(Z + 1, 2 * Pi, basePnt)<BR> End Sub
'齿数、模数改变,其他结构参数根据经验公式改变<BR>Private Sub TextBox1_Change()<BR>Me.TextBox4.Text = CInt(Val(Me.TextBox1.Text) * Val(Me.TextBox2.Text) * 0.3) '轴径D4<BR> D4 = Val(Me.TextBox4.Text)<BR> Me.TextBox5.Text = CInt(1.2 * Val(Me.TextBox4.Text)) '齿宽B<BR> Da = Val(Me.TextBox1.Text) * Val(Me.TextBox2.Text) + 2 * Val(Me.TextBox2.Text)<BR> Me.TextBox6.Text = Da - 12 * Val(Me.TextBox2.Text) 'D0<BR> Me.TextBox7.Text = 1.6 * D4 'D3<BR>End Sub
Private Sub UserForm_Initialize()<BR>Me.Caption = "齿轮结构参数化三维造型"<BR> <BR> Me.Label1.Caption = "齿数Z"<BR> Me.Label2.Caption = "模数m"<BR> Me.Label3.Caption = "压力角Af"<BR> Me.Label4.Caption = "轴径D4"<BR> Me.Label5.Caption = "齿宽B"<BR> Me.Label6.Caption = "D0"<BR> Me.Label7.Caption = "D3"
'赋初值<BR> Me.TextBox1.Text = 40 '齿数<BR> Me.TextBox2.Text = 6 '模数<BR> Me.TextBox3.Text = 20 '压力角<BR> Me.TextBox4.Text = CInt(Val(Me.TextBox1.Text) * Val(Me.TextBox2.Text) * 0.3) '轴径D4<BR> D4 = Val(Me.TextBox4.Text)<BR> Me.TextBox5.Text = CInt(1.2 * Val(Me.TextBox4.Text)) '齿宽B<BR> Da = Val(Me.TextBox1.Text) * Val(Me.TextBox2.Text) + 2 * Val(Me.TextBox2.Text)<BR> Me.TextBox6.Text = Da - 12 * Val(Me.TextBox2.Text) 'D0<BR> Me.TextBox7.Text = 1.6 * D4 'D3
Me.CheckBox1.Caption = "画腹板孔"<BR> Me.CheckBox1.Value = True '画腹板孔
Me.CommandButton1.Caption = "绘图"<BR> Me.CommandButton2.Caption = "退出"<BR>End Sub<BR>Function Acos(ByVal x As Double) As Double<BR> Acos = Atn(Sqr(1 - x * x) / x)<BR>End Function<BR>
效果如下图:
gzy的 VB.Net书到了?可以分享一下么? 我等的花儿也谢了!!!搞了我25天才搞来!!!!
当然可以共享啦!!光盘210M,压缩也是21M,放在论坛不大合适吧?
晚上跟老大讨论一下到FTP怎么样。不行的话我传给你。 谢了!我已经收到你的了! 收到,献花一朵以表谢意 呵呵,能否传一份给我?先谢了!
<A href="mailto:MinGoldFish@163.com" target="_blank" >MinGoldFish@163.com</A> 能否给我一份?<A href="mailto:michael5277@163.com" target="_blank" >michael5277@163.com</A>,谢谢! 好东西,期待您的下一版。 能不能给我一份 能不能给我传一个 zhoucj@fc18.com