Vue 之webpack BIMFACE(JavaScript 转 Vue) 搭建工程

x33g5p2x  于2022-03-09 转载在 Java  
字(7.7k)|赞(0)|评价(0)|浏览(579)

1、Vue 之 BIMFACE 搭建工程

在webpack当中执行如下语句

  1. npm i bimfacesdkloader

2、原本案例的JS代码

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <meta name="renderer" content="webkit">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>隐藏构件</title>
  8. <style media="screen">
  9. * {
  10. margin: 0;
  11. padding: 0
  12. }
  13. html,
  14. body {
  15. height: 100%
  16. }
  17. .main {
  18. display: flex;
  19. flex-direction: column;
  20. overflow: hidden;
  21. height: 100%;
  22. width: 100%
  23. }
  24. .model {
  25. flex: 1
  26. }
  27. .buttons {
  28. font-size: 0;
  29. }
  30. .button {
  31. margin: 5px 0 5px 5px;
  32. width: 120px;
  33. height: 30px;
  34. border-radius: 3px;
  35. border: none;
  36. background: #32D3A6;
  37. color: #FFFFFF;
  38. }
  39. </style>
  40. <!-- 引用BIMFACE的JavaScript显示组件库 -->
  41. <script src="https://static.bimface.com/api/BimfaceSDKLoader/BimfaceSDKLoader@latest-release.js" charset="utf-8">
  42. </script>
  43. </head>
  44. <body>
  45. <!-- 定义DOM元素,用于在该DOM元素中显示模型或图纸 -->
  46. <div class='main'>
  47. <div class='buttons'>
  48. <button type="button" class="button" id="btnHideComponentsByObjectId"
  49. onclick="hideComponentsById()">根据ID隐藏构件</button>
  50. <button type="button" class="button" id="btnHideComponentsByObjectData"
  51. onclick="hideComponentsByData()">根据条件隐藏构件</button>
  52. </div>
  53. <!-- 定义DOM元素,用于在该DOM元素中显示模型或图纸 -->
  54. <div class='model' id="domId"></div>
  55. </div>
  56. <script type="text/javascript">
  57. let viewer3D;
  58. let app;
  59. let model3D;
  60. let viewToken = '48c4c5b367c348b08cca7a1b935226b9';
  61. // 初始化显示组件
  62. let options = new BimfaceSDKLoaderConfig();
  63. options.viewToken = viewToken;
  64. BimfaceSDKLoader.load(options, successCallback, failureCallback);
  65. function successCallback(viewMetaData) {
  66. if (viewMetaData.viewType == "3DView") {
  67. // ======== 判断是否为3D模型 ========
  68. // 获取DOM元素
  69. let dom4Show = document.getElementById('domId');
  70. let webAppConfig = new Glodon.Bimface.Application.WebApplication3DConfig();
  71. webAppConfig.domElement = dom4Show;
  72. // 创建WebApplication
  73. app = new Glodon.Bimface.Application.WebApplication3D(webAppConfig);
  74. // 添加待显示的模型
  75. app.addView(viewToken);
  76. // 从WebApplication获取viewer3D对象
  77. viewer3D = app.getViewer();
  78. // 监听添加view完成的事件
  79. viewer3D.addEventListener(Glodon.Bimface.Viewer.Viewer3DEvent.ViewAdded, function () {
  80. // 调用viewer3D对象的Method,可以继续扩展功能
  81. //自适应屏幕大小
  82. window.onresize = function () {
  83. viewer3D.resize(document.documentElement.clientWidth, document.documentElement.clientHeight - 40)
  84. }
  85. // 从viewer3D对象中获取模型对象model3D
  86. model3D = viewer3D.getModel();
  87. // 渲染3D模型
  88. viewer3D.render();
  89. });
  90. }
  91. }
  92. function failureCallback(error) {
  93. console.log(error);
  94. }
  95. //********************根据Id隐藏构件*********************
  96. let isHideComponentsByIdActivated = false;
  97. function hideComponentsById() {
  98. if (!isHideComponentsByIdActivated) {
  99. model3D.hideComponentsById(["307240", "267327", "307240", "259504", "264857"]);
  100. viewer3D.render();
  101. setButtonText("btnHideComponentsByObjectId", "显示构件");
  102. } else {
  103. //取消隐藏
  104. model3D.showComponentsById(["307240", "267327", "307240", "259504", "264857"]);
  105. viewer3D.render();
  106. setButtonText("btnHideComponentsByObjectId", "根据ID隐藏构件");
  107. }
  108. isHideComponentsByIdActivated = !isHideComponentsByIdActivated;
  109. }
  110. //********************根据条件隐藏构件*********************
  111. let isHideComponentsByDataActivated = false;
  112. function hideComponentsByData() {
  113. if (!isHideComponentsByDataActivated) {
  114. //设置隐藏构件的条件
  115. model3D.hideComponentsByObjectData([{
  116. "levelName": "F2"
  117. }, {
  118. "levelName": "F1"
  119. }, {
  120. "levelName": "标高 5"
  121. }]);
  122. viewer3D.render();
  123. setButtonText("btnHideComponentsByObjectData", "显示构件");
  124. } else {
  125. //取消隐藏
  126. model3D.showComponentsByObjectData([{
  127. "levelName": "F2"
  128. }, {
  129. "levelName": "F1"
  130. }, {
  131. "levelName": "标高 5"
  132. }]);
  133. viewer3D.render();
  134. setButtonText("btnHideComponentsByObjectData", "根据条件隐藏构件");
  135. }
  136. isHideComponentsByDataActivated = !isHideComponentsByDataActivated;
  137. }
  138. // *********************** 按钮文字 ***********************
  139. function setButtonText(btnId, text) {
  140. let dom = document.getElementById(btnId);
  141. if (dom != null && dom.nodeName == "BUTTON") {
  142. dom.innerText = text;
  143. }
  144. }
  145. </script>
  146. </body>

