两种方法在.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);
}
}
}
|