MeasureSelectionTool.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * MeasureSelectionTool.js
  3. *
  4. * @author realor
  5. */
  6. import { Tool } from './Tool.js'
  7. import { Solid } from '../core/Solid.js'
  8. import { Dialog } from '../ui/Dialog.js'
  9. class MeasureSelectionTool extends Tool {
  10. constructor(application, options) {
  11. super(application)
  12. this.name = 'measure_selection'
  13. this.label = 'tool.measure_selection.label'
  14. this.className = 'measure_selection'
  15. this.setOptions(options)
  16. this.immediate = true
  17. }
  18. execute() {
  19. let area = 0
  20. let volume = 0
  21. let solids = 0
  22. const application = this.application
  23. const roots = application.selection.roots
  24. for (let object of roots) {
  25. object.traverse(obj => {
  26. if (obj instanceof Solid) {
  27. if (obj.visible) {
  28. solids++
  29. area += obj.getArea()
  30. volume += obj.getVolume()
  31. }
  32. }
  33. })
  34. }
  35. const decimals = application.decimals
  36. const units = ' ' + application.units
  37. const dialog = new Dialog(this.label)
  38. dialog.setSize(240, 160)
  39. dialog.setI18N(application.i18n)
  40. dialog.addTextWithArgs('message.solids_count', [solids], 'row')
  41. dialog.addTextWithArgs('message.solids_area', [area.toFixed(decimals), units], 'row')
  42. dialog.addTextWithArgs('message.solids_volume', [volume.toFixed(decimals), units], 'row')
  43. let av = volume === 0 ? 0 : area / volume
  44. dialog.addTextWithArgs('message.solids_area_volume', [av.toFixed(decimals)], 'row')
  45. let button = dialog.addButton('accept', 'button.accept', () => dialog.hide())
  46. dialog.onShow = () => button.focus()
  47. dialog.show()
  48. }
  49. }
  50. export { MeasureSelectionTool }