用NET写一个程序在CAD中选取2点把这2点的区域图案打印成图片
时间: 2024-05-16 12:14:20 浏览: 169
用net做的打印程序
这个任务需要使用CAD的API来实现。以下是一个基本的程序框架:
```csharp
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.IO;
namespace PrintCADRegion
{
public class Commands
{
[CommandMethod("PrintCADRegion")]
public void PrintCADRegion()
{
// 获取当前文档和编辑器对象
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
// 提示用户选择两个点
PromptPointOptions ppo1 = new PromptPointOptions("\n请选择第一个点:");
PromptPointResult ppr1 = ed.GetPoint(ppo1);
if (ppr1.Status != PromptStatus.OK)
return;
Point3d pt1 = ppr1.Value;
PromptPointOptions ppo2 = new PromptPointOptions("\n请选择第二个点:");
ppo2.BasePoint = pt1;
PromptPointResult ppr2 = ed.GetPoint(ppo2);
if (ppr2.Status != PromptStatus.OK)
return;
Point3d pt2 = ppr2.Value;
// 创建一个矩形框选区域
Point2d minPt = new Point2d(Math.Min(pt1.X, pt2.X), Math.Min(pt1.Y, pt2.Y));
Point2d maxPt = new Point2d(Math.Max(pt1.X, pt2.X), Math.Max(pt1.Y, pt2.Y));
Extents2d ext = new Extents2d(minPt, maxPt);
// 获取当前布局的ObjectId
LayoutManager lm = LayoutManager.Current;
Layout lo = lm.GetLayoutId(lm.CurrentLayout).GetObject(OpenMode.ForRead) as Layout;
// 创建一个视口对象,设置视口范围和比例
Viewport vp = new Viewport();
vp.Width = lo.PlotPaperSize.X;
vp.Height = lo.PlotPaperSize.Y;
vp.ViewCenter = ext.MinPoint + (ext.MaxPoint - ext.MinPoint) / 2.0;
vp.ViewHeight = Math.Max(ext.MaxPoint.Y - ext.MinPoint.Y, ext.MaxPoint.X - ext.MinPoint.X) * 1.2;
vp.CustomScale = lo.PlotScale;
// 打开当前布局的块表,创建一个块参照对象
BlockTable bt = doc.Database.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = bt[BlockTableRecord.PaperSpace].GetObject(OpenMode.ForWrite) as BlockTableRecord;
ObjectId vpId = btr.AppendEntity(vp);
doc.TransactionManager.AddNewlyCreatedDBObject(vp, true);
// 将视口设置为当前视口,并转换为模型空间坐标系
ed.SetCurrentViewportId(vpId);
Matrix3d mat = ed.CurrentUserCoordinateSystem;
mat = mat.Inverse();
vp.TransformBy(mat);
// 打印当前视口
PlotInfo pi = new PlotInfo();
pi.Layout = lo.ObjectId;
PlotSettings ps = new PlotSettings(lo.ModelType);
ps.CopyFrom(lo);
ps.CenterPlot = true;
ps.PrintViewportBorders = false;
ps.PrintPlotStyles = true;
ps.ScaleLineweights = true;
pi.OverrideSettings = ps;
PlotInfoValidator piv = new PlotInfoValidator();
piv.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
piv.Validate(pi);
// 执行打印操作,并保存为图片文件
PlotEngine pe = PlotFactory.CreatePublishEngine();
pe.BeginPlot(null, null);
pe.BeginDocument(pi, doc.Name, null, 1, true, null);
pe.BeginPage(pi, null);
pe.BeginGenerateGraphics(null);
pe.EndGenerateGraphics(null);
pe.EndPage(null);
pe.EndDocument(null);
pe.EndPlot(null);
string fileName = Path.Combine(Path.GetTempPath(), "CADRegion.png");
ps.ShadePlot = PlotSettingsShadePlotType.Shaded;
ps.ShadePlotResLevel = ShadePlotResLevel.Normal;
ps.ShadePlotCustomDPI = 300;
using (var bmp = new System.Drawing.Bitmap((int)vp.Width, (int)vp.Height))
{
using (var g = System.Drawing.Graphics.FromImage(bmp))
{
pe.BeginDocument(pi, doc.Name, null, 1, true, null);
pe.BeginPage(pi, null);
pe.BeginGenerateGraphics(null);
pe.EndGenerateGraphics(null);
pe.EndPage(null);
pe.EndDocument(null);
g.DrawImage(pe.GetPreviewBitmap(pi), 0, 0);
bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
}
}
// 删除视口对象
vp.Erase();
}
}
}
```
代码中的注释已经解释了每一步的作用。你可以将这段代码保存为一个C#文件,然后在Visual Studio中编译成一个.NET程序集。然后在CAD中通过NETLOAD命令加载这个程序集,就可以使用PRINTCADREGION命令来打印选定区域的图案了。
阅读全文