controls.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * Copyright (c) 2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Sukolsak Sakshuwong <sukolsak@stanford.edu>
  5. */
  6. import { PluginComponent } from '../../mol-plugin-state/component';
  7. import { PluginContext } from '../../mol-plugin/context';
  8. import { Task } from '../../mol-task';
  9. import { PluginStateObject } from '../../mol-plugin-state/objects';
  10. import { StateSelection } from '../../mol-state';
  11. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  12. import { SetUtils } from '../../mol-util/set';
  13. import { ObjExporter } from './obj-exporter';
  14. import { GlbExporter } from './glb-exporter';
  15. import { StlExporter } from './stl-exporter';
  16. export const GeometryParams = {
  17. format: PD.Select('glb', [
  18. ['glb', 'glTF 2.0 Binary (.glb)'],
  19. ['stl', 'Stl (.stl)'],
  20. ['obj', 'Wavefront (.obj)']
  21. ])
  22. };
  23. export class GeometryControls extends PluginComponent {
  24. readonly behaviors = {
  25. params: this.ev.behavior<PD.Values<typeof GeometryParams>>(PD.getDefaultValues(GeometryParams))
  26. }
  27. private getFilename() {
  28. const models = this.plugin.state.data.select(StateSelection.Generators.rootsOfType(PluginStateObject.Molecule.Model)).map(s => s.obj!.data);
  29. const uniqueIds = new Set<string>();
  30. models.forEach(m => uniqueIds.add(m.entryId.toUpperCase()));
  31. const idString = SetUtils.toArray(uniqueIds).join('-');
  32. return `${idString || 'molstar-model'}`;
  33. }
  34. exportGeometry() {
  35. const task = Task.create('Export Geometry', async ctx => {
  36. try {
  37. const renderObjects = this.plugin.canvas3d?.getRenderObjects()!;
  38. const filename = this.getFilename();
  39. let renderObjectExporter: ObjExporter | GlbExporter | StlExporter;
  40. switch (this.behaviors.params.value.format) {
  41. case 'obj':
  42. renderObjectExporter = new ObjExporter(filename);
  43. break;
  44. case 'glb':
  45. renderObjectExporter = new GlbExporter();
  46. break;
  47. case 'stl':
  48. renderObjectExporter = new StlExporter();
  49. break;
  50. default: throw new Error('Unsupported format.');
  51. }
  52. for (let i = 0, il = renderObjects.length; i < il; ++i) {
  53. await ctx.update({ message: `Exporting object ${i}/${il}` });
  54. await renderObjectExporter.add(renderObjects[i], this.plugin.canvas3d?.webgl!, ctx);
  55. }
  56. const blob = await renderObjectExporter.getBlob(ctx);
  57. return {
  58. blob,
  59. filename: filename + '.' + renderObjectExporter.fileExtension
  60. };
  61. } catch (e) {
  62. this.plugin.log.error('' + e);
  63. throw e;
  64. }
  65. });
  66. return this.plugin.runTask(task, { useOverlay: true });
  67. }
  68. constructor(private plugin: PluginContext) {
  69. super();
  70. }
  71. }