1.将当前文档中的图纸截图输出:
/// <summary>
/// 图纸输出图片
/// </summary>
[CommandMethod("SCPRINT")]
static public void CreatePreviewImage()
{
var doc = Application.DocumentManager.MdiActiveDocument;
if (doc == null) return;
var ed = doc.Editor;
// Select the filename and type for our output image
var pofo = new PromptSaveFileOptions("\nSelect image location");
pofo.Filter =
"Bitmap (*.bmp)|*.bmp|GIF (*.gif)|*.gif|JPEG (*.jpg)|*.jpg|" +
"PNG (*.png)|*.png|TIFF (*.tif)|*.tif";
// Set the default save location to be that of the current drawing
string fn = doc.Database.Filename;
if (fn.Contains("."))
{
int extIdx = fn.LastIndexOf(".");
if (fn.Substring(extIdx + 1) != "dwt" && fn.Contains("\\"))
{
pofo.InitialDirectory = Path.GetDirectoryName(doc.Database.Filename);
}
}
var pfnr = ed.GetFileNameForSave(pofo);
if (pfnr.Status != PromptStatus.OK)
return;
var outFile = pfnr.StringResult;
// Get the size of the document and capture the preview at that size
var size = doc.Window.DeviceIndependentSize;//.DeviceIndependentSize;
using (var bmp =doc.CapturePreviewImage(Convert.ToUInt32(size.Width), Convert.ToUInt32(size.Height)))
{
// Save the file with the format derived from the filename
bmp.Save(outFile, outFile.GetFormat());
}
}
2.自动打开本地dwg文档
/// <summary>
/// 打开本地的dwg文件,实测有效
/// </summary>
[CommandMethod("DwgFileOpen")]
public void ReadEntitiesFromDWG()
{
//Application.DocumentManager.AppContextOpenDocument(@"C:\Users\c-yuanp01\Documents\Drawdding1.dwg");
string fileName = @"C:\Users\yuanp\Documents\Drawdding1.dwg";
try
{
//Document doc = Application.DocumentManager.MdiActiveDocument;
//doc.CloseAndDiscard();
Document ndoc = Application.DocumentManager.Open(fileName, true, "");
Application.DocumentManager.MdiActiveDocument = ndoc;
}
catch (System.Exception ex)
{
Console.WriteLine("Error opening file: " + ex.Message);
}
}
3.画一个圆
/// <summary>
/// 画一个圆
/// </summary>
[CommandMethod("DrawCircle")]
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();
}