本课题是刀具半径补偿的直线-直线软件实现,是南京工程学院数控专业的数控原理与系统的课程设计,整个工程都是自己随便弄弄的,作为课设作业还算比较严谨吧。题目不难,具体的直线-直线的刀具补偿原理,见机械工业出版社出版的汪木兰主编的《数控原理与系统》的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 DoublePublic Xs1 As Double, Ys1 As Double, Xs2 As Double, Ys2 As DoublePublic Ori As IntegerPublic 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 DoublePrivate Sub Command1_Click()Dim X1_FWD As Integer, Y1_FWD As Integer, X2_FWD As Integer, Y2_FWD As IntegerDim alfa As Double, beta As DoubleCall PaintAxis'绘制补偿前图像Picture1.ForeColor = vbBluePicture1.DrawWidth = 1Picture1.Line (X0, Y0)-(X1, Y1)Picture1.Line (X1, Y1)-(X2, Y2)'算法设计'计算坐标增量dX1 = X1 - X0dY1 = Y1 - Y0dX2 = X2 - X1dY2 = Y2 - Y1alfa = Atn(dY1 / dX1)beta = Atn(dY2 / dX2)If dX1 >= 0 ThenX1_FWD = 1ElseX1_FWD = -1End IfIf dX2 >= 0 ThenX2_FWD = 1ElseX2_FWD = -1End IfIf dY1 >= 0 ThenY1_FWD = 1ElseY1_FWD = -1End IfIf dY2 >= 0 ThenY2_FWD = 1ElseY2_FWD = -1End If'计算d1,d2d1 = Sqr(dX1 ^ 2 + dY1 ^ 2)d2 = Sqr(dX2 ^ 2 + dY2 ^ 2)'计算方向矢量投影Xl1 = dX1 / d1Yl1 = dY1 / d1Xl2 = dX2 / d2Yl2 = dY2 / d2'判断缩短型,伸长型,插入型If Ori * (Yl2 * Xl1 - Xl2 * Yl1) >= 0 Then '缩短型'刀补建立If Combo1.ListIndex = 0 And Ori * (Yl2 * Xl1 - Xl2 * Yl1) <> 0 ThenXs1 = X1 - R * Ori * Yl2Ys1 = Y1 + R * Ori * Xl2X_0p.Text = X0Y_0p.Text = Y0X_s1.Text = Xs1Y_s1.Text = Ys1X_2p.Text = Xs1 + dX2Y_2p.Text = Ys1 + dY2Picture1.ForeColor = vbMagentaPicture1.Line (X0, Y0)-(Xs1, Ys1)Picture1.Line (Xs1, Ys1)-(Xs1 + dX2, Ys1 + dY2)'刀补进行ElseIf Combo1.ListIndex = 1 ThenIf Yl2 * Xl1 - Xl2 * Yl1 = 0 Then 'l1与l2共线Xs1 = X1 - R * Ori * Yl1Ys1 = Y1 + R * Ori * Xl1X_0p.Text = Xs1 - dX1Y_0p.Text = Ys1 - dY1X_s1.Text = Xs1Y_s1.Text = Ys1X_2p.Text = Xs1 + dX2Y_2p.Text = Ys1 + dY2Picture1.ForeColor = vbMagentaPicture1.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 - dX1Y_0p.Text = Ys1 - dY1X_s1.Text = Xs1Y_s1.Text = Ys1X_2p.Text = Xs1 + dX2Y_2p.Text = Ys1 + dY2Picture1.ForeColor = vbMagentaPicture1.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 ThenXs1 = X1 - R * Ori * Yl1Ys1 = Y1 + R * Ori * Xl1X_0p.Text = Xs1 - dX1Y_0p.Text = Ys1 - dY1X_s1.Text = Xs1Y_s1.Text = Ys1X_2p.Text = X2Y_2p.Text = Y2Picture1.ForeColor = vbMagentaPicture1.Line (Xs1 - dX1, Ys1 - dY1)-(Xs1, Ys1)Picture1.Line (Xs1, Ys1)-(X2, Y2)End IfElseIf Ori * (Yl2 * Xl1 - Xl2 * Yl1) < 0 And (Yl2 * Yl1 + Xl2 * Xl1) >= 0 Then '伸长型'刀补建立If Combo1.ListIndex = 0 Then'第一对转接点Xs1 = X1 - R * Ori * Yl1Ys1 = 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 = X0Y_0p.Text = Y0'Xs1,Ys1X_s1.Text = Xs1Y_s1.Text = Ys1'Xs2,Ys2X_s2.Text = Xs2Y_s2.Text = Ys2'X2' Y2'X_2p.Text = Xs2 + dX2Y_2p.Text = Ys2 + dY2'绘图Picture1.ForeColor = vbMagentaPicture1.Line (X0, Y0)-(Xs1, Ys1)Picture1.Line (Xs1, Ys1)-(Xs2, Ys2)Picture1.Line (Xs2, Ys2)-(Xs2 + dX2, Ys2 + dY2)'刀补进行ElseIf Combo1.ListIndex = 1 ThenXs1 = X1 + (Xl2 - Xl1) * R * Ori / (Xl1 * Yl2 - Xl2 * Yl1)Ys1 = Y1 + (Yl2 - Yl1) * R * Ori / (Xl1 * Yl2 - Xl2 * Yl1)'输出坐标'X0',Y0'X_0p.Text = X0Y_0p.Text = Y0'Xs1,Ys1X_s1.Text = Xs1Y_s1.Text = Ys1'X2' Y2'X_2p.Text = Xs2 + dX2Y_2p.Text = Ys2 + dY2'绘图Picture1.ForeColor = vbMagentaPicture1.Line (Xs1 - dX1, Ys1 - dY1)-(Xs1, Ys1)Picture1.Line (Xs1, Ys1)-(Xs1 + dX2, Ys1 + dY2)'刀补撤销ElseIf Combo1.ListIndex = 2 ThenXs1 = X1 + (Xl2 - Xl1) * R * Ori / (Xl1 * Yl2 - Xl2 * Yl1)Ys1 = Y1 + (Yl2 - Yl1) * R * Ori / (Xl1 * Yl2 - Xl2 * Yl1)Xs2 = X1 - R * Ori * Yl2Ys2 = Y1 + R * Ori * Xl2'输出坐标'X0',Y0'X_0p.Text = Xs1 - dX1Y_0p.Text = Ys1 - dY1'Xs1,Ys1X_s1.Text = Xs1Y_s1.Text = Ys1'Xs2,Ys2X_s2.Text = Xs2Y_s2.Text = Ys2'X2' Y2'X_2p.Text = X2Y_2p.Text = Y2'绘图Picture1.ForeColor = vbMagentaPicture1.Line (Xs1 - dX1, Ys1 - dY1)-(Xs1, Ys1)Picture1.Line (Xs1, Ys1)-(Xs2, Ys2)Picture1.Line (Xs2, Ys2)-(X2, Y2)End IfElseIf Ori * (Yl2 * Xl1 - Xl2 * Yl1) < 0 And (Yl2 * Yl1 + Xl2 * Xl1) < 0 Then '插入型'刀补建立If Combo1.ListIndex = 0 Then'第一对转接点Xs1 = X1 - R * Ori * Yl1Ys1 = Y1 + R * Ori * Xl1'第二对转接点Xs2 = X1 - Ori * R * Yl1 + R * Xl1Ys2 = Y1 + Ori * R * Xl1 + R * Yl1'第三对转接点Xs3 = X1 - R * Ori * Yl2 - R * Xl2Ys3 = Y1 + R * Ori * Xl2 - R * Yl2'输出坐标'X0',Y0'X_0p.Text = X0Y_0p.Text = Y0'Xs1,Ys1X_s1.Text = Xs1Y_s1.Text = Ys1'Xs2,Ys2X_s2.Text = Xs2Y_s2.Text = Ys2'Xs3,Ys3X_s3.Text = Xs3Y_s3.Text = Ys3'X2' Y2'X_2p.Text = Xs3 + dX2 + Abs(R * Cos(beta)) * X2_FWDY_2p.Text = Ys3 + dY2 + Abs(R * Sin(beta)) * X2_FWD'绘图Picture1.ForeColor = vbMagentaPicture1.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 * Xl1Ys1 = Y1 + R * Ori * Xl1 + R * Yl1'第二对转接点Xs2 = X1 - R * Ori * Yl2 - R * Xl2Ys2 = Y1 + R * Ori * Xl2 - R * Yl2'输出坐标'X0',Y0'X_0p.Text = Xs1 - dX1 - Abs(R * Cos(alfa)) * X1_FWDY_0p.Text = Ys1 - dY1 - Abs(R * Sin(alfa)) * Y1_FWD'Xs1,Ys1X_s1.Text = Xs1Y_s1.Text = Ys1'Xs2,Ys2X_s2.Text = Xs2Y_s2.Text = Ys2'X2' Y2'X_2p.Text = Xs2 + dX2 + Abs(R * Cos(beta)) * X2_FWDY_2p.Text = Ys2 + dY2 + Abs(R * Sin(beta)) * Y2_FWD'绘图Picture1.ForeColor = vbMagentaPicture1.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 * Xl1Ys1 = Y1 + R * Ori * Xl1 + R * Yl1'第二对转接点Xs2 = X1 - R * Ori * Yl2 - R * Xl2Ys2 = Y1 + R * Ori * Xl2 - R * Yl2'第三对转接点Xs3 = X1 - R * Ori * Yl2Ys3 = Y1 + R * Ori * Xl2'输出坐标'X0',Y0'X_0p.Text = Xs1 - dX1 - Abs(R * Cos(alfa)) * X1_FWDY_0p.Text = Ys1 - dY1 - Abs(R * Sin(alfa)) * Y1_FWD'Xs1,Ys1X_s1.Text = Xs1Y_s1.Text = Ys1'Xs2,Ys2X_s2.Text = Xs2Y_s2.Text = Ys2'Xs3,Ys3X_s3.Text = Xs3Y_s3.Text = Ys3'X2' Y2'X_2p.Text = X2Y_2p.Text = Y2'绘图Picture1.ForeColor = vbMagentaPicture1.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 IfEnd IfEnd SubPrivate Sub Command2_Click()Call PaintAxisEnd SubPrivate Sub Form_Load()'补偿后坐标不可编辑,只读X_0p.Locked = TrueY_0p.Locked = TrueX_s1.Locked = TrueY_s1.Locked = TrueX_s2.Locked = TrueY_s2.Locked = TrueX_2p.Locked = TrueY_2p.Locked = True'初始化ComboBox 两个Combo1.AddItem "刀补建立", 0Combo1.AddItem "刀补进行", 1Combo1.AddItem "刀补撤销", 2End SubPrivate Sub Text1_Change()End SubPrivate Sub Option1_Click()Ori = 1End SubPrivate Sub Option2_Click()Ori = -1End SubPrivate Sub Picture1_Paint()Call PaintAxisEnd SubPrivate Sub Text9_Change()R = Val(R_K.Text)End SubPrivate Sub R_K_Change()R = Val(R_K.Text)End SubPrivate Sub X_0_Change()'赋值坐标X0 = Val(X_0.Text)End SubPrivate Sub X_1_Change()'赋值坐标X1 = Val(X_1.Text)End SubPrivate Sub X_2_Change()'赋值坐标X2 = Val(X_2.Text)End SubPrivate Sub Y_0_Change()'赋值坐标Y0 = Val(Y_0.Text)End SubPrivate Sub Y_1_Change()'赋值坐标Y1 = Val(Y_1.Text)End SubPrivate Sub Y_2_Change()'赋值坐标Y2 = Val(Y_2.Text)End SubPrivate Sub PaintAxis()ClsDim i As IntegerPicture1.BackColor = vbWhitePicture1.ForeColor = vbBlackPicture1.Scale (-1000, 1000)-(1000, -1000)Picture1.DrawWidth = 2Picture1.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 1If i <> 0 ThenPicture1.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 IfNext iPicture1.ForeColor = vbRedPicture1.CurrentX = 10: Picture1.CurrentY = -10: Picture1.Print 0Picture1.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文档作为附件上传了,有需要可以自己下载。工程也在下边的压缩包
程序运行结果:
/detail/u013926582/9405668
VB全部工程文件:
/detail/u013926582/9405678
可执行文件EXE:
/detail/u013926582/9405694
祝大家学习愉快