Identifying Un-Initialised Layouts

Identifying Un-Initialised Layouts

dbrblg
Collaborator Collaborator
1,739 Views
9 Replies
Message 1 of 10

Identifying Un-Initialised Layouts

dbrblg
Collaborator
Collaborator

How do you detect an un-initialised layout?

 

I saw an article which suggested you could look at the number of viewports in a layout but this was written in .net and after translating to VBA, I cannot get this to work.

 

As a crude proof-of-concept, I tried this:

Private Function IsInitialized()
    Debug.Print ThisDrawing.Viewports.Count
End Function

I have a drawing with un-initialised layout and this always reports a count of 1 regardless of what I have activated and whether or not it is un-initialised.

 

Is there another way?

 

Thanks

0 Likes
Accepted solutions (1)
1,740 Views
9 Replies
Replies (9)
Message 2 of 10

BestFriendCZ
Advocate
Advocate

i don't know if i understand correct but if you want to know count of viewports try this

Sub TEST()
    Debug.Print (GetViewPortCount("Layout2"))
End Sub

Public Function GetViewPortCount(ByVal layoutName As Variant) As Integer
    Dim count As Integer
    count = 0
    For Each objLayout In ThisDrawing.Layouts
        If objLayout.Name = layoutName Then
            For i = 1 To objLayout.Block.count - 1
                Set Entity = objLayout.Block.Item(i)
                If TypeOf Entity Is AcadPViewport Then
                    count = count + 1
                End If
            Next
        End If
    Next
    GetViewPortCount = count
End Function

 

...
0 Likes
Message 3 of 10

dbrblg
Collaborator
Collaborator

@BestFriendCZ I am looking to determine if a drawing 'layout not initialised' and I understand (rightly or wrongly) that counting the number of viewports can determine whether the layout has been initialised or not.

 

Unfortunately, the code you provided indicates the same number of viewports when the drawing is uninitialised and initialised.

 

0 Likes
Message 4 of 10

BestFriendCZ
Advocate
Advocate

If your layout has only one viewport so both functions returns same value 1... if you create layout with more viewports so your function will return still 1 but second returns correct number of viewports.

 

What do you exactly mean that layout is indicate

- Do you want check if you active windows is layout or model?
- Do you want to know if layout is set model space(viewport is active) or paper space?

...
0 Likes
Message 5 of 10

dbrblg
Collaborator
Collaborator

I'm looking to determine this:

1.png

 

0 Likes
Message 6 of 10

norman.yuan
Mentor
Mentor
Accepted solution

It looks like AutoCAD did not do a very good job here: when a new layout is created (by clicking "+" at bottom beside the layout tabs), a new layout is created, and the current plot device name is assigned  (i.e. "ConfigName" property is set). However, the property "CanonicalMediaName" is not assigned (i.e. it is an empty string value), until the layout is activated (user clicks the layout tab to make it active), at this moment either AutoCAD assigns "CanonicalMediaName" to a default value (if the "Show Page Setup Manager for new layout" check box is not selected in "Options"); or the Page Setup Manager pops up to force user choose a print media.

 

In my opinion, it could be consider as a minor bug in AutoCAD side: if user did not check the box of "Show Page Setup Manager for new layout", when new layout is created, not only the default device name should be set, as it already does, but also "CanonicalMediaName" should be set to default without having to wait until user activates the layout.

 

Now that we know the reason why the "Publish" UI showing "Layout not initialized" (meaning the layout has not been activated for the first time), the workaround (assume you want to programmatically do the publishing), you can do one of the following:

 

1. Loop through all layouts to make each active once. This may result in time-consuming "Regen" what switching active layout, though. The code would be like:

Public Sub ActivateEveryLayout()
    
    Dim curLay As AcadLayout
    Dim curSpace As AcActiveSpace
    
    curSpace = ThisDrawing.ActiveSpace
    Set curLay = ThisDrawing.ActiveLayout
    
    Dim lay As AcadLayout
    ThisDrawing.ActiveSpace = acPaperSpace
    For Each lay In ThisDrawing.Layouts
        If UCase(lay.Name) <> "MODEL" Then
            ThisDrawing.ActiveLayout = lay
        End If
    Next
    
    ThisDrawing.ActiveSpace = curSpace
    ThisDrawing.ActiveLayout = curLay
    
End Sub

2. You can simply test each layout's "CanonicalMediaName", if it is empty, then it is not "initialized":

 

Dim lay as layout

For Each lay in ThisDrawing.Layouts

  If Len(lay.CanonicalMediaName) = 0 Then

     Msgbox "Layout """ & lay.Name & """ is not initialized!"

  End If

Next

 

HTH

 

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 7 of 10

BestFriendCZ
Advocate
Advocate

hmmm don't know for sure...when i create new document so layouts 1 and 2 are uninitialised. They have zero count block so ... it is little blind shot

Sub TEST()
    Debug.Print (CheckInitialised("Layout2"))
End Sub

Public Function CheckInitialised(ByVal layoutName As Variant) As Boolean
    For Each objLayout In ThisDrawing.Layouts
        If objLayout.Name = layoutName Then
            If objLayout.Block.count > 0 Then
                CheckInitialised = True
                Exit Function
            End If
        End If
    Next
    CheckInitialised = False
End Function
...
0 Likes
Message 8 of 10

Ed__Jobe
Mentor
Mentor

@BestFriendCZ Your function will always return False. Check the last line. You need to put the return value in an If..Then..Else statement.

 

oops. I didn't notice the Exit Sub line. Sorry.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 9 of 10

BestFriendCZ
Advocate
Advocate

no i did test it 

             CheckInitialised = True
                Exit Function
...
0 Likes
Message 10 of 10

norman.yuan
Mentor
Mentor

AcadLayout.Block.Count = 0 DOES NOT mean the layout is not publish-able (not initialized for publishing). A totally blank layout (nothing drawn on the PaperSpace) is still print/publish-able, as long as it has valid ConfigName and CanonicalMediaName set.

 

So, your "CheckInitialised()" function is not technically correct, but maybe practically good, because no one would want to just plot/publish a blank layout, in most cases.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes