Manipulating Paper Space Viewports with VBA

Manipulating Paper Space Viewports with VBA

ericanastas
Enthusiast Enthusiast
6,882 Views
3 Replies
Message 1 of 4

Manipulating Paper Space Viewports with VBA

ericanastas
Enthusiast
Enthusiast
I'm supporting a condo project where we have to create 4 blown up plan drawings of each unit, including a construction plan, rcp, finish plan, and ff&e plan. There are 110 unique units and we are putting two drawings on each sheet so that's a total of 220 sheets. I'm trying to create a VBA script to automate this process.


The steps I want the script to take include

1) Open an existing template file (with title block, and two paper space viewports)
2) For each viewport:
-Zoom extents to center the view
-Set the viewport scale to 1/4"
-VP Freeze specific layers depending on the drawing
3) Set the sheet number and sheet name (change attributes of a block)


I'm having the hardest time manipulating the viewports. Right now I've able to get it to work partially with a series of SendCommand call and a call to SendKeys to switch between viewports. See my code bellow.


'switches from model space to the "FULL SIZE" layout tab
ThisDrawing.SendCommand ("layout set ")
ThisDrawing.SendCommand ("FULL SIZE" & vbCr)

'Right viewport opens by default
ThisDrawing.SendCommand ("mspace ")

'zooms extents in the right viewport and sets scale
ThisDrawing.SendCommand ("Z E ")
ThisDrawing.SendCommand ("Z S .02083333333xp ")

'switches to the left viewport
SendKeys "^r", True
ThisDrawing.SendCommand ("Z E ") 'clears send keys

'zooms extents is left viewport
ThisDrawing.SendCommand ("Z E ")
ThisDrawing.SendCommand ("Z S .02083333333xp" & vbCr)


This is hardly a clean way of doing this, and I'd much rather use the object model directly rather then using SendCommand. Plus the script above gets stuck at the SendKeys command for some reason.

I'd really like to be able to select the left or right viewport directly rather then by luck that the right one become active when I send the "mspace" command. I've tried to select the view port by entity handle using the code bellow. But it gives the the error "Invalid argument filter list in Select". I"m not sure what I did wrong.

'Creates Selection Sets
Dim grpCode(0) As Integer
Dim dataVal(0) As Variant
Mode = acSelectionSetAll

Set LeftVPSelection = ThisDrawing.SelectionSets.Add("LeftVP")

grpCode(0) = 5 'type five is entity handle
dataVal(0) = "266bf" 'This is the entity handle for the left VP

LeftVPSelection.Select Mode, , , grpCode, dataVal
LeftVPSelection.Delete


If I can get that to work I'm assuming that next I would need to extract a single AcadPViewport object out of the selection set (should be item 0 because there should only be one object in the set). Then I would set this to the "ActivePViewPort" so I can mess with the scale. Message was edited by: aireq
0 Likes
6,883 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable
as long as you're ok with hardcoding a handle why not use objectid?

Set VpObject = oDoc.ObjectIdToObject(vp.ObjectID)

no need to mess with selection sets

or since hardcoding is *almost* as bad as SendCommand
and since you only have two vports you can just iterate pspace, get the
vports, use .Center or .Bounding box to determine right from left etc

to activate layouts use ctab

'switches from model space to the "FULL SIZE" layout tab
ThisDrawing.SetVariable("Ctab", "Full size")

look up .Mspace in help
read all the help sections on pviewports...there are samples to do all this
with no evil "SendCommand"

hth
mark

wrote in message news:5866458@discussion.autodesk.com...
I'm supporting a condo project where we have to create 4 blown up plan
drawings of each unit, including a construction plan, rcp, finish plan, and
ff&e plan. There are 110 unique units and we are putting two drawings on
each sheet so that's a total of 220 sheets. I'm trying to create a VBA
script to automate this process.


The steps I want the script to take include

1) Open an existing template file (with title block, and two paper space
viewports)
2) For each viewport:
-Zoom extents to center the view
-Set the viewport scale to 1/4"
-VP Freeze specific layers depending on the drawing
3) Set the sheet number and sheet name (change attributes of a block)


I'm having the hardest time manipulating the viewports. Right now I've able
to get it to work partially with a series of SendCommand call and a call to
SendKeys to switch between viewports. See my code bellow.


'switches from model space to the "FULL SIZE" layout tab
ThisDrawing.SendCommand ("layout set ")
ThisDrawing.SendCommand ("FULL SIZE" & vbCr)

'Right viewport opens by default
ThisDrawing.SendCommand ("mspace ")

