Error trying to create page setup

Error trying to create page setup

dbrblg
Collaborator Collaborator
1,839 Views
7 Replies
Message 1 of 8

Error trying to create page setup

dbrblg
Collaborator
Collaborator

So, I'm trying to use this code in order to create a page setup in a drawing:

 

Function PageSetup()

    Dim sSetupName As String
    Dim oPlotConfig As AcadPlotConfiguration
    
    'Add new plot configuration
    sSetupName = "My_New_Setup"
    Set oPlotConfig = ThisDrawing.PlotConfigurations.Add(sSetupName)
    
    'Setup desired plot parameters
    oPlotConfig.PaperUnits = acMillimeters
    oPlotConfig.PlotType = acExtents
    oPlotConfig.PlotRotation = ac90degrees
    
    'Copy the new plot config parameters into the active layout
    Call ThisDrawing.ActiveLayout.CopyFrom(ThisDrawing.PlotConfigurations(sSetupName))
    'Call ThisDrawing.ActiveLayout.CopyFrom(ThisDrawing.PlotConfigurations(oPlotConfig.Name))
    'Call ThisDrawing.ActiveLayout.CopyFrom(oPlotConfig)
    
End Function

When I run this code, I get the following error:

 

Run-time error '2145386464 (80200020)'
Invalid object type

As you can see, from the last four lines of the code, I've tried various combinations of the same thing, however all of these produces the same errror.

 

Can anyone suggest where the issue lies please?

 

 

0 Likes
Accepted solutions (1)
1,840 Views
7 Replies
Replies (7)
Message 2 of 8

BestFriendCZ
Advocate
Advocate

Hi,

your code is correct but you have to call this code over active layout...otherwise you will get this type of error.

 

....So activate layout first

...
0 Likes
Message 3 of 8

Ed__Jobe
Mentor
Mentor

When you create a PlotConfiguration, it is assigned either modelspace type or paperspace type. You're not assigning the type when you create it, so when you try to use CopyFrom, its probably the wrong type. When you create the pc, specify the model type, e.g.

Set objPC = ThisDrawing.PlotConfigurations.Add("Name", True) 'True for MS, False for PS

Then specify only the AcadPlotconfiguration object in the CopyFrom method, as in your last example.

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 4 of 8

dbrblg
Collaborator
Collaborator

@Ed__JobeIf I understand correctly, I believe these are the modifications you believe necessary:

Function PageSetup()

    Dim sSetupName As String
    Dim oPlotConfig As AcadPlotConfiguration
    
    'Add new plot configuration
    sSetupName = "My_New_Setup"
    Set oPlotConfig = ThisDrawing.PlotConfigurations.Add(sSetupName, True) '<<====== Here
    
    'Setup desired plot parameters
    oPlotConfig.PaperUnits = acMillimeters
    oPlotConfig.PlotType = acExtents
    oPlotConfig.PlotRotation = ac90degrees
    
    'Copy the new plot config parameters into the active layout
    Call ThisDrawing.ActiveLayout.CopyFrom(oPlotConfig) '<<====== And here
    
End Function

This does not work unless I select the layout as per BestFriendCZ' reply....

 

The aim of the exercise for me is to create a pagelayout for the model space to get rid of the 'layout not initialized' error and also to set the correct printers, settings, sheets etc...

Therefore, I'm not sure I am doing the right thing in selectign the layout first??????  How would you achieve achieve a new layout for the model?

 

Thanks

0 Likes
Message 5 of 8

Ed__Jobe
Mentor
Mentor
Accepted solution

When you use ActiveLayout, the layout is "selected". You just need to be sure that the Model tab is the Active layout. You can do this in code. Your above code should work if you had made the Model tab active before running it. I just iterate the Layouts collection. e.g. The following set a page setup for ALL layouts.

    'set a current "Page Setup" for the layout
    Set objPC = ThisDrawing.PlotConfigurations.Item(strPSSetup)
    For Each objLayout In ThisDrawing.Layouts
        If objLayout.ModelType = True Then
            If strMSSetup <> "**None**" Then
                objLayout.CopyFrom ThisDrawing.PlotConfigurations.Item(strMSSetup)
            Else
                objLayout.PlotType = acLimits
                objLayout.ViewToPlot = "PLOTM"
            End If
        ElseIf objLayout.ModelType = False And strPSSetup <> "**None**" Then
            objLayout.CopyFrom objPC
        End If
    Next objLayout

However, you don't need to iterate the Layouts collection, you can just grab the ms layout, e.g.

Set objLayout = ThisDrawing.Layouts.Item("Model")
objLayout.CopyFrom objPC

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 6 of 8

dbrblg
Collaborator
Collaborator

Hello @Ed__Jobe,

 

Set objLayout = ThisDrawing.Layouts.Item("Model")
objLayout.CopyFrom objPC

appears to work, mostly.  I had one particular drawing which gave the same error as before but I never got to the bottom of this.  Reopening the drawings looks like it may have fixed this.

 

However, I would have thought this would have worked but it doesn't:

Set objLayout = ThisDrawing.Layouts.Item("Layout1")
objLayout.CopyFrom objPC

Since I'm not "activating" the drawing per se, I am "activating" it via code in the same way as I am for a model, so I would have expected it to work just the same.

I'm getting the same error as before.  As the error suggests a layout is not selected, this would imply "layout1" is not being selected...

 

0 Likes
Message 7 of 8

Ed__Jobe
Mentor
Mentor

You can't use the same PlotConfiguration. You need to use one whose ModelType property is False.

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 8 of 8

dbrblg
Collaborator
Collaborator

@Ed__Jobeoh, yes, doh Smiley Surprised

0 Likes