标题:求高手帮我解读一下。
只看楼主
pan1510456
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-5-23
结帖率:0
已结贴  问题点数:20 回复次数:4 
求高手帮我解读一下。
本人VB菜鸟,想分离这段语言中涉及到的函数,求高手帮我解读一下,万分感谢!
Sub G01(X As Single, Y As Single, Z As Single)   'G01过程
  Dim X1 As Single, Y1 As Single, SPointX As Single, SPointY As Single, I As Single, L As Single
  On Error GoTo errhand
  If X = CurrentNX And Y = CurrentNY And Z = CurrentNZ Then
     Exit Sub
  End If
  If StopRun = True Then
    Exit Sub
  End If
  frmmain.lblState.Caption = "直线插补|" & "起点: X" & CurrentNX & " Y" & CurrentNY & " Z" & CurrentNZ & "终点: X" & X & " Y" & Y & " Z" & Z
  SPointX = CurrentNX
  SPointY = CurrentNY
  If X <> CurrentNX Then
     Dim K As Single
     K = (Y - CurrentNY) / (X - CurrentNX)
     For I = CurrentNX To X Step Sgn(X - CurrentNX)
        Y1 = SPointY + K * (I - SPointX)
        X1 = I
        Call DrawLine(CurrentNX, CurrentNY, X1, Y1)
        CurrentNX = X1
        CurrentNY = Y1
        CurrentNZ = Z
     Next I
  Else
     For I = CurrentNY To Y Step Sgn(Y - CurrentNY)
        Y1 = I
        X1 = CurrentNX
        Call DrawLine(CurrentNX, CurrentNY, X1, Y1)
        CurrentNX = X1
        CurrentNY = Y1
        CurrentNZ = Z
        If X = CurrentNX And Y = CurrentNY And Z = CurrentNZ Then
           Exit Sub
        End If
    Next I
  End If
  Exit Sub
errhand:
  If StopRun = False Then
  MsgBox "程序发生错误,请检查G代码!", vbOKOnly, "错误"
  End If
End Sub
搜索更多相关主题的帖子: 起点 
2015-05-23 09:14
renxiaoyao36
Rank: 9Rank: 9Rank: 9
来 自:七宝中学
等 级:贵宾
威 望:31
帖 子:347
专家分:1077
注 册:2014-9-18
得分:10 
以下是引用pan1510456在2015-5-23 09:14:20的发言:



  If X = CurrentNX And Y = CurrentNY And Z = CurrentNZ Then
 

CurrentNX未知。。可能是我不知道吧,反正我是不知道的……你把其他的定义也发上来

编程蛋疼的不是枯燥,而是辛辛苦苦编完几百行的代码,运行,“Runtime Error “xxx””。
2015-05-23 10:05
pan1510456
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-5-23
得分:0 
回复 2楼 renxiaoyao36
Option Explicit
Public CurrentNX As Single, CurrentNY As Single, CurrentNZ As Single, CurrentNF As Single, CurrentNR As Single, CurrentNI As Single, CurrentNJ As Single, PauseRun As Boolean, TempX As Single, TempY As Single, TempZ As Single
Public VIEWXYZ As Boolean, OPX As Single, OPY As Single, OPZ As Single, FalsePoint As Boolean, MirrorX As Boolean, MirrorY As Boolean, CurrentGM As String
Dim RunFast As Integer, FirstPoint As Boolean, NOTX As Boolean, NOTIJ As Boolean, NEWZ As Single
Public StopRun As Boolean, SCALESIZE As Single, HowDC As Boolean, UserRTODC As Boolean, LineW As Integer
Sub G00(X As Single, Y As Single, Z As Single)   'G00过程
  frmmain.lblState.Caption = "快速定位到" & "X:" & X & " Y:" & Y & " Z:" & Z
  CurrentNX = X      '定位到当前坐标
  CurrentNY = Y
  CurrentNZ = Z
