CAD二次開發 學習筆記(1)
總結一張關系圖

合並兩個選擇集,並改變所有對象的顏色
/// <summary>
/// 合並兩次選擇的選擇集,並將所有選擇對象改變顏色
/// </summary>
[CommandMethod("MergeSelectionSet")]
public void MergeSelectionSet()
{
//獲取編輯器
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
//開始選擇
PromptSelectionResult result = ed.GetSelection();
//聲明選擇集
SelectionSet ss = null;
//聲明對象集合
ObjectIdCollection collection = new ObjectIdCollection();
//第一次選擇
if (result.Status == PromptStatus.OK)
{
ss = result.Value;
collection = new ObjectIdCollection(ss.GetObjectIds());
ed.WriteMessage($"第一次選擇了{ss.Count}個對象");
}
else
{
ed.WriteMessage($"第一次選擇了{0}個對象");
}
//第二次選擇
result = ed.GetSelection();
if (result.Status == PromptStatus.OK)
{
ss = result.Value;
ed.WriteMessage($"第二次選擇了{ss.Count}個對象");
}
else
{
ed.WriteMessage($"第二次選擇了{0}個對象");
}
//合並選擇集
if (collection.Count == 0)
{
collection = new ObjectIdCollection(ss.GetObjectIds());
}
else
{
foreach (SelectedObject item in ss)
{
collection.Add(item.ObjectId);
}
}
ed.WriteMessage($"\n一共選擇了{collection.Count}個對象");
//獲取數據庫
Database db = HostApplicationServices.WorkingDatabase;
//改變所有選擇對象的顏色
using (Transaction tr = db.TransactionManager.StartTransaction())
{
foreach (ObjectId item in collection)
{
Entity entity = tr.GetObject(item, OpenMode.ForWrite) as Entity;
entity.ColorIndex = 3;
}
tr.Commit();
}
}
運行結果

通過交叉矩形選擇對象
/// <summary>
/// 通過交叉窗口進行選擇(窗口內的、與窗口相交的都將納入選擇)
/// </summary>
[CommandMethod("selectByCrossingWindow")]
public void SelectByCrossingWindow()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptSelectionResult result = ed.SelectCrossingWindow(new Point3d(2,2,0),new Point3d(10,8,0));
if (result.Status == PromptStatus.OK)
{
SelectionSet ss = result.Value;
ed.WriteMessage($"選擇了{ss.Count}個對象");
}
else
{
ed.WriteMessage($"選擇了{0}個對象");
}
}
運行結果

選擇並改變顏色(綠色代碼3)
/// <summary>
/// 選擇並將選擇對象變成綠色
/// </summary>
[CommandMethod("changeColor")]
public void SelectAndChangeColor()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr=db.TransactionManager.StartTransaction())
{
PromptSelectionResult result = ed.GetSelection();
if (result.Status==PromptStatus.OK)
{
SelectionSet ss = result.Value;
ed.WriteMessage($"選擇了{ss.Count}個對象");
foreach (SelectedObject selectedObject in ss)
{
if (selectedObject != null)
{
Entity entity = tr.GetObject(selectedObject.ObjectId, OpenMode.ForWrite) as Entity;
if (entity!=null)
{
entity.ColorIndex = 3;
}
}
}
}
else
{
ed.WriteMessage($"選擇了{0}個對象");
}
tr.Commit();
}
}
運行結果

選擇對象Pickfirst
[CommandMethod("PickFirstSelection",CommandFlags.UsePickSet)]
public void PickFirstSlection()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptSelectionResult result = ed.SelectImplied();
SelectionSet ss;
//如果狀態OK,說明啟動命令前,就已經選擇了對象
if (result.Status==PromptStatus.OK)
{
ss = result.Value;
Application.ShowAlertDialog($"選擇了{ss.Count}個對象");
}
else
{
Application.ShowAlertDialog("選擇了0個對象");
}
//清空選擇集
ObjectId[] objects = new ObjectId[0];
ed.SetImpliedSelection(objects);
result = ed.GetSelection();
//如果狀態OK,說明選擇了對象
if (result.Status == PromptStatus.OK)
{
ss = result.Value;
Application.ShowAlertDialog($"選擇了{ss.Count}個對象");
}
else
{
Application.ShowAlertDialog("選擇了0個對象");
}
}
運行結果

