Attribute VB_Name = "CortarPlotter" Option Explicit ' ============================================================================ ' CortarPlotter - corte direto do CorelDRAW para plotter de recorte (HPGL) ' Testado em Foison C24 via USB (porta COM). Deve funcionar na maioria das ' plotters de recorte que aceitam HPGL pela porta serial / USB-serial. ' ' Macros: ' CortarPlotter -> corta a selecao ' CortarPlotterEspelhado -> corta a selecao espelhada (termocolante) ' ConfigurarPlotter -> porta, velocidade, largura e folgas ' TesteDirecao -> descobre o sentido do rolo (nao corta nada) ' ' Use por sua conta e risco: teste sempre com um pedaco pequeno de material ' e mantenha a mao no botao Pause da plotter. ' Guia completo: veja a pagina onde voce baixou este arquivo. ' ============================================================================ Private Const APP As String = "CortarPlotter" Private Const PASSOS_CURVA As Long = 16 Private gDir As Double, gEsq As Double, gBase As Double Private gRoloEmX As Boolean, gEspelhar As Boolean Private gMargemLado As Double, gMargemInicio As Double Private gPartes() As String, gChaves() As Double, gN As Long Private Const FAIXA As Double = 20 ' faixas de ordenacao ao longo do rolo (mm) Public Sub CortarPlotter() ExecutarCorte False End Sub Public Sub CortarPlotterEspelhado() ExecutarCorte True End Sub Public Sub ConfigurarPlotter() Dim v As String v = InputBox("Porta da plotter (veja no Gerenciador de Dispositivos > Portas COM e LPT)." & vbCrLf & "Exemplo: COM3", APP, GetSetting(APP, "Config", "Porta", "COM3")) If v = "" Then Exit Sub SaveSetting APP, "Config", "Porta", UCase$(Trim$(v)) v = InputBox("Velocidade da porta (baud) configurada no painel da plotter." & vbCrLf & "Valores comuns: 9600, 19200, 38400, 57600, 115200", APP, GetSetting(APP, "Config", "Baud", "9600")) If v = "" Then Exit Sub SaveSetting APP, "Config", "Baud", Trim$(v) v = InputBox("Largura maxima de corte da plotter, em mm." & vbCrLf & "Exemplo: 560 para uma plotter de 60 cm", APP, GetSetting(APP, "Config", "Largura", "560")) If v = "" Then Exit Sub SaveSetting APP, "Config", "Largura", Trim$(v) v = InputBox("Folga, em mm, entre a origem marcada na plotter e o inicio do corte." & vbCrLf & "Use 0 a 5 para economizar material.", APP, GetSetting(APP, "Config", "Margem", "2")) If v = "" Then Exit Sub SaveSetting APP, "Config", "Margem", Trim$(v) SaveSetting APP, "Config", "Ok", "1" MsgBox "Configuracao salva." & vbCrLf & vbCrLf & "Agora rode a macro TesteDirecao (uma vez so) para acertar o sentido do rolo.", vbInformation, APP End Sub Public Sub TesteDirecao() If GetSetting(APP, "Config", "Ok", "") = "" Then ConfigurarPlotter If GetSetting(APP, "Config", "Ok", "") = "" Then Exit Sub If MsgBox("A plotter vai se mover 5 cm com a lamina LEVANTADA (nao corta)." & vbCrLf & "Olhe a maquina e clique em OK.", vbOKCancel + vbInformation, APP) <> vbOK Then Exit Sub If Not Enviar("IN;PA;PU0,0;PU2000,0;") Then Exit Sub If MsgBox("O que se mexeu?" & vbCrLf & vbCrLf & "SIM = o material (vinil) rolou para frente ou para tras" & vbCrLf & "NAO = a cabeca andou de lado", vbYesNo + vbQuestion, APP) = vbYes Then SaveSetting APP, "Config", "RoloEmX", "1" Else SaveSetting APP, "Config", "RoloEmX", "0" End If SaveSetting APP, "Config", "Direcao", "1" MsgBox "Sentido salvo. Faca um corte pequeno de teste." & vbCrLf & "Se o desenho sair espelhado, rode a macro InverterLado uma vez.", vbInformation, APP End Sub Public Sub InverterLado() ' Use se o corte sair espelhado na sua maquina Dim atual As String atual = GetSetting(APP, "Config", "Inverter", "0") If atual = "1" Then atual = "0" Else atual = "1" SaveSetting APP, "Config", "Inverter", atual MsgBox "Inversao " & IIf(atual = "1", "LIGADA", "DESLIGADA") & ".", vbInformation, APP End Sub Private Sub ExecutarCorte(ByVal espelhado As Boolean) If ActiveDocument Is Nothing Then Exit Sub If GetSetting(APP, "Config", "Ok", "") = "" Then ConfigurarPlotter If GetSetting(APP, "Config", "Ok", "") = "" Then Exit Sub End If If GetSetting(APP, "Config", "Direcao", "") = "" Then TesteDirecao If GetSetting(APP, "Config", "Direcao", "") = "" Then Exit Sub End If If ActiveSelectionRange.Count = 0 Then MsgBox "Selecione o que vai cortar.", vbExclamation, APP Exit Sub End If Dim largMax As Double largMax = Val(GetSetting(APP, "Config", "Largura", "560")) gMargemLado = Val(GetSetting(APP, "Config", "Margem", "2")) gMargemInicio = gMargemLado gRoloEmX = (GetSetting(APP, "Config", "RoloEmX", "1") = "1") gEspelhar = espelhado Xor (GetSetting(APP, "Config", "Inverter", "0") = "1") Dim unidadeAnt As cdrUnit, x0 As Double, y0 As Double, w As Double, h As Double unidadeAnt = ActiveDocument.Unit ActiveDocument.Unit = cdrMillimeter ActiveSelectionRange.GetBoundingBox x0, y0, w, h If w + gMargemLado > largMax Then ActiveDocument.Unit = unidadeAnt MsgBox "Largura de " & Format(w, "0") & " mm passa do limite configurado (" & largMax & " mm)." & vbCrLf & "Diminua a largura (esquerda-direita) do que vai cortar.", vbExclamation, APP Exit Sub End If If MsgBox("Cortar agora" & IIf(espelhado, " (ESPELHADO)", "") & "?" & vbCrLf & vbCrLf & _ "Largura (cabeca): " & Format(w, "0") & " mm" & vbCrLf & _ "Comprimento (rolo): " & Format(h, "0") & " mm" & vbCrLf & vbCrLf & _ "Confira o material e a origem marcada na maquina.", _ vbQuestion + vbYesNo + vbDefaultButton2, APP) <> vbYes Then ActiveDocument.Unit = unidadeAnt Exit Sub End If gDir = x0 + w: gEsq = x0: gBase = y0 gN = 0 ReDim gPartes(0 To 255): ReDim gChaves(0 To 255) Dim s As Shape For Each s In ActiveSelectionRange.Shapes.FindShapes() AdicionarCurva s Next s ' ordena: comeca pela peca mais perto da origem e avanca em faixas ao longo do rolo Dim i As Long, j As Long, k As Double, t As String For i = 1 To gN - 1 k = gChaves(i): t = gPartes(i): j = i - 1 Do While j >= 0 If gChaves(j) <= k Then Exit Do gChaves(j + 1) = gChaves(j): gPartes(j + 1) = gPartes(j): j = j - 1 Loop gChaves(j + 1) = k: gPartes(j + 1) = t Next i Dim hp As String hp = "IN;PA;" For i = 0 To gN - 1 hp = hp & gPartes(i) Next i If gRoloEmX Then hp = hp & "PU" & U(gMargemInicio + h) & ",0;" Else hp = hp & "PU0," & U(gMargemInicio + h) & ";" End If ActiveDocument.Unit = unidadeAnt Enviar hp End Sub Private Function Enviar(ByVal hp As String) As Boolean Dim porta As String, baud As String porta = GetSetting(APP, "Config", "Porta", "COM3") baud = GetSetting(APP, "Config", "Baud", "9600") Dim arq As String, lg As String, f As Integer, cmd As String arq = Environ$("TEMP") & "\cortar_plotter.plt" lg = Environ$("TEMP") & "\cortar_plotter.log" f = FreeFile Open arq For Output As #f Print #f, hp Close #f cmd = "cmd /c (mode " & porta & ": baud=" & baud & " parity=n data=8 stop=1 & copy /b """ & arq & """ " & porta & ") > """ & lg & """ 2>&1" CreateObject("WScript.Shell").Run cmd, 0, True f = FreeFile Open lg For Input As #f Do While Not EOF(f) Line Input #f, cmd If InStr(1, cmd, "copiado", vbTextCompare) > 0 Or InStr(1, cmd, "copied", vbTextCompare) > 0 Then Enviar = True Loop Close #f If Not Enviar Then MsgBox "Nao consegui enviar para a " & porta & "." & vbCrLf & "Confira se a plotter esta ligada, o cabo conectado e se nenhum outro programa esta usando a porta.", vbExclamation, APP End If End Function Private Function U(ByVal mm As Double) As String U = CStr(CLng(mm * 40)) ' HPGL: 40 unidades por mm End Function Private Function Cabeca(ByVal x As Double) As Double ' distancia da origem no sentido da cabeca, em mm If gRoloEmX Xor gEspelhar Then Cabeca = gDir - x + gMargemLado Else Cabeca = x - gEsq + gMargemLado End If End Function Private Function P(ByVal x As Double, ByVal y As Double) As String Dim rolo As Double rolo = y - gBase + gMargemInicio If gRoloEmX Then P = U(rolo) & "," & U(Cabeca(x)) Else P = U(Cabeca(x)) & "," & U(rolo) End If End Function Private Sub Guardar(ByVal parte As String, ByVal minRolo As Double, ByVal minCab As Double) If gN > UBound(gPartes) Then ReDim Preserve gPartes(0 To gN * 2): ReDim Preserve gChaves(0 To gN * 2) End If gPartes(gN) = parte gChaves(gN) = Int(minRolo / FAIXA) * 1000000# + minCab gN = gN + 1 End Sub Private Sub AdicionarCurva(s As Shape) On Error GoTo fim If s.Type = cdrGroupShape Then Exit Sub Dim c As Curve, sp As SubPath, sg As Segment Dim r As String, i As Long, t As Double, mt As Double Dim ax As Double, ay As Double, bx As Double, by As Double Dim cx As Double, cy As Double, dx As Double, dy As Double Dim minRolo As Double, minCab As Double Set c = s.DisplayCurve If c Is Nothing Then Exit Sub For Each sp In c.SubPaths minRolo = sp.StartNode.PositionY - gBase: minCab = Cabeca(sp.StartNode.PositionX) r = "PU" & P(sp.StartNode.PositionX, sp.StartNode.PositionY) & ";" For Each sg In sp.Segments ax = sg.StartNode.PositionX: ay = sg.StartNode.PositionY dx = sg.EndNode.PositionX: dy = sg.EndNode.PositionY If dy - gBase < minRolo Then minRolo = dy - gBase If Cabeca(dx) < minCab Then minCab = Cabeca(dx) If sg.Type = cdrCurveSegment Then bx = sg.StartingControlPointX: by = sg.StartingControlPointY cx = sg.EndingControlPointX: cy = sg.EndingControlPointY For i = 1 To PASSOS_CURVA t = i / PASSOS_CURVA: mt = 1 - t r = r & "PD" & P(mt ^ 3 * ax + 3 * mt ^ 2 * t * bx + 3 * mt * t ^ 2 * cx + t ^ 3 * dx, mt ^ 3 * ay + 3 * mt ^ 2 * t * by + 3 * mt * t ^ 2 * cy + t ^ 3 * dy) & ";" Next i Else r = r & "PD" & P(dx, dy) & ";" End If Next sg If sp.Closed Then r = r & "PD" & P(sp.StartNode.PositionX, sp.StartNode.PositionY) & ";" Guardar r, minRolo, minCab Next sp fim: End Sub