第一课:编程环境和基础使用

打开在线编辑器https://openjscad.xyz/ 设置语言:中文(代码区域支持中文注释),超时设置为8000

输入下面代码:

// 导入模块
const { cube, sphere } = require('@jscad/modeling').primitives
const { translate } = require('@jscad/modeling').transforms

// 定义一个返回几何体的函数
const main = () => {
  // 创建一个边长为20的立方体
  const myCube = cube({ size: 20 })

  // 创建一个半径为10的球体
  const mySphere = sphere({ radius: 10 })

  // 将球体平移,以避免与立方体重叠
  const translatedSphere = translate([30, 0, 0], mySphere)

  // 返回包含两个几何体的数组
  return [myCube, translatedSphere]
}

// 导出模块
module.exports = { main }

第一行,导入cude(正方形)和sphere(球体),第二行导入translate(平移)。

函数main返回物体列表

module.exports显示main中的物体

第二课:场景物体相减

两个立方体,一个边长20,一个边长10,用大立方体减去小立方体

const { cube } = require('@jscad/modeling').primitives
const { subtract } = require('@jscad/modeling').booleans
const { translate } = require('@jscad/modeling').transforms

const main = () => {
  const bigCube = cube({ size: 20 })
  let smallCube = cube({ size: 10 })

  // 调整translate的参数,可以看到不同效果
  smallCube = translate([5, 5, 5], smallCube)
  const result = subtract(bigCube, smallCube)
  return result
}

module.exports = { main }

第三课:组合复杂形状

//练习1 联合
const { cube } = require('@jscad/modeling').primitives
const { union } = require('@jscad/modeling').booleans
const { translate } = require('@jscad/modeling').transforms;

main = ()=>{
  const shape1 = cube({ size: 10 })
  let shape2 = cube({ size: 5 })
  shape2 = translate( [5,0,0], shape2 )

  const unionedShape   = union(shape1, shape2)
  return unionedShape ;
};
module.exports = {main};

//练习2 差集
const { cube } = require('@jscad/modeling').primitives
const { sphere } = require('@jscad/modeling').primitives
const { subtract } = require('@jscad/modeling').booleans
const { translate } = require('@jscad/modeling').transforms;

main = ()=>{
  const shape1 = cube({ size: 10 })
  let shape2 = sphere({ radius: 5 })
  shape2 = translate( [5,0,0], shape2 )

  const subtractedShape   = subtract(shape1, shape2)
  return subtractedShape ;
};
module.exports = {main};


//练习3 复杂几何体
const { cube } = require('@jscad/modeling').primitives
const { intersect  } = require('@jscad/modeling').booleans
const { translate,rotate } = require('@jscad/modeling').transforms;

main = ()=>{
  const shape1 = cube({ size: 10 })
  let shape2 = cube({ size: 10 })
  shape2 = translate( [5,0,0], shape2 )
  shape2 = rotate([Math.PI / 4, 0, 0], shape2)

  const intersectedShape   = intersect(shape1, shape2)
  return intersectedShape ;
};
module.exports = {main};

第四课 导出STL格式

STL有ASCII和二进制两种格式,通过浏览器上的export按钮可以导出。

Open3D读取导出的STL:

import open3d as o3d

# 读取STL文件
mesh = o3d.io.read_triangle_mesh("example.stl")

# 检查是否成功读取
if mesh.is_empty():
    print("读取STL文件失败")
else:
    print("成功读取STL文件")

# 计算法线
mesh.compute_vertex_normals()

# 显示网格
o3d.visualization.draw_geometries([mesh])

第五课 创建复杂物体并导出STL

创建代码

import open3d as o3d

# 读取STL文件
mesh = o3d.io.read_triangle_mesh("example.stl")

# 检查是否成功读取
if mesh.is_empty():
    print("读取STL文件失败")
else:
    print("成功读取STL文件")

# 计算法线
mesh.compute_vertex_normals()

