CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

从模型空间打印 (.NET)

2023-1-1 09:20| 发布者: admin| 查看: 349| 评论: 0|来自: AutoCAD

通常,在打印大型图形(如平面图)时,需要指定比例以将实际图形单位转换为打印的英寸或毫米。但是,从模型空间打印时,如果未指定设置,则使用的默认值包括打印到系统打印机、打印当前显示、缩放以适合、0 旋转和 0,0 偏移。

使用 aobject 修改要打印的布局的打印设置,然后在将对象传递给对象之前使用对象验证打印设置。定义对象后,可以使用对象来打印布局或图纸。PlotSettingsPlotSettingsValidatorPlotSettingsPlotInfoPlotInfoPlotEngine

绘制当前布局

本示例将当前布局的范围打印到名为 MyPlot 的 DWF 文件中。

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.PlottingServices
 
' Plots the current layout to a DWF file
<CommandMethod("PlotLayout")> _
Public Shared Sub PlotLayout()
    ' Get the current document and database, and start a transaction
    Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
    Dim acCurDb As Database = acDoc.Database

    Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
        ' Reference the Layout Manager
        Dim acLayoutMgr As LayoutManager = LayoutManager.Current

        ' Get the current layout and output its name in the Command Line window
        Dim acLayout As Layout = _
        acTrans.GetObject(acLayoutMgr.GetLayoutId(acLayoutMgr.CurrentLayout), _
                          OpenMode.ForRead)

        ' Get the PlotInfo from the layout
        Using acPlInfo As PlotInfo = New PlotInfo()
            acPlInfo.Layout = acLayout.ObjectId

            ' Get a copy of the PlotSettings from the layout
            Using acPlSet As PlotSettings = New PlotSettings(acLayout.ModelType)
                acPlSet.CopyFrom(acLayout)

                ' Update the PlotSettings object
                Dim acPlSetVdr As PlotSettingsValidator = _
                    PlotSettingsValidator.Current

                ' Set the plot type
                acPlSetVdr.SetPlotType(acPlSet, _
                                       DatabaseServices.PlotType.Extents)

                ' Set the plot scale
                acPlSetVdr.SetUseStandardScale(acPlSet, True)
                acPlSetVdr.SetStdScaleType(acPlSet, StdScaleType.ScaleToFit)

                ' Center the plot
                acPlSetVdr.SetPlotCentered(acPlSet, True)

                ' Set the plot device to use
                acPlSetVdr.SetPlotConfigurationName(acPlSet, "DWF6 ePlot.pc3", _
                                                    "ANSI_A_(8.50_x_11.00_Inches)")

                ' Set the plot info as an override since it will
                ' not be saved back to the layout
                acPlInfo.OverrideSettings = acPlSet

                ' Validate the plot info
                Using acPlInfoVdr As PlotInfoValidator = New PlotInfoValidator()
                    acPlInfoVdr.MediaMatchingPolicy = MatchingPolicy.MatchEnabled
                    acPlInfoVdr.Validate(acPlInfo)

                    ' Check to see if a plot is already in progress
                    If PlotFactory.ProcessPlotState = _
                        ProcessPlotState.NotPlotting Then

                        Using acPlEng As PlotEngine = _
                            PlotFactory.CreatePublishEngine()

                            ' Track the plot progress with a Progress dialog
                            Using acPlProgDlg As PlotProgressDialog = _
                                New PlotProgressDialog(False, 1, True)

                                Using (acPlProgDlg)
                                    ' Define the status messages to display 
                                    ' when plotting starts
                                    acPlProgDlg.PlotMsgString( _
                                        PlotMessageIndex.DialogTitle) = "Plot Progress"
                                    acPlProgDlg.PlotMsgString( _
                                        PlotMessageIndex.CancelJobButtonMessage) = _
                                                               "Cancel Job"
                                    acPlProgDlg.PlotMsgString( _
                                        PlotMessageIndex.CancelSheetButtonMessage) = _
                                                               "Cancel Sheet"
                                    acPlProgDlg.PlotMsgString( _
                                        PlotMessageIndex.SheetSetProgressCaption) = _
                                                               "Sheet Set Progress"
                                    acPlProgDlg.PlotMsgString( _
                                        PlotMessageIndex.SheetProgressCaption) = _
                                                               "Sheet Progress"

                                    ' Set the plot progress range
                                    acPlProgDlg.LowerPlotProgressRange = 0
                                    acPlProgDlg.UpperPlotProgressRange = 100
                                    acPlProgDlg.PlotProgressPos = 0

                                    ' Display the Progress dialog
                                    acPlProgDlg.OnBeginPlot()
                                    acPlProgDlg.IsVisible = True

                                    ' Start to plot the layout
                                    acPlEng.BeginPlot(acPlProgDlg, Nothing)

                                    ' Define the plot output
                                    acPlEng.BeginDocument(acPlInfo, _
                                                          acDoc.Name, _
                                                          Nothing, _
                                                          1, _
                                                          True, _
                                                          "c:\myplot")

                                    ' Display information about the current plot
                                    acPlProgDlg.PlotMsgString( _
                                        PlotMessageIndex.Status) = _
                                        "Plotting: " & acDoc.Name & _
                                        " - " & acLayout.LayoutName

                                    ' Set the sheet progress range
                                    acPlProgDlg.OnBeginSheet()
                                    acPlProgDlg.LowerSheetProgressRange = 0
                                    acPlProgDlg.UpperSheetProgressRange = 100
                                    acPlProgDlg.SheetProgressPos = 0

                                    ' Plot the first sheet/layout
                                    Using acPlPageInfo As PlotPageInfo = _
                                        New PlotPageInfo()
                                        acPlEng.BeginPage(acPlPageInfo, _
                                                          acPlInfo, _
                                                          True, _
                                                          Nothing)
                                    End Using

                                    acPlEng.BeginGenerateGraphics(Nothing)
                                    acPlEng.EndGenerateGraphics(Nothing)

                                    ' Finish plotting the sheet/layout
                                    acPlEng.EndPage(Nothing)
                                    acPlProgDlg.SheetProgressPos = 100
                                    acPlProgDlg.OnEndSheet()

                                    ' Finish plotting the document
                                    acPlEng.EndDocument(Nothing)

                                    ' Finish the plot
                                    acPlProgDlg.PlotProgressPos = 100
                                    acPlProgDlg.OnEndPlot()
                                    acPlEng.EndPlot(Nothing)
                                End Using
                            End Using
                        End Using
                    End If
                End Using
            End Using
        End Using
    End Using
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.PlottingServices;
 
