三维线段,用户坐标系,多边形网格,多面网格,长方体,楔形实体,旋转长方体,长方体与椭圆体的交集运算,裁切长方体
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;
namespace AutoDemo22
{
public class Class1
{
[CommandMethod("SWXD")]
public static void SWXD()
{
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(5, 0), 0, 0, 0);
Pl.AddVertexAt(2, new Point2d(5, 5), 0, 0, 0);
Pl.ColorIndex = 1;
btr.AppendEntity(Pl);
tran.AddNewlyCreatedDBObject(Pl, true);
Polyline3d pl3d = new Polyline3d();
pl3d.ColorIndex = 5;
btr.AppendEntity(pl3d);
tran.AddNewlyCreatedDBObject(pl3d, true);
Point3dCollection p3dC = new Point3dCollection();
p3dC.Add(new Point3d(5, 5, 0));
p3dC.Add(new Point3d(0, 5, 5));
p3dC.Add(new Point3d(0, 0, 5));
p3dC.Add(new Point3d(0, 0, 0));
foreach (Point3d p in p3dC)
{
PolylineVertex3d pv = new PolylineVertex3d(p);
pl3d.AppendVertex(pv);
tran.AddNewlyCreatedDBObject(pv, true);
}
tran.Commit();
}
}
[CommandMethod("YHZBX")]
public static void YHZBX()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tran = db.TransactionManager.StartTransaction())
{
UcsTable ucs = tran.GetObject(db.UcsTableId, OpenMode.ForRead) as UcsTable;
UcsTableRecord ucsr;
if (ucs.Has("用户坐标系") == false)
{
ucsr = new UcsTableRecord();
tran.GetObject(db.UcsTableId, OpenMode.ForWrite);
ucsr.Name = "用户坐标系";
ucs.Add(ucsr);
tran.AddNewlyCreatedDBObject(ucsr, true);
}
else
{
ucsr = tran.GetObject(ucs["用户坐标系"], OpenMode.ForWrite) as UcsTableRecord;
}
ucsr.Origin = new Point3d(5, 5, 0);
ucsr.XAxis = new Vector3d(1, 0, 0);
ucsr.YAxis = new Vector3d(0, 1, 0);
ViewportTableRecord vprt = tran.GetObject(doc.Editor.ActiveViewportId, OpenMode.ForRead) as ViewportTableRecord;
vprt.IconAtOrigin = true;
vprt.IconEnabled = true;
vprt.SetUcs(ucsr.Id);
doc.Editor.UpdateTiledViewportsFromDatabase();
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(0, 0, 0);
c.Radius = 1;
c.ColorIndex = 1;
btr.AppendEntity(c);
tran.AddNewlyCreatedDBObject(c, true);
Circle c2 = c.Clone() as Circle;
c2.ColorIndex = 5;
Matrix3d m3d = Matrix3d.AlignCoordinateSystem(Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis,
vprt.Ucs.Origin, vprt.Ucs.Xaxis, vprt.Ucs.Yaxis, vprt.Ucs.Zaxis);
c2.TransformBy(m3d);
btr.AppendEntity(c2);
tran.AddNewlyCreatedDBObject(c2, true);
tran.Commit();
}
}
[CommandMethod("DBXWG")]
public static void DBXWG()
{
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;
PolygonMesh pm = new PolygonMesh();
pm.MSize = 3;
pm.NSize = 4;
btr.AppendEntity(pm);
tran.AddNewlyCreatedDBObject(pm, true);
Point3dCollection p3dC = new Point3dCollection();
p3dC.Add(new Point3d(0, 0, 0));
p3dC.Add(new Point3d(5, 0, 5));
p3dC.Add(new Point3d(10, 0, 0));
p3dC.Add(new Point3d(15, 0, 5));
p3dC.Add(new Point3d(0, 5, 0));
p3dC.Add(new Point3d(5, 5, 5));
p3dC.Add(new Point3d(10, 5, 0));
p3dC.Add(new Point3d(15, 5, 5));
p3dC.Add(new Point3d(0, 10, 0));
p3dC.Add(new Point3d(5, 10, 5));
p3dC.Add(new Point3d(10, 10, 0));
p3dC.Add(new Point3d(15, 10, 5));
foreach (Point3d p in p3dC)
{
PolygonMeshVertex pmv = new PolygonMeshVertex(p);
pm.AppendVertex(pmv);
tran.AddNewlyCreatedDBObject(pmv, true);
}
tran.Commit();
}
}
[CommandMethod("DMWG")]
public static void DMWG()
{
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;
PolyFaceMesh pfm = new PolyFaceMesh();
btr.AppendEntity(pfm);
tran.AddNewlyCreatedDBObject(pfm, true);
Point3dCollection p3dC = new Point3dCollection();
p3dC.Add(new Point3d(0, 0, 0));
p3dC.Add(new Point3d(0, 5, 0));
p3dC.Add(new Point3d(0, 10, 5));
p3dC.Add(new Point3d(5, 0, 0));
p3dC.Add(new Point3d(5, 5, 0));
p3dC.Add(new Point3d(5, 10, 5));
foreach (Point3d p in p3dC)
{
PolyFaceMeshVertex pfmv = new PolyFaceMeshVertex(p);
pfm.AppendVertex(pfmv);
tran.AddNewlyCreatedDBObject(pfmv, true);
}
FaceRecord fr1 = new FaceRecord(1, 2, 5, 4);
pfm.AppendFaceRecord(fr1);
tran.AddNewlyCreatedDBObject(fr1, true);
FaceRecord fr2 = new FaceRecord(2, 3, 6, 5);
pfm.AppendFaceRecord(fr2);
tran.AddNewlyCreatedDBObject(fr2, true);
tran.Commit();
}
}
[CommandMethod("CFT")]
public static void CFT()
{
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;
Solid3d s3d = new Solid3d();
s3d.CreateBox(10, 20, 40);
Matrix3d m3d = Matrix3d.Displacement(new Point3d(5, 10, 20) - Point3d.Origin);
s3d.TransformBy(m3d);
btr.AppendEntity(s3d);
tran.AddNewlyCreatedDBObject(s3d, true);
tran.Commit();
}
}
[CommandMethod("XXST")]
public static void XXST()
{
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;
Solid3d s3d = new Solid3d();
s3d.CreateWedge(10, 20, 40);
Matrix3d ma3d = Matrix3d.Displacement(new Point3d(5, 10, 20) - Point3d.Origin);
s3d.TransformBy(ma3d);
btr.AppendEntity(s3d);
tran.AddNewlyCreatedDBObject(s3d, true);
tran.Commit();
}
}
[CommandMethod("XZST")]
public static void XZST()
{
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;
Solid3d s3d = new Solid3d();
s3d.CreateBox(10, 20, 40);
Matrix3d m3d = Matrix3d.Displacement(new Point3d(5, 10, 20) - Point3d.Origin);
s3d.TransformBy(m3d);
btr.AppendEntity(s3d);
tran.AddNewlyCreatedDBObject(s3d, true);
Solid3d s3d2 = s3d.Clone() as Solid3d;
s3d2.ColorIndex = 1;
Matrix3d m = doc.Editor.CurrentUserCoordinateSystem;
CoordinateSystem3d cs = m.CoordinateSystem3d;
Matrix3d m3dx = Matrix3d.Rotation(3.14, cs.Zaxis, new Point3d(0, 0, 0));
s3d2.TransformBy(m3dx);
btr.AppendEntity(s3d2);
tran.AddNewlyCreatedDBObject(s3d2, true);
tran.Commit();
}
}
[CommandMethod("JSJJ")]
public static void JSJJ()
{
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;
Solid3d s3d = new Solid3d();
s3d.CreateBox(10, 20, 40);
Matrix3d m3d = Matrix3d.Displacement(new Point3d(5, 10, 20) - Point3d.Origin);
s3d.TransformBy(m3d);
btr.AppendEntity(s3d);
tran.AddNewlyCreatedDBObject(s3d, true);
Solid3d s3d2 = new Solid3d();
s3d2.CreateFrustum(20, 5, 10, 5);
s3d2.ColorIndex = 5;
btr.AppendEntity(s3d2);
tran.AddNewlyCreatedDBObject(s3d2, true);
Solid3d s3dc = s3d2.Clone() as Solid3d;
if (s3dc.CheckInterference(s3d) == true)
{
s3dc.BooleanOperation(BooleanOperationType.BoolIntersect, s3d.Clone() as Solid3d);
s3dc.ColorIndex = 1;
}
btr.AppendEntity(s3dc);
tran.AddNewlyCreatedDBObject(s3dc, true);
tran.Commit();
}
}
[CommandMethod("CQST")]
public static void CQST()
{
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;
Solid3d s3d = new Solid3d();
s3d.CreateBox(10, 20, 40);
Matrix3d m3d = Matrix3d.Displacement(new Point3d(5, 10, 20) - Point3d.Origin);
s3d.TransformBy(m3d);
btr.AppendEntity(s3d);
tran.AddNewlyCreatedDBObject(s3d, true);
Plane p = new Plane(new Point3d(5, 10, 20), Vector3d.XAxis, Vector3d.YAxis);
Solid3d s3d2 = new Solid3d();
s3d2 = s3d.Slice(p, true);
s3d2.ColorIndex = 1;
btr.AppendEntity(s3d2);
tran.AddNewlyCreatedDBObject(s3d2, true);
tran.Commit();
}
}
}
}