End Sub
Sub G01(X As Single, Y As Single, Z As Single)   'G01过程
  Dim X1 As Single, Y1 As Single, SPointX As Single, SPointY As Single, I As Single, L As Single
  On Error GoTo errhand
  If X = CurrentNX And Y = CurrentNY And Z = CurrentNZ Then
     Exit Sub
  End If
  If StopRun = True Then
    Exit Sub
  End If
  frmmain.lblState.Caption = "直线插补|" & "起点: X" & CurrentNX & " Y" & CurrentNY & " Z" & CurrentNZ & "终点: X" & X & " Y" & Y & " Z" & Z
  SPointX = CurrentNX
  SPointY = CurrentNY
  If X <> CurrentNX Then
     Dim K As Single
     K = (Y - CurrentNY) / (X - CurrentNX)
     For I = CurrentNX To X Step Sgn(X - CurrentNX)
        Y1 = SPointY + K * (I - SPointX)
        X1 = I
        Call DrawLine(CurrentNX, CurrentNY, X1, Y1)
        CurrentNX = X1
        CurrentNY = Y1
        CurrentNZ = Z
     Next I
  Else
     For I = CurrentNY To Y Step Sgn(Y - CurrentNY)
        Y1 = I
        X1 = CurrentNX
        Call DrawLine(CurrentNX, CurrentNY, X1, Y1)
        CurrentNX = X1
        CurrentNY = Y1
        CurrentNZ = Z
        If X = CurrentNX And Y = CurrentNY And Z = CurrentNZ Then
           Exit Sub
        End If
    Next I
  End If
  Exit Sub
errhand:
  If StopRun = False Then
  MsgBox "程序发生错误,请检查G代码!", vbOKOnly, "错误"
  End If
End Sub

