Author Topic: Copying plot settings  (Read 22858 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Copying plot settings
« Reply #15 on: January 05, 2007, 09:44:36 AM »
Would someone like to give this a run around the yard please.
It should import ALL the page setups from the template drawing ..

Added Note : This is concept code. You should check each PageSetup to see if it exists in the current drawing  and act accordingly .. otherwise you may end up with multiple copies [ yes, really ;-) ]

Another Note : I don't think this code will be suitable for public consumption .. the WblockCloneObjects does not work the way I expected.
.. the DuplicateRecordCloning.Ignore, does not Ignore the existing record
and
.. the DuplicateRecordCloning.Replace, spits the dummy in a rather rude manner.

I'll leave the code in case anyone gets any ideas.
... back to the drawing board  :-(

Code: [Select]
// Codehimbelonga kwb@theSwamp  Kerry Brown 20070105
// Credits to Glenn R for original concept.
// For AC2007: VS2005

        [CommandMethod("PSI")]
        static public void PageSetupsImport()
        { 
            Document doc = AcadApp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
           
            ObjectIdCollection psIds = new ObjectIdCollection();

            using (Database psDb = new Database(false, true))
            {
                psDb.ReadDwgFile(@"c:\Scripter.dwg", System.IO.FileShare.ReadWrite, true, string.Empty);
                using (Transaction psTr = psDb.TransactionManager.StartTransaction())
                {
                    DBDictionary psDicts =  (DBDictionary)psTr.GetObject(
                        psDb.PlotSettingsDictionaryId, OpenMode.ForRead);

                    foreach (System.Collections.DictionaryEntry psDict in psDicts)
                    {
                        psIds.Add((ObjectId)psDict.Value);
                    }
                }
                IdMapping idMap = new IdMapping();
                db.WblockCloneObjects(
                    psIds,
                    db.PlotSettingsDictionaryId,
                    idMap,
                    DuplicateRecordCloning.Ignore,
                    false);
            }
        }

« Last Edit: January 05, 2007, 10:23:56 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8659
  • AKA Daniel
Re: Copying plot settings
« Reply #16 on: January 05, 2007, 12:17:18 PM »
Wow! Kerry
Very creative

Dan

mohnston

  • Bull Frog
  • Posts: 305
  • CAD Programmer
Re: Copying plot settings
« Reply #17 on: January 05, 2007, 01:01:49 PM »
Thank you all for your interest, work, comments, guesses and input.
I continue to try to get to the bottom of this.

If you want an idea of the end goal take a look at AutoCAD's Publish feature.
Note that you can select many drawings, import a Page Setup, and use that setup on all the drawings in your list.
I'm building a program that is like Publish but built to fill our company's needs.

As I understand it the players are:
The subjectDocument - a Document to print
The controlDB - a Database that contains many plot settings (called Page Setups in AutoCAD). My preset, approved, pristine plot settings
The pSet - a PlotSettings (a single PlotSettings object out of the PlotSettings collection) that I want
The pInfo - a PlotInfo which contains all the information that AutoCAD should need to plot a given page

The idea (suggested by some here) was to not have to set all the many settings needed to plot a drawings. Instead import a Page Setup (plotsettings) from a source database.

I think I need a transaction to get the pSet because I am pulling an object out of a database.
I don't think I need a transaction to print subjectDocument because I'm not changing anything about the drawing. I'm not adding or reading anything from that drawing file.
When a drawing is printed using the Publish feature the Page Setup used to print the drawing does not get added to the drawing file.
I think the PlotInfo is a separate set of parameters that can be applied to any drawing.

I shall persevere.

It's amazing what you can do when you don't know what you can't do.
CAD Programming Solutions

mohnston

  • Bull Frog
  • Posts: 305
  • CAD Programmer
Re: Copying plot settings
« Reply #18 on: January 05, 2007, 08:45:16 PM »
Ever notice how sometimes explaining a problem helps you to answer it?

. . .
If you want an idea of the end goal take a look at AutoCAD's Publish feature.
. . .
I'm building a program that is like Publish but built to fill our company's needs.

Autodesk.AutoCAD.ApplicationServices.Application.Publisher
It's amazing what you can do when you don't know what you can't do.
CAD Programming Solutions

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Copying plot settings
« Reply #19 on: January 05, 2007, 09:19:29 PM »
Mark, I just came back to post that namespace link for you :-)

... yep, sometimes the question is the answer.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Glenn R

  • Guest
Re: Copying plot settings
« Reply #20 on: January 30, 2007, 07:40:19 PM »
Daniel,

The reason you don't get an exception when the dwg is CLOSED is because of this line:
Code: [Select]
controlDB.ReadDwgFile(plotControlDWG, System.IO.FileShare.Read, false, string.Empty);

The FileShare.Read is telling it that you want to LOCK the file, read it and only allow read access to others...hence if you have it open, it's already locked and your code can't get an exclusive lock.

Kerry and others,

