Author Topic: How can .net api Set dwg's plotconfigration?  (Read 3045 times)

0 Members and 1 Guest are viewing this topic.

ahopera

  • Guest
How can .net api Set dwg's plotconfigration?
« on: September 23, 2009, 06:43:28 AM »
I use the followingCode that always make CAD system crashed,So it maybe the "LayOutDictionary" can't open for write, how could i Modify the dwgfile's Plotconfigration? such as CanonicalMediaName,etc
the code is as the following:

       Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
        Using trans As Transaction = db.TransactionManager.StartTransaction
            LayOuts = trans.GetObject(db.LayoutDictionaryId, OpenMode.Forwrite)
   end using


n.yuan

  • Bull Frog
  • Posts: 347
Re: How can .net api Set dwg's plotconfigration?
« Reply #1 on: September 23, 2009, 02:16:18 PM »
Have you looked AutoCAD .NET Developer Guide at http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer's%20Guide/index.html?
Go to "Define Layouts and Plot" section: there is sample code for you to get started with layout/plotting thing.

HTH

I use the followingCode that always make CAD system crashed,So it maybe the "LayOutDictionary" can't open for write, how could i Modify the dwgfile's Plotconfigration? such as CanonicalMediaName,etc
the code is as the following:

       Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
        Using trans As Transaction = db.TransactionManager.StartTransaction
            LayOuts = trans.GetObject(db.LayoutDictionaryId, OpenMode.Forwrite)
   end using



mcarson

  • Guest
Re: How can .net api Set dwg's plotconfigration?
« Reply #2 on: September 30, 2009, 04:16:28 PM »
I use the followingCode that always make CAD system crashed,So it maybe the "LayOutDictionary" can't open for write, how could i Modify the dwgfile's Plotconfigration? such as CanonicalMediaName,etc
the code is as the following:

       Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
        Using trans As Transaction = db.TransactionManager.StartTransaction
            LayOuts = trans.GetObject(db.LayoutDictionaryId, OpenMode.Forwrite)
   end using



I have had a problem or two with this myself! See if the following helps...

Still work in progress.