Public Sub TXTTOGRAPHIC(CODETXT As String)   'G代码分析过程
   Dim I As Single, J As Single, X1 As Single, Y1 As Single, Z1 As Single, R1 As Single, I2 As Single, J2 As Single, D2 As Single, F1 As Integer
   Dim TempTxt As String, TempCode() As String, HX1 As Boolean, HY1 As Boolean, HZ1 As Boolean, HR1 As Boolean, HI2 As Boolean, HJ2 As Boolean, HD2 As Boolean, HF1 As Boolean
   Dim CodeNUM As String, TempZ As Single, TempI As Single, TempJ As Single, GMTRUE As Boolean, TempX1 As Single, TempX2 As Single, TempY1 As Single, TempY2 As Single
   On Error GoTo errhand
   If StopRun = True Then
      Exit Sub
   End If
   I = 0
   GMTRUE = False
   NOTIJ = False
   NOTX = False
   UserRTODC = False
   For J = Len(CODETXT) To 1 Step -1     '分离X\Y\Z\G\M等到数组,如G00X10Y10 分离后为 G00、X10、Y10
         If IsNumeric(Mid$(CODETXT, J, 1)) Or Mid$(CODETXT, J, 1) = "-" Or Mid$(CODETXT, J, 1) = "." Or Mid$(CODETXT, J, 1) = " " Then
           If Mid$(CODETXT, J, 1) <> " " Then
            TempTxt = Mid$(CODETXT, J, 1) & TempTxt
           End If
         Else
            ReDim Preserve TempCode(I)
            TempCode(I) = Mid$(CODETXT, J, 1) & TempTxt
            TempTxt = ""
            I = I + 1
         End If
    Next J
    NOTX = True
    For J = 0 To UBound(TempCode)   '把XYZIJR等的值存到变量,并用标记
       TempTxt = Left$(TempCode(J), 1)
       Select Case TempTxt
       Case "X"
          X1 = Val(Mid$(TempCode(J), 2))
          NOTX = False
          HX1 = True
       Case "Y"
          Y1 = Val(Mid$(TempCode(J), 2))
          HY1 = True
          NOTX = False
       Case "Z"
          Z1 = Val(Mid$(TempCode(J), 2))
          NEWZ = Z1
          HZ1 = True
          NOTX = False
       Case "I"
          I2 = Val(Mid$(TempCode(J), 2))
          HI2 = True
       Case "J"
          J2 = Val(Mid$(TempCode(J), 2))
          HJ2 = True
       Case "R"
          R1 = Val(Mid$(TempCode(J), 2))
          HR1 = True
          UserRTODC = True
       Case "D"
          D2 = Val(Mid$(TempCode(J), 2))
          HD2 = True
       Case "F"
          F1 = Val(Mid$(TempCode(J), 2))
          frmmain.LblV.Caption = F1
          CurrentNF = F1
          HF1 = True
       Case "N"
       Case Else
       End Select
    Next J
    If HI2 = False And HJ2 = False Then
       NOTIJ = True
    End If
    If HR1 = True Then
       NOTIJ = False
    End If
    If HX1 = False Then    '当G代码中没有X,则用当前点X代替,Y,Z也一样
      If FalsePoint = False Then
       If MirrorY = True Then
          X1 = -CurrentNX
       Else
          X1 = CurrentNX
       End If
      Else
       X1 = 0
      End If
    End If
    If HY1 = False Then
      If FalsePoint = False Then
       If MirrorX = True Then
          Y1 = -CurrentNY
       Else
          Y1 = CurrentNY
       End If
      Else
       Y1 = 0
      End If
    End If
    If HZ1 = False Then
      If FalsePoint = False Then
       Z1 = CurrentNZ
       NEWZ = Z1
      Else
       Z1 = 0
       NEWZ = Z1
      End If
    End If
    If HI2 = False Then       '没有输入IJ,则用零代替
       I2 = 0
    End If
    If HJ2 = False Then
       J2 = 0
    End If
    If HF1 = True Then
       frmmain.LblV.Caption = F1
    End If
    If HD2 = True Then
       If D2 = 1 Then
          frmmain.lblST.Caption = frmmain.TXTST(0).Text
       Else
          frmmain.lblST.Caption = frmmain.TXTST(1).Text
       End If
    End If
    For J = 0 To UBound(TempCode)   '先处理M代码
       TempTxt = Left$(TempCode(J), 1)
       Select Case TempTxt
       Case "M"
      
       CodeNUM = Trim$(Mid$(TempCode(J), 2))
          Select Case CodeNUM
          Case "02"
             Call M02
          Case "03"
             Call M03
          Case "04"
             Call M04
          Case "05"
             Call M05
          Case "08"
             Call M08
          Case "09"
             Call M09
             Case "02"
             Call M2
          Case "03"
             Call M3
          Case "04"
             Call M4
          Case "05"
             Call M5
          Case "08"
             Call M8
          Case "09"
             Call M9
         Case "21"
             Call M21
             Case "22"
             Call M22
             Case "23"
             Call M23
          Case "30"
             Call M30
          End Select
       End Select
     Next J
     
    For J = 0 To UBound(TempCode)   '先处理G54\G92\G90\G91
       TempTxt = Trim$(TempCode(J))
       If TempTxt = "G54" Then
          Call G54
       End If
    Next J
    For J = 0 To UBound(TempCode)
       TempTxt = Trim$(TempCode(J))
       If TempTxt = "G92" Then
          Call G92(X1, Y1, Z1)
       End If
    Next J
    For J = 0 To UBound(TempCode)
       TempTxt = Trim$(TempCode(J))
       If TempTxt = "G90" Then
          Call G90
       End If
    Next J
    For J = 0 To UBound(TempCode)
       TempTxt = Trim$(TempCode(J))
       If TempTxt = "G91" Then
          Call G91
       End If
    Next J
   
    For J = 0 To UBound(TempCode)         '处理G00,G01,G02,G03
       TempTxt = Left$(TempCode(J), 1)
       Select Case TempTxt
       Case "G"
          CodeNUM = Trim$(Mid$(TempCode(J), 2))
          Select Case CodeNUM
          Case "00"
             Call ABSPoint(X1, Y1, Z1)
             Call G00(X1, Y1, Z1)
             GMTRUE = True               '保留功能
             CurrentGM = "G00"
             Case "0"
             Call ABSPoint(X1, Y1, Z1)
             Call G00(X1, Y1, Z1)
             GMTRUE = True               '保留功能
             CurrentGM = "G00"
          Case "01"
             Call ABSPoint(X1, Y1, Z1)
             Call G01(X1, Y1, Z1)
             GMTRUE = True
             CurrentGM = "G01"
             Case "1"
             Call ABSPoint(X1, Y1, Z1)
             Call G01(X1, Y1, Z1)
             GMTRUE = True
             CurrentGM = "G01"
          Case "02"
             If HR1 = True Then
                Call GetCenterPoint(X1, Y1, R1, True, I2, J2)   '根据R计算圆心位置
                I2 = I2
                J2 = J2
             End If
             Call ABSPoint(X1, Y1, Z1)
            If MirrorY = True Or MirrorX = True Then
             Call ABSPoint(I2, J2, TempZ)               '转换到绝对坐标
            End If
             If HR1 = True Then
                CurrentNR = Abs(R1)
             Else
                CurrentNR = (I2 ^ 2 + J2 ^ 2) ^ 0.5
             End If
             I2 = I2 + CurrentNX
             J2 = J2 + CurrentNY
             If (MirrorY Xor MirrorX) = True Then
               Call G03(X1, Y1, I2, J2)
             Else
               Call G02(X1, Y1, I2, J2)
             End If
             GMTRUE = True
             CurrentGM = "G02"
             Case "2"
             If HR1 = True Then
                Call GetCenterPoint(X1, Y1, R1, True, I2, J2)   '根据R计算圆心位置
                I2 = I2
                J2 = J2
             End If
             Call ABSPoint(X1, Y1, Z1)
            If MirrorY = True Or MirrorX = True Then
             Call ABSPoint(I2, J2, TempZ)               '转换到绝对坐标
            End If
             If HR1 = True Then
                CurrentNR = Abs(R1)
             Else
                CurrentNR = (I2 ^ 2 + J2 ^ 2) ^ 0.5
             End If
             I2 = I2 + CurrentNX
             J2 = J2 + CurrentNY
             If (MirrorY Xor MirrorX) = True Then
               Call G03(X1, Y1, I2, J2)
             Else
               Call G02(X1, Y1, I2, J2)
             End If
             GMTRUE = True
             CurrentGM = "G02"
            
          Case "03"
             If HR1 = True Then
                Call GetCenterPoint(X1, Y1, R1, False, I2, J2)
             End If
             Call ABSPoint(X1, Y1, Z1)
            If MirrorY = True Or MirrorX = True Then
             Call ABSPoint(I2, J2, TempZ)
            End If
             If HR1 = True Then
                CurrentNR = Abs(R1)
             Else
                CurrentNR = (I2 ^ 2 + J2 ^ 2) ^ 0.5
             End If
             I2 = I2 + CurrentNX
             J2 = J2 + CurrentNY
             If (MirrorY Xor MirrorX) = True Then
               Call G02(X1, Y1, I2, J2)
             Else
               Call G03(X1, Y1, I2, J2)
             End If
             GMTRUE = True
             CurrentGM = "G03"
             Case "3"
             If HR1 = True Then
                Call GetCenterPoint(X1, Y1, R1, False, I2, J2)
             End If
             Call ABSPoint(X1, Y1, Z1)
            If MirrorY = True Or MirrorX = True Then
             Call ABSPoint(I2, J2, TempZ)
            End If
             If HR1 = True Then
                CurrentNR = Abs(R1)
             Else
                CurrentNR = (I2 ^ 2 + J2 ^ 2) ^ 0.5
             End If
             I2 = I2 + CurrentNX
             J2 = J2 + CurrentNY
             If (MirrorY Xor MirrorX) = True Then
               Call G02(X1, Y1, I2, J2)
             Else
               Call G03(X1, Y1, I2, J2)
             End If
             GMTRUE = True
             CurrentGM = "G03"
          Case "04"
            Call G04
          Case "28"
            Call G28
          Case "29"
            Call G29
          Case "40"
            Call G40
          Case "41"
            Call G41
          Case "42"
            Call G42
          End Select
       End Select
    Next J
    If GMTRUE = False Then        '如果没输入G指令,则用保留功能中的G指令代替
       Select Case CurrentGM
       Case "G01"
          If NOTX = False Then
             Call ABSPoint(X1, Y1, Z1)
             Call G01(X1, Y1, Z1)
             GMTRUE = True
             CurrentGM = "G01"
          End If
       Case "G02"
          If NOTX = False And NOTIJ = False Then
             If HR1 = True Then
                Call GetCenterPoint(X1, Y1, R1, True, I2, J2)
             End If
             Call ABSPoint(X1, Y1, Z1)
            If MirrorY = True Or MirrorX = True Then
             Call ABSPoint(I2, J2, TempZ)
            End If
             If HR1 = True Then
                CurrentNR = Abs(R1)
             Else
                CurrentNR = (I2 ^ 2 + J2 ^ 2) ^ 0.5
             End If
             I2 = I2 + CurrentNX
             J2 = J2 + CurrentNY
             If (MirrorX Xor MirrorY) = True Then
               Call G03(X1, Y1, I2, J2)
             Else
               Call G02(X1, Y1, I2, J2)
             End If
             GMTRUE = True
             CurrentGM = "G02"
          End If
       Case "G03"
          If NOTX = False And NOTIJ = False Then
             If HR1 = True Then
                Call GetCenterPoint(X1, Y1, R1, False, I2, J2)
             End If
             Call ABSPoint(X1, Y1, Z1)
            If MirrorY = True Or MirrorX = True Then
             Call ABSPoint(I2, J2, TempZ)
            End If
             If HR1 = True Then
                CurrentNR = Abs(R1)
             Else
                CurrentNR = (I2 ^ 2 + J2 ^ 2) ^ 0.5
             End If
             I2 = I2 + CurrentNX
             J2 = J2 + CurrentNY
             If (MirrorX Xor MirrorY) = True Then
               Call G02(X1, Y1, I2, J2)
             Else
               Call G03(X1, Y1, I2, J2)
             End If
             GMTRUE = True
             CurrentGM = "G03"
          End If
       End Select
    End If
    Exit Sub
