import { v4 as uuidv4 } from 'uuid' import { faFileExport, faXmark } from '@fortawesome/free-solid-svg-icons' import { ActionButton } from './ActionButton' import Modal from 'react-modal' 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?: { button?: string icon?: string bg?: string } } 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 defaultStorage = 'embedded' const [storage, setStorage] = React.useState(defaultStorage) function openModal() { setIsOpen(true) } function closeModal() { setIsOpen(false) } // Default to gltf and embedded. const initialValues: OutputFormat = { type: defaultType, storage: defaultStorage, presentation: 'pretty', } const formik = useFormik({ initialValues, onSubmit: (values: OutputFormat) => { // Set the default coords. if ( values.type === 'obj' || values.type === 'ply' || values.type === 'step' || values.type === 'stl' ) { // Set the default coords. // In the future we can make this configurable. // But for now, its probably best to keep it consistent with the // UI. values.coords = { forward: { axis: 'y', direction: 'negative', }, up: { axis: 'z', direction: 'positive', }, } } if ( values.type === 'obj' || values.type === 'stl' || values.type === 'ply' ) { values.units = baseUnit } if ( values.type === 'ply' || values.type === 'stl' || values.type === 'gltf' ) { // Set the storage type. values.storage = storage } if (values.type === 'ply' || values.type === 'stl') { values.selection = { type: 'default_scene' } } engineCommandManager.sendSceneCommand({ type: 'modeling_cmd_req', cmd: { type: 'export', // By default let's leave this blank to export the whole scene. // In the future we might want to let the user choose which entities // 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(), }) closeModal() }, }) return ( <> {children || 'Export'}

Export your design

{(type === 'gltf' || type === 'ply' || type === 'stl') && ( )}
Close Export
) }