huangxingxing 5 months ago
parent
commit
d1e6045280

+ 52 - 0
src/main/java/com/miniframe/mdo/controller/GeoInfoExtractController.java

@@ -0,0 +1,52 @@
+package com.miniframe.mdo.controller;
+
+
+import com.miniframe.core.ext.UtilTools;
+import com.miniframe.mdo.service.FileService;
+import com.miniframe.mdo.service.factory.FileProcessorFactory;
+import com.miniframe.mdo.service.processor.FileProcessor;
+import com.miniframe.mdo.utils.FileUtils;
+import com.miniframe.model.mdo.MdoProFfd;
+import com.miniframe.model.mdo.dao.MdoProFfdMapper;
+import com.miniframe.model.system.SysFile;
+import com.miniframe.model.system.dao.SysFileMapper;
+import com.miniframe.tools.XIFileUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.util.ResourceUtils;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.io.File;
+import java.util.HashMap;
+import java.util.Map;
+
+@RestController
+public class GeoInfoExtractController {
+
+    @Autowired
+    private FileProcessorFactory fileProcessorFactory;
+
+    @RequestMapping("/getXyz/{fid}")
+    public Map<String, Object> getXyz(@PathVariable("fid") String fid) throws Exception {
+        Map<String, Object> result = new HashMap<>();
+        SysFileMapper sysFileDAO = UtilTools.getBean(SysFileMapper.class);
+        SysFile sysFile = sysFileDAO.selectByPrimaryKey(fid);
+        if(sysFile!=null){
+            String filepath=XIFileUtils.getRootPathStr()+"/"+sysFile.getFilepath();
+            File file = new File(filepath);
+            // 获取文件后缀
+            String fileExtension = FileUtils.getFileExtension(file.getName());
+            // 根据后缀获取对应的处理器
+            FileProcessor processor = fileProcessorFactory.getProcessor(fileExtension);
+            if (processor == null) {
+                throw new UnsupportedOperationException("Unsupported file type: " + fileExtension);
+            }
+            Map<String, Object> content = processor.processFile(file.getAbsolutePath());
+            // 将文件内容和数据集类型放入结果中
+            result.put("data", content);
+        }
+        return result;
+    }
+}

+ 9 - 0
src/main/java/com/miniframe/mdo/entity/BdfConm2.java

@@ -0,0 +1,9 @@
+package com.miniframe.mdo.entity;
+import lombok.Data;
+
+@Data
+public class BdfConm2 {
+    private int id;
+    private int nodeId;
+    private double mass;
+}

+ 11 - 0
src/main/java/com/miniframe/mdo/entity/BdfCquad4.java

@@ -0,0 +1,11 @@
+package com.miniframe.mdo.entity;
+import lombok.Data;
+
+import java.util.List;
+
+@Data
+public class BdfCquad4 {
+    private int id;
+    private int materialId;
+    private List<Integer> nodeIds;
+}

+ 11 - 0
src/main/java/com/miniframe/mdo/entity/BdfCquadr.java

@@ -0,0 +1,11 @@
+package com.miniframe.mdo.entity;
+import lombok.Data;
+
+import java.util.List;
+
+@Data
+public class BdfCquadr {
+    private int id;
+    private int propertyId;
+    private List<Integer> nodeIds;
+}

+ 10 - 0
src/main/java/com/miniframe/mdo/entity/BdfGrid.java

@@ -0,0 +1,10 @@
+package com.miniframe.mdo.entity;
+import lombok.Data;
+
+@Data
+public class BdfGrid {
+    private int id;
+    private double x;
+    private double y;
+    private double z;
+}

+ 10 - 0
src/main/java/com/miniframe/mdo/entity/BdfMaterial.java

@@ -0,0 +1,10 @@
+package com.miniframe.mdo.entity;
+import lombok.Data;
+
+@Data
+public class BdfMaterial {
+    private int id;
+    private double elasticModulus;
+    private double poissonRatio;
+    private double density;
+}

+ 12 - 0
src/main/java/com/miniframe/mdo/entity/BdfRbe3.java

