欢迎加我的QQ群:193522571,一起来讨论、交流!

两种方法在.Net中调用AutoCAD中的命令

1.using wrapper RunCommand:

using System;
using System.Linq.Expressions;
using System.Reflection;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace RevisionCloud
{
    public class UsingRunCommand
    {
        [CommandMethod("CLOUD1")]
        public void Cloud()
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            PromptPointResult ppr = ed.GetPoint("\nSpecify the first corner: ");
            if (ppr.Status != PromptStatus.OK) return;
            Point3d pt1 = ppr.Value;

            ppr = ed.GetCorner("\nSpecify the opposite corner: ", pt1);
            if (ppr.Status != PromptStatus.OK) return;
            Point3d pt2 = ppr.Value;

            ObjectId id = DrawRectangle(db, pt1, pt2);
            ed.Command("_revcloud", "_arc", 2.0, 6.0, "_object", id, "_no");
        }

        private static ObjectId DrawRectangle(Database db, Point3d pt1, Point3d pt2)
        {
            ObjectId id;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            using (Polyline pline = new Polyline(4))
            {
                pline.AddVertexAt(0, new Point2d(pt1.X, pt1.Y), 0.0, 0.0, 0.0);
                pline.AddVertexAt(1, new Point2d(pt2.X, pt1.Y), 0.0, 0.0, 0.0);
                pline.AddVertexAt(2, new Point2d(pt2.X, pt2.Y), 0.0, 0.0, 0.0);
                pline.AddVertexAt(3, new Point2d(pt1.X, pt2.Y), 0.0, 0.0, 0.0);
                pline.Closed = true;
                BlockTableRecord space =
                    (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                id = space.AppendEntity(pline);
                tr.AddNewlyCreatedDBObject(pline, true);
                tr.Commit();
            }
            return id;
        }
    }

    public static class Extension
    {
        public static PromptStatus Command(this Editor ed, params object[] args)
        {
            if (ed == null)
                throw new ArgumentNullException("ed");
            return runCommand(ed, args);
        }

        static Func<Editor, object[], PromptStatus> runCommand = GenerateRunCommand();

        static Func<Editor, object[], PromptStatus> GenerateRunCommand()
        {
            MethodInfo method = typeof(Editor).GetMethod(
                "RunCommand", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
            ParameterExpression instance = Expression.Parameter(typeof(Editor), "ed");
            ParameterExpression args = Expression.Parameter(typeof(object[]), "args");
            return Expression.Lambda<Func<Editor, object[], PromptStatus>>(
               Expression.Call(instance, method, args), instance, args)
                  .Compile();
        }
    }
}

2.using acedcommand:

using System;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace RevisionCloud
{
    public class UsingAcedCommand
    {
        [CommandMethod("CLOUD2")]
        public static void Cloud2()
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            PromptPointResult ppr = ed.GetPoint("\nSpecify the first corner: ");
            if (ppr.Status != PromptStatus.OK) return;
            Point3d pt1 = ppr.Value;

            ppr = ed.GetCorner("\nSpecify the opposite corner: ", pt1);
            if (ppr.Status != PromptStatus.OK) return;
            Point3d pt2 = ppr.Value;

            ObjectId id = DrawRectangle(db, pt1, pt2);
            DrawRevCloud(id, 1.0);
        }

        [System.Security.SuppressUnmanagedCodeSecurity]
        [DllImport("accore.dll", EntryPoint = "acedCmd", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        extern static private int acedCmd(IntPtr resbuf);

        private static ObjectId DrawRectangle(Database db, Point3d pt1, Point3d pt2)
        {
            ObjectId id;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            using (Polyline pline = new Polyline(4))
            {
                pline.AddVertexAt(0, new Point2d(pt1.X, pt1.Y), 0.0, 0.0, 0.0);
                pline.AddVertexAt(1, new Point2d(pt2.X, pt1.Y), 0.0, 0.0, 0.0);
                pline.AddVertexAt(2, new Point2d(pt2.X, pt2.Y), 0.0, 0.0, 0.0);
                pline.AddVertexAt(3, new Point2d(pt1.X, pt2.Y), 0.0, 0.0, 0.0);
                pline.Closed = true;
                BlockTableRecord space =
                    (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                id = space.AppendEntity(pline);
                tr.AddNewlyCreatedDBObject(pline, true);
                tr.Commit();
            }
            return id;
        }

        private static void DrawRevCloud(ObjectId id, double scale)
        {
            ResultBuffer args = new ResultBuffer(
                new TypedValue((int)LispDataType.Text, "_revcloud"),
                new TypedValue((int)LispDataType.Text, "_arc"),
                new TypedValue((int)LispDataType.Double, 2.0 / scale),
                new TypedValue((int)LispDataType.Double, 6.0 / scale),
                new TypedValue((int)LispDataType.Text, "_object"),
                new TypedValue((int)LispDataType.ObjectId, id),
                new TypedValue((int)LispDataType.Text, "_no"));
            acedCmd(args.UnmanagedObject);
        }
    }
}

  

posted @ 2014-03-19 20:05  swtool  阅读(4905)  评论(0编辑  收藏  举报
欢迎加我的QQ群:193522571,一起来讨论、交流!