绘制圆,圆弧,曲线,点,实体填充区域,创建面域,创建组合面域,图案填充,以及选择集(选择个数)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using Autodesk.AutoCAD.EditorInput;
namespace AutoDemo18
{
public class Class1
{
[CommandMethod("ADDC")]
public static void ADDC()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acDB = acDoc.Database;
using (Transaction acTran = acDB.TransactionManager.StartTransaction())
{
BlockTable acBT = acTran.GetObject(acDB.BlockTableId,OpenMode.ForRead) as BlockTable;
BlockTableRecord acBTR = acTran.GetObject(acBT[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
using (Circle c = new Circle())
{
c.Center = new Point3d(10, 10, 0);
c.Radius = 10;
c.ColorIndex = 3;
acBTR.AppendEntity(c);
acTran.AddNewlyCreatedDBObject(c, true);
}
acTran.Commit();
}
}
[CommandMethod("ADDARC")]
public static void ADDARC()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acDB = acDoc.Database;
using (Transaction acTran = acDB.TransactionManager.StartTransaction())
{
BlockTable acBT = acTran.GetObject(acDB.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord acBTR = acTran.GetObject(acBT[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
Arc a = new Arc(new Point3d(20, 10, 0), 10, 0, 1.57);
a.ColorIndex = 3;
acBTR.AppendEntity(a);
acTran.AddNewlyCreatedDBObject(a, true);
acTran.Commit();
}
}
[CommandMethod("AddSpline")]
public static void AddSpline()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acDB = acDoc.Database;
using (Transaction acTran = acDB.TransactionManager.StartTransaction())
{
BlockTable acBT = acTran.GetObject(acDB.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord acBTR = acTran.GetObject(acBT[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
Point3dCollection p3d = new Point3dCollection();
p3d.Add(new Point3d(0, 0, 0));
p3d.Add(new Point3d(10, 10, 0));
p3d.Add(new Point3d(20, 30, 0));
Vector3d vc3 = new Point3d(10, 10, 0).GetAsVector();
Spline sp = new Spline(p3d,vc3,vc3,2,0);
sp.ColorIndex = 1;
acBTR.AppendEntity(sp);
acTran.AddNewlyCreatedDBObject(sp, true);
acTran.Commit();
}
}
[CommandMethod("AddD")]
public static void AddD()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acDB = acDoc.Database;
using (Transaction acTran = acDB.TransactionManager.StartTransaction())
{
BlockTable acBT = acTran.GetObject(acDB.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord acBTR = acTran.GetObject(acBT[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
DBPoint DP = new DBPoint(new Point3d(10,10,0));
acBTR.AppendEntity(DP);
acTran.AddNewlyCreatedDBObject(DP, true);
acDB.Pdmode = 66;
acDB.Pdsize = 5;
acTran.Commit();
}
}
[CommandMethod("AddP")]
public static void AddP()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acDB = acDoc.Database;
using (Transaction acTran = acDB.TransactionManager.StartTransaction())
{
BlockTable acBT = acTran.GetObject(acDB.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord acBTR = acTran.GetObject(acBT[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
Point3d p3d1 = new Point3d(0, 10, 0);
Point3d p3d2 = new Point3d(10, 10, 0);
Point3d p3d3 = new Point3d(0, 0, 0);
Point3d p3d4 = new Point3d(10, 0, 0);
Solid s = new Solid(p3d1, p3d2, p3d3, p3d4);
acBTR.AppendEntity(s);
acTran.AddNewlyCreatedDBObject(s, true);
Point3d p3d5 = new Point3d(20, 10, 0);
Point3d p3d6 = new Point3d(30, 10, 0);
Point3d p3d7 = new Point3d(30, 0, 0);
Point3d p3d8 = new Point3d(20, 0, 0);
Solid sa= new Solid(p3d5, p3d6, p3d7, p3d8);
acBTR.AppendEntity(sa);
acTran.AddNewlyCreatedDBObject(sa, true);
acTran.Commit();
}
}
[CommandMethod("AddM")]
public static void AddM()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acDB = acDoc.Database;
using (Transaction acTran = acDB.TransactionManager.StartTransaction())
{
BlockTable acBT = acTran.GetObject(acDB.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord acBTR = acTran.GetObject(acBT[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
Circle c = new Circle();
c.Center = new Point3d(10, 10, 0);
c.Radius = 10;
c.ColorIndex = 3;
DBObjectCollection dboc = new DBObjectCollection();
dboc.Add(c);
DBObjectCollection dbr = new DBObjectCollection();
dbr = Region.CreateFromCurves(dboc);
Region r = dbr[0] as Region;
acBTR.AppendEntity(r);
acTran.AddNewlyCreatedDBObject(r, true);
acTran.Commit();
}
}
[CommandMethod("AddMS")]
public static void AddMS()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acDB = acDoc.Database;
using (Transaction acTran = acDB.TransactionManager.StartTransaction())
{
BlockTable acBT = acTran.GetObject(acDB.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord acBTR = acTran.GetObject(acBT[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
Circle c = new Circle();
c.Center = new Point3d(10, 10, 0);
c.Radius = 10;
c.ColorIndex = 1;
Circle c2 = new Circle();
c2.Center = new Point3d(15, 10, 0);
c2.Radius = 10;
c2.ColorIndex = 3;
acBTR.AppendEntity(c);
acTran.AddNewlyCreatedDBObject(c, true);
acBTR.AppendEntity(c2);
acTran.AddNewlyCreatedDBObject(c2, true);
DBObjectCollection dboc = new DBObjectCollection();
dboc.Add(c);
dboc.Add(c2);
DBObjectCollection dbr = new DBObjectCollection();
dbr = Region.CreateFromCurves(dboc);
Region r1 = dbr[1] as Region;
Region r2 = dbr[0] as Region;
r1.BooleanOperation(BooleanOperationType.BoolSubtract, r2);
acBTR.AppendEntity(r1);
acTran.AddNewlyCreatedDBObject(r1, true);
acTran.Commit();
}
}
[CommandMethod("AddH")]
public static void AddH()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acDB = acDoc.Database;
using (Transaction acTran = acDB.TransactionManager.StartTransaction())
{
BlockTable acBT = acTran.GetObject(acDB.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord acBTR = acTran.GetObject(acBT[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
Polyline pl = new Polyline();
pl.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0);
pl.AddVertexAt(1, new Point2d(10, 0), 0, 0, 0);
pl.AddVertexAt(2, new Point2d(10, 5), 0, 0, 0);
pl.AddVertexAt(3, new Point2d(0, 5), 0, 0, 0);
pl.AddVertexAt(4, new Point2d(0, 0), 0, 0, 0);
acBTR.AppendEntity(pl);
acTran.AddNewlyCreatedDBObject(pl, true);
ObjectIdCollection oic = new ObjectIdCollection();
oic.Add(pl.ObjectId);
Hatch h = new Hatch();
acBTR.AppendEntity(h);
acTran.AddNewlyCreatedDBObject(h, true);
h.SetHatchPattern(HatchPatternType.PreDefined, "angle");
h.Associative = true;
h.AppendLoop(HatchLoopTypes.Outermost, oic);
h.EvaluateHatch(true);
acTran.Commit();
}
}
[CommandMethod("pickFirst", CommandFlags.UsePickSet)]
public static void pickFirst()
{
object pickF = Application.GetSystemVariable("pickfirst");
if (System.Convert.ToInt16(pickF) != 1)
{
Application.SetSystemVariable("pickfirst", 1);
}
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptSelectionResult psr = ed.SelectImplied();
SelectionSet ss;
if (psr.Status == PromptStatus.OK)
{
ss = psr.Value;
Application.ShowAlertDialog("选中图形的数量是" + ss.Count.ToString());
}
else
{
Application.ShowAlertDialog("没有图形对象被选中!!");
}
ObjectId[] id = new ObjectId[0];
ed.SetImpliedSelection(id);
psr = ed.GetSelection();
if (psr.Status == PromptStatus.OK)
{
ss = psr.Value;
Application.ShowAlertDialog("选中图形的数量是" + ss.Count.ToString());
}
else
{
Application.ShowAlertDialog("没有图形对象被选中!!");
}
}
}
}