- [CommandMethod("ca2dr")]
- public static void CreateArc()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- var resPt1 = ed.GetPoint("\nInput First Point:");
- if (resPt1.Status != PromptStatus.OK)
- return;
- var pt1 = resPt1.Value;
- var resPt2 = ed.GetPoint("\nInput Second Point:");
- if (resPt2.Status != PromptStatus.OK)
- return;
- var pt2 = resPt2.Value;
- var resDbl = ed.GetDouble("Input R:");
- if (resDbl.Status != PromptStatus.OK)
- return;
- var r = resDbl.Value;
- Point3d center;
- if (pt1.DistanceTo(pt2) > r * 2)
- {
- ed.WriteMessage("\n半径太小!");
- return;
- }
- else if (pt1.DistanceTo(pt2) == r * 2)
- {
- center = (pt1 + pt2.GetAsVector()) / 2;
- }
- else
- {
- CircularArc3d ca3d1 = new CircularArc3d(pt1, Vector3d.ZAxis, r);
- CircularArc3d ca3d2 = new CircularArc3d(pt2, Vector3d.ZAxis, r);
- CurveCurveIntersector3d cci3d = new CurveCurveIntersector3d(ca3d1, ca3d2, Vector3d.ZAxis);
- center = cci3d.GetIntersectionPoint(0);
- double ang = (pt2 - center).GetAngleTo(pt1 - center, -Vector3d.ZAxis);
- if(ang > Math.PI )
- center = cci3d.GetIntersectionPoint(1);
- }
- using (var tr = db.TransactionManager.StartTransaction())
- {
- Arc arc =
- new Arc(
- center,
- r,
- (pt1 - center).AngleOnPlane(new Plane()),
- (pt2 - center).AngleOnPlane(new Plane()));
- var btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
- btr.AppendEntity(arc);
- tr.AddNewlyCreatedDBObject(arc, true);
- tr.Commit();
- }
- }
|