Skip to content
On this page

mxcad3d具备绘制常见的基本曲线的能力,以下是绘制基本曲线的示例代码。

直线

typescript
// 非参数几何点
const pt = new Mx3dGePoint(950, 0, 0);
// 方向向量
const dir = new Mx3dGeDir(0, 1, 1);
// 参数几何直线,由点和方向构造
const line = new Mx3dGeomLine(pt, dir);
// 取[0, 50]参数区间的直线段
const lineEdge = line.Edge(0, 50);
// 形状添加至文档
addShapeToDoc(lineEdge);
// 更新显示(会更新canvas中显示的模型)
mxcad3d.update();

圆、圆弧

typescript
// 默认的右手坐标系
const csysr = new Mx3dGeCSYSR();
// 参数几何圆,中心与csysr的原点重合,位于csysr的XOY平面,半径20
const circle = new Mx3dGeomCircle(csysr, 20);
// 获取圆的形状,Wire是拓扑形状的一种
const circleWire = circle.Wire();
// 修改圆的半径
circle.SetRadius(30);
// 获取圆弧的形状,Edge方法的参数是圆弧角的范围[0, 2π]
const circleEdge = circle.Wire(0, Math.PI / 2);
// 形状添加至文档
addShapeToDoc(circleWire);
addShapeToDoc(circleEdge);
// 更新显示(会更新canvas中显示的模型)
mxcad3d.update();

三点圆弧

typescript
// 三个不共线的点
const pt1 = new Mx3dGePoint(0, 0, 0);
const pt2 = new Mx3dGePoint(1, 0.5, 0);
const pt3 = new Mx3dGePoint(2, 0, 0);
// 通过Mx3dGeomCircle类的静态方法MakeArcOfCircle进行构造三点圆弧
const arcEdge = Mx3dGeomCircle.MakeArcOfCircle(pt1, pt2, pt3);
// 形状添加至文档
addShapeToDoc(arcEdge);
// 更新显示(会更新canvas中显示的模型)
mxcad3d.update();

椭圆、椭圆弧

typescript
// 默认的右后坐标系
const csysr = new Mx3dGeCSYSR();
// 参数几何椭圆,中心与csysr的原点重合,位于csysr的XOY平面,主轴半径20,副轴半径10
const ellipse = new Mx3dGeomEllipse(csysr, 20, 10);
// 获取椭圆的形状
const ellipseWire = ellipse.Wire();
// 修改椭圆的主轴半径和副轴半径
ellipse.SetMajorRadius(30);
ellipse.SetMinorRadius(15);
// 获取椭圆弧
const ellipseEdge = ellipse.Edge(0, Math.PI);
// 形状添加至文档
addShapeToDoc(ellipseWire);
addShapeToDoc(ellipseEdge);
// 更新显示(会更新canvas中显示的模型)
mxcad3d.update();

双曲线

typescript
// 默认的右手坐标系
const csysr = new Mx3dGeCSYSR();
// 参数几何双曲线,位于csysr的XOY平面,主轴半径20,副轴半径5
const hyperbola = new Mx3dGeomHyperbola(csysr, 20, 5);
// 获取双曲线的形状,参数是双曲线的离心角的范围
const hyperbolaEdge = hyperbola.Edge(-Math.PI / 2, Math.PI / 2);
// 形状添加至文档
addShapeToDoc(hyperbolaEdge);
// 更新显示(会更新canvas中显示的模型)
mxcad3d.update();

抛物线

typescript
// 默认的右手坐标系
const csysr = new Mx3dGeCSYSR();
// 参数几何抛物线,位于csysr的XOY平面,主轴半径20,副轴半径9
const parabola = new Mx3dGeomHyperbola(csysr, 20, 9);
// 获取抛物线的形状
const parabolaEdge = parabola.Edge(-Math.PI / 2, Math.PI / 2);
// 形状添加至文档
addShapeToDoc(parabolaEdge);
// 更新显示(会更新canvas中显示的模型)
mxcad3d.update();

B样条曲线

typescript
// 控制点
const Poles: Mx3dGePoint[] = [];
Poles.push(new Mx3dGePoint(0, -10, 0));
Poles.push(new Mx3dGePoint(0, -10, 5));
Poles.push(new Mx3dGePoint(0, -5, 5));
Poles.push(new Mx3dGePoint(0, 0, 5));
Poles.push(new Mx3dGePoint(0, 0, 0));
Poles.push(new Mx3dGePoint(0, 0, -5));
Poles.push(new Mx3dGePoint(0, 5, -5));
Poles.push(new Mx3dGePoint(0, 10, -5));
Poles.push(new Mx3dGePoint(0, 10, 0));
// 节点
const Knots = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
// 重复度
const Multiplicities = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
// 2次B样条曲线
const bspl = new Mx3dGeomBSplineCurve(Poles, Knots, Multiplicities, 2);
// 获取样条曲线的形状
const bsplWire = bspl.Wire();
// 形状添加至文档
addShapeToDoc(bsplWire);
// 更新显示(会更新canvas中显示的模型)
mxcad3d.update();

