2023-08-24 15:34:51 -07:00
|
|
|
//! Functions for managing engine communications.
|
|
|
|
|
2023-08-28 14:58:24 -07:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2023-08-31 22:19:23 -07:00
|
|
|
#[cfg(feature = "engine")]
|
2023-08-28 14:58:24 -07:00
|
|
|
pub mod conn;
|
2024-03-13 12:56:46 -07:00
|
|
|
pub mod conn_mock;
|
2023-08-28 14:58:24 -07:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
2023-08-31 22:19:23 -07:00
|
|
|
#[cfg(feature = "engine")]
|
2023-08-28 14:58:24 -07:00
|
|
|
pub mod conn_wasm;
|
2023-08-31 22:19:23 -07:00
|
|
|
|
2025-03-18 16:19:24 +13:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
sync::{
|
|
|
|
atomic::{AtomicUsize, Ordering},
|
|
|
|
Arc,
|
|
|
|
},
|
|
|
|
};
|
2024-03-23 15:45:55 -07:00
|
|
|
|
2024-09-19 14:06:29 -07:00
|
|
|
use indexmap::IndexMap;
|
|
|
|
use kcmc::{
|
|
|
|
each_cmd as mcmd,
|
2025-02-18 13:50:13 -08:00
|
|
|
id::ModelingCmdId,
|
2024-09-19 14:06:29 -07:00
|
|
|
length_unit::LengthUnit,
|
|
|
|
ok_response::OkModelingCmdResponse,
|
|
|
|
shared::Color,
|
|
|
|
websocket::{
|
|
|
|
BatchResponse, ModelingBatch, ModelingCmdReq, ModelingSessionData, OkWebSocketResponseData, WebSocketRequest,
|
|
|
|
WebSocketResponse,
|
|
|
|
},
|
|
|
|
ModelingCmd,
|
2024-08-23 17:40:30 -05:00
|
|
|
};
|
2024-09-18 17:04:04 -05:00
|
|
|
use kittycad_modeling_cmds as kcmc;
|
2024-04-15 17:18:32 -07:00
|
|
|
use schemars::JsonSchema;
|
|
|
|
use serde::{Deserialize, Serialize};
|
2025-02-18 13:50:13 -08:00
|
|
|
use tokio::sync::RwLock;
|
2024-09-18 17:04:04 -05:00
|
|
|
use uuid::Uuid;
|
2024-03-23 15:45:55 -07:00
|
|
|
|
2024-04-15 17:18:32 -07:00
|
|
|
use crate::{
|
|
|
|
errors::{KclError, KclErrorDetails},
|
2025-01-08 20:02:30 -05:00
|
|
|
execution::{ArtifactCommand, DefaultPlanes, IdGenerator, Point3d},
|
2024-12-03 16:39:51 +13:00
|
|
|
SourceRange,
|
2024-04-15 17:18:32 -07:00
|
|
|
};
|
2024-03-23 15:45:55 -07:00
|
|
|
|
2024-06-30 19:21:24 -07:00
|
|
|
lazy_static::lazy_static! {
|
|
|
|
pub static ref GRID_OBJECT_ID: uuid::Uuid = uuid::Uuid::parse_str("cfa78409-653d-4c26-96f1-7c45fb784840").unwrap();
|
|
|
|
|
|
|
|
pub static ref GRID_SCALE_TEXT_OBJECT_ID: uuid::Uuid = uuid::Uuid::parse_str("10782f33-f588-4668-8bcd-040502d26590").unwrap();
|
|
|
|
}
|
|
|
|
|
2024-10-17 00:48:33 -04:00
|
|
|
/// The mode of execution. When isolated, like during an import, attempting to
|
|
|
|
/// send a command results in an error.
|
|
|
|
#[derive(Debug, Default, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, ts_rs::TS, JsonSchema)]
|
|
|
|
#[ts(export)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub enum ExecutionKind {
|
|
|
|
#[default]
|
|
|
|
Normal,
|
|
|
|
Isolated,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ExecutionKind {
|
|
|
|
pub fn is_isolated(&self) -> bool {
|
|
|
|
matches!(self, ExecutionKind::Isolated)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-18 16:19:24 +13:00
|
|
|
#[derive(Default, Debug)]
|
|
|
|
pub struct EngineStats {
|
|
|
|
pub commands_batched: AtomicUsize,
|
|
|
|
pub batches_sent: AtomicUsize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Clone for EngineStats {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
commands_batched: AtomicUsize::new(self.commands_batched.load(Ordering::Relaxed)),
|
|
|
|
batches_sent: AtomicUsize::new(self.batches_sent.load(Ordering::Relaxed)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-12 13:37:47 -07:00
|
|
|
#[async_trait::async_trait]
|
2024-03-13 12:56:46 -07:00
|
|
|
pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static {
|
2024-03-23 15:45:55 -07:00
|
|
|
/// Get the batch of commands to be sent to the engine.
|
2025-02-18 13:50:13 -08:00
|
|
|
fn batch(&self) -> Arc<RwLock<Vec<(WebSocketRequest, SourceRange)>>>;
|
2024-03-23 15:45:55 -07:00
|
|
|
|
2024-06-22 14:31:37 -07:00
|
|
|
/// Get the batch of end commands to be sent to the engine.
|
2025-02-18 13:50:13 -08:00
|
|
|
fn batch_end(&self) -> Arc<RwLock<IndexMap<uuid::Uuid, (WebSocketRequest, SourceRange)>>>;
|
2024-06-22 14:31:37 -07:00
|
|
|
|
2025-01-17 14:34:36 -05:00
|
|
|
/// Get the command responses from the engine.
|
2025-02-18 13:50:13 -08:00
|
|
|
fn responses(&self) -> Arc<RwLock<IndexMap<Uuid, WebSocketResponse>>>;
|
2025-01-17 14:34:36 -05:00
|
|
|
|
2025-02-18 13:50:13 -08:00
|
|
|
/// Get the artifact commands that have accumulated so far.
|
|
|
|
fn artifact_commands(&self) -> Arc<RwLock<Vec<ArtifactCommand>>>;
|
2025-01-08 20:02:30 -05:00
|
|
|
|
|
|
|
/// Clear all artifact commands that have accumulated so far.
|
2025-02-18 13:50:13 -08:00
|
|
|
async fn clear_artifact_commands(&self) {
|
|
|
|
self.artifact_commands().write().await.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Take the artifact commands that have accumulated so far and clear them.
|
|
|
|
async fn take_artifact_commands(&self) -> Vec<ArtifactCommand> {
|
|
|
|
std::mem::take(&mut *self.artifact_commands().write().await)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Take the responses that have accumulated so far and clear them.
|
|
|
|
async fn take_responses(&self) -> IndexMap<Uuid, WebSocketResponse> {
|
|
|
|
std::mem::take(&mut *self.responses().write().await)
|
2025-01-08 20:02:30 -05:00
|
|
|
}
|
|
|
|
|
2024-10-17 00:48:33 -04:00
|
|
|
/// Get the current execution kind.
|
2025-02-18 13:50:13 -08:00
|
|
|
async fn execution_kind(&self) -> ExecutionKind;
|
2024-10-17 00:48:33 -04:00
|
|
|
|
|
|
|
/// Replace the current execution kind with a new value and return the
|
|
|
|
/// existing value.
|
2025-02-18 13:50:13 -08:00
|
|
|
async fn replace_execution_kind(&self, execution_kind: ExecutionKind) -> ExecutionKind;
|
2024-10-17 00:48:33 -04:00
|
|
|
|
2024-04-15 17:18:32 -07:00
|
|
|
/// Get the default planes.
|
2025-03-15 10:08:39 -07:00
|
|
|
fn get_default_planes(&self) -> Arc<RwLock<Option<DefaultPlanes>>>;
|
|
|
|
|
2025-03-18 16:19:24 +13:00
|
|
|
fn stats(&self) -> &EngineStats;
|
|
|
|
|
2025-03-15 10:08:39 -07:00
|
|
|
/// Get the default planes, creating them if they don't exist.
|
2024-04-15 17:18:32 -07:00
|
|
|
async fn default_planes(
|
|
|
|
&self,
|
2024-10-09 19:38:40 -04:00
|
|
|
id_generator: &mut IdGenerator,
|
2025-03-15 10:08:39 -07:00
|
|
|
source_range: SourceRange,
|
|
|
|
) -> Result<DefaultPlanes, KclError> {
|
|
|
|
{
|
|
|
|
let opt = self.get_default_planes().read().await.as_ref().cloned();
|
|
|
|
if let Some(planes) = opt {
|
|
|
|
return Ok(planes);
|
|
|
|
}
|
|
|
|
} // drop the read lock
|
|
|
|
|
|
|
|
let new_planes = self.new_default_planes(id_generator, source_range).await?;
|
|
|
|
*self.get_default_planes().write().await = Some(new_planes.clone());
|
|
|
|
|
|
|
|
Ok(new_planes)
|
|
|
|
}
|
2024-04-15 17:18:32 -07:00
|
|
|
|
|
|
|
/// Helpers to be called after clearing a scene.
|
2024-07-29 20:40:07 -05:00
|
|
|
/// (These really only apply to wasm for now).
|
2024-04-15 17:18:32 -07:00
|
|
|
async fn clear_scene_post_hook(
|
|
|
|
&self,
|
2024-10-09 19:38:40 -04:00
|
|
|
id_generator: &mut IdGenerator,
|
2024-12-03 16:39:51 +13:00
|
|
|
source_range: SourceRange,
|
2024-04-15 17:18:32 -07:00
|
|
|
) -> Result<(), crate::errors::KclError>;
|
|
|
|
|
2025-03-20 15:59:16 +13:00
|
|
|
async fn clear_queues(&self) {
|
|
|
|
self.batch().write().await.clear();
|
|
|
|
self.batch_end().write().await.clear();
|
|
|
|
}
|
|
|
|
|
2023-09-17 21:57:43 -07:00
|
|
|
/// Send a modeling command and wait for the response message.
|
2024-03-23 15:45:55 -07:00
|
|
|
async fn inner_send_modeling_cmd(
|
|
|
|
&self,
|
|
|
|
id: uuid::Uuid,
|
2024-12-03 16:39:51 +13:00
|
|
|
source_range: SourceRange,
|
2024-09-18 17:04:04 -05:00
|
|
|
cmd: WebSocketRequest,
|
2024-12-03 16:39:51 +13:00
|
|
|
id_to_source_range: HashMap<uuid::Uuid, SourceRange>,
|
2024-09-18 17:04:04 -05:00
|
|
|
) -> Result<kcmc::websocket::WebSocketResponse, crate::errors::KclError>;
|
2024-03-23 15:45:55 -07:00
|
|
|
|
2024-10-09 19:38:40 -04:00
|
|
|
async fn clear_scene(
|
|
|
|
&self,
|
|
|
|
id_generator: &mut IdGenerator,
|
2024-12-03 16:39:51 +13:00
|
|
|
source_range: SourceRange,
|
2024-10-09 19:38:40 -04:00
|
|
|
) -> Result<(), crate::errors::KclError> {
|
2025-03-20 15:59:16 +13:00
|
|
|
// Clear any batched commands leftover from previous scenes.
|
|
|
|
self.clear_queues().await;
|
|
|
|
|
2024-06-19 13:57:50 -07:00
|
|
|
self.batch_modeling_cmd(
|
2025-03-29 21:23:11 -07:00
|
|
|
id_generator.next_uuid(),
|
2024-04-15 17:18:32 -07:00
|
|
|
source_range,
|
2025-01-08 20:02:30 -05:00
|
|
|
&ModelingCmd::SceneClearAll(mcmd::SceneClearAll::default()),
|
2024-04-15 17:18:32 -07:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
2025-03-31 10:56:03 -04:00
|
|
|
// Reset to the default units. Modules assume the engine starts in the
|
|
|
|
// default state.
|
|
|
|
self.set_units(Default::default(), source_range, id_generator).await?;
|
|
|
|
|
2024-04-15 17:18:32 -07:00
|
|
|
// Flush the batch queue, so clear is run right away.
|
|
|
|
// Otherwise the hooks below won't work.
|
2024-06-22 14:31:37 -07:00
|
|
|
self.flush_batch(false, source_range).await?;
|
2024-04-15 17:18:32 -07:00
|
|
|
|
2025-01-08 20:02:30 -05:00
|
|
|
// Ensure artifact commands are cleared so that we don't accumulate them
|
|
|
|
// across runs.
|
2025-02-18 13:50:13 -08:00
|
|
|
self.clear_artifact_commands().await;
|
2025-01-08 20:02:30 -05:00
|
|
|
|
2024-04-15 17:18:32 -07:00
|
|
|
// Do the after clear scene hook.
|
2024-10-09 19:38:40 -04:00
|
|
|
self.clear_scene_post_hook(id_generator, source_range).await?;
|
2024-04-15 17:18:32 -07:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-12-10 18:50:22 -08:00
|
|
|
/// Set the visibility of edges.
|
|
|
|
async fn set_edge_visibility(
|
|
|
|
&self,
|
|
|
|
visible: bool,
|
|
|
|
source_range: SourceRange,
|
2025-03-29 21:23:11 -07:00
|
|
|
id_generator: &mut IdGenerator,
|
2024-12-10 18:50:22 -08:00
|
|
|
) -> Result<(), crate::errors::KclError> {
|
|
|
|
self.batch_modeling_cmd(
|
2025-03-29 21:23:11 -07:00
|
|
|
id_generator.next_uuid(),
|
2024-12-10 18:50:22 -08:00
|
|
|
source_range,
|
|
|
|
&ModelingCmd::from(mcmd::EdgeLinesVisible { hidden: !visible }),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2025-02-18 13:50:13 -08:00
|
|
|
async fn handle_artifact_command(
|
|
|
|
&self,
|
|
|
|
cmd: &ModelingCmd,
|
|
|
|
cmd_id: ModelingCmdId,
|
|
|
|
id_to_source_range: &HashMap<Uuid, SourceRange>,
|
|
|
|
) -> Result<(), KclError> {
|
|
|
|
let cmd_id = *cmd_id.as_ref();
|
|
|
|
let range = id_to_source_range
|
|
|
|
.get(&cmd_id)
|
|
|
|
.copied()
|
|
|
|
.ok_or_else(|| KclError::internal(format!("Failed to get source range for command ID: {:?}", cmd_id)))?;
|
|
|
|
|
|
|
|
// Add artifact command.
|
|
|
|
self.artifact_commands().write().await.push(ArtifactCommand {
|
|
|
|
cmd_id,
|
|
|
|
range,
|
|
|
|
command: cmd.clone(),
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-12-10 18:50:22 -08:00
|
|
|
async fn set_units(
|
|
|
|
&self,
|
|
|
|
units: crate::UnitLength,
|
|
|
|
source_range: SourceRange,
|
2025-03-29 21:23:11 -07:00
|
|
|
id_generator: &mut IdGenerator,
|
2024-12-10 18:50:22 -08:00
|
|
|
) -> Result<(), crate::errors::KclError> {
|
|
|
|
// Before we even start executing the program, set the units.
|
|
|
|
self.batch_modeling_cmd(
|
2025-03-29 21:23:11 -07:00
|
|
|
id_generator.next_uuid(),
|
2024-12-10 18:50:22 -08:00
|
|
|
source_range,
|
|
|
|
&ModelingCmd::from(mcmd::SetSceneUnits { unit: units.into() }),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Re-run the command to apply the settings.
|
|
|
|
async fn reapply_settings(
|
|
|
|
&self,
|
|
|
|
settings: &crate::ExecutorSettings,
|
|
|
|
source_range: SourceRange,
|
2025-03-29 21:23:11 -07:00
|
|
|
id_generator: &mut IdGenerator,
|
2024-12-10 18:50:22 -08:00
|
|
|
) -> Result<(), crate::errors::KclError> {
|
|
|
|
// Set the edge visibility.
|
2025-03-29 21:23:11 -07:00
|
|
|
self.set_edge_visibility(settings.highlight_edges, source_range, id_generator)
|
|
|
|
.await?;
|
2024-12-10 18:50:22 -08:00
|
|
|
|
|
|
|
// Send the command to show the grid.
|
2025-03-29 21:23:11 -07:00
|
|
|
self.modify_grid(!settings.show_grid, source_range, id_generator)
|
|
|
|
.await?;
|
2024-12-10 18:50:22 -08:00
|
|
|
|
|
|
|
// We do not have commands for changing ssao on the fly.
|
|
|
|
|
|
|
|
// Flush the batch queue, so the settings are applied right away.
|
|
|
|
self.flush_batch(false, source_range).await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-06-19 13:57:50 -07:00
|
|
|
// Add a modeling command to the batch but don't fire it right away.
|
|
|
|
async fn batch_modeling_cmd(
|
2023-09-20 16:59:03 -05:00
|
|
|
&self,
|
2023-09-17 21:57:43 -07:00
|
|
|
id: uuid::Uuid,
|
2024-12-03 16:39:51 +13:00
|
|
|
source_range: SourceRange,
|
2024-09-18 17:04:04 -05:00
|
|
|
cmd: &ModelingCmd,
|
2024-06-19 13:57:50 -07:00
|
|
|
) -> Result<(), crate::errors::KclError> {
|
2025-02-19 00:11:11 -05:00
|
|
|
// In isolated mode, we don't send the command to the engine.
|
|
|
|
if self.execution_kind().await.is_isolated() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2024-09-18 17:04:04 -05:00
|
|
|
let req = WebSocketRequest::ModelingCmdReq(ModelingCmdReq {
|
2024-03-23 15:45:55 -07:00
|
|
|
cmd: cmd.clone(),
|
2024-09-18 17:04:04 -05:00
|
|
|
cmd_id: id.into(),
|
|
|
|
});
|
2024-03-23 15:45:55 -07:00
|
|
|
|
2024-04-09 00:18:35 -05:00
|
|
|
// Add cmd to the batch.
|
2025-02-18 13:50:13 -08:00
|
|
|
self.batch().write().await.push((req, source_range));
|
2025-03-18 16:19:24 +13:00
|
|
|
self.stats().commands_batched.fetch_add(1, Ordering::Relaxed);
|
2024-03-23 15:45:55 -07:00
|
|
|
|
2024-06-19 13:57:50 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
2024-03-23 15:45:55 -07:00
|
|
|
|
2025-03-13 21:59:39 -07:00
|
|
|
// Add a vector of modeling commands to the batch but don't fire it right away.
|
|
|
|
// This allows you to force them all to be added together in the same order.
|
|
|
|
// When we are running things in parallel this prevents race conditions that might come
|
|
|
|
// if specific commands are run before others.
|
|
|
|
async fn batch_modeling_cmds(
|
|
|
|
&self,
|
|
|
|
source_range: SourceRange,
|
|
|
|
cmds: &[ModelingCmdReq],
|
|
|
|
) -> Result<(), crate::errors::KclError> {
|
|
|
|
// In isolated mode, we don't send the command to the engine.
|
|
|
|
if self.execution_kind().await.is_isolated() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add cmds to the batch.
|
|
|
|
let mut extended_cmds = Vec::with_capacity(cmds.len());
|
|
|
|
for cmd in cmds {
|
|
|
|
extended_cmds.push((WebSocketRequest::ModelingCmdReq(cmd.clone()), source_range));
|
|
|
|
}
|
2025-03-18 16:19:24 +13:00
|
|
|
self.stats()
|
|
|
|
.commands_batched
|
|
|
|
.fetch_add(extended_cmds.len(), Ordering::Relaxed);
|
2025-03-13 21:59:39 -07:00
|
|
|
self.batch().write().await.extend(extended_cmds);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-06-22 14:31:37 -07:00
|
|
|
/// Add a command to the batch that needs to be executed at the very end.
|
|
|
|
/// This for stuff like fillets or chamfers where if we execute too soon the
|
|
|
|
/// engine will eat the ID and we can't reference it for other commands.
|
|
|
|
async fn batch_end_cmd(
|
|
|
|
&self,
|
|
|
|
id: uuid::Uuid,
|
2024-12-03 16:39:51 +13:00
|
|
|
source_range: SourceRange,
|
2024-09-18 17:04:04 -05:00
|
|
|
cmd: &ModelingCmd,
|
2024-06-22 14:31:37 -07:00
|
|
|
) -> Result<(), crate::errors::KclError> {
|
2025-02-19 00:11:11 -05:00
|
|
|
// In isolated mode, we don't send the command to the engine.
|
|
|
|
if self.execution_kind().await.is_isolated() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2024-09-18 17:04:04 -05:00
|
|
|
let req = WebSocketRequest::ModelingCmdReq(ModelingCmdReq {
|
2024-06-22 14:31:37 -07:00
|
|
|
cmd: cmd.clone(),
|
2024-09-18 17:04:04 -05:00
|
|
|
cmd_id: id.into(),
|
|
|
|
});
|
2024-06-22 14:31:37 -07:00
|
|
|
|
|
|
|
// Add cmd to the batch end.
|
2025-02-18 13:50:13 -08:00
|
|
|
self.batch_end().write().await.insert(id, (req, source_range));
|
2025-03-18 16:19:24 +13:00
|
|
|
self.stats().commands_batched.fetch_add(1, Ordering::Relaxed);
|
2024-06-22 14:31:37 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-06-19 13:57:50 -07:00
|
|
|
/// Send the modeling cmd and wait for the response.
|
|
|
|
async fn send_modeling_cmd(
|
|
|
|
&self,
|
|
|
|
id: uuid::Uuid,
|
2024-12-03 16:39:51 +13:00
|
|
|
source_range: SourceRange,
|
2025-01-08 20:02:30 -05:00
|
|
|
cmd: &ModelingCmd,
|
2024-09-18 17:04:04 -05:00
|
|
|
) -> Result<OkWebSocketResponseData, crate::errors::KclError> {
|
2025-01-08 20:02:30 -05:00
|
|
|
self.batch_modeling_cmd(id, source_range, cmd).await?;
|
2024-03-23 15:45:55 -07:00
|
|
|
|
|
|
|
// Flush the batch queue.
|
2024-06-22 14:31:37 -07:00
|
|
|
self.flush_batch(false, source_range).await
|
2024-03-23 15:45:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Force flush the batch queue.
|
|
|
|
async fn flush_batch(
|
|
|
|
&self,
|
2024-06-22 14:31:37 -07:00
|
|
|
// Whether or not to flush the end commands as well.
|
|
|
|
// We only do this at the very end of the file.
|
|
|
|
batch_end: bool,
|
2024-12-03 16:39:51 +13:00
|
|
|
source_range: SourceRange,
|
2024-09-18 17:04:04 -05:00
|
|
|
) -> Result<OkWebSocketResponseData, crate::errors::KclError> {
|
2024-06-22 14:31:37 -07:00
|
|
|
let all_requests = if batch_end {
|
2025-02-18 13:50:13 -08:00
|
|
|
let mut requests = self.batch().read().await.clone();
|
|
|
|
requests.extend(self.batch_end().read().await.values().cloned());
|
2024-06-22 14:31:37 -07:00
|
|
|
requests
|
|
|
|
} else {
|
2025-02-18 13:50:13 -08:00
|
|
|
self.batch().read().await.clone()
|
2024-06-22 14:31:37 -07:00
|
|
|
};
|
|
|
|
|
2024-03-23 15:45:55 -07:00
|
|
|
// Return early if we have no commands to send.
|
2024-06-22 14:31:37 -07:00
|
|
|
if all_requests.is_empty() {
|
2024-03-23 15:45:55 -07:00
|
|
|
return Ok(OkWebSocketResponseData::Modeling {
|
2024-09-18 17:04:04 -05:00
|
|
|
modeling_response: OkModelingCmdResponse::Empty {},
|
2024-03-23 15:45:55 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-22 14:31:37 -07:00
|
|
|
let requests: Vec<ModelingCmdReq> = all_requests
|
2024-04-09 00:18:35 -05:00
|
|
|
.iter()
|
|
|
|
.filter_map(|(val, _)| match val {
|
2024-09-18 17:04:04 -05:00
|
|
|
WebSocketRequest::ModelingCmdReq(ModelingCmdReq { cmd, cmd_id }) => Some(ModelingCmdReq {
|
2024-03-23 15:45:55 -07:00
|
|
|
cmd: cmd.clone(),
|
|
|
|
cmd_id: *cmd_id,
|
2024-04-09 00:18:35 -05:00
|
|
|
}),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect();
|
2024-06-22 14:31:37 -07:00
|
|
|
|
2024-09-18 17:04:04 -05:00
|
|
|
let batched_requests = WebSocketRequest::ModelingCmdBatchReq(ModelingBatch {
|
2024-04-09 00:18:35 -05:00
|
|
|
requests,
|
2024-09-18 17:04:04 -05:00
|
|
|
batch_id: uuid::Uuid::new_v4().into(),
|
2024-06-19 13:57:50 -07:00
|
|
|
responses: true,
|
2024-09-18 17:04:04 -05:00
|
|
|
});
|
2024-03-23 15:45:55 -07:00
|
|
|
|
2024-06-22 14:31:37 -07:00
|
|
|
let final_req = if all_requests.len() == 1 {
|
2024-03-23 15:45:55 -07:00
|
|
|
// We can unwrap here because we know the batch has only one element.
|
2024-06-22 14:31:37 -07:00
|
|
|
all_requests.first().unwrap().0.clone()
|
2024-03-23 15:45:55 -07:00
|
|
|
} else {
|
|
|
|
batched_requests
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create the map of original command IDs to source range.
|
|
|
|
// This is for the wasm side, kurt needs it for selections.
|
2024-09-18 17:04:04 -05:00
|
|
|
let mut id_to_source_range = HashMap::new();
|
2024-06-22 14:31:37 -07:00
|
|
|
for (req, range) in all_requests.iter() {
|
2024-03-23 15:45:55 -07:00
|
|
|
match req {
|
2024-09-18 17:04:04 -05:00
|
|
|
WebSocketRequest::ModelingCmdReq(ModelingCmdReq { cmd: _, cmd_id }) => {
|
|
|
|
id_to_source_range.insert(Uuid::from(*cmd_id), *range);
|
2024-03-23 15:45:55 -07:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return Err(KclError::Engine(KclErrorDetails {
|
|
|
|
message: format!("The request is not a modeling command: {:?}", req),
|
|
|
|
source_ranges: vec![*range],
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-18 13:50:13 -08:00
|
|
|
// Do the artifact commands.
|
|
|
|
for (req, _) in all_requests.iter() {
|
|
|
|
match &req {
|
|
|
|
WebSocketRequest::ModelingCmdBatchReq(ModelingBatch { requests, .. }) => {
|
|
|
|
for request in requests {
|
|
|
|
self.handle_artifact_command(&request.cmd, request.cmd_id, &id_to_source_range)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WebSocketRequest::ModelingCmdReq(request) => {
|
|
|
|
self.handle_artifact_command(&request.cmd, request.cmd_id, &id_to_source_range)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-23 15:45:55 -07:00
|
|
|
// Throw away the old batch queue.
|
2025-02-18 13:50:13 -08:00
|
|
|
self.batch().write().await.clear();
|
2024-06-22 14:31:37 -07:00
|
|
|
if batch_end {
|
2025-02-18 13:50:13 -08:00
|
|
|
self.batch_end().write().await.clear();
|
2024-06-22 14:31:37 -07:00
|
|
|
}
|
2025-03-18 16:19:24 +13:00
|
|
|
self.stats().batches_sent.fetch_add(1, Ordering::Relaxed);
|
2024-03-23 15:45:55 -07:00
|
|
|
|
|
|
|
// We pop off the responses to cleanup our mappings.
|
2024-06-19 13:57:50 -07:00
|
|
|
match final_req {
|
2024-09-18 17:04:04 -05:00
|
|
|
WebSocketRequest::ModelingCmdBatchReq(ModelingBatch {
|
2024-06-19 13:57:50 -07:00
|
|
|
ref requests,
|
2024-04-11 13:15:49 -07:00
|
|
|
batch_id,
|
|
|
|
responses: _,
|
2024-09-18 17:04:04 -05:00
|
|
|
}) => {
|
2024-06-19 13:57:50 -07:00
|
|
|
// Get the last command ID.
|
|
|
|
let last_id = requests.last().unwrap().cmd_id;
|
|
|
|
let ws_resp = self
|
2024-09-18 17:04:04 -05:00
|
|
|
.inner_send_modeling_cmd(batch_id.into(), source_range, final_req, id_to_source_range.clone())
|
2024-06-19 13:57:50 -07:00
|
|
|
.await?;
|
|
|
|
let response = self.parse_websocket_response(ws_resp, source_range)?;
|
|
|
|
|
|
|
|
// If we have a batch response, we want to return the specific id we care about.
|
2024-09-18 17:04:04 -05:00
|
|
|
if let OkWebSocketResponseData::ModelingBatch { responses } = response {
|
|
|
|
let responses = responses.into_iter().map(|(k, v)| (Uuid::from(k), v)).collect();
|
|
|
|
self.parse_batch_responses(last_id.into(), id_to_source_range, responses)
|
2024-06-19 13:57:50 -07:00
|
|
|
} else {
|
|
|
|
// We should never get here.
|
|
|
|
Err(KclError::Engine(KclErrorDetails {
|
|
|
|
message: format!("Failed to get batch response: {:?}", response),
|
|
|
|
source_ranges: vec![source_range],
|
|
|
|
}))
|
|
|
|
}
|
2024-03-23 15:45:55 -07:00
|
|
|
}
|
2024-09-18 17:04:04 -05:00
|
|
|
WebSocketRequest::ModelingCmdReq(ModelingCmdReq { cmd: _, cmd_id }) => {
|
2024-06-22 16:22:32 -07:00
|
|
|
// You are probably wondering why we can't just return the source range we were
|
|
|
|
// passed with the function. Well this is actually really important.
|
|
|
|
// If this is the last command in the batch and there is only one and we've reached
|
|
|
|
// the end of the file, this will trigger a flush batch function, but it will just
|
|
|
|
// send default or the end of the file as it's source range not the origin of the
|
|
|
|
// request so we need the original request source range in case the engine returns
|
|
|
|
// an error.
|
2024-09-18 17:04:04 -05:00
|
|
|
let source_range = id_to_source_range.get(cmd_id.as_ref()).cloned().ok_or_else(|| {
|
2024-06-22 16:22:32 -07:00
|
|
|
KclError::Engine(KclErrorDetails {
|
|
|
|
message: format!("Failed to get source range for command ID: {:?}", cmd_id),
|
|
|
|
source_ranges: vec![],
|
|
|
|
})
|
|
|
|
})?;
|
2024-06-19 13:57:50 -07:00
|
|
|
let ws_resp = self
|
2024-09-18 17:04:04 -05:00
|
|
|
.inner_send_modeling_cmd(cmd_id.into(), source_range, final_req, id_to_source_range)
|
2024-06-19 13:57:50 -07:00
|
|
|
.await?;
|
|
|
|
self.parse_websocket_response(ws_resp, source_range)
|
|
|
|
}
|
|
|
|
_ => Err(KclError::Engine(KclErrorDetails {
|
|
|
|
message: format!("The final request is not a modeling command: {:?}", final_req),
|
|
|
|
source_ranges: vec![source_range],
|
|
|
|
})),
|
|
|
|
}
|
2024-03-23 15:45:55 -07:00
|
|
|
}
|
2024-04-15 17:18:32 -07:00
|
|
|
|
|
|
|
async fn make_default_plane(
|
|
|
|
&self,
|
2024-10-09 19:38:40 -04:00
|
|
|
plane_id: uuid::Uuid,
|
2024-04-15 17:18:32 -07:00
|
|
|
x_axis: Point3d,
|
|
|
|
y_axis: Point3d,
|
|
|
|
color: Option<Color>,
|
2024-12-03 16:39:51 +13:00
|
|
|
source_range: SourceRange,
|
2025-03-29 21:23:11 -07:00
|
|
|
id_generator: &mut IdGenerator,
|
2024-04-15 17:18:32 -07:00
|
|
|
) -> Result<uuid::Uuid, KclError> {
|
|
|
|
// Create new default planes.
|
|
|
|
let default_size = 100.0;
|
|
|
|
let default_origin = Point3d { x: 0.0, y: 0.0, z: 0.0 }.into();
|
|
|
|
|
2024-06-19 13:57:50 -07:00
|
|
|
self.batch_modeling_cmd(
|
2024-04-15 17:18:32 -07:00
|
|
|
plane_id,
|
|
|
|
source_range,
|
2024-09-18 17:04:04 -05:00
|
|
|
&ModelingCmd::from(mcmd::MakePlane {
|
2024-04-15 17:18:32 -07:00
|
|
|
clobber: false,
|
|
|
|
origin: default_origin,
|
2024-09-18 17:04:04 -05:00
|
|
|
size: LengthUnit(default_size),
|
2024-04-15 17:18:32 -07:00
|
|
|
x_axis: x_axis.into(),
|
|
|
|
y_axis: y_axis.into(),
|
|
|
|
hide: Some(true),
|
2024-09-18 17:04:04 -05:00
|
|
|
}),
|
2024-04-15 17:18:32 -07:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
if let Some(color) = color {
|
|
|
|
// Set the color.
|
2024-06-19 13:57:50 -07:00
|
|
|
self.batch_modeling_cmd(
|
2025-03-29 21:23:11 -07:00
|
|
|
id_generator.next_uuid(),
|
2024-04-15 17:18:32 -07:00
|
|
|
source_range,
|
2024-09-18 17:04:04 -05:00
|
|
|
&ModelingCmd::from(mcmd::PlaneSetColor { color, plane_id }),
|
2024-04-15 17:18:32 -07:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(plane_id)
|
|
|
|
}
|
|
|
|
|
2024-10-09 19:38:40 -04:00
|
|
|
async fn new_default_planes(
|
|
|
|
&self,
|
|
|
|
id_generator: &mut IdGenerator,
|
2024-12-03 16:39:51 +13:00
|
|
|
source_range: SourceRange,
|
2024-10-09 19:38:40 -04:00
|
|
|
) -> Result<DefaultPlanes, KclError> {
|
2024-12-05 11:04:04 -05:00
|
|
|
let plane_settings: Vec<(PlaneName, Uuid, Point3d, Point3d, Option<Color>)> = vec![
|
2024-04-15 17:18:32 -07:00
|
|
|
(
|
|
|
|
PlaneName::Xy,
|
2024-12-05 11:04:04 -05:00
|
|
|
id_generator.next_uuid(),
|
|
|
|
Point3d { x: 1.0, y: 0.0, z: 0.0 },
|
|
|
|
Point3d { x: 0.0, y: 1.0, z: 0.0 },
|
|
|
|
Some(Color {
|
|
|
|
r: 0.7,
|
|
|
|
g: 0.28,
|
|
|
|
b: 0.28,
|
|
|
|
a: 0.4,
|
|
|
|
}),
|
2024-04-15 17:18:32 -07:00
|
|
|
),
|
|
|
|
(
|
|
|
|
PlaneName::Yz,
|
2024-12-05 11:04:04 -05:00
|
|
|
id_generator.next_uuid(),
|
|
|
|
Point3d { x: 0.0, y: 1.0, z: 0.0 },
|
|
|
|
Point3d { x: 0.0, y: 0.0, z: 1.0 },
|
|
|
|
Some(Color {
|
|
|
|
r: 0.28,
|
|
|
|
g: 0.7,
|
|
|
|
b: 0.28,
|
|
|
|
a: 0.4,
|
|
|
|
}),
|
2024-04-15 17:18:32 -07:00
|
|
|
),
|
|
|
|
(
|
|
|
|
PlaneName::Xz,
|
2024-12-05 11:04:04 -05:00
|
|
|
id_generator.next_uuid(),
|
|
|
|
Point3d { x: 1.0, y: 0.0, z: 0.0 },
|
|
|
|
Point3d { x: 0.0, y: 0.0, z: 1.0 },
|
|
|
|
Some(Color {
|
|
|
|
r: 0.28,
|
|
|
|
g: 0.28,
|
|
|
|
b: 0.7,
|
|
|
|
a: 0.4,
|
|
|
|
}),
|
2024-04-15 17:18:32 -07:00
|
|
|
),
|
|
|
|
(
|
|
|
|
PlaneName::NegXy,
|
2024-12-05 11:04:04 -05:00
|
|
|
id_generator.next_uuid(),
|
|
|
|
Point3d {
|
|
|
|
x: -1.0,
|
|
|
|
y: 0.0,
|
|
|
|
z: 0.0,
|
|
|
|
},
|
|
|
|
Point3d { x: 0.0, y: 1.0, z: 0.0 },
|
|
|
|
None,
|
2024-04-15 17:18:32 -07:00
|
|
|
),
|
|
|
|
(
|
|
|
|
PlaneName::NegYz,
|
2024-12-05 11:04:04 -05:00
|
|
|
id_generator.next_uuid(),
|
|
|
|
Point3d {
|
|
|
|
x: 0.0,
|
|
|
|
y: -1.0,
|
|
|
|
z: 0.0,
|
|
|
|
},
|
|
|
|
Point3d { x: 0.0, y: 0.0, z: 1.0 },
|
|
|
|
None,
|
2024-04-15 17:18:32 -07:00
|
|
|
),
|
|
|
|
(
|
|
|
|
PlaneName::NegXz,
|
2024-12-05 11:04:04 -05:00
|
|
|
id_generator.next_uuid(),
|
|
|
|
Point3d {
|
|
|
|
x: -1.0,
|
|
|
|
y: 0.0,
|
|
|
|
z: 0.0,
|
|
|
|
},
|
|
|
|
Point3d { x: 0.0, y: 0.0, z: 1.0 },
|
|
|
|
None,
|
2024-04-15 17:18:32 -07:00
|
|
|
),
|
2024-12-05 11:04:04 -05:00
|
|
|
];
|
2024-04-15 17:18:32 -07:00
|
|
|
|
|
|
|
let mut planes = HashMap::new();
|
2024-12-05 11:04:04 -05:00
|
|
|
for (name, plane_id, x_axis, y_axis, color) in plane_settings {
|
2024-04-15 17:18:32 -07:00
|
|
|
planes.insert(
|
|
|
|
name,
|
2025-03-29 21:23:11 -07:00
|
|
|
self.make_default_plane(plane_id, x_axis, y_axis, color, source_range, id_generator)
|
2024-10-09 19:38:40 -04:00
|
|
|
.await?,
|
2024-04-15 17:18:32 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush the batch queue, so these planes are created right away.
|
2024-06-22 14:31:37 -07:00
|
|
|
self.flush_batch(false, source_range).await?;
|
2024-04-15 17:18:32 -07:00
|
|
|
|
|
|
|
Ok(DefaultPlanes {
|
|
|
|
xy: planes[&PlaneName::Xy],
|
|
|
|
neg_xy: planes[&PlaneName::NegXy],
|
|
|
|
xz: planes[&PlaneName::Xz],
|
|
|
|
neg_xz: planes[&PlaneName::NegXz],
|
|
|
|
yz: planes[&PlaneName::Yz],
|
|
|
|
neg_yz: planes[&PlaneName::NegYz],
|
|
|
|
})
|
|
|
|
}
|
2024-03-23 15:45:55 -07:00
|
|
|
|
2024-06-19 13:57:50 -07:00
|
|
|
fn parse_websocket_response(
|
|
|
|
&self,
|
2024-09-18 17:04:04 -05:00
|
|
|
response: WebSocketResponse,
|
2024-12-03 16:39:51 +13:00
|
|
|
source_range: SourceRange,
|
2024-09-18 17:04:04 -05:00
|
|
|
) -> Result<OkWebSocketResponseData, crate::errors::KclError> {
|
|
|
|
match response {
|
|
|
|
WebSocketResponse::Success(success) => Ok(success.resp),
|
|
|
|
WebSocketResponse::Failure(fail) => {
|
|
|
|
let _request_id = fail.request_id;
|
|
|
|
Err(KclError::Engine(KclErrorDetails {
|
|
|
|
message: format!("Modeling command failed: {:?}", fail.errors),
|
|
|
|
source_ranges: vec![source_range],
|
|
|
|
}))
|
|
|
|
}
|
2024-06-19 13:57:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_batch_responses(
|
|
|
|
&self,
|
|
|
|
// The last response we are looking for.
|
|
|
|
id: uuid::Uuid,
|
|
|
|
// The mapping of source ranges to command IDs.
|
2024-12-03 16:39:51 +13:00
|
|
|
id_to_source_range: HashMap<uuid::Uuid, SourceRange>,
|
2024-06-19 13:57:50 -07:00
|
|
|
// The response from the engine.
|
2024-09-18 17:04:04 -05:00
|
|
|
responses: HashMap<uuid::Uuid, BatchResponse>,
|
|
|
|
) -> Result<OkWebSocketResponseData, crate::errors::KclError> {
|
2024-06-19 13:57:50 -07:00
|
|
|
// Iterate over the responses and check for errors.
|
2024-12-05 12:09:35 -05:00
|
|
|
#[expect(
|
|
|
|
clippy::iter_over_hash_type,
|
|
|
|
reason = "modeling command uses a HashMap and keys are random, so we don't really have a choice"
|
|
|
|
)]
|
2024-06-19 13:57:50 -07:00
|
|
|
for (cmd_id, resp) in responses.iter() {
|
2024-09-18 17:04:04 -05:00
|
|
|
match resp {
|
|
|
|
BatchResponse::Success { response } => {
|
|
|
|
if cmd_id == &id {
|
|
|
|
// This is the response we care about.
|
|
|
|
return Ok(OkWebSocketResponseData::Modeling {
|
|
|
|
modeling_response: response.clone(),
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Continue the loop if this is not the response we care about.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BatchResponse::Failure { errors } => {
|
|
|
|
// Get the source range for the command.
|
|
|
|
let source_range = id_to_source_range.get(cmd_id).cloned().ok_or_else(|| {
|
|
|
|
KclError::Engine(KclErrorDetails {
|
|
|
|
message: format!("Failed to get source range for command ID: {:?}", cmd_id),
|
|
|
|
source_ranges: vec![],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
return Err(KclError::Engine(KclErrorDetails {
|
|
|
|
message: format!("Modeling command failed: {:?}", errors),
|
|
|
|
source_ranges: vec![source_range],
|
|
|
|
}));
|
2024-06-19 13:57:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an error that we did not get an error or the response we wanted.
|
|
|
|
// This should never happen but who knows.
|
|
|
|
Err(KclError::Engine(KclErrorDetails {
|
|
|
|
message: format!("Failed to find response for command ID: {:?}", id),
|
|
|
|
source_ranges: vec![],
|
|
|
|
}))
|
|
|
|
}
|
2024-06-30 19:21:24 -07:00
|
|
|
|
2025-03-29 21:23:11 -07:00
|
|
|
async fn modify_grid(
|
|
|
|
&self,
|
|
|
|
hidden: bool,
|
|
|
|
source_range: SourceRange,
|
|
|
|
id_generator: &mut IdGenerator,
|
|
|
|
) -> Result<(), KclError> {
|
2024-06-30 19:21:24 -07:00
|
|
|
// Hide/show the grid.
|
|
|
|
self.batch_modeling_cmd(
|
2025-03-29 21:23:11 -07:00
|
|
|
id_generator.next_uuid(),
|
2024-12-10 18:50:22 -08:00
|
|
|
source_range,
|
2024-09-18 17:04:04 -05:00
|
|
|
&ModelingCmd::from(mcmd::ObjectVisible {
|
2024-06-30 19:21:24 -07:00
|
|
|
hidden,
|
|
|
|
object_id: *GRID_OBJECT_ID,
|
2024-09-18 17:04:04 -05:00
|
|
|
}),
|
2024-06-30 19:21:24 -07:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
// Hide/show the grid scale text.
|
|
|
|
self.batch_modeling_cmd(
|
2025-03-29 21:23:11 -07:00
|
|
|
id_generator.next_uuid(),
|
2024-12-10 18:50:22 -08:00
|
|
|
source_range,
|
2024-09-18 17:04:04 -05:00
|
|
|
&ModelingCmd::from(mcmd::ObjectVisible {
|
2024-06-30 19:21:24 -07:00
|
|
|
hidden,
|
|
|
|
object_id: *GRID_SCALE_TEXT_OBJECT_ID,
|
2024-09-18 17:04:04 -05:00
|
|
|
}),
|
2024-06-30 19:21:24 -07:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-08-23 17:40:30 -05:00
|
|
|
|
|
|
|
/// Get session data, if it has been received.
|
|
|
|
/// Returns None if the server never sent it.
|
2025-02-18 13:50:13 -08:00
|
|
|
async fn get_session_data(&self) -> Option<ModelingSessionData> {
|
2024-08-23 17:40:30 -05:00
|
|
|
None
|
|
|
|
}
|
2025-01-10 20:05:27 -05:00
|
|
|
|
|
|
|
/// Close the engine connection and wait for it to finish.
|
|
|
|
async fn close(&self);
|
2023-08-24 15:34:51 -07:00
|
|
|
}
|
2024-04-15 17:18:32 -07:00
|
|
|
|
|
|
|
#[derive(Debug, Hash, Eq, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
|
|
|
#[ts(export)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub enum PlaneName {
|
|
|
|
/// The XY plane.
|
|
|
|
Xy,
|
|
|
|
/// The opposite side of the XY plane.
|
|
|
|
NegXy,
|
|
|
|
/// The XZ plane.
|
|
|
|
Xz,
|
|
|
|
/// The opposite side of the XZ plane.
|
|
|
|
NegXz,
|
|
|
|
/// The YZ plane.
|
|
|
|
Yz,
|
|
|
|
/// The opposite side of the YZ plane.
|
|
|
|
NegYz,
|
|
|
|
}
|
2025-02-05 17:53:49 +13:00
|
|
|
|
|
|
|
/// Create a new zoo api client.
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
pub fn new_zoo_client(token: Option<String>, engine_addr: Option<String>) -> anyhow::Result<kittycad::Client> {
|
|
|
|
let user_agent = concat!(env!("CARGO_PKG_NAME"), ".rs/", env!("CARGO_PKG_VERSION"),);
|
|
|
|
let http_client = reqwest::Client::builder()
|
|
|
|
.user_agent(user_agent)
|
|
|
|
// For file conversions we need this to be long.
|
|
|
|
.timeout(std::time::Duration::from_secs(600))
|
|
|
|
.connect_timeout(std::time::Duration::from_secs(60));
|
|
|
|
let ws_client = reqwest::Client::builder()
|
|
|
|
.user_agent(user_agent)
|
|
|
|
// For file conversions we need this to be long.
|
|
|
|
.timeout(std::time::Duration::from_secs(600))
|
|
|
|
.connect_timeout(std::time::Duration::from_secs(60))
|
|
|
|
.connection_verbose(true)
|
|
|
|
.tcp_keepalive(std::time::Duration::from_secs(600))
|
|
|
|
.http1_only();
|
|
|
|
|
|
|
|
let zoo_token_env = std::env::var("ZOO_API_TOKEN");
|
|
|
|
|
|
|
|
let token = if let Some(token) = token {
|
|
|
|
token
|
|
|
|
} else if let Ok(token) = std::env::var("KITTYCAD_API_TOKEN") {
|
|
|
|
if let Ok(zoo_token) = zoo_token_env {
|
|
|
|
if zoo_token != token {
|
|
|
|
return Err(anyhow::anyhow!(
|
|
|
|
"Both environment variables KITTYCAD_API_TOKEN=`{}` and ZOO_API_TOKEN=`{}` are set. Use only one.",
|
|
|
|
token,
|
|
|
|
zoo_token
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
token
|
|
|
|
} else if let Ok(token) = zoo_token_env {
|
|
|
|
token
|
|
|
|
} else {
|
|
|
|
return Err(anyhow::anyhow!(
|
|
|
|
"No API token found in environment variables. Use KITTYCAD_API_TOKEN or ZOO_API_TOKEN"
|
|
|
|
));
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create the client.
|
|
|
|
let mut client = kittycad::Client::new_from_reqwest(token, http_client, ws_client);
|
|
|
|
// Set an engine address if it's set.
|
|
|
|
let kittycad_host_env = std::env::var("KITTYCAD_HOST");
|
|
|
|
if let Some(addr) = engine_addr {
|
|
|
|
client.set_base_url(addr);
|
|
|
|
} else if let Ok(addr) = std::env::var("ZOO_HOST") {
|
|
|
|
if let Ok(kittycad_host) = kittycad_host_env {
|
|
|
|
if kittycad_host != addr {
|
|
|
|
return Err(anyhow::anyhow!(
|
|
|
|
"Both environment variables KITTYCAD_HOST=`{}` and ZOO_HOST=`{}` are set. Use only one.",
|
|
|
|
kittycad_host,
|
|
|
|
addr
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
client.set_base_url(addr);
|
|
|
|
} else if let Ok(addr) = kittycad_host_env {
|
|
|
|
client.set_base_url(addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(client)
|
|
|
|
}
|