添加圖案填充升級版Hatch
/// <summary>
/// 添加圖案填充【升級版】
/// </summary>
///
int hatchY = 0;
[CommandMethod("hatchCmdPlus")]
public void HatchCmdPlus()
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
Random random = new Random();
List<string> hatchTypeList = new List<string>() { "ANSI31", "SOLID", "ANGLE", "BRSTONE", "CLAY", "PLASTI", "MUDST", "SWAMP" };
for (int i = 0; i < 20; i++)
{
Circle circle = new Circle(new Point3d(random.Next(0, 300), random.Next(0+ hatchY, 200+ hatchY), 0), Vector3d.ZAxis, random.Next(10, 30));
btr.AppendEntity(circle);
tr.AddNewlyCreatedDBObject(circle, true);
//添加到對象集合
ObjectIdCollection collection = new ObjectIdCollection();
collection.Add(circle.ObjectId);
//先添加
Hatch hatch = new Hatch();
btr.AppendEntity(hatch);
tr.AddNewlyCreatedDBObject(hatch, true);
//再設置,關聯Associative要在AppendLoop之前設置
hatch.SetHatchPattern(HatchPatternType.PreDefined, hatchTypeList[random.Next(0,hatchTypeList.Count)]);
hatch.Associative = true;
hatch.Color = Color.FromColorIndex(ColorMethod.ByAci,(short)random.Next(0,100));
hatch.AppendLoop(HatchLoopTypes.Outermost, collection);
hatch.EvaluateHatch(true);
}
tr.Commit();
}
hatchY += 300;
//總結:Hatch創建需要嚴格遵循順序操作
//0、准備工作:創建填充對象並添加到對象集合ObjectIdCollection
//1、創建hatch,
//2、添加hatch到塊表記錄和事務,
//3、設置hatch
//3.1、設置填充模式SetHatchPattern
//3.2、設置關聯Associative
//3.3、設置循環模式AppendLoop
//3.4、進行計算EvaluateHatch
//要點:先添加,再設置;先關聯,再循環;
}
運行結果

添加圖案填充Hatch
//總結:Hatch創建需要嚴格遵循順序操作
//0、准備工作:創建填充對象並添加到對象集合ObjectIdCollection
//1、創建hatch,
//2、添加hatch到塊表記錄和事務,
//3、設置hatch
//3.1、設置填充模式SetHatchPattern
//3.2、設置關聯Associative
//3.3、設置循環模式AppendLoop
//3.4、進行計算EvaluateHatch
//要點:先添加,再設置;先關聯,再循環;
/// <summary>
/// 添加圖案填充
/// </summary>
[CommandMethod("hatchCmd")]
public void HatchCmd()
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr=db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
Circle circle = new Circle(new Point3d(0, 0, 0), Vector3d.ZAxis, 10);
btr.AppendEntity(circle);
tr.AddNewlyCreatedDBObject(circle,true);
//添加到對象集合
ObjectIdCollection collection = new ObjectIdCollection();
collection.Add(circle.ObjectId);
//先添加
Hatch hatch = new Hatch();
btr.AppendEntity(hatch);
tr.AddNewlyCreatedDBObject(hatch, true);
//再設置,關聯Associative要在AppendLoop之前設置
hatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");
hatch.Associative = true;
hatch.AppendLoop(HatchLoopTypes.Outermost, collection);
hatch.EvaluateHatch(true);
tr.Commit();
}
//總結:Hatch創建需要嚴格遵循順序操作
//0、准備工作:創建填充對象並添加到對象集合ObjectIdCollection
//1、創建hatch,
//2、添加hatch到塊表記錄和事務,
//3、設置hatch
//3.1、設置填充模式SetHatchPattern
//3.2、設置關聯Associative
//3.3、設置循環模式AppendLoop
//3.4、進行計算EvaluateHatch
//要點:先添加,再設置;先關聯,再循環;
}
運行結果

