c#+CAD动态移动效果

  1  public class MoveRotateScaleJig : DrawJig
  2     {
  3         public static List<Entity> entities = new List<Entity>();
  4         private int step = 1;
  5         private int totalStepNum = 3;
  6 
  7         public Point3d moveStartPnt;
  8         public static Point3d moveEndPnt;
  9         public Double rotateAngle;
 10         public Double scaleFactor;
 11 
 12         public MoveRotateScaleJig(Point3d basePnt)
 13         {
 14             moveStartPnt = basePnt;
 15             moveEndPnt = moveStartPnt;
 16             rotateAngle = 0;
 17             scaleFactor = 1;
 18         }
 19 
 20         public Matrix3d Transformation
 21         {
 22             get
 23             {
 24                 return Matrix3d.Scaling(scaleFactor, moveEndPnt).
 25                  PostMultiplyBy(Matrix3d.Rotation(rotateAngle, Vector3d.ZAxis, moveEndPnt)).
 26                  PostMultiplyBy(Matrix3d.Displacement(moveStartPnt.GetVectorTo(moveEndPnt)));
 27             }
 28         }
 29 
 30         public void AddEntity(Entity ent)
 31         {
 32             entities.Add(ent);
 33         }
 34 
 35         public void TransformEntities()
 36         {
 37             Matrix3d mat = Transformation;
 38             foreach (Entity ent in entities)
 39             {
 40                 ent.TransformBy(mat);
 41             }
 42         }
 43 
 44         protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
 45         {
 46             Matrix3d mat = Transformation;
 47 
 48             WorldGeometry geo = draw.Geometry;
 49             if (geo != null)
 50             {
 51                 geo.PushModelTransform(mat);
 52 
 53                 foreach (Entity ent in e
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用C#中的DrawJig类来实现动态移动和旋转。DrawJig类是用于绘制交互图形的基类,它提供了许多方法来实现交互式绘图。 以下是使用DrawJig类实现动态移动和旋转的示例代码: ```csharp using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.GraphicsInterface; namespace DynamicMoveRotate { public class MyDrawJig : DrawJig { private Point3d _basePoint; private Vector3d _normal; private double _angle; private bool _move; public MyDrawJig(Point3d basePoint, Vector3d normal) { _basePoint = basePoint; _normal = normal; _angle = 0.0; _move = true; } protected override SamplerStatus Sampler(JigPrompts prompts) { if (_move) { JigPromptPointOptions jppo = new JigPromptPointOptions("\n指定移动点: "); PromptPointResult ppr = prompts.AcquirePoint(jppo); if (ppr.Status == PromptStatus.Cancel) return SamplerStatus.Cancel; if (_basePoint.DistanceTo(ppr.Value) > Tolerance.Global.EqualPoint) { _move = false; _angle = _normal.GetAngleTo(ppr.Value - _basePoint); } } else { JigPromptAngleOptions jpa = new JigPromptAngleOptions("\n指定旋转角度: "); jpa.BasePoint = _basePoint; jpa.UseBasePoint = true; PromptDoubleResult pdr = prompts.AcquireAngle(jpa); if (pdr.Status == PromptStatus.Cancel) return SamplerStatus.Cancel; _angle = pdr.Value; } return SamplerStatus.OK; } protected override bool WorldDraw(WorldDraw draw) { Vector3d axis = _normal.GetPerpendicularVector(); Matrix3d mat = Matrix3d.Rotation(_angle, axis, _basePoint); draw.Geometry.Draw(new Line(_basePoint, _basePoint.TransformBy(mat))); return true; } public ResultBuffer Run() { Document doc = Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; PromptEntityOptions peo = new PromptEntityOptions("\n选择实体: "); peo.SetRejectMessage("\n只能选择直线或圆弧!"); peo.AddAllowedClass(typeof(Line), true); peo.AddAllowedClass(typeof(Circle), true); PromptEntityResult per = ed.GetEntity(peo); if (per.Status != PromptStatus.OK) return null; using (Transaction tr = doc.TransactionManager.StartTransaction()) { Entity ent = tr.GetObject(per.ObjectId, OpenMode.ForWrite) as Entity; if (ent == null) return null; _basePoint = ent.GeometricExtents.MinPoint; _normal = ent.GetPlane().Normal; MyDrawJig jig = new MyDrawJig(_basePoint, _normal); PromptResult pr = ed.Drag(jig); if (pr.Status != PromptStatus.OK) return null; Matrix3d mat = Matrix3d.Rotation(jig.Angle, _normal, _basePoint); ent.TransformBy(mat); tr.Commit(); } return new ResultBuffer(new TypedValue[] { new TypedValue((int)DxfCode.Start, per.ObjectId) }); } } public class Commands { [CommandMethod("DYNMOVE")] public void DynamicMove() { MyDrawJig jig = new MyDrawJig(Point3d.Origin, Vector3d.ZAxis); jig.Run(); } } } ``` 在这个示例中,我们创建了一个名为MyDrawJig的派生类来实现DrawJig。在Sampler方法中,我们首先采集移动点,然后在旋转点上采集旋转角度。在WorldDraw方法中,我们绘制了一个直线来表示移动和旋转的效果。最后,在Run方法中,我们选择实体,然后调用Drag方法来启动交互式绘图操作。 要使用此命令,请打开AutoCAD并输入"DYNMOVE"命令。然后,选择要移动和旋转的实体,按照提示来指定移动点和旋转角度即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值