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
12 changes: 12 additions & 0 deletions .claude/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
"runtimeExecutable": "trunk",
"runtimeArgs": ["serve", "--port", "8081", "--dist", "target/dist-fresh"],
"port": 8081
},
{
"name": "parts-manager-web",
"runtimeExecutable": "trunk",
"runtimeArgs": ["serve", "parts-manager/index.html", "--port", "8082", "--dist", "target/dist-parts-manager"],
"port": 8082
},
{
"name": "parts-server",
"runtimeExecutable": "cargo",
"runtimeArgs": ["run", "-p", "openecraft-server", "--", "--token", "local-test-token", "--data", "target/parts-db-dev.json"],
"port": 8090
}
]
}
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ jobs:
target/release/openecraft.exe
target/release/openecraft-server
target/release/openecraft-server.exe
target/release/openecraft-parts-manager
target/release/openecraft-parts-manager.exe

web:
runs-on: ubuntu-latest
Expand All @@ -39,7 +41,13 @@ jobs:
targets: wasm32-unknown-unknown
- run: cargo install trunk --locked
- run: trunk build --release
- run: trunk build --release
working-directory: parts-manager
- uses: actions/upload-artifact@v4
with:
name: openecraft-web
path: dist
- uses: actions/upload-artifact@v4
with:
name: openecraft-parts-manager-web
path: parts-manager/dist
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/target
/dist
/parts-manager/dist
/.idea/
# Local parts server databases
parts-db.json
Expand Down
21 changes: 15 additions & 6 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ Run these before considering a change done. All of them must pass without warnin
```bash
cargo test --workspace
cargo clippy --workspace --all-targets
cargo clippy -p openecraft --target wasm32-unknown-unknown
cargo clippy -p openecraft -p openecraft-parts-manager --target wasm32-unknown-unknown
```

