From ffc47f8f40ee1d415d19fdcf0d40bcc354c018de Mon Sep 17 00:00:00 2001 From: Jess Frazelle Date: Wed, 27 Sep 2023 15:42:16 -0700 Subject: [PATCH] actually use the units the user sets (#727) * actually use the units the user sets Signed-off-by: Jess Frazelle * update kittycad lib Signed-off-by: Jess Frazelle * next Signed-off-by: Jess Frazelle * fix tests Signed-off-by: Jess Frazelle * updates Signed-off-by: Jess Frazelle * fix bug Signed-off-by: Jess Frazelle * fix types Signed-off-by: Jess Frazelle --------- Signed-off-by: Jess Frazelle --- package.json | 2 +- src/components/ExportButton.tsx | 49 +++++++++++++++--- src/components/ProjectSidebarMenu.test.tsx | 23 ++++++-- src/machines/settingsMachine.ts | 5 +- src/wasm-lib/.config/nextest.toml | 4 +- src/wasm-lib/Cargo.lock | 4 +- .../tests/executor/outputs/close_arc.png | Bin 97187 -> 98091 bytes yarn.lock | 8 +-- 8 files changed, 74 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index 0effb430d..0315ae782 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "@fortawesome/react-fontawesome": "^0.2.0", "@headlessui/react": "^1.7.13", "@headlessui/tailwindcss": "^0.2.0", - "@kittycad/lib": "^0.0.39", + "@kittycad/lib": "^0.0.40", "@lezer/javascript": "^1.4.7", "@open-rpc/client-js": "^1.8.1", "@react-hook/resize-observer": "^1.2.6", diff --git a/src/components/ExportButton.tsx b/src/components/ExportButton.tsx index aa6d75652..ee6ec8c69 100644 --- a/src/components/ExportButton.tsx +++ b/src/components/ExportButton.tsx @@ -6,8 +6,12 @@ import React from 'react' import { useFormik } from 'formik' import { Models } from '@kittycad/lib' import { engineCommandManager } from '../lang/std/engineConnection' +import { useGlobalStateContext } from 'hooks/useGlobalStateContext' type OutputFormat = Models['OutputFormat_type'] +type OutputTypeKey = OutputFormat['type'] +type ExtractStorageTypes = T extends { storage: infer U } ? U : never +type StorageUnion = ExtractStorageTypes interface ExportButtonProps extends React.PropsWithChildren { className?: { @@ -19,9 +23,18 @@ interface ExportButtonProps extends React.PropsWithChildren { export const ExportButton = ({ children, className }: ExportButtonProps) => { const [modalIsOpen, setIsOpen] = React.useState(false) + const { + settings: { + state: { + context: { baseUnit }, + }, + }, + } = useGlobalStateContext() const defaultType = 'gltf' - const [type, setType] = React.useState(defaultType) + const [type, setType] = React.useState(defaultType) + const defaultStorage = 'embedded' + const [storage, setStorage] = React.useState(defaultStorage) function openModal() { setIsOpen(true) @@ -34,7 +47,7 @@ export const ExportButton = ({ children, className }: ExportButtonProps) => { // Default to gltf and embedded. const initialValues: OutputFormat = { type: defaultType, - storage: 'embedded', + storage: defaultStorage, presentation: 'pretty', } const formik = useFormik({ @@ -62,6 +75,17 @@ export const ExportButton = ({ children, className }: ExportButtonProps) => { }, } } + if (values.type === 'obj' || values.type === 'stl') { + values.units = baseUnit + } + if ( + values.type === 'ply' || + values.type === 'stl' || + values.type === 'gltf' + ) { + // Set the storage type. + values.storage = storage + } engineCommandManager.sendSceneCommand({ type: 'modeling_cmd_req', cmd: { @@ -71,6 +95,7 @@ export const ExportButton = ({ children, className }: ExportButtonProps) => { // in the scene to export. In that case, you'd pass the IDs thru here. entity_ids: [], format: values, + source_unit: baseUnit, }, cmd_id: uuidv4(), }) @@ -105,7 +130,17 @@ export const ExportButton = ({ children, className }: ExportButtonProps) => { id="type" name="type" onChange={(e) => { - setType(e.target.value) + setType(e.target.value as OutputTypeKey) + if (e.target.value === 'gltf') { + // Set default to embedded. + setStorage('embedded') + } else if (e.target.value === 'ply') { + // Set default to ascii. + setStorage('ascii') + } else if (e.target.value === 'stl') { + // Set default to ascii. + setStorage('ascii') + } formik.handleChange(e) }} className="bg-chalkboard-20 dark:bg-chalkboard-90 w-full" @@ -123,10 +158,10 @@ export const ExportButton = ({ children, className }: ExportButtonProps) => {