JCSG差异产生空STL。这是我的错误还是错误?

问题描述 投票:1回答:1

我正在使用带有以下代码的JCSG来制作带有两个多边形(一个大和一个小)的孔的三角形。我希望它看起来像附加的图像,但结果是一个空的STL。 enter image description here

    List<Vector3d> t1 = new ArrayList<>();
    t1.add(Vector3d.xyz(0, 0, 0));
    t1.add(Vector3d.xyz(100, 0, 0));
    t1.add(Vector3d.xyz(0, 100, 0));

    List<Polygon> poly1 = Polygon.fromConcavePoints(t1);
    CSG csg1 = CSG.fromPolygons(poly1);

    List<Vector3d> t2 = new ArrayList<>();
    t2.add(Vector3d.xyz(10, 10, 0));
    t2.add(Vector3d.xyz(80, 10, 0));
    t2.add(Vector3d.xyz(10, 80, 0));

    List<Polygon> poly2 = Polygon.fromConcavePoints(t2);
    CSG csg2 = CSG.fromPolygons(poly2);

    CSG csg = csg1.difference(csg2);
    String stl = csg.toStlString();
    System.out.println(stl);

结果是: solid v3d.csg endsolid v3d.csg

java 3d cad
1个回答
0
投票

JCSG仅适用于封闭表面,即它不适用于没有体积的表面。但是有一个技巧可以用来做到这一点:

1. extrude your polygons by 1
2. perform the difference operation
3. select all polygons that are in the XY-plane and save them as STL

下面是一个VRL-Studio示例(屏幕截图和代码),演示了如何在JCSG中完成差异操作和XY平面选择。代码是Groovy但它接近Java。所有功能都适用于没有VRL-Studio的普通JCSG。

JCSG Difference Operation

这是源代码:

DiffSurface:

import eu.mihosoft.jcsg.Extrude;
import java.util.List;
import eu.mihosoft.jcsg.CSG;
import eu.mihosoft.jcsg.Polygon;
import eu.mihosoft.vvecmath.Transform;
import eu.mihosoft.vvecmath.Vector3d as Vec3d;
import eu.mihosoft.vvecmath.Vectors3d as Vecs;

@ComponentInfo(name="DiffSurface", category="Custom")
@groovy.transform.CompileStatic
public class DiffSurface implements java.io.Serializable {
  private static final long serialVersionUID=1L;

  // *** DOES NOT WORK (SURFACE IS NOT CLOSED) ***
  public CSG diff1() { 

    List<Vec3d> t1 = new ArrayList<>();
    t1.add(Vec3d.xyz(0, 0, 0));
    t1.add(Vec3d.xyz(100, 0, 0));
    t1.add(Vec3d.xyz(0, 100, 0));

    List<Polygon> poly1 = Polygon.fromConcavePoints(t1);
    CSG csg1 = CSG.fromPolygons(poly1);

    List<Vec3d> t2 = new ArrayList<>();
    t2.add(Vec3d.xyz(10, 10, 0));
    t2.add(Vec3d.xyz(80, 10, 0));
    t2.add(Vec3d.xyz(10, 80, 0));

    List<Polygon> poly2 = Polygon.fromConcavePoints(t2);
    CSG csg2 = CSG.fromPolygons(poly2);

    CSG csg = csg1.difference(csg2);

    return csg

  }

  // *** WORKS (SURFACE CLOSED) ***
  public CSG diff2(double height) { 

    List<Vec3d> t1 = new ArrayList<>();
    t1.add(Vec3d.xyz(0, 0, 0));
    t1.add(Vec3d.xyz(100, 0, 0));
    t1.add(Vec3d.xyz(0, 100, 0));

    // EXTRUDE Triangle 1
    CSG csg1 = Extrude.points(Vec3d.z(height), t1)

    List<Vec3d> t2 = new ArrayList<>();
    t2.add(Vec3d.xyz(10, 10, 0));
    t2.add(Vec3d.xyz(80, 10, 0));
    t2.add(Vec3d.xyz(10, 80, 0));

    // EXTRUDE Triangle 2
    CSG csg2 = Extrude.points(Vec3d.z(height), t2)

    CSG csg = csg1.difference(csg2);

    return csg

  }
}

ReduceToXYPlane:

import eu.mihosoft.jcsg.Extrude;
import java.util.List;
import eu.mihosoft.jcsg.CSG;
import eu.mihosoft.jcsg.Polygon;
import eu.mihosoft.vvecmath.Transform;
import eu.mihosoft.vvecmath.Vector3d as Vec3d;
import eu.mihosoft.vvecmath.Vectors3d as Vecs;

@ComponentInfo(name="Reduce", category="Custom")
public class Reduce implements java.io.Serializable {
  private static final long serialVersionUID=1L;

  // add your code here

 public CSG reduce(CSG csg) {

  List<Polygon> result = new ArrayList<>();

  // for each polygon in the CSG object
  for (Polygon poly : csg.getPolygons()) {

    // store all vertices in the X,Y plane
    // in a vertex list
    List<Vec3d> vertices = new ArrayList<>();
    poly.vertices.each{v -> 
      if (Math.abs(v.pos.z) < 1e-6) {
        vertices.add(v.pos)
      }
    }

    // if more than 2 vertices are in the list
    // create a polygon and add it to the 
    // polygon list
    if(vertices.size()>2) {
      Polygon p = Polygon.fromPoints(vertices);
      result.add(p)
    }
  }

  // create a CSG from polygons that can be exported
  // as STL
  //
  // NOTE: performing CSG operations on this CSG
  //       will result in invalid computations.
  //       Only use non-closed CSG for converting to STL files
  return CSG.fromPolygons(result);
 }

}

© www.soinside.com 2019 - 2024. All rights reserved.