Are you still having trouble findinng an optimum solution for this? If yes, create a template file with some named pagesetups in it, then post back and we can have a little lesson/experiment.

I won't do this if there's little interest.

Cheers,
Glenn.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Copying plot settings
« Reply #21 on: January 30, 2007, 07:58:26 PM »
Hi Glenn .. great to have you back from your holidays ... hope you had a fun time !!

I'd be interested in playing along ... just can't apply ongoing undivided attention at the moment
 be assured that your postings will be followed with interest, even if most viewers don't respond  :|

I had thought that CopyFrom would do the job, but haven't made the time to follow the idea through ..

/// kwb

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Glenn R

  • Guest
Re: Copying plot settings
« Reply #22 on: January 30, 2007, 08:11:56 PM »
Oooook then.

First a few experiments.

1. Put this code into the ThisDrawing module in a new VBA project and change the variable 'sTemplate' to point to your template.
2. Make sure to add a reference in VBA to AutoCAD/ObjectDBX Common 16.0 Type Library as well.
Code: [Select]
Public Sub ImportPageSetupsToCurrentDwg()
    Dim pAxDbDoc As AxDbDocument
    Dim pAxDbPageSetups As AcadPlotConfigurations
    Dim pAxDbPageSetup As AcadPlotConfiguration
    Dim pltCfgsArray() As AcadPlotConfiguration
    Dim i As Integer
    Dim sTemplate As String
   
    On Error Resume Next
    If Err Then Err.Clear
   
    Set pAxDbDoc = New AxDbDocument
    sTemplate = "C:\Temp\Plot.dwt"
    pAxDbDoc.Open sTemplate
   
    If Err Then
        Err.Clear
        MsgBox Err.Description
        MsgBox "Error opening " & sTemplate & "!", vbExclamation, "Batch Error"
        Set pAxDbDoc = Nothing
        Exit Sub
    End If
   
    ' get a pointer to the pagesetups in the in-memory dbase...
    Set pAxDbPageSetups = pAxDbDoc.PlotConfigurations
    ' loop the pagesetup's and add them...
    i = -1
    For Each pAxDbPageSetup In pAxDbPageSetups
        i = i + 1
        ReDim Preserve pltCfgsArray(i) As AcadPlotConfiguration
        Set pltCfgsArray(i) = pAxDbPageSetup
    Next
    ' clone the plot configs (pagesetups) into the current dwg...
    pAxDbDoc.CopyObjects pltCfgsArray, ThisDrawing.PlotConfigurations
    ' check if the clone op worked...
    If Err Then
        Err.Clear
    End If
   
    ' Clean up...
    For i = LBound(pltCfgsArray) To UBound(pltCfgsArray)
        Set pltCfgsArray(i) = Nothing
    Next
    Set pAxDbPageSetup = Nothing
    Set pAxDbPageSetups = Nothing
    Set pAxDbDoc = Nothing
   
    If Err Then Err.Clear
End Sub

Now, run the code, then right click on a layout tab and go to the Page Setup Manager, and you should see all the pagesetups from your template have been imported.

Now, just for kicks, run the code again and revisit the Page Setup Manager...what do you see? Report your findings here.

Cheers,
Glenn.
« Last Edit: January 30, 2007, 08:13:24 PM by Glenn R »

Glenn R

  • Guest
Re: Copying plot settings
« Reply #23 on: January 30, 2007, 08:14:04 PM »
BTW, I'm using 2006 and Visual Studio 2005.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Copying plot settings
« Reply #24 on: January 30, 2007, 08:34:23 PM »
Now, run the code, then right click on a layout tab and go to the Page Setup Manager, and you should see all the pagesetups from your template have been imported.

Now, just for kicks, run the code again and revisit the Page Setup Manager...what do you see? Report your findings here.

First Pass .. the Page setups were imported and Listed.
Second Pass .. each Page setups is listed twice
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Glenn R

  • Guest
Re: Copying plot settings
« Reply #25 on: January 30, 2007, 09:07:23 PM »
Excellent Kerry.

Can you guesstimate as to why they're duplicated? Also, have a look in the ACAD_PLOTSETTINGS dictionary in the NOD (Named Objects Dictionary) with DBVIEW...you will some interesting results.

Glenn R

  • Guest
Re: Copying plot settings
« Reply #26 on: January 30, 2007, 09:19:58 PM »
Also, when you've fired up DBVIEW and looked where I suggested, post your findings and musings here.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Copying plot settings
« Reply #27 on: January 30, 2007, 09:38:41 PM »
Heh, they're all anonymous !!
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Glenn R

  • Guest
Re: Copying plot settings
« Reply #28 on: January 30, 2007, 09:48:45 PM »
Excellent...why?

Glenn R

  • Guest
Re: Copying plot settings
« Reply #29 on: January 30, 2007, 09:50:19 PM »
Also, click on one of the anon names (*A?), then in the right hand pane view, look at the 001 code...what do you notice?
Post your findings here.