Skip to content

Commit 1509290

Browse files
committed
adding basic parameter system
1 parent e49ad88 commit 1509290

2 files changed

Lines changed: 120 additions & 2 deletions

File tree

src/main/java/com/neuronrobotics/bowlerstudio/scripting/cadoodle/CaDoodleFile.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,11 @@ public class CaDoodleFile {
7878
@Expose(serialize = true, deserialize = true)
7979
private TransformNR rulerLocation = new TransformNR();
8080
@Expose(serialize = true, deserialize = true)
81-
82-
// Non Serialised private variables
8381
private TransformNR workplane = new TransformNR();
82+
@Expose(serialize = true, deserialize = true)
83+
private CaDoodleParameters parameters;
84+
85+
8486
private File self;
8587
// @Expose (serialize = false, deserialize = false)
8688
// private List<CSG> currentState = new ArrayList<CSG>();
@@ -1516,4 +1518,11 @@ public ThumbnailImage getImageEngine() {
15161518
private void setImageEngine(ThumbnailImage imageEngine) {
15171519
this.imageEngine = imageEngine;
15181520
}
1521+
1522+
public CaDoodleParameters getParameters() {
1523+
if(parameters==null)
1524+
parameters=new CaDoodleParameters();
1525+
parameters.setDb(csgDBinstance);
1526+
return parameters;
1527+
}
15191528
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.neuronrobotics.bowlerstudio.scripting.cadoodle;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import java.util.Map.Entry;
7+
8+
import com.google.gson.annotations.Expose;
9+
import com.neuronrobotics.bowlerstudio.scripting.ScriptingEngine;
10+
11+
import eu.mihosoft.vrl.v3d.parametrics.CSGDatabaseInstance;
12+
13+
public class CaDoodleParameters {
14+
@Expose(serialize = true, deserialize = true)
15+
private ArrayList<Map.Entry<String, String>> params;
16+
17+
private HashMap<String, Double> values = null;
18+
19+
private CSGDatabaseInstance db;
20+
21+
public String getString(String key) {
22+
for (Map.Entry<String, String> m : getParams()) {
23+
if (m.getKey().contentEquals(key))
24+
return m.getValue();
25+
}
26+
throw new NumberFormatException();
27+
}
28+
public void delete(String key) {
29+
Map.Entry<String, String> set = null;
30+
for (Map.Entry<String, String> m : getParams()) {
31+
if (m.getKey().contentEquals(key)) {
32+
set = m;
33+
break;
34+
}
35+
}
36+
if(set!=null)
37+
params.remove(set);
38+
}
39+
public void set(String key, Object value) {
40+
Map.Entry<String, String> set = null;
41+
for (Map.Entry<String, String> m : getParams()) {
42+
if (m.getKey().contentEquals(key)) {
43+
set = m;
44+
break;
45+
}
46+
}
47+
if (set == null) {
48+
set =Map.entry(key,value.toString());
49+
getParams().add(set);
50+
}
51+
set.setValue(value.toString());
52+
values=null;
53+
}
54+
public ArrayList<String> keys(){
55+
ArrayList<String> keys=new ArrayList<String>();
56+
for(Entry<String, String> e:getParams()) {
57+
keys.add(e.getKey());
58+
}
59+
return keys;
60+
}
61+
private ArrayList<Map.Entry<String, String>> getParams() {
62+
if (params == null) {
63+
params = new ArrayList<Map.Entry<String, String>>();
64+
}
65+
return params;
66+
}
67+
68+
public double getValue(String key) throws Exception {
69+
return getValues().get(key).doubleValue();
70+
}
71+
72+
private HashMap<String, Double> getValues() throws Exception {
73+
if (values == null) {
74+
String code = "HashMap<String,Double> numbers = new HashMap<>()\n";
75+
String vars = "";
76+
String equs = "";
77+
78+
for (Map.Entry<String, String> m : getParams()) {
79+
// System.out.println(line);
80+
String value = m.getValue();
81+
String key =m.getKey();
82+
String reconstructed = key + "=" + value;
83+
try {
84+
Double.parseDouble(value);
85+
vars += reconstructed + "\n";
86+
vars += "numbers.put(\"" + key + "\"," + key + ");\n";
87+
} catch (NumberFormatException ex) {
88+
equs += reconstructed + "\n";
89+
equs += "numbers.put(\"" + key + "\"," + key + ");\n";
90+
}
91+
}
92+
code += vars;
93+
code += equs;
94+
code += "return numbers";
95+
// println code
96+
values = (HashMap<String, Double>) ScriptingEngine.inlineScriptStringRun(getDb(), code, null, "Groovy");
97+
}
98+
return values;
99+
}
100+
101+
public CSGDatabaseInstance getDb() {
102+
return db;
103+
}
104+
105+
public void setDb(CSGDatabaseInstance db) {
106+
this.db = db;
107+
}
108+
109+
}

0 commit comments

Comments
 (0)