3、转换为Vue代码

(1)引入依赖

  1. import {BimfaceSDKLoader,BimfaceSDKLoaderConfig,Glodon} from 'bimfacesdkloader'

(2)mounted(将JS的代码直接复制到mounted当中)

在div上设置一下标题

  1. ref="bfContainer"

获取当中对应的div,负责到渲染BIM项目当中

4、转换后全部Vue代码(HelloWorld.vue)

  1. <template>
  2. <div class="hello" ref="bfContainer">
  3. </div>
  4. </template>
  5. <script>
  6. import {BimfaceSDKLoader,BimfaceSDKLoaderConfig,Glodon} from 'bimfacesdkloader'
  7. export default {
  8. name: 'HelloWorld',
  9. data () {
  10. return {
  11. msg: 'Welcome to Your Vue.js App'
  12. }
  13. },
  14. mounted(){
  15. const dom = this.$refs.bfContainer;
  16. let viewer3D;
  17. let app;
  18. let model3D;
  19. let viewToken = '48c4c5b367c348b08cca7a1b935226b9';
  20. // 初始化显示组件
  21. let options = new BimfaceSDKLoaderConfig();
  22. options.viewToken = viewToken;
  23. // BimfaceSDKLoader.load(options, successCallback, failureCallback);
  24. BimfaceSDKLoader.load(options).then((viewMetaData)=>{
  25. if (viewMetaData.viewType == "3DView") {
  26. // ======== 判断是否为3D模型 ========
  27. // 获取DOM元素
  28. let dom4Show = document.getElementById('domId');
  29. let webAppConfig = new Glodon.Bimface.Application.WebApplication3DConfig();
  30. webAppConfig.domElement = dom;
  31. // 创建WebApplication
  32. app = new Glodon.Bimface.Application.WebApplication3D(webAppConfig);
  33. // 添加待显示的模型
  34. app.addView(viewToken);
  35. // 从WebApplication获取viewer3D对象
  36. viewer3D = app.getViewer();
  37. // 监听添加view完成的事件
  38. viewer3D.addEventListener(Glodon.Bimface.Viewer.Viewer3DEvent.ViewAdded, function () {
  39. // 调用viewer3D对象的Method,可以继续扩展功能
  40. //自适应屏幕大小
  41. window.onresize = function () {
  42. viewer3D.resize(document.documentElement.clientWidth, document.documentElement.clientHeight - 40)
  43. }
  44. // 从viewer3D对象中获取模型对象model3D
  45. model3D = viewer3D.getModel();
  46. // 渲染3D模型
  47. viewer3D.render();
  48. });
  49. }
  50. });
  51. function successCallback(viewMetaData) {
  52. }
  53. function failureCallback(error) {
  54. console.log(error);
  55. }
  56. //********************根据Id隐藏构件*********************
  57. let isHideComponentsByIdActivated = false;
  58. function hideComponentsById() {
  59. if (!isHideComponentsByIdActivated) {
  60. model3D.hideComponentsById(["307240", "267327", "307240", "259504", "264857"]);
  61. viewer3D.render();
  62. setButtonText("btnHideComponentsByObjectId", "显示构件");
  63. } else {
  64. //取消隐藏
  65. model3D.showComponentsById(["307240", "267327", "307240", "259504", "264857"]);
  66. viewer3D.render();
  67. setButtonText("btnHideComponentsByObjectId", "根据ID隐藏构件");
  68. }
  69. isHideComponentsByIdActivated = !isHideComponentsByIdActivated;
  70. }
  71. //********************根据条件隐藏构件*********************
  72. let isHideComponentsByDataActivated = false;
  73. function hideComponentsByData() {
  74. if (!isHideComponentsByDataActivated) {
  75. //设置隐藏构件的条件
  76. model3D.hideComponentsByObjectData([{
  77. "levelName": "F2"
  78. }, {
  79. "levelName": "F1"
  80. }, {
  81. "levelName": "标高 5"
  82. }]);
  83. viewer3D.render();
  84. setButtonText("btnHideComponentsByObjectData", "显示构件");
  85. } else {
  86. //取消隐藏
  87. model3D.showComponentsByObjectData([{
  88. "levelName": "F2"
  89. }, {
  90. "levelName": "F1"
  91. }, {
  92. "levelName": "标高 5"
  93. }]);
  94. viewer3D.render();
  95. setButtonText("btnHideComponentsByObjectData", "根据条件隐藏构件");
  96. }
  97. isHideComponentsByDataActivated = !isHideComponentsByDataActivated;
  98. }
  99. // *********************** 按钮文字 ***********************
  100. function setButtonText(btnId, text) {
  101. let dom = document.getElementById(btnId);
  102. if (dom != null && dom.nodeName == "BUTTON") {
  103. dom.innerText = text;
  104. }
  105. }
  106. }
  107. }
  108. </script>
  109. <!-- Add "scoped" attribute to limit CSS to this component only -->
  110. <style scoped>
  111. h1, h2 {
  112. font-weight: normal;
  113. }
  114. .hello{
  115. height: 100vh;;
  116. }
  117. ul {
  118. list-style-type: none;
  119. padding: 0;
  120. }
  121. li {
  122. display: inline-block;
  123. margin: 0 10px;
  124. }
  125. a {
  126. color: #42b983;
  127. }
  128. </style>

相关文章

最新文章

更多