# 显示网格
o3d.visualization.draw_geometries([mesh])

导出open3D并显示(略)

第六课 镜像

使用mirror函数可以实现镜像操作

const { sphere } = require('@jscad/modeling').primitives;
const { mirror, translate } = require('@jscad/modeling').transforms;

const main = () => {
  // 创建一个球体
  const mySphere = sphere({ radius: 10 });
  const translatedSphere = translate([15, 15, 0], mySphere); // 平移以便更好地查看镜像效果

  // 沿Y轴平面镜像变换
  const mirroredSphere = mirror({ normal: [0, 1, 0] }, translatedSphere);

  return [translatedSphere, mirroredSphere];
};

module.exports = { main };

第七课 带孔的正方体

const { sphere } = require('@jscad/modeling').primitives;
const { mirror, translate } = require('@jscad/modeling').transforms;

const main = () => {
  // 创建一个球体
  const mySphere = sphere({ radius: 10 });
  const translatedSphere = translate([15, 15, 0], mySphere); // 平移以便更好地查看镜像效果

  // 沿Y轴平面镜像变换
  const mirroredSphere = mirror({ normal: [0, 1, 0] }, translatedSphere);

  return [translatedSphere, mirroredSphere];
};

module.exports = { main };

第八课 复杂机械零件

const { cuboid, cylinder } = require('@jscad/modeling').primitives;
const { subtract, union } = require('@jscad/modeling').booleans;
const { translate } = require('@jscad/modeling').transforms;

const createBase = () => {
  return cuboid({ size: [80, 80, 15] });
};

const createRaisedStructure = () => {
  const structure1 = translate([20, 20, 10], cuboid({ size: [20, 20, 20] }));
  const structure2 = translate([-20, -20, 10], cuboid({ size: [20, 20, 20] }));
  return union(structure1, structure2);
};

const createHoles = () => {
  const hole1 = translate([20, 20, 0], cylinder({ height: 65, radius: 5 }));
  const hole2 = translate([-20, -20, 0], cylinder({ height: 65, radius: 5 }));
  return union(hole1, hole2);
};

const main = () => {
  const base = createBase();
  const raisedStructure = createRaisedStructure();
  const holes = createHoles();
  
  const partWithHoles = subtract(union(base, raisedStructure), holes);
  
  return partWithHoles;
};

module.exports = { main };

第九课 由一条二维线条生成三维面

一个圆圈生成球,代码中shape2D是一个半径10的圆,extrudeRotate是绕z旋转生成三维图形。

const { circle } = require('@jscad/modeling').primitives;
const { extrudeRotate } = require('@jscad/modeling').extrusions;

const createRevolvedShape = () => {
  const shape2D = circle({ radius: 10 });
  return extrudeRotate({}, shape2D);
};

const main = () => {
  return createRevolvedShape();
};

module.exports = { main };

生成贝塞尔曲线,代码如下:

const { bezier } = require('@jscad/modeling').curves;
const { path2 } = require('@jscad/modeling').geometries;

const createBezierCurve = () => {
  const controlPoints = [
    [0, 0],
    [10, 15],
    [20, -15],
    [30, 0]
  ];

  const bezierCurve = bezier.create(controlPoints);
  const segments = 16; // 分段数量
  const increment = 1 / segments;
  const points = [];

  for (let i = 0; i <= segments; i++) {
    const t = i * increment;

    // 从bezier曲线上安segments步幅取点
    const point = bezier.valueAt(t, bezierCurve);
    points.push(point);
  }

  return path2.fromPoints({}, points);
};

const main = () => {
  return createBezierCurve();
};

module.exports = { main };

生成贝塞尔曲面

const createBezierSurface = () => {
  const path = createBezierCurve();
  return extrudeFromSlices({
    numberOfSlices: 10,
    callback: (progress, index, base) => {
      return slice.fromPoints(path.points.map(point => [point[0], point[1], index * 2]));
    }
  }, path);
};
分类: 未分类