LoadController.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * LoadController.js
  3. *
  4. * @author realor
  5. */
  6. import { Controller } from './Controller.js'
  7. import { IOManager } from '../io/IOManager.js'
  8. import { ObjectUtils } from '../utils/ObjectUtils.js'
  9. import { Formula } from '../formula/Formula.js'
  10. class LoadController extends Controller {
  11. constructor(object, name) {
  12. super(object, name)
  13. this.url = 'https://your_server.com/model.brf'
  14. this.autoStart = false
  15. this._url = null // last url loaded
  16. this._onLoad = this.onLoad.bind(this)
  17. this._onProgress = this.onProgress.bind(this)
  18. this._onError = this.onError.bind(this)
  19. }
  20. onStart() {
  21. this.updateFormulas()
  22. const object = this.object
  23. if (object.userData.export === undefined) {
  24. object.userData.export = {}
  25. }
  26. object.userData.export.exportChildren = false
  27. let url = this.url
  28. if (url.trim().length === 0) return
  29. if (url[0] === '/') {
  30. url = document.location.protocol + '//' + document.location.host + url
  31. }
  32. if (url !== this._url || this.object.children.length === 0) {
  33. if (url !== this._url && this.object.children.length > 0) {
  34. this.clearModel()
  35. }
  36. this.loadModel(url)
  37. }
  38. }
  39. onStop() {}
  40. onLoad(dae) {
  41. this.application.addObject(dae.scene, this.object)
  42. }
  43. onProgress() {}
  44. onError(error) {
  45. console.info(error)
  46. }
  47. loadModel(url) {
  48. const application = this.application
  49. const intent = {
  50. url: url,
  51. options: { units: this.application.units },
  52. onCompleted: object => {
  53. this._url = url
  54. object.updateMatrix()
  55. application.initControllers(object)
  56. application.addObject(object, this.object)
  57. },
  58. onError: error => this.onError(error)
  59. }
  60. IOManager.load(intent)
  61. }
  62. clearModel() {
  63. const application = this.application
  64. ObjectUtils.dispose(this.object)
  65. this.object.clear()
  66. application.notifyObjectsChanged(this.object, this, 'structureChanged')
  67. }
  68. }
  69. Controller.addClass(LoadController)
  70. export { LoadController }