Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,25 +27,50 @@
public class Build123dLoader implements IScriptingLanguage {

@Override
public Object inlineScriptRun(CSGDatabaseInstance db,File code, ArrayList<Object> args) throws Exception {
File stl = File.createTempFile(sanitizeString(code.getName()), ".stl");
stl.deleteOnExit();
HashMap<String,Double> params=new HashMap<String, Double>();
if(args!=null) {
public Object inlineScriptRun(CSGDatabaseInstance db, File code, ArrayList<Object> args) throws Exception {
ArrayList<Object> params = new ArrayList<>();
if (args != null) {
Object o = args.get(0);
if(HashMap.class.isInstance(o)) {
params=(HashMap<String,Double>)o;
if (HashMap.class.isInstance(o)) {
params = (ArrayList<Object>) o;
}
}
Path tempDir = Files.createTempDirectory("build123d-");

List<CSG> back = toCSG(db, code, tempDir, params);
return back;
}

public static List<CSG> toCSG(CSGDatabaseInstance db, ArrayList<Object> params)
throws InvalidRemoteException, TransportException, GitAPIException, IOException, InterruptedException {
Path tempDir = Files.createTempDirectory("build123d-");

return toCSG(db, null, tempDir, params);
}

public static List<CSG> toCSG(CSGDatabaseInstance db, Path stl, ArrayList<Object> params)
throws InvalidRemoteException, TransportException, GitAPIException, IOException, InterruptedException {
return toCSG(db, null, stl, params);
}

public static List<CSG> toCSG(CSGDatabaseInstance db, File code, Path stl, ArrayList<Object> params)
throws InvalidRemoteException, TransportException, GitAPIException, IOException, InterruptedException {
toSTLFile(code, stl, params);

ArrayList<CSG> back = new ArrayList<CSG>();
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<Object> args) throws Exception {
public Object inlineScriptRun(CSGDatabaseInstance db, String code, ArrayList<Object> args) throws Exception {
throw new RuntimeException("Build123d can not run from a string");
}

Expand All @@ -58,51 +87,57 @@ public ArrayList<String> getFileExtenetion() {
return ext;
}

public static void toSTLFile(Path stlout, ArrayList<Object> params)
throws InvalidRemoteException, TransportException, GitAPIException, IOException, InterruptedException {
toSTLFile(null, stlout, params);
}



public static void toSTLFile(File build123dScript,File stlout, HashMap<String,Double> params) throws InvalidRemoteException, TransportException, GitAPIException, IOException, InterruptedException {
public static void toSTLFile(File build123dScript, Path stlout, ArrayList<Object> params)
throws IOException, InterruptedException {
File exe = getConfigExecutable("build123d", null);
File dir = getDestinationDir("build123d");
if(params==null)
params=new HashMap<String, Double>();
if (params == null)
params = new ArrayList<Object>();
ArrayList<String> 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
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<String,Double> params = new HashMap<String, Double>();
toSTLFile(testblend, new File("build123dTest.py.stl"),params);
ArrayList<Object> params = new ArrayList<Object>();
toSTLFile(testblend, new File("build123dTest").toPath(), params);
}

}
7 changes: 3 additions & 4 deletions test/java/src/junit/bowler/Build123dTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<CSG > parts = (ArrayList<CSG>)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");


}
Expand Down
36 changes: 0 additions & 36 deletions test/java/src/junit/bowler/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down