Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Named Views VB Code Clarification

  1. #1
    Active Member
    Join Date
    2004-07
    Posts
    76

    Question Named Views VB Code Clarification

    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:

    Code:
    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
    Last edited by RobertB; 2004-10-14 at 04:40 AM. Reason: To place code in code window

  2. #2
    Administrator RobertB's Avatar
    Join Date
    2001-08
    Location
    Dallas TX USA
    Posts
    5,825

    Default Re: Named Views VB Code Clarification

    What space are you trying this in?

  3. #3
    100 Club mtuersley's Avatar
    Join Date
    2001-12
    Posts
    122

    Default Re: Named Views VB Code Clarification

    Quote Originally Posted by RobertB
    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.

  4. #4
    Active Member
    Join Date
    2004-07
    Posts
    76

    Default Re: Named Views VB Code Clarification

    Modelspace is what I am currently wanting it for....
    When I define the view, the centerpoint is way off....

  5. #5
    100 Club mtuersley's Avatar
    Join Date
    2001-12
    Posts
    122

    Default Re: Named Views VB Code Clarification

    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.
    Last edited by mtuersley; 2004-10-14 at 04:25 AM.

  6. #6
    Administrator RobertB's Avatar
    Join Date
    2001-08
    Location
    Dallas TX USA
    Posts
    5,825

    Default Re: Named Views VB Code Clarification

    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.

    Code:
    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
    Last edited by RobertB; 2004-10-14 at 04:42 AM.

  7. #7
    100 Club mtuersley's Avatar
    Join Date
    2001-12
    Posts
    122

    Default Re: Named Views VB Code Clarification

    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.

  8. #8
    Active Member
    Join Date
    2004-07
    Posts
    76

    Smile Re: Named Views VB Code Clarification

    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

  9. #9
    Administrator RobertB's Avatar
    Join Date
    2001-08
    Location
    Dallas TX USA
    Posts
    5,825

    Default Re: Named Views VB Code Clarification

    Quote Originally Posted by mtuersley
    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:
    Code:
    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.

  10. #10
    100 Club mtuersley's Avatar
    Join Date
    2001-12
    Posts
    122

    Default Re: Named Views VB Code Clarification

    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.

Page 1 of 2 12 LastLast

Similar Threads

  1. Plotting Named Views
    By msmith.77557 in forum AutoCAD Plotting
    Replies: 2
    Last Post: 2008-11-22, 06:57 PM
  2. Named Views
    By fletch97 in forum AutoCAD General
    Replies: 2
    Last Post: 2005-08-30, 12:49 PM
  3. Exporting Named UCS views
    By eleonard in forum AutoCAD General
    Replies: 1
    Last Post: 2005-07-20, 10:22 PM
  4. Importing Named Views
    By crohloff.72988 in forum AutoCAD General
    Replies: 7
    Last Post: 2005-06-16, 12:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •