Skip to content

Commit

Permalink
Fix message parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitris Zervas <[email protected]>
  • Loading branch information
dzervas authored and MattFerraro committed May 27, 2024
1 parent 6581476 commit 238d3e2
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 76 deletions.
63 changes: 63 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions applications/web/src/routes/(CADmium)/NewRectangleTool.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
// there WAS an anchor point, so we should create a rectangle!
// if the anchor point doesn't exist, then we should create a point
if (anchorPoint.id === null) anchorPoint.id = addPointToSketch(sketchIndex, anchorPoint.twoD!, false)
if (anchorPoint.id === null || anchorPoint.id === undefined) anchorPoint.id = addPointToSketch(sketchIndex, anchorPoint.twoD!, false)
// if (point?.id && anchorPoint.id) {
// // if the point exists, then we should create a circle between the two existing points
Expand All @@ -44,9 +44,9 @@
if (point) point.id = addPointToSketch(sketchIndex, point.twoD!, false)
// }
// log("setting rectangle")
log("setting rectangle", anchorPoint, point)
addRectangleBetweenPoints(sketchIndex, +anchorPoint.id!, +point.id!)
anchorPoint = null
// anchorPoint = null
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cadmium/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ geo = "0.26.0"
serde_with = "3.4.0"
crc32fast = "1.3.2"
indexmap = "2.1.0"
anyhow = "1.0.86"
anyhow = { version = "1.0.86", features = ["backtrace"] }
thiserror = "1.0.61"
strum = { version = "0.26.2", features = ["derive"] }

