1. 程式人生 > 其它 >autocad中填充的不等比例縮放

autocad中填充的不等比例縮放

前段時間折騰了一下不等比例縮放的矩陣,

可以看一下這篇隨筆

昨天在寫真實專案程式碼時,

在縮放填充時遇到了問題,

經過摸索,

找到了解決方法。

需要處理的填充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); } }