374 lines
16 KiB
Java
374 lines
16 KiB
Java
/*
|
|
* To change this license header, choose License Headers in Project Properties.
|
|
* To change this template file, choose Tools | Templates
|
|
* and open the template in the editor.
|
|
*/
|
|
package info.moow.hpgl2gcode;
|
|
|
|
import java.awt.Color;
|
|
import java.io.BufferedReader;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.io.PrintWriter;
|
|
import javax.swing.BorderFactory;
|
|
import javax.swing.JFileChooser;
|
|
import javax.swing.JFrame;
|
|
import javax.swing.UIManager;
|
|
import javax.swing.event.ChangeEvent;
|
|
import javax.swing.event.ChangeListener;
|
|
import javax.swing.text.DefaultCaret;
|
|
|
|
/**
|
|
*
|
|
* @author Dominik
|
|
*/
|
|
public class GUI extends javax.swing.JFrame {
|
|
|
|
private final int zup;
|
|
private final double unit;
|
|
private int feed;
|
|
|
|
private JFileChooser fc;
|
|
|
|
/**
|
|
* Creates new form GUI
|
|
*/
|
|
public GUI() {
|
|
initComponents();
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
setLocationRelativeTo(null);
|
|
setTitle("HPGL to GCode converter v1.1");
|
|
|
|
zup = 5;
|
|
unit = 40.2;
|
|
feed = 30000;
|
|
fc = new JFileChooser();
|
|
|
|
jSpinner1.setValue(feed);
|
|
jSpinner1.addChangeListener(new ChangeListener() {
|
|
@Override
|
|
public void stateChanged(ChangeEvent e) {
|
|
feed = (int)jSpinner1.getValue();
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* This method is called from within the constructor to initialize the form.
|
|
* WARNING: Do NOT modify this code. The content of this method is always
|
|
* regenerated by the Form Editor.
|
|
*/
|
|
@SuppressWarnings("unchecked")
|
|
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
|
|
private void initComponents() {
|
|
|
|
jLabel1 = new javax.swing.JLabel();
|
|
jSpinner1 = new javax.swing.JSpinner();
|
|
jLabel2 = new javax.swing.JLabel();
|
|
jScrollPane1 = new javax.swing.JScrollPane();
|
|
jTextArea1 = new javax.swing.JTextArea();
|
|
jScrollPane2 = new javax.swing.JScrollPane();
|
|
jTextArea2 = new javax.swing.JTextArea();
|
|
jButton1 = new javax.swing.JButton();
|
|
jLabel3 = new javax.swing.JLabel();
|
|
jLabel4 = new javax.swing.JLabel();
|
|
jProgressBar1 = new javax.swing.JProgressBar();
|
|
jButton2 = new javax.swing.JButton();
|
|
jButton3 = new javax.swing.JButton();
|
|
jButton4 = new javax.swing.JButton();
|
|
|
|
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
|
|
|
|
jLabel1.setText("Feed Rate");
|
|
|
|
jLabel2.setText("HPGL input");
|
|
|
|
jTextArea1.setColumns(20);
|
|
jTextArea1.setRows(5);
|
|
jScrollPane1.setViewportView(jTextArea1);
|
|
|
|
jTextArea2.setColumns(20);
|
|
jTextArea2.setRows(5);
|
|
jScrollPane2.setViewportView(jTextArea2);
|
|
|
|
jButton1.setText("Convert >");
|
|
jButton1.addActionListener(new java.awt.event.ActionListener() {
|
|
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
|
jButton1ActionPerformed(evt);
|
|
}
|
|
});
|
|
|
|
jLabel3.setText("GCode output");
|
|
|
|
jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
|
|
jLabel4.setText("HPGL to GCode converter v1.1");
|
|
|
|
jButton2.setText("Flush all");
|
|
jButton2.addActionListener(new java.awt.event.ActionListener() {
|
|
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
|
jButton2ActionPerformed(evt);
|
|
}
|
|
});
|
|
|
|
jButton3.setText("Open HPGL");
|
|
jButton3.addActionListener(new java.awt.event.ActionListener() {
|
|
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
|
jButton3ActionPerformed(evt);
|
|
}
|
|
});
|
|
|
|
jButton4.setText("Save GCode");
|
|
jButton4.addActionListener(new java.awt.event.ActionListener() {
|
|
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
|
jButton4ActionPerformed(evt);
|
|
}
|
|
});
|
|
|
|
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
|
|
getContentPane().setLayout(layout);
|
|
layout.setHorizontalGroup(
|
|
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
|
.addGroup(layout.createSequentialGroup()
|
|
.addContainerGap()
|
|
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
|
.addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
|
|
.addGroup(layout.createSequentialGroup()
|
|
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
|
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
|
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
|
|
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
|
|
.addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
|
|
.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
|
|
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
|
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
|
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
.addComponent(jLabel3)))
|
|
.addGroup(layout.createSequentialGroup()
|
|
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
|
.addGroup(layout.createSequentialGroup()
|
|
.addComponent(jLabel1)
|
|
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
|
.addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
|
|
.addComponent(jLabel2)
|
|
.addComponent(jLabel4))
|
|
.addGap(0, 0, Short.MAX_VALUE)))
|
|
.addContainerGap())
|
|
);
|
|
layout.setVerticalGroup(
|
|
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
|
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
|
|
.addContainerGap()
|
|
.addComponent(jLabel4)
|
|
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
|
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
|
|
.addComponent(jLabel1)
|
|
.addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
|
|
.addGap(9, 9, 9)
|
|
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
|
|
.addComponent(jLabel2)
|
|
.addComponent(jLabel3))
|
|
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
|
.addGroup(layout.createSequentialGroup()
|
|
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 155, Short.MAX_VALUE)
|
|
.addComponent(jButton3)
|
|
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
|
.addComponent(jButton2)
|
|
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
|
.addComponent(jButton1)
|
|
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
|
.addComponent(jButton4)
|
|
.addGap(172, 172, 172))
|
|
.addGroup(layout.createSequentialGroup()
|
|
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
|
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
|
|
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
|
|
.addComponent(jScrollPane1))
|
|
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
|
|
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
.addContainerGap())
|
|
);
|
|
|
|
pack();
|
|
}// </editor-fold>//GEN-END:initComponents
|
|
|
|
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
|
|
//convert button
|
|
jTextArea1.setBorder(BorderFactory.createLineBorder(Color.gray));
|
|
clearGCODE();
|
|
|
|
if (jTextArea1.getText().length() < 1) {
|
|
jTextArea1.setBorder(BorderFactory.createLineBorder(Color.red));
|
|
}
|
|
else {
|
|
appendGCODE("G21");
|
|
appendGCODE("G1 Z"+zup+" F"+feed);
|
|
|
|
String lines[] = jTextArea1.getText().split("\\r?\\n");
|
|
enableProgress(lines.length/1000, 0);
|
|
|
|
new Thread() {
|
|
public void run() {
|
|
for (int i=0; i < lines.length; i++) {
|
|
updateProgress(i/1000);
|
|
if (!(lines[i].contains("PA") && lines[i].contains("PU"))) {
|
|
if (lines[i].contains("PA")) {
|
|
int x = Integer.parseInt(lines[i].replace("PA ", "").replace(";", "").split(",")[0]);
|
|
int y = Integer.parseInt(lines[i].replace("PA ", "").replace(";", "").split(",")[1]);
|
|
appendGCODE("G1 X"+x/unit+" Y"+y/unit+" F"+feed);
|
|
}
|
|
else if (lines[i].contains("PU")) {
|
|
appendGCODE("M5 F"+feed);
|
|
}
|
|
else if (lines[i].contains("PD")) {
|
|
appendGCODE("M3 S1000 F"+feed);
|
|
}
|
|
}
|
|
else {
|
|
appendGCODE("M5 F"+feed);
|
|
appendGCODE("G1 X"+0+" Y"+0+" F"+feed);
|
|
}
|
|
}
|
|
disableProgress();
|
|
|
|
}
|
|
}.start();
|
|
|
|
}
|
|
|
|
}//GEN-LAST:event_jButton1ActionPerformed
|
|
|
|
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
|
|
jTextArea1.setText("");
|
|
jTextArea2.setText("");
|
|
}//GEN-LAST:event_jButton2ActionPerformed
|
|
|
|
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
|
|
int returnVal = fc.showOpenDialog(GUI.this);
|
|
if (returnVal == JFileChooser.APPROVE_OPTION) {
|
|
File f = new File(fc.getSelectedFile().getAbsolutePath());
|
|
if (!f.isDirectory()) {
|
|
//System.out.println("File");
|
|
try {
|
|
new Thread() {
|
|
public void run() {
|
|
FileInputStream fs;
|
|
try {
|
|
fs = new FileInputStream(fc.getSelectedFile().getAbsolutePath());
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
|
|
String strLine;
|
|
enableProgress((int)f.length()/8,0);
|
|
clearHPGL();
|
|
int i = 0;
|
|
while((strLine = br.readLine()) != null) {
|
|
i = i + (strLine.length()/5);
|
|
updateProgress(i);
|
|
//System.out.println((int)((double)i/((int)f.length()/8)*100)+": "+strLine);
|
|
appendHPGL(strLine);
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}.start();
|
|
}
|
|
catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}//GEN-LAST:event_jButton3ActionPerformed
|
|
|
|
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
|
|
int returnVal = fc.showSaveDialog(GUI.this);
|
|
if (returnVal == JFileChooser.APPROVE_OPTION) {
|
|
new Thread() {
|
|
public void run() {
|
|
try {
|
|
File f = new File(fc.getSelectedFile().getAbsolutePath());
|
|
if (!f.isDirectory()) {
|
|
PrintWriter writer = new PrintWriter(fc.getSelectedFile().getAbsolutePath(), "UTF-8");
|
|
String lines[] = jTextArea2.getText().split("\\r?\\n");
|
|
enableProgress(lines.length, 0);
|
|
for (int i=0; i < lines.length; i++) {
|
|
writer.println(lines[i]);
|
|
updateProgress(i);
|
|
}
|
|
disableProgress();
|
|
writer.close();
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}.start();
|
|
}
|
|
}//GEN-LAST:event_jButton4ActionPerformed
|
|
|
|
public void updateProgress(int p) {
|
|
jProgressBar1.setValue(p);
|
|
}
|
|
public void disableProgress() {
|
|
jProgressBar1.setValue(0);
|
|
jProgressBar1.setEnabled(false);
|
|
}
|
|
public void enableProgress(int max, int min) {
|
|
jProgressBar1.setMaximum(max);
|
|
jProgressBar1.setMinimum(min);
|
|
jProgressBar1.setValue(0);
|
|
jProgressBar1.setEnabled(true);
|
|
}
|
|
public void clearGCODE() {
|
|
jTextArea2.setText("");
|
|
DefaultCaret caret = (DefaultCaret) jTextArea2.getCaret();
|
|
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
|
|
}
|
|
public void appendGCODE(String l) {
|
|
jTextArea2.append(l+"\r\n");
|
|
}
|
|
public void clearHPGL() {
|
|
jTextArea1.setText("");
|
|
DefaultCaret caret = (DefaultCaret) jTextArea1.getCaret();
|
|
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
|
|
}
|
|
public void appendHPGL(String l) {
|
|
jTextArea1.append(l+"\r\n");
|
|
}
|
|
|
|
/**
|
|
* @param args the command line arguments
|
|
*/
|
|
public static void main(String args[]) {
|
|
try {
|
|
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
/* Create and display the form */
|
|
java.awt.EventQueue.invokeLater(new Runnable() {
|
|
public void run() {
|
|
new GUI().setVisible(true);
|
|
}
|
|
});
|
|
}
|
|
|
|
// Variables declaration - do not modify//GEN-BEGIN:variables
|
|
private javax.swing.JButton jButton1;
|
|
private javax.swing.JButton jButton2;
|
|
private javax.swing.JButton jButton3;
|
|
private javax.swing.JButton jButton4;
|
|
private javax.swing.JLabel jLabel1;
|
|
private javax.swing.JLabel jLabel2;
|
|
private javax.swing.JLabel jLabel3;
|
|
private javax.swing.JLabel jLabel4;
|
|
private javax.swing.JProgressBar jProgressBar1;
|
|
private javax.swing.JScrollPane jScrollPane1;
|
|
private javax.swing.JScrollPane jScrollPane2;
|
|
private javax.swing.JSpinner jSpinner1;
|
|
private javax.swing.JTextArea jTextArea1;
|
|
private javax.swing.JTextArea jTextArea2;
|
|
// End of variables declaration//GEN-END:variables
|
|
}
|