using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
namespace MyFirstProject
{
public class Class1
{
[CommandMethod("HelloNet")]
public void HelloNet()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("使用NET開發AutoCAD 程序bygisoracle");
}
[CommandMethod("PickPoint")]
public void PickPoint()
{
//獲取Editor 對象
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptPointOptions promptPtOp = new PromptPointOptions("選擇一個點:");
//指定的基點,如果指定了該點,則在選擇的時候繪制一條橡皮線。
promptPtOp.BasePoint = new Autodesk.AutoCAD.Geometry.Point3d(0, 0, 0);
PromptPointResult resPt;
resPt = ed.GetPoint(promptPtOp);
if (resPt.Status == PromptStatus.OK)
{
ed.WriteMessage(" 選擇的點為: " + resPt.Value.ToString());
}
}
[CommandMethod("createCircle")]
public void createCircle()
{
//首先聲明我們要使用的對象
Circle circle; //這個是我們要加入到模型空間的圓
BlockTableRecord btr;//要加入圓,我們必須打開模型空間
BlockTable bt; //要打開模型空間,我們必須通過塊表(BlockTable)來訪問它
//我們使用一個名為‘Transaction’的對象,把函數中有關數據庫的操作封裝起來
Transaction trans;
//使用TransactionManager的StartTransaction()成員來開始事務處理
trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
//現在創建圓……請仔細看這些參數——注意創建Point3d對象的‘New’和Vector3d的靜態成員ZAxis
circle = new Circle(new Point3d(10, 10, 0), Vector3d.ZAxis, 2);
bt = (BlockTable)trans.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead);
//使用當前的空間Id來獲取塊表記錄——注意我們是打開它用來寫入
btr = (BlockTableRecord)trans.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId, OpenMode.ForWrite);
//現在使用btr對象來加入圓
btr.AppendEntity(circle);
trans.AddNewlyCreatedDBObject(circle, true); //並確定事務處理知道要加入圓!
//一旦完成以上操作,我們就提交事務處理,這樣以上所做的改變就被保存了……
trans.Commit();
//…然后銷毀事務處理,因為我們已經完成了相關的操作(事務處理不是數據庫駐留對象,可以銷毀)
trans.Dispose();
}
[CommandMethod("SelectAPoint")]
public void SelectAPoint()
{
//實例化一個 PromptPointOptions類用來設置提示字符串和其他的一些控制提示
PromptPointOptions prPointOptions = new PromptPointOptions("Select a point");
PromptPointResult prPointRes;
// 實例化一個Editor類,使用GetPoint方法返回
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
prPointRes = ed.GetPoint(prPointOptions);
if (prPointRes.Status != PromptStatus.OK)
{
ed.WriteMessage("Error");
}
else
{
ed.WriteMessage("選擇的點為:" + prPointRes.Value.ToString());
}
}
[CommandMethod("getDistance")]
public void GetDistance()
{
PromptDistanceOptions prDistOptions = new
PromptDistanceOptions("計算兩點距離,請選擇第一個點:");
PromptDoubleResult prDistRes;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
prDistRes = ed.GetDistance(prDistOptions);
if (prDistRes.Status != PromptStatus.OK)
{
ed.WriteMessage("選擇錯誤!");
}
else
{
ed.WriteMessage("兩點的距離為:" + prDistRes.Value.ToString());
}
}
[CommandMethod("AddPointAndSetPointStyle")]
public static void AddPointAndSetPointStyle()
{
// 獲得當前文檔和數據庫 Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// 啟動一個事務 Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// 以只讀方式打開塊表 Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// 以寫方式打開模型空間塊表記錄 Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
// 在模型空間中創建一個坐標為(4,3,0)的點 Create a point at (4, 3, 0) in Model space
for (int i = 0; i < 100; i++)
{
DBPoint acPoint = new DBPoint(new Point3d(4*i, 3, 0));
acPoint.SetDatabaseDefaults();
// 添加新對象到塊表記錄和事務中 Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acPoint);
acTrans.AddNewlyCreatedDBObject(acPoint, true);
}
// 在圖形中設置所有點對象的樣式 Set the style for all point objects in the drawing
acCurDb.Pdmode = 34;
acCurDb.Pdsize = 1;
// 保存新對象到數據庫中 Save the new object to the database
acTrans.Commit();
}
}
[CommandMethod("Add2DSolid")]
public static void Add2DSolid()
{
// 獲得當前文檔和數據庫 Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// 啟動一個事務 Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// 以只讀方式打開塊表 Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// 以寫方式打開模型空間塊表記錄 Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
// Create a quadrilateral (bow-tie) solid in Model space
Solid ac2DSolidBow = new Solid(new Point3d(0, 0, 0),
new Point3d(5, 0, 0),
new Point3d(5, 8, 0),
new Point3d(0, 8, 0));
ac2DSolidBow.SetDatabaseDefaults();
// 添加新對象到塊表記錄和事務中 Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(ac2DSolidBow);
acTrans.AddNewlyCreatedDBObject(ac2DSolidBow, true);
// Create a quadrilateral (square) solid in Model space
Solid ac2DSolidSqr = new Solid(new Point3d(10, 0, 0),
new Point3d(15, 0, 0),
new Point3d(10, 8, 0),
new Point3d(15, 8, 0));
ac2DSolidSqr.SetDatabaseDefaults();
// 添加新對象到塊表記錄和事務中 Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(ac2DSolidSqr);
acTrans.AddNewlyCreatedDBObject(ac2DSolidSqr, true);
// 保存新對象到數據庫中 Save the new object to the database
acTrans.Commit();
}
}
[CommandMethod("AddLine")]
public static void AddLine()
{
// 獲得當前文檔和數據庫 Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// 啟動一個事務 Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// 以只讀方式打開塊表 Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// 以寫方式打開模型空間塊表記錄 Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
// 創建一條起點為(5,5,0),終點為(12,3,0)的直線 Create a line that starts at 5,5 and ends at 12,3
Line acLine = new Line(new Point3d(5, 5, 0),
new Point3d(12, 3, 0));
acLine.SetDatabaseDefaults();
// 添加新對象到塊表記錄和事務中 Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acLine);
acTrans.AddNewlyCreatedDBObject(acLine, true);
// 保存新對象到數據庫中 Save the new object to the database
acTrans.Commit();
}
}
}
}