diff --git a/src/main/java/com/neuronrobotics/bowlerstudio/scripting/Build123dLoader.java b/src/main/java/com/neuronrobotics/bowlerstudio/scripting/Build123dLoader.java index 89db491b..018b296b 100644 --- a/src/main/java/com/neuronrobotics/bowlerstudio/scripting/Build123dLoader.java +++ b/src/main/java/com/neuronrobotics/bowlerstudio/scripting/Build123dLoader.java @@ -1,11 +1,15 @@ package com.neuronrobotics.bowlerstudio.scripting; + import static com.neuronrobotics.bowlerstudio.scripting.DownloadManager.*; import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.InvalidRemoteException; @@ -23,25 +27,50 @@ public class Build123dLoader implements IScriptingLanguage { @Override - public Object inlineScriptRun(CSGDatabaseInstance db,File code, ArrayList args) throws Exception { - File stl = File.createTempFile(sanitizeString(code.getName()), ".stl"); - stl.deleteOnExit(); - HashMap params=new HashMap(); - if(args!=null) { + public Object inlineScriptRun(CSGDatabaseInstance db, File code, ArrayList args) throws Exception { + ArrayList params = new ArrayList<>(); + if (args != null) { Object o = args.get(0); - if(HashMap.class.isInstance(o)) { - params=(HashMap)o; + if (HashMap.class.isInstance(o)) { + params = (ArrayList) o; + } + } + Path tempDir = Files.createTempDirectory("build123d-"); + + List back = toCSG(db, code, tempDir, params); + return back; + } + + public static List toCSG(CSGDatabaseInstance db, ArrayList params) + throws InvalidRemoteException, TransportException, GitAPIException, IOException, InterruptedException { + Path tempDir = Files.createTempDirectory("build123d-"); + + return toCSG(db, null, tempDir, params); + } + + public static List toCSG(CSGDatabaseInstance db, Path stl, ArrayList params) + throws InvalidRemoteException, TransportException, GitAPIException, IOException, InterruptedException { + return toCSG(db, null, stl, params); + } + + public static List toCSG(CSGDatabaseInstance db, File code, Path stl, ArrayList params) + throws InvalidRemoteException, TransportException, GitAPIException, IOException, InterruptedException { + toSTLFile(code, stl, params); + + ArrayList back = new ArrayList(); + for (File f : stl.toFile().listFiles()) { + Log.debug("Loading " + f); + if (f.getName().toLowerCase().endsWith(".stl")) { + CSG b = Vitamins.get(db, f, true); + b.setColor(Color.ANTIQUEWHITE); + back.add(b); } } - - toSTLFile(code,stl,params); - CSG back = Vitamins.get(db,stl,true); - back.setColor(Color.ANTIQUEWHITE); return back; } @Override - public Object inlineScriptRun(CSGDatabaseInstance db,String code, ArrayList args) throws Exception { + public Object inlineScriptRun(CSGDatabaseInstance db, String code, ArrayList args) throws Exception { throw new RuntimeException("Build123d can not run from a string"); } @@ -58,35 +87,40 @@ public ArrayList getFileExtenetion() { return ext; } + public static void toSTLFile(Path stlout, ArrayList params) + throws InvalidRemoteException, TransportException, GitAPIException, IOException, InterruptedException { + toSTLFile(null, stlout, params); + } - - - public static void toSTLFile(File build123dScript,File stlout, HashMap params) throws InvalidRemoteException, TransportException, GitAPIException, IOException, InterruptedException { + public static void toSTLFile(File build123dScript, Path stlout, ArrayList params) + throws IOException, InterruptedException { File exe = getConfigExecutable("build123d", null); File dir = getDestinationDir("build123d"); - if(params==null) - params=new HashMap(); + if (params == null) + params = new ArrayList(); ArrayList args = new ArrayList<>(); - if(stlout.exists()) - stlout.delete(); + if (!stlout.toFile().isDirectory()) + throw new RuntimeException("Output file should be a directory"); args.add(exe.getAbsolutePath()); args.add("run"); args.add("python"); - for(String key:params.keySet()) { - args.add("-D"); - args.add(key+"="+params.get(key)); + if (build123dScript != null) + args.add(build123dScript.getAbsolutePath()); + else + args.add(".venv/lib64/python3.12/site-packages/build123d_cli/build123d_cli.py"); + for (Object key : params) { + args.add(key.toString()); } - args.add(build123dScript.getAbsolutePath()); -// args.add("-o"); -// args.add(stlout.getAbsolutePath()); + + args.add("export_directory"); + args.add(stlout.toFile().getAbsolutePath()); legacySystemRun(null, dir, System.out, args); } + @Override public String getDefaultContents() { - return "from build123d import *\n" - + "\n" - + "cube = Box(10, 10, 10)"; + return "from build123d import *\n" + "\n" + "cube = Box(10, 10, 10)"; } @Override @@ -94,15 +128,16 @@ public boolean getIsTextFile() { return true; } - public static void main(String[] args) throws InvalidRemoteException, TransportException, GitAPIException, IOException, InterruptedException { + public static void main(String[] args) + throws InvalidRemoteException, TransportException, GitAPIException, IOException, InterruptedException { Build123dLoader loader = new Build123dLoader(); Log.enableDebugPrint(); // create test file File testblend = new File("build123dTest.py"); - if(!testblend.exists()) + if (!testblend.exists()) loader.getDefaultContents(testblend); - HashMap params = new HashMap(); - toSTLFile(testblend, new File("build123dTest.py.stl"),params); + ArrayList params = new ArrayList(); + toSTLFile(testblend, new File("build123dTest").toPath(), params); } } diff --git a/test/java/src/junit/bowler/Build123dTest.java b/test/java/src/junit/bowler/Build123dTest.java index 5e614a07..b77598ce 100644 --- a/test/java/src/junit/bowler/Build123dTest.java +++ b/test/java/src/junit/bowler/Build123dTest.java @@ -3,6 +3,7 @@ import static org.junit.Assert.*; import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; @@ -19,16 +20,14 @@ public class Build123dTest { @Test - @Ignore public void test() throws Exception { - Build123dLoader loader = new Build123dLoader(); Log.enableDebugPrint(); //ScriptingEngine.pull("https://github.com/madhephaestus/CaDoodle-Example-Objects.git"); ArrayList parts = (ArrayList)ScriptingEngine.gitScriptRun(CSGDatabase.getInstance(), "https://github.com/madhephaestus/CaDoodle-Example-Objects.git", "build123d/gggears.groovy"); -// if(parts.size()==0) -// throw new IOException("Failed to create files"); + if(parts.size()==0) + throw new IOException("Failed to create files"); } diff --git a/test/java/src/junit/bowler/GitHub.java b/test/java/src/junit/bowler/GitHub.java index d0ea3004..d13d0d11 100644 --- a/test/java/src/junit/bowler/GitHub.java +++ b/test/java/src/junit/bowler/GitHub.java @@ -32,42 +32,6 @@ public class GitHub { private static boolean shutdownInProgress; - static { - Runtime.getRuntime().addShutdownHook(new Thread(() -> { - if (shutdownInProgress) return; - shutdownInProgress = true; - - Log.info("Beginning graceful shutdown..."); - - try { - // 1. Stop JavaFX - if ( isPlatformInitialized()&&Platform.isFxApplicationThread()) { - Platform.exit(); - Thread.sleep(300); - } - - - // 3. Force GC - System.gc(); - System.runFinalization(); - - // 4. Final wait - Thread.sleep(500); - - Log.info("Shutdown complete"); - } catch (Exception e) { - Log.error( e); - } - }, "Shutdown-Hook")); - } - private static boolean isPlatformInitialized() { - try { - Platform.runLater(() -> {}); - return true; - } catch (IllegalStateException e) { - return false; - } - } @Test public void test() throws Exception {