- Desktop: `cargo run --release`
- Parts server: `cargo run -p openecraft-server -- --token <secret>` (http://127.0.0.1:8090)
- Parts manager: `cargo run -p openecraft-parts-manager`; web: `cd parts-manager && trunk serve` (http://127.0.0.1:8082)
- Web: `trunk serve` (dev server on http://127.0.0.1:8080), `trunk build --release`
- The web target needs `rustup target add wasm32-unknown-unknown` and `cargo install trunk --locked`.

Expand All @@ -25,11 +26,16 @@ cargo clippy -p openecraft --target wasm32-unknown-unknown
| --- | --- | --- |
| `openecraft` | `.` (`src/`) | Editor (desktop + web) – the root package, built by trunk |
| `openecraft-parts` | `crates/openecraft-parts` | Shared library: part model, function templates, CSV/JSON, catalog, parts server API types |
| `openecraft-ui` | `crates/openecraft-ui` | Shared egui library: part tree / editor / previews, parts server client, file dialogs |
| `openecraft-server` | `server/` | Standalone parts database server (axum, JSON file storage) |
| `openecraft-parts-manager` | `parts-manager/` | Standalone parts server maintenance app (desktop + web, own `index.html` / `Trunk.toml`) |

- Code needed by more than one executable belongs in a library crate under `crates/`,
never duplicated. The library must stay free of UI and platform dependencies
(only `serde` / `serde_json`).
never duplicated.
- `openecraft-parts` stays free of UI and platform dependencies (only `serde` /
`serde_json`); pure logic such as 3D math (`vec3`) goes here.
- `openecraft-ui` holds egui widgets and platform code shared by the editor and the
parts manager. It must not depend on an application crate.
- API types and request paths of the parts server live in `openecraft_parts::api`; change
server and client together.
- New crates are added to `[workspace] members` in the root `Cargo.toml`; the
Expand Down Expand Up @@ -68,10 +74,11 @@ violations. Do not weaken or delete that test to make a change pass.

- **Glyphs:** every character used in a string literal must exist in the bundled
egui fonts. Native builds have no font fallback, so missing characters render as
empty boxes (the browser hides the problem). `src/glyphs/tests.rs` checks this; if
empty boxes (the browser hides the problem). `src/glyphs/tests.rs` checks this for
every UI crate (editor, `openecraft-parts`, `openecraft-ui`, parts manager); if
it fails, pick a character the fonts contain instead of disabling the check. Keep
the egui feature `monochrome_emoji_fonts` enabled.
- **eframe features:** default features are disabled in `Cargo.toml`; when changing
- **eframe features:** default features are disabled in both app manifests; when changing
the feature list keep `clipboard` (desktop copy/paste), `persistence`,
`default_fonts` and `glow`.
- **Demo project:** `src/sample.rs` is checked by `sample/tests.rs` (no check errors,
Expand All @@ -81,7 +88,9 @@ violations. Do not weaken or delete that test to make a change pass.
- **Undo:** edits are captured by snapshot comparison (`history.rs`); just mutate the
`Project`, no explicit undo bookkeeping is needed.
- **Platform code:** keep `#[cfg(target_arch = "wasm32")]` branches confined to
`io.rs` and `main.rs`.
`crates/openecraft-ui/src/io.rs` and the `main.rs` files.
- **Parts manager:** keep its editing rules (drafts, renames, unsaved changes) in
`parts-manager/src/session.rs` (no UI code, unit tested); `app.rs` only draws.
- **Style:** match the surrounding code; keep comments as sparse as the existing ones.

## Commits
Expand Down
37 changes: 34 additions & 3 deletions Cargo.lock

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

22 changes: 3 additions & 19 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ name = "openecraft"
path = "src/main.rs"

[workspace]
members = [".", "crates/openecraft-parts", "server"]
members = [".", "crates/openecraft-parts", "crates/openecraft-ui", "server", "parts-manager"]

[dependencies]
eframe = { version = "0.37", default-features = false, features = [
Expand All @@ -29,32 +29,16 @@ egui = { version = "0.37", features = ["monochrome_emoji_fonts"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
log = "0.4"
rfd = "0.17"
ehttp = "0.7"
openecraft-parts = { path = "crates/openecraft-parts" }
openecraft-ui = { path = "crates/openecraft-ui" }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
env_logger = "0.11"

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
js-sys = "0.3"
web-sys = { version = "0.3", features = [
"Window",
"Document",
"Element",
"HtmlCanvasElement",
"HtmlAnchorElement",
"HtmlInputElement",
"Blob",
"BlobPropertyBag",
"Url",
"File",
"FileList",
"FileReader",
"Event",
] }
web-sys = { version = "0.3", features = ["Window", "Document", "Element", "HtmlCanvasElement"] }

[profile.release]
opt-level = 2
Expand Down
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,23 @@ cargo run --release -p openecraft-server -- --bind 0.0.0.0:8090 --data parts-db.
- The server speaks plain HTTP; put it behind a TLS reverse proxy when it is reachable
from outside your network.

### Parts manager

`openecraft-parts-manager` is a standalone application that maintains the parts of a
parts server: create new parts, edit existing ones (including 2D / 3D models), rename
and delete parts, upload CSV / JSON parts files and export all parts. It uses the same
part editor as the editor's parts management and runs on the desktop and in the browser.

```bash
cargo run --release -p openecraft-parts-manager # desktop
cd parts-manager && trunk serve # web, http://127.0.0.1:8082
cd parts-manager && trunk build --release # static files in parts-manager/dist/
```

Enter the server URL and the access token (kept in memory only) and click *Connect*.
Changes are saved to the server with *Save* (`Ctrl+S`); the editor asks before unsaved
changes are discarded and before a part is deleted.

### Tests

```bash
Expand All @@ -133,9 +150,10 @@ cargo test --workspace

| File | Purpose |
| --- | --- |
| `crates/openecraft-parts` | Shared library: part model, function templates, CSV/JSON, catalog, server API types |
| `crates/openecraft-parts` | Shared library: part model, function templates, CSV/JSON, catalog, server API types, 3D math |
| `crates/openecraft-ui` | Shared egui library: part editor and previews, parts server client, file dialogs |
| `server/` | `openecraft-server`: parts database server (REST API, JSON storage) |
| `src/parts_server.rs` | Client for the parts server |
| `parts-manager/` | `openecraft-parts-manager`: standalone parts server maintenance (desktop + web) |
| `src/model.rs` | Project data model (pages, symbols, wires, parts) and page layout constants |
| `src/symbols.rs` | Built-in IEC symbol library |
| `src/netlist.rs` | Connectivity analysis (nets / potentials, junctions) |
Expand All @@ -147,7 +165,7 @@ cargo test --workspace
| `src/panel.rs` | Panel layout model, device sizes, rail snapping, collisions, duct routing |
| `src/panel_ui.rs` | Panel layout workspace: navigator, 2D editor, 3D view, properties |
| `src/parts.rs` | Parts database, function templates, project parts, CSV import/export |
| `src/parts_ui.rs` | Parts management window and part selection dialog |
| `src/parts_ui.rs` | Parts management window, part selection dialog, parts server panel |
| `src/app.rs` | Application UI (menus, panels, dialogs) |
| `src/svg.rs` | SVG / print export |
| `src/io.rs` | Native file dialogs and browser up/downloads |
| `src/io.rs` | File kinds of the editor (dialogs are in `openecraft-ui`) |
1 change: 1 addition & 0 deletions crates/openecraft-parts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod csv;
mod graphics;
mod part;
mod text;
pub mod vec3;

pub use catalog::default_catalog;
pub use csv::{from_csv, import, to_csv};
Expand Down
23 changes: 23 additions & 0 deletions crates/openecraft-parts/src/part.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,26 @@ impl PartsDb {
pub fn sort(&mut self) {
self.parts.sort_by(|a, b| natural_cmp(&a.number, &b.number));
}

/// Adds / replaces parts (e.g. downloaded from a parts server). Returns (added, updated).
pub fn merge(&mut self, parts: Vec<Part>) -> (usize, usize) {
let (mut added, mut updated) = (0, 0);
for part in parts {
match self.parts.iter_mut().find(|p| p.number == part.number) {
Some(existing) if *existing != part => {
*existing = part;
updated += 1;
}
Some(_) => {}
None => {
self.parts.push(part);
added += 1;
}
}
}
self.sort();
(added, updated)
}
}

pub fn upsert(list: &mut Vec<Part>, part: Part) {
Expand All @@ -363,3 +383,6 @@ pub fn unique_number(list: &[Part], base: &str) -> String {
.find(|n| !list.iter().any(|p| &p.number == n))
.unwrap()
}

#[cfg(test)]
mod tests;
21 changes: 21 additions & 0 deletions crates/openecraft-parts/src/part/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use super::*;

#[test]
fn merge_adds_and_updates() {
let mut db = PartsDb { parts: vec![Part { number: "A".into(), description: "old".into(), ..Default::default() }] };
let incoming = vec![
Part { number: "A".into(), description: "new".into(), ..Default::default() },
Part { number: "B".into(), ..Default::default() },
];
assert_eq!(db.merge(incoming.clone()), (1, 1));
assert_eq!(db.get("A").unwrap().description, "new");
// Same data again: nothing changes.
assert_eq!(db.merge(incoming), (0, 0));
}

#[test]
fn unique_numbers() {
let list = vec![Part { number: "X".into(), ..Default::default() }, Part { number: "X_1".into(), ..Default::default() }];
assert_eq!(unique_number(&list, "Y"), "Y");
assert_eq!(unique_number(&list, "X"), "X_2");
}
47 changes: 47 additions & 0 deletions crates/openecraft-parts/src/vec3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! Small 3D vector helpers and an orbit camera, shared by the 3D views.

pub type V3 = [f32; 3];

pub fn sub(a: V3, b: V3) -> V3 {
[a[0] - b[0], a[1] - b[1], a[2] - b[2]]
}

pub fn dot(a: V3, b: V3) -> f32 {
a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
}

pub fn cross(a: V3, b: V3) -> V3 {
[a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]]
}

pub fn norm(a: V3) -> V3 {
let l = dot(a, a).sqrt().max(1e-6);
[a[0] / l, a[1] / l, a[2] / l]
}

/// Camera orbiting around `target` (y up).
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Orbit {
pub yaw: f32,
pub pitch: f32,
pub dist: f32,
pub target: V3,
}

impl Orbit {
/// Eye position and camera axes (right, up, forward).
pub fn basis(&self) -> (V3, V3, V3, V3) {
let eye = [
self.target[0] + self.dist * self.pitch.cos() * self.yaw.sin(),
self.target[1] + self.dist * self.pitch.sin(),
self.target[2] + self.dist * self.pitch.cos() * self.yaw.cos(),
];
let fwd = norm(sub(self.target, eye));
let right = norm(cross(fwd, [0.0, 1.0, 0.0]));
let up = cross(right, fwd);
(eye, right, up, fwd)
}
}

#[cfg(test)]
mod tests;
22 changes: 22 additions & 0 deletions crates/openecraft-parts/src/vec3/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use super::*;

fn close(a: V3, b: V3) -> bool {
a.iter().zip(b).all(|(x, y)| (x - y).abs() < 1e-5)
}

#[test]
fn vector_basics() {
assert_eq!(dot([1.0, 2.0, 3.0], [4.0, 5.0, 6.0]), 32.0);
assert_eq!(cross([1.0, 0.0, 0.0], [0.0, 1.0, 0.0]), [0.0, 0.0, 1.0]);
assert!(close(norm([3.0, 0.0, 4.0]), [0.6, 0.0, 0.8]));
}

#[test]
fn orbit_from_the_front_looks_down_minus_z() {
let cam = Orbit { yaw: 0.0, pitch: 0.0, dist: 10.0, target: [1.0, 2.0, 3.0] };
let (eye, right, up, fwd) = cam.basis();
assert!(close(eye, [1.0, 2.0, 13.0]));
assert!(close(fwd, [0.0, 0.0, -1.0]));
assert!(close(right, [1.0, 0.0, 0.0]));
assert!(close(up, [0.0, 1.0, 0.0]));
}
Loading
Loading