Compare commits

...

18 Commits

Author SHA1 Message Date
5bea90ad9a add API fallback for engine utils lib 2024-10-22 12:58:35 -07:00
d23ddc19eb Merge branch 'main' into mike/engine-utils-wasm 2024-10-22 10:27:12 -07:00
4bd7e02271 Support negative start and end in ranges (#4249)
Signed-off-by: Nick Cameron <nrc@ncameron.org>
2024-10-22 14:28:48 +13:00
26042790b6 Remove setInterval implementations from camera controls (#4255)
* remove setInterval implementations from camera controls

* fmt
2024-10-21 16:46:47 -07:00
af74f3bb05 Upgrade to rust toolchain 1.82.0 (#4245)
* Upgrade to rust toolchain 1.82.0

* Fix lint about variant being too large

* Fix lint about Err variant being too large
2024-10-21 16:35:04 -05:00
0bdedf5854 fix job name for printers (#4234)
Signed-off-by: Jess Frazelle <github@jessfraz.com>
Co-authored-by: Paul Tagliamonte <paul@zoo.dev>
2024-10-21 21:27:32 +00:00
d2c6b5cf3a Buffer file writes, because writing to file after every keypress has bad consequences (#4242)
* Buffer file writes, because writing to file after every keypress does bad things

* fix: kevin -- added timeouts for the time being since the workflow for saving data to disk is now changed

---------

Co-authored-by: Kevin Nadro <kevin@zoo.dev>
2024-10-21 15:07:20 -05:00
e65358f635 add needed tangential arc params to the serializable path 2024-10-11 11:11:27 -07:00
0a1201e680 cleanup 2024-10-11 10:55:55 -07:00
9db013e672 fmt 2024-10-11 10:47:17 -07:00
0196d72a2d mini code cleanup 2024-10-11 10:44:46 -07:00
e6af4078bd actual working arcs using engine utils 2024-10-10 18:19:26 -07:00
2b233dc705 it works! 2024-10-10 16:58:12 -07:00
b11e8af9c7 _ 2024-10-10 11:52:33 -07:00
c017847d7b Fix to convert JS Promise to async Rust 2024-10-09 00:09:19 -04:00
9635eea8c1 walking away 2024-10-08 13:15:24 -07:00
5a2df642b1 thrashing 2024-10-08 12:00:34 -07:00
621e41080e delme 2024-10-07 11:36:33 -07:00
33 changed files with 465 additions and 110 deletions

BIN
..env.development.local.swp Normal file

Binary file not shown.

View File

@ -1,3 +1,4 @@
src/wasm-lib/*
src/lib/engine-utils/engine.js
*.typegen.ts
packages/codemirror-lsp-client/dist/*

4
.gitignore vendored
View File

@ -66,3 +66,7 @@ venv
# electron
out/
# engine wasm utils
src/lib/engine-utils/engine.wasm
src/lib/engine-utils/engine.js

View File

@ -136,6 +136,9 @@ test.describe('when using the file tree to', () => {
)
await pasteCodeInEditor(kclCube)
// TODO: We have a timeout of 1s between edits to write to disk. If you reload the page too quickly it won't write to disk.
await tronApp.page.waitForTimeout(2000)
await renameFile(fromFile, toFile)
await tronApp.page.reload()
@ -222,9 +225,11 @@ test.describe('when using the file tree to', () => {
)
await pasteCodeInEditor(kclCube)
// TODO: We have a timeout of 1s between edits to write to disk. If you reload the page too quickly it won't write to disk.
await tronApp.page.waitForTimeout(2000)
const kcl1 = 'main.kcl'
const kcl2 = '2.kcl'
await createNewFileAndSelect(kcl2)
const kclCylinder = await fsp.readFile(
'src/wasm-lib/tests/executor/inputs/cylinder.kcl',
@ -232,6 +237,9 @@ test.describe('when using the file tree to', () => {
)
await pasteCodeInEditor(kclCylinder)
// TODO: We have a timeout of 1s between edits to write to disk. If you reload the page too quickly it won't write to disk.
await tronApp.page.waitForTimeout(2000)
await renameFile(kcl2, kcl1)
await test.step(`Postcondition: ${kcl1} still has the original content`, async () => {

View File

@ -64,6 +64,27 @@ export type ReactCameraProperties =
const lastCmdDelay = 50
class CameraRateLimiter {
lastSend?: Date = undefined
rateLimitMs: number = 16 //60 FPS
send = (f: () => void) => {
let now = new Date()
if (
this.lastSend === undefined ||
now.getTime() - this.lastSend.getTime() > this.rateLimitMs
) {
f()
this.lastSend = now
}
}
reset = () => {
this.lastSend = undefined
}
}
export class CameraControls {
engineCommandManager: EngineCommandManager
syncDirection: 'clientToEngine' | 'engineToClient' = 'engineToClient'
@ -77,9 +98,8 @@ export class CameraControls {
enableRotate = true
enablePan = true
enableZoom = true
zoomDataFromLastFrame?: number = undefined
// holds coordinates, and interaction
moveDataFromLastFrame?: [number, number, string] = undefined
moveSender: CameraRateLimiter = new CameraRateLimiter()
zoomSender: CameraRateLimiter = new CameraRateLimiter()
lastPerspectiveFov: number = 45
pendingZoom: number | null = null
pendingRotation: Vector2 | null = null
@ -171,6 +191,36 @@ export class CameraControls {
}
}
doMove = (interaction: any, coordinates: any) => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.engineCommandManager.sendSceneCommand({
type: 'modeling_cmd_req',
cmd: {
type: 'camera_drag_move',
interaction: interaction,
window: {
x: coordinates[0],
y: coordinates[1],
},
},
cmd_id: uuidv4(),
})
}
doZoom = (zoom: number) => {
this.handleStart()
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.engineCommandManager.sendSceneCommand({
type: 'modeling_cmd_req',
cmd: {
type: 'default_camera_zoom',
magnitude: (-1 * zoom) / window.devicePixelRatio,
},
cmd_id: uuidv4(),
})
this.handleEnd()
}
constructor(
isOrtho = false,
domElement: HTMLCanvasElement,
@ -258,49 +308,6 @@ export class CameraControls {
this.onCameraChange()
}
// Our stream is never more than 60fps.
// We can get away with capping our "virtual fps" to 60 then.
const FPS_VIRTUAL = 60
const doZoom = () => {
if (this.zoomDataFromLastFrame !== undefined) {
this.handleStart()
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.engineCommandManager.sendSceneCommand({
type: 'modeling_cmd_req',
cmd: {
type: 'default_camera_zoom',
magnitude:
(-1 * this.zoomDataFromLastFrame) / window.devicePixelRatio,
},
cmd_id: uuidv4(),
})
this.handleEnd()
}
this.zoomDataFromLastFrame = undefined
}
setInterval(doZoom, 1000 / FPS_VIRTUAL)
const doMove = () => {
if (this.moveDataFromLastFrame !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.engineCommandManager.sendSceneCommand({
type: 'modeling_cmd_req',
cmd: {
type: 'camera_drag_move',
interaction: this.moveDataFromLastFrame[2] as any,
window: {
x: this.moveDataFromLastFrame[0],
y: this.moveDataFromLastFrame[1],
},
},
cmd_id: uuidv4(),
})
}
this.moveDataFromLastFrame = undefined
}
setInterval(doMove, 1000 / FPS_VIRTUAL)
setTimeout(() => {
this.engineCommandManager.subscribeTo({
event: 'camera_drag_end',
@ -386,7 +393,9 @@ export class CameraControls {
if (interaction === 'none') return
if (this.syncDirection === 'engineToClient') {
this.moveDataFromLastFrame = [event.clientX, event.clientY, interaction]
this.moveSender.send(() => {
this.doMove(interaction, [event.clientX, event.clientY])
})
return
}
@ -459,7 +468,9 @@ export class CameraControls {
if (this.syncDirection === 'engineToClient') {
if (interaction === 'zoom') {
this.zoomDataFromLastFrame = event.deltaY
this.zoomSender.send(() => {
this.doZoom(event.deltaY)
})
} else {
// This case will get handled when we add pan and rotate using Apple trackpad.
console.error(

View File

@ -488,6 +488,12 @@ export const FileTreeInner = ({
// Refresh the file tree when there are changes.
useFileSystemWatcher(
async (eventType, path) => {
// Our other watcher races with this watcher on the current file changes,
// so we need to stop this one from reacting at all, otherwise Bad Things
// Happen™.
const isCurrentFile = loaderData.file?.path === path
const hasChanged = eventType === 'change'
if (isCurrentFile && hasChanged) return
fileSend({ type: 'Refresh' })
},
[loaderData?.project?.path, fileContext.selectedDirectory.path].filter(

View File

@ -69,7 +69,7 @@ import { exportFromEngine } from 'lib/exportFromEngine'
import { Models } from '@kittycad/lib/dist/types/src'
import toast from 'react-hot-toast'
import { EditorSelection, Transaction } from '@codemirror/state'
import { useNavigate, useSearchParams } from 'react-router-dom'
import { useLoaderData, useNavigate, useSearchParams } from 'react-router-dom'
import { letEngineAnimateAndSyncCamAfter } from 'clientSideScene/CameraControls'
import { getVarNameModal } from 'hooks/useToolbarGuards'
import { err, reportRejection, trap } from 'lib/trap'
@ -84,6 +84,7 @@ import {
import { submitAndAwaitTextToKcl } from 'lib/textToCad'
import { useFileContext } from 'hooks/useFileContext'
import { uuidv4 } from 'lib/utils'
import { IndexLoaderData } from 'lib/types'
type MachineContext<T extends AnyStateMachine> = {
state: StateFrom<T>
@ -116,6 +117,7 @@ export const ModelingMachineProvider = ({
} = useSettingsAuthContext()
const navigate = useNavigate()
const { context, send: fileMachineSend } = useFileContext()
const { file } = useLoaderData() as IndexLoaderData
const token = auth?.context?.token
const streamRef = useRef<HTMLDivElement>(null)
const persistedContext = useMemo(() => getPersistedContext(), [])
@ -409,12 +411,15 @@ export const ModelingMachineProvider = ({
Make: ({ event }) => {
if (event.type !== 'Make') return
// Check if we already have an export intent.
if (engineCommandManager.exportIntent) {
if (engineCommandManager.exportInfo) {
toast.error('Already exporting')
return
}
// Set the export intent.
engineCommandManager.exportIntent = ExportIntent.Make
engineCommandManager.exportInfo = {
intent: ExportIntent.Make,
name: file?.name || '',
}
// Set the current machine.
machineManager.currentMachine = event.data.machine
@ -443,12 +448,16 @@ export const ModelingMachineProvider = ({
},
'Engine export': ({ event }) => {
if (event.type !== 'Export') return
if (engineCommandManager.exportIntent) {
if (engineCommandManager.exportInfo) {
toast.error('Already exporting')
return
}
// Set the export intent.
engineCommandManager.exportIntent = ExportIntent.Save
engineCommandManager.exportInfo = {
intent: ExportIntent.Save,
// This never gets used its only for make.
name: '',
}
const format = {
...event.data,

View File

@ -18,6 +18,7 @@ export default class CodeManager {
#updateState: (arg: string) => void = () => {}
private _currentFilePath: string | null = null
private _hotkeys: { [key: string]: () => void } = {}
private timeoutWriter: ReturnType<typeof setTimeout> | undefined = undefined
constructor() {
if (isDesktop()) {
@ -115,7 +116,11 @@ export default class CodeManager {
async writeToFile() {
if (isDesktop()) {
setTimeout(() => {
// Only write our buffer contents to file once per second. Any faster
// and file-system watchers which read, will receive empty data during
// writes.
clearTimeout(this.timeoutWriter)
this.timeoutWriter = setTimeout(() => {
// Wait one event loop to give a chance for params to be set
// Save the file to disk
this._currentFilePath &&
@ -126,7 +131,7 @@ export default class CodeManager {
console.error('error saving file', err)
toast.error('Error saving file, please check file permissions')
})
})
}, 1000)
} else {
safeLSSetItem(PERSIST_CODE_KEY, this.code)
}

View File

@ -50,6 +50,11 @@ export enum ExportIntent {
Make = 'make',
}
export interface ExportInfo {
intent: ExportIntent
name: string
}
type ClientMetrics = Models['ClientMetrics_type']
interface WebRTCClientMetrics extends ClientMetrics {
@ -1354,7 +1359,7 @@ export class EngineCommandManager extends EventTarget {
* export in progress. Otherwise it is an enum value of the intent.
* Another export cannot be started if one is already in progress.
*/
private _exportIntent: ExportIntent | null = null
private _exportInfo: ExportInfo | null = null
_commandLogCallBack: (command: CommandLog[]) => void = () => {}
subscriptions: {
@ -1410,12 +1415,12 @@ export class EngineCommandManager extends EventTarget {
(() => {}) as any
kclManager: null | KclManager = null
set exportIntent(intent: ExportIntent | null) {
this._exportIntent = intent
set exportInfo(info: ExportInfo | null) {
this._exportInfo = info
}
get exportIntent() {
return this._exportIntent
get exportInfo() {
return this._exportInfo
}
start({
@ -1607,7 +1612,7 @@ export class EngineCommandManager extends EventTarget {
// because in all other cases we send JSON strings. But in the case of
// export we send a binary blob.
// Pass this to our export function.
if (this.exportIntent === null || this.pendingExport === undefined) {
if (this.exportInfo === null || this.pendingExport === undefined) {
toast.error(
'Export intent was not set, but export data was received'
)
@ -1617,7 +1622,7 @@ export class EngineCommandManager extends EventTarget {
return
}
switch (this.exportIntent) {
switch (this.exportInfo.intent) {
case ExportIntent.Save: {
exportSave(event.data, this.pendingExport.toastId).then(() => {
this.pendingExport?.resolve(null)
@ -1625,21 +1630,22 @@ export class EngineCommandManager extends EventTarget {
break
}
case ExportIntent.Make: {
exportMake(event.data, this.pendingExport.toastId).then(
(result) => {
if (result) {
this.pendingExport?.resolve(null)
} else {
this.pendingExport?.reject('Failed to make export')
}
},
this.pendingExport?.reject
)
exportMake(
event.data,
this.exportInfo.name,
this.pendingExport.toastId
).then((result) => {
if (result) {
this.pendingExport?.resolve(null)
} else {
this.pendingExport?.reject('Failed to make export')
}
}, this.pendingExport?.reject)
break
}
}
// Set the export intent back to null.
this.exportIntent = null
this.exportInfo = null
return
}
@ -1953,15 +1959,15 @@ export class EngineCommandManager extends EventTarget {
return Promise.resolve(null)
} else if (cmd.type === 'export') {
const promise = new Promise<null>((resolve, reject) => {
if (this.exportIntent === null) {
if (this.exportIntent === null) {
if (this.exportInfo === null) {
if (this.exportInfo === null) {
toast.error('Export intent was not set, but export is being sent')
console.error('Export intent was not set, but export is being sent')
return
}
}
const toastId = toast.loading(
this.exportIntent === ExportIntent.Save
this.exportInfo.intent === ExportIntent.Save
? EXPORT_TOAST_MESSAGES.START
: MAKE_TOAST_MESSAGES.START
)
@ -1975,7 +1981,7 @@ export class EngineCommandManager extends EventTarget {
resolve(passThrough)
},
reject: (reason: string) => {
this.exportIntent = null
this.exportInfo = null
reject(reason)
},
commandId: command.cmd_id,

View File

@ -110,6 +110,7 @@ const initialise = async () => {
const fullUrl = wasmUrl()
const input = await fetch(fullUrl)
const buffer = await input.arrayBuffer()
return await init(buffer)
} catch (e) {
console.log('Error initialising WASM', e)

View File

@ -92,6 +92,7 @@ export const MAKE_TOAST_MESSAGES = {
NO_MACHINE_API_IP: 'No machine api ip available',
NO_CURRENT_MACHINE: 'No current machine available',
NO_MACHINE_ID: 'No machine id available',
NO_NAME: 'No name provided',
ERROR_STARTING_PRINT: 'Error while starting print',
SUCCESS: 'Started print successfully',
}

31
src/lib/engineUtils.ts Normal file
View File

@ -0,0 +1,31 @@
import EngineUtils from '@engine-utils'
type KCEngineUtilsEvaluatePath = {
(sketch: string, t: number): string
}
let kcEngineUtilsEvaluatePath: KCEngineUtilsEvaluatePath
export async function init() {
return await new Promise((resolve, reject) => {
try {
EngineUtils().then((module) => {
kcEngineUtilsEvaluatePath = module.cwrap(
'kcEngineUtilsEvaluatePath',
'string',
['string', 'number']
)
resolve(true)
})
} catch (e) {
reject(e)
}
})
}
export async function getTruePathEndPos(sketch: string) {
if (!kcEngineUtilsEvaluatePath) {
await init()
}
return kcEngineUtilsEvaluatePath(sketch, 1.0)
}

View File

@ -8,8 +8,15 @@ import { MAKE_TOAST_MESSAGES } from './constants'
// Make files locally from an export call.
export async function exportMake(
data: ArrayBuffer,
name: string,
toastId: string
): Promise<Response | null> {
if (name === '') {
console.error(MAKE_TOAST_MESSAGES.NO_NAME)
toast.error(MAKE_TOAST_MESSAGES.NO_NAME, { id: toastId })
return null
}
if (machineManager.machineCount() === 0) {
console.error(MAKE_TOAST_MESSAGES.NO_MACHINES)
toast.error(MAKE_TOAST_MESSAGES.NO_MACHINES, { id: toastId })
@ -39,7 +46,7 @@ export async function exportMake(
const params: components['schemas']['PrintParameters'] = {
machine_id: machineId,
job_name: 'Exported Job', // TODO: make this the project name.
job_name: name,
}
try {
console.log('params', params)

View File

@ -121,9 +121,9 @@ dependencies = [
[[package]]
name = "anyhow"
version = "1.0.89"
version = "1.0.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6"
checksum = "c042108f3ed77fd83760a5fd79b53be043192bb3b9dba91d8c574c0ada7850c8"
dependencies = [
"backtrace",
]
@ -1684,9 +1684,9 @@ dependencies = [
[[package]]
name = "kittycad-modeling-cmds"
version = "0.2.68"
version = "0.2.70"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e3aedfcc1d8ea9995ec3eb78a6743c585c9380475c48701797f107489b696aa"
checksum = "b135696d07a4fab928e5abace4dd05f4976eafab5d73e5747a85dc5a684b936c"
dependencies = [
"anyhow",
"chrono",
@ -3005,9 +3005,9 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.128"
version = "1.0.132"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8"
checksum = "d726bfaff4b320266d395898905d0eba0345aae23b54aee3a737e260fd46db03"
dependencies = [
"indexmap 2.6.0",
"itoa",

View File

@ -72,7 +72,7 @@ members = [
[workspace.dependencies]
http = "1"
kittycad = { version = "0.3.23", default-features = false, features = ["js", "requests"] }
kittycad-modeling-cmds = { version = "0.2.68", features = ["websocket"] }
kittycad-modeling-cmds = { version = "0.2.70", features = ["websocket"] }
[[test]]
name = "executor"

View File

@ -12,7 +12,7 @@ fn basic() {
let expected = Program {
start: 0,
end: 11,
body: vec![BodyItem::VariableDeclaration(VariableDeclaration {
body: vec![BodyItem::VariableDeclaration(Box::new(VariableDeclaration {
start: 0,
end: 11,
declarations: vec![VariableDeclarator {
@ -36,7 +36,7 @@ fn basic() {
visibility: ItemVisibility::Default,
kind: VariableKind::Const,
digest: None,
})],
}))],
non_code_meta: NonCodeMeta::default(),
digest: None,
};

View File

@ -68,7 +68,7 @@ tokio-tungstenite = { version = "0.24.0", features = ["rustls-tls-native-roots"]
tower-lsp = { version = "0.20.0", features = ["proposed"] }
[features]
default = ["engine"]
default = ["engine"] # add wasm-engine-utils here when we're ready
cli = ["dep:clap"]
# For the lsp server, when run with stdout for rpc we want to disable println.
# This is used for editor extensions that use the lsp server.
@ -77,6 +77,10 @@ engine = []
pyo3 = ["dep:pyo3"]
# Helper functions also used in benchmarks.
lsp-test-util = []
#if enabled, kcl will link directly against a wasm build of the engine utils lib to save latency
wasm-engine-utils = []
#if enabled, kcl will link directly against a native build of the engine utils lib to save latency (not yet functional)
native-engine-utils = []
tabled = ["dep:tabled"]

View File

@ -454,7 +454,7 @@ pub(crate) use impl_value_meta;
pub enum BodyItem {
ImportStatement(Box<ImportStatement>),
ExpressionStatement(ExpressionStatement),
VariableDeclaration(VariableDeclaration),
VariableDeclaration(Box<VariableDeclaration>),
ReturnStatement(ReturnStatement),
}
@ -2719,7 +2719,7 @@ pub struct FunctionExpression {
impl_value_meta!(FunctionExpression);
#[derive(Debug, PartialEq, Clone)]
pub struct RequiredParamAfterOptionalParam(pub Parameter);
pub struct RequiredParamAfterOptionalParam(pub Box<Parameter>);
impl std::fmt::Display for RequiredParamAfterOptionalParam {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@ -2751,7 +2751,7 @@ impl FunctionExpression {
if param.optional {
found_optional = true;
} else if found_optional {
return Err(RequiredParamAfterOptionalParam(param.clone()));
return Err(RequiredParamAfterOptionalParam(Box::new(param.clone())));
}
}
let boundary = self.params.partition_point(|param| !param.optional);

View File

@ -551,13 +551,13 @@ impl ArrayRangeExpression {
.execute_expr(&self.start_element, exec_state, &metadata, StatementKind::Expression)
.await?
.get_json_value()?;
let start = parse_json_number_as_u64(&start, (&*self.start_element).into())?;
let start = parse_json_number_as_i64(&start, (&*self.start_element).into())?;
let metadata = Metadata::from(&*self.end_element);
let end = ctx
.execute_expr(&self.end_element, exec_state, &metadata, StatementKind::Expression)
.await?
.get_json_value()?;
let end = parse_json_number_as_u64(&end, (&*self.end_element).into())?;
let end = parse_json_number_as_i64(&end, (&*self.end_element).into())?;
if end < start {
return Err(KclError::Semantic(KclErrorDetails {
@ -603,9 +603,9 @@ impl ObjectExpression {
}
}
pub fn parse_json_number_as_u64(j: &serde_json::Value, source_range: SourceRange) -> Result<u64, KclError> {
fn parse_json_number_as_i64(j: &serde_json::Value, source_range: SourceRange) -> Result<i64, KclError> {
if let serde_json::Value::Number(n) = &j {
n.as_u64().ok_or_else(|| {
n.as_i64().ok_or_else(|| {
KclError::Syntax(KclErrorDetails {
source_ranges: vec![source_range],
message: format!("Invalid integer: {}", j),

View File

@ -0,0 +1,57 @@
//! Functions for calling into the engine-utils library (a set of C++ utilities containing various logic for client-side CAD processing)
//! Note that this binary may not be available to all builds of kcl, so fallbacks that call the engine API should be implemented
use crate::{
errors::{KclError, KclErrorDetails},
std::Args,
};
use anyhow::Result;
use std::ffi::{CString, CStr};
use kittycad_modeling_cmds::{length_unit::LengthUnit, shared::Point3d};
mod cpp {
use std::os::raw::c_char;
extern "C" {
pub fn kcEngineUtilsEvaluatePath(sketch: *const c_char, t: f64) -> *const c_char;
}
}
pub fn is_available() -> bool {
true
}
pub async fn get_true_path_end_pos(sketch: String, args: &Args) -> Result<Point3d<LengthUnit>, KclError> {
let c_string = CString::new(sketch).map_err(|e| {
KclError::Internal(KclErrorDetails {
message: format!("{:?}", e),
source_ranges: vec![args.source_range],
})
})?;
let arg = c_string.into_raw();
let result_string: String;
unsafe {
let result = cpp::kcEngineUtilsEvaluatePath(arg, 1.0);
let result_cstr = CStr::from_ptr(result);
let str_slice: &str = result_cstr.to_str().map_err(|e| {
KclError::Internal(KclErrorDetails {
message: format!("{:?}", e),
source_ranges: vec![args.source_range],
})
})?;
let str_buf: String = str_slice.to_owned();
result_string = str_buf.clone();
let _ = CString::from_raw(arg);
}
let point: Point3d<f64> = serde_json::from_str(&result_string).map_err(|e| {
KclError::Type(KclErrorDetails {
message: format!("Failed to path position from json: {}", e),
source_ranges: vec![args.source_range],
})
})?;
Ok(Point3d::<f64>::from(point).map(LengthUnit))
}

View File

@ -0,0 +1,35 @@
//! Functions for calling into the engine-utils library (a set of C++ utilities containing various logic for client-side CAD processing)
//! Note that this binary may not be available to all builds of kcl, so fallbacks that call the engine API should be implemented
use crate::{
errors::{KclError, KclErrorDetails},
std::Args,
};
use crate::engine::kcmc::{each_cmd as mcmd, ModelingCmd};
use anyhow::Result;
use kittycad_modeling_cmds::{length_unit::LengthUnit, ok_response::OkModelingCmdResponse, shared::Point3d, websocket::OkWebSocketResponseData};
pub fn is_available() -> bool {
true
}
pub async fn get_true_path_end_pos(sketch: String, args: &Args) -> Result<Point3d<LengthUnit>, KclError> {
let id = uuid::Uuid::new_v4();
let resp = args.send_modeling_cmd(id, ModelingCmd::from(mcmd::EngineUtilEvaluatePath {
path_json: sketch,
t: 1.0,
})).await?;
let OkWebSocketResponseData::Modeling {
modeling_response: OkModelingCmdResponse::EngineUtilEvaluatePath(point),
} = &resp
else {
return Err(KclError::Engine(KclErrorDetails {
message: format!("mcmd::EngineUtilEvaluatePath response was not as expected: {:?}", resp),
source_ranges: vec![args.source_range],
}));
};
Ok(point.pos)
}

View File

@ -0,0 +1,56 @@
//! Functions for calling into the engine-utils library (a set of C++ utilities containing various logic for client-side CAD processing)
//! Note that this binary may not be available to all builds of kcl, so fallbacks that call the engine API should be implemented
use crate::{
errors::{KclError, KclErrorDetails},
std::Args,
};
use anyhow::Result;
use kittycad_modeling_cmds::{length_unit::LengthUnit, shared::Point3d};
mod cpp {
use wasm_bindgen::prelude::wasm_bindgen;
#[wasm_bindgen(module = "/../../lib/engineUtils.ts")]
extern "C" {
#[wasm_bindgen(js_name = getTruePathEndPos, catch)]
pub fn get_true_path_end_pos(sketch: String) -> Result<js_sys::Promise, js_sys::Error>;
}
}
pub fn is_available() -> bool {
true
}
async fn call_cpp<F>(args: &Args, f: F) -> Result<String, KclError>
where
F: FnOnce() -> Result<js_sys::Promise, js_sys::Error>,
{
let promise = f().map_err(|e| {
KclError::Internal(KclErrorDetails {
message: format!("{:?}", e),
source_ranges: vec![args.source_range],
})
})?;
let result = crate::wasm::JsFuture::from(promise).await.map_err(|e| {
KclError::Internal(KclErrorDetails {
message: format!("{:?}", e),
source_ranges: vec![args.source_range],
})
})?;
Ok(result.as_string().unwrap_or_default())
}
pub async fn get_true_path_end_pos(sketch: String, args: &Args) -> Result<Point3d<LengthUnit>, KclError> {
let result_str = call_cpp(args, || cpp::get_true_path_end_pos(sketch.into())).await?;
let point: Point3d<f64> = serde_json::from_str(&result_str).map_err(|e| {
KclError::Type(KclErrorDetails {
message: format!("Failed to path position from json: {}", e),
source_ranges: vec![args.source_range],
})
})?;
Ok(Point3d::<f64>::from(point).map(LengthUnit))
}

View File

@ -8,6 +8,17 @@ pub mod conn_mock;
#[cfg(feature = "engine")]
pub mod conn_wasm;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "native-engine-utils")]
pub mod engine_utils;
#[cfg(target_arch = "wasm32")]
#[cfg(feature = "wasm-engine-utils")]
pub mod engine_utils_wasm;
#[cfg(feature = "engine")]
#[cfg(any(not(target_arch = "wasm32"), all(not(feature = "native-engine-utils"), not(feature = "wasm-engine-utils"))))]
pub mod engine_utils_api;
use std::{
collections::HashMap,
sync::{Arc, Mutex},

View File

@ -1601,6 +1601,19 @@ pub enum Path {
#[serde(flatten)]
base: BasePath,
},
/// An arc (only used for engine-utils arg serialization for now)
Arc {
#[serde(flatten)]
base: BasePath,
/// angle range
#[ts(type = "[number, number]")]
angle_range: [f64; 2],
/// center
#[ts(type = "[number, number]")]
center: [f64; 2],
/// the arc's radius
radius: f64,
},
/// A arc that is tangential to the last path segment that goes to a point
TangentialArcTo {
#[serde(flatten)]
@ -1620,6 +1633,10 @@ pub enum Path {
center: [f64; 2],
/// arc's direction
ccw: bool,
/// the arc's radius
radius: f64,
/// the arc's angle offset
offset: f64,
},
// TODO: consolidate segment enums, remove Circle. https://github.com/KittyCAD/modeling-app/issues/3940
/// a complete arc
@ -1668,6 +1685,7 @@ impl Path {
Path::TangentialArcTo { base, .. } => base.geo_meta.id,
Path::TangentialArc { base, .. } => base.geo_meta.id,
Path::Circle { base, .. } => base.geo_meta.id,
Path::Arc { base, .. } => base.geo_meta.id,
}
}
@ -1680,6 +1698,7 @@ impl Path {
Path::TangentialArcTo { base, .. } => base.tag.clone(),
Path::TangentialArc { base, .. } => base.tag.clone(),
Path::Circle { base, .. } => base.tag.clone(),
Path::Arc { base, .. } => base.tag.clone(),
}
}
@ -1692,6 +1711,7 @@ impl Path {
Path::TangentialArcTo { base, .. } => base,
Path::TangentialArc { base, .. } => base,
Path::Circle { base, .. } => base,
Path::Arc { base, .. } => base,
}
}
@ -1704,6 +1724,7 @@ impl Path {
Path::TangentialArcTo { base, .. } => Some(base),
Path::TangentialArc { base, .. } => Some(base),
Path::Circle { base, .. } => Some(base),
Path::Arc { base, .. } => Some(base),
}
}
}

View File

@ -1342,7 +1342,7 @@ fn declaration_keyword(i: TokenSlice) -> PResult<(VariableKind, Token)> {
}
/// Parse a variable/constant declaration.
fn declaration(i: TokenSlice) -> PResult<VariableDeclaration> {
fn declaration(i: TokenSlice) -> PResult<Box<VariableDeclaration>> {
let (visibility, visibility_token) = opt(terminated(item_visibility, whitespace))
.parse_next(i)?
.map_or((ItemVisibility::Default, None), |pair| (pair.0, Some(pair.1)));
@ -1404,7 +1404,7 @@ fn declaration(i: TokenSlice) -> PResult<VariableDeclaration> {
.map_err(|e| e.cut())?;
let end = val.end();
Ok(VariableDeclaration {
Ok(Box::new(VariableDeclaration {
start,
end,
declarations: vec![VariableDeclarator {
@ -1417,7 +1417,7 @@ fn declaration(i: TokenSlice) -> PResult<VariableDeclaration> {
visibility,
kind,
digest: None,
})
}))
}
impl TryFrom<Token> for Identifier {

View File

@ -256,6 +256,7 @@ pub(crate) async fn do_post_extrude(
});
Some(extrude_surface)
}
Path::Arc { .. } => todo!(),
}
} else if args.ctx.is_mock() {
// Only pre-populate the extrude surface if we are in mock mode.

View File

@ -6,7 +6,7 @@ use anyhow::Result;
use derive_docs::stdlib;
use kcmc::shared::Point2d as KPoint2d; // Point2d is already defined in this pkg, to impl ts_rs traits.
use kcmc::{each_cmd as mcmd, length_unit::LengthUnit, shared::Angle, ModelingCmd};
use kittycad_modeling_cmds as kcmc;
use kittycad_modeling_cmds::{self as kcmc, units};
use kittycad_modeling_cmds::shared::PathSegment;
use parse_display::{Display, FromStr};
use schemars::JsonSchema;
@ -21,13 +21,26 @@ use crate::{
},
std::{
utils::{
arc_angles, arc_center_and_end, get_tangent_point_from_previous_arc, get_tangential_arc_to_info,
get_x_component, get_y_component, intersection_with_parallel_line, TangentialArcInfoInput,
arc_angles, arc_center_and_end, arc_start_center_and_end, get_tangent_point_from_previous_arc,
get_tangential_arc_to_info, get_x_component, get_y_component, intersection_with_parallel_line,
TangentialArcInfoInput,
},
Args,
},
};
#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "native-engine-utils")]
use crate::engine::engine_utils;
#[cfg(target_arch = "wasm32")]
#[cfg(feature = "wasm-engine-utils")]
use crate::engine::engine_utils_wasm as engine_utils;
#[cfg(feature = "engine")]
#[cfg(any(not(target_arch = "wasm32"), all(not(feature = "native-engine-utils"), not(feature = "wasm-engine-utils"))))]
use crate::engine::engine_utils_api as engine_utils;
/// A tag for a face.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
@ -1495,7 +1508,45 @@ pub(crate) async fn inner_arc(
} => {
let a_start = Angle::from_degrees(*angle_start);
let a_end = Angle::from_degrees(*angle_end);
let (center, end) = arc_center_and_end(from, a_start, a_end, *radius);
let (_, center, mut end) = arc_start_center_and_end(from, a_start, a_end, *radius);
if engine_utils::is_available() {
let mut path_plus_arc = sketch.clone();
let to = [0.0, 0.0];
let arc = Path::Arc {
base: BasePath {
from: from.into(),
to,
tag: None,
geo_meta: GeoMeta {
id: uuid::Uuid::new_v4(),
metadata: args.source_range.into(),
},
},
angle_range: [*angle_start, *angle_end],
center: [center.x, center.y],
radius: *radius,
};
path_plus_arc.value.push(arc);
let sketch_json_value = serde_json::to_string(&path_plus_arc).map_err(|e| {
KclError::Type(KclErrorDetails {
message: format!("Failed to convert sketch to json: {}", e),
source_ranges: vec![args.source_range],
})
})?;
//???? someone double check me on this unit conversion - mike
let units = units::UnitLength::Millimeters;
let result_end = engine_utils::get_true_path_end_pos(sketch_json_value, &args).await?;
end = Point2d {
x: result_end.x.to_millimeters(units),
y: result_end.y.to_millimeters(units),
};
}
(center, a_start, a_end, *radius, end)
}
ArcData::CenterToRadius { center, to, radius } => {
@ -1616,7 +1667,7 @@ async fn inner_tangential_arc(
let id = exec_state.id_generator.next_uuid();
let (center, to, ccw) = match data {
let (center, to, ccw, radius, offset) = match data {
TangentialArcData::RadiusAndOffset { radius, offset } => {
// KCL stdlib types use degrees.
let offset = Angle::from_degrees(offset);
@ -1654,13 +1705,15 @@ async fn inner_tangential_arc(
}),
)
.await?;
(center, to.into(), ccw)
(center, to.into(), ccw, radius, offset)
}
};
let current_path = Path::TangentialArc {
ccw,
center: center.into(),
radius,
offset: offset.to_degrees(),
base: BasePath {
from: from.into(),
to,

View File

@ -221,6 +221,33 @@ pub fn arc_center_and_end(from: Point2d, start_angle: Angle, end_angle: Angle, r
(center, end)
}
pub fn arc_start_center_and_end(
from: Point2d,
start_angle: Angle,
end_angle: Angle,
radius: f64,
) -> (Point2d, Point2d, Point2d) {
let start_angle = start_angle.to_radians();
let end_angle = end_angle.to_radians();
let center = Point2d {
x: -1.0 * (radius * start_angle.cos() - from.x),
y: -1.0 * (radius * start_angle.sin() - from.y),
};
let start = Point2d {
x: center.x + radius * start_angle.cos(),
y: center.y + radius * start_angle.sin(),
};
let end = Point2d {
x: center.x + radius * end_angle.cos(),
y: center.y + radius * end_angle.sin(),
};
(start, center, end)
}
pub fn arc_angles(
from: Point2d,
to: Point2d,

View File

@ -290,7 +290,7 @@ where
walk_value(&xs.expression, f)
}
BodyItem::VariableDeclaration(vd) => {
if !f.walk(vd.into())? {
if !f.walk(vd.as_ref().into())? {
return Ok(false);
}
for dec in &vd.declarations {

View File

@ -1,3 +1,3 @@
[toolchain]
channel = "1.81.0"
channel = "1.82.0"
components = ["clippy", "rustfmt"]

View File

@ -1,3 +1,2 @@
xs = [-5..5]
xs = [int(-5)..5]
assertEqual(xs[0], -5, 0.001, "first element is -5")
assert(false)

View File

@ -101,7 +101,7 @@ gen_test!(property_of_object);
gen_test!(index_of_array);
gen_test!(comparisons);
gen_test!(array_range_expr);
gen_test_fail!(array_range_negative_expr, "syntax: Invalid integer: -5.0");
gen_test!(array_range_negative_expr);
gen_test_fail!(
invalid_index_str,
"semantic: Only integers >= 0 can be used as the index of an array, but you're using a string"

View File

@ -58,6 +58,7 @@ const config = defineConfig({
resolve: {
alias: {
'@kittycad/codemirror-lsp-client': '/packages/codemirror-lsp-client/src',
'@engine-utils': '/src/lib/engine-utils/engine.js',
},
},
plugins: [react(), viteTsconfigPaths(), eslint(), version(), lezer()],