Skip to content

Commit

Permalink
creating a solid now automatically creates truck faces
Browse files Browse the repository at this point in the history
  • Loading branch information
MattFerraro committed May 15, 2024
1 parent 492463e commit d5bdf07
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 3 deletions.
99 changes: 98 additions & 1 deletion packages/cadmium/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,104 @@ use truck_shapeops::{and, or, ShapeOpsCurve, ShapeOpsSurface};
use truck_topology::{Shell, Solid};

fn main() {
simple_cube();
stacked_cubes();
}

fn stacked_cubes() {
let mut el = EvolutionLog::new();

let workbench_id = el.append(Operation::CreateWorkbench {
nonce: "Workbench 1".to_string(),
});
el.append(Operation::SetWorkbenchName {
workbench_id: workbench_id.clone(),
name: "Main Workbench".to_string(),
});

// Create the Top Plane
let top_plane_id = el.append(Operation::CreatePlane {
nonce: "the top plane".to_string(),
workbench_id: workbench_id.clone(),
});
el.append(Operation::SetPlaneName {
plane_id: top_plane_id.clone(),
name: "Top".to_string(),
});
let set_plane = el.append(Operation::SetPlane {
plane_id: top_plane_id.clone(),
plane: Plane::top(),
});
let top_plane_real = el.realize_plane(&top_plane_id);

// Create the sketch
let sketch_id = el.append(Operation::CreateSketch {
nonce: "top sketch".to_string(),
workbench_id: workbench_id.clone(),
});
el.append(Operation::SetSketchName {
sketch_id: sketch_id.clone(),
name: "Original Sketch".to_string(),
});
el.append(Operation::SetSketchPlane {
sketch_id: sketch_id.clone(),
plane_id: top_plane_real.clone(),
});

// make a square
el.append(Operation::AddSketchLine {
sketch_id: sketch_id.clone(),
start: (0.0, 0.0),
end: (0.0, 100.0),
});
el.append(Operation::AddSketchLine {
sketch_id: sketch_id.clone(),
start: (0.0, 100.0),
end: (100.0, 100.0),
});
el.append(Operation::AddSketchLine {
sketch_id: sketch_id.clone(),
start: (100.0, 100.0),
end: (100.0, 0.0),
});
el.append(Operation::AddSketchLine {
sketch_id: sketch_id.clone(),
start: (100.0, 0.0),
end: (0.0, 0.0),
});
let realized_sketch = el.realize_sketch(&sketch_id);

// extrude the square
let extrusion_id = el.append(Operation::CreateExtrusion {
workbench_id: workbench_id.clone(),
nonce: "first extrusion".to_string(),
});
el.append(Operation::SetExtrusionName {
extrusion_id: extrusion_id.clone(),
name: "Extrude1".to_string(),
});
el.append(Operation::SetExtrusionDepth {
extrusion_id: extrusion_id.clone(),
depth: 100.0,
});
el.append(Operation::SetExtrusionSketch {
extrusion_id: extrusion_id.clone(),
sketch_id: realized_sketch.clone(),
});
el.append(Operation::SetExtrusionFaces {
extrusion_id: extrusion_id.clone(),
faces: vec![0],
});

el.realize_extrusion(&extrusion_id);

// print each solid
// for (solid_id, solid) in el.solids.iter() {
// println!("Solid: {:?}", solid.truck_solid);
// solid.save_as_obj("first_solid.obj", 0.01);
// }

el.git_log();
// el.to_project();
}

fn simple_cube() {
Expand Down
75 changes: 73 additions & 2 deletions packages/cadmium/src/oplog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ use std::hash::{Hash, Hasher};
use std::process::id;
use std::vec;
use truck_polymesh::faces;
use truck_topology::{
FaceDisplayFormat, ShellDisplayFormat, SolidDisplayFormat, VertexDisplayFormat,
WireDisplayFormat,
};

use crate::extrusion::Solid;
use crate::project::{
Plane, PlaneDescription, Project, RealPlane, RealSketch, StepData, Workbench,
};
use crate::sketch::Face;
use FaceDisplayFormat as FDF;
use ShellDisplayFormat as ShDF;
use SolidDisplayFormat as SDF;
use VertexDisplayFormat as VDF;
use WireDisplayFormat as WDF;

pub type Sha = String;

Expand Down Expand Up @@ -411,6 +420,16 @@ impl EvolutionLog {

pub fn realize_extrusion(&mut self, extrusion_id: &Sha) {
let (workbench_idx, extrusion_idx) = self.extrusions.get(extrusion_id).unwrap();
// iterate through all of self.workbenches to find the one whose index matches workbench_idx
let workbench_sha = self.workbenches.iter().find_map(|(key, &val)| {
if val == *workbench_idx {
Some(key)
} else {
None
}
});
let workbench_sha = workbench_sha.unwrap().clone();

let mut wb = self.project.workbenches.get_mut(*workbench_idx).unwrap();
let step = wb.history.get_mut(*extrusion_idx).unwrap();

Expand All @@ -425,8 +444,19 @@ impl EvolutionLog {
nonce: name,
solid: solid.clone(),
};

self.append(new_op);
self.solids.insert(self.cursor.clone(), solid);
self.solids.insert(self.cursor.clone(), solid.clone());
for boundary in solid.truck_solid.boundaries() {
boundary.face_iter().for_each(|face| {
let o = Operation::CreateTruckFace {
workbench_id: workbench_sha.clone(),
solid_id: self.cursor.clone(),
face: face.clone(),
};
self.append(o);
});
}
}
} else {
unreachable!()
Expand Down Expand Up @@ -720,6 +750,15 @@ pub enum Operation {
sketch_id: Sha,
face: Face,
},
CreateTruckFace {
workbench_id: Sha,
solid_id: Sha,
face: truck_topology::Face<
truck_meshalgo::prelude::cgmath::Point3<f64>,
truck_modeling::Curve,
truck_modeling::Surface,
>,
},

CreateExtrusion {
workbench_id: Sha,
Expand Down Expand Up @@ -845,6 +884,11 @@ impl Operation {
sketch_id,
face,
} => hasher.update(format!("{workbench_id}-{sketch_id}-{face:?}").as_bytes()),
Operation::CreateTruckFace {
workbench_id,
solid_id,
face,
} => hasher.update(format!("{workbench_id}-{solid_id}-{face:?}").as_bytes()),

Operation::CreateExtrusion {
nonce,
Expand Down Expand Up @@ -1061,6 +1105,22 @@ impl Operation {
face
)
}
Operation::CreateTruckFace {
workbench_id,
solid_id,
face,
} => {
format!(
"CreateTruckFace: {} {} {:?}",
workbench_id.to_owned()[..num_chars].to_string(),
solid_id.to_owned()[..num_chars].to_string(),
face.display(FDF::BoundariesAndID {
wire_format: WDF::VerticesList {
vertex_format: VDF::AsPoint,
},
})
)
}
Operation::CreateExtrusion {
nonce,
workbench_id,
Expand Down Expand Up @@ -1125,7 +1185,18 @@ impl Operation {
)
}
Operation::CreateSolid { nonce, solid } => {
format!("CreateSolid: {nonce} {solid:?}")
format!(
"CreateSolid: {nonce} {:?}",
solid.truck_solid.display(SDF::ShellsList {
shell_format: ShDF::FacesList {
face_format: FDF::LoopsList {
wire_format: WDF::VerticesList {
vertex_format: VDF::AsPoint
}
}
}
})
)
}
}
}
Expand Down

0 comments on commit d5bdf07

Please sign in to comment.