BooleanOperationTool.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * BooleanOperationTool.js
  3. *
  4. * @author realor
  5. */
  6. import { Tool } from './Tool.js'
  7. import { Solid } from '../core/Solid.js'
  8. import { ObjectBuilder } from '../builders/ObjectBuilder.js'
  9. import { BooleanOperator } from '../builders/BooleanOperator.js'
  10. import { I18N } from '../i18n/I18N.js'
  11. import * as THREE from '../lib/three.module.js'
  12. class BooleanOperationTool extends Tool {
  13. constructor(application, options) {
  14. super(application)
  15. this.name = 'boolean_operation'
  16. this.label = 'tool.boolean_operation.label'
  17. this.help = 'tool.boolean_operation.help'
  18. this.className = 'boolean_operation'
  19. this.operation = BooleanOperator.SUBTRACT
  20. this.keepParent = true
  21. this.immediate = true
  22. this.setOptions(options)
  23. this.material = new THREE.MeshPhongMaterial({ color: 0x4040ff, side: THREE.DoubleSide })
  24. }
  25. execute() {
  26. const application = this.application
  27. let operands = application.selection.roots
  28. // let operands = [];
  29. // for (let object of objects)
  30. // {
  31. // if (object instanceof THREE.Mesh)
  32. // {
  33. // let solid = new Solid(object.geometry.clone());
  34. // object.matrixWorld.decompose(
  35. // solid.position, solid.rotation, solid.scale);
  36. // solid.updateMatrix();
  37. // solid.updateMatrixWorld();
  38. // operands.push(solid);
  39. // }
  40. // else if (object instanceof Solid)
  41. // {
  42. // operands.push(object);
  43. // }
  44. // }
  45. if (operands.length > 1) {
  46. const parent = operands[0].parent
  47. let result = new Solid()
  48. result.name = this.operation
  49. for (let operand of operands) {
  50. let removeEvent = { type: 'removed', object: operand, parent: operand.parent, source: this }
  51. result.attach(operand)
  52. application.notifyEventListeners('scene', removeEvent)
  53. }
  54. result.builder = new BooleanOperator(this.operation)
  55. ObjectBuilder.build(result)
  56. if (this.keepParent) {
  57. application.addObject(result, parent, true, true)
  58. } else {
  59. application.addObject(result, application.baseObject, true, true)
  60. }
  61. application.selection.set(result)
  62. }
  63. }
  64. }
  65. export { BooleanOperationTool }