本课题是刀具半径补偿的直线-直线软件实现,是南京工程学院数控专业的数控原理与系统的课程设计,整个工程都是自己随便弄弄的,作为课设作业还算比较严谨吧。题目不难,具体的直线-直线的刀具补偿原理,见机械工业出版社出版的汪木兰主编的《数控原理与系统》的P56~P59,这里不再赘述,具体的代码贴在下边,另外附件中有zip包附带整个工程文件(界面、工程、代码)。具体实现都很简单,因为学期末了,找工作的找工作,考研复试的复试,大多都没兴趣好好弄了,在这里贴上来方便大家交差把。
具体思路稍微一提,对给定的直线的3个点,首先在界面中选择刀补阶段,到底是刀补建立,还是刀补进行,还是刀补撤销,进一步通过直线投影判断两直线夹角,分为伸长型,缩短型,插入型三种情况,分别予以讨论,计算出补偿后的刀具轨迹,然后加以在PICTUREbox中显示。
VB工程代码:
Public X0 As Double, Y0 As Double, X1 As Double, Y1 As Double, X2 As Double, Y2 As Double, R As Double Public Xs1 As Double, Ys1 As Double, Xs2 As Double, Ys2 As Double Public Ori As Integer Public Xl1 As Double, Yl1 As Double, Xl2 As Double, Yl2 As Double, dX1 As Double, dY1 As Double, dX2 As Double, dY2 As Double, d1 As Double, d2 As Double Private Sub Command1_Click() Dim X1_FWD As Integer, Y1_FWD As Integer, X2_FWD As Integer, Y2_FWD As Integer Dim alfa As Double, beta As Double Call PaintAxis '绘制补偿前图像 Picture1.ForeColor = vbBlue Picture1.DrawWidth = 1 Picture1.Line (X0, Y0)-(X1, Y1) Picture1.Line (X1, Y1)-(X2, Y2) '算法设计 '计算坐标增量 dX1 = X1 - X0 dY1 = Y1 - Y0 dX2 = X2 - X1 dY2 = Y2 - Y1 alfa = Atn(dY1 / dX1) beta = Atn(dY2 / dX2) If dX1 >= 0 Then X1_FWD = 1 Else X1_FWD = -1 End If If dX2 >= 0 Then X2_FWD = 1 Else X2_FWD = -1 End If If dY1 >= 0 Then Y1_FWD = 1 Else Y1_FWD = -1 End If If dY2 >= 0 Then Y2_FWD = 1 Else Y2_FWD = -1 End If '计算d1,d2 d1 = Sqr(dX1 ^ 2 + dY1 ^ 2) d2 = Sqr(dX2 ^ 2 + dY2 ^ 2) '计算方向矢量投影 Xl1 = dX1 / d1 Yl1 = dY1 / d1 Xl2 = dX2 / d2 Yl2 = dY2 / d2 '判断缩短型,伸长型,插入型 If Ori * (Yl2 * Xl1 - Xl2 * Yl1) >= 0 Then '缩短型 '刀补建立 If Combo1.ListIndex = 0 And Ori * (Yl2 * Xl1 - Xl2 * Yl1) <> 0 Then Xs1 = X1 - R * Ori * Yl2 Ys1 = Y1 + R * Ori * Xl2 X_0p.Text = X0 Y_0p.Text = Y0 X_s1.Text = Xs1 Y_s1.Text = Ys1 X_2p.Text = Xs1 + dX2 Y_2p.Text = Ys1 + dY2 Picture1.ForeColor = vbMagenta Picture1.Line (X0, Y0)-(Xs1, Ys1) Picture1.Line (Xs1, Ys1)-(Xs1 + dX2, Ys1 + dY2) '刀补进行 ElseIf Combo1.ListIndex = 1 Then If Yl2 * Xl1 - Xl2 * Yl1 = 0 Then 'l1与l2共线 Xs1 = X1 - R * Ori * Yl1 Ys1 = Y1 + R * Ori * Xl1 X_0p.Text = Xs1 - dX1 Y_0p.Text = Ys1 - dY1 X_s1.Text = Xs1 Y_s1.Text = Ys1 X_2p.Text = Xs1 + dX2 Y_2p.Text = Ys1 + dY2 Picture1.ForeColor = vbMagenta Picture1.Line (Xs1 - dX1, Ys1 - dY1)-(Xs1, Ys1) Picture1.Line (Xs1, Ys1)-(Xs1 + dX2, Ys1 + dY2) Else ' l1与l2不共线 Xs1 = X1 + (Xl2 - Xl1) * Ori * R / (Xl1 * Yl2 - Xl2 * Yl1) Ys1 = Y1 + (Yl2 - Yl1) * Ori * R / (Xl1 * Yl2 - Xl2 * Yl1) X_0p.Text = Xs1 - dX1 Y_0p.Text = Ys1 - dY1 X_s1.Text = Xs1 Y_s1.Text = Ys1 X_2p.Text = Xs1 + dX2 Y_2p.Text = Ys1 + dY2 Picture1.ForeColor = vbMagenta Picture1.Line (Xs1 - dX1, Ys1 - dY1)-(Xs1, Ys1) Picture1.Line (Xs1, Ys1)-(Xs1 + dX2, Ys1 + dY2) End If '刀补撤销 ElseIf Combo1.ListIndex = 2 And Ori * (Yl2 * Xl1 - Xl2 * Yl1) <> 0 Then Xs1 = X1 - R * Ori * Yl1 Ys1 = Y1 + R * Ori * Xl1 X_0p.Text = Xs1 - dX1 Y_0p.Text = Ys1 - dY1 X_s1.Text = Xs1 Y_s1.Text = Ys1 X_2p.Text = X2 Y_2p.Text = Y2 Picture1.ForeColor = vbMagenta Picture1.Line (Xs1 - dX1, Ys1 - dY1)-(Xs1, Ys1) Picture1.Line (Xs1, Ys1)-(X2, Y2) End If ElseIf Ori * (Yl2 * Xl1 - Xl2 * Yl1) < 0 And (Yl2 * Yl1 + Xl2 * Xl1) >= 0 Then '伸长型 '刀补建立 If Combo1.ListIndex = 0 Then '第一对转接点 Xs1 = X1 - R * Ori * Yl1 Ys1 = Y1 + R * Ori * Yl1 '第二对转接点 Xs2 = X1 + (Xl2 - Xl1) * R * Ori / (Xl1 * Yl2 - Xl2 * Yl1) Ys2 = Y1 + (Yl2 - Yl1) * R * Ori / (Xl1 * Yl2 - Xl2 * Yl1) '输出坐标 'X0',Y0' X_0p.Text = X0 Y_0p.Text = Y0 'Xs1,Ys1 X_s1.Text = Xs1 Y_s1.Text = Ys1 'Xs2,Ys2 X_s2.Text = Xs2 Y_s2.Text = Ys2 'X2' Y2' X_2p.Text = Xs2 + dX2 Y_2p.Text = Ys2 + dY2 '绘图 Picture1.ForeColor = vbMagenta Picture1.Line (X0, Y0)-(Xs1, Ys1) Picture1.Line (Xs1, Ys1)-(Xs2, Ys2) Picture1.Line (Xs2, Ys2)-(Xs2 + dX2, Ys2 + dY2) '刀补进行 ElseIf Combo1.ListIndex = 1 Then Xs1 = X1 + (Xl2 - Xl1) * R * Ori / (Xl1 * Yl2 - Xl2 * Yl1) Ys1 = Y1 + (Yl2 - Yl1) * R * Ori / (Xl1 * Yl2 - Xl2 * Yl1) '输出坐标 'X0',Y0' X_0p.Text = X0 Y_0p.Text = Y0 'Xs1,Ys1 X_s1.Text = Xs1 Y_s1.Text = Ys1 'X2' Y2' X_2p.Text = Xs2 + dX2 Y_2p.Text = Ys2 + dY2 '绘图 Picture1.ForeColor = vbMagenta Picture1.Line (Xs1 - dX1, Ys1 - dY1)-(Xs1, Ys1) Picture1.Line (Xs1, Ys1)-(Xs1 + dX2, Ys1 + dY2) '刀补撤销 ElseIf Combo1.ListIndex = 2 Then Xs1 = X1 + (Xl2 - Xl1) * R * Ori / (Xl1 * Yl2 - Xl2 * Yl1) Ys1 = Y1 + (Yl2 - Yl1) * R * Ori / (Xl1 * Yl2 - Xl2 * Yl1) Xs2 = X1 - R * Ori * Yl2 Ys2 = Y1 + R * Ori * Xl2 '输出坐标 'X0',Y0' X_0p.Text = Xs1 - dX1 Y_0p.Text = Ys1 - dY1 'Xs1,Ys1 X_s1.Text = Xs1 Y_s1.Text = Ys1 'Xs2,Ys2 X_s2.Text = Xs2 Y_s2.Text = Ys2 'X2' Y2' X_2p.Text = X2 Y_2p.Text = Y2 '绘图 Picture1.ForeColor = vbMagenta Picture1.Line (Xs1 - dX1, Ys1 - dY1)-(Xs1, Ys1) Picture1.Line (Xs1, Ys1)-(Xs2, Ys2) Picture1.Line (Xs2, Ys2)-(X2, Y2) End If ElseIf Ori * (Yl2 * Xl1 - Xl2 * Yl1) < 0 And (Yl2 * Yl1 + Xl2 * Xl1) < 0 Then '插入型 '刀补建立 If Combo1.ListIndex = 0 Then '第一对转接点 Xs1 = X1 - R * Ori * Yl1 Ys1 = Y1 + R * Ori * Xl1 '第二对转接点 Xs2 = X1 - Ori * R * Yl1 + R * Xl1 Ys2 = Y1 + Ori * R * Xl1 + R * Yl1 '第三对转接点 Xs3 = X1 - R * Ori * Yl2 - R * Xl2 Ys3 = Y1 + R * Ori * Xl2 - R * Yl2 '输出坐标 'X0',Y0' X_0p.Text = X0 Y_0p.Text = Y0 'Xs1,Ys1 X_s1.Text = Xs1 Y_s1.Text = Ys1 'Xs2,Ys2 X_s2.Text = Xs2 Y_s2.Text = Ys2 'Xs3,Ys3 X_s3.Text = Xs3 Y_s3.Text = Ys3 'X2' Y2' X_2p.Text = Xs3 + dX2 + Abs(R * Cos(beta)) * X2_FWD Y_2p.Text = Ys3 + dY2 + Abs(R * Sin(beta)) * X2_FWD '绘图 Picture1.ForeColor = vbMagenta Picture1.Line (X0, Y0)-(Xs1, Ys1) Picture1.Line (Xs1, Ys1)-(Xs2, Ys2) Picture1.Line (Xs2, Ys2)-(Xs3, Ys3) Picture1.Line (Xs3, Ys3)-(Xs3 + dX2 + Abs(R * Cos(beta)) * X2_FWD, Ys3 + dY2 + Abs(R * Sin(beta)) * Y2_FWD) '刀补进行 ElseIf Combo1.ListIndex = 1 Then '第一对转接点 Xs1 = X1 - R * Ori * Yl1 + R * Xl1 Ys1 = Y1 + R * Ori * Xl1 + R * Yl1 '第二对转接点 Xs2 = X1 - R * Ori * Yl2 - R * Xl2 Ys2 = Y1 + R * Ori * Xl2 - R * Yl2 '输出坐标 'X0',Y0' X_0p.Text = Xs1 - dX1 - Abs(R * Cos(alfa)) * X1_FWD Y_0p.Text = Ys1 - dY1 - Abs(R * Sin(alfa)) * Y1_FWD 'Xs1,Ys1 X_s1.Text = Xs1 Y_s1.Text = Ys1 'Xs2,Ys2 X_s2.Text = Xs2 Y_s2.Text = Ys2 'X2' Y2' X_2p.Text = Xs2 + dX2 + Abs(R * Cos(beta)) * X2_FWD Y_2p.Text = Ys2 + dY2 + Abs(R * Sin(beta)) * Y2_FWD '绘图 Picture1.ForeColor = vbMagenta Picture1.Line (Xs1 - dX1 - Abs(R * Cos(alfa)) * X1_FWD, Ys1 - dY1 - Abs(R * Sin(alfa)) * Y1_FWD)-(Xs1, Ys1) Picture1.Line (Xs1, Ys1)-(Xs2, Ys2) Picture1.Line (Xs2, Ys2)-(Xs2 + dX2 + Abs(R * Cos(beta)) * X2_FWD, Ys2 + dY2 + Abs(R * Sin(beta)) * Y2_FWD) '刀补撤销 ElseIf Combo1.ListIndex = 2 Then '第一对转接点 Xs1 = X1 - R * Ori * Yl1 + R * Xl1 Ys1 = Y1 + R * Ori * Xl1 + R * Yl1 '第二对转接点 Xs2 = X1 - R * Ori * Yl2 - R * Xl2 Ys2 = Y1 + R * Ori * Xl2 - R * Yl2 '第三对转接点 Xs3 = X1 - R * Ori * Yl2 Ys3 = Y1 + R * Ori * Xl2 '输出坐标 'X0',Y0' X_0p.Text = Xs1 - dX1 - Abs(R * Cos(alfa)) * X1_FWD Y_0p.Text = Ys1 - dY1 - Abs(R * Sin(alfa)) * Y1_FWD 'Xs1,Ys1 X_s1.Text = Xs1 Y_s1.Text = Ys1 'Xs2,Ys2 X_s2.Text = Xs2 Y_s2.Text = Ys2 'Xs3,Ys3 X_s3.Text = Xs3 Y_s3.Text = Ys3 'X2' Y2' X_2p.Text = X2 Y_2p.Text = Y2 '绘图 Picture1.ForeColor = vbMagenta Picture1.Line (Xs1 - dX1 - Abs(R * Cos(alfa)) * X1_FWD, Ys1 - dY1 - Abs(R * Sin(alfa)) * Y1_FWD)-(Xs1, Ys1) Picture1.Line (Xs1, Ys1)-(Xs2, Ys2) Picture1.Line (Xs2, Ys2)-(Xs3, Ys3) Picture1.Line (Xs3, Ys3)-(X2, Y2) End If End If End Sub Private Sub Command2_Click() Call PaintAxis End Sub Private Sub Form_Load() '补偿后坐标不可编辑,只读 X_0p.Locked = True Y_0p.Locked = True X_s1.Locked = True Y_s1.Locked = True X_s2.Locked = True Y_s2.Locked = True X_2p.Locked = True Y_2p.Locked = True '初始化ComboBox 两个 Combo1.AddItem "刀补建立", 0 Combo1.AddItem "刀补进行", 1 Combo1.AddItem "刀补撤销", 2 End Sub Private Sub Text1_Change() End Sub Private Sub Option1_Click() Ori = 1 End Sub Private Sub Option2_Click() Ori = -1 End Sub Private Sub Picture1_Paint() Call PaintAxis End Sub Private Sub Text9_Change() R = Val(R_K.Text) End Sub Private Sub R_K_Change() R = Val(R_K.Text) End Sub Private Sub X_0_Change() '赋值坐标 X0 = Val(X_0.Text) End Sub Private Sub X_1_Change() '赋值坐标 X1 = Val(X_1.Text) End Sub Private Sub X_2_Change() '赋值坐标 X2 = Val(X_2.Text) End Sub Private Sub Y_0_Change() '赋值坐标 Y0 = Val(Y_0.Text) End Sub Private Sub Y_1_Change() '赋值坐标 Y1 = Val(Y_1.Text) End Sub Private Sub Y_2_Change() '赋值坐标 Y2 = Val(Y_2.Text) End Sub Private Sub PaintAxis() Cls Dim i As Integer Picture1.BackColor = vbWhite Picture1.ForeColor = vbBlack Picture1.Scale (-1000, 1000)-(1000, -1000) Picture1.DrawWidth = 2 Picture1.Line (-1000, 0)-(1000, 0) '画x轴 Picture1.Line (1000, 0)-(970, 15) '画箭头 Picture1.Line (1000, 0)-(970, -15) '画箭头 Picture1.Line (0, -1000)-(0, 1000) '画y轴 Picture1.Line (0, 1000)-(10, 964) '画箭头 Picture1.Line (0, 1000)-(-10, 964) '画箭头 '画坐标刻度 For i = -10 To 9 Step 1 If i <> 0 Then Picture1.Line (i * 100, 0)-(i * 100, 10) 'x轴刻度 Picture1.CurrentX = i * 100 - 52: Picture1.CurrentY = -10: Picture1.Print i * 100 'x轴数字 Picture1.Line (0, i * 100)-(10, i * 100) 'y轴刻度 Picture1.CurrentX = -88: Picture1.CurrentY = i * 100 + 16: Picture1.Print i * 100 'y轴数字 End If Next i Picture1.ForeColor = vbRed Picture1.CurrentX = 10: Picture1.CurrentY = -10: Picture1.Print 0 Picture1.CurrentX = 980: Picture1.CurrentY = -14: Picture1.Print "X" Picture1.CurrentX = 15: Picture1.CurrentY = 990: Picture1.Print "Y" Picture1.ForeColor = vbBlack '显示框清零 X_0p.Text = "" Y_0p.Text = "" X_s1.Text = "" Y_s1.Text = "" X_s2.Text = "" Y_s2.Text = "" X_s3.Text = "" Y_s3.Text = "" X_2p.Text = "" Y_2p.Text = "" End Sub
图形界面如下图所示:
以下再贴一张运行结果图:
各种情况的运行结果都打包在一个word文档作为附件上传了,有需要可以自己下载。工程也在下边的压缩包
程序运行结果:
http://download.csdn.net/detail/u013926582/9405668
VB全部工程文件:
http://download.csdn.net/detail/u013926582/9405678
可执行文件EXE:
http://download.csdn.net/detail/u013926582/9405694
祝大家学习愉快