Change so that new KCL files respect the length unit setting

This commit is contained in:
Jonathan Tran
2025-03-12 20:31:43 -04:00
parent 494d8bdf4c
commit e94bdeb2ce
3 changed files with 57 additions and 5 deletions

View File

@ -34,7 +34,8 @@ import { settingsActor, useSettings } from 'machines/appMachine'
import { createRouteCommands } from 'lib/commandBarConfigs/routeCommandConfig' import { createRouteCommands } from 'lib/commandBarConfigs/routeCommandConfig'
import { useToken } from 'machines/appMachine' import { useToken } from 'machines/appMachine'
import { createNamedViewsCommand } from 'lib/commandBarConfigs/namedViewsConfig' import { createNamedViewsCommand } from 'lib/commandBarConfigs/namedViewsConfig'
import { reportRejection } from 'lib/trap' import { err, reportRejection } from 'lib/trap'
import { newKclFile } from 'lang/project'
type MachineContext<T extends AnyStateMachine> = { type MachineContext<T extends AnyStateMachine> = {
state: StateFrom<T> state: StateFrom<T>
@ -248,7 +249,12 @@ export const FileMachineProvider = ({
createdPath createdPath
) )
} else { } else {
await window.electron.writeFile(createdPath, input.content ?? '') const codeToWrite = newKclFile(
input.content ?? '',
settings.modeling.defaultUnit.current
)
if (err(codeToWrite)) return Promise.reject(codeToWrite)
await window.electron.writeFile(createdPath, codeToWrite)
} }
} }
@ -277,7 +283,12 @@ export const FileMachineProvider = ({
}) })
createdName = name createdName = name
createdPath = path createdPath = path
await window.electron.writeFile(createdPath, input.content ?? '') const codeToWrite = newKclFile(
input.content ?? '',
settings.modeling.defaultUnit.current
)
if (err(codeToWrite)) return Promise.reject(codeToWrite)
await window.electron.writeFile(createdPath, codeToWrite)
} }
return { return {

View File

@ -32,6 +32,8 @@ import {
} from 'lib/constants' } from 'lib/constants'
import { codeManager, kclManager } from 'lib/singletons' import { codeManager, kclManager } from 'lib/singletons'
import { Project } from 'lib/project' import { Project } from 'lib/project'
import { newKclFile } from 'lang/project'
import { err } from 'lib/trap'
type MachineContext<T extends AnyStateMachine> = { type MachineContext<T extends AnyStateMachine> = {
state?: StateFrom<T> state?: StateFrom<T>
@ -120,7 +122,13 @@ const ProjectsContextWeb = ({ children }: { children: React.ReactNode }) => {
createFile: fromPromise(async ({ input }) => { createFile: fromPromise(async ({ input }) => {
// Browser version doesn't navigate, just overwrites the current file // Browser version doesn't navigate, just overwrites the current file
clearImportSearchParams() clearImportSearchParams()
codeManager.updateCodeStateEditor(input.code || '')
const codeToWrite = newKclFile(
input.code ?? '',
settings.modeling.defaultUnit.current
)
if (err(codeToWrite)) return Promise.reject(codeToWrite)
codeManager.updateCodeStateEditor(codeToWrite)
await codeManager.writeToFile() await codeManager.writeToFile()
await kclManager.executeCode(true) await kclManager.executeCode(true)
@ -412,7 +420,12 @@ const ProjectsContextDesktop = ({
}) })
fileName = name fileName = name
await window.electron.writeFile(path, input.code || '') const codeToWrite = newKclFile(
input.code ?? '',
settings.modeling.defaultUnit.current
)
if (err(codeToWrite)) return Promise.reject(codeToWrite)
await window.electron.writeFile(path, codeToWrite)
return { return {
message, message,

28
src/lang/project.ts Normal file
View File

@ -0,0 +1,28 @@
import { UnitLength } from '@rust/kcl-lib/bindings/ModelingCmd'
import {
changeKclSettings,
DEFAULT_DEFAULT_ANGLE_UNIT,
unitAngleToUnitAng,
unitLengthToUnitLen,
} from './wasm'
import { DEFAULT_DEFAULT_LENGTH_UNIT } from 'lib/settings/settingsTypes'
/**
* Create a new KCL file with the given initial content and default length unit.
*/
export function newKclFile(
initialContent: string,
defaultLengthUnit: UnitLength
): string | Error {
// If the default length unit is the same as the default default length unit,
// there's no need to add the attribute.
if (defaultLengthUnit === DEFAULT_DEFAULT_LENGTH_UNIT) {
return initialContent
}
return changeKclSettings(initialContent, {
defaultLengthUnits: unitLengthToUnitLen(defaultLengthUnit),
defaultAngleUnits: unitAngleToUnitAng(DEFAULT_DEFAULT_ANGLE_UNIT),
stdPath: null,
})
}