Grid scale proportional to user's units

The grid lines should be 1 unit apart, where the unit is whatever the
user's defaultUnitLength setting is, e.g. 1mm or 1cm or 1in apart.
This commit is contained in:
Adam Chalmers
2025-05-29 16:40:52 -05:00
parent 13c4de77c3
commit 9419381068
5 changed files with 39 additions and 9 deletions

View File

@ -369,13 +369,15 @@ pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static {
settings: &crate::ExecutorSettings,
source_range: SourceRange,
id_generator: &mut IdGenerator,
grid_scale_unit: Option<kcmc::units::UnitLength>,
) -> Result<(), crate::errors::KclError> {
// Set the edge visibility.
self.set_edge_visibility(settings.highlight_edges, source_range, id_generator)
.await?;
// Send the command to show the grid.
self.modify_grid(!settings.show_grid, source_range, id_generator)
self.modify_grid(!settings.show_grid, grid_scale_unit, source_range, id_generator)
.await?;
// We do not have commands for changing ssao on the fly.
@ -829,6 +831,7 @@ pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static {
async fn modify_grid(
&self,
hidden: bool,
base_unit: Option<kcmc::units::UnitLength>,
source_range: SourceRange,
id_generator: &mut IdGenerator,
) -> Result<(), KclError> {
@ -843,6 +846,17 @@ pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static {
)
.await?;
let grid_scale = if let Some(units) = base_unit {
// The grid is 10x10, so setting the value to 10 means that
// users whose unit is cm will see each cell as 1cm by 1cm,
// which is useful for sketching.
ModelingCmd::from(mcmd::SetGridScale { value: 10.0, units })
} else {
ModelingCmd::from(mcmd::SetGridAutoScale {})
};
self.batch_modeling_cmd(id_generator.next_uuid(), source_range, &grid_scale)
.await?;
// Hide/show the grid scale text.
self.batch_modeling_cmd(
id_generator.next_uuid(),

View File

@ -593,6 +593,12 @@ impl ExecutorContext {
pub async fn run_with_caching(&self, program: crate::Program) -> Result<ExecOutcome, KclErrorWithOutputs> {
assert!(!self.is_mock());
let grid_scale = program
.meta_settings()
.ok()
.flatten()
.map(|s| s.default_length_units)
.map(kcmc::units::UnitLength::from);
let (program, mut exec_state, preserve_mem, cached_body_items, imports_info) = if let Some(OldAstState {
ast: old_ast,
@ -623,7 +629,7 @@ impl ExecutorContext {
if reapply_settings
&& self
.engine
.reapply_settings(&self.settings, Default::default(), old_state.id_generator())
.reapply_settings(&self.settings, Default::default(), old_state.id_generator(), grid_scale)
.await
.is_err()
{
@ -647,7 +653,7 @@ impl ExecutorContext {
if reapply_settings
&& self
.engine
.reapply_settings(&self.settings, Default::default(), old_state.id_generator())
.reapply_settings(&self.settings, Default::default(), old_state.id_generator(), grid_scale)
.await
.is_err()
{
@ -697,7 +703,7 @@ impl ExecutorContext {
CacheResult::NoAction(true) => {
if self
.engine
.reapply_settings(&self.settings, Default::default(), old_state.id_generator())
.reapply_settings(&self.settings, Default::default(), old_state.id_generator(), grid_scale)
.await
.is_ok()
{
@ -1089,8 +1095,19 @@ impl ExecutorContext {
let _stats = crate::log::LogPerfStats::new("Interpretation");
// Re-apply the settings, in case the cache was busted.
let grid_scale = program
.meta_settings()
.ok()
.flatten()
.map(|s| s.default_length_units)
.map(kcmc::units::UnitLength::from);
self.engine
.reapply_settings(&self.settings, Default::default(), exec_state.id_generator())
.reapply_settings(
&self.settings,
Default::default(),
exec_state.id_generator(),
grid_scale,
)
.await
.map_err(KclErrorWithOutputs::no_outputs)?;