- UID
- 658062
- 积分
- 2147
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2008-10-22
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
讨论帖 http://bbs.xdcad.net/forum.php?m ... &extra=page%3D1

- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.EditorInput;
- [assembly: CommandClass(typeof(PenLin.MyCommands))]
- namespace PenLin
- {
- public class MyCommands
- {
- [CommandMethod("Penlin")]
- public void PenLinArea()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Editor ed = doc.Editor;
- Database db = doc.Database;
- Transaction tr = db.TransactionManager.StartTransaction();
- //选择圆
- PromptEntityOptions peo = new PromptEntityOptions("\nPick curve");
- peo.SetRejectMessage("\nOnly Circle!");
- peo.AddAllowedClass(typeof(Circle), true);
- PromptEntityResult per = ed.GetEntity(peo);
- if (per.Status != PromptStatus.OK)
- {
- return;
- }
- PromptDoubleResult pdr = ed.GetDistance("\nRadius");
- if (pdr.Status != PromptStatus.OK) return;
- double radius = pdr.Value;
- ObjectId id = per.ObjectId;
- using (tr)
- {
- BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
- Curve cv = (Curve)id.GetObject(OpenMode.ForRead);
- Circle ccv = (Circle)cv;
- //判断质心在圆内
- CircularArc3d c3d = new CircularArc3d(ccv.Center, ccv.Normal, ccv.Radius);
- //构造 WP 选择点表
- PointOnCurve3d[] pts = cv.GetGeCurve().GetSamplePoints(0, cv.EndParam, Tolerance.Global.EqualPoint);
- Point3d[] npts = new Point3d[pts.Length];
- for (int i = 0; i < pts.Length; i++)
- {
- npts[i] = pts[i].Point;
- }
- Point3dCollection wpts = new Point3dCollection(npts);
- TypedValue[] filter = new TypedValue[1]
- {
- new TypedValue(0, "CIRCLE")
- };
- PromptSelectionResult psr = ed.SelectWindowPolygon(wpts, new SelectionFilter(filter));
- if (psr.Status != PromptStatus.OK) return;
- SelectionSet ss = psr.Value;
- ObjectId[] ids = ss.GetObjectIds();
- ObjectId[] clst = new ObjectId[ids.Length + 1];
- //圆心列表
- List<Point3d> cPoints = new List<Point3d>();
- for (int i = 0; i < ids.Length; i++)
- {
- Circle circle = (Circle)ids[i].GetObject(OpenMode.ForRead);
- Circle ncircle = new Circle(circle.Center, circle.Normal, radius);
- cPoints.Add(circle.Center);
- clst[i + 1] = btr.AppendEntity(ncircle);//先要加入数据库才可以进行交点断开
- }
- clst[0] = id;
- //对圆交点断开返回断开后的曲线集合
- List<Curve> ncvs = CurveBreakAtInters(clst);
- DBObjectCollection rCollection = new DBObjectCollection();
- for (int i = 0; i < ncvs.Count; i++)
- {
- rCollection.Add(ncvs[i]);
- }
- //由断开的曲线生成 Region
- DBObjectCollection regions = Region.CreateFromCurves(rCollection);
- /*
- * 删除最大Region,没有成功
- DBObject[] nrs = new DBObject[regions.Count];
- regions.CopyTo(nrs, 0);
- List<DBObject> lrs = nrs.ToList();
- lrs.OrderBy(r => ((Region)r).Area);
- */
- foreach (Region region in regions)
- {
- Point3d po = new Point3d();
- Vector3d vx = new Vector3d(1, 0, 0);
- Vector3d vy = new Vector3d(0, 1, 0);
- RegionAreaProperties ars = region.AreaProperties(ref po, ref vx, ref vy);
- Point2d cp = ars.Centroid;
- Point3d cp1 = new Point3d(cp.X, cp.Y, 0);
- //质心在圆外或者面积大于范围的丢弃
- if (!c3d.IsInside(cp1, Tolerance.Global) || region .Area > cv.Area )
- {
- region.Dispose();
- }
- else
- {
- ////*
- /// 判断质心在几个园内
- var quray = cPoints.Where(p => p.DistanceTo(cp1) < radius);
- int j = 0;
- foreach (var point3D in quray)
- {
- j++;
- }
- if (j > 1)
- {
- region.ColorIndex = j;
- }
- ////*/
- DBObjectCollection res = new DBObjectCollection();
- //Region要先加入 BlockTableRecord 才可以作为 HatchLoop
- ObjectId ren = btr.AppendEntity(region);
-
- ObjectIdCollection loopids = new ObjectIdCollection( );
- loopids.Add(ren);
- Hatch hatch = new Hatch();
- hatch.SetHatchPattern(HatchPatternType.PreDefined, "solid");
- hatch.Associative = false;
- hatch.ColorIndex = j; ;
- btr.AppendEntity(hatch);
- tr.AddNewlyCreatedDBObject(hatch, true);
- tr.AddNewlyCreatedDBObject(region, true);
- //HatchLoop 需要 Loop 都加入 btr并显示时才可以 AppendLoodp
- hatch.AppendLoop(HatchLoopTypes.Outermost, loopids );
- hatch.EvaluateHatch(true);//重新计算 Hatch
- }
- }
- //断开的曲线没用了,删除
- for (int i = 1; i < clst.Length; i++)
- {
- Entity ent = (Entity)clst[i].GetObject(OpenMode.ForWrite);
- ent.Erase();
- }
- tr.Commit();
- }
- }
- /// <summary>
- /// 曲线集交点断开
- /// </summary>
- /// <param name="curves">List<Curve></param>
- /// <returns>DBObjectCollection</returns>
- private static DBObjectCollection CreatRegions(List<Curve> curves)
- {
- DBObjectCollection cvs = new DBObjectCollection();
- for (int i = 0; i < curves.Count; i++)
- {
- cvs.Add(curves[i]);
- }
- return Region.CreateFromCurves(cvs);
- }
- private static List<Curve> CurveBreakAtInters(ObjectId[] ssbrk)
- {
- List<Curve> ret = new List<Curve>();
- Dictionary<Curve, List<double>> objpts;
- objpts = new Dictionary<Curve, List<double>>(ssbrk.Length);
- Curve[] cvs = new Curve[ssbrk.Length];
- for (int i = ssbrk.Length - 1; i > -1; i--)
- {
- Curve cv = ssbrk[i].GetObject(OpenMode.ForWrite) as Curve;
- cvs[i] = cv;
- objpts.Add(cv, new List<double>());
- }
- for (int cur = ssbrk.Count() - 1; cur > -1; cur--)
- {
- Curve cv1 = cvs[cur];
- List<double> cv1ps = objpts[cv1];
- for (int n = cur - 1; n > -1; n--)
- {
- Curve cv2 = cvs[n];
- List<double> cv2ps = objpts[cv2];
- Point3dCollection points = new Point3dCollection();
- cv1.IntersectWith(cv2, Intersect.OnBothOperands, points, IntPtr.Zero, IntPtr.Zero);
- foreach (Point3d pt in points)
- {
- cv1ps.Add(cv1.GetParameterAtPoint(pt));
- cv2ps.Add(cv2.GetParameterAtPoint(pt));
- }
- }
- }
- foreach (KeyValuePair<Curve, List<double>> var in objpts)
- {
- Curve cv = var.Key;
- if (var.Value.Count == 0) //没有交点
- {
- continue;
- }
- else
- {
- if (var.Value.Count == 1 && cv.IsPeriodic && cv.IsPersistent)
- {
- continue;
- }
- var.Value.Sort();
- double[] arrpt = new double[var.Value.Count];
- var.Value.CopyTo(arrpt);
- DoubleCollection pts = new DoubleCollection(arrpt);
- DBObjectCollection objs = cv.GetSplitCurves(pts);
- foreach (DBObject dbobj in objs)
- {
- Curve brks = (Curve)dbobj;
- if (cv.GetDistanceAtParameter(brks.EndParam) > 1e-6)
- {
- ret.Add(brks);
- }
- }
- }
- }
- return ret;
- }
- }
- }
|
|