拓扑形状具有形位变换的能力,如平移、缩放、旋转、镜像。通常,这些变换接口分为两类,如 Translate* 和 Translated* 这两种接口,前者是对调用对象自身进行变换,后者是复制调用对象的副本,对副本进行变换,然后返回副本。
为了代码的简洁,此处定义一个工具函数用于将形状添加到文档中
typescript
function addShapeToDoc(theShape: Mx3dShapeObject, color?: Mx3dGeColor){
// 获取文档
const doc = mxcad3d.getDocument();
// 新增标签
const label = doc.addShapeLabel();
// 设置形状
label.setShape(theShape);
// 设置颜色
if(color) label.setColor(color);
}
平移
typescript
const csysr = new Mx3dGeCSYSR();
csysr.SetLocation(450, 0, 0);
const box = new Mx3dMkBox(csysr, 20, 20, 20);
const boxShape = box.Shape();
const movedBoxShape = boxShape.TranslatedByVec(new Mx3dGeVec(0, 50, 0));
addShapeToDoc(boxShape);
addShapeToDoc(movedBoxShape);
// 更新显示(会更新canvas中显示的模型)
mxcad3d.update();
缩放
typescript
const csysr = new Mx3dGeCSYSR();
csysr.SetLocation(500, 0, 0);
const box = new Mx3dMkBox(csysr, 20, 20, 20);
const boxShape = box.Shape();
const movedBoxShape = boxShape.TranslatedByVec(new Mx3dGeVec(0, 50, 0));
movedBoxShape.Scale(new Mx3dGePoint(510, 50, 0), 2);
addShapeToDoc(boxShape);
addShapeToDoc(movedBoxShape);
// 更新显示(会更新canvas中显示的模型)
mxcad3d.update();
旋转
typescript
const csysr = new Mx3dGeCSYSR();
csysr.SetLocation(550, 0, 0);
const box = new Mx3dMkBox(csysr, 20, 20, 20);
const boxShape = box.Shape();
const movedBoxShape = boxShape.TranslatedByVec(new Mx3dGeVec(0, 50, 0));
const axis = new Mx3dGeAxis(new Mx3dGePoint(560, 60, 0), new Mx3dGeDir(0, 0, 1));
movedBoxShape.Rotate(axis, Math.PI / 4);
addShapeToDoc(boxShape);
addShapeToDoc(movedBoxShape);
// 更新显示(会更新canvas中显示的模型)
mxcad3d.update();
镜像
typescript
const csysr = new Mx3dGeCSYSR();
csysr.SetLocation(600, 0, 0);
const box = new Mx3dMkBox(csysr, 20, 20, 20);
const boxShape = box.Shape();
const axis = new Mx3dGeAxis(new Mx3dGePoint(615, 30, 0), new Mx3dGeDir(0, 0, 1));
const mirroredBoxShape = boxShape.MirroredByAxis(axis);
addShapeToDoc(boxShape);
addShapeToDoc(mirroredBoxShape);
// 更新显示(会更新canvas中显示的模型)
mxcad3d.update();