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

两种方法在.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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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);
         }
     }
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值