// Plots the current layout to a DWF file
[CommandMethod("PlotLayout")]
public static void PlotLayout()
{
    // Get the current document and database, and start a transaction
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;

    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Reference the Layout Manager
        LayoutManager acLayoutMgr = LayoutManager.Current;

        // Get the current layout and output its name in the Command Line window
        Layout acLayout = acTrans.GetObject(acLayoutMgr.GetLayoutId(acLayoutMgr.CurrentLayout),
                                            OpenMode.ForRead) as Layout;

        // Get the PlotInfo from the layout
        using (PlotInfo acPlInfo = new PlotInfo())
        {
            acPlInfo.Layout = acLayout.ObjectId;

            // Get a copy of the PlotSettings from the layout
            using (PlotSettings acPlSet = new PlotSettings(acLayout.ModelType))
            {
                acPlSet.CopyFrom(acLayout);

                // Update the PlotSettings object
                PlotSettingsValidator acPlSetVdr = PlotSettingsValidator.Current;

                // Set the plot type
                acPlSetVdr.SetPlotType(acPlSet, Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);

                // Set the plot scale
                acPlSetVdr.SetUseStandardScale(acPlSet, true);
                acPlSetVdr.SetStdScaleType(acPlSet, StdScaleType.ScaleToFit);

                // Center the plot
                acPlSetVdr.SetPlotCentered(acPlSet, true);

                // Set the plot device to use
                acPlSetVdr.SetPlotConfigurationName(acPlSet, "DWF6 ePlot.pc3", "ANSI_A_(8.50_x_11.00_Inches)");

                // Set the plot info as an override since it will
                // not be saved back to the layout
                acPlInfo.OverrideSettings = acPlSet;

                // Validate the plot info
                using (PlotInfoValidator acPlInfoVdr = new PlotInfoValidator())
                {
                    acPlInfoVdr.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
                    acPlInfoVdr.Validate(acPlInfo);

                    // Check to see if a plot is already in progress
                    if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
                    {
                        using (PlotEngine acPlEng = PlotFactory.CreatePublishEngine())
                        {
                            // Track the plot progress with a Progress dialog
                            using (PlotProgressDialog acPlProgDlg = new PlotProgressDialog(false, 1, true))
                            {
                                using ((acPlProgDlg))
                                {
                                    // Define the status messages to display 
                                    // when plotting starts
                                    acPlProgDlg.set_PlotMsgString(PlotMessageIndex.DialogTitle, "Plot Progress");
                                    acPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage, "Cancel Job");
                                    acPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage, "Cancel Sheet");
                                    acPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "Sheet Set Progress");
                                    acPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "Sheet Progress");

                                    // Set the plot progress range
                                    acPlProgDlg.LowerPlotProgressRange = 0;
                                    acPlProgDlg.UpperPlotProgressRange = 100;
                                    acPlProgDlg.PlotProgressPos = 0;

                                    // Display the Progress dialog
                                    acPlProgDlg.OnBeginPlot();
                                    acPlProgDlg.IsVisible = true;

                                    // Start to plot the layout
                                    acPlEng.BeginPlot(acPlProgDlg, null);

                                    // Define the plot output
                                    acPlEng.BeginDocument(acPlInfo, acDoc.Name, null, 1, true, "c:\\myplot");

                                    // Display information about the current plot
                                    acPlProgDlg.set_PlotMsgString(PlotMessageIndex.Status, "Plotting: " + acDoc.Name + " - " + acLayout.LayoutName);

                                    // Set the sheet progress range
                                    acPlProgDlg.OnBeginSheet();
                                    acPlProgDlg.LowerSheetProgressRange = 0;
                                    acPlProgDlg.UpperSheetProgressRange = 100;
                                    acPlProgDlg.SheetProgressPos = 0;

                                    // Plot the first sheet/layout
                                    using (PlotPageInfo acPlPageInfo = new PlotPageInfo())
                                    {
                                        acPlEng.BeginPage(acPlPageInfo, acPlInfo, true, null);
                                    }

                                    acPlEng.BeginGenerateGraphics(null);
                                    acPlEng.EndGenerateGraphics(null);

                                    // Finish plotting the sheet/layout
                                    acPlEng.EndPage(null);
                                    acPlProgDlg.SheetProgressPos = 100;
                                    acPlProgDlg.OnEndSheet();

                                    // Finish plotting the document
                                    acPlEng.EndDocument(null);

                                    // Finish the plot
                                    acPlProgDlg.PlotProgressPos = 100;
                                    acPlProgDlg.OnEndPlot();
                                    acPlEng.EndPlot(null);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

VBA/ActiveX 代码参考

Sub PlotLayout()
    ' Set the extents and scale of the plot area
    ThisDrawing.ActiveLayout.PlotType = acExtents
    ThisDrawing.ActiveLayout.StandardScale = acScaleToFit
 
    ' Center the plot
    ThisDrawing.ActiveLayout.CenterPlot = True
 
    ' Set the plot device to use
    ThisDrawing.ActiveLayout.ConfigName = "DWF6 ePlot.pc3"
 
    ' Set the plot output size
    ThisDrawing.ActiveLayout.CanonicalMediaName = "ANSI_A_(8.50_x_11.00_Inches)"
 
    ' Set the number of copies to one
    ThisDrawing.Plot.NumberOfCopies = 1
 
    ' Initiate the plot
    ThisDrawing.Plot.PlotToFile "c:\myplot"
End Sub

可以使用属性指定设备名称。可以通过指定 PC3 文件在方法中重写此设备。ConfigNamePlotToDevice


路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-19 15:29

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部