Here's my code that draws a polyline in model space at the extents of the viewport.
Public Sub VPExtentsBox()
'call w/o arguments
VPExtentsBox2
End Sub
Public Function VPExtentsBox2(Optional lwp As Variant, Optional CtrPnt As Variant)
'draws a box in MS representing the extents of the current PS viewport
'
' optional argument lwp returns the polyline
' optional argument CtrPnt returns the center of the box as a 3 element array of doubles
Dim oPsVp As AcadPViewport
Dim oBox As AcadLWPolyline
Dim dPt1(0 To 2) As Double
Dim dPt2(0 To 2) As Double
Dim vCtrPt As Variant
Dim vCtrPt1 As Variant
Dim vMinPoint As Variant
Dim vMaxPoint As Variant
Dim BBpoints(0 To 9) As Double 'Bounding box points list
If ThisDrawing.ActiveSpace = acPaperSpace Then 'first check for tilemode = 0
If ThisDrawing.MSpace = True Then 'then make sure ms is active
Set oPsVp = ThisDrawing.ActivePViewport
vCtrPt = ThisDrawing.GetVariable("viewctr")
VPCoords oPsVp, vMinPoint, vMaxPoint
BBpoints(0) = vMinPoint(0): BBpoints(1) = vMinPoint(1)
BBpoints(2) = vMaxPoint(0): BBpoints(3) = vMinPoint(1)
BBpoints(4) = vMaxPoint(0): BBpoints(5) = vMaxPoint(1)
BBpoints(6) = vMinPoint(0): BBpoints(7) = vMaxPoint(1)
BBpoints(8) = vMinPoint(0): BBpoints(9) = vMinPoint(1)
Set oBox = ThisDrawing.ModelSpace.AddLightWeightPolyline(BBpoints)
vCtrPt1 = MidPoint(vMinPoint, vMaxPoint)
dPt1(0) = vCtrPt1(0): dPt1(1) = vCtrPt1(1): dPt1(2) = vCtrPt1(2)
dPt2(0) = vCtrPt(0): dPt2(1) = vCtrPt(1): dPt2(2) = vCtrPt(2)
oBox.Move dPt1, dPt2
If Not IsMissing(lwp) Then Set lwp = oBox
If Not IsMissing(CtrPnt) Then
CtrPnt = vCtrPt
End If
Else
MsgBox "The active viewport must have ModelSpace active for this command to work.", vbExclamation, "Viewport Extents Box"
End If
Else
MsgBox "You must be in paperspace with the active viewport in ModelSpace for this command to work.", vbExclamation, "Viewport Extents Box"
End If
End Function
Public Sub VPCoords(vp As AcadPViewport, ll, ur)
'Calculates the extents of a PaperSpace viewport in ModelSpace units
'Arguments: An AcadPViewport entity and three variants.
' The two variants will be filled with the corner points.
Dim min, MAX
Dim oldMode As Boolean
vp.GetBoundingBox min, MAX
oldMode = ThisDrawing.MSpace
ThisDrawing.MSpace = True
ll = ThisDrawing.Utility.TranslateCoordinates(min, acPaperSpaceDCS, acDisplayDCS, False)
ll = ThisDrawing.Utility.TranslateCoordinates(ll, acDisplayDCS, acUCS, False)
ur = ThisDrawing.Utility.TranslateCoordinates(MAX, acPaperSpaceDCS, acDisplayDCS, False)
ur = ThisDrawing.Utility.TranslateCoordinates(ur, acDisplayDCS, acUCS, False)
ThisDrawing.MSpace = oldMode
End Sub
Public Function MidPoint(varPnt1 As Variant, varPnt2 As Variant) As Variant
Dim varMidPnt As Variant
varMidPnt = Array((varPnt1(0) + varPnt2(0)) / 2, _
(varPnt1(1) + varPnt2(1)) / 2, (varPnt1(2) + varPnt2(2)) / 2)
MidPoint = varMidPnt
End Function
Ed
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to
post your code.