autocad打印窗口坐标定位错误

使用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

但实际这不是主要的原因,主要原因是获得的bondingbox坐标是以UCS用户坐标系,作为坐标系,而打印窗口设置是以DCS窗口坐标系。两个坐标系需要转换,也就是TARGET(DCS坐标系相对于UCS坐标系的零点)设置成UCS的零点(用lisp命令 DVIEW PO 0,0,0),也可以在转换的时候减去这个值。然后将打印范围的X和Y分别减去ptTarget的X和Y值即可。
Point3d ptTarget = Application.GetSystemVariable("TARGET");

posted on 2023-04-14 15:06  因思道客  阅读(253)  评论(0编辑  收藏  举报

导航