矩形阵列,延伸直线,分解多段线,编辑多段线,编辑图案填充,使用图层,使用线性,使用图层
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;
using Autodesk.AutoCAD.Colors;
namespace AutoDemo20
{
public class Class1
{
static Point2d PointC(Point2d zxd, double ang, double dic)
{
double x = zxd.X + dic * Math.Cos(ang);
double y = zxd.Y + dic * Math.Sin(ang);
Point2d p = new Point2d(x, y);
return p;
}
[CommandMethod("HXZL")]
public static void HXZL()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tran = db.TransactionManager.StartTransaction())
{
BlockTable bt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tran.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
DBPoint p = new DBPoint(new Point3d(100, 100, 0));
btr.AppendEntity(p);
tran.AddNewlyCreatedDBObject(p, true);
Point2d pZXD = new Point2d(100, 100);
Circle c = new Circle();
c.Center = new Point3d(100, 150, 0);
c.Radius = 15;
btr.AppendEntity(c);
tran.AddNewlyCreatedDBObject(c, true);
int n = 1;
double angle = 1.0472;
while (n < 6)
{
Circle xC = c.Clone() as Circle;
Point2d pJD = new Point2d(xC.Center.X, xC.Center.Y);
double dic = pZXD.GetDistanceTo(pJD);
double angleFX = pZXD.GetVectorTo(pJD).Angle;
Point2d pXCc = PointC(pZXD, angleFX + n * angle, dic);
Vector2d v2d = pJD.GetVectorTo(pXCc);
Vector3d v3d = new Vector3d(v2d.X, v2d.Y, 0);
xC.TransformBy(Matrix3d.Displacement(v3d));
btr.AppendEntity(xC);
tran.AddNewlyCreatedDBObject(xC, true);
n = n + 1;
}
tran.Commit();
}
}
[CommandMethod("YCZX")]
public static void YCZX()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tran = db.TransactionManager.StartTransaction())
{
BlockTable bt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tran.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
Line L = new Line(new Point3d(0, 0, 0), new Point3d(5, 5, 0));
btr.AppendEntity(L);
tran.AddNewlyCreatedDBObject(L, true);
doc.Editor.Regen();
Application.ShowAlertDialog("延长之前的线段");
L.EndPoint = L.EndPoint + L.Delta;
tran.Commit();
}
}
[CommandMethod("FJDDX")]
public static void FJDDX()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tran = db.TransactionManager.StartTransaction())
{
BlockTable bt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tran.GetObject(bt[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, 10), 0, 0, 0);
PL.AddVertexAt(2, new Point2d(10, 20), 0, 0, 0);
PL.AddVertexAt(3, new Point2d(15, 30), 0, 0, 0);
btr.AppendEntity(PL);
tran.AddNewlyCreatedDBObject(PL, true);
DBObjectCollection dbOC = new DBObjectCollection();
PL.Explode(dbOC);
foreach (Entity ent in dbOC)
{
ent.ColorIndex = 1;
btr.AppendEntity(ent);
tran.AddNewlyCreatedDBObject(ent, true);
}
tran.Commit();
}
}
[CommandMethod("BJDDX")]
public static void BJDDX()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tran = db.TransactionManager.StartTransaction())
{
BlockTable bt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tran.GetObject(bt[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, 5), 0, 0, 0);
pL.AddVertexAt(2, new Point2d(20, 25), 0, 0, 0);
btr.AppendEntity(pL);
tran.AddNewlyCreatedDBObject(pL, true);
Polyline pL2 = pL.Clone() as Polyline;
pL2.ColorIndex = 1;
pL2.AddVertexAt(3, new Point2d(30, 40), 0, 0, 0);
pL2.SetStartWidthAt(1, 0.1);
pL2.SetEndWidthAt(1, 0.5);
pL2.Closed = true;
pL2.SetBulgeAt(2, -0.5);
btr.AppendEntity(pL2);
tran.AddNewlyCreatedDBObject(pL2, true);
tran.Commit();
}
}
[CommandMethod("BJTC")]
public static void BJTC()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tran = db.TransactionManager.StartTransaction())
{
BlockTable bt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tran.GetObject(bt[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(0, 100), 0, 0, 0);
pl.AddVertexAt(2, new Point2d(100, 100), 0, 0, 0);
pl.AddVertexAt(3, new Point2d(100, 0), 0, 0, 0);
btr.AppendEntity(pl);
tran.AddNewlyCreatedDBObject(pl, true);
Line L = new Line(new Point3d(0, 0, 0), new Point3d(100, 0, 0));
btr.AppendEntity(L);
tran.AddNewlyCreatedDBObject(L, true);
ObjectIdCollection oic = new ObjectIdCollection();
oic.Add(pl.Id);
oic.Add(L.Id);
Hatch H = new Hatch();
btr.AppendEntity(H);
tran.AddNewlyCreatedDBObject(H, true);
H.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");
H.Associative = true;
H.AppendLoop(HatchLoopTypes.Outermost, oic);
Hatch H2 = H.Clone() as Hatch;
btr.AppendEntity(H2);
tran.AddNewlyCreatedDBObject(H2, true);
H2.ColorIndex = 1;
H2.PatternScale = H.PatternScale + 2;
H2.SetHatchPattern(H.PatternType, H.PatternName);
H2.EvaluateHatch(true);
tran.Commit();
}
}
[CommandMethod("SYTC")]
public static void SYTC()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tran = db.TransactionManager.StartTransaction())
{
LayerTable lt = tran.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;
if (lt.Has("绘图") == false)
{
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = "绘图";
ltr.Color = Color.FromRgb(255, 0, 0);
lt.Add(ltr);
tran.AddNewlyCreatedDBObject(ltr, true);
}
BlockTable bt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tran.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
Circle c = new Circle();
c.Center = new Point3d(100, 100, 0);
c.Radius = 20;
c.Layer = "绘图";
btr.AppendEntity(c);
tran.AddNewlyCreatedDBObject(c, true);
if (lt.Has("关闭图层") == false)
{
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = "关闭图层";
ltr.IsOff = true;
lt.Add(ltr);
tran.AddNewlyCreatedDBObject(ltr, true);
}
if (lt.Has("冻结图层") == false)
{
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = "冻结图层";
ltr.IsFrozen = true;
lt.Add(ltr);
tran.AddNewlyCreatedDBObject(ltr, true);
}
tran.Commit();
}
}
[CommandMethod("SYXX")]
public static void SYXX()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tran = db.TransactionManager.StartTransaction())
{
LinetypeTable ltt = tran.GetObject(db.LinetypeTableId, OpenMode.ForRead) as LinetypeTable;
string strLN = "dot";
if (ltt.Has(strLN) == false)
{
db.LoadLineTypeFile(strLN, "acad.lin");
}
BlockTable bt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tran.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
Line L = new Line(new Point3d(0, 0, 0), new Point3d(100, 100, 0));
L.Linetype = strLN;
btr.AppendEntity(L);
tran.AddNewlyCreatedDBObject(L, true);
tran.Commit();
}
}
[CommandMethod("BCYX")]
public static void BCYX()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
LayerStateManager lsn;
lsn = doc.Database.LayerStateManager;
string name;
name = "yxxx";
if (lsn.HasLayerState(name) == false)
{
lsn.SaveLayerState(name, LayerStateMasks.Color | LayerStateMasks.LineType, ObjectId.Null);
}
}
[CommandMethod("HFYX")]
public static void HFYX()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
LayerStateManager lsn = doc.Database.LayerStateManager;
string name;
name = "yxxx";
if (lsn.HasLayerState(name) == true)
{
lsn.RestoreLayerState(name, ObjectId.Null, 1, LayerStateMasks.Color | LayerStateMasks.LineType);
}
else Application.ShowAlertDialog("不含有该图层状态");
}
[CommandMethod("DCZT")]
public static void DCZT()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
LayerStateManager lsn = doc.Database.LayerStateManager;
string name;
name = "yxxx";
if (lsn.HasLayerState(name) == true)
{
lsn.ExportLayerState(name, "c:\\" + name + ".las");
}
else Application.ShowAlertDialog("该图层状态不存在!!");
}
[CommandMethod("DRZT")]
public static void DRZT()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
LayerStateManager lsn = doc.Database.LayerStateManager;
string path = "c:\\yxxx.las";
if (System.IO.File.Exists(path))
{
try
{
lsn.ImportLayerState(path);
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
Application.ShowAlertDialog(ex.Message);
}
}
}
}
}