Option Explicit
Private Declare Sub PortOut Lib "IO.DLL" (ByVal Port As Integer, ByVal Data As Byte)
'variables para lectura de código
Dim cCadena As String
Dim cLetra As String * 1
Dim cCodigoP As String * 2
Dim cX As String, cY As String
Dim nComa As Integer, nLargoCad As Integer
Dim n As Integer, nFin As Integer, Rta As Integer
Dim xi As Single, yi As Single
Dim bArriba As Boolean, bSubir As Boolean 'variables para el taladro
'matrices para motores(8 pasos para cada motor)
Dim BitEjeZ(8) As Byte, BitEjeX(8) As Byte, BitEjeY(8) As Byte
'datos para cada elemento de las matrices
Dim nBitx As Integer, nBity As Integer, nBitz As Integer
'variables para retardos
Dim VelocDesp As Integer
Dim VelocPerf As Integer
Dim TT As Integer, T As Integer
'variables para cálculo de desplazamiento
Dim nNewX As Single, nNewY As Single, nNewZ As Integer
Dim nPlusx As Single, nPlusy As Single, nPrevx As Single, nPrevy As Single
Dim Profund As Integer, nEscala As Integer, nPrevz As Integer
Private Sub cmdCargar_Click()
Timer1.Enabled = False
' CancelError es True.
On Error GoTo solucion
' Establece los filtros.
CommonDialog1.Filter = "All Files (*.*)|*.*|Ploter (*.cnc)|*.cnc"
' Especifica el filtro predeterminado.
CommonDialog1.FilterIndex = 2
' Presenta el cuadro de diálogo Abrir.
CommonDialog1.ShowOpen
Dim lineareg As String
Open CommonDialog1.FileName For Input As #2
n = 0
' Carga el archivo plt en un list
While Not EOF(2) ' lee mientras el archivo no termine
Line Input #2, lineareg ' lee desde la primera linea
lineareg = Trim(lineareg)
lstCodigo.AddItem lineareg, n
n = n + 1
lstCodigo.Refresh
Wend
Close #2
n = 0
solucion:
Exit Sub
End Sub
Private Sub cmdContinuar_Click()
Timer1.Enabled = True
End Sub
Private Sub cmdDetener_Click()
Timer1.Enabled = False
End Sub
Private Sub cmdLimpiar_Click()
'limpia el list para cargar un nuevo archivo
nFin = lstCodigo.ListCount
lstCodigo.ListIndex = 0
For n = 0 To nFin - 1
lstCodigo.RemoveItem 0
Next
lstCodigo.Refresh
Picture1.BackColor = &H0
End Sub
Private Sub cmdTrazar_Click()
nPrevz = 0
nPrevx = 0
nPrevy = 0
bSubir = True
nNewZ = Val(txtProfundidad.Text)
Timer1.Enabled = True
End Sub
Private Sub Form_Load()
'apaga el puerto
Call PortOut(888, 0)
Call PortOut(890, 11)
'carga valores iniciales para la velocidad de motores
hscPerforacion.Value = 6
hscDesplazamiento.Value = 8
End Sub
Private Sub Form_Resize()
' si se modifica la ventana y no está minimizado entonces
If WindowState <> 1 Then
'se desplazan el picture y el frame con sus controles
Picture1.Width = Form1.Width - 3210
Picture1.Height = Form1.Height - 960
Frame1.Left = Form1.Width - 2790
Frame2.Left = Form1.Width - 2790
'aquí se mantendran la coordenada de origen de y
'en la parte inferior de picture aunque este cambie de tamaño
Picture1.ScaleTop = -Picture1.Height
End If
End Sub
Private Sub hscDesplazamiento_Change()
'muestra el valor de la velocidad en los ejes x,y
Label9.Caption = hscDesplazamiento.Value
End Sub
Private Sub hscPerforacion_Change()
'muestra el valor de la velocidad en el eje z
Label8.Caption = hscPerforacion.Value
End Sub
Private Sub Timer1_Timer()
nEscala = Val(txtEscala.Text) 'carga la escala
cCadena = "" 'limpia la variable que carga la 1º linea
cCadena = lstCodigo.Text 'lee la primer línea de código
nLargoCad = Len(cCadena) 'calcula longitud del código
cCodigoP = Left(cCadena, 2) 'toma las dos primeras letras de la línea
On Error GoTo Solutions 'por si surge algún error
If cCodigoP = "SP" Then 'si se lee la última línea de código
Timer1.Enabled = False 'detiene el timer
Rta = MsgBox("Grafico Finalizado", vbInformation, "Graficadora CNC") 'y muestra un mensaje de finde trazado
Call PortOut(888, 0)
Call PortOut(890, 11)
End If
'ahora se incrementa el puntero en el list
lstCodigo.ListIndex = lstCodigo.ListIndex + 1
'Aquí se buscan los valores para x y para y
If nLargoCad > 3 Then
nComa = InStr(cCadena, ",") 'busca la ubicación de la coma
cX = Mid(cCadena, 3, nComa - 3) 'carga el valor para el eje x
nNewX = Val(cX) * nEscala 'lo multiplica por el valor de la escala
cY = Mid(cCadena, nComa + 1, nLargoCad - Len(cX) - 1) 'carga el valor para el eje y
nNewY = Val(cY) * nEscala 'lo multiplica por la escala
End If
Timer1.Enabled = False
Mover
Solutions:
Exit Sub
End Sub
Private Sub cmdSalir_Click()
Call PortOut(890, 11)
Call PortOut(888, 0)
End
End Sub
Private Sub Mover()
BitEjeZ(0) = 10
BitEjeZ(1) = 8
BitEjeZ(2) = 9
BitEjeZ(3) = 13
BitEjeZ(4) = 15
BitEjeZ(5) = 7
BitEjeZ(6) = 3
BitEjeZ(7) = 2
BitEjeX(0) = 1
BitEjeX(1) = 3
BitEjeX(2) = 2
BitEjeX(3) = 6
BitEjeX(4) = 4
BitEjeX(5) = 12
BitEjeX(6) = 8
BitEjeX(7) = 9
BitEjeY(0) = 16
BitEjeY(1) = 48
BitEjeY(2) = 32
BitEjeY(3) = 96
BitEjeY(4) = 64
BitEjeY(5) = 192
BitEjeY(6) = 128
BitEjeY(7) = 144
'************************************************************
'Desplazamiento para el eje z
'-----------si el taladro está abajo debe subir-------------
If cCodigoP = "PU" Then
nBitz = 0
Subir:
VelocPerf = (15 - hscPerforacion.Value) * 10 'velocidad para subir
If nPrevz > 0 Then 'sube hasta la posición 0
nPrevz = nPrevz - 1
nBitz = Int(nPrevz) Mod 8
Call PortOut(890, BitEjeZ(nBitz)) 'aquí se manda el dato al puerto
'Retardo
For TT = 1 To VelocPerf
For T = 1 To 10000: Next T
Next TT
GoTo Subir
End If
'de ahora en más el taladro permanece arriba
End If
'-----------si el taladro está arriba debe bajar------------
If cCodigoP = "PD" Then
nBitz = 0
Bajar:
VelocPerf = (15 - hscPerforacion.Value) * 10 'velocidad para bajar
If nPrevz < nNewZ Then 'baja hasta la posicion indicada en nNewZ
nPrevz = nPrevz + 1
nBitz = Int(nPrevz) Mod 8
Call PortOut(890, BitEjeZ(nBitz)) 'aquí se manda el dato al puerto
'Retardo
For TT = 1 To VelocPerf
For T = 1 To 10000: Next T
Next TT
GoTo Bajar
End If
'de ahora en más el taladro permanece abajo
End If
Call PortOut(890, 11)
'************************************************************
'Desplazamiento para los ejes x-y
'****************************************************
If cCodigoP = "PU" Then bArriba = False 'sube lápiz
If cCodigoP = "PD" Then bArriba = True 'baja lápiz
'****************************************************
graficar:
VelocDesp = (15 - hscDesplazamiento.Value) * 10
'busca diferencias entre origen y destino
If nNewX <> nPrevx Then xi = Abs(nNewX - nPrevx)
If nNewY <> nPrevy Then yi = Abs(nPrevy - nNewY)
'según la diferemcia que exista entre x,y
'hay tres posibilidades
'incrementará el eje x, luego calculará el de y
If xi > yi Then
nPlusx = 1
nPlusy = yi / xi
End If
'incrementará eje y, luego calculará el de x
If xi < yi Then
nPlusx = xi / yi
nPlusy = 1
End If
'incrementará los dos por igual
If xi = yi Then
nPlusx = 1
nPlusy = 1
End If
'se actualizan las coordenadas mientras avanza
If nPrevx < nNewX Then nPrevx = nPrevx + nPlusx
If nPrevx > nNewX Then nPrevx = nPrevx - nPlusx
If nPrevy < nNewY Then nPrevy = nPrevy + nPlusy
If nPrevy > nNewY Then nPrevy = nPrevy - nPlusy
nBitx = Int(nPrevx) Mod 8
nBity = Int(nPrevy) Mod 8
Call PortOut(888, BitEjeX(nBitx) + BitEjeY(nBity))
'pixeliza la línea en el Picture1 (color rojo)
If bArriba = True Then Picture1.PSet (nPrevx, -nPrevy), RGB(255, 0, 0)
'Retardo
For TT = 1 To VelocDesp
For T = 1 To 10000: Next T
Next TT
'si termina de dibujar detiene el timer
If nPrevy = nNewY And nPrevx = nNewX Then
Timer1.Enabled = True
Else
GoTo graficar 'Si no terminó, sigue graficando
End If
End Sub