B样条曲线(插值算法)

typescript
// 控制点(插值算法会尽量使得曲线经过这些点)
const pts:Mx3dGePoint[] = [];
pts.push(new Mx3dGePoint(0, 0, 0));
pts.push(new Mx3dGePoint(0, 0, 20));
pts.push(new Mx3dGePoint(0, 0, 40));
pts.push(new Mx3dGePoint(0, 0, 60));
pts.push(new Mx3dGePoint(0, 0, 80));
pts.push(new Mx3dGePoint(20, 0, 100));
pts.push(new Mx3dGePoint(40, 0, 100));
pts.push(new Mx3dGePoint(60, 0, 100));
pts.push(new Mx3dGePoint(80, 0, 100));
pts.push(new Mx3dGePoint(100, 0, 100));
// false非周期性,精度设置为1e-6
const interpolateBSPL = new Mx3dInterpolateBSplineCurve(pts, false, 1e-6);
// 可以设置起点终点的切向向量
const sVec = new Mx3dGeVec(-10, 0, 1);
const eVec = new Mx3dGeVec(1, 0, -1);
interpolateBSPL.LoadEnds(sVec, eVec);
// 执行算法
interpolateBSPL.Perform();
// 算法执行成功后获取B样条曲线
if (interpolateBSPL.IsDone()) {
  const bspl = interpolateBSPL.Curve();
  // 获取样条曲线的形状
  const bsplWire = bspl.Wire();
  // 形状添加至文档
  addShapeToDoc(bsplWire);
  // 更新显示(会更新canvas中显示的模型)
  mxcad3d.update();
}

贝塞尔曲线

typescript
// 控制点
const Poles: Mx3dGePoint[] = [];
Poles.push(new Mx3dGePoint(0, 0, 0));
Poles.push(new Mx3dGePoint(0, 0, 10));
Poles.push(new Mx3dGePoint(0, 0, 20));
Poles.push(new Mx3dGePoint(0, 0, 30));
Poles.push(new Mx3dGePoint(0, 0, 40));
Poles.push(new Mx3dGePoint(10, 0, 50));
Poles.push(new Mx3dGePoint(20, 0, 50));
Poles.push(new Mx3dGePoint(30, 0, 50));
Poles.push(new Mx3dGePoint(40, 0, 50));
Poles.push(new Mx3dGePoint(50, 0, 50));
// 贝塞尔曲线,通过控制点和点权重(可选参数)初始化
const bezier = new Mx3dGeomBezierCurve(Poles);
// 获取贝塞尔曲线的形状
const bezierWire = bezier.Wire();
// 形状添加至文档
addShapeToDoc(bezierWire);
// 更新显示(会更新canvas中显示的模型)
mxcad3d.update();

多段线

typescript
// 使用多段线创建一个矩形
const polygon = new Mx3dMkPolygon();
const pt = new Mx3dGePoint(0, 0, 0);
polygon.Add(pt); pt.setX(50);
polygon.Add(pt); pt.setY(30);
polygon.Add(pt); pt.setX(0);
polygon.Add(pt);
// 闭合
polygon.Close();
// 获取多段线的形状
const polygonWire = polygon.Wire();
// 形状添加至文档
addShapeToDoc(polygonWire);
// 更新显示(会更新canvas中显示的模型)
mxcad3d.update();

将Edge/Wire连接成Wire

typescript
// 用于构建Edge的点
const pt1 = new Mx3dGePoint(0, 25, 0);
const pt2 = new Mx3dGePoint(40, 30, 0);
const pt3 = new Mx3dGePoint(80, 25, 0);
const pt4 = new Mx3dGePoint(0, 5, 0);
const pt5 = new Mx3dGePoint(40, 0, 0);
const pt6 = new Mx3dGePoint(80, 5, 0);
// 以下Edge用于构建Wire
const arc1 = Mx3dGeomCircle.MakeArcOfCircle(pt1, pt2, pt3);
const arc2 = Mx3dGeomCircle.MakeArcOfCircle(pt4, pt5, pt6);
const edge1 = new Mx3dShapeEdge(pt1, pt4);
const edge2 = new Mx3dShapeEdge(pt3, pt6);
// ConnectEdgesToWires算法是Mx3dWireTool类的一个静态方法
// ConnectEdgesToWires算法传入一组Edge,算法会按照给定的精度尽可能的连接能连接的Edge。
// 如果传入的这些Edge不能全部都连接到一起,则可能会返回多个Wire
const wires = Mx3dWireTool.ConnectEdgesToWires([arc1, arc2, edge1, edge2], 1e-5, false);
// 返回的第一个Wire形状添加至文档
addShapeToDoc(wires[0]);
// 更新显示(会更新canvas中显示的模型)
mxcad3d.update();

Mx3dWireTool 类中还有ConnectWiresToWires算法用于将多个Wire连接成Wire,以及EdgeToWire算法将Edge转换为Wire。