PDA

View Full Version : Named Views VB Code Clarification



danderson.71652
2004-10-13, 02:47 PM
OK, I have tried to research the help files, and the NG, but I have yet to
determine why when I create a named view via:

Set viewObj = ThisDrawing.Views.Add("currentview")

The view thats created does not have the same view as if I did it from the
command prompt.

-view;s;currentview; <---------------This is the outcome I want

I just want the current view to be created. It seams the center point is
always different.

I know I can simply use the send command, but I do not wish to. I know you
can define a center point, but how can this be done if your view changes all
the time. The location will be determined as the user initiate the macro,
then I wish to run some code, and return the user to where they started.
Then the view is deleted, so it is not a permanent.

This doesn't work:


Sub test_view()

Dim viewObj As IAcadView2
Dim dCPt(1) As Double
Dim vCtr As VariantDim vSize As String
vCtr = ThisDrawing.GetVariable("viewctr")
vSize = ThisDrawing.GetVariable("viewsize")
dCPt(0) = vCtr(0)dCPt(1) = vCtr(1)
Set viewObj = ThisDrawing.Views.Add("TEST")
With viewObj
Center = dCPt
End With
End Sub


??????

Thanks for clearing this up,
Dan

RobertB
2004-10-13, 08:16 PM
What space are you trying this in?

mtuersley
2004-10-13, 08:24 PM
What space are you trying this in?

Looks like 2005 - Layout based upon his line:

Dim viewObj As IAcadView2

Only 2005 has IAcadView2 and the only reason to use it is to set the LayoutID.

danderson.71652
2004-10-13, 10:38 PM
Modelspace is what I am currently wanting it for....
When I define the view, the centerpoint is way off....

mtuersley
2004-10-14, 04:09 AM
First off, I know you're learning and doing a good job of it, but be careful grabbing code from posts. The IAcadView2 object is undocumented and intended only for 2005 and creating views in a layout. You shouldn't be using it for model space.

Second, the info you recv'd through the newsgroups is close but not quite there. In order to get the exact view, you MUST supply a height AND WIDTH - otherwise AutoCAD extrapolates the size for you. So, to solve this problem we'll use basic geometry. We can get the AREA variable which stores the area of the current view. Since A = L x W, we'll just solve for W since we know the area and the height:

Sub ReCreateCurrentView()
Dim oV As AcadView
Dim vCPt As Variant
Dim dArea As Double
Dim dHgt As Double
With ThisDrawing
vCPt = .GetVariable("VIEWCTR")
dHgt = .GetVariable("VIEWSIZE")
dArea = .GetVariable("AREA")
Set oV = .Views.Add("DAN")
ReDim Preserve vCPt(1)
oV.Center = vCPt
oV.Height = dHgt
oV.Width = dArea / dHgt
End With
End Sub

Notice that the view requires a 2D point but the VIEWCTR variable returns a 3D point. I am redefining the variable with the preserve option to convert the 3D point to 2D.

RobertB
2004-10-14, 04:39 AM
This ought to get you close. Note that attempting to create views for AcadView in a Layout via ActiveX just doesn't work. This sample code is most accurate when there are no toolbar/palettes attached at the sides of the application window.


Sub Test()
Dim myView As AcadView
Set myView = ThisDrawing.Views.Add("Test")

Dim viewCtr
viewCtr = ThisDrawing.GetVariable("ViewCtr")
ReDim Preserve viewCtr(0 To 1)
With myView
.Center = viewCtr
.Height = ThisDrawing.GetVariable("ViewSize")

Dim scrSize
scrSize = ThisDrawing.GetVariable("ScreenSize")
Dim factor As Double
factor = .Height / scrSize(1)
.Width = scrSize(0) * factor
End With
End Sub

mtuersley
2004-10-14, 04:47 AM
Slick code Bob!

But, I beg to differ on the views in Layout ;-) It works like a charm using either IAcadView2 [if you like intellisense] or plain AcadView in 2005. Just set the LayoutID to the ObjectID of the Layout you're creating the view on.

Also, Dan, I should add that this solution is better than mine because I omitted an important fact --- it'll only work if the user hasn't used the AREA command. The Area variable stores the area of the current view until someone uses the area command and then it retains that area. So, if your users use the area command, you're stuck with the extrapolated view.

danderson.71652
2004-10-14, 03:39 PM
Thank you very much gentlemen,
I have been learning from the school of hard knocks, and have just recently been given the support to really dive into learning VB, and obtaining the materials necessary. These Newsgroups, and help files have been the source of my education, and I have learned very much by example. I have been using AutoCAD for over 12 years, but never dove into the VB side of things....WOW!

I really do appreciate your time, and sharing to help others like myself.

I hope to be able to contribute back soon.

Thanks again,
Dan

RobertB
2004-10-16, 03:58 PM
Slick code Bob!

But, I beg to differ on the views in Layout ;-) It works like a charm using either IAcadView2 [if you like intellisense] or plain AcadView in 2005. Just set the LayoutID to the ObjectID of the Layout you're creating the view on.

Also, Dan, I should add that this solution is better than mine because I omitted an important fact --- it'll only work if the user hasn't used the AREA command. The Area variable stores the area of the current view until someone uses the area command and then it retains that area. So, if your users use the area command, you're stuck with the extrapolated view.

Mike, thanks for the kudos!

However, on your begging to differ, I begger to differ <snort>. Run this code from a Layout:

Sub Test()
Dim myView As AcadView
Set myView = ThisDrawing.Views.Add("Test")
End Sub
Now bring up the View dialog box. Notice the new view's location? I haven't found a way to fix this.

mtuersley
2004-10-16, 09:35 PM
True! Thats because you didn't read my post! If you are running 2005, try this code:

Sub Test()
Dim myView As IAcadView2
Set myView = ThisDrawing.Views.Add("Test")
myView.LayoutID = ThisDrawing.ActiveLayout.ObjectID
End Sub

Now, the view should be associated with your active layout. You can also use straight AcadView.LayoutID as well but intellisense won't recognize LayoutID as a property. Again, this only works for 2005-based products.

RobertB
2004-10-17, 12:24 AM
Bah! I did read your post. And browsed the object in the object viewer. And myoptically missed the LayoutID property. <groan> I guess this means we are tied on this one? BwaHaHa!!!!

mtuersley
2004-10-17, 03:37 AM
A tie it is :D I've had those myoptic episodes, too....but next time bwahhahhah!