添加組合區域/面域Region
交集、並集、差集
/// <summary>
/// 添加組合區域Region,並設置顏色,求交集、並集、差集
/// </summary>
int regionCount = 0;
double x1 = 0;
[CommandMethod("compositeRegionCmd")]
public void CompositeRegionCmd()
{
Database db = HostApplicationServices.WorkingDatabase;
//創建圓
Circle circle1 = new Circle(new Point3d(x1+2, 2, 0), Vector3d.ZAxis, 10);
Circle circle2 = new Circle(new Point3d(x1+5, 5, 0), Vector3d.ZAxis, 10);
//創建對象集合
DBObjectCollection collection = new DBObjectCollection();
collection.Add(circle1);
collection.Add(circle2);
DBObjectCollection regionCollection = Region.CreateFromCurves(collection);
Region region1 = regionCollection[0] as Region;
region1.Color = Color.FromColorIndex(ColorMethod.ByAci,0);
Region region2 = regionCollection[1] as Region;
region1.Color = Color.FromColorIndex(ColorMethod.ByAci, 3);
switch (regionCount)
{
case 0://交集
region1.BooleanOperation(BooleanOperationType.BoolIntersect,region2);
region2.Dispose();
db.AddEntityToModelSpace(region1);
break;
case 1://並集
region1.BooleanOperation(BooleanOperationType.BoolUnite, region2);
region2.Dispose();
db.AddEntityToModelSpace(region1);
break;
case 2://差集
region1.BooleanOperation(BooleanOperationType.BoolSubtract, region2);
region2.Dispose();
db.AddEntityToModelSpace(region1);
break;
case 3:
db.AddEntityToModelSpace(region1);
db.AddEntityToModelSpace(region2);
break;
}
regionCount++;
regionCount %= 4;
x1 += 30;
}
運行結果

添加區域/面域Region
/// <summary>
/// 添加區域Region,並設置顏色
/// </summary>
[CommandMethod("RegionCmd")]
public void RegionCmd()
{
Database db = HostApplicationServices.WorkingDatabase;
//創建圓
Circle circle1 = new Circle(new Point3d(2, 2, 0), Vector3d.ZAxis, 10);
Circle circle2 = new Circle(new Point3d(5, 5, 0), Vector3d.ZAxis, 10);
//創建對象集合
DBObjectCollection collection = new DBObjectCollection();
collection.Add(circle1);
collection.Add(circle2);
DBObjectCollection regionCollection = Region.CreateFromCurves(collection);
short i = 1;
foreach (var item in regionCollection)
{
var region = item as Region;
region.Color = Color.FromColorIndex(ColorMethod.ByAci, i++);
db.AddEntityToModelSpace(region);
}
}
運行結果

添加點DBPoint升級版
/// <summary>
/// 添加點,並設置點樣式
/// </summary>
int x = 0, y = 20, mode = 0;
[CommandMethod("PointCmd")]
public void PointCmd()
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
//創建點的實體,注意Point3d並不是Entity的子類,應該添加DBPoint類
DBPoint dBPoint = new DBPoint(new Point3d(2 * x, y, 0));
btr.AppendEntity(dBPoint);
tr.AddNewlyCreatedDBObject(dBPoint, true);
//設置點的樣式
db.Pdmode = 32 + mode;
x++;
mode++;
mode %= 5;
//設置點的大小
db.Pdsize = 1;
tr.Commit();
}
}
運行結果

添加實體填充區Solid
/// <summary>
/// 添加實體填充
/// </summary>
[CommandMethod("SolidCmd")]
public void SolidCmd()
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr=db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId,OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite) as BlockTableRecord;
Solid solid1 = new Solid(new Point3d(0,0,0),new Point3d(5,0,0),new Point3d(5,8,0),new Point3d(0,8,0));
solid1.Color = Color.FromColorIndex(ColorMethod.ByColor,1);
btr.AppendEntity(solid1);
tr.AddNewlyCreatedDBObject(solid1,true);
Solid solid2 = new Solid(new Point3d(10, 0, 0), new Point3d(15, 0, 0), new Point3d(10, 8, 0), new Point3d(15, 8, 0));
solid2.Color = Color.FromColorIndex(ColorMethod.ByColor, 3);
btr.AppendEntity(solid2);
tr.AddNewlyCreatedDBObject(solid2,true);
tr.Commit();
}
}
運行結果

添加點DBPoint

點的樣式編碼(本例添加代號34樣式的點)
int x = 4;
[CommandMethod("PointCmd")]
public void PointCmd()
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr=db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId,OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite) as BlockTableRecord;
//創建點的實體,注意Point3d並不是Entity的子類,應該添加DBPoint類
DBPoint dBPoint = new DBPoint(new Point3d(x,3,0));
btr.AppendEntity(dBPoint);
tr.AddNewlyCreatedDBObject(dBPoint,true);
//設置點的樣式
db.Pdmode = 34;
//設置點的大小
db.Pdsize = 1;
x++;
tr.Commit();
}
運行結果

