Author Topic: Copy PlotSetting from External File  (Read 3327 times)

0 Members and 1 Guest are viewing this topic.

GumbyCAD

  • Newt
  • Posts: 84
Copy PlotSetting from External File
« on: March 17, 2014, 07:53:56 AM »
Hi Guys,

I tried one of the samples that was posted here and a error message that I could not explain.

eWrongObjectType was the exeception.

It happened here:
Code: [Select]
                        Dim cpl As New PlotSettings(False)
                        cpl.CopyFrom(pl)

Anyone got any ideas?

Original Code below posted on:
http://www.theswamp.org/index.php?topic=31867.msg398478#msg398478
(Thanks Jeff H.)

Code: [Select]
Public Sub AddPlotSettings(ByVal PlotSettingsFile As String, ByVal PlotSettingsName As String)
            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)
                    If sourcePlotDic.Contains(PlotSettingsName) Then
                        Dim objID As ObjectId = sourcePlotDic.GetAt(PlotSettingsName)
                        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

I do have to say this place is an awesome resource.  Hi Fives To All.  :mrgreen:

Jeff H

  • Needs a day job
  • Posts: 6151
Re: Copy PlotSetting from External File
« Reply #1 on: March 17, 2014, 10:44:08 AM »
Oops did not dispose the Database

GumbyCAD

  • Newt
  • Posts: 84
Re: Copy PlotSetting from External File
« Reply #2 on: March 17, 2014, 07:03:34 PM »
Jeff not sure how that would have an exception at:

Code: [Select]
                        Dim cpl As New PlotSettings(False)
                        cpl.CopyFrom(pl)

Or am I just missing something?

Jeff H

  • Needs a day job
  • Posts: 6151
Re: Copy PlotSetting from External File
« Reply #3 on: March 17, 2014, 07:17:51 PM »
I do not think so but just noticed it.

Jeff H

  • Needs a day job
  • Posts: 6151
Re: Copy PlotSetting from External File
« Reply #4 on: March 17, 2014, 07:35:13 PM »
Can you post the code that invokes it?

GumbyCAD

  • Newt
  • Posts: 84
