前段时间折腾了一下不等比例缩放的矩阵,
可以看一下这篇随笔
昨天在写真实项目代码时,
在缩放填充时遇到了问题,
经过摸索,
找到了解决方法。
需要处理的填充loop是由curves组成的,
开始我直接缩放这些curves,
添加到集合(Curve2dCollection)中,
创建新的loop,
但在添加到集合中时,
遇到错误,
也就是没法添加到集合中,
后来通过clone这些curves,
成功添加到集合中,
实现了自己的需求。
public void C_haTest() { Editor ed = doc.Editor; PromptEntityResult per = ed.GetEntity("\n拾取填充"); double sx = 2, sy = 3, sz = 1; var ppr = ed.GetPoint("\n拾取点"); if (ppr.Status != PromptStatus.OK) return; Point3d basePoint = ppr.Value; double[] ds2 = new double[] { sx, 0, (1 - sx) * basePoint.X, 0, sy, (1 - sy) * basePoint.Y, 0, 0, 1 }; if (per.Status != PromptStatus.OK) return; try { using (Transaction tr = doc.TransactionManager.StartTransaction()) { var hatch = per.ObjectId.GetObject(OpenMode.ForWrite) as Hatch; int n = hatch.NumberOfLoops; for (int i = 0; i < n; i++) { var loop = hatch.GetLoopAt(i); var loopType = loop.LoopType; Curve2dCollection curs = new Curve2dCollection(); IntegerCollection edgeTypeCollection = new IntegerCollection(); // 注意填充loop不一定是用curves组成的, // 有可能是Polyline for (int j = 0; j < loop.Curves.Count; j++) { //cur.TransformBy(new Matrix2d(ds2)); //curs.Add(cur); //edgeTypeCollection.Add(1); // 需要克隆之前的曲线, // 直接缩放原有曲线时,无法添加到集合中 // 上面注释掉的代码时无法运行, // 会提示值不在范围内..... var cur = loop.Curves[j].Clone(); if (cur.GetType() == typeof(LineSegment2d)) { LineSegment2d ls = cur as LineSegment2d; ls.TransformBy(new Matrix2d(ds2)); curs.Add(ls); edgeTypeCollection.Add(1); } else if (cur.GetType() == typeof(CircularArc2d)) { CircularArc2d ls = cur as CircularArc2d; ls.TransformBy(new Matrix2d(ds2)); curs.Add(ls); edgeTypeCollection.Add(2); } else if (cur.GetType() == typeof(EllipticalArc2d)) { EllipticalArc2d ls = cur as EllipticalArc2d; ls.TransformBy(new Matrix2d(ds2)); curs.Add(ls); edgeTypeCollection.Add(3); } else if (cur.GetType() == typeof(SplineEntity2d)) { SplineEntity2d ls = cur as SplineEntity2d; ls.TransformBy(new Matrix2d(ds2)); curs.Add(ls); edgeTypeCollection.Add(4); } } hatch.AppendLoop(loopType, curs, edgeTypeCollection); } // 删掉之前的边界 for (int i = n - 1; i > 0; i--) { hatch.RemoveLoopAt(i); } tr.Commit(); } } catch (System.Exception ex) { ed.WriteMessage(ex.Message); } }