errhand:
  If StopRun = False Then
    MsgBox "仿真过程发生错误,请检查G代码!", vbOKOnly, "错误"
  End If
End Sub
我是个小白,谢谢大神指点了。这是个模拟画直线或圆弧的程序,我想知道其中用的函数是什么。
2015-05-23 10:25
renxiaoyao36
Rank: 9Rank: 9Rank: 9
来 自:七宝中学
等 级:贵宾
威 望:31
帖 子:347
专家分:1077
注 册:2014-9-18
得分:0 
头已晕,另外,我不是该方面专长的,有些代码,呃呃呃,说着不怕丢人,我也看不懂……

编程蛋疼的不是枯燥,而是辛辛苦苦编完几百行的代码,运行,“Runtime Error “xxx””。
2015-05-23 21:57
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2276
专家分:10647
注 册:2015-3-19
得分:10 
Sub G01(X As Single, Y As Single, Z As Single)   'G01过程
  Dim X1 As Single
  Dim Y1 As Single
  Dim SPointX As Single                    '起点X维
  Dim SPointY As Single                    '起点Y维
  Dim I As Single
  Dim L As Single
  On Error GoTo errhand
  If X = CurrentNX And Y = CurrentNY And Z = CurrentNZ Then        
     Exit Sub                             'CurrentNX等应该是全局变量,与形参相等则退出
  End If
  If StopRun = True Then                  'StopRun应该也是全局变量,数据类型为布偶型为真时退出
    Exit Sub
  End If
  frmmain.lblState.Caption = "直线插补|" & "起点: X" & CurrentNX & " Y" & CurrentNY & " Z" & CurrentNZ & "终点: X" & X & " Y" & Y & " Z" & Z              '这里应该是frmmain的控件lblState的标题显示一个串
  SPointX = CurrentNX                              '为局部变量SPointX赋值CurrentNX,定下起点的X维
  SPointY = CurrentNY                              '为局部变量SPointY赋值CurrentNY,定下起点的Y维
  If X <> CurrentNX Then                           '这里形参的X维与当前对象的X维不一致则执行往下执行
     Dim K As Single
     K = (Y - CurrentNY) / (X - CurrentNX)   'k应该是全局变量这里初值为(Y - CurrentNY) / (X - CurrentNX)
     For I = CurrentNX To X Step Sgn(X - CurrentNX)     '循环体,步长为Sgn(X-CurrentNX)
        Y1 = SPointY + K * (I - SPointX)                'DrawLine划线函数的终点Y维:Y1
        X1 = I                                          'DrawLine划线函数的终点X维:X1
        Call DrawLine(CurrentNX, CurrentNY, X1, Y1)     'DrawLine划线函数起点X,Y维由下面可知是变化的
        CurrentNX = X1
        CurrentNY = Y1
        CurrentNZ = Z                                    '当前对象的Z维CurrentNZ出现赋值为形参Z
     Next I
  Else                                       '这里形参的X维与当前对象的X维致则跳过上面语句再往下执行
     For I = CurrentNY To Y Step Sgn(Y - CurrentNY)
        Y1 = I
        X1 = CurrentNX
        Call DrawLine(CurrentNX, CurrentNY, X1, Y1)
        CurrentNX = X1
        CurrentNY = Y1
        CurrentNZ = Z
        If X = CurrentNX And Y = CurrentNY And Z = CurrentNZ Then    '到了终点结束退出
           Exit Sub
        End If
    Next I
  End If
  Exit Sub
