Skip to content

Commit 8c36105

Browse files
committed
Generate angle suffixes in the frontend
Signed-off-by: Nick Cameron <nrc@ncameron.org>
1 parent 77e5d6e commit 8c36105

29 files changed

+403
-397
lines changed

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/kcl-lib/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,9 @@ impl Program {
231231
pub fn change_default_units(
232232
&self,
233233
length_units: Option<execution::types::UnitLen>,
234-
angle_units: Option<execution::types::UnitAngle>,
235234
) -> Result<Self, KclError> {
236235
Ok(Self {
237-
ast: self.ast.change_default_units(length_units, angle_units)?,
236+
ast: self.ast.change_default_units(length_units)?,
238237
original_file_contents: self.original_file_contents.clone(),
239238
})
240239
}

rust/kcl-lib/src/parsing/ast/types/mod.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::{
2828
errors::KclError,
2929
execution::{
3030
annotations,
31-
types::{ArrayLen, UnitAngle, UnitLen},
31+
types::{ArrayLen, UnitLen},
3232
KclValue, Metadata, TagIdentifier,
3333
},
3434
parsing::{ast::digest::Digest, token::NumericSuffix, PIPE_OPERATOR},
@@ -360,7 +360,6 @@ impl Node<Program> {
360360
pub fn change_default_units(
361361
&self,
362362
length_units: Option<UnitLen>,
363-
angle_units: Option<UnitAngle>,
364363
) -> Result<Self, KclError> {
365364
let mut new_program = self.clone();
366365
let mut found = false;
@@ -372,13 +371,6 @@ impl Node<Program> {
372371
Expr::Name(Box::new(Name::new(&len.to_string()))),
373372
);
374373
}
375-
if let Some(angle) = angle_units {
376-
node.inner.add_or_update(
377-
annotations::SETTINGS_UNIT_ANGLE,
378-
Expr::Name(Box::new(Name::new(&angle.to_string()))),
379-
);
380-
}
381-
382374
// Previous source range no longer makes sense, but we want to
383375
// preserve other things like comments.
384376
node.reset_source();
@@ -395,12 +387,6 @@ impl Node<Program> {
395387
Expr::Name(Box::new(Name::new(&len.to_string()))),
396388
);
397389
}
398-
if let Some(angle) = angle_units {
399-
settings.inner.add_or_update(
400-
annotations::SETTINGS_UNIT_ANGLE,
401-
Expr::Name(Box::new(Name::new(&angle.to_string()))),
402-
);
403-
}
404390

405391
new_program.inner_attrs.push(settings);
406392
}
@@ -4256,7 +4242,7 @@ startSketchOn(XY)"#;
42564242

42574243
// Edit the ast.
42584244
let new_program = program
4259-
.change_default_units(Some(crate::execution::types::UnitLen::Mm), None)
4245+
.change_default_units(Some(crate::execution::types::UnitLen::Mm))
42604246
.unwrap();
42614247

42624248
let result = new_program.meta_settings().unwrap();
@@ -4285,7 +4271,7 @@ startSketchOn(XY)
42854271

42864272
// Edit the ast.
42874273
let new_program = program
4288-
.change_default_units(Some(crate::execution::types::UnitLen::Mm), None)
4274+
.change_default_units(Some(crate::execution::types::UnitLen::Mm))
42894275
.unwrap();
42904276

42914277
let result = new_program.meta_settings().unwrap();
@@ -4320,7 +4306,7 @@ startSketchOn(XY)
43204306
let program = crate::parsing::top_level_parse(code).unwrap();
43214307

43224308
let new_program = program
4323-
.change_default_units(Some(crate::execution::types::UnitLen::Cm), None)
4309+
.change_default_units(Some(crate::execution::types::UnitLen::Cm))
43244310
.unwrap();
43254311

43264312
let result = new_program.meta_settings().unwrap();

rust/kcl-wasm-lib/src/wasm.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,13 @@ pub fn kcl_settings(program_json: &str) -> Result<JsValue, String> {
289289

290290
/// Takes a kcl string and Meta settings and changes the meta settings in the kcl string.
291291
#[wasm_bindgen]
292-
pub fn change_default_units(code: &str, len_str: &str, angle_str: &str) -> Result<String, String> {
292+
pub fn change_default_units(code: &str, len_str: &str) -> Result<String, String> {
293293
console_error_panic_hook::set_once();
294294

295295
let len: Option<kcl_lib::UnitLen> = serde_json::from_str(len_str).map_err(|e| e.to_string())?;
296-
let angle: Option<kcl_lib::UnitAngle> = serde_json::from_str(angle_str).map_err(|e| e.to_string())?;
297296
let program = Program::parse_no_errs(code).map_err(|e| e.to_string())?;
298297

299-
let new_program = program.change_default_units(len, angle).map_err(|e| e.to_string())?;
298+
let new_program = program.change_default_units(len).map_err(|e| e.to_string())?;
300299

301300
let formatted = new_program.recast();
302301

src/components/SetAngleLengthModal.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type ModalResolve = {
2222
type ModalReject = boolean
2323

2424
type SetAngleLengthModalProps = InstanceProps<ModalResolve, ModalReject> & {
25-
value: number
25+
value: string
2626
valueName: string
2727
shouldCreateVariable?: boolean
2828
}
@@ -41,8 +41,10 @@ export const SetAngleLengthModal = ({
4141
valueName,
4242
shouldCreateVariable: initialShouldCreateVariable = false,
4343
}: SetAngleLengthModalProps) => {
44-
const [sign, setSign] = useState(Math.sign(Number(initialValue)))
45-
const [value, setValue] = useState(String(initialValue * sign))
44+
const [sign, setSign] = useState(initialValue.startsWith('-') ? -1 : 1)
45+
const [value, setValue] = useState(
46+
initialValue.startsWith('-') ? initialValue.substring(1) : initialValue
47+
)
4648
const [shouldCreateVariable, setShouldCreateVariable] = useState(
4749
initialShouldCreateVariable
4850
)

src/components/SetHorVertDistanceModal.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type ModalReject = boolean
2525
type GetInfoModalProps = InstanceProps<ModalResolve, ModalReject> & {
2626
segName: string
2727
isSegNameEditable: boolean
28-
value?: number
28+
value?: string
2929
initialVariableName: string
3030
}
3131

@@ -44,10 +44,12 @@ export const GetInfoModal = ({
4444
value: initialValue,
4545
initialVariableName,
4646
}: GetInfoModalProps) => {
47-
const [sign, setSign] = useState(Math.sign(Number(initialValue)))
47+
const [sign, setSign] = useState(initialValue?.startsWith('-') ? -1 : 1)
4848
const [segName, setSegName] = useState(initialSegName)
4949
const [value, setValue] = useState(
50-
initialValue === undefined ? '' : String(Math.abs(initialValue))
50+
initialValue?.startsWith('-')
51+
? initialValue.substring(1)
52+
: initialValue || ''
5153
)
5254
const [shouldCreateVariable, setShouldCreateVariable] = useState(false)
5355

src/components/Toolbar/Intersect.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export async function applyConstraintIntersect({
171171
if (
172172
!variableName &&
173173
segName === tagInfo?.tag &&
174-
Number(value) === valueUsedInTransform
174+
value === valueUsedInTransform
175175
) {
176176
return {
177177
modifiedAst,

src/components/Toolbar/SetAbsDistance.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export async function applyConstraintAbsDistance({
108108
if (err(transform1)) return Promise.reject(transform1)
109109
const { valueUsedInTransform } = transform1
110110

111-
let forceVal = valueUsedInTransform || 0
111+
let forceVal = valueUsedInTransform || ''
112112
const { valueNode, variableName, newVariableInsertIndex, sign } =
113113
await getModalInfo({
114114
value: forceVal,

src/components/Toolbar/SetAngleBetween.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export async function applyConstraintAngleBetween({
120120
} as any)
121121
if (
122122
segName === tagInfo?.tag &&
123-
Number(value) === valueUsedInTransform &&
123+
value === valueUsedInTransform &&
124124
!variableName
125125
) {
126126
return {

src/components/Toolbar/SetHorzVertDistance.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export async function applyConstraintHorzVertDistance({
127127
if (
128128
!variableName &&
129129
segName === tagInfo?.tag &&
130-
Number(value) === valueUsedInTransform
130+
value === valueUsedInTransform
131131
) {
132132
return {
133133
modifiedAst,

0 commit comments

Comments
 (0)