Author Topic: Create a Layout and Set PlotConfiguration  (Read 1671 times)

0 Members and 1 Guest are viewing this topic.

Offline rom1

  • Visual Basic
  • *
  • Posts: 21
  • Karma: +3/-0
  • Gender: Male
    • prefered language: VB
    • Prog expertise: Good
    • View Profile
Create a Layout and Set PlotConfiguration
« on: November 26, 2010, 11:33:14 AM »
Hi,

Here is a function (in vb.net) that i've created thanks to some examples and that i've adapted to my needs.
The function create a new layout with the name in parameter (the name is incremented with a counter if it already exists) and then the function change the plot settings (stylesheet, format, rotation, printername...)

Thanks for your remarks and improvements as i'm still a beginner with the .Net api.

Code: [Select]
''' <summary>
    ''' Create a new layout and set the layout plotsettings
    ''' </summary>
    ''' <param name="LayName">Layout Name</param>
    ''' <remarks></remarks>
    Public Sub CreateLayout(ByVal LayName As String)
        Dim ed As Editor = AcadAp.DocumentManager.MdiActiveDocument.Editor
        Dim db As Database = HostApplicationServices.WorkingDatabase

        Using tr As Transaction = db.TransactionManager.StartTransaction()
            Try
                Dim layouts As DBDictionary = DirectCast(tr.GetObject(db.LayoutDictionaryId, OpenMode.ForWrite), DBDictionary)

                ' Add a number at the end of layout name if already exists
                Dim i As Integer = 0
                Dim OriginalName As String = LayName
                Do While layouts.Contains(LayName)
                    i += 1
                    LayName = OriginalName & "-" & i
                Loop
                ' Create and activate the new layout
                Dim layoutId As ObjectId = LayoutManager.Current.CreateLayout(LayName)
                LayoutManager.Current.CurrentLayout = LayName

                'Plot Settings
                '' Get the PlotInfo from the layout
                Dim acLayout As Layout = DirectCast(tr.GetObject(layoutId, OpenMode.ForRead), Layout)
                Dim acPlInfo As PlotInfo = New PlotInfo()
                acPlInfo.Layout = acLayout.ObjectId

                '' Get a copy of the PlotSettings from the layout
                Dim acPlSet As PlotSettings = New PlotSettings(acLayout.ModelType)
                acPlSet.CopyFrom(acLayout)

                '' Update some properties of the PlotSettings object
                Dim acPlSetVdr As PlotSettingsValidator = PlotSettingsValidator.Current
                acPlSetVdr.SetPlotConfigurationName(acPlSet, "\\dani-print2\BE-Blanc_ps", "A4")
                acPlSetVdr.SetPlotType(acPlSet, Autodesk.AutoCAD.DatabaseServices.PlotType.Layout)
                acPlSetVdr.SetUseStandardScale(acPlSet, True)
                acPlSetVdr.SetPlotRotation(acPlSet, Autodesk.AutoCAD.DatabaseServices.PlotRotation.Degrees000)
                acPlSetVdr.SetCurrentStyleSheet(acPlSet, "Danisoft.ctb")

                '' Update the layout
                acLayout.UpgradeOpen()
                acLayout.CopyFrom(acPlSet)

                tr.Commit() 'Commit Transaction
            Catch ex As System.Exception
                Dim Msg As String = "Error in function CreateLayout"
                Dim Title As String = "Operation cancelled"
                MessageBox.Show(Msg, Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
                tr.Abort()
            End Try
        End Using
    End Sub



target audience:{beginner}

Offline AlexF

  • Newbie
  • *
  • Posts: 2
  • Karma: +0/-0
  • Gender: Male
    • prefered language: VB
    • Prog expertise: Good
    • View Profile
  • Twitter: @AlexFielder
Re: Create a Layout and Set PlotConfiguration
« Reply #1 on: December 09, 2010, 03:33:06 PM »
Hi,

I've borrowed heavily from this (and other sources) to create my own implementation, and, even though I can successfully create a layout, and set plot settings, I can't figure out why after my routine has finished the layout is offset from the drawing border I insert.

If I subsequently go into the plot settings dialogue, and select "center* plot" and click apply to layout, the layout then centres itself correctly.

Any ideas?

*stupid US Spelling!

Offline rom1

  • Visual Basic
  • *
  • Posts: 21
  • Karma: +3/-0
  • Gender: Male
    • prefered language: VB
    • Prog expertise: Good
    • View Profile
Re: Create a Layout and Set PlotConfiguration
« Reply #2 on: December 10, 2010, 07:50:53 AM »
Hi,

In the function I've posted, there is a lot of parameters that are not set, such as SetPlotCentered or SetPlotOrigin ; do you set these parameters in your code?

Could you maybe post your code, it will be easier to make tests?

Thanks

Offline AlexF

  • Newbie
  • *
  • Posts: 2
  • Karma: +0/-0
  • Gender: Male
    • prefered language: VB
    • Prog expertise: Good
    • View Profile
  • Twitter: @AlexFielder
Re: Create a Layout and Set PlotConfiguration
« Reply #3 on: December 10, 2010, 12:27:01 PM »
Here's my code:

Code: [Select]
//assumes we already created a layout with the ObjectId ltid
ObjectId ltid = lman.CreateLayout(layoutname);
Layout lt = (Layout)tr.GetObject(ltid, OpenMode.ForWrite);
{
lt.UpgradeOpen();
lt.Initialize();
//change to the one we need.
LayoutManager lytmgr = LayoutManager.Current;
lytmgr.CurrentLayout = layoutname;
//begin configuring plot settings
PlotInfo plotinf = new PlotInfo();
plotinf.Layout = lt.ObjectId;
PlotSettings plotset = new PlotSettings(lt.ModelType);
plotset.CopyFrom(lt);
PlotSettingsValidator plotsetvdr = PlotSettingsValidator.Current;
PlotInfoValidator plotvdr = new PlotInfoValidator();
Plotterused = "MyPrinter.pc3"; //for test purposes only
Papersize = "A3"; //for test purposes only
plotsetvdr.SetPlotConfigurationName(plotset, Plotterused, Papersize);
plotsetvdr.SetPlotType(plotset, Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);
plotsetvdr.SetUseStandardScale(plotset, true);
plotsetvdr.SetPlotRotation(plotset, PlotRotation.Degrees090);
plotsetvdr.SetPlotPaperUnits(plotset, PlotPaperUnit.Millimeters);
//if we centre the plot here,  even without then changing the origin, the resultant layout still isn't centred correctly.
plotsetvdr.SetPlotCentered(plotset, true);
//commented these lines out for now as they have no overall effect on the centring of the subsequent layout.
//Point2d offset = new Point2d(0, 0);
//plotsetvdr.SetPlotOrigin(plotset, offset);
//override the current settings?
plotinf.OverrideSettings = plotset;
//validate the new settings
plotvdr.Validate(plotinf);
//sets the layout plot settings
lt.CopyFrom(plotset);
lt.TabOrder = 1;
lt.DowngradeOpen();
ed.Regen();
}

I think my issue lies in the ordering of operations, unless I've overlooked a setting?