本例创建一个Leader(引线)对象,并以Y轴为镜像轴镜像,类似的对对象的操作还有偏移、复制、缩放、旋转,相关的函数及操作见第五章的对象操作方法。 - using System;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Runtime;
- [assembly: CommandClass(typeof(Sample.EditEntity))]
- namespace Sample
- {
- class EditEntity
- {
- [CommandMethod("EditEntity")]
- public void EditEnt()
- {
- Database db = HostApplicationServices.WorkingDatabase;
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- Leader Leader = (Leader)SelectEntity("\n选择对象");
- if (Leader != null)
- Mirror(Leader, new Point3d(0, 0, 0), new Point3d(0, 1, 0));
- tr.Commit();
- }
- }
- /// <summary>
- /// 提示用户选择单个实体
- /// </summary>
- /// <param name="word">选择提示</param>
- /// <returns>实体对象</returns>
- public static Entity SelectEntity(string word)
- {
- Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- Entity entity = null;
- PromptEntityResult ent = ed.GetEntity(word);
- if (ent.Status == PromptStatus.OK)
- {
- using (Transaction transaction = db.TransactionManager.StartTransaction())
- {
- entity = (Entity)transaction.GetObject(ent.ObjectId, OpenMode.ForWrite, true);
- transaction.Commit();
- }
- }
- return entity;
- }
- /// <summary>
- /// 指定两个镜像参照点得到实体镜像
- /// </summary>
- /// <param name="ent">实体对象</param>
- /// <param name="mirrorPt1">镜像点1</param>
- /// <param name="mirrorPt2">镜像点2</param>
- public static Entity Mirror(Entity ent, Point3d mirrorPt1, Point3d mirrorPt2)
- {
- Line3d mirrorLine = new Line3d(mirrorPt1, mirrorPt2);
- Matrix3d mt = Matrix3d.Mirroring(mirrorLine);
- ent.TransformBy(mt);
- return ent;
- }
- }
- }
复制代码
先画一个用于镜像的对象:
运行命令” EditEntity”后选中对象,可以看到对象按照我们规定的镜像线翻转了。
|