@@ -0,0 +1,12 @@
+package com.miniframe.mdo.entity;
+import lombok.Data;
+
+import java.util.List;
+
+@Data
+public class BdfRbe3 {
+    private int id;
+    private int masterNodeId;
+    private String degreesOfFreedom;
+    private List<Integer> slaveNodeIds;
+}

+ 31 - 0
src/main/java/com/miniframe/mdo/entity/XyzPoint.java

@@ -0,0 +1,31 @@
+package com.miniframe.mdo.entity;
+
+public class XyzPoint {
+    private final double x;
+    private final double y;
+    private final double z;
+
+    public XyzPoint(double x, double y, double z) {
+        this.x = x;
+        this.y = y;
+        this.z = z;
+    }
+
+    // 添加getter方法
+    public double getX() {
+        return x;
+    }
+
+    public double getY() {
+        return y;
+    }
+
+    public double getZ() {
+        return z;
+    }
+
+    @Override
+    public String toString() {
+        return String.format("(%.8f, %.8f, %.8f)", x, y, z);
+    }
+}

+ 36 - 0
src/main/java/com/miniframe/mdo/service/FileService.java

@@ -0,0 +1,36 @@
+package com.miniframe.mdo.service;
+
+
+import com.miniframe.mdo.service.factory.FileProcessorFactory;
+import com.miniframe.mdo.service.processor.FileProcessor;
+import com.miniframe.mdo.utils.FileUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.util.ResourceUtils;
+
+import java.io.File;
+import java.util.Map;
+
+@Service
+public class FileService {
+
+    @Autowired
+    private FileProcessorFactory fileProcessorFactory;
+
+    public Map<String, Object> readFileContent(String fileName) throws Exception {
+        // 获取文件路径
+        File file = ResourceUtils.getFile("classpath:files/" + fileName);
+
+        // 获取文件后缀
+        String fileExtension = FileUtils.getFileExtension(fileName);
+
+        // 根据后缀获取对应的处理器
+        FileProcessor processor = fileProcessorFactory.getProcessor(fileExtension);
+        if (processor == null) {
+            throw new UnsupportedOperationException("Unsupported file type: " + fileExtension);
+        }
+        // 处理文件
+        return processor.processFile(file.getAbsolutePath());
+    }
+
+}

+ 33 - 0
src/main/java/com/miniframe/mdo/service/factory/FileProcessorFactory.java

@@ -0,0 +1,33 @@
+package com.miniframe.mdo.service.factory;
+
+
+import com.miniframe.mdo.service.processor.FileProcessor;
+import com.miniframe.mdo.service.processor.impl.BdfFileProcessor;
+import com.miniframe.mdo.service.processor.impl.CgnsFileProcessor;
+import com.miniframe.mdo.service.processor.impl.PltFileProcessor;
+import com.miniframe.mdo.service.processor.impl.XyzFileProcessor;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@Service
+public class FileProcessorFactory {
+
+    private final Map<String, FileProcessor> processorMap = new HashMap<>();
+
+    @Autowired
+    public FileProcessorFactory(XyzFileProcessor xyzFileProcessor, PltFileProcessor pltFileProcessor,
+                                BdfFileProcessor bdfFileProcessor, CgnsFileProcessor cgnsFileProcessor) {
+        // 注册处理器
+        processorMap.put("xyz", xyzFileProcessor);
+//        processorMap.put("plt", pltFileProcessor);
+        processorMap.put("bdf", bdfFileProcessor);
+//        processorMap.put("cgns", cgnsFileProcessor);
+    }
+
+    public FileProcessor getProcessor(String fileExtension) {
+        return processorMap.get(fileExtension);
+    }
+}

+ 8 - 0
src/main/java/com/miniframe/mdo/service/processor/FileProcessor.java

@@ -0,0 +1,8 @@
+package com.miniframe.mdo.service.processor;
+
+
+import java.util.Map;
+
+public interface FileProcessor {
+    Map<String, Object> processFile(String filePath) throws Exception;
+}

+ 155 - 0
src/main/java/com/miniframe/mdo/service/processor/impl/BdfFileProcessor.java

