Jump to content

Recommended Posts

Posted

Hi,

 

So, I've been trying write a program to go into a layout, select the viewport and they edit the viewport's various properties.

 

I have tried searching various combinations of viewports, vba, properties, edit and editing in the forum search and I have not been able to find a relavent thread.

 

My biggest difficulty is accessing the viewport itself. I can't seem to get the sytax correct...

 

Esentially, I'd like something that looks like:

Dim lyt As Object 'layout
Dim vp As Object 'viewport
For Each lyt In acaddoc.Layouts
   If lyt.Name <> "Model" Then
       'grab the layout as "vp", I tried something like: lyt.Viewport
       'vp.SomeProperty = WhatIWant
       'insert some other stuff
   End If
Next lyt

 

Also, I have been having trouble deciphering all of the viewport properties that I would need in order to:

-change the viewport's size and position in the current layout

-change where the viewport is "looking" into modelspace

 

I hope to hear from someone soon!

 

Thanks in advance.

Riddlez.

Posted

You appear to be accessing Autocad from the outside. If so, you may have to modify the "Thisdrawing" designation.

 

Sub PVPortManipulation()

'Iterate through the viewports of every paperspace Layout.
'Change the pviewports height to 6
'Change it to SE Iso view
'Move each paperspace vport 2 units to the right.
'Set vport scale
'Modify target


Dim Entity As AcadEntity
Dim VPcenter As Variant
Dim PVport As AcadPViewport
Dim objLayout As AcadLayout
Dim dblDir(0 To 2) As Double
Dim i As Integer
  For Each objLayout In ThisDrawing.Layouts
     If objLayout.Name <> "Model" Then
        ThisDrawing.ActiveLayout = objLayout
        ThisDrawing.MSpace = False
        For i = 1 To objLayout.Block.Count - 1 'avoid primary Paperspace PViewport
           Set Entity = objLayout.Block.Item(i)
           If TypeOf Entity Is AcadPViewport Then
              Set PVport = Entity
              PVport.Display True
              PVport.Height = 6
              dblDir(0) = -1#: dblDir(1) = -1#: dblDir(2) = 1#:
              PVport.Direction = dblDir
              VPcenter = PVport.Center
              VPcenter(0) = VPcenter(0) + 2#
              PVport.Center = VPcenter
              PVport.StandardScale = acVp1_10
              dblDir(0) = -6
              PVport.Target = dblDir
              PVport.Update
           End If
        Next
        ThisDrawing.Regen acAllViewports
     End If
  Next
End Sub

Posted

Thanks for the quick response SEANT!

 

1. a) The code looks quite helpful. However, if I create a new layout it would only have the primary layout and no other, correct? So if I am to use the primary viewport for my modifications, would I remove the "For Loop" meant to exclude it?

 

b) Even if that is the case, should I have any reservations about not 'playing' with the primary viewports? I am quite new to coding in VBA and AutoCAD in general, and didn't know something such as having more than one viewport in paperspace was a good thing or a bad thing.

 

2. Also, regarding "what the viewport is looking at", I can see that you are placing a unit vector to tell the viewport which way to 'look.' However, to clarify, VPcenter is to move the paperspace centre of the view port?

 

If so, then how would I adjust the modelspace centre of the viewport?

 

Thanks in advance again!

Riddlez.

Posted
Thanks for the quick response SEANT!

 

1. a) The code looks quite helpful. However, if I create a new layout it would only have the primary layout and no other, correct? So if I am to use the primary viewport for my modifications, would I remove the "For Loop" meant to exclude it?

 

 

By primary viewport I mean the one associated with the Layout View. In other words, even if there were no pviewports drawn in the layout, one (Layout View) would show up in the code. Fortunately it is always the first item in the layout’s Block so can be excluded by simply starting with the layout Block’s second item (i.e., For I = 1 . . . .)

 

b) Even if that is the case, should I have any reservations about not 'playing' with the primary viewports? I am quite new to coding in VBA and AutoCAD in general, and didn't know something such as having more than one viewport in paperspace was a good thing or a bad thing.

 

