Author Topic: Plot Settings  (Read 5017 times)

0 Members and 1 Guest are viewing this topic.

neyton

  • Newt
  • Posts: 68
Plot Settings
« on: October 04, 2010, 03:28:18 PM »
I'm making a program to set the print settings of the layouts in a drawing, but an error happens when you finish the program:

Code: [Select]
<CommandMethod("fazteste")>
    Public Sub teste()
        Dim wdb As Database = HostApplicationServices.WorkingDatabase
        Dim file As String = "D:\desenho.dwg"

        Dim db As New Database(False, True)
        db.ReadDwgFile(file, System.IO.FileShare.None, False, Nothing)

        HostApplicationServices.WorkingDatabase = db
        Dim acLayoutMgr As LayoutManager = LayoutManager.Current

        With db.TransactionManager.StartTransaction
            Dim layoutDict As DBDictionary = db.LayoutDictionaryId.GetObject(OpenMode.ForRead)
            ''em todos os layouts exceto o Model, faça:
            For Each id As DictionaryEntry In layoutDict
                Dim acLayout As Layout = id.Value.GetObject(OpenMode.ForRead)
                If acLayout.LayoutName = "Model" Then Continue For
                Try
                    Dim curr As String = acLayoutMgr.CurrentLayout

                    acLayoutMgr.CurrentLayout = acLayout.LayoutName

                    '' Get the PlotInfo from the 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(False)
                    acPlSet.CopyFrom(acLayout)

                    '' Update the PlotSettings object
                    Dim acPlSetVdr As PlotSettingsValidator = PlotSettingsValidator.Current

                    '' Set the plot type
                    acPlSetVdr.SetPlotWindowArea(acPlSet, New Extents2d(0, 0, 840, 594))

                    acPlSetVdr.SetPlotType(acPlSet, Autodesk.AutoCAD.DatabaseServices.PlotType.Window)

                    '' Set the plot scale
                    acPlSetVdr.SetUseStandardScale(acPlSet, True)
                    acPlSetVdr.RefreshLists(acPlSet)
                    acPlSetVdr.SetStdScaleType(acPlSet, StdScaleType.StdScale1To1)
                    acPlSetVdr.RefreshLists(acPlSet)
                    acPlSetVdr.SetPlotPaperUnits(acPlSet, PlotPaperUnit.Millimeters)
                    acPlSetVdr.RefreshLists(acPlSet)
                    acPlSetVdr.SetPlotRotation(acPlSet, PlotRotation.Degrees000)
                    acPlSetVdr.RefreshLists(acPlSet)
                    acPlSetVdr.SetCurrentStyleSheet(acPlSet, "penas.ctb")
                    acPlSetVdr.RefreshLists(acPlSet)
                    '' Center the plot
                    acPlSetVdr.SetPlotCentered(acPlSet, True)
                    acPlSetVdr.RefreshLists(acPlSet)
                    '' Set the plot device to use
                    acPlSetVdr.SetPlotConfigurationName(acPlSet, "DesignJet 755CM C3198B.pc3", "ISO_expand_A1_(594.00_x_841.00_MM)")
                    acPlSetVdr.RefreshLists(acPlSet)
                    '' Validate the plot info
                    Dim acPlInfoVdr As PlotInfoValidator = New PlotInfoValidator()
                    acPlInfoVdr.MediaMatchingPolicy = MatchingPolicy.MatchEnabled
                    acPlInfoVdr.Validate(acPlInfo)
                    acLayout.CopyFrom(acPlSet)
                    acLayoutMgr.CurrentLayout = curr
                Catch
                    ED.WriteMessage(vbLf & Err.Description)
                End Try
            Next
            .Commit()
        End With
        acLayoutMgr.Dispose()
        db.SaveAs(file, True, DwgVersion.Current, db.SecurityParameters)
        db.Dispose()
        MsgBox("ok")
        HostApplicationServices.WorkingDatabase = wdb
    End Sub

the program comes to making changes, but causes a fatal error and closes autocad
What is missing?