Expand Down
2 changes: 1 addition & 1 deletion packages/cadmium/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "A CAD program written in Rust with a JS front end",
"scripts": {
"dev": "pnpm run build:dev; onchange 'src/**/*.rs' -- pnpm run build:dev",
"build:dev": "cargo check && wasm-pack build --no-pack --target web --dev",
"build:dev": "cargo check && RUST_BACKTRACE=1 wasm-pack build --no-pack --target web --dev",
"build": "wasm-pack build --target web --no-pack",
"clean": "rm -rf target pkg node_modules",
"test": "cargo test",
Expand Down
4 changes: 2 additions & 2 deletions packages/cadmium/src/extrusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,8 @@ mod tests {
let realization = workbench.realize(1000);
let keys = Vec::from_iter(realization.solids.keys());

realization.save_solid_as_step_file(keys[0], "target/test.step");
realization.save_solid_as_obj_file(keys[0], "target/test.obj", 0.001);
realization.save_solid_as_step_file(keys[0], "pkg/test.step");
realization.save_solid_as_obj_file(keys[0], "pkg/test.obj", 0.001);
}

}
5 changes: 3 additions & 2 deletions packages/cadmium/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ pub enum MessageResult {
impl From<Result<String, anyhow::Error>> for MessageResult {
fn from(result: Result<String, anyhow::Error>) -> Self {
match result {
Ok(msg) => MessageResult::Success(msg),
Err(e) => MessageResult::Error(e.to_string()),
// TODO: The Success should be a stable enum
Ok(msg) => MessageResult::Success(format!("{{ {} }}", msg)),
Err(e) => MessageResult::Error(e.backtrace().to_string()),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/cadmium/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,10 @@ pub mod tests {
println!("Final solid: {:?}", final_solid.truck_solid);
let mut mesh = final_solid.truck_solid.triangulation(0.02).to_polygon();
mesh.put_together_same_attrs();
let file = std::fs::File::create("target/bruno.obj").unwrap();
let file = std::fs::File::create("pkg/bruno.obj").unwrap();
obj::write(&mesh, file).unwrap();

let file = std::fs::File::create("target/bruno.json").unwrap();
let file = std::fs::File::create("pkg/bruno.json").unwrap();
serde_json::to_writer(file, &p).unwrap();
}

Expand Down
26 changes: 4 additions & 22 deletions packages/shared/projectUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import type {
Entity,
ExtrusionHistoryStep,
HistoryStep,
Message,
MessageHistory,
PlaneHistoryStep,
PointHistoryStep,
Expand All @@ -26,7 +25,7 @@ import type {
WithTarget,
WorkBench
} from "./types"
import type { Realization as WasmRealization } from "cadmium"
import type { Realization as WasmRealization, Message } from "cadmium"
import {
isDeleteArcs,
isDeleteCircles,
Expand Down Expand Up @@ -73,11 +72,9 @@ export function arraysEqual(a: any[], b: any[]) {

function sendWasmMessage(message: Message) {
let wp = get(wasmProject)
const messageStr = JSON.stringify(message)
log("[sendWasmMessage] sending message:", message)
let reply = wp.send_message(messageStr)
log("[sendWasmMessage] reply:", reply)
let result = JSON.parse(reply)
let result = wp.send_message(message)
log("[sendWasmMessage] reply:", result)

messageHistory.update((history: MessageHistory[]) => {
log("[sendWasmMessage] [messageHistory.update] update:", { message, result })
Expand Down Expand Up @@ -321,26 +318,11 @@ export function addPointToSketch(sketchIdx: string, point: Vector2Like, hidden:
const reply = sendWasmMessage(message)
// log("[addPointToSketch sendWasmMessage]", "message:", message, "reply:", reply)

const exampleMessage = {
NewPointOnSketch2: {
workbench_id: 0,
sketch_id: "Sketch-1",
x: -35.45466015827466,
y: 34.131346610327284,
hidden: false
}
}
const exampleReplySuccess = {
success: {
id: "1"
}
}

if (!reply.success)
console.error("ERROR [projectUtils.ts addPointToSketch sendWasmMessage]", "message:", message, "reply:", reply)

workbenchIsStale.set(true)
return reply.success.id
return JSON.parse(reply.success).id
}

export function renameStep(stepIdx: number, newName: string): void {
Expand Down
45 changes: 2 additions & 43 deletions packages/shared/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Message, MessageResult } from "cadmium"
import type { Vector2, Vector3, Vector2Like, Vector3Like } from "three"

interface IDictionary<TValue> {
Expand Down Expand Up @@ -312,51 +313,9 @@ interface RenameWorkbench {
new_name: string
}

interface RenameProject {
new_name: string
}

export type Message_GeneratedFromRust =
| { RenameWorkbench: { workbench_id: number; new_name: string } }
| { RenameStep: { workbench_id: number; step_id: number; new_name: string } }
| { RenameProject: { new_name: string } }
| { DeleteLines: { workbench_id: number; sketch_id: string; line_ids: number[] } }
| { DeleteArcs: { workbench_id: number; sketch_id: string; arc_ids: number[] } }
| { DeleteCircles: { workbench_id: number; sketch_id: string; circle_ids: number[] } }
| { NewPointOnSketch: { workbench_id: number; sketch_id: string; point_id: number; x: number; y: number } }
| { NewPointOnSketch2: { workbench_id: number; sketch_id: string; x: number; y: number; hidden: boolean } }
| { NewCircleBetweenPoints: { workbench_id: number; sketch_id: string; center_id: number; edge_id: number } }
| { NewRectangleBetweenPoints: { workbench_id: number; sketch_id: string; start_id: number; end_id: number } }
| { NewLineOnSketch: { workbench_id: number; sketch_id: string; start_point_id: number; end_point_id: number } }
| { DeleteLineSegment: { workbench_id: number; sketch_name: string; line_segment_id: number } }
| { StepSketch: { workbench_id: number; sketch_name: string; steps: number } }
| { SolveSketch: { workbench_id: number; sketch_name: string; max_steps: number } }
| { NewSketchOnPlane: { workbench_id: number; sketch_name: string; plane_id: string } }
| { SetSketchPlane: { workbench_id: number; sketch_id: string; plane_id: string } }
| { DeleteSketch: { workbench_id: number; sketch_name: string } }
| { NewExtrusion: { workbench_id: number; extrusion_name: string; sketch_id: string; face_ids: number[]; length: number; offset: number; direction: Direction } }
| { UpdateExtrusion: { workbench_id: number; extrusion_name: string; extrusion_id: string; sketch_id: string; face_ids: number[]; length: number; offset: number; direction: Direction } }
| { UpdateExtrusionLength: { workbench_id: number; extrusion_name: string; length: number } }

type Message =
| { UpdateExtrusion: UpdateExtrusion }
| { SetSketchPlane: SetSketchPlane }
| { NewSketchOnPlane: NewSketchOnPlane }
| { NewExtrusion: NewExtrusion }
| { DeleteLines: DeleteLines }
| { DeleteArcs: DeleteArcs }
| { DeleteCircles: DeleteCircles }
| { NewRectangleBetweenPoints: NewRectangleBetweenPoints }
| { NewCircleBetweenPoints: NewCircleBetweenPoints }
| { NewLineOnSketch: NewLineOnSketch }
| { NewPointOnSketch2: NewPointOnSketch2 }
| { RenameStep: RenameStep }
| { RenameWorkbench: RenameWorkbench }
| { RenameProject: RenameProject }

interface MessageHistory {
message: Message
result: any
result: MessageResult
}

interface Point2D {
Expand Down

0 comments on commit 238d3e2

Please sign in to comment.