// 异步加载
const LoadObj = async (key?: string) => {
      LoadState.value = true
      var objStr, mtlStr
      var imgStrObj = {}
      let len = 0

      const zp: any = (await getoBJList({ key })).data

      JSZip.loadAsync(zp).then(function (zip) {
        zip.forEach((relativePath: string, file: JSZip.JSZipObject) => {
          try {
            len++
            let blobs = false
            if (file.name.indexOf('.obj') > 0 || file.name.indexOf('.mtl') > 0)
              blobs = true

            zip
              .file(file.name)
              .async(blobs ? 'text' : 'base64')
              .then(
                function success(text) {
                  len--

                  if (file.name.indexOf('.obj') > 0) objStr = text
                  else if (file.name.indexOf('.mtl') > 0) mtlStr = text
                  else
                    ht.Default.setImage(
                      file.name,
                      (imgStrObj[file.name] = 'data:image/png;base64,' + text)
                    )
                  if (len <= 0) {
                    try {
                      dataModel.clear()

                      var rawS3 = parseObj('2121', objStr, mtlStr, imgStrObj)

                      var node = new ht.Node()
                      node.s({
                        shape3d: '2121',
                        '3d.selectable': false
                      })
                      node.s3(rawS3)
                      node.p3(0, rawS3[1] / 2, 0)
                      dataModel.add(node)
                      var sz = node.getSize3d()
                      var sa = node.getScale3d()
                      graphView.setNear(
                        Math.floor(
                          Math.min(sz[0] * sa[0], sz[1] * sa[1], sz[2] * sa[2])
                        )
                      )
                      graphView.setFar(
                        Math.floor(
                          Math.max(sz[0] * sa[0], sz[1] * sa[1], sz[2] * sa[2])
                        ) * 10
                      )
                      graphView.flyTo(node, true)
                      LoadState.value = true
                    } catch {
                      ElMessage.error(i18n.global.t('message.Objerr'))
                      LoadState.value = false
                    }
                  }
                },
                function error(e) {
                  len--
                  console.log(e)
                }
              )
          } catch {
            ElMessage.error(i18n.global.t('message.Objerr'))
            LoadState.value = false
          }
        })
      })
    }

import { DownGet } from '/@/type/config/down'
const api = {
    getoBJList: '/admin/down/obj', // 获取模型

}
// 下载模型文件
export function getoBJList(param: DownGet): Promise<AxiosResponse<IResponse>> {
    return request({
        url: api.getoBJList,
        method: 'post',
        data: param,
        responseType: 'blob'
    })
}
// 下载请求
export interface DownGet {
    key: string
}
   /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        private FileStreamResult DownloadFile(string Name, string key, string path = null)
        {
            if (string.IsNullOrEmpty(path))
                path = $"{Directory.GetCurrentDirectory()}/wwwroot";

            //Determine the Content Type of the File.
            string contentType = "";
            new FileExtensionContentTypeProvider().TryGetContentType(Name, out contentType);

            //Build the File Path.
            path = Path.Combine(path, Name);

            //Read the File data into FileStream.
            FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);

            //Send the File to Download.
            return new FileStreamResult(fileStream, contentType) { FileDownloadName = key };
        }

        /// <summary>
        /// 下载obj模型
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost("obj"), Authorize]
        
        public IActionResult DownoBJ(XJDataDll.DataHttp.Admin.Down.Select obj)
        {

            try 
            {
                var model = (from v in _dbcontext.company_models.Where(o => obj.key.Equals(o.key)).OrderBy(v => v.index) select v).FirstOrDefault();
                 
                FileStreamResult files = DownloadFile(model.path,model.key); 
                 
                return files;
            }
            catch 
            {
                return NotFound(); 
            }

        }

 

posted on 2022-06-01 14:04  金科许俊  阅读(82)  评论(0编辑  收藏  举报