@@ -0,0 +1,155 @@
+package com.miniframe.mdo.service.processor.impl;
+
+import com.miniframe.mdo.entity.*;
+import com.miniframe.mdo.service.processor.FileProcessor;
+import org.springframework.stereotype.Service;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.util.*;
+
+@Service
+public class BdfFileProcessor implements FileProcessor {
+    @Override
+    public Map<String, Object> processFile(String filePath) throws Exception {
+        Map<String, Object> result = new HashMap<>();
+        List<BdfGrid> grids = new ArrayList<>();
+        List<BdfConm2> conm2s = new ArrayList<>();
+        List<BdfRbe3> rbe3s = new ArrayList<>();
+        List<BdfCquad4> cquad4s = new ArrayList<>();
+        List<BdfCquadr> cquadrs = new ArrayList<>();
+        List<BdfMaterial> materials = new ArrayList<>();
+
+        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
+            String line;
+            while ((line = reader.readLine()) != null) {
+                line = line.trim();
+                if (line.startsWith("GRID")) {
+                    // 读取下一行
+                    String nextLine = reader.readLine();
+                    if (nextLine != null) {
+                        grids.add(parseGrid(line, nextLine));
+                    }
+                } else if (line.startsWith("CONM2")) {
+                    conm2s.add(parseConm2(line));
+                } else if (line.startsWith("RBE3")) {
+                    rbe3s.add(parseRbe3(line));
+                } else if (line.startsWith("CQUAD4")) {
+                    cquad4s.add(parseCquad4(line));
+                } else if (line.startsWith("CQUADR")) {
+                    cquadrs.add(parseCquadr(line));
+                } else if (line.startsWith("MAT1")) {
+                    materials.add(parseMaterial(line));
+                }
+            }
+        }
+
+        result.put("grids", grids);
+        result.put("conm2s", conm2s);
+        result.put("rbe3s", rbe3s);
+        result.put("cquad4s", cquad4s);
+        result.put("cquadrs", cquadrs);
+        result.put("materials", materials);
+        result.put("datasetType", "bdf");
+
+        return result;
+    }
+
+    private static BdfGrid parseGrid(String line1, String line2) {
+        // 解析第一行
+        String[] part1 = line1.split("\\s+");
+
+        // 检查part1数组的长度
+        if (part1.length < 6) {
+            throw new IllegalArgumentException("Invalid GRID format: " + line1);
+        }
+
+        int id = Integer.parseInt(part1[1]); // 第2个字段是ID
+
+        double x = Double.parseDouble(part1[3]); // 第4个字段是X坐标
+
+        // 解析Y坐标,去掉末尾的*字符
+        String yStr = part1[4];
+        if (yStr.endsWith("*")) {
+            yStr = yStr.substring(0, yStr.length() - 1); // 去掉最后一个字符
+        }
+        double y = Double.parseDouble(yStr); // 第5个字段是Y坐标
+
+        // 解析第二行
+        String[] part2 = line2.split("\\s+");
+
+        // 检查part2数组的长度
+        if (part2.length < 3) {
+            throw new IllegalArgumentException("Invalid GRID continuation format: " + line2);
+        }
+
+        double z = Double.parseDouble(part2[2]); // 第3个字段是Z坐标
+
+        // 创建Grid对象
+        BdfGrid grid = new BdfGrid();
+        grid.setId(id);
+        grid.setX(x);
+        grid.setY(y);
+        grid.setZ(z);
+        return grid;
+    }
+
+    private static BdfConm2 parseConm2(String line) {
+        String[] parts = line.split("\\s+");
+        BdfConm2 conm2 = new BdfConm2();
+        conm2.setId(Integer.parseInt(parts[1]));
+        conm2.setNodeId(Integer.parseInt(parts[2]));
+        conm2.setMass(Double.parseDouble(parts[4]));
+        return conm2;
+    }
+
+    private static BdfRbe3 parseRbe3(String line) {
+        String[] parts = line.split("\\s+");
+        BdfRbe3 rbe3 = new BdfRbe3();
+        rbe3.setId(Integer.parseInt(parts[1]));
+        rbe3.setMasterNodeId(Integer.parseInt(parts[2]));
+        rbe3.setDegreesOfFreedom(parts[3]);
+        List<Integer> slaveNodeIds = new ArrayList<>();
+        for (int i = 4; i < parts.length; i++) {
+            slaveNodeIds.add(Integer.parseInt(parts[i]));
+        }
+        rbe3.setSlaveNodeIds(slaveNodeIds);
+        return rbe3;
+    }
+
+    private static BdfCquad4 parseCquad4(String line) {
+        String[] parts = line.split("\\s+");
+        BdfCquad4 cquad4 = new BdfCquad4();
+        cquad4.setId(Integer.parseInt(parts[1]));
+        cquad4.setMaterialId(Integer.parseInt(parts[2]));
+        List<Integer> nodeIds = new ArrayList<>();
+        for (int i = 3; i < parts.length; i++) {
+            nodeIds.add(Integer.parseInt(parts[i]));
+        }
+        cquad4.setNodeIds(nodeIds);
+        return cquad4;
+    }
+
+    private static BdfCquadr parseCquadr(String line) {
+        String[] parts = line.split("\\s+");
+        BdfCquadr cquadr = new BdfCquadr();
+        cquadr.setId(Integer.parseInt(parts[1]));
+        cquadr.setPropertyId(Integer.parseInt(parts[2]));
+        List<Integer> nodeIds = new ArrayList<>();
+        for (int i = 3; i < parts.length; i++) {
+            nodeIds.add(Integer.parseInt(parts[i]));
+        }
+        cquadr.setNodeIds(nodeIds);
+        return cquadr;
+    }
+
+    private static BdfMaterial parseMaterial(String line) {
+        String[] parts = line.split("\\s+");
+        BdfMaterial material = new BdfMaterial();
+        material.setId(Integer.parseInt(parts[1]));
+        material.setElasticModulus(Double.parseDouble(parts[2]));
+        material.setPoissonRatio(Double.parseDouble(parts[3]));
+        material.setDensity(Double.parseDouble(parts[4]));
+        return material;
+    }
+}