用EntityJig動態繪制一條直線
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
[assembly: CommandClass(typeof(JigTest.LineJig))]
namespace JigTest
{
/// <summary>
/// 命令類
/// </summary>
public class LineJig
{
/// <summary>
/// 命令方法
/// </summary>
[CommandMethod("linejig")]
public void DoIt()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptPointOptions options = new PromptPointOptions("\n 指定起點");
ed.WriteMessage("\n 准備接收用戶輸入的點");
PromptPointResult result = ed.GetPoint(options);//阻塞方法
ed.WriteMessage("\n 已獲取用戶輸入的點");
ed.WriteMessage($"\n 起點坐標:{result.Value.X},{result.Value.Y},{result.Value.Z}");
Database db = Application.DocumentManager.MdiActiveDocument.Database;
LineJigImpl jig = new LineJigImpl(result.Value);
Application.DocumentManager.MdiActiveDocument.Editor.Drag(jig);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
btr.AppendEntity(jig.GetEntity());
tr.AddNewlyCreatedDBObject(jig.GetEntity(),true);
tr.Commit();
}
}
}
/// <summary>
/// Jig實現類
/// </summary>
class LineJigImpl : EntityJig
{
Point3d m_StartPoint, m_EndPoint;
Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
/// <summary>
/// 構造函數
/// </summary>
/// <param name="start">起點</param>
public LineJigImpl(Point3d start) : base(new Line())
{
editor.WriteMessage("\n 執行構造函數");
m_StartPoint = start;
editor.WriteMessage($"\n m_StartPoint初始值:{m_StartPoint}");
editor.WriteMessage($"\n m_EndPoint默認值:{m_EndPoint}");
}
/// <summary>
/// 動態/即時/實時采樣器
/// </summary>
/// <param name="prompts">即時提示</param>
/// <returns></returns>
protected override SamplerStatus Sampler(JigPrompts prompts)
{
editor.WriteMessage("\n 執行Sampler;");
JigPromptPointOptions options = new JigPromptPointOptions("\n 指定終點");
options.UserInputControls =
UserInputControls.Accept3dCoordinates |
UserInputControls.NoZeroResponseAccepted |
UserInputControls.NoNegativeResponseAccepted;
PromptPointResult result = prompts.AcquirePoint(options);
Point3d tempPoint = result.Value;
if (tempPoint != m_EndPoint) m_EndPoint = tempPoint;
else return SamplerStatus.NoChange;
if (result.Status == PromptStatus.Cancel) return SamplerStatus.Cancel;
else return SamplerStatus.OK;
}
/// <summary>
/// (動態/即時/實時)更新顯示/動態顯示
/// </summary>
/// <returns></returns>
protected override bool Update()
{
editor.WriteMessage("\n 執行Update;");
try
{
((Line)Entity).StartPoint = m_StartPoint;
((Line)Entity).EndPoint = m_EndPoint;
editor.WriteMessage($"\n m_EndPoint當前值:{m_EndPoint}");
}
catch (System.Exception)
{
return false;
}
return true;
}
/// <summary>
/// 獲取實體,便於外部調用
/// </summary>
/// <returns></returns>
public Entity GetEntity()
{ return Entity; }
}
}
運行結果

列出實體信息
[CommandMethod("listEntity")]
public void ListEntity()
{
Database db = Application.DocumentManager.MdiActiveDocument.Database;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
ed.WriteMessage($"\ndb.BlockTableId:{db.BlockTableId}");
ed.WriteMessage($"\nBlockTableRecord.ModelSpace:{BlockTableRecord.ModelSpace}");
ed.WriteMessage($"\nbt[BlockTableRecord.ModelSpace]:{bt[BlockTableRecord.ModelSpace]}");
ed.WriteMessage($"\n BlockBeginId:{btr.BlockBeginId}");
ed.WriteMessage($"\n BlockEndId:{btr.BlockEndId}");
ed.WriteMessage($"\n btr.Name:{btr.Name}");
ed.WriteMessage($"\n btr.PathName:{btr.PathName}");
ed.WriteMessage($"\n btr.Comments:{btr.Comments}");
ed.WriteMessage($"\n --------------------------------");
int count = 0;
foreach (ObjectId entityId in btr)
{
ed.WriteMessage($"\n 這是模型空間的第{count + 1}個實體,詳細信息如下:");
ed.WriteMessage($"\nName:{entityId.ObjectClass.Name}");
ed.WriteMessage($"\nAppName:{entityId.ObjectClass.AppName}");
ed.WriteMessage($"\nDxfName:{entityId.ObjectClass.DxfName}");
count++;
}
ed.WriteMessage($"\n塊表-模型空間-實體個數:{count}");
}
}
運行結果