errhand:
  If StopRun = False Then                                              '运行出错处理
  MsgBox "程序发生错误,请检查G代码!", vbOKOnly, "错误"
  End If
End Sub


当X <> CurrentNX 时分析这段:
K = (Y - CurrentNY) / (X - CurrentNX)     
     For I = CurrentNX To X Step Sgn(X - CurrentNX)     
        Y1 = SPointY + K * (I - SPointX)                 
        X1 = I     
        Call DrawLine(CurrentNX, CurrentNY, X1, Y1)      
        CurrentNX = X1
        CurrentNY = Y1
        CurrentNZ = Z                                    
     Next I
X1维变化随着循环增量I线性变化,而Y1变化是SPointY + K* (I - SPointX) K是什么?
两点(当前对象和形参两点间X,Y维上的)斜率,那么Y1=SPoint+K* (I - SPointX)
可以理解为随着循环的进行每次沿着两点见得方向与X1的值成比例变化。换句话说就是:
画一段线段分成X - CurrentNX步每步沿着线的方向画一点,最后循环结束线也画完了。
而X=CurrentNX 就不多述了
我只看了一楼的代码不知道我的分析对你有没有帮助。

剑栈风樯各苦辛,别时冰雪到时春
2015-05-24 11:25



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-445648-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.252575 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved