diff --git a/src/RSIPI/cli.py b/src/RSIPI/cli.py index 843047d..5dbf329 100644 --- a/src/RSIPI/cli.py +++ b/src/RSIPI/cli.py @@ -1,5 +1,6 @@ from .rsi_client import RSIClient from .kuka_visualizer import KukaRSIVisualizer +from .krl_parser import KRLParser class RSICommandLineInterface: """Command-Line Interface for controlling RSI Client.""" @@ -78,6 +79,9 @@ class RSICommandLineInterface: csv_file = parts[1] export = ("export" in parts) self.visualize(csv_file, export) + elif cmd == "krlparse" and len(parts) == 4: + src_file, dat_file, output_file = parts[1], parts[2], parts[3] + self.krl_parse(src_file, dat_file, output_file) else: print("❌ Unknown command. Type 'help' for a list of commands.") @@ -154,6 +158,17 @@ Available Commands: except Exception as e: print(f"❌ Failed to visualize '{csv_file}': {e}") + def krl_parse(self, src_file, dat_file, output_file): + """CLI method to parse KRL files and output CSV.""" + try: + parser = KRLParser(src_file, dat_file) + parser.parse_src() + parser.parse_dat() + parser.export_csv(output_file) + print(f"✅ KRL files parsed successfully. Output CSV: {output_file}") + except Exception as e: + print(f"❌ Failed to parse KRL files: {e}") + if __name__ == "__main__": config_file = "RSI_EthernetConfig.xml" cli = RSICommandLineInterface(config_file) diff --git a/src/RSIPI/rsi_api.py b/src/RSIPI/rsi_api.py index 6a9c71c..dd3e559 100644 --- a/src/RSIPI/rsi_api.py +++ b/src/RSIPI/rsi_api.py @@ -6,6 +6,7 @@ import matplotlib.pyplot as plt from .rsi_client import RSIClient from .graphing import RSIGraphing from .kuka_visualizer import KukaRSIVisualizer +from .krl_parser import KRLParser class RSIAPI: """RSI API for programmatic control, including alerts, logging, graphing, and data retrieval.""" @@ -212,4 +213,22 @@ class RSIAPI: visualizer.plot_force_trends() if export: - visualizer.export_graphs() \ No newline at end of file + visualizer.export_graphs() + + def parse_krl_to_csv(self, src_file, dat_file, output_file): + """ + Parses KRL files (.src, .dat) and exports coordinates to CSV. + + Args: + src_file (str): Path to KRL .src file. + dat_file (str): Path to KRL .dat file. + output_file (str): Path for output CSV file. + """ + try: + parser = KRLParser(src_file, dat_file) + parser.parse_src() + parser.parse_dat() + parser.export_csv(output_file) + return f"✅ KRL data successfully exported to {output_file}" + except Exception as e: + return f"❌ Error parsing KRL files: {e}" \ No newline at end of file diff --git a/src/RSIPI/test_rsipi.py b/src/RSIPI/test_rsipi.py index 81e7df7..4f8a5cf 100644 --- a/src/RSIPI/test_rsipi.py +++ b/src/RSIPI/test_rsipi.py @@ -103,5 +103,34 @@ class TestRSIPI(unittest.TestCase): if os.path.exists("exports"): shutil.rmtree("exports") + def test_krl_parsing(self): + """Test KRL parsing functionality.""" + src_file = "test.src" + dat_file = "test.dat" + output_file = "test_output.csv" + + # Create temporary dummy KRL files for testing + with open(src_file, "w") as f_src, open(dat_file, "w") as f_dat: + f_src.write("PDAT_ACT=PPDAT1\nPDAT_ACT=PPDAT2\n") + f_dat.write("DECL E6POS XP1={X 10,Y 20,Z 30,A 0,B 90,C 180,S 2,T 1,E1 0,E2 0}\n") + f_dat.write("DECL E6POS XP2={X 40,Y 50,Z 60,A 0,B 90,C 180,S 2,T 1,E1 0,E2 0}\n") + + response = self.api.parse_krl_to_csv(src_file, dat_file, output_file) + self.assertIn("✅ KRL data successfully exported", response) + + # Verify the CSV content + import pandas as pd + df = pd.read_csv(output_file) + self.assertEqual(len(df), 2) + self.assertEqual(df.iloc[0]["X"], 10) + self.assertEqual(df.iloc[1]["Y"], 50) + + # Clean up temporary files + import os + os.remove(src_file) + os.remove(dat_file) + os.remove(output_file) + + if __name__ == '__main__': unittest.main() \ No newline at end of file