+ 15 - 0
src/main/java/com/miniframe/mdo/service/processor/impl/CgnsFileProcessor.java

@@ -0,0 +1,15 @@
+package com.miniframe.mdo.service.processor.impl;
+
+import org.springframework.stereotype.Service;
+
+import java.nio.file.Files;
+import java.nio.file.Paths;
+
+@Service
+public class CgnsFileProcessor {
+//    @Override
+//    public String processFile(String filePath) throws Exception {
+//        // 读取文本文件内容
+//        return new String(Files.readAllBytes(Paths.get(filePath)));
+//    }
+}

+ 18 - 0
src/main/java/com/miniframe/mdo/service/processor/impl/PltFileProcessor.java

@@ -0,0 +1,18 @@
+package com.miniframe.mdo.service.processor.impl;
+
+import org.springframework.stereotype.Service;
+
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Map;
+
+@Service
+public class PltFileProcessor {
+//    @Override
+//    public Map<Integer, XyzPoint> processFile(String filePath) throws Exception {
+//        // 读取文本文件内容
+//        Map<Integer, XyzPoint> Map;
+////        return  Files.readAllBytes(Paths.get(filePath));
+//        return Map;
+//    }
+}

+ 79 - 0
src/main/java/com/miniframe/mdo/service/processor/impl/XyzFileProcessor.java