'zooms extents in the right viewport and sets scale
ThisDrawing.SendCommand ("Z E ")
ThisDrawing.SendCommand ("Z S .02083333333xp ")

'switches to the left viewport
SendKeys "^r", True
ThisDrawing.SendCommand ("Z E ") 'clears send keys

'zooms extents is left viewport
ThisDrawing.SendCommand ("Z E ")
ThisDrawing.SendCommand ("Z S .02083333333xp" & vbCr)


This is hardly a clean way of doing this, and I'd much rather use the object
model directly rather then using SendCommand. Plus the script above gets
stuck at the SendKeys command for some reason.

I'd really like to be able to select the left or right viewport directly
rather then by luck that the right one become active when I send the
"mspace" command. I've tried to select the view port by entity handle using
the code bellow. But it gives the the error "Invalid argument filter list in
Select". I"m not sure what I did wrong.

'Creates Selection Sets
Dim grpCode(0) As Integer
Dim dataVal(0) As Variant
Mode = acSelectionSetAll

Set LeftVPSelection = ThisDrawing.SelectionSets.Add("LeftVP")

grpCode(0) = 5 'type five is entity handle
dataVal(0) = "266bf" 'This is the entity handle for the left VP

LeftVPSelection.Select Mode, , , grpCode, dataVal
LeftVPSelection.Delete


If I can get that to work I'm assuming that next I would need to extract a
single AcadPViewport object out of the selection set (should be item 0
because there should only be one object in the set). Then I would set this
to the "ActivePViewPort" so I can mess with the scale.

Message was edited by: aireq
0 Likes
Message 3 of 4

ericanastas
Enthusiast
Enthusiast
Sweet that helps a lot thanks. I'm pretty new to using VBA so I didn't know about using objectID. How do I find the object ID of either of the exsisting viewports. I was using the List command to find the entity handle.


Also from reading a few other posts it sounds like the ObjectID may change where as the entity handle remain constant. Would it make more sense to use the AcadDocument.HandleToObject() prodecure instead?


Eric


Eric Message was edited by: aireq
0 Likes
Message 4 of 4

Anonymous
Not applicable
I would suggest forgetting about using handles or ObjectIds. Use what Mark
suggested as an alternative (which, IMHO, is a far superior approach).

Here's an example of how to do with the ActiveX API in lieu of what you were
doing with SendCommand. Note that it expects 2, and only 2, PSVports....if
there is only 1 or more than 2 this code will likely fail.

Sub SetupFullSizeTab()
Dim objVPLeft As AcadPViewport
Dim objVPRight As AcadPViewport
Dim objEnt As AcadEntity
Dim objLayout As AcadLayout
Dim bTest As Boolean
Dim xL As Double
Dim xR As Double

ThisDrawing.SetVariable "CTAB", "FULL SIZE"
For Each objEnt In ThisDrawing.ActiveLayout.Block
'this test skips the first object in the layout which is always _
the PS Vport of the layout itself
If bTest = True Then
If TypeOf objEnt Is AcadPViewport Then
If objVPLeft Is Nothing Then
Set objVPLeft = objEnt
Else
Set objVPRight = objEnt
xL = objVPLeft.Center(0)
xR = objVPRight.Center(0)
If xL > xR Then
Set objVPRight = objVPLeft
Set objVPLeft = objEnt
Exit For
Else
Exit For
End If
End If
End If
Else
bTest = True
End If
Next

''do whatever with the 2 viewports now
objVPLeft.DisplayLocked = False
objVPLeft.Display True
ThisDrawing.MSpace = True
ThisDrawing.ActivePViewport = objVPLeft
ThisDrawing.Application.ZoomExtents
objVPLeft.StandardScale = acVpCustomScale
objVPLeft.CustomScale = 0.25 / 12#
objVPLeft.DisplayLocked = True
objVPRight.DisplayLocked = False

ThisDrawing.ActivePViewport = objVPRight
ThisDrawing.Application.ZoomExtents
objVPRight.StandardScale = acVpCustomScale
objVPRight.CustomScale = 0.25 / 12#
objVPRight.DisplayLocked = True
ThisDrawing.MSpace = False

End Sub


wrote in message news:5866578@discussion.autodesk.com...
Sweet that helps a lot thanks. I'm pretty new to using VBA so I didn't know
about using objectID. How do I find the object ID of either of the exsisting
viewports. I was using the List command to find the entity handle.


Also from reading a few other posts it sounds like the ObjectID may change
where as the entity handle remain constant. Would it make more sense to use
the AcadDocument.HandleToObject() prodecure instead?


Eric


Eric

Message was edited by: aireq