- UID
- 658062
- 积分
- 2147
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2008-10-22
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
Autodesk.AutoCAD.GraphicsInterface Class
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.GraphicsInterface;
- using Autodesk.AutoCAD.Runtime;
- using System.Collections.Generic;
- using System;
- using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;
- namespace CustomMove
- {
- public class Commands
- {
- [CommandMethod("MYMOVE", CommandFlags.UsePickSet)]
- public static void CustomMoveCmd()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Editor ed = doc.Editor;
- // Start by getting the objects to move
- // (will use the pickfirst set, if defined)
- PromptSelectionResult psr = ed.GetSelection();
- if (psr.Status != PromptStatus.OK || psr.Value.Count == 0)
- return;
- // Create a collection of the selected objects' IDs
- var ids = new ObjectIdCollection(psr.Value.GetObjectIds());
- // Ask the user to select a base point for the move
- var ppr = ed.GetPoint("\nSpecify base point: ");
- if (ppr.Status != PromptStatus.OK)
- return;
- var basePt = ppr.Value;
- var curPt = basePt;
- // A local delegate for our event handler so
- // we can remove it at the end
- PointMonitorEventHandler handler = null;
- // Our transaction
- var tr = doc.Database.TransactionManager.StartTransaction();
- using (tr)
- {
- // Create our transient drawables, with associated
- // graphics, from the selected objects
- List<Drawable> drawables = CreateTransGraphics(tr, ids);
- try
- {
- // Add our point monitor
- // (as a delegate we have access to basePt and curPt,
- // which avoids having to access global/member state)
- handler = delegate(object sender, PointMonitorEventArgs e)
- {
- // Get the point, with "ortho" applied, if needed
- Point3d pt = e.Context.RawPoint;
- if (IsOrthModeOn())
- pt = GetOrthoPoint(basePt, pt);
- // Update our graphics and the current point
- UpdateTransGraphics(drawables, curPt, pt);
- curPt = pt;
- };
- ed.PointMonitor += handler;
- // Ask for the destination, during which the point
- // monitor will be updating the transient graphics
- var opt = new PromptPointOptions("\nSpecify second point: ");
- opt.UseBasePoint = true;
- opt.BasePoint = basePt;
- ppr = ed.GetPoint(opt);
- // If the point was selected successfully...
- if (ppr.Status == PromptStatus.OK)
- {
- // ... move the entities to their destination
- MoveEntities(
- tr, basePt,
- IsOrthModeOn() ?
- GetOrthoPoint(basePt, ppr.Value) :
- ppr.Value,
- ids
- );
- // And inform the user
- ed.WriteMessage("\n{0} object{1} moved", ids.Count, ids.Count == 1 ? "" : "s");
- }
- }
- catch (Autodesk.AutoCAD.Runtime.Exception ex)
- {
- ed.WriteMessage("\nException: {0}", ex.Message);
- }
- finally
- {
- // Clear any transient graphics
- ClearTransGraphics(drawables);
- // Remove the event handler
- if (handler != null)
- ed.PointMonitor -= handler;
- tr.Commit();
- tr.Dispose();
- }
- }
- }
- private static bool IsOrthModeOn()
- {
- // Check the value of the ORTHOMODE sysvar
- object orth = Application.GetSystemVariable("ORTHOMODE");
- return Convert.ToInt32(orth) > 0;
- }
- private static Point3d GetOrthoPoint(Point3d basePt, Point3d pt)
- {
- // Apply a crude orthographic mode
- double x = pt.X;
- double y = pt.Y;
- Vector3d vec = basePt.GetVectorTo(pt);
- if (Math.Abs(vec.X) >= Math.Abs(vec.Y))
- y = basePt.Y;
- else
- x = basePt.X;
- return new Point3d(x, y, 0.0);
- }
- private static void MoveEntities(Transaction tr, Point3d basePt, Point3d moveTo, ObjectIdCollection ids)
- {
- // Transform a set of entities to a new location
- Matrix3d mat = Matrix3d.Displacement(basePt.GetVectorTo(moveTo));
- foreach (ObjectId id in ids)
- {
- var ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
- ent.TransformBy(mat);
- }
- }
- private static List<Drawable> CreateTransGraphics(Transaction tr, ObjectIdCollection ids)
- {
- // Create our list of drawables to return
- var drawables = new List<Drawable>();
- foreach (ObjectId id in ids)
- {
- // Read each entity
- var ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
- // Clone it, make it red & add the clone to the list
- var drawable = ent.Clone() as Entity;
- drawable.ColorIndex = 1;
- drawables.Add(drawable);
- }
- // Draw each one initially
- foreach (Drawable d in drawables)
- {
- TransientManager.CurrentTransientManager.AddTransient(
- d, TransientDrawingMode.DirectShortTerm,
- 128, new IntegerCollection()
- );
- }
- return drawables;
- }
- private static void UpdateTransGraphics(List<Drawable> drawables, Point3d curPt, Point3d moveToPt)
- {
- // Displace each of our drawables
- Matrix3d mat = Matrix3d.Displacement(curPt.GetVectorTo(moveToPt));
- // Update their graphics
- foreach (Drawable d in drawables)
- {
- var e = d as Entity;
- e.TransformBy(mat);
- TransientManager.CurrentTransientManager.UpdateTransient(
- d, new IntegerCollection()
- );
- }
- }
- private static void ClearTransGraphics(List<Drawable> drawables)
- {
- // Clear the transient graphics for our drawables
- TransientManager.CurrentTransientManager.EraseTransients(
- TransientDrawingMode.DirectShortTerm,
- 128, new IntegerCollection()
- );
- // Dispose of them and clear the list
- foreach (Drawable d in drawables)
- {
- d.Dispose();
- }
- drawables.Clear();
- }
- }
- }
|
|