@@ -0,0 +1,79 @@
+package com.miniframe.mdo.service.processor.impl;
+
+import com.miniframe.mdo.service.processor.FileProcessor;
+import org.springframework.stereotype.Service;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@Service
+public class XyzFileProcessor implements FileProcessor {
+    @Override
+    public Map<String, Object> processFile(String filePath) throws Exception {
+        Map<String, Object> result = new HashMap<>();
+        List<Double> points = new ArrayList<>();
+        int u = 0, v = 0, w = 0;
+
+        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
+            String line;
+            int lineNumber = 0;
+            int totalPoints = 0;
+            int pointIndex = 0;
+
+            // 读取文件内容
+            while ((line = reader.readLine()) != null) {
+                lineNumber++;
+                if (lineNumber == 2) {
+                    // 解析第二行,获取 u, v, w
+                    String[] counts = line.trim().split("\\s+");
+                    if (counts.length < 3) {
+                        throw new IllegalArgumentException("Invalid format in line 2: " + line);
+                    }
+                    u = Integer.parseInt(counts[0]);
+                    v = Integer.parseInt(counts[1]);
+                    w = Integer.parseInt(counts[2]);
+                    totalPoints = u * v * w;
+                } else if (lineNumber >= 3) {
+                    // 从第三行开始解析坐标数据
+                    String[] values = line.trim().split("\\s+");
+                    for (String value : values) {
+                        double coordinate = Double.parseDouble(value);
+                        points.add(coordinate);
+                        pointIndex++;
+                    }
+                }
+            }
+
+            // 检查数据是否完整
+            if (pointIndex != 3 * totalPoints) {
+                throw new IllegalArgumentException("Incomplete data: expected " + (3 * totalPoints) +
+                        " coordinates, but got " + pointIndex);
+            }
+
+            // 按照 x1, y1, z1, x2, y2, z2, ... 的顺序组装点
+            List<Double> flattenedPoints = new ArrayList<>();
+            for (int i = 0; i < totalPoints; i++) {
+                flattenedPoints.add(points.get(i)); // x
+                flattenedPoints.add(points.get(totalPoints + i)); // y
+                flattenedPoints.add(points.get(2 * totalPoints + i)); // z
+            }
+
+            // 组装结果
+            result.put("uvw", new int[]{u, v, w});
+            result.put("points", flattenedPoints);
+            result.put("datasetType", "xyz");
+
+        } catch (IOException e) {
+            throw new IOException("Failed to read file: " + filePath, e);
+        } catch (NumberFormatException e) {
+            throw new NumberFormatException("Failed to parse a number: " + e.getMessage());
+        }
+
+        return result;
+    }
+}

+ 18 - 0
src/main/java/com/miniframe/mdo/utils/FileUtils.java

@@ -0,0 +1,18 @@
+package com.miniframe.mdo.utils;
+
+/**
+ * 提取后缀,文件格式
+ */
+public class FileUtils {
+
+    public static String getFileExtension(String fileName) {
+        if (fileName == null || fileName.isEmpty()) {
+            throw new IllegalArgumentException("File name cannot be null or empty");
+        }
+        int lastDotIndex = fileName.lastIndexOf('.');
+        if (lastDotIndex == -1) {
+            throw new IllegalArgumentException("File has no extension: " + fileName);
+        }
+        return fileName.substring(lastDotIndex + 1).toLowerCase();
+    }
+}

+ 36 - 0
src/main/java/com/miniframe/mdo/utils/testMain.java

@@ -0,0 +1,36 @@
+package com.miniframe.mdo.utils;
+
+import ncsa.hdf.object.FileFormat;
+import ncsa.hdf.object.h5.H5Datatype;
+import ncsa.hdf.object.h5.H5File;
+import ncsa.hdf.object.h5.H5ScalarDS;
+
+public class testMain {
+    public static void main(String[] args) {
+        String fileName = "data.h5";
+        H5File file = null;
+
+        try {
+            // 创建HDF5文件
+            file = new H5File(fileName, FileFormat.CREATE);
+            file.open();
+
+            // 创建数据集
+            long[] dims = {5};  // 一维数组
+            int[] data = {1, 2, 3, 4, 5}; // 示例数据
+            H5ScalarDS dataset = (H5ScalarDS) file.createScalarDS(
+                    "/dataset1",     // 数据集名称
+                    null,            // 父组
+                    new H5Datatype(H5Datatype.CLASS_INTEGER, 4, H5Datatype.ORDER_LE, -1),
+                    dims, null, null, 0, data
+            );
+
+            // 关闭文件
+            file.close();
+            System.out.println("HDF5 文件写入成功!");
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+    }
+}