CAD二次开发 学习笔记(2)
CAD二次开发 学习笔记(2)
创建浮动视口
/// <summary>
/// 创建浮动视口,并将新视口切换为当前视口
/// </summary>
[CommandMethod("CreatFloatingViewport")]
public void CreatFloatingViewport()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
//获取块表和PaperSpace的块表记录;
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.PaperSpace], OpenMode.ForWrite) as BlockTableRecord;
//切换到Paper布局空间
Application.SetSystemVariable("TILEMODE", 0);
//ed.SwitchToPaperSpace();
//创建视口Viewport
Viewport viewport = new Viewport()
{
CenterPoint = new Point3d(100, 100, 0),
Width = 200,
Height = 50,
//ViewDirection = new Vector3d(1, 1, 1),
//On = true,
};
btr.AppendEntity(viewport);
tr.AddNewlyCreatedDBObject(viewport, true);
viewport.ViewDirection = new Vector3d(1, 1, 1);
viewport.On = true;
//切换到模型空间
ed.SwitchToModelSpace();
//使用导入的ObjectARX函数,设置新视口为当前视口
//acedSetCurrentVPort(viewport.UnmanagedObject);
tr.Commit();
}
}
运行结果
打印测试
/// <summary>
/// 打印当前布局的测试
/// </summary>
[CommandMethod("PlotCurrentLayout")]
public void PlotCurrentLayout()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
//获取当前布局管理器Layout Manager
LayoutManager manager = LayoutManager.Current;
//从布局管理器LayoutManager获取布局
Layout layout = tr.GetObject(manager.GetLayoutId(manager.CurrentLayout), OpenMode.ForRead) as Layout;
//从Layout获取PlotInfo
PlotInfo info = new PlotInfo();
info.Layout = layout.ObjectId;
//复制Layout中的PlotSettings
PlotSettings settings = new PlotSettings(layout.ModelType);
settings.CopyFrom(layout);
//获取当前设置检验器,PlotSettingsValidator
PlotSettingsValidator settingsValidator = PlotSettingsValidator.Current;
//设置打印区域SetPlotType
settingsValidator.SetPlotType(settings, Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);
//设置缩放比例
settingsValidator.SetUseStandardScale(settings, true);
settingsValidator.SetStdScaleType(settings, StdScaleType.ScaleToFit);
//设置居中打印
settingsValidator.SetPlotCentered(settings, true);
//设置打印设备名称PlotConfigurationName
settingsValidator.SetPlotConfigurationName(settings, "DWG To PDF.pc3", "ANSI_A_(8.50_x_11.00_Inches)");
//用上述设置信息 覆盖Plot Info
//不会将修改保存到布局Layout
info.OverrideSettings = settings;
//验证打印信息PlotInfo
PlotInfoValidator infoValidator = new PlotInfoValidator();
infoValidator.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
infoValidator.Validate(info);
//检查当前是否有打印任务正在执行
if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
{
//使用打印对话框PlotProgressDialog显示打印状态
using (PlotEngine engine = PlotFactory.CreatePublishEngine())
{
PlotProgressDialog dialog = new PlotProgressDialog(false, 1, true);
using (dialog)
{
//定义打印开始时的显示信息
dialog.set_PlotMsgString(PlotMessageIndex.DialogTitle, "打印进度");
dialog.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage, "取消任务");
dialog.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage, "CancelSheet");
dialog.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "SheetSetProgress");
dialog.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "SheetProgress");
//设置打印进度条的范围和起始位置
dialog.LowerPlotProgressRange = 0;
dialog.UpperPlotProgressRange = 100;
dialog.PlotProgressPos = 0;
//显示打印对话框
dialog.OnBeginPlot();
dialog.IsVisible = true;
//开始打印
engine.BeginPlot(dialog, null);
//定义打印输出-需要完整的路径和文件名
engine.BeginDocument(info, doc.Name, null, 1, true, @"C:\Users\Administrator\Desktop\myFirtPlot.pdf");
//显示当前打印任务信息
dialog.set_PlotMsgString(PlotMessageIndex.Status, $"打印:{doc.Name}-{layout.LayoutName}");
//设置图纸进度范围
dialog.OnBeginSheet();
//设置图纸进度范围
dialog.LowerSheetProgressRange = 0;
dialog.UpperSheetProgressRange = 100;
dialog.SheetProgressPos = 0;
//打印第一张图纸/布局
PlotPageInfo pageInfo = new PlotPageInfo();
engine.BeginPage(pageInfo, info, true, null);
engine.BeginGenerateGraphics(null);
engine.EndGenerateGraphics(null);
//结束第一张图纸/布局的打印
engine.EndPage(null);
dialog.SheetProgressPos = 100;
dialog.OnEndSheet();
//结束文档打印
engine.EndDocument(null);
//打印结束
dialog.PlotProgressPos = 100;
dialog.OnEndPlot();
engine.EndPlot(null);
}
}
}
}
}
运行结果
切换布局空间 模型空间-图纸空间
/// <summary>
/// 切换布局空间ModelSpace--PaperSpace
/// </summary>
[CommandMethod("ToggleSpace")]
public void ToggleSpace()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
//获取系统变量
var cvPort = Application.GetSystemVariable("CVPORT");
var tileMode = Application.GetSystemVariable("TILEMODE");
//TILEMODE为1,说明Model布局是活动的
if (Convert.ToInt16(tileMode) == 0)
{
if (Convert.ToInt16(cvPort) == 2)
ed.SwitchToPaperSpace();
else
ed.SwitchToModelSpace();
}
else
{
Application.SetSystemVariable("TILEMODE", 0);
}
}
获取打印相关信息的测试
/// <summary>
/// 获取打印相关信息的测试
/// </summary>
[CommandMethod("GetPlotInfo")]
public void GetPlotInfo()
{
ChangePlotSettings();
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Database db = HostApplicationServices.WorkingDatabase;
//获取所有打印机信息
ed.WriteMessage("\n ---------------GetPlotDeviceList-----------------");
PlotSettingsValidator validator = PlotSettingsValidator.Current;
StringCollection deviceCollection = validator.GetPlotDeviceList();
foreach (var item in deviceCollection)
{
ed.WriteMessage("\n PlotDevice: " + item);
}
//获取所有打印样式表信息
ed.WriteMessage("\n ---------------PlotStyleSheet-----------------");
StringCollection plotStyleCollection = validator.GetPlotStyleSheetList();
foreach (var item in plotStyleCollection)
{
ed.WriteMessage("\n PlotStyleSheet: " + item);
}
ed.WriteMessage("\n -------------PlotSettingsValidator-------------------");
ed.WriteMessage("\n LifetimeService:" + validator.GetLifetimeService());
ed.WriteMessage("\n GetHashCode: " + validator.GetHashCode());
ed.WriteMessage("\n --------------LayoutManager------------------");
LayoutManager manager = LayoutManager.Current;
ed.WriteMessage("\n LayoutCount: " + manager.LayoutCount);
ed.WriteMessage("\n CurrentLayout: " + manager.CurrentLayout);
ed.WriteMessage("\n --------------------------------");
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Layout layout = tr.GetObject(manager.GetLayoutId(manager.CurrentLayout), OpenMode.ForRead) as Layout;
PlotSettings settings = new PlotSettings(layout.ModelType);
//获取所有纸张信息,共94个
for (int i = 0; i < 94; i++)
{
ed.WriteMessage($"\n LocaleMediaName{i}: " + validator.GetLocaleMediaName(settings, i));
}
//获取布局信息
ed.WriteMessage("\n -------------Layout-------------------");
ed.WriteMessage("\n LayoutName: " + layout.LayoutName);
ed.WriteMessage("\n Thumbnail: " + layout.Thumbnail);
ed.WriteMessage("\n Extents: " + layout.Extents);
ed.WriteMessage("\n Limits: " + layout.Limits);
ed.WriteMessage("\n TabOrder: " + layout.TabOrder);
ed.WriteMessage("\n ObjectId: " + layout.ObjectId);
//获取设置信息
ed.WriteMessage("\n -------------PlotSettings-------------------");
ed.WriteMessage("\n LayoutName: " + settings.ObjectBirthVersion);
ed.WriteMessage("\n ObjectId: " + settings.ObjectId);
ed.WriteMessage("\n OwnerId: " + settings.OwnerId);
ed.WriteMessage("\n PaperOrientation: " + settings.PaperOrientation);
ed.WriteMessage("\n PlotAsRaster: " + settings.PlotAsRaster);
ed.WriteMessage("\n PlotCentered: " + settings.PlotCentered);
ed.WriteMessage("\n PlotConfigurationName: " + settings.PlotConfigurationName);
ed.WriteMessage("\n PlotHidden: " + settings.PlotHidden);
ed.WriteMessage("\n PlotOrigin: " + settings.PlotOrigin);
ed.WriteMessage("\n PlotPaperMargins: " + settings.PlotPaperMargins);
ed.WriteMessage("\n PlotPaperSize: " + settings.PlotPaperSize);
ed.WriteMessage("\n PlotPaperUnits: " + settings.PlotPaperUnits);
ed.WriteMessage("\n PlotPlotStyles: " + settings.PlotPlotStyles);
ed.WriteMessage("\n PlotRotation: " + settings.PlotRotation);
ed.WriteMessage("\n PlotSettingsName: " + settings.PlotSettingsName);
ed.WriteMessage("\n PlotTransparency: " + settings.PlotTransparency);
ed.WriteMessage("\n PlotType: " + settings.PlotType);
ed.WriteMessage("\n PlotViewName: " + settings.PlotViewName);
ed.WriteMessage("\n PlotViewportBorders: " + settings.PlotViewportBorders);
ed.WriteMessage("\n PlotWindowArea: " + settings.PlotWindowArea);
ed.WriteMessage("\n PlotWireframe: " + settings.PlotWireframe);
ed.WriteMessage("\n PrintLineweights: " + settings.PrintLineweights);
}
}
运行结果
命令: NETLOAD
命令: GETPLOTINFO
LayoutName:Model
PlotConfigurationName:无
---------------GetPlotDeviceList-----------------
PlotDevice: 无
PlotDevice: Microsoft XPS Document Writer
PlotDevice: Microsoft Print to PDF
PlotDevice: MF110/910 Series
PlotDevice: Foxit PDF Reader Printer
PlotDevice: Fax
PlotDevice: AutoCAD PDF (General Documentation).pc3
PlotDevice: AutoCAD PDF (High Quality Print).pc3
PlotDevice: AutoCAD PDF (Smallest File).pc3
PlotDevice: AutoCAD PDF (Web and Mobile).pc3
PlotDevice: Default Windows System Printer.pc3
PlotDevice: DWF6 ePlot.pc3
PlotDevice: DWFx ePlot (XPS Compatible).pc3
PlotDevice: DWG To PDF.pc3
PlotDevice: PublishToWeb JPG.pc3
PlotDevice: PublishToWeb PNG.pc3
---------------PlotStyleSheet-----------------
PlotStyleSheet: acad.stb
PlotStyleSheet: Autodesk-Color.stb
PlotStyleSheet: Autodesk-MONO.stb
PlotStyleSheet: monochrome.stb
PlotStyleSheet: acad.ctb
PlotStyleSheet: DWF Virtual Pens.ctb
PlotStyleSheet: Fill Patterns.ctb
PlotStyleSheet: Grayscale.ctb
PlotStyleSheet: monochrome.ctb
PlotStyleSheet: Screening 100%.ctb
PlotStyleSheet: Screening 25%.ctb
PlotStyleSheet: Screening 50%.ctb
PlotStyleSheet: Screening 75%.ctb
-------------PlotSettingsValidator-------------------
LifetimeService:
GetHashCode: 860604160
--------------LayoutManager------------------
LayoutCount: 3
CurrentLayout: Model
--------------------------------
LocaleMediaName0: ISO full bleed B5 (250.00 x 176.00 毫米)
LocaleMediaName1: ISO full bleed B5 (176.00 x 250.00 毫米)
LocaleMediaName2: ISO full bleed B4 (353.00 x 250.00 毫米)
LocaleMediaName3: ISO full bleed B4 (250.00 x 353.00 毫米)
LocaleMediaName4: ISO full bleed B3 (500.00 x 353.00 毫米)
LocaleMediaName5: ISO full bleed B3 (353.00 x 500.00 毫米)
LocaleMediaName6: ISO full bleed B2 (707.00 x 500.00 毫米)
LocaleMediaName7: ISO full bleed B2 (500.00 x 707.00 毫米)
LocaleMediaName8: ISO full bleed B1 (1000.00 x 707.00 毫米)
LocaleMediaName9: ISO full bleed B1 (707.00 x 1000.00 毫米)
LocaleMediaName10: ISO full bleed B0 (1414.00 x 1000.00 毫米)
LocaleMediaName11: ISO full bleed B0 (1000.00 x 1414.00 毫米)
LocaleMediaName12: ISO full bleed A5 (210.00 x 148.00 毫米)
LocaleMediaName13: ISO full bleed A5 (148.00 x 210.00 毫米)
LocaleMediaName14: ISO full bleed 2A0 (1189.00 x 1682.00 毫米)
LocaleMediaName15: ISO full bleed 4A0 (1682.00 x 2378.00 毫米)
LocaleMediaName16: ISO full bleed A4 (297.00 x 210.00 毫米)
LocaleMediaName17: ISO full bleed A4 (210.00 x 297.00 毫米)
LocaleMediaName18: ISO full bleed A3 (420.00 x 297.00 毫米)
LocaleMediaName19: ISO full bleed A3 (297.00 x 420.00 毫米)
LocaleMediaName20: ISO full bleed A2 (594.00 x 420.00 毫米)
LocaleMediaName21: ISO full bleed A2 (420.00 x 594.00 毫米)
LocaleMediaName22: ISO full bleed A1 (841.00 x 594.00 毫米)
LocaleMediaName23: ISO full bleed A1 (594.00 x 841.00 毫米)
LocaleMediaName24: ISO full bleed A0 (841.00 x 1189.00 毫米)
LocaleMediaName25: ISO full bleed B1 (707.00 x 1000.00 毫米)
LocaleMediaName26: ARCH full bleed E1 (30.00 x 42.00 英寸)
LocaleMediaName27: ARCH full bleed E (36.00 x 48.00 英寸)
LocaleMediaName28: ARCH full bleed D (36.00 x 24.00 英寸)
LocaleMediaName29: ARCH full bleed D (24.00 x 36.00 英寸)
LocaleMediaName30: ARCH full bleed C (24.00 x 18.00 英寸)
LocaleMediaName31: ARCH full bleed C (18.00 x 24.00 英寸)
LocaleMediaName32: ARCH full bleed B (18.00 x 12.00 英寸)
LocaleMediaName33: ARCH full bleed B (12.00 x 18.00 英寸)
LocaleMediaName34: ARCH full bleed A (12.00 x 9.00 英寸)
LocaleMediaName35: ARCH full bleed A (9.00 x 12.00 英寸)
LocaleMediaName36: ANSI full bleed F (28.00 x 40.00 英寸)
LocaleMediaName37: ANSI full bleed E (34.00 x 44.00 英寸)
LocaleMediaName38: ANSI full bleed D (34.00 x 22.00 英寸)
LocaleMediaName39: ANSI full bleed D (22.00 x 34.00 英寸)
LocaleMediaName40: ANSI full bleed C (22.00 x 17.00 英寸)
LocaleMediaName41: ANSI full bleed C (17.00 x 22.00 英寸)
LocaleMediaName42: ANSI full bleed B (17.00 x 11.00 英寸)
LocaleMediaName43: ANSI full bleed B (11.00 x 17.00 英寸)
LocaleMediaName44: ANSI full bleed A (11.00 x 8.50 英寸)
LocaleMediaName45: ANSI full bleed A (8.50 x 11.00 英寸)
LocaleMediaName46: ISO expand A0 (841.00 x 1189.00 毫米)
LocaleMediaName47: ISO A0 (841.00 x 1189.00 毫米)
LocaleMediaName48: ISO expand A1 (841.00 x 594.00 毫米)
LocaleMediaName49: ISO expand A1 (594.00 x 841.00 毫米)
LocaleMediaName50: ISO A1 (841.00 x 594.00 毫米)
LocaleMediaName51: ISO A1 (594.00 x 841.00 毫米)
LocaleMediaName52: ISO expand A2 (594.00 x 420.00 毫米)
LocaleMediaName53: ISO expand A2 (420.00 x 594.00 毫米)
LocaleMediaName54: ISO A2 (594.00 x 420.00 毫米)
LocaleMediaName55: ISO A2 (420.00 x 594.00 毫米)
LocaleMediaName56: ISO expand A3 (420.00 x 297.00 毫米)
LocaleMediaName57: ISO expand A3 (297.00 x 420.00 毫米)
LocaleMediaName58: ISO A3 (420.00 x 297.00 毫米)
LocaleMediaName59: ISO A3 (297.00 x 420.00 毫米)
LocaleMediaName60: ISO expand A4 (297.00 x 210.00 毫米)
LocaleMediaName61: ISO expand A4 (210.00 x 297.00 毫米)
LocaleMediaName62: ISO A4 (297.00 x 210.00 毫米)
LocaleMediaName63: ISO A4 (210.00 x 297.00 毫米)
LocaleMediaName64: ARCH expand E1 (30.00 x 42.00 英寸)
LocaleMediaName65: ARCH E1 (30.00 x 42.00 英寸)
LocaleMediaName66: ARCH expand E (36.00 x 48.00 英寸)
LocaleMediaName67: ARCH E (36.00 x 48.00 英寸)
LocaleMediaName68: ARCH expand D (36.00 x 24.00 英寸)
LocaleMediaName69: ARCH expand D (24.00 x 36.00 英寸)
LocaleMediaName70: ARCH D (36.00 x 24.00 英寸)
LocaleMediaName71: ARCH D (24.00 x 36.00 英寸)
LocaleMediaName72: ARCH expand C (24.00 x 18.00 英寸)
LocaleMediaName73: ARCH expand C (18.00 x 24.00 英寸)
LocaleMediaName74: ARCH C (24.00 x 18.00 英寸)
LocaleMediaName75: ARCH C (18.00 x 24.00 英寸)
LocaleMediaName76: ANSI expand E (34.00 x 44.00 英寸)
LocaleMediaName77: ANSI E (34.00 x 44.00 英寸)
LocaleMediaName78: ANSI expand D (34.00 x 22.00 英寸)
LocaleMediaName79: ANSI expand D (22.00 x 34.00 英寸)
LocaleMediaName80: ANSI D (34.00 x 22.00 英寸)
LocaleMediaName81: ANSI D (22.00 x 34.00 英寸)
LocaleMediaName82: ANSI expand C (22.00 x 17.00 英寸)
LocaleMediaName83: ANSI expand C (17.00 x 22.00 英寸)
LocaleMediaName84: ANSI C (22.00 x 17.00 英寸)
LocaleMediaName85: ANSI C (17.00 x 22.00 英寸)
LocaleMediaName86: ANSI expand B (17.00 x 11.00 英寸)
LocaleMediaName87: ANSI expand B (11.00 x 17.00 英寸)
正在重生成模型。
AutoCAD 菜单实用工具 已加载。*取消*
命令:
命令:
命令:
命令: NETLOAD
命令: GETPLOTINFO
LayoutName:Model
PlotConfigurationName:无
---------------GetPlotDeviceList-----------------
PlotDevice: 无
PlotDevice: Microsoft XPS Document Writer
PlotDevice: Microsoft Print to PDF
PlotDevice: MF110/910 Series
PlotDevice: Foxit PDF Reader Printer
PlotDevice: Fax
PlotDevice: AutoCAD PDF (General Documentation).pc3
PlotDevice: AutoCAD PDF (High Quality Print).pc3
PlotDevice: AutoCAD PDF (Smallest File).pc3
PlotDevice: AutoCAD PDF (Web and Mobile).pc3
PlotDevice: Default Windows System Printer.pc3
PlotDevice: DWF6 ePlot.pc3
PlotDevice: DWFx ePlot (XPS Compatible).pc3
PlotDevice: DWG To PDF.pc3
PlotDevice: PublishToWeb JPG.pc3
PlotDevice: PublishToWeb PNG.pc3
---------------PlotStyleSheet-----------------
PlotStyleSheet: acad.stb
PlotStyleSheet: Autodesk-Color.stb
PlotStyleSheet: Autodesk-MONO.stb
PlotStyleSheet: monochrome.stb
PlotStyleSheet: acad.ctb
PlotStyleSheet: DWF Virtual Pens.ctb
PlotStyleSheet: Fill Patterns.ctb
PlotStyleSheet: Grayscale.ctb
PlotStyleSheet: monochrome.ctb
PlotStyleSheet: Screening 100%.ctb
PlotStyleSheet: Screening 25%.ctb
PlotStyleSheet: Screening 50%.ctb
PlotStyleSheet: Screening 75%.ctb
-------------PlotSettingsValidator-------------------
LifetimeService:
GetHashCode: 860604160
--------------LayoutManager------------------
LayoutCount: 3
CurrentLayout: Model
--------------------------------
LocaleMediaName0: ISO full bleed B5 (250.00 x 176.00 毫米)
LocaleMediaName1: ISO full bleed B5 (176.00 x 250.00 毫米)
LocaleMediaName2: ISO full bleed B4 (353.00 x 250.00 毫米)
LocaleMediaName3: ISO full bleed B4 (250.00 x 353.00 毫米)
LocaleMediaName4: ISO full bleed B3 (500.00 x 353.00 毫米)
LocaleMediaName5: ISO full bleed B3 (353.00 x 500.00 毫米)
LocaleMediaName6: ISO full bleed B2 (707.00 x 500.00 毫米)
LocaleMediaName7: ISO full bleed B2 (500.00 x 707.00 毫米)
LocaleMediaName8: ISO full bleed B1 (1000.00 x 707.00 毫米)
LocaleMediaName9: ISO full bleed B1 (707.00 x 1000.00 毫米)
LocaleMediaName10: ISO full bleed B0 (1414.00 x 1000.00 毫米)
LocaleMediaName11: ISO full bleed B0 (1000.00 x 1414.00 毫米)
LocaleMediaName12: ISO full bleed A5 (210.00 x 148.00 毫米)
LocaleMediaName13: ISO full bleed A5 (148.00 x 210.00 毫米)
LocaleMediaName14: ISO full bleed 2A0 (1189.00 x 1682.00 毫米)
LocaleMediaName15: ISO full bleed 4A0 (1682.00 x 2378.00 毫米)
LocaleMediaName16: ISO full bleed A4 (297.00 x 210.00 毫米)
LocaleMediaName17: ISO full bleed A4 (210.00 x 297.00 毫米)
LocaleMediaName18: ISO full bleed A3 (420.00 x 297.00 毫米)
LocaleMediaName19: ISO full bleed A3 (297.00 x 420.00 毫米)
LocaleMediaName20: ISO full bleed A2 (594.00 x 420.00 毫米)
LocaleMediaName21: ISO full bleed A2 (420.00 x 594.00 毫米)
LocaleMediaName22: ISO full bleed A1 (841.00 x 594.00 毫米)
LocaleMediaName23: ISO full bleed A1 (594.00 x 841.00 毫米)
LocaleMediaName24: ISO full bleed A0 (841.00 x 1189.00 毫米)
LocaleMediaName25: ISO full bleed B1 (707.00 x 1000.00 毫米)
LocaleMediaName26: ARCH full bleed E1 (30.00 x 42.00 英寸)
LocaleMediaName27: ARCH full bleed E (36.00 x 48.00 英寸)
LocaleMediaName28: ARCH full bleed D (36.00 x 24.00 英寸)
LocaleMediaName29: ARCH full bleed D (24.00 x 36.00 英寸)
LocaleMediaName30: ARCH full bleed C (24.00 x 18.00 英寸)
LocaleMediaName31: ARCH full bleed C (18.00 x 24.00 英寸)
LocaleMediaName32: ARCH full bleed B (18.00 x 12.00 英寸)
LocaleMediaName33: ARCH full bleed B (12.00 x 18.00 英寸)
LocaleMediaName34: ARCH full bleed A (12.00 x 9.00 英寸)
LocaleMediaName35: ARCH full bleed A (9.00 x 12.00 英寸)
LocaleMediaName36: ANSI full bleed F (28.00 x 40.00 英寸)
LocaleMediaName37: ANSI full bleed E (34.00 x 44.00 英寸)
LocaleMediaName38: ANSI full bleed D (34.00 x 22.00 英寸)
LocaleMediaName39: ANSI full bleed D (22.00 x 34.00 英寸)
LocaleMediaName40: ANSI full bleed C (22.00 x 17.00 英寸)
LocaleMediaName41: ANSI full bleed C (17.00 x 22.00 英寸)
LocaleMediaName42: ANSI full bleed B (17.00 x 11.00 英寸)
LocaleMediaName43: ANSI full bleed B (11.00 x 17.00 英寸)
LocaleMediaName44: ANSI full bleed A (11.00 x 8.50 英寸)
LocaleMediaName45: ANSI full bleed A (8.50 x 11.00 英寸)
LocaleMediaName46: ISO expand A0 (841.00 x 1189.00 毫米)
LocaleMediaName47: ISO A0 (841.00 x 1189.00 毫米)
LocaleMediaName48: ISO expand A1 (841.00 x 594.00 毫米)
LocaleMediaName49: ISO expand A1 (594.00 x 841.00 毫米)
LocaleMediaName50: ISO A1 (841.00 x 594.00 毫米)
LocaleMediaName51: ISO A1 (594.00 x 841.00 毫米)
LocaleMediaName52: ISO expand A2 (594.00 x 420.00 毫米)
LocaleMediaName53: ISO expand A2 (420.00 x 594.00 毫米)
LocaleMediaName54: ISO A2 (594.00 x 420.00 毫米)
LocaleMediaName55: ISO A2 (420.00 x 594.00 毫米)
LocaleMediaName56: ISO expand A3 (420.00 x 297.00 毫米)
LocaleMediaName57: ISO expand A3 (297.00 x 420.00 毫米)
LocaleMediaName58: ISO A3 (420.00 x 297.00 毫米)
LocaleMediaName59: ISO A3 (297.00 x 420.00 毫米)
LocaleMediaName60: ISO expand A4 (297.00 x 210.00 毫米)
LocaleMediaName61: ISO expand A4 (210.00 x 297.00 毫米)
LocaleMediaName62: ISO A4 (297.00 x 210.00 毫米)
LocaleMediaName63: ISO A4 (210.00 x 297.00 毫米)
LocaleMediaName64: ARCH expand E1 (30.00 x 42.00 英寸)
LocaleMediaName65: ARCH E1 (30.00 x 42.00 英寸)
LocaleMediaName66: ARCH expand E (36.00 x 48.00 英寸)
LocaleMediaName67: ARCH E (36.00 x 48.00 英寸)
LocaleMediaName68: ARCH expand D (36.00 x 24.00 英寸)
LocaleMediaName69: ARCH expand D (24.00 x 36.00 英寸)
LocaleMediaName70: ARCH D (36.00 x 24.00 英寸)
LocaleMediaName71: ARCH D (24.00 x 36.00 英寸)
LocaleMediaName72: ARCH expand C (24.00 x 18.00 英寸)
LocaleMediaName73: ARCH expand C (18.00 x 24.00 英寸)
LocaleMediaName74: ARCH C (24.00 x 18.00 英寸)
LocaleMediaName75: ARCH C (18.00 x 24.00 英寸)
LocaleMediaName76: ANSI expand E (34.00 x 44.00 英寸)
LocaleMediaName77: ANSI E (34.00 x 44.00 英寸)
LocaleMediaName78: ANSI expand D (34.00 x 22.00 英寸)
LocaleMediaName79: ANSI expand D (22.00 x 34.00 英寸)
LocaleMediaName80: ANSI D (34.00 x 22.00 英寸)
LocaleMediaName81: ANSI D (22.00 x 34.00 英寸)
LocaleMediaName82: ANSI expand C (22.00 x 17.00 英寸)
LocaleMediaName83: ANSI expand C (17.00 x 22.00 英寸)
LocaleMediaName84: ANSI C (22.00 x 17.00 英寸)
LocaleMediaName85: ANSI C (17.00 x 22.00 英寸)
LocaleMediaName86: ANSI expand B (17.00 x 11.00 英寸)
LocaleMediaName87: ANSI expand B (11.00 x 17.00 英寸)
LocaleMediaName88: ANSI B (17.00 x 11.00 英寸)
LocaleMediaName89: ANSI B (11.00 x 17.00 英寸)
LocaleMediaName90: ANSI expand A (11.00 x 8.50 英寸)
LocaleMediaName91: ANSI expand A (8.50 x 11.00 英寸)
LocaleMediaName92: ANSI A (11.00 x 8.50 英寸)
LocaleMediaName93: ANSI A (8.50 x 11.00 英寸)
-------------Layout-------------------
LayoutName: Model
Thumbnail: System.Drawing.Bitmap
Extents: ((0,0,0),(0,0,0))
Limits: ((0,0),(12,9))
TabOrder: 0
ObjectId: (3059947280928)
-------------PlotSettings-------------------
LayoutName: AC1500,Release42
ObjectId: (0)
OwnerId: (0)
PaperOrientation: NotApplicable
PlotAsRaster: False
PlotCentered: False
PlotConfigurationName: Microsoft Print to PDF
PlotHidden: False
PlotOrigin: (0,0)
PlotPaperMargins: ((0,0),(0,0))
PlotPaperSize: (0,0)
PlotPaperUnits: Inches
PlotPlotStyles: True
PlotRotation: Degrees000
PlotSettingsName:
PlotTransparency: False
PlotType: Display
PlotViewName:
PlotViewportBorders: False
PlotWindowArea: ((0,0),(0,0))
PlotWireframe: True
PrintLineweights: True
获取所有打印机
/// <summary>
/// 获取所有打印机
/// </summary>
[CommandMethod("GetPlotDevices")]
public void GetPlotDevices()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
IEnumerator enumerator = PlotConfigManager.Devices.GetEnumerator();
while (enumerator.MoveNext())
{
PlotConfigInfo info = enumerator.Current as PlotConfigInfo;
ed.WriteMessage("\nDeviceName" + info.DeviceName);
}
}
//命令: GETPLOTDEVICES
//DeviceName无
//DeviceNameMicrosoft XPS Document Writer
//DeviceNameMicrosoft Print to PDF
//DeviceNameMF110/910 Series
//DeviceNameFoxit PDF Reader Printer
//DeviceNameFax
//DeviceNameAutoCAD PDF(General Documentation).pc3
//DeviceNameAutoCAD PDF(High Quality Print).pc3
//DeviceNameAutoCAD PDF(Smallest File).pc3
//DeviceNameAutoCAD PDF(Web and Mobile).pc3
//DeviceNameDefault Windows System Printer.pc3
//DeviceNameDWF6 ePlot.pc3
//DeviceNameDWFx ePlot(XPS Compatible).pc3
//DeviceNameDWG To PDF.pc3
//DeviceNamePublishToWeb JPG.pc3
//DeviceNamePublishToWeb PNG.pc3
运行结果
更改打印设置
/// <summary>
/// 更改打印设置
/// </summary>
[CommandMethod("changePlotSettings")]
public void ChangePlotSettings()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
//布局管理器
LayoutManager manager = LayoutManager.Current;
Layout layout = tr.GetObject(manager.GetLayoutId(manager.CurrentLayout), OpenMode.ForRead) as Layout;
//布局名称
ed.WriteMessage("\nLayoutName:" + layout.LayoutName);
//当前打印设备名称
ed.WriteMessage("\nPlotConfigurationName:" + layout.PlotConfigurationName);
//从布局中获取打印信息
PlotInfo plotInfo = new PlotInfo();
plotInfo.Layout = layout.ObjectId;
//复制布局中的打印设置
PlotSettings plotSettings = new PlotSettings(layout.ModelType);
plotSettings.CopyFrom(layout);
//更新PlotSettings 对象的PlotConfigurationName属性
PlotSettingsValidator validator = PlotSettingsValidator.Current;
validator.SetPlotConfigurationName(plotSettings, "DWF6 ePlot.pc3", "ANSI_A_(8.50_x_11.00_Inches)");
//更新布局
layout.UpgradeOpen();
layout.CopyFrom(plotSettings);
//将对象保存到数据库
tr.Commit();
}
}
环形阵列3-矩形
/// <summary>
/// 环形阵列3-矩形
/// </summary>
[CommandMethod("PolorArryTest3")]
public void PolorArryTest3()
{
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 r = new Random();
Point2d ul = new Point2d(r.Next(0, 2000), r.Next(0, 500));
double width = r.Next(20, 30);
double height = r.Next(10, 20);
Point2d ur = new Point2d(ul.X + width, ul.Y);
Point2d lr = new Point2d(ul.X + width, ul.Y - height);
Point2d ll = new Point2d(ul.X, ul.Y - height);
Polyline polyline = new Polyline();
polyline.AddVertexAt(0, ul, 0, 0, 0);
polyline.AddVertexAt(0, ur, 0, 0, 0);
polyline.AddVertexAt(0, lr, 0, 0, 0);
polyline.AddVertexAt(0, ll, 0, 0, 0);
polyline.Closed = true;
polyline.ColorIndex = r.Next(1,10);
btr.AppendEntity(polyline);
tr.AddNewlyCreatedDBObject(polyline, true);
int count = r.Next(3, 6);
double angle = Math.PI / count;
Point2d center = ul;
double radius = r.Next(50, 100);
for (int i = 0; i < 2 * count; i++)
{
Polyline clone = polyline.Clone() as Polyline;
Point2d polorPoint = GetPolorPoint(center, angle * i, radius);
Vector2d v2 = center.GetVectorTo(polorPoint);
clone.TransformBy(Matrix3d.Displacement(new Vector3d(v2.X, v2.Y, 0)));
clone.TransformBy(Matrix3d.Rotation(angle * i, Vector3d.ZAxis, new Point3d(polorPoint.X, polorPoint.Y,0)));
btr.AppendEntity(clone);
tr.AddNewlyCreatedDBObject(clone, true);
}
tr.Commit();
}
}
运行结果
环形阵列2-圆
/// <summary>
/// 环形阵列2-圆
/// </summary>
[CommandMethod("PolorArryTest2")]
public void PolorArryTest2()
{
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 r = new Random();
Circle circle = new Circle(new Point3d(r.Next(0, 2000), r.Next(0, 500), 0), Vector3d.ZAxis, r.Next(10, 20));
circle.ColorIndex = r.Next(1, 10);
btr.AppendEntity(circle);
tr.AddNewlyCreatedDBObject(circle, true);
int count = r.Next(3, 6);
double angle = Math.PI / count;
Point2d center = new Point2d(circle.Center.X, circle.Center.Y);
double radius = r.Next(30, 100);
for (int i = 0; i < 2 * count; i++)
{
Circle clone = circle.Clone() as Circle;
Point2d polorPoint = GetPolorPoint(center, angle * i, radius);
Vector2d v2 = center.GetVectorTo(polorPoint);
clone.TransformBy(Matrix3d.Displacement(new Vector3d(v2.X, v2.Y, 0)));
btr.AppendEntity(clone);
tr.AddNewlyCreatedDBObject(clone, true);
}
tr.Commit();
}
}
运行结果
图层测试Layer
/// <summary>
/// 复制图层,并重命名
/// </summary>
[CommandMethod("RenameLayerTest")]
public void RenameLayerTest()
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;
Random r = new Random();
for (int i = 0; i < 10; i++)
{
LayerTableRecord myLayer = tr.GetObject(lt["0"], OpenMode.ForRead).Clone() as LayerTableRecord;
myLayer.Name = $"我的第{i + 1}个图层";
myLayer.Color = Color.FromColorIndex(ColorMethod.ByAci,(short)r.Next(1,10));
lt.Add(myLayer);
tr.AddNewlyCreatedDBObject(myLayer, true);
}
tr.Commit();
}
}
运行结果
偏移测试Offset
/// <summary>
/// 偏移测试
/// </summary>
[CommandMethod("OffsetTest")]
public void OffsetTest()
{
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;
Polyline polyline = new Polyline();
Random r = new Random();
for (int i = 0; i < 10; i++)
{
polyline.AddVertexAt(i, new Point2d(i * 10, r.Next(0, 10)), r.NextDouble(), r.NextDouble(), r.NextDouble());
}
btr.AppendEntity(polyline);
tr.AddNewlyCreatedDBObject(polyline, true);
DBObjectCollection collection = polyline.GetOffsetCurves(r.Next(1,10));
int index = 1;
foreach (Entity entity in collection)
{
entity.ColorIndex = index;
btr.AppendEntity(entity);
tr.AddNewlyCreatedDBObject(entity, true);
index++;
}
tr.Commit();
}
}
运行结果
变换测试Transform(平移、旋转、镜像、缩放)
/// <summary>
/// 变换测试(平移、旋转、镜像、缩放)
/// </summary>
[CommandMethod("TransformTest")]
public void TransformTest()
{
Database db = HostApplicationServices.WorkingDatabase;
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.ForWrite) as BlockTableRecord;
Random r = new Random();
//创建多段线
Polyline polyline = new Polyline();
for (int i = 0; i < 5; i++)
{
polyline.AddVertexAt(i, new Point2d(r.Next(0, 100), r.Next(0, 100)), r.NextDouble(), r.NextDouble(), r.NextDouble());
}
polyline.ColorIndex = 1;
polyline.Closed = true;
btr.AppendEntity(polyline);
tr.AddNewlyCreatedDBObject(polyline, true);
//旋转45度
Polyline polylineClone = polyline.Clone() as Polyline;
polylineClone.ColorIndex =2;
polylineClone.TransformBy(Matrix3d.Rotation(Math.PI / 2, Vector3d.ZAxis, new Point3d(100, 100, 0)));
btr.AppendEntity(polylineClone);
tr.AddNewlyCreatedDBObject(polylineClone, true);
//平移
Polyline polylineClone1= polyline.Clone() as Polyline;
polylineClone1.ColorIndex = 3;
Vector3d vector = new Vector3d(100, 100, 0);
polylineClone1.TransformBy(Matrix3d.Displacement(vector));
btr.AppendEntity(polylineClone1);
tr.AddNewlyCreatedDBObject(polylineClone1, true);
//镜像
Polyline polylineClone2 = polyline.Clone() as Polyline;
polylineClone2.ColorIndex = 4;
Line3d line3D = new Line3d(new Point3d(0,0,0),new Point3d(100,0,0));
polylineClone2.TransformBy(Matrix3d.Mirroring(line3D));
btr.AppendEntity(polylineClone2);
tr.AddNewlyCreatedDBObject(polylineClone2, true);
//缩放
Polyline polylineClone3 = polyline.Clone() as Polyline;
polylineClone3.ColorIndex = 5;
polylineClone3.TransformBy(Matrix3d.Scaling(0.5,new Point3d(0,0,0)));
btr.AppendEntity(polylineClone3);
tr.AddNewlyCreatedDBObject(polylineClone3, true);
Polyline polylineClone4 = polyline.Clone() as Polyline;
polylineClone4.ColorIndex = 6;
polylineClone4.TransformBy(Matrix3d.Scaling(2, new Point3d(0, 0, 0)));
btr.AppendEntity(polylineClone4);
tr.AddNewlyCreatedDBObject(polylineClone4, true);
tr.Commit();
}
}
运行结果
克隆测试
/// <summary>
/// 克隆测试
/// </summary>
[CommandMethod("CloneTest")]
public void CloneTest()
{
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(100, 100, 0), Vector3d.ZAxis, 100);
btr.AppendEntity(circle);
tr.AddNewlyCreatedDBObject(circle, true);
for (int i = 0; i < 10; i++)
{
Circle clone = circle.Clone() as Circle;
clone.Radius = circle.Radius - 8 * (i + 1);
clone.ColorIndex = i + 1;
btr.AppendEntity(clone);
tr.AddNewlyCreatedDBObject(clone, true);
}
tr.Commit();
}
}
运行结果
样条线测试Spline
[CommandMethod("SplineTest")]
public void SplineTest()
{
Database db = HostApplicationServices.WorkingDatabase;
Random r = new Random();
//三点的
for (int i = 0; i < 5; i++)
{
Point3dCollection pointCollection = new Point3dCollection();
pointCollection.Add(new Point3d(r.Next(0,10), r.Next(0, 10)+i*20,0));
pointCollection.Add(new Point3d(r.Next(10, 20), r.Next(0, 10) + i * 20, 0));
pointCollection.Add(new Point3d(r.Next(20, 30), r.Next(0, 10) + i * 20, 0));
Spline s1 = new Spline(pointCollection,r.Next(0,10),r.NextDouble());
s1.ColorIndex = r.Next(1,10);
db.AddEntityToModelSpace(s1);
}
//四点的
for (int i = 0; i < 5; i++)
{
Point3dCollection pointCollection = new Point3dCollection();
pointCollection.Add(new Point3d(r.Next(50, 60), r.Next(0, 15) + i * 20, 0));
pointCollection.Add(new Point3d(r.Next(60, 80), r.Next(0, 10) + i * 20, 0));
pointCollection.Add(new Point3d(r.Next(80, 100), r.Next(0, 15) + i * 20, 0));
pointCollection.Add(new Point3d(r.Next(100, 110), r.Next(0, 15) + i * 20, 0));
Spline s1 = new Spline(pointCollection, r.Next(0, 10), r.NextDouble());
s1.ColorIndex = r.Next(1, 10);
db.AddEntityToModelSpace(s1);
}
//循环的
for (int i = 0; i < 5; i++)
{
Point3dCollection pointCollection = new Point3dCollection();
pointCollection.Add(new Point3d(r.Next(120, 130), r.Next(0, 10) + i * 20, 0));
pointCollection.Add(new Point3d(r.Next(130, 140), r.Next(0, 10) + i * 20, 0));
pointCollection.Add(new Point3d(r.Next(140, 150), r.Next(0, 10) + i * 20, 0));
Spline s1 = new Spline(pointCollection, true,KnotParameterizationEnum.Chord, r.Next(180, 720),r.NextDouble());
s1.ColorIndex = r.Next(1, 10);
db.AddEntityToModelSpace(s1);
}
//有切线的
for (int i = 0; i < 5; i++)
{
Point3dCollection pointCollection = new Point3dCollection();
pointCollection.Add(new Point3d(r.Next(160, 180), r.Next(0, 10) + i * 20, 0));
pointCollection.Add(new Point3d(r.Next(180, 200), r.Next(0, 10) + i * 20, 0));
pointCollection.Add(new Point3d(r.Next(200, 210), r.Next(0, 10) + i * 20, 0));
Vector3d tanStart = new Vector3d(r.NextDouble(), r.NextDouble(),0);
Vector3d tanEnd = new Vector3d(r.NextDouble(), r.NextDouble(), 0);
Spline s1 = new Spline(pointCollection, tanStart, tanEnd, r.Next(10, 50), r.NextDouble());
s1.ColorIndex = r.Next(1, 10);
db.AddEntityToModelSpace(s1);
}
//椭圆的
for (int i = 0; i < 5; i++)
{
Point3d center = new Point3d(250, r.Next(0, 10) + i * 20,0);
Spline s1 = new Spline(center,Vector3d.ZAxis,Vector3d.XAxis, r.NextDouble()*0.9+0.1,-Math.PI * r.NextDouble(), Math.PI * r.NextDouble());
s1.ColorIndex = r.Next(1, 10);
db.AddEntityToModelSpace(s1);
}
}
运行结果
圆弧测试Arc
/// <summary>
/// 圆弧测试
/// </summary>
[CommandMethod("ArcTest")]
public void ArcTest()
{
Database db = HostApplicationServices.WorkingDatabase;
Random r = new Random();
for (int i = 0; i < 10; i++)
{
Arc arc = new Arc(new Point3d(r.Next(0, 1000), r.Next(0, 500), 0), r.Next(10, 100), r.NextDouble() * (-Math.PI), r.NextDouble() * Math.PI);
arc.ColorIndex = r.Next(1, 20);
db.AddEntityToModelSpace(arc);
}
}
运行结果
标注测试
[CommandMethod("DimensionTest")]
public void DimensionTest()
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr1 = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr1.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr1.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
//旋转尺寸标注
RotatedDimension rotatedDim = new RotatedDimension()
{
XLine1Point = new Point3d(10, 0, 0),
XLine2Point = new Point3d(50, 0, 0),
Rotation = Math.PI / 4,
DimLinePoint = new Point3d(0, 20, 0),
DimensionStyle = db.Dimstyle,
ColorIndex = 1,
};
btr.AppendEntity(rotatedDim);
tr1.AddNewlyCreatedDBObject(rotatedDim, true);
//半径标注
RadialDimension radialDim = new RadialDimension()
{
Center = new Point3d(100, 0, 0),
ChordPoint = new Point3d(80, 20, 0),
LeaderLength = 30,
DimensionStyle = db.Dimstyle,
ColorIndex = 2,
};
btr.AppendEntity(radialDim);
tr1.AddNewlyCreatedDBObject(radialDim, true);
//二维角度标注
LineAngularDimension2 lineAngularDim = new LineAngularDimension2()
{
XLine1Start = new Point3d(200, 0, 0),
XLine1End = new Point3d(220, 0, 0),
XLine2Start = new Point3d(200, 0, 0),
XLine2End = new Point3d(200, 50, 0),
ArcPoint = new Point3d(250, 60, 0),
DimensionStyle = db.Dimstyle,
ColorIndex = 3,
};
btr.AppendEntity(lineAngularDim);
tr1.AddNewlyCreatedDBObject(lineAngularDim, true);
//折弯半径标注
RadialDimensionLarge radialLargeDim = new RadialDimensionLarge()
{
Center = new Point3d(0, 100, 0),
ChordPoint = new Point3d(50, 100, 0),
OverrideCenter = new Point3d(0, 80, 0),
JogPoint = new Point3d(60, 80, 0),
JogAngle = Math.PI / 3,
DimensionStyle = db.Dimstyle,
ColorIndex = 4,
};
btr.AppendEntity(radialLargeDim);
tr1.AddNewlyCreatedDBObject(radialLargeDim, true);
//弧长标注
ArcDimension arcDim = new ArcDimension(
new Point3d(100, 100, 0),
new Point3d(90, 80, 0),
new Point3d(70, 110, 0),
new Point3d(90, 80, 0),
"ArcDimension",
db.Dimstyle
);
arcDim.ColorIndex = 5;
btr.AppendEntity(arcDim);
tr1.AddNewlyCreatedDBObject(arcDim, true);
//坐标标注
OrdinateDimension ordinateDim = new OrdinateDimension()
{
UsingXAxis=true,
DefiningPoint=new Point3d(200,100,0),
LeaderEndPoint=new Point3d(220,130,0),
DimensionStyle=db.Dimstyle,
ColorIndex=6,
};
btr.AppendEntity(ordinateDim);
tr1.AddNewlyCreatedDBObject(ordinateDim, true);
tr1.Commit();
}
}
运行结果
文本测试
[CommandMethod("TextTest")]
public void TextTest()
{
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;
DBText text1 = new DBText()
{
Position = new Point3d(10, 20, 0),
Height = 30,
TextString = "Hello,你好啊,,",
IsMirroredInX = true,
ColorIndex = 1,
};
btr.AppendEntity(text1);
tr.AddNewlyCreatedDBObject(text1,true);
DBText text2 = new DBText()
{
Position = new Point3d(10, 50, 0),
Height = 30,
ColorIndex = 2,
VerticalMode = TextVerticalMode.TextBottom,
IsMirroredInY=true,
TextString= "CAD二次开发",
Rotation=Math.PI/4,
};
btr.AppendEntity(text2);
tr.AddNewlyCreatedDBObject(text2, true);
MText mText1 = new MText()
{
Contents = "角度符号:%%d,公差符号:%%p,直径符号:%%c,上划线符号:%%o,下划线符号:%%u,%符号:%%%,,//radiusRatio半径比>1画不出来;如果半径比是1,就是短轴半径与长轴半径相等,画出来的就是圆形;//radiusRatio半径比=短轴半径/长轴半径,取值范围-1到1;",
Location = new Point3d(100, 0, 0),
Width = 200,
ColorIndex = 3,
Rotation = Math.PI / 6,
TextHeight = 20,
BackgroundFill = true,
BackgroundFillColor=Color.FromColorIndex(ColorMethod.ByAci,4),
};
btr.AppendEntity(mText1);
tr.AddNewlyCreatedDBObject(mText1, true);
tr.Commit();
}
运行结果
椭圆画法测试,及各个参数的解读
- //public Ellipse(Point3d center, Vector3d unitNormal, Vector3d majorAxis, double radiusRatio, double startAngle, double endAngle);
- //center圆心,unitNormal所在平面法向量,majorAxis主轴,
- //radiusRatio半径比,startAngle起始角,endAngle结束角
- //radiusRatio半径比>1画不出来;如果半径比是1,就是短轴半径与长轴半径相等,画出来的就是圆形;
- //radiusRatio半径比=短轴半径/长轴半径,取值范围-1到1;
- //半径比可以接受负值的输入,如果输入负值,则会按照其绝对值进行绘制;
- //半径比越小,椭圆就越扁,半径比越大,椭圆就越圆,但是不能超过1;
- //majorAxis主轴new Vector3d(0, 0, 0),主轴长度为0(短轴长度=长轴长度*半径比,也是0),画不出来
- //majorAxis主轴,主轴长度就是长轴半径;
- //主轴不仅决定了长轴长度,还决定了长轴的方向;
- //起止角度startAngle/endAngle单位为弧度,而不是角度;
- //起止角度支持负值,夹角为与主轴正方向,逆时针为正,顺时针为负;
- //如果起始角度大于终止角度,那么就按照起始角度逆时针旋转至终止角度进行绘制;
- //如果起止角度相等,例如都是0,则绘制一圈360度
- //( pi, -pi)(-pi , 0)( pi, 0)这三种起止角度是等价的,都是绘制180-360的范围;
[CommandMethod("EllipseTest")]
public void EllipseTest()
{
Database db = HostApplicationServices.WorkingDatabase;
//public Ellipse(Point3d center, Vector3d unitNormal, Vector3d majorAxis, double radiusRatio, double startAngle, double endAngle);
//center圆心,unitNormal所在平面法向量,majorAxis主轴,
//radiusRatio半径比,startAngle起始角,endAngle结束角
//radiusRatio半径比>1画不出来;如果半径比是1,就是短轴半径与长轴半径相等,画出来的就是圆形;
//radiusRatio半径比=短轴半径/长轴半径,取值范围-1到1;
//半径比可以接受负值的输入,如果输入负值,则会按照其绝对值进行绘制;
//半径比越小,椭圆就越扁,半径比越大,椭圆就越圆,但是不能超过1;
//majorAxis主轴new Vector3d(0, 0, 0),主轴长度为0(短轴长度=长轴长度*半径比,也是0),画不出来
//majorAxis主轴,主轴长度就是长轴半径;
//主轴不仅决定了长轴长度,还决定了长轴的方向;
//起止角度startAngle/endAngle单位为弧度,而不是角度;
//起止角度支持负值,夹角为与主轴正方向,逆时针为正,顺时针为负;
//如果起始角度大于终止角度,那么就按照起始角度逆时针旋转至终止角度进行绘制;
//如果起止角度相等,例如都是0,则绘制一圈360度
//( pi, -pi)(-pi , 0)( pi, 0)这三种起止角度是等价的,都是绘制180-360的范围;
double pi = Math.PI;
//不同半径比
Ellipse ellipse1 = new Ellipse(new Point3d(100, 0, 0), Vector3d.ZAxis, new Vector3d(200, 0, 0), 0.5, pi, 0);
ellipse1.ColorIndex = 1;
Ellipse ellipse2 = new Ellipse(new Point3d(100, 0, 0), Vector3d.ZAxis, new Vector3d(200, 0, 0), -1, pi / 2, 0);
ellipse2.ColorIndex = 2;
Ellipse ellipse3 = new Ellipse(new Point3d(100, 0, 0), Vector3d.ZAxis, new Vector3d(200, 0, 0), 0.2, 2 * pi, 0);
ellipse3.ColorIndex = 3;
db.AddEntityToModelSpace(ellipse1, ellipse2, ellipse3);
//不同主轴
Ellipse ellipse4 = new Ellipse(new Point3d(500, 0, 0), Vector3d.ZAxis, new Vector3d(0, 100, 0), -0.5, 2 * pi, 2 * pi);
ellipse4.ColorIndex = 1;
Ellipse ellipse5 = new Ellipse(new Point3d(500, 0, 0), Vector3d.ZAxis, new Vector3d(50, 50, 0), 0.5, 0, -pi);
ellipse5.ColorIndex = 2;
Ellipse ellipse6 = new Ellipse(new Point3d(500, 0, 0), Vector3d.ZAxis, new Vector3d(200, 0, 0), 0.5, pi, 0);
ellipse6.ColorIndex = 3;
db.AddEntityToModelSpace(ellipse4, ellipse5, ellipse6);
//不同角度
Ellipse ellipse7 = new Ellipse(new Point3d(900, 0, 0), Vector3d.ZAxis, new Vector3d(100, 100, 0), 0.5, 0, 0);
ellipse7.ColorIndex = 1;
Ellipse ellipse8 = new Ellipse(new Point3d(900, 0, 0), Vector3d.ZAxis, new Vector3d(50, 0, 0), 0.5, -pi, 0);
ellipse8.ColorIndex = 2;
Ellipse ellipse9 = new Ellipse(new Point3d(900, 0, 0), Vector3d.ZAxis, new Vector3d(100, 0, 0), 0.5, pi, -pi)
{
ColorIndex = 3
};
db.AddEntityToModelSpace(ellipse7, ellipse8, ellipse9);
}
添加实体到模型空间的静态扩展方法
/// <summary>
/// 添加多个实体到模型空间
/// </summary>
/// <param name="db">数据库</param>
/// <param name="entityArry">实体数组</param>
/// <returns>实体对象Id数组</returns>
public static ObjectId[] AddEntityToModelSpace(this Database db, params Entity[] entityArry)
{
ObjectId[] idArry = new ObjectId[entityArry.Length];
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
for (int i = 0; i < entityArry.Length; i++)
{
idArry[i] = btr.AppendEntity(entityArry[i]);
trans.AddNewlyCreatedDBObject(entityArry[i], true);
}
trans.Commit();
}
return idArry;
}
运行结果
三点法画圆的预备知识(点径法和两点法相对比较简单,不作详述):
思路:
- 已知三个点:p1点p2点p3点
- 求出两个向量v1(p1-p2),v2(p2-p3);
- 上述两个向量叉乘,得到圆所在平面的法向量n=v1×v2;
- 上述两个向量分别和n叉乘,得到两条直径的方向向量n1=v1×n,n2=v2×n;
- 求出两个中点m1(p1,p2之间)m2(p2,p3之间);
- 根据两个中点和两个方向向量,得到两个直线的点向式方程L1,L2;
- 联立两个方程,求出交点(也就是圆心)的坐标表达式
两个点向式方程
联立求出圆心坐标表达式
注意事项:
上述表达式中,分母可能为0;
如果圆的平面在XY平面,那么z坐标表达式的分母则为0,会导致错误;
有两种解决方案(等效的):
方案一:
在使用坐标表达式之前对分母进行判定,如果分母为0,则表示该坐标为0,而不用调用坐标表达式;
方案二:
使用圆所在平面的法向量进行判定,如果法向量的x,y坐标都是0,说明圆所在平面为xy平面,那么z就可以直接赋值0,而不用调用坐标表达式;
三种画圆的方法及其实现
[CommandMethod("circleTest")]
public void CircleTest()
{
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 center = new Point3d(10, 10, 0);
Circle circle1 = CreatCircle(center, 30);
circle1.ColorIndex = 1;
DBPoint dbCenter = new DBPoint(center);
btr.AppendEntity(circle1);
tr.AddNewlyCreatedDBObject(circle1, true);
btr.AppendEntity(dbCenter);
tr.AddNewlyCreatedDBObject(dbCenter, true);
//两点法创建圆
Point3d point1 = new Point3d(300, 300, 0);
Point3d point2 = new Point3d(200, 200, 0);
DBPoint dbPoint1 = new DBPoint(point1);
DBPoint dbPoint2 = new DBPoint(point2);
Circle circle2 = CreatCircle(point1, point2);
circle2.ColorIndex = 2;
btr.AppendEntity(circle2);
tr.AddNewlyCreatedDBObject(circle2, true);
btr.AppendEntity(dbPoint1);
tr.AddNewlyCreatedDBObject(dbPoint1, true);
btr.AppendEntity(dbPoint2);
tr.AddNewlyCreatedDBObject(dbPoint2, true);
//三点法创建圆
Point3d p1 = new Point3d(100, 0, 0);
Point3d p2 = new Point3d(200, 0, 0);
Point3d p3 = new Point3d(100, 100, 0);
DBPoint dbP1 = new DBPoint(p1);
DBPoint dbP2 = new DBPoint(p2);
DBPoint dbP3 = new DBPoint(p3);
Circle circle3 = CreatCircle(p1, p2, p3);
circle3.ColorIndex = 3;
btr.AppendEntity(circle3);
tr.AddNewlyCreatedDBObject(circle3, true);
btr.AppendEntity(dbP1);
tr.AddNewlyCreatedDBObject(dbP1, true);
btr.AppendEntity(dbP2);
tr.AddNewlyCreatedDBObject(dbP2, true);
btr.AppendEntity(dbP3);
tr.AddNewlyCreatedDBObject(dbP3, true);
db.Pdmode = 34;
db.Pdsize = 1;
tr.Commit();
}
}
//点径法创建圆
public Circle CreatCircle(Point3d center, double radius)
{
return new Circle(center, Vector3d.ZAxis, radius);
}
//两点法创建圆
public Circle CreatCircle(Point3d point1, Point3d point2)
{
Point3d center = MiddlePoint(point1, point2);
double radius = center.DistanceTo(point2);
return new Circle(center, Vector3d.ZAxis, radius);
}
//三点法创建圆
public Circle CreatCircle(Point3d point1, Point3d point2, Point3d point3)
{
if (IsCollinear(point1, point2, point3)) return new Circle();
Vector3d v1 = point1.GetVectorTo(point2);
Vector3d v2 = point2.GetVectorTo(point3);
Vector3d n = v1.CrossProduct(v2);
Point3d p1 = MiddlePoint(point1, point2);
Point3d p2 = MiddlePoint(point2, point3);
Vector3d n1 = v1.CrossProduct(n);
Vector3d n2 = v2.CrossProduct(n);
double x = Math.Abs(n1.Y * n2.X - n2.Y * n1.X) < 0.000001 ? 0 : (p2.Y * n1.X * n2.X - p1.Y * n1.X * n2.X + p1.X * n1.Y * n2.X - p2.X * n2.Y * n1.X) / (n1.Y * n2.X - n2.Y * n1.X);
double y = Math.Abs(n1.X * n2.Y - n2.X * n1.Y) < 0.000001 ? 0 : (p2.X * n1.Y * n2.Y - p1.X * n1.Y * n2.Y + p1.Y * n1.X * n2.Y - p2.Y * n2.X * n1.Y) / (n1.X * n2.Y - n2.X * n1.Y);
double z = Math.Abs(n1.Y * n2.Z - n2.Y * n1.Z) < 0.000001 ? 0 : (p2.Y * n1.Z * n2.Z - p1.Y * n1.Z * n2.Z + p1.Z * n1.Y * n2.Z - p2.Z * n2.Y * n1.Z) / (n1.Y * n2.Z - n2.Y * n1.Z);
Point3d center = new Point3d(x, y, z);
double radius = point1.DistanceTo(center);
return new Circle(center, Vector3d.ZAxis, radius);
}
//判断三点是否共线
public bool IsCollinear(Point3d point1, Point3d point2, Point3d point3)
{
Vector3d vector1 = point1.GetVectorTo(point2);
Vector3d vector2 = point2.GetVectorTo(point3);
double angle = vector1.GetAngleTo(vector2);
if (Math.Abs(angle) < 0.0001 || Math.Abs(angle - Math.PI) < 0.000001) return true;
return false;
}
//求两点的中点
public Point3d MiddlePoint(Point3d point1, Point3d point2)
{
return new Point3d((point1.X + point2.X) / 2, (point1.Y + point2.Y) / 2, (point1.Z + point2.Z) / 2);
}
运行结果
角度测试
- GetAngleTo的返回值为弧度值;
- 返回值介于0-180度(0-π)之间;
- 不会有负值;
[CommandMethod("AngleTest")]
public void AngleTest()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage($"Vector3d.XAxis.GetAngleTo(Vector3d.YAxis):{Vector3d.XAxis.GetAngleTo(Vector3d.YAxis)}\n");
ed.WriteMessage($"Vector3d.XAxis.GetAngleTo(Vector3d.YAxis):{Vector3d.XAxis.GetAngleTo(Vector3d.YAxis) * 180 / Math.PI}\n");
ed.WriteMessage($"Vector3d.XAxis.GetAngleTo(-Vector3d.YAxis):{Vector3d.XAxis.GetAngleTo(-Vector3d.YAxis)}\n");
ed.WriteMessage($"Vector3d.XAxis.GetAngleTo(-Vector3d.YAxis):{Vector3d.XAxis.GetAngleTo(-Vector3d.YAxis) * 180 / Math.PI}\n");
ed.WriteMessage($"new Vector3d(1,1,0).GetAngleTo(Vector3d.XAxis):{new Vector3d(1, 1, 0).GetAngleTo(Vector3d.XAxis)}\n");
ed.WriteMessage($"new Vector3d(1,1,0).GetAngleTo(Vector3d.XAxis):{new Vector3d(1, 1, 0).GetAngleTo(Vector3d.XAxis) * 180 / Math.PI}\n");
ed.WriteMessage($"new Vector3d(1,1,0).GetAngleTo(-Vector3d.XAxis):{new Vector3d(1, 1, 0).GetAngleTo(-Vector3d.XAxis)}\n");
ed.WriteMessage($"new Vector3d(1,1,0).GetAngleTo(-Vector3d.XAxis):{new Vector3d(1, 1, 0).GetAngleTo(-Vector3d.XAxis) * 180 / Math.PI}\n");
ed.WriteMessage($"new Vector3d(1, 1, 0).GetAngleTo(Vector3d.YAxis):{new Vector3d(1, 1, 0).GetAngleTo(Vector3d.YAxis)}\n");
ed.WriteMessage($"new Vector3d(1, 1, 0).GetAngleTo(Vector3d.YAxis):{new Vector3d(1, 1, 0).GetAngleTo(Vector3d.YAxis) * 180 / Math.PI}\n");
ed.WriteMessage($"new Vector3d(1, 1, 0).GetAngleTo(-Vector3d.YAxis):{new Vector3d(1, 1, 0).GetAngleTo(-Vector3d.YAxis)}\n");
ed.WriteMessage($"new Vector3d(1, 1, 0).GetAngleTo(-Vector3d.YAxis):{new Vector3d(1, 1, 0).GetAngleTo(-Vector3d.YAxis) * 180 / Math.PI}\n");
}
运行结果