Code: [Select]
''' <summary>
    ''' Plot to file
    ''' </summary>
    ''' <returns>True if file was successfully written</returns>
    Private Function Plot(ByVal PlotConfigurationName As String, ByVal OutputFileName As String) As Boolean
      'Get a pointer to the current document
      Dim doc As AcAp.Document = AcAp.Application.DocumentManager.MdiActiveDocument
      'Get the active document's database
      Dim db As AcDb.Database = doc.Database

      'Start a new transaction to determine what to plot
      Using tr As AcDb.Transaction = db.TransactionManager.StartTransaction
        'Get the block table record of the active space (Modelspace or PaperSpace)
        Dim btr As AcDb.BlockTableRecord = DirectCast(tr.GetObject(db.CurrentSpaceId, AcDb.OpenMode.ForRead), AcDb.BlockTableRecord)
        'Get the layout
        Dim lay As AcDb.Layout = DirectCast(tr.GetObject(btr.LayoutId, AcDb.OpenMode.ForRead), AcDb.Layout)

        'Get the current canonical media size
        Dim LayoutSize As String = GetCanonicalFromPaperSize()

        'Create a new plot settings object and copy all settings from the current layout
        Dim ps As New AcDb.PlotSettings(lay.ModelType)
        ps.CopyFrom(lay)

        'Set the plot options for auto plot
        Dim psv As AcDb.PlotSettingsValidator = AcDb.PlotSettingsValidator.Current
        'Set plot extents
        psv.SetPlotType(ps, AcDb.PlotType.Extents)
        'Make sure the layout can fit on output by setting scale to fit
        psv.SetUseStandardScale(ps, True)
        psv.SetStdScaleType(ps, AcDb.StdScaleType.ScaleToFit)
        'Center the plot on the outputted page
        psv.SetPlotCentered(ps, True)
        'Set the plot configuration for the plot
        'psv.GetLocaleMediaName(ps, LayoutSize)

        psv.SetPlotConfigurationName(ps, PlotConfigurationName, LayoutSize)

        'Check the plot style mode of the current drawing and set the plot style table accordingly
        If AcAp.Application.GetSystemVariable("PSTYLEMODE") = 1 Then
          'psv.SetCurrentStyleSheet(ps, "WAF.stb")
        Else
          'psv.SetCurrentStyleSheet(ps, "WAF.ctb")
        End If

        'Create a new plot info object
        Dim pi As New AcPl.PlotInfo
        pi.Layout = btr.LayoutId
        pi.OverrideSettings = ps

        'Ensure that the new media size is matched to the previous one
        Dim piv As New AcPl.PlotInfoValidator
        piv.MediaMatchingPolicy = AcPl.MatchingPolicy.MatchEnabled
        piv.Validate(pi)

        'Turn background plot off
        AcAp.Application.SetSystemVariable("BACKGROUNDPLOT", 0)

        'Only continue if we are not plotting
        If AcPl.PlotFactory.ProcessPlotState = AcPl.ProcessPlotState.NotPlotting Then
          Dim pe As AcPl.PlotEngine = AcPl.PlotFactory.CreatePublishEngine
          Using pe

            Dim ppd As New AcPl.PlotProgressDialog(False, 1, False)
            Using ppd
              ppd.PlotMsgString(AcPl.PlotMessageIndex.DialogTitle) = "WAF Publish Progress"
              ppd.PlotMsgString(AcPl.PlotMessageIndex.CancelJobButtonMessage) = "Cancel Job"
              ppd.PlotMsgString(AcPl.PlotMessageIndex.CancelSheetButtonMessage) = "Cancel Sheet"
              ppd.PlotMsgString(AcPl.PlotMessageIndex.SheetSetProgressCaption) = "Sheet Set Progress"
              ppd.PlotMsgString(AcPl.PlotMessageIndex.SheetProgressCaption) = "Plot Progress"
              ppd.LowerPlotProgressRange = 0
              ppd.UpperPlotProgressRange = 100
              ppd.PlotProgressPos = 0

              'Let's start the plot, at last
              ppd.OnBeginPlot()
              ppd.IsVisible = True
              pe.BeginPlot(ppd, Nothing)

              ' We'll be plotting a single document
              ' Let's plot to file
              pe.BeginDocument(pi, doc.Name, Nothing, 1, True, OutputFileName)

              ' Which contains a single sheet
              ppd.OnBeginSheet()
              ppd.LowerSheetProgressRange = 0
              ppd.UpperSheetProgressRange = 100
              ppd.SheetProgressPos = 0

              Dim ppi As New AcPl.PlotPageInfo()
              pe.BeginPage(ppi, pi, True, Nothing)
              pe.BeginGenerateGraphics(Nothing)
              pe.EndGenerateGraphics(Nothing)
              ' Finish the sheet
              pe.EndPage(Nothing)
              ppd.SheetProgressPos = 100
              ppd.OnEndSheet()

              ' Finish the document
              pe.EndDocument(Nothing)

              ' And finish the plot
              ppd.PlotProgressPos = 100
              ppd.OnEndPlot()
              pe.EndPlot(Nothing)

            End Using
          End Using
        End If
        'Turn background plotting back on
        AcAp.Application.SetSystemVariable("BACKGROUNDPLOT", 0)
      End Using

      'Put this code to sleep for 2 seconds to wait for any file locks on the output to be removed
      'System.Threading.Thread.Sleep(2000)

      'Check if the file exists, if so, return true and exit
      If System.IO.File.Exists(OutputFileName) Then
        Return True
      Else
        Return False
      End If
    End Function

    Private Function GetCanonicalFromPaperSize() As String
      Dim result As String = Nothing

      Dim doc As Autodesk.AutoCAD.Interop.AcadDocument = AcAp.Application.DocumentManager.MdiActiveDocument.AcadDocument
      Dim lay As Autodesk.AutoCAD.Interop.Common.AcadLayout = doc.ActiveLayout

      Dim PaperWidth As Double = 0.0#
      Dim PaperHeight As Double = 0.0#

      lay.GetPaperSize(PaperWidth, PaperHeight)

      PaperWidth = Math.Round(PaperWidth)
      PaperHeight = Math.Round(PaperHeight)

      Select Case PaperWidth
        Case 210
          Select Case PaperHeight
            Case 297
              result = "ISO_A4_(210.00_x_297.00_MM)"
          End Select
        Case 297
          Select Case PaperHeight
            Case 210
              result = "ISO_A4_(297.00_x_210.00_MM)"
            Case 420
              result = "ISO_A3_(297.00_x_420.00_MM)"
          End Select
        Case 420
          Select Case PaperHeight
            Case 297
              result = "ISO_A3_(420.00_x_297.00_MM)"
            Case 594
              result = "ISO_A2_(420.00_x_594.00_MM)"
          End Select
        Case 594
          Select Case PaperHeight
            Case 420
              result = "ISO_A2_(594.00_x_420.00_MM)"
            Case 841
              result = "ISO_A1_(594.00_x_841.00_MM)"
          End Select
        Case 841
          Select Case PaperHeight
            Case 594
              result = "ISO_A1_(841.00_x_594.00_MM)"
            Case 1189
              result = "ISO_A0_(841.00_x_1189.00_MM)"
          End Select
        Case 1189
          Select Case PaperHeight
            Case 841
              result = "ISO_A0_(841.00_x_1189.00_MM)"
          End Select
        Case Else
          If PaperWidth >= PaperHeight Then
            result = "ISO_A3_(420.00_x_297.00_MM)"
          Else
            result = "ISO_A3_(297.00_x_420.00_MM)"
          End If
      End Select
      Return result
    End Function

With the GetCanonicalName stuff; a nightmare!