插入光栅图像,定义并插入块,常见具有属性的块,附加拆离外部参考,附加扩展数据,查看扩展数据,定义布局,创建布局视口,将图形打印到PDF文件
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;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.PlottingServices;
namespace AutoDemo23
{
public class Class1
{
[CommandMethod("CRTX")]
public static void CRTX()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tran = db.TransactionManager.StartTransaction())
{
string strImgName = "世界地图";
string strFileName = "D:\\世界地图.tif";
ObjectId zdID = RasterImageDef.GetImageDictionary(db);
if (zdID.IsNull == true)
{
zdID = RasterImageDef.CreateImageDictionary(db);
}
DBDictionary imgDic = tran.GetObject(zdID, OpenMode.ForRead) as DBDictionary;
RasterImageDef imgDef;
ObjectId imgDefId;
if (imgDic.Contains(strImgName))
{
imgDefId = imgDic.GetAt(strImgName);
imgDef = tran.GetObject(imgDefId, OpenMode.ForWrite) as RasterImageDef;
}
else
{
RasterImageDef newImgDef = new RasterImageDef();
newImgDef.SourceFileName = strFileName;
newImgDef.Load();
tran.GetObject(zdID, OpenMode.ForWrite);
imgDefId = imgDic.SetAt(strImgName, newImgDef);
tran.AddNewlyCreatedDBObject(newImgDef, true);
imgDef = newImgDef;
}
BlockTable bt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tran.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
RasterImage img = new RasterImage();
img.ImageDefId = imgDefId;
Vector3d with = new Vector3d(10, 0, 0);
Vector3d height = new Vector3d(0, 5, 0);
Point3d wz = new Point3d(0, 0, 0);
CoordinateSystem3d cs = new CoordinateSystem3d(wz, with, height);
img.Orientation = cs;
btr.AppendEntity(img);
tran.AddNewlyCreatedDBObject(img, true);
img.AssociateRasterDef(imgDef);
tran.Commit();
}
}
[CommandMethod("CRK")]
public static void CRK()
{
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;
ObjectId blockID = ObjectId.Null;
if (bt.Has("我的块") == false)
{
BlockTableRecord block = new BlockTableRecord();
block.Name = "我的块";
block.Origin = new Point3d(0, 0, 0);
Circle c = new Circle();
c.Center = new Point3d(0, 0, 0);
c.Radius = 1;
block.AppendEntity(c);
Line L = new Line(new Point3d(-1, 0, 0), new Point3d(1, 0, 0));
block.AppendEntity(L);
tran.GetObject(db.BlockTableId, OpenMode.ForWrite);
bt.Add(block);
tran.AddNewlyCreatedDBObject(block, true);
blockID = block.ObjectId;
}
else
{
blockID = bt["我的块"];
}
if (blockID != null)
{
BlockReference blkR = new BlockReference(new Point3d(5, 5, 0), blockID);
BlockTableRecord btrC = tran.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
btrC.AppendEntity(blkR);
tran.AddNewlyCreatedDBObject(blkR, true);
}
tran.Commit();
}
}
[CommandMethod("SXK")]
public static void SXK()
{
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;
if (bt.Has("属性块") == false)
{
BlockTableRecord sxk = new BlockTableRecord();
sxk.Name = "属性块";
sxk.Origin = new Point3d(0, 0, 0);
sxk.Units = UnitsValue.Millimeters;
Circle c = new Circle();
c.Center = new Point3d(0, 0, 0);
c.Radius = 1;
sxk.AppendEntity(c);
AttributeDefinition ad = new AttributeDefinition();
ad.Position = new Point3d(0, 0, 0);
ad.Verifiable = true;
ad.Prompt = "输入名称";
ad.Tag = "名称";
ad.TextString = "张三";
ad.Height = 0.5;
ad.Justify = AttachmentPoint.MiddleCenter;
sxk.AppendEntity(ad);
tran.GetObject(db.BlockTableId, OpenMode.ForWrite);
bt.Add(sxk);
tran.AddNewlyCreatedDBObject(sxk, true);
tran.Commit();
}
}
}
[CommandMethod("FZCK")]
public static void FZCK()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tran = db.TransactionManager.StartTransaction())
{
string path = "D:\\测试.dwg";
ObjectId FJId;
FJId = db.AttachXref(path, "测试");
if (FJId.IsNull == false)
{
Point3d p = new Point3d(5, 5, 0);
BlockReference bf = new BlockReference(p, FJId);
BlockTableRecord btrC = tran.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
btrC.AppendEntity(bf);
tran.AddNewlyCreatedDBObject(bf, true);
}
tran.Commit();
}
}
[CommandMethod("FLCK")]
public static void FLCK()
{
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;
ObjectId ckID;
ckID = bt["测试"];
if (ckID.IsNull == false)
{
db.DetachXref(ckID);
}
tran.Commit();
}
}
[CommandMethod("FJKZ")]
public static void FJKZ()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
string appName = "测试";
using (Transaction tran = db.TransactionManager.StartTransaction())
{
PromptSelectionResult psr = doc.Editor.GetSelection();
if (psr.Status == PromptStatus.OK)
{
RegAppTable art = tran.GetObject(db.RegAppTableId, OpenMode.ForRead) as RegAppTable;
if (art.Has(appName) == false)
{
RegAppTableRecord artr = new RegAppTableRecord();
artr.Name = appName;
tran.GetObject(db.RegAppTableId, OpenMode.ForWrite);
art.Add(artr);
tran.AddNewlyCreatedDBObject(artr, true);
}
SelectionSet ss = psr.Value;
foreach (SelectedObject so in ss)
{
Entity ent = tran.GetObject(so.ObjectId, OpenMode.ForWrite) as Entity;
ResultBuffer rb = new ResultBuffer();
rb.Add(new TypedValue((int)DxfCode.ExtendedDataRegAppName, appName));
string kzsj = "图形的类型是:" + ent.GetType().Name;
rb.Add(new TypedValue((int)DxfCode.ExtendedDataAsciiString, kzsj));
ent.XData = rb;
}
}
tran.Commit();
}
}
[CommandMethod("CKKZ")]
public static void CKKZ()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
string appName = "测试";
using (Transaction tran = db.TransactionManager.StartTransaction())
{
PromptSelectionResult psr = doc.Editor.GetSelection();
if (psr.Status == PromptStatus.OK)
{
SelectionSet ss = psr.Value;
foreach (SelectedObject so in ss)
{
Entity ent = tran.GetObject(so.ObjectId, OpenMode.ForRead) as Entity;
ResultBuffer rb = ent.GetXDataForApplication(appName);
string kzsj;
kzsj = "";
foreach (TypedValue tv in rb)
{
kzsj = kzsj + "\n" + tv.TypeCode.ToString() + ":" + tv.Value;
}
Application.ShowAlertDialog(kzsj);
kzsj = "";
}
}
tran.Abort();
}
}
[CommandMethod("DYBJ")]
public static void DYBJ()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tran = db.TransactionManager.StartTransaction())
{
LayoutManager Lm = LayoutManager.Current;
ObjectId bjId = Lm.CreateLayout("测试");
Layout Lt = tran.GetObject(bjId, OpenMode.ForRead) as Layout;
if (Lt.TabSelected == false)
{
Lm.CurrentLayout = Lt.LayoutName;
}
PlotSettings ps = new PlotSettings(Lt.ModelType);
ps.CopyFrom(Lt);
PlotSettingsValidator psv = PlotSettingsValidator.Current;
psv.SetPlotConfigurationName(ps, "DWG To PDF.pc3", "ISO_A2_(594.00_x_420.00_mm)");
psv.SetZoomToPaperOnUpdate(ps, true);
tran.GetObject(bjId, OpenMode.ForWrite);
Lt.CopyFrom(ps);
tran.Commit();
doc.Editor.Regen();
}
}
[CommandMethod("BJSK")]
public static void BJSK()
{
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.PaperSpace], OpenMode.ForWrite) as BlockTableRecord;
Application.SetSystemVariable("TILEMODE", 0);
doc.Editor.SwitchToPaperSpace();
Viewport vp = new Viewport();
vp.CenterPoint = new Autodesk.AutoCAD.Geometry.Point3d(1.5, 1.5, 0);
vp.Width = 3;
vp.Height = 3;
btr.AppendEntity(vp);
tran.AddNewlyCreatedDBObject(vp, true);
vp.ViewDirection = new Autodesk.AutoCAD.Geometry.Vector3d(0, 0, 1);
vp.On = true;
doc.Editor.SwitchToModelSpace();
tran.Commit();
}
}
[CommandMethod("HTDY")]
public static void HTDY()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tran = db.TransactionManager.StartTransaction())
{
LayoutManager LM = LayoutManager.Current;
Layout Lt = tran.GetObject(LM.GetLayoutId(LM.CurrentLayout), OpenMode.ForRead) as Layout;
using (PlotInfo PI = new PlotInfo())
{
PI.Layout = Lt.ObjectId;
using (PlotInfoValidator PIV = new PlotInfoValidator())
{
PIV.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
PIV.Validate(PI);
if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
{
using (PlotEngine PE = PlotFactory.CreatePublishEngine())
{
PlotProgressDialog PPD = new PlotProgressDialog(false, 1, true);
using ((PPD))
{
PPD.set_PlotMsgString(PlotMessageIndex.DialogTitle, "打印进度显示");
PPD.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage, "取消工作");
PPD.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage, "取消工作表");
PPD.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "工作表进度");
PPD.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "打印进度");
PPD.LowerPlotProgressRange = 0;
PPD.UpperPlotProgressRange = 100;
PPD.PlotProgressPos = 0;
PPD.OnBeginPlot();
PPD.IsVisible = true;
PE.BeginPlot(PPD, null);
PE.BeginDocument(PI, doc.Name, null, 1, true, "c:\\测试");
PPD.set_PlotMsgString(PlotMessageIndex.Status, "绘图:" + doc.Name);
PPD.OnBeginSheet();
PPD.LowerSheetProgressRange = 0;
PPD.UpperSheetProgressRange = 100;
PPD.SheetProgressPos = 0;
using (PlotPageInfo PPI = new PlotPageInfo())
{
PE.BeginPage(PPI, PI, true, null);
}
PE.BeginGenerateGraphics(null);
PE.EndGenerateGraphics(null);
PE.EndPage(null);
PPD.SheetProgressPos = 100;
PPD.OnEndSheet();
PE.EndDocument(null);
PPD.PlotProgressPos = 100;
PPD.OnEndPlot();
PE.EndPlot(null);
}
}
}
}
}
}
}
}
}