private void cbSize_SelectedIndexChanged(object sender, EventArgs e) { using (var cad = new AutoCadConnector()) { try { AcadDocument doc; if (cad.Application.Documents.Count > 0) { doc = cad.Application.Documents.Item(0) ?? cad.Application.Documents.Add(); } else { doc = cad.Application.Application.Documents.Add(); } AcadLayout layout = doc.ModelSpace.Layout; layout.ConfigName = cbPrinterList.Text; layout.CanonicalMediaName = mediaNameDictionary[cbSize.Text]; double w, h; layout.GetPaperSize(out w, out h); layout.PlotRotation = w > h ? AcPlotRotation.ac0degrees : AcPlotRotation.ac90degrees; layout.RefreshPlotDeviceInfo(); lblPaperSize.Text = string.Format("打印尺寸:{0}*{1}", Math.Ceiling(w), Math.Ceiling(h)); } catch (Exception ex) { MessageBox.Show(string.Format("调用CAD程序出错!\n错误描述:{0}", ex.Message), @"批量打印", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
public void Print() { using (var cad = new AutoCadConnector()) { try { AcadDocument doc = cad.Application.Documents.Open(FileFullName); AcadLayout layout = doc.ModelSpace.Layout; #region Autocad.Layout对象的属性及方法,主要用于打印输出 //Application //Block //CanonicalMediaName //CenterPlot //ConfigName //Document //GetCanonicalMediaNames //GetLocaleMediaName //GetPlotDeviceNames //GetPlotStyleTableNames //Handle //HasExtensionDictionary //ModelType //Name //ObjectID //ObjectName //OwnerID //PaperUnits //PlotHidden //PlotOrigin //PlotRotation //PlotType //PlotViewportBorders //PlotViewportsFirst //PlotWithLineweights //PlotWithPlotStyles //RefreshPlotDeviceInfo //ScaleLineweights //ShowPlotStyles //StandardScale //StyleSheet //TabOrder //UseStandardScale //ViewToPlot #endregion //layout.RefreshPlotDeviceInfo(); //ConfigName 指定打印机配置名。用于布局或打印配置的 PC3 文件或打印设备的名称。 layout.ConfigName = PlotConfigName; layout.StyleSheet = StyleSheet; #region /* * //PlotType 指定布局或打印配置的类型。 * //acDisplay 打印当前显示的所有对象。 * //acExtents 打印当前选定空间范围内的所有对象。 * //acLimits 打印当前空间界限内的所有对象。 * //acView 打印在ViewToPlot属性中命名的视口。 * //acWindow 打印在SetWindowToPlot方法中指定的窗口中的所有对象。 * //acLayout 打印指定纸张尺寸边界内的所有对象,它的原点是由布局选项卡中从坐标(0,0)计算得到。该选项在从模型空间打印时不可用。 * * */ #endregion layout.PlotType = AcPlotType.acExtents; //PlotWithPlotStyles:指定对象是按在打印样式文件中分配的配置打印,还是按图形文件中的配置打印。 layout.PlotWithPlotStyles = true; //CanonicalMediaName 按名称指定图纸尺寸.String[字符串]; 可读写,图纸尺寸的名称 layout.CanonicalMediaName = CanonicalMediaName; layout.PaperUnits = AcPlotPaperUnits.acMillimeters; layout.PlotOrigin = new double[] { 0, 0 }; layout.UseStandardScale = true; layout.StandardScale = AcPlotScale.acScaleToFit; if (MapSheet == "A3" || MapSheet == "A4") { layout.PlotRotation = Angle == 0 ? AcPlotRotation.ac90degrees : AcPlotRotation.ac0degrees; } else { layout.PlotRotation = Angle == 90 ? AcPlotRotation.ac90degrees : AcPlotRotation.ac0degrees; } layout.RefreshPlotDeviceInfo(); double w, h; layout.GetPaperSize(out w, out h); Console.WriteLine(@"图幅:{0},{1}*{2}", CanonicalMediaName, w, h); doc.Plot.PlotToDevice(null); doc.Close(false); PrintedNum++; } catch (Exception ex) { MessageBox.Show(string.Format("调用CAD执行批量打印时发生错误!\n错误描述:{0}", ex.Message), @"批量打印", MessageBoxButtons.OK, MessageBoxIcon.Error); throw; } } }