ExportSelectionTool.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * ExportSelectionTool.js
  3. *
  4. * @author realor
  5. */
  6. import { Tool } from './Tool.js'
  7. import { Controls } from '../ui/Controls.js'
  8. import { PropertySelectorDialog } from '../ui/PropertySelectorDialog.js'
  9. import { MessageDialog } from '../ui/MessageDialog.js'
  10. import { ObjectUtils } from '../utils/ObjectUtils.js'
  11. import * as THREE from '../lib/three.module.js'
  12. class ExportSelectionTool extends Tool {
  13. constructor(application, options) {
  14. super(application)
  15. this.name = 'export_selection'
  16. this.label = 'tool.export_selection.label'
  17. this.help = 'tool.export_selection.help'
  18. this.className = 'export_selection'
  19. this.setOptions(options)
  20. this.immediate = true
  21. this.dialog = new PropertySelectorDialog(this.application, {
  22. title: 'title.export_selection',
  23. selectValues: false,
  24. treeLabel: 'label.selection_properties',
  25. editorLabel: 'label.exported_properties',
  26. findPropertiesOnSelection: true
  27. })
  28. const dialog = this.dialog
  29. this.addButton = dialog.addContextButton('add_prop', 'button.add', () => this.addProperty())
  30. this.clearButton = dialog.addContextButton('clear_prop', 'button.clear', () => this.clearProperties())
  31. dialog.onAccept = () => this.exportProperties()
  32. dialog.updateContextButtons = () => {
  33. let path = dialog.getSelectedNodePath()
  34. this.addButton.disabled = path.length === 0
  35. }
  36. }
  37. execute() {
  38. this.dialog.show()
  39. }
  40. addProperty() {
  41. const dialog = this.dialog
  42. let path = dialog.getSelectedNodePath()
  43. let propertyMap = dialog.propertyMap
  44. let paths = []
  45. if (path.length === 1) {
  46. let items = propertyMap.get(path[0])
  47. if (items instanceof Map) {
  48. for (let key of items.keys()) {
  49. paths.push([path[0], key])
  50. }
  51. } else paths.push(path)
  52. } else paths.push(path)
  53. for (let path of paths) {
  54. let line = '"' + path[path.length - 1] + '" : $('
  55. for (let i = 0; i < path.length; i++) {
  56. let part = path[i]
  57. if (i > 0) line += ', '
  58. line += '"' + part + '"'
  59. }
  60. line += ')'
  61. dialog.appendCode(line + '\n')
  62. }
  63. }
  64. clearProperties() {
  65. const dialog = this.dialog
  66. dialog.setCode('')
  67. }
  68. exportProperties() {
  69. const application = this.application
  70. const dialog = this.dialog
  71. try {
  72. let properties = dialog.getCode()
  73. let lines = properties.split('\n').filter(line => line.trim().length > 0)
  74. let exportExpression = '{' + lines.join(',') + '}'
  75. let exportedData = []
  76. let fn = ObjectUtils.createEvalFunction(exportExpression)
  77. let headersObject = fn(new THREE.Object3D())
  78. let headers = []
  79. for (let key in headersObject) {
  80. headers.push(key)
  81. }
  82. exportedData.push(headers.join(';'))
  83. let objects = application.selection.objects
  84. for (let object of objects) {
  85. exportedData.push(this.toCSVRow(fn(object)))
  86. }
  87. let csv = '\uFEFF' + exportedData.join('\n')
  88. const blob = new Blob([csv], { type: 'text/csv' })
  89. let url = window.URL.createObjectURL(blob)
  90. let linkElem = document.createElement('a')
  91. linkElem.download = 'export.csv'
  92. linkElem.target = '_blank'
  93. linkElem.href = url
  94. linkElem.style.display = 'block'
  95. linkElem.click()
  96. dialog.hide()
  97. } catch (ex) {
  98. MessageDialog.create('ERROR', ex)
  99. .setClassName('error')
  100. .setI18N(this.application.i18n)
  101. .show()
  102. }
  103. }
  104. toCSVRow(data) {
  105. let line = ''
  106. for (let columnName in data) {
  107. let value = data[columnName]
  108. if (line.length > 0) line += ';'
  109. if (value === undefined) {
  110. } else if (typeof value === 'string') {
  111. line += '"' + value + '"'
  112. } else if (typeof value === 'number') {
  113. line += ('' + value).replace('.', ',')
  114. } else line += value
  115. }
  116. return line
  117. }
  118. }
  119. export { ExportSelectionTool }