-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcmd_file.rs
870 lines (760 loc) · 32 KB
/
cmd_file.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
use std::{
hash::{DefaultHasher, Hash, Hasher},
str::FromStr,
};
use anyhow::Result;
use base64::prelude::*;
use clap::Parser;
use kcl_lib::EngineManager;
use kittycad_modeling_cmds as kcmc;
use crate::cmd_kcl::write_deterministic_export;
/// Perform operations on CAD files.
///
/// # convert a step file to an obj file
/// $ zoo file convert --output-format=obj ./input.step ./
#[derive(Parser, Debug, Clone)]
#[clap(verbatim_doc_comment)]
pub struct CmdFile {
#[clap(subcommand)]
subcmd: SubCommand,
}
#[derive(Parser, Debug, Clone)]
enum SubCommand {
Convert(CmdFileConvert),
Snapshot(CmdFileSnapshot),
Volume(CmdFileVolume),
Mass(CmdFileMass),
CenterOfMass(CmdFileCenterOfMass),
Density(CmdFileDensity),
SurfaceArea(CmdFileSurfaceArea),
}
#[async_trait::async_trait(?Send)]
impl crate::cmd::Command for CmdFile {
async fn run(&self, ctx: &mut crate::context::Context) -> Result<()> {
match &self.subcmd {
SubCommand::Convert(cmd) => cmd.run(ctx).await,
SubCommand::Snapshot(cmd) => cmd.run(ctx).await,
SubCommand::Volume(cmd) => cmd.run(ctx).await,
SubCommand::Mass(cmd) => cmd.run(ctx).await,
SubCommand::CenterOfMass(cmd) => cmd.run(ctx).await,
SubCommand::Density(cmd) => cmd.run(ctx).await,
SubCommand::SurfaceArea(cmd) => cmd.run(ctx).await,
}
}
}
/// Convert a CAD file from one format to another.
///
/// If the file being converted is larger than a certain size it will be
/// performed asynchronously, you can then check its status with the
/// `zoo api-call status <id_of_your_operation>` command.
///
/// # convert step to obj
/// $ zoo file convert --output-format=obj my-file.step output_dir
///
/// # convert obj to step
/// $ zoo file convert --output-format=step my-obj.obj .
///
/// # pass a file to convert from stdin
/// # when converting from stdin, the original file type is required
/// $ cat my-obj.obj | zoo file convert --output-format=step - output_dir
#[derive(Parser, Debug, Clone)]
#[clap(verbatim_doc_comment)]
pub struct CmdFileConvert {
/// The path to the input file to convert.
/// If you pass `-` as the path, the file will be read from stdin.
#[clap(name = "input", required = true)]
pub input: std::path::PathBuf,
/// The path to a directory to output the files.
#[clap(name = "output-dir", required = true)]
pub output_dir: std::path::PathBuf,
/// A valid source file format.
#[clap(short = 's', long = "src-format", value_enum)]
src_format: Option<kittycad::types::FileImportFormat>,
/// A valid output file format.
#[clap(short = 't', long = "output-format", value_enum)]
output_format: kittycad::types::FileExportFormat,
/// Command output format.
#[clap(long, short, value_enum)]
pub format: Option<crate::types::FormatOutput>,
/// If true, the output file should be deterministic, meaning any date or time information
/// will be replaced with a fixed value.
/// This is useful for when pushing to version control.
#[clap(long, default_value = "false")]
pub deterministic: bool,
}
#[async_trait::async_trait(?Send)]
impl crate::cmd::Command for CmdFileConvert {
async fn run(&self, ctx: &mut crate::context::Context) -> Result<()> {
// Make sure the output dir is a directory.
if !self.output_dir.is_dir() {
anyhow::bail!(
"output directory `{}` does not exist or is not a directory",
self.output_dir.to_str().unwrap_or("")
);
}
// Parse the source format.
let src_format = if let Some(src_format) = &self.src_format {
src_format.clone()
} else {
get_import_format_from_extension(&get_extension(self.input.clone()))?
};
// Get the contents of the input file.
let input = ctx.read_file(self.input.to_str().unwrap_or(""))?;
// Do the conversion.
let client = ctx.api_client("")?;
// Create the file conversion.
let mut file_conversion = client
.file()
.create_conversion(self.output_format.clone(), src_format, &input.into())
.await?;
// If they specified an output file, save the output to that file.
if file_conversion.status == kittycad::types::ApiCallStatus::Completed {
if let Some(outputs) = file_conversion.outputs {
// Write the contents of the files to the output directory.
for (filename, data) in outputs.iter() {
let path = self.output_dir.clone().join(filename);
if self.deterministic {
write_deterministic_export(&path, &data.0)?;
} else {
std::fs::write(&path, data)?;
}
writeln!(
ctx.io.out,
"wrote file `{}` to {}",
filename,
path.to_str().unwrap_or("")
)?;
}
} else {
anyhow::bail!("no output was generated! (this is probably a bug in the API) you should report it to [email protected]");
}
}
// Reset the outputs field of the file conversion.
// Otherwise what we print will be crazy big.
file_conversion.outputs = None;
// Print the output of the conversion.
let format = ctx.format(&self.format)?;
ctx.io.write_output(&format, &file_conversion)?;
Ok(())
}
}
/// Snapshot a render of a CAD file as any supported image format.
///
/// # snapshot as png
/// $ zoo file snapshot my-file.obj my-file.png
///
/// # pass a file to snapshot from stdin
/// $ cat my-obj.obj | zoo file snapshot --output-format=png - my-file.png
#[derive(Parser, Debug, Clone)]
#[clap(verbatim_doc_comment)]
pub struct CmdFileSnapshot {
/// The path to the input file to snapshot.
/// If you pass `-` as the path, the file will be read from stdin.
#[clap(name = "input", required = true)]
pub input: std::path::PathBuf,
/// A valid source file format.
#[clap(short = 's', long = "src-format", value_enum)]
src_format: Option<kittycad::types::FileImportFormat>,
/// The path to a file to output the image.
#[clap(name = "output-file", required = true)]
pub output_file: std::path::PathBuf,
/// A valid output image format.
#[clap(short = 't', long = "output-format", value_enum)]
output_format: Option<kittycad::types::ImageFormat>,
/// Command output format.
#[clap(long, short, value_enum)]
pub format: Option<crate::types::FormatOutput>,
}
#[async_trait::async_trait(?Send)]
impl crate::cmd::Command for CmdFileSnapshot {
async fn run(&self, ctx: &mut crate::context::Context) -> Result<()> {
// Make sure the parent directory is a directory and exists.
if let Some(parent) = self.output_file.parent() {
if !parent.is_dir() && !parent.to_str().unwrap_or("").is_empty() {
anyhow::bail!(
"directory `{}` does not exist or is not a directory",
parent.to_str().unwrap_or("")
);
}
}
// Parse the image format.
let output_format = if let Some(output_format) = &self.output_format {
match output_format {
kittycad::types::ImageFormat::Png => kcmc::ImageFormat::Png,
kittycad::types::ImageFormat::Jpeg => kcmc::ImageFormat::Jpeg,
}
} else {
crate::cmd_kcl::get_image_format_from_extension(&crate::cmd_file::get_extension(self.output_file.clone()))?
};
// Parse the source format.
let src_format = if let Some(src_format) = &self.src_format {
src_format.clone()
} else {
get_import_format_from_extension(&get_extension(self.input.clone()))?
};
// TODO: let user choose the units.
let src_format = get_input_format(src_format, kittycad::types::UnitLength::Mm)?;
// Get the contents of the input file.
let file_location_str = self.input.to_str().unwrap_or_default();
let input = ctx.read_file(file_location_str)?;
let filename = self.input.file_name().unwrap_or_default().to_str().unwrap_or("");
// gltf with "standard" storage is an oddball in the KittyCAD system.
// In order for the program to know it's dealing with this type, an
// attempt to parse as json is made, then we check for the buffers
// property which describes what external files are needed.
let mut files: Vec<kittycad::types::ImportFile> = vec![kittycad::types::ImportFile {
path: filename.to_string(),
data: input.clone(),
}];
if let kittycad::types::InputFormat::Gltf {} = src_format {
if let Ok(str) = std::str::from_utf8(&input) {
if let Ok(json) = serde_json::from_str::<crate::types::GltfStandardJsonLite>(str) {
// Use the path of the control file as the prefix path of
// the relative file name.
for buffer in json.buffers {
if is_data_uri(&buffer.uri) {
// Using the whole data URI would create massive
// path properties. Use a hash instead.
let mut hasher = DefaultHasher::new();
buffer.uri.hash(&mut hasher);
let hash_u64 = hasher.finish();
if let Some(buf_base64) = buffer.uri.split(',').nth(1) {
files.push(kittycad::types::ImportFile {
path: hash_u64.to_string(),
data: BASE64_STANDARD.decode(buf_base64)?,
});
} else {
anyhow::bail!("invalid data uri in gltf.buffers.uri property");
}
} else {
let path_ = self
.input
.parent()
.unwrap_or(std::path::Path::new(""))
.join(std::path::Path::new(&buffer.uri));
let path = path_.to_str().unwrap_or_default();
let data = ctx.read_file(path)?;
files.push(kittycad::types::ImportFile {
path: path_.file_name().unwrap_or_default().to_str().unwrap_or("").to_string(),
data,
});
}
}
}
}
}
let engine = ctx.engine("", None).await?;
// Send an import request to the engine.
let resp = engine
.send_modeling_cmd(
uuid::Uuid::new_v4(),
kcl_lib::SourceRange::default(),
&kcmc::ModelingCmd::ImportFiles(kcmc::ImportFiles {
files: files.into_iter().map(|f| f.into()).collect(),
format: src_format.into(),
}),
)
.await?;
let kittycad_modeling_cmds::websocket::OkWebSocketResponseData::Modeling {
modeling_response: kittycad_modeling_cmds::ok_response::OkModelingCmdResponse::ImportFiles(data),
} = &resp
else {
anyhow::bail!("Unexpected response from engine import: {:?}", resp);
};
let object_id = data.object_id;
// Zoom on the object.
engine
.send_modeling_cmd(
uuid::Uuid::new_v4(),
kcl_lib::SourceRange::default(),
&kittycad_modeling_cmds::ModelingCmd::DefaultCameraFocusOn(
kittycad_modeling_cmds::DefaultCameraFocusOn { uuid: object_id },
),
)
.await?;
// Spin up websockets and do the conversion.
// This will not return until there are files.
let resp = engine
.send_modeling_cmd(
uuid::Uuid::new_v4(),
kcl_lib::SourceRange::default(),
&kittycad_modeling_cmds::ModelingCmd::TakeSnapshot(kittycad_modeling_cmds::TakeSnapshot {
format: output_format,
}),
)
.await?;
if let kittycad_modeling_cmds::websocket::OkWebSocketResponseData::Modeling {
modeling_response: kittycad_modeling_cmds::ok_response::OkModelingCmdResponse::TakeSnapshot(data),
} = &resp
{
// Save the snapshot locally.
std::fs::write(&self.output_file, &data.contents.0)?;
} else {
anyhow::bail!("Unexpected response from engine: {:?}", resp);
}
writeln!(
ctx.io.out,
"Snapshot saved to `{}`",
self.output_file.to_str().unwrap_or("")
)?;
Ok(())
}
}
/// Get the volume of an object in a CAD file.
///
/// If the input file is larger than a certain size it will be
/// performed asynchronously, you can then check the status with the
/// `zoo api-call status <id_of_your_operation>` command.
///
/// # get the volume of a file
/// $ zoo file volume my-file.step
///
/// # pass a file from stdin, the original file type is required
/// $ cat my-obj.obj | zoo file volume - --src-format=obj
#[derive(Parser, Debug, Clone)]
#[clap(verbatim_doc_comment)]
pub struct CmdFileVolume {
/// The path to the input file.
/// If you pass `-` as the path, the file will be read from stdin.
#[clap(name = "input", required = true)]
pub input: std::path::PathBuf,
/// A valid source file format.
#[clap(short = 's', long = "src-format", value_enum)]
src_format: Option<kittycad::types::FileImportFormat>,
/// Output format.
#[clap(long, short, value_enum)]
pub format: Option<crate::types::FormatOutput>,
/// Output unit.
#[clap(long = "output-unit", short = 'u', value_enum)]
pub output_unit: kittycad::types::UnitVolume,
}
#[async_trait::async_trait(?Send)]
impl crate::cmd::Command for CmdFileVolume {
async fn run(&self, ctx: &mut crate::context::Context) -> Result<()> {
// Parse the source format.
let src_format = if let Some(src_format) = &self.src_format {
src_format.clone()
} else {
get_import_format_from_extension(&get_extension(self.input.clone()))?
};
// Get the contents of the input file.
let input = ctx.read_file(self.input.to_str().unwrap_or(""))?;
// Do the operation.
let client = ctx.api_client("")?;
let file_volume = client
.file()
.create_volume(Some(self.output_unit.clone()), src_format, &input.into())
.await?;
// Print the output of the conversion.
let format = ctx.format(&self.format)?;
ctx.io.write_output(&format, &file_volume)?;
Ok(())
}
}
/// Get the mass of an object in a CAD file.
///
/// If the input file is larger than a certain size it will be
/// performed asynchronously, you can then check the status with the
/// `zoo api-call status <id_of_your_operation>` command.
///
/// # get the mass of a file
/// $ zoo file mass my-file.step
///
/// # pass a file from stdin, the original file type is required
/// $ cat my-obj.obj | zoo file mass - --src-format=obj
#[derive(Parser, Debug, Clone)]
#[clap(verbatim_doc_comment)]
pub struct CmdFileMass {
/// The path to the input file.
/// If you pass `-` as the path, the file will be read from stdin.
#[clap(name = "input", required = true)]
pub input: std::path::PathBuf,
/// A valid source file format.
#[clap(short = 's', long = "src-format", value_enum)]
src_format: Option<kittycad::types::FileImportFormat>,
/// Material density.
#[clap(short = 'm', long = "material-density")]
material_density: f32,
/// Material density unit.
#[clap(long = "material-density-unit", value_enum)]
material_density_unit: kittycad::types::UnitDensity,
/// Output format.
#[clap(long, short, value_enum)]
pub format: Option<crate::types::FormatOutput>,
/// Output unit.
#[clap(long = "output-unit", short = 'u', value_enum)]
pub output_unit: kittycad::types::UnitMass,
}
#[async_trait::async_trait(?Send)]
impl crate::cmd::Command for CmdFileMass {
async fn run(&self, ctx: &mut crate::context::Context) -> Result<()> {
if self.material_density == 0.0 {
anyhow::bail!("`--material-density` must not be 0.0");
}
// Parse the source format.
let src_format = if let Some(src_format) = &self.src_format {
src_format.clone()
} else {
get_import_format_from_extension(&get_extension(self.input.clone()))?
};
// Get the contents of the input file.
let input = ctx.read_file(self.input.to_str().unwrap_or(""))?;
// Do the operation.
let client = ctx.api_client("")?;
let file_mass = client
.file()
.create_mass(
self.material_density.into(),
Some(self.material_density_unit.clone()),
Some(self.output_unit.clone()),
src_format,
&input.into(),
)
.await?;
// Print the output of the conversion.
let format = ctx.format(&self.format)?;
ctx.io.write_output(&format, &file_mass)?;
Ok(())
}
}
/// Get the center of mass of an object in a CAD file.
///
/// If the input file is larger than a certain size it will be
/// performed asynchronously, you can then check the status with the
/// `zoo api-call status <id_of_your_operation>` command.
///
/// # get the mass of a file
/// $ zoo file center-of-mass my-file.step
///
/// # pass a file from stdin, the original file type is required
/// $ cat my-obj.obj | zoo file center-of-mass - --src-format=obj
#[derive(Parser, Debug, Clone)]
#[clap(verbatim_doc_comment)]
pub struct CmdFileCenterOfMass {
/// The path to the input file.
/// If you pass `-` as the path, the file will be read from stdin.
#[clap(name = "input", required = true)]
pub input: std::path::PathBuf,
/// A valid source file format.
#[clap(short = 's', long = "src-format", value_enum)]
src_format: Option<kittycad::types::FileImportFormat>,
/// Output format.
#[clap(long, short, value_enum)]
pub format: Option<crate::types::FormatOutput>,
/// Output unit.
#[clap(long = "output-unit", short = 'u', value_enum)]
pub output_unit: kittycad::types::UnitLength,
}
#[async_trait::async_trait(?Send)]
impl crate::cmd::Command for CmdFileCenterOfMass {
async fn run(&self, ctx: &mut crate::context::Context) -> Result<()> {
// Parse the source format.
let src_format = if let Some(src_format) = &self.src_format {
src_format.clone()
} else {
get_import_format_from_extension(&get_extension(self.input.clone()))?
};
// Get the contents of the input file.
let input = ctx.read_file(self.input.to_str().unwrap_or(""))?;
// Do the operation.
let client = ctx.api_client("")?;
let file_center_of_mass = client
.file()
.create_center_of_mass(Some(self.output_unit.clone()), src_format, &input.into())
.await?;
// Print the output of the conversion.
let format = ctx.format(&self.format)?;
ctx.io.write_output(&format, &file_center_of_mass)?;
Ok(())
}
}
/// Get the density of an object in a CAD file.
///
/// If the input file is larger than a certain size it will be
/// performed asynchronously, you can then check the status with the
/// `zoo api-call status <id_of_your_operation>` command.
///
/// # get the density of a file
/// $ zoo file density my-file.step
///
/// # pass a file from stdin, the original file type is required
/// $ cat my-obj.obj | zoo file density - --src-format=obj
#[derive(Parser, Debug, Clone)]
#[clap(verbatim_doc_comment)]
pub struct CmdFileDensity {
/// The path to the input file.
/// If you pass `-` as the path, the file will be read from stdin.
#[clap(name = "input", required = true)]
pub input: std::path::PathBuf,
/// A valid source file format.
#[clap(short = 's', long = "src-format")]
src_format: Option<kittycad::types::FileImportFormat>,
/// Material mass.
#[clap(short = 'm', long = "material-mass")]
material_mass: f32,
/// The unit of the material mass.
#[clap(long = "material-mass-unit", value_enum)]
material_mass_unit: kittycad::types::UnitMass,
/// Output format.
#[clap(long, short, value_enum)]
pub format: Option<crate::types::FormatOutput>,
/// Output unit.
#[clap(long = "output-unit", short = 'u', value_enum)]
pub output_unit: kittycad::types::UnitDensity,
}
#[async_trait::async_trait(?Send)]
impl crate::cmd::Command for CmdFileDensity {
async fn run(&self, ctx: &mut crate::context::Context) -> Result<()> {
if self.material_mass == 0.0 {
anyhow::bail!("`--material-mass` must not be 0.0");
}
// Parse the source format.
let src_format = if let Some(src_format) = &self.src_format {
src_format.clone()
} else {
get_import_format_from_extension(&get_extension(self.input.clone()))?
};
// Get the contents of the input file.
let input = ctx.read_file(self.input.to_str().unwrap_or(""))?;
// Do the operation.
let client = ctx.api_client("")?;
let file_density = client
.file()
.create_density(
self.material_mass.into(),
Some(self.material_mass_unit.clone()),
Some(self.output_unit.clone()),
src_format,
&input.into(),
)
.await?;
// Print the output of the conversion.
let format = ctx.format(&self.format)?;
ctx.io.write_output(&format, &file_density)?;
Ok(())
}
}
/// Get the surface area of an object in a CAD file.
///
/// If the input file is larger than a certain size it will be
/// performed asynchronously, you can then check the status with the
/// `zoo api-call status <id_of_your_operation>` command.
///
/// # get the surface-area of a file
/// $ zoo file surface-area my-file.step
///
/// # pass a file from stdin, the original file type is required
/// $ cat my-obj.obj | zoo file surface-area - --src-format=obj
#[derive(Parser, Debug, Clone)]
#[clap(verbatim_doc_comment)]
pub struct CmdFileSurfaceArea {
/// The path to the input file.
/// If you pass `-` as the path, the file will be read from stdin.
#[clap(name = "input", required = true)]
pub input: std::path::PathBuf,
/// A valid source file format.
#[clap(short = 's', long = "src-format")]
src_format: Option<kittycad::types::FileImportFormat>,
/// Output format.
#[clap(long, short, value_enum)]
pub format: Option<crate::types::FormatOutput>,
/// Output unit.
#[clap(long = "output-unit", short = 'u', value_enum)]
pub output_unit: kittycad::types::UnitArea,
}
#[async_trait::async_trait(?Send)]
impl crate::cmd::Command for CmdFileSurfaceArea {
async fn run(&self, ctx: &mut crate::context::Context) -> Result<()> {
// Parse the source format.
let src_format = if let Some(src_format) = &self.src_format {
src_format.clone()
} else {
get_import_format_from_extension(&get_extension(self.input.clone()))?
};
// Get the contents of the input file.
let input = ctx.read_file(self.input.to_str().unwrap_or(""))?;
// Do the operation.
let client = ctx.api_client("")?;
let file_surface_area = client
.file()
.create_surface_area(Some(self.output_unit.clone()), src_format, &input.into())
.await?;
// Print the output of the conversion.
let format = ctx.format(&self.format)?;
ctx.io.write_output(&format, &file_surface_area)?;
Ok(())
}
}
/// Get the extension for a path buffer.
pub fn get_extension(path: std::path::PathBuf) -> String {
path.into_boxed_path()
.extension()
.unwrap_or_default()
.to_str()
.unwrap_or("")
.to_string()
}
/// Get the source format from the extension.
fn get_import_format_from_extension(ext: &str) -> Result<kittycad::types::FileImportFormat> {
match kittycad::types::FileImportFormat::from_str(ext) {
Ok(format) => Ok(format),
Err(_) => {
if ext == "stp" {
Ok(kittycad::types::FileImportFormat::Step)
} else if ext == "glb" {
Ok(kittycad::types::FileImportFormat::Gltf)
} else {
anyhow::bail!(
"unknown source format for file extension: {}. Try setting the `--src-format` flag explicitly or use a valid format.",
ext
)
}
}
}
}
/// Get the source format from the extension.
fn get_input_format(
format: kittycad::types::FileImportFormat,
ul: kittycad::types::UnitLength,
) -> Result<kittycad::types::InputFormat> {
// Zoo co-ordinate system.
//
// * Forward: -Y
// * Up: +Z
// * Handedness: Right
let coords = kittycad::types::System {
forward: kittycad::types::AxisDirectionPair {
axis: kittycad::types::Axis::Y,
direction: kittycad::types::Direction::Negative,
},
up: kittycad::types::AxisDirectionPair {
axis: kittycad::types::Axis::Z,
direction: kittycad::types::Direction::Positive,
},
};
match format {
kittycad::types::FileImportFormat::Step => Ok(kittycad::types::InputFormat::Step {
split_closed_faces: false,
}),
kittycad::types::FileImportFormat::Stl => Ok(kittycad::types::InputFormat::Stl { coords, units: ul }),
kittycad::types::FileImportFormat::Obj => Ok(kittycad::types::InputFormat::Obj { coords, units: ul }),
kittycad::types::FileImportFormat::Gltf => Ok(kittycad::types::InputFormat::Gltf {}),
kittycad::types::FileImportFormat::Ply => Ok(kittycad::types::InputFormat::Ply { coords, units: ul }),
kittycad::types::FileImportFormat::Fbx => Ok(kittycad::types::InputFormat::Fbx {}),
kittycad::types::FileImportFormat::Sldprt => Ok(kittycad::types::InputFormat::Sldprt {
split_closed_faces: false,
}),
}
}
/// Determine if buffers[].buffer.uri is a data uri.
fn is_data_uri(s: &str) -> bool {
matches!(s.split(':').next(), Some("data"))
}
#[cfg(test)]
mod test {
use pretty_assertions::assert_eq;
use crate::cmd::Command;
pub struct TestItem {
name: String,
cmd: crate::cmd_file::SubCommand,
stdin: String,
want_out: String,
want_err: String,
}
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
#[serial_test::serial]
async fn test_cmd_file() {
let tests: Vec<TestItem> = vec![
TestItem {
name: "convert input with bad ext".to_string(),
cmd: crate::cmd_file::SubCommand::Convert(crate::cmd_file::CmdFileConvert {
input: std::path::PathBuf::from("test/bad_ext.bad_ext"),
output_dir: std::path::PathBuf::from("tests/"),
output_format: kittycad::types::FileExportFormat::Obj,
src_format: None,
format: None,
deterministic:false,
}),
stdin: "".to_string(),
want_out: "".to_string(),
want_err: "unknown source format for file extension: bad_ext. Try setting the `--src-format` flag explicitly or use a valid format.".to_string(),
},
TestItem {
name: "convert: input file does not exist".to_string(),
cmd: crate::cmd_file::SubCommand::Convert(crate::cmd_file::CmdFileConvert {
input: std::path::PathBuf::from("test/bad_ext.stp"),
output_dir: std::path::PathBuf::from("tests/"),
output_format: kittycad::types::FileExportFormat::Obj,
src_format: None,
format: None,
deterministic:false,
}),
stdin: "".to_string(),
want_out: "".to_string(),
want_err: "File 'test/bad_ext.stp' does not exist.".to_string(),
},
TestItem {
name: "volume with bad ext".to_string(),
cmd: crate::cmd_file::SubCommand::Volume(crate::cmd_file::CmdFileVolume {
input: std::path::PathBuf::from("tests/bad_ext.bad_ext"),
src_format: None,
format: None,
output_unit: kittycad::types::UnitVolume::Cm3,
}),
stdin: "".to_string(),
want_out: "".to_string(),
want_err: "unknown source format for file extension: bad_ext. Try setting the `--src-format` flag explicitly or use a valid format.".to_string(),
},
TestItem {
name: "volume: input file does not exist".to_string(),
cmd: crate::cmd_file::SubCommand::Volume(crate::cmd_file::CmdFileVolume {
input: std::path::PathBuf::from("test/bad_ext.stp"),
src_format: None,
format: None,
output_unit: kittycad::types::UnitVolume::Cm3,
}),
stdin: "".to_string(),
want_out: "".to_string(),
want_err: "File 'test/bad_ext.stp' does not exist.".to_string(),
}
];
let mut config = crate::config::new_blank_config().unwrap();
let mut c = crate::config_from_env::EnvConfig::inherit_env(&mut config);
for t in tests {
let (mut io, stdout_path, stderr_path) = crate::iostreams::IoStreams::test();
if !t.stdin.is_empty() {
io.stdin = Box::new(std::io::Cursor::new(t.stdin));
}
// We need to also turn off the fancy terminal colors.
// This ensures it also works in GitHub actions/any CI.
io.set_color_enabled(false);
// TODO: we should figure out how to test the prompts.
io.set_never_prompt(true);
let mut ctx = crate::context::Context {
config: &mut c,
io,
debug: false,
};
let cmd_file = crate::cmd_file::CmdFile { subcmd: t.cmd };
match cmd_file.run(&mut ctx).await {
Ok(()) => {
let stdout = std::fs::read_to_string(stdout_path).unwrap();
let stderr = std::fs::read_to_string(stderr_path).unwrap();
assert!(stderr.is_empty(), "test {}: {}", t.name, stderr);
if !stdout.contains(&t.want_out) {
assert_eq!(stdout, t.want_out, "test {}: stdout mismatch", t.name);
}
}
Err(err) => {
let stdout = std::fs::read_to_string(stdout_path).unwrap();
let stderr = std::fs::read_to_string(stderr_path).unwrap();
assert_eq!(stdout, t.want_out, "test {}", t.name);
if !err.to_string().contains(&t.want_err) {
assert_eq!(err.to_string(), t.want_err, "test {}: err mismatch", t.name);
}
assert!(stderr.is_empty(), "test {}: {}", t.name, stderr);
}
}
}
}
}