the drawing, the printer and paper size there and the program comes to show the message "ok" at the end
Visit my website: http://tbn2.blogspot.com

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Plot Settings
« Reply #1 on: October 04, 2010, 03:38:33 PM »
Why are you disposing the Layout Manager?  That would be my guess to the issue.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Plot Settings
« Reply #2 on: October 04, 2010, 03:53:09 PM »
Here is some code I have that will copy plot settings from another drawing
You can eaisly modify it if you do not know the name of the plot settings.

Code: [Select]

 Public Const PLOTSETTINGSFILE As String = "C:\Users\Jeff\Desktop\PlotDrawing.dwg"
        <CommandMethod("AddPlotSetting")> _
        Public Sub AddPlotSetting()
            Dim CurrentDatabase As Database = HostApplicationServices.WorkingDatabase
            Dim SourceDatabase As New Database(False, True)
            SourceDatabase.ReadDwgFile(PLOTSETTINGSFILE, FileOpenMode.OpenForReadAndAllShare, True, "")
            Using currentTransaction As Transaction = CurrentDatabase.TransactionManager.StartTransaction
                Using sourceTransaction As Transaction = SourceDatabase.TransactionManager.StartTransaction
                    Dim sourcePlotDic As DBDictionary = SourceDatabase.PlotSettingsDictionaryId.GetObject(OpenMode.ForRead)
                    Dim objID As ObjectId
                    If sourcePlotDic.Contains("HCS PDF") Then
                        objID = sourcePlotDic.GetAt("HCS PDF")
                        Dim pl As PlotSettings = objID.GetObject(OpenMode.ForRead)
                        Dim cpl As New PlotSettings(False)
                        cpl.CopyFrom(pl)
                        cpl.AddToPlotSettingsDictionary(CurrentDatabase)
                        Dim bt As BlockTable = CurrentDatabase.BlockTableId.GetObject(OpenMode.ForRead)
                        Dim btr As BlockTableRecord = bt(BlockTableRecord.PaperSpace).GetObject(OpenMode.ForRead)
                        Dim lytobjID As ObjectId = btr.LayoutId.GetObject(OpenMode.ForRead).ObjectId
                        Dim lytps As PlotSettings = lytobjID.GetObject(OpenMode.ForWrite)
                        lytps.CopyFrom(cpl)
                    End If
                    currentTransaction.Commit()
                End Using
            End Using
        End Sub

***********Edited *********
Imports Statements
Code: [Select]
Imports System
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.ApplicationServices.Application

***********Edited *********

***********Edited *********
If anyone wants the C# of it let me know my old VB is giving me a headache.
***********Edited *********
« Last Edit: October 04, 2010, 04:01:04 PM by Jeff H »

neyton

  • Newt
  • Posts: 68
Re: Plot Settings
« Reply #3 on: October 05, 2010, 07:19:46 AM »
google translates... (hehehe)

disposing or not disposing the Layout Manager causes error in the same way...

Jeff H, the code provides for a change in only one layout, I need it to change all the layouts, ie the various paper spaces

I'll try to adapt it, thanks!
Visit my website: http://tbn2.blogspot.com

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Plot Settings
« Reply #4 on: October 05, 2010, 12:50:01 PM »
I'm not VB fluent, so this question might be dumb, but does ' End With ' dispose of the transaction?

Jeff H uses the ' using ' call, and that disposes of the transaction, and that is how I do it in C#, so I thinking that you are not disposing the transaction, and maybe that is the issue.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Plot Settings
« Reply #5 on: October 05, 2010, 01:28:39 PM »
The End Using will call dispose on the transaction.

C# }                                                   C#                             VB
VB End Using                                      void YadaYada             Sub YadaYada
                                                            {                            End Sub
C# }                                                      }
VB End Class                                          Void ,Sub have no return values

and so on.....
« Last Edit: October 05, 2010, 01:32:43 PM by Jeff H »

neyton

  • Newt
  • Posts: 68
Re: Plot Settings
« Reply #6 on: October 07, 2010, 03:08:40 PM »
remove the line:

db.Dispose()

and works!!
Visit my website: http://tbn2.blogspot.com

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Plot Settings
« Reply #7 on: October 07, 2010, 03:29:12 PM »
I think you should leave that line since you created the New Database.  If you New something, then you should Dispose of it.  If Acad New's something, then it should Dispose of it.

I think you should add a ' .Dispose() ' after your ' .Commit() ' line, and see if that works.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.