Jump to content

How to insert named view onto sheet in VBA


jacksoncarl5

Recommended Posts

I have a named view "name3" that I want to insert on my layout sheet Layout2. I could not find a way to insert it directly the best I could do is the code below where I insert a viewport and hardcode the target, however I get very mixed results and the viewports generated do not match the named view, so I would really like to insert it directly.

Sub InsertNamedViewIntoLayout()
    Dim doc As AcadDocument
    Dim layout As AcadLayout
    Dim view As AcadView
    Dim viewName As String
    Dim layoutName As String
    Dim layoutIndex As Integer
    Dim mtextObj As AcadMText
    ' Get the current document
    Set doc = ThisDrawing

    ' Specify the layout name where you want to insert the named view
    layoutName = "Layout2"

    ' Find the layout by name and get its index
    For layoutIndex = 0 To doc.Layouts.Count - 1
        If doc.Layouts.Item(layoutIndex).Name = layoutName Then
            Exit For
        End If
    Next layoutIndex

    ' Specify the named view to insert
    viewName = "name3"

    ' Find the named view
    For Each view In doc.Views
        If view.Name = viewName Then
            ' Activate the layout
            ThisDrawing.ActiveLayout = ThisDrawing.Layouts("Layout2")

            ' Get the layout object again after activation
            Set layout = doc.Layouts.Item(layoutIndex)

            ' Insert the named view into the layout
            Dim insertPoint(0 To 2) As Double
            insertPoint(0) = 0
            insertPoint(1) = 0

            ' Insert layout view here
            Dim pViewport As AcadPViewport
            Set pViewport = layout.block.AddPViewport(insertPoint, 1, 1)

            ' Link viewport to modelspace
    Dim direction(0 To 2) As Double
    direction(0) = 0
    direction(1) = 0
    direction(2) = 0
    
            
            pViewport.direction = direction
            
    
            
            Dim target(0 To 2) As Double
            target(0) = 872369 - 129
            target(1) = 1266978
            target(2) = 0
            pViewport.target = target
            pViewport.ViewportOn = True
            
           pViewport.CustomScale = 1 / 96
            
            
            
            pViewport.Height = view.Height ' You might need to adjust this value according to your model size
            pViewport.Width = view.Width ' You might need to adjust this value according to your model size

            ' Add text to the layout
            Set mtextObj = layout.block.AddMText(insertPoint, 20, "Hello World")
            mtextObj.Height = 200
            mtextObj.BackgroundFill = True

            ' Regenerate the layout to update changes
            doc.Regen acAllViewports

            ' Refresh the screen
            doc.Application.ZoomAll

            Exit For
        End If
    Next view

    ' Cleanup
    Set doc = Nothing
    Set layout = Nothing
    Set view = Nothing
End Sub

 

Link to comment
Share on other sites

I have tried setting pViewport.target = view.target and pViewport.direction = view.direction but this did not work either, according to the debugger view.direction = (0,0,1) and view.target is (0,0,0) but this is not what is in the named view the named view is centered at the hardcoded values.

Link to comment
Share on other sites

I scratch the surface of VBA but look carefully at code below Dim insertPoint(0 To 2) As Double I think that is where your problem is. 

Link to comment
Share on other sites

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...