We learn a lot of new things by playing around. :)

 

 

2. Also, regarding "what the viewport is looking at", I can see that you are placing a unit vector to tell the viewport which way to 'look.' However, to clarify, VPcenter is to move the paperspace centre of the view port?

 

Correct.

 

If so, then how would I adjust the modelspace centre of the viewport?

 

The view center in modelspace is adjusted by the .Target property. To be honest, however, I's not easy to determine how to control it predictably. It is one of the arcane arts we’ve all come to love about AutoCAD VBA programming.

Posted

My first question is are you inside or outside of Autocad trying to do this?

Posted

That's the first thing I wondered as well. It does sound like Riddlez has a handle on it either way.

Posted

I guess to clarify:

 

I am accessing this from excel VBA. I have a handle on the sytax to get into the drawing and what not.

 

The reason for this is that I am using a spreadsheet as the basis for user inputs.

 

So far it's getting a lot more complicated than I thought.... The reasons being:

-A sample viewport says, when I add a watch to it, it's 19" high by 35" wide, but when I create the new viewport and enter those dimensions it appears more than twice the size of my layout :cry:

-Similar effect for the centre :x

-I cannot get the target to 'look' where I want it to. :glare:

 

I am going to take a look at something I found a while ago that I couldn't grasp on another site and see what I can figure out now that I a better basis from SEANT's help so far. It had something to do with something like "acad.thisdrawing.utilities" and a bunch of space conversions.

 

If you know what I'm talking about, by all means share. :)

Riddlez.

Posted

I found the other thread with the 'utility' I was talking about.

http://www.cadtutor.net/forum/showthread.php?t=18955

 

I have a hunch that LL & UR are intended to be the Lower Left and Upper Right corner locations.

 

However, I do not know if it is just an alternative to height and width.

 

I also don't know what the utility is doing in the two lines. I'm going to play with it a bit and look at what the help file says.

 

Riddlez.

Posted

HELLOO!!

 

My mentor of sorts had time to give me a hand to we figured something that works to pick your target:

 

Essentially, you take the viewport, activate the modelspace and then use the ZoomWindow command to pick your 'target.' (You will need the lower left corner and upper right corner of the space that you want see for ZoomWindow to work.)

 

Codewise:

 

Sub IncompleteCode ()

Dim LLCZ (0 to 2) As Double
Dim UPCZ (0 to 2) As Double
Dim acaddoc As Object
Dim acadapp As Object

Set acadapp = CreateObject("AutoCADLT.Application") 'only use if not open
Set acadapp = GetObject(, "AutoCADLT.Application")
Set acaddoc = acadapp.ActiveDocument

'retrieve your viewport some how

acaddoc.mspace = True
LLCZ(0) = 130.9999: LLCZ(1) = 2.25: LLCZ(2) = 0:
UPCZ(0) = 130.9999 + 32.5: UPCZ(1) = 2.25 + 19.25: UPCZ(2) = 0:
acadapp.ZoomWindow LLCZ, UPCZ
acaddoc.mspace = False

End Sub

 

Thanks again SEANT -I couldn't have gotten this far without your help.

 

I'll be checking back every now and then for any clarifications on my solution.

Riddlez.

Posted

That seems like a sensible enough solution. A possible issue would be if the viewport requires particular scale. If the extents of the ZoomWindow are known, then the viewport would have to be sized to allow for the appropriate ratio. Once that were achieved it would be prudent to set the viewports scale explicitly via PVport.StandardScale or PVport.CustomScale.

 

I should point out that the link you provided gives a good demonstration of the “arcane arts” I alluded to in my earlier post. As a matter of fact much of the non-intuitive aspects of AutoCAD VBA programming involve the TranslateCoordinates call.

 

Here would be an example of centering a viewport with a standard WCS Plan view:

 

Sub NewPVP()
Dim varTarg As Variant
Dim PVport As IAcadPViewport2
Dim objLayout As AcadLayout
Dim dblWTarg(0 To 2) As Double
Dim objView As AcadView
Dim varViewCtr As Variant
Dim dblCen(0 To 2) As Double
Dim dblTarg(0 To 2) As Double
  With ThisDrawing
     For Each objLayout In .Layouts
        If objLayout.Name = "Layout1" Then
           .ActiveLayout = objLayout
           varViewCtr = .GetVariable("VIEWCTR")
           Set PVport = .PaperSpace.AddPViewport(dblCen, 30, 20) 'Using a ARCH E sheet
           PVport.StandardScale = 4
           dblCen(0) = 24#
           dblCen(1) = 18#
           dblWTarg(0) = 60#: dblWTarg(1) = 30#: dblWTarg(2) = 0#: 'Modelspace center (WCS)
           PVport.Update
           PVport.Display True
           .MSpace = True
           PVport.Target = dblWTarg
           varTarg = .Utility.TranslateCoordinates(dblWTarg, acWorld, acDisplayDCS, 0)
           varTarg(0) = varTarg(0) - varViewCtr(0)
           varTarg(1) = varTarg(1) - varViewCtr(1)
           varTarg(2) = varTarg(2) - varViewCtr(2)
           varTarg = .Utility.TranslateCoordinates(varTarg, acDisplayDCS, acPaperSpace, 0)
           .MSpace = False
           PVport.Target = varTarg
           PVport.Center = dblCen
           PVport.Update
        End If
     Next
  End With
End Sub

Posted

Luckily for me I'm just using a 1:1 ratio, but your point is still an important point to make. I planned on developing my code so that you input a width and the program calculates a height depending on the height and width of the modelspace target.

 

I.e. VPHeight = VPWidth*TarHeight/TarWidth

 

Regarding your example of an the arcane art of using translate coordinates: I don't speak arcana.:P

 

If you wouldn't mind tossing in a few extra comments, it would be appreciated. (I'm having difficultly following a few of the variable names)

 

Just thought I'd add (to direct the 'audience' that you comments will be speaking to):

- I don't have a rigorous understanding of the various co-ordinate systems in AutoCAD or most of its many other nuances

-I have only been using the VB language since Septemeber and never thought this kind of stuff was possible

 

As a result here are some questions:

1. I noticed you use "#" in your numbers, that just means to use a displacement vs. an absolute position?

 

2. Is the comment, 'Using a ARCH E sheet, meant to justify the centre location of the new VP being, seemingly, far off?

 

3. Is the "Modelspace center (WCS)" the point in the model space that the VP is centred upon? (i.e. what I failed to figure out and therefore went to my zoom window method?)

 

4. What is "varViewCtr" retrieving in the line:

 varViewCtr = .GetVariable("VIEWCTR") 

 

Thanks again for all the help SEANT.:D

Riddlez

Posted

 

As a result here are some questions:

1. I noticed you use "#" in your numbers, that just means to use a displacement vs. an absolute position?

 

Actually the # is placed there automatically by the VBAIDE (try typing in 1.0, it will automatically change to 1#). The # is VBA's way of signifying a Double, as in non-integer numbers.

 

2. Is the comment, 'Using a ARCH E sheet, meant to justify the centre location of the new VP being, seemingly, far off?

 

Yes. I was afraid you would try it on a standard 8 1/2 x 11 sheet and the Viewport would end up halfway to the next cubicle.:)

 

3. Is the "Modelspace center (WCS)" the point in the model space that the VP is centred upon? (i.e. what I failed to figure out and therefore went to my zoom window method?)

 

Yup. I just picked an arbitrary position, where a drawing feature may be centered, perhaps.

 

4. What is "varViewCtr" retrieving in the line:

 varViewCtr = .GetVariable("VIEWCTR") 

 

That is one of AutoCADs System Variables. FYI Any system variable can be queried by typing the name i.e., VIEWCTR at the command prompt. That particular variable gives the point, in either Model or Paperspace, which is dead center of the current view. It's required to compute the required Target point.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...