Re: Copy PlotSetting from External File
« Reply #5 on: March 17, 2014, 07:43:15 PM »
I am using an exact copy of the sample.  (At least I don't I modified it...)

Code: [Select]
    Public Sub AddPlotSettings(ByVal PlotSettingsFile As String, ByVal PlotSettingsName As String)
        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 = CType(SourceDatabase.PlotSettingsDictionaryId.GetObject(OpenMode.ForRead), DBDictionary)
                If sourcePlotDic.Contains(PlotSettingsName) Then
                    Dim objID As ObjectId = sourcePlotDic.GetAt(PlotSettingsName)
                    Dim pl As PlotSettings = CType(objID.GetObject(OpenMode.ForRead), PlotSettings)
                    Dim cpl As New PlotSettings(False)
                    cpl.CopyFrom(pl)
                    cpl.AddToPlotSettingsDictionary(CurrentDatabase)
                    Dim bt As BlockTable = CType(CurrentDatabase.BlockTableId.GetObject(OpenMode.ForRead), BlockTable)
                    Dim btr As BlockTableRecord = CType(bt(BlockTableRecord.PaperSpace).GetObject(OpenMode.ForRead), BlockTableRecord)
                    Dim lytobjID As ObjectId = btr.LayoutId.GetObject(OpenMode.ForRead).ObjectId
                    Dim lytps As PlotSettings = CType(lytobjID.GetObject(OpenMode.ForWrite), PlotSettings)
                    lytps.CopyFrom(cpl)
                End If
                currentTransaction.Commit()
            End Using
        End Using
    End Sub

See attachments

GumbyCAD

  • Newt
  • Posts: 84
Re: Copy PlotSetting from External File
« Reply #6 on: March 17, 2014, 07:43:55 PM »
If I try to step to the next line I get the exeception

Jeff H

  • Needs a day job
  • Posts: 6151
Re: Copy PlotSetting from External File
« Reply #7 on: March 17, 2014, 09:01:15 PM »
Try this and what do the message boxes  say?

Code - Visual Basic: [Select]
  1.         Public Sub AddPlotSettings(ByVal PlotSettingsFile As String, ByVal PlotSettingsName As String)
  2.             Dim CurrentDatabase As Database = HostApplicationServices.WorkingDatabase
  3.             Dim layName As String = Nothing
  4.             Using SourceDatabase As New Database(False, True)
  5.                 SourceDatabase.ReadDwgFile(PlotSettingsFile, FileOpenMode.OpenForReadAndAllShare, True, "")
  6.                 Using currentTransaction As Transaction = CurrentDatabase.TransactionManager.StartTransaction
  7.  
  8.                     Using sourceTransaction As Transaction = SourceDatabase.TransactionManager.StartTransaction
  9.                         Dim sourcePlotDic As DBDictionary = SourceDatabase.PlotSettingsDictionaryId.GetObject(OpenMode.ForRead)
  10.                         If sourcePlotDic.Contains(PlotSettingsName) Then
  11.                             Dim objID As ObjectId = sourcePlotDic.GetAt(PlotSettingsName)
  12.                             Dim pl As PlotSettings = objID.GetObject(OpenMode.ForRead)
  13.                             Dim cpl As New PlotSettings(False)
  14.                             Application.ShowAlertDialog(pl.GetType.Name)
  15.                             Application.ShowAlertDialog(cpl.GetType.Name)
  16.                             cpl.CopyFrom(pl)
  17.                             cpl.AddToPlotSettingsDictionary(CurrentDatabase)
  18.                             Dim bt As BlockTable = CurrentDatabase.BlockTableId.GetObject(OpenMode.ForRead)
  19.                             Dim btr As BlockTableRecord = bt(BlockTableRecord.PaperSpace).GetObject(OpenMode.ForRead)
  20.                             Dim lay As Layout = btr.LayoutId.GetObject(OpenMode.ForWrite)
  21.                             lay.CopyFrom(cpl)
  22.                             layName = lay.LayoutName
  23.                         End If
  24.                         sourceTransaction.Commit()
  25.                     End Using
  26.                     currentTransaction.Commit()
  27.                 End Using
  28.  
  29.             End Using
  30.             If Not layName = Nothing Then
  31.                 LayoutManager.Current.CurrentLayout = layName
  32.             End If
  33.  
  34.         End Sub
  35.  

GumbyCAD

  • Newt
  • Posts: 84
Re: Copy PlotSetting from External File
« Reply #8 on: March 17, 2014, 09:11:49 PM »
Plot Settings, Plot Settings  :-o

Jeff H

  • Needs a day job
  • Posts: 6151
Re: Copy PlotSetting from External File
« Reply #9 on: March 17, 2014, 09:38:37 PM »
Your trying to copy plot settings for model space aren't you?

The constructor for PlotSettings takes a bool to indicate if model type.

You will need to update the function.

You can just check the plotsettings your coping ModelType property for setting constructor.

Something like

Code - Visual Basic: [Select]
  1.         Public Sub AddPlotSettings(ByVal PlotSettingsFile As String, ByVal PlotSettingsName As String)
  2.             Dim CurrentDatabase As Database = HostApplicationServices.WorkingDatabase
  3.             Dim layName As String = Nothing
  4.             Using SourceDatabase As New Database(False, True)
  5.                 SourceDatabase.ReadDwgFile(PlotSettingsFile, FileOpenMode.OpenForReadAndAllShare, True, "")
  6.                 Using currentTransaction As Transaction = CurrentDatabase.TransactionManager.StartTransaction
  7.  
  8.                     Using sourceTransaction As Transaction = SourceDatabase.TransactionManager.StartTransaction
  9.                         Dim sourcePlotDic As DBDictionary = SourceDatabase.PlotSettingsDictionaryId.GetObject(OpenMode.ForRead)
  10.                         If sourcePlotDic.Contains(PlotSettingsName) Then
  11.                             Dim objID As ObjectId = sourcePlotDic.GetAt(PlotSettingsName)
  12.                             Dim pl As PlotSettings = objID.GetObject(OpenMode.ForRead)
  13.                             Dim cpl As New PlotSettings(pl.ModelType)
  14.                             cpl.CopyFrom(pl)
  15.                             cpl.AddToPlotSettingsDictionary(CurrentDatabase)
  16.                             Dim bt As BlockTable = CurrentDatabase.BlockTableId.GetObject(OpenMode.ForRead)
  17.                             Dim btr As BlockTableRecord
  18.                             If pl.ModelType Then
  19.                                 btr = bt(BlockTableRecord.ModelSpace).GetObject(OpenMode.ForRead)
  20.                             Else
  21.                                 btr = bt(BlockTableRecord.PaperSpace).GetObject(OpenMode.ForRead)
  22.                             End If
  23.  
  24.                             Dim lay As Layout = btr.LayoutId.GetObject(OpenMode.ForWrite)
  25.                             lay.CopyFrom(cpl)
  26.                             layName = lay.LayoutName
  27.                         End If
  28.                         sourceTransaction.Commit()
  29.                     End Using
  30.                     currentTransaction.Commit()
  31.                 End Using
  32.  
  33.             End Using
  34.             If Not layName = Nothing Then
  35.                 LayoutManager.Current.CurrentLayout = layName
  36.             End If
  37.  
  38.         End Sub


GumbyCAD

  • Newt
  • Posts: 84
Re: Copy PlotSetting from External File
« Reply #10 on: March 17, 2014, 10:00:01 PM »
I think your spot on.

Ill test it some more and get back to you with results.

Many Many Many Thanks

bargool

  • Guest
Re: Copy PlotSetting from External File
« Reply #11 on: March 20, 2014, 11:41:01 AM »
Here I copy plot settings from another file.
Method CreatePlotSettingsInfos(string templatePath)
Sorry for Russian comments.

This is part of little application, so maybe don't work with copy-paste, but maybe helps.

Jeff H

  • Needs a day job
  • Posts: 6151
Re: Copy PlotSetting from External File
« Reply #12 on: May 13, 2015, 02:16:38 AM »

Here is example that works for me that prompts you to select a file then imports all the page setups into current drawing
Code - Visual Basic: [Select]
  1. Public Class Commands
  2.  
  3.     <CommandMethod("CpyPltSettings")> _
  4.     Public Sub CpyPltSettings()
  5.         Dim opts As New PromptOpenFileOptions(vbLf & "Enter File")
  6.         opts.PreferCommandLine = False
  7.         opts.DialogName = "Select File"
  8.         opts.Filter = "dwg files (*.dwg)|*.dwg"
  9.         opts.DialogName = "Plot Settings"
  10.         opts.DialogCaption = "Plot Settings"
  11.  
  12.         Dim pfr As PromptFileNameResult = Application.DocumentManager.MdiActiveDocument.Editor.GetFileNameForOpen(opts)
  13.         If pfr.Status <> PromptStatus.OK Then
  14.             Return
  15.         End If
  16.  
  17.         AddPlotSettings(pfr.StringResult)
  18.  
  19.     End Sub
  20.  
  21.     Sub AddPlotSettings(ByVal PlotSettingsFile As String)
  22.         Dim CurrentDatabase As Database = HostApplicationServices.WorkingDatabase
  23.         Dim layName As String = Nothing
  24.         Using SourceDatabase As New Database(False, True)
  25.             SourceDatabase.ReadDwgFile(PlotSettingsFile, FileOpenMode.OpenForReadAndAllShare, True, "")
  26.             Using currentTransaction As Transaction = CurrentDatabase.TransactionManager.StartTransaction
  27.  
  28.                 Using sourceTransaction As Transaction = SourceDatabase.TransactionManager.StartTransaction
  29.                     Dim sourcePlotDic As DBDictionary = SourceDatabase.PlotSettingsDictionaryId.GetObject(OpenMode.ForRead)
  30.  
  31.                     For Each item As DBDictionaryEntry In sourcePlotDic
  32.                         Dim pl As PlotSettings = sourcePlotDic.GetAt(item.Key).GetObject(OpenMode.ForRead)
  33.                         Dim cpl As New PlotSettings(pl.ModelType)
  34.                         cpl.CopyFrom(pl)
  35.                         cpl.AddToPlotSettingsDictionary(CurrentDatabase)
  36.                     Next
  37.  
  38.                     sourceTransaction.Commit()
  39.                 End Using
  40.                 currentTransaction.Commit()
  41.             End Using
  42.         End Using
  43.     End Sub
  44. End Class