autocad打印窗口坐标定位错误

发布时间 2023-04-14 15:13:52作者: 因思道客

使用SetWindowToPlot方法打印,设置坐标总是出错,但是使用ThisDrawing.ActiveLayout.GetWindowToPlot,返回的却是正确的坐标。查看了PaperUnits和UCS、PlotType都正确。而且autocad官网上提供的范例也是这么写的,同样也出错。https://help.autodesk.com/view/OARX/2023/DEU/?guid=GUID-9F4784EE-3203-4C7C-A27E-720B25BB1BD9
最后自己通过手动的打印,发现正确打印不出错。这时再通过程序打印有时就正常了,有时不设置,使用默认设置,手动打印也出错。那么应该是某个参数漏了,各个参数逐一试一试,发现ThisDrawing.ActiveLayout.PlotOrigin的打印偏移不是零点的原因造成的,把这个值设置成0点,或者设置图纸居中,就可以解决了。VBA代码示例如下:

根据官网示例修改的代码
Sub Example_SetWindowToPlot()
    ' This example allows the user to define an area in the current layout
    ' and displays a plot preview of the defined area.
    '
    ' * Note: You have to exit the
    ' plot preview before the VBA example will stop and control will be returned

    AppActivate ThisDrawing.Application.Caption

    Dim point1 As Variant, point2 As Variant
    ' Get first point in window
    point1 = ThisDrawing.Utility.GetPoint(, "Click the lower-left of the window to plot.")
    ReDim Preserve point1(0 To 1)   ' Change this to a 2D array by removing the Z position
    ' Get second point in window
    point2 = ThisDrawing.Utility.GetPoint(, "Click the upper-right of the window to plot.")
    ReDim Preserve point2(0 To 1)   ' Change this to a 2D array by removing the Z position
    ThisDrawing.SetVariable "BACKGROUNDPLOT", 0
    ' Send information about window to current layout
    ThisDrawing.ActiveLayout.SetWindowToPlot point1, point2
    ThisDrawing.ActiveLayout.StandardScale = acScaleToFit
    ThisDrawing.ActiveLayout.CenterPlot = True
    ' Read back window information
    ThisDrawing.ActiveLayout.GetWindowToPlot point1, point2
    MsgBox "Press any key to plot the following window:" & vbCrLf & vbCrLf & _
           "Lower Left: " & point1(0) & ", " & point1(1) & vbCrLf & _
           "Upper Right: " & point2(0) & ", " & point2(1)
    Dim point3 As Variant
    point3 = ThisDrawing.ActiveLayout.PlotOrigin
    MsgBox point3(0) & vbCrLf & point3(1)
    ' Be sure to plot a view, not some other plot style
    ThisDrawing.ActiveLayout.PlotType = acWindow
    ' Send Plot To Window
    ThisDrawing.ActiveLayout.ConfigName = "DWG to PDF.pc3"
    ThisDrawing.Plot.DisplayPlotPreview acFullPreview
End Sub