本帖最后由 mrhvslisp 于 2011-11-2 20:33 编辑
- #region//求取坐标方位角
- [CommandMethod("NorthAngle")]
- public void NorthAngle()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- PromptEntityOptions options = new PromptEntityOptions("");
- options.Message = "\n请选取直线";
- options.SetRejectMessage("\n该实体不是直线,请重新选择");
- options.AddAllowedClass(typeof(Line), true);
- options.AllowNone = false;
- PromptEntityResult result=ed.GetEntity(options);
- if (result.Status == PromptStatus.OK)
- {
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- Line line = (Line)trans.GetObject(result.ObjectId, OpenMode.ForRead);
- Point3d pt1 = line.StartPoint;
- Point3d pt2 = line.EndPoint;
- double angle;
- double x = pt2.X - pt1.X;
- double y = pt2.Y - pt1.Y;
- if (y == 0)
- {
- if (x > 0)
- {
- angle = 90;
- ed.WriteMessage("\n坐标方位角为{0}°", angle);
- }
- else
- {
- angle = 270;
- ed.WriteMessage("\n坐标方位角为{0}°", angle);
- }
- }
- else
- {
- angle = Math.Atan(Math.Abs(x) / Math.Abs(y)) * 360 / (2 * Math.PI);
- }
- if (x >=0)
- {
- if (y > 0)
- {
- ed.WriteMessage("\n坐标方位角为{0}°", angle);
- }
- else if (y < 0)
- {
- angle = 180 - angle;
- ed.WriteMessage("\n坐标方位角为{0}°", angle);
- }
- }
- else if (x < 0)
- {
- if (y < 0)
- {
- angle = 180 + angle;
- ed.WriteMessage("\n坐标方位角为{0}°", angle);
- }
- else if (y > 0)
- {
- angle = 360 - angle;
- ed.WriteMessage("\n坐标方位角为{0}°", angle);
- }
- }
- }
-
- }
- }
- #endregion
|