package com.fdkankan.fusion.common.util; import cn.hutool.core.io.FileUtil; import com.alibaba.fastjson.JSONObject; import com.fdkankan.fusion.common.FilePath; import com.fdkankan.fusion.common.ResultCode; import com.fdkankan.fusion.exception.BusinessException; import lombok.extern.slf4j.Slf4j; import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper; import java.io.*; import java.util.LinkedHashSet; @Slf4j public class OBJToGLBUtil { public static String objToGlb(String objPath, String glbPath) { log.info("obj转换glb开始,{}",objPath); if(!checkObj(objPath)){ throw new BusinessException(-1,"obj文件错误"); } log.info("obj转换glb开始"); String command = "obj2gltf -i " + objPath + " -o " + glbPath; log.info("执行obj转换glb命令路径-{}", command); ShellUtil.execCmd(command); log.info("obj转换glb完毕:" + command); return glbPath; } public static void objToGlb2(String objPath,String glbPath) { log.info("obj转换glb开始,{}",objPath); log.info("obj转换glb开始"); String command = "obj2gltf -i " + objPath + " -o " + glbPath; log.info("执行obj转换glb命令路径-{}", command); ShellUtil.execCmd(command); log.info("obj转换glb完毕:" + command); } public static String objToB3dm(String objPath, String glbPath) { log.info("obj转换glb开始,{}",objPath); if(!checkObj(objPath)){ throw new BusinessException(-1,"obj文件错误"); } log.info("obj转换glb开始"); String command = "obj2gltf -i " + objPath + " -o " + glbPath; log.info("执行obj转换glb命令路径-{}", command); ShellUtil.execCmd(command); log.info("obj转换glb完毕:" + command); return glbPath; } public static void objToB3dm2(String objPath,String glbPath) { log.info("obj转换glb开始,{}",objPath); log.info("obj转换glb开始"); String command = "obj2gltf -i " + objPath + " -o " + glbPath; log.info("执行obj转换glb命令路径-{}", command); ShellUtil.execCmd(command); log.info("obj转换glb完毕:" + command); } public static boolean checkObj(String objPath) { File file = new File(objPath); File file1 = file.getParentFile(); File[] files = file1.listFiles(); if(files == null || files.length <=0){ throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST); } File mtlFile = null; File objFile = null; for (File file2 : files) { if(file2.isDirectory()){ return checkObj(file2.getPath()); } if(file2.getName().contains(".obj") ){ objFile = file2; } if(file2.getName().contains(".mtl") ){ mtlFile = file2; } } if(mtlFile == null || objFile == null ){ throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR); } return checkMtl(file1,mtlFile); } private static boolean checkMtl(File allFile,File file) { if(!file.getName().contains("mtl")){ return false; } LinkedHashSet imgName = new LinkedHashSet<>(); if(allFile == null || allFile.length()<=0 ){ return false; } File[] files = allFile.listFiles(); if(files == null || files.length<=0 ){ return false; } for (File listFile : files) { String modelName = listFile.getName(); imgName.add(modelName); } LinkedHashSet imgMtl = readMtlFile(file.getPath()); for (String mtlName : imgMtl) { if(!imgName.contains(mtlName)){ throw new BusinessException(-1,mtlName +".jpg 图片缺失!"); } } return true; } public static LinkedHashSet readMtlFile(String mtlPath) { LinkedHashSet imgName = new LinkedHashSet<>(); FileInputStream fis = null; BufferedReader br = null; try { fis = new FileInputStream(new File(mtlPath)); br = new BufferedReader(new InputStreamReader(fis)); String line = null; while ((line = br.readLine()) != null) { String[] tempsa = line.split("[ ]+"); if (tempsa[0].trim().equals("map_Ka")) { String mtlName = tempsa[1]; if(mtlName.contains("/")){ String[] split = mtlName.split("/"); mtlName = split[split.length-1]; } imgName.add(mtlName); } if (tempsa[0].trim().equals("map_Kd")) { String mtlName = tempsa[1]; if(mtlName.contains("/")){ String[] split = mtlName.split("/"); mtlName = split[split.length-1]; } imgName.add(mtlName); } } } catch (Exception e) { e.printStackTrace(); } return imgName; } public static File lasOrPlyToBin(File srcFile) throws IOException { String mntPath = srcFile.getPath().replace(FilePath.LOCAL_BASE_PATH,FilePath.MNT_BASE_PATH); String mntPathEmt = srcFile.getParent().replace(FilePath.LOCAL_BASE_PATH,FilePath.MNT_BASE_PATH)+"/res"; File file = new File(mntPathEmt); if(!file.exists()){ file.mkdirs(); } FileUtil.copy(srcFile.getPath(),mntPath,true); String cmd = ShellUtil.LAS_TO_BIN; cmd = cmd.replace("@inPath",mntPath); cmd = cmd.replace("@outPath",mntPathEmt); ShellUtil.execCmd(cmd); log.info("lasOrPlyToBin---------cmd-over"); String cloudJs = mntPathEmt +"/webcloud/"+ "cloud.js"; JSONObject jsonObject = ShellUtil.fixCloud(cloudJs); FileWriterUtil.writerJson(mntPathEmt +"/webcloud/","cloud.js",jsonObject.toJSONString()); return file; } public static String OsgbToB3dm(File objPathFile) { String targetPath = FilePath.MNT_BASE_PATH +"osgb" +"/" + objPathFile.getName(); String sourcePath = FilePath.MNT_BASE_PATH +"b3dm" +"/" + objPathFile.getName(); //ShellUtil.execCmd("cp -r " + objPath + " " + dockerPath+ objPathFile.getName() ); File file = new File(targetPath); FileUtil.copyContent(objPathFile,file,true); String cmd = ShellCmd.osgbTob3dmCmd.replaceAll("@path",FilePath.MNT_BASE_PATH); cmd =cmd.replaceAll("@inputFile",targetPath); cmd =cmd.replaceAll("@outputFile",sourcePath); ShellUtil.execCmd(cmd); return sourcePath; } }