Author Topic: Copying plot settings  (Read 22857 times)

0 Members and 1 Guest are viewing this topic.

mohnston

  • Bull Frog
  • Posts: 305
  • CAD Programmer
Copying plot settings
« on: January 04, 2007, 04:29:35 PM »
I'm trying to copy plot settings from a drawing to use in a plotting program I am writing.

I get an AccessViolationException error "Attempted to read or write protected memory." when I run this code:
Code: [Select]
---------------------------------------
Database controlDB = new Database();
PlotSettings pSet = new PlotSettings(true);
PlotInfo pInf = new PlotInfo();
PlotSettingsValidator pValid = PlotSettingsValidator.Current;

controlDB.ReadDwgFile(plotControlDWG, System.IO.FileShare.Read, false, string.Empty);

Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = controlDB.TransactionManager;
//controlDB.CloseInput(true);
Transaction trans = tm.StartTransaction();
try
{
DBDictionary dic = (DBDictionary)trans.GetObject(controlDB.PlotSettingsDictionaryId, OpenMode.ForRead);
foreach (DBDictionaryEntry dicEnt in dic)
{
if (dicEnt.Key == pgSetupName)
{
pSet = (PlotSettings)trans.GetObject(dicEnt.Value, OpenMode.ForRead, false);
}
}
trans.Commit();
}
finally { trans.Dispose(); }


pInf.Layout = LayoutManager.Current.GetLayoutId("Model");
pInf.OverrideSettings = pSet;
-------------------------

The error happens on this last line.
pSet is a valid PlotSettings. I see all the properties are set when I examine it.
pInf says that "Operation is not valid due to the current state of the object.". It reports this in the .DeviceOverride property which I am not setting.

Any idea why this is generating the error?
Has anyone done this successfully?
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 #1 on: January 04, 2007, 06:46:01 PM »
Any chance of getting something closer to a 'workable'  class or method.
... would make any attempt to help a little easier .. :-)

eg:
where do these come from ?
plotControlDWG
pgSetupName

What are your special dependencies ?

Can we assume AC2007 ?
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.

mohnston

  • Bull Frog
  • Posts: 305
  • CAD Programmer
Re: Copying plot settings
« Reply #2 on: January 04, 2007, 07:26:46 PM »
AutoCAD 2007
Windows XP Pro
VS 2005

plotControlDWG is a string containing the full path to an existing drawing.
pgSetupName is a string containing the name of an existing Page Setup in the above drawing.

Dependencies are:
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.PlottingServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;

Thank you for your reply.
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 #3 on: January 04, 2007, 10:08:30 PM »
Mark,

Don't have much spare time, but ..

this
Code: [Select]
        [CommandMethod("AsdkCmd1")]
        static public void test()
        {
            Database controlDB = new Database();
            PlotSettings pSet = new PlotSettings(true);
            PlotInfo pInf = new PlotInfo();
            PlotSettingsValidator pValid = PlotSettingsValidator.Current;
   
            string plotControlDWG = @"c:\Scripter.dwg";
            string pgSetupName = "CT1-A3MOD";

            controlDB.ReadDwgFile(plotControlDWG, System.IO.FileShare.Read, false, string.Empty);

            Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = controlDB.TransactionManager;
            //controlDB.CloseInput(true);
            using (Transaction trans = tm.StartTransaction())
            {
                DBDictionary dic = (DBDictionary)trans.GetObject(controlDB.PlotSettingsDictionaryId, OpenMode.ForRead);
                foreach (DBDictionaryEntry dicEnt in dic)
                {
                    if (dicEnt.Key == pgSetupName)
                    {
                         pSet = (PlotSettings)trans.GetObject(dicEnt.Value, OpenMode.ForRead, false);
                    }
                }
                trans.Commit();
            }
            pInf.Layout = LayoutManager.Current.GetLayoutId("Model");
            pInf.OverrideSettings = pSet;

        }
produces a different exception at this line ;
Code: [Select]
pSet = (PlotSettings)trans.GetObject(dicEnt.Value, OpenMode.ForRead, false);
Code: [Select]
************** Exception Text **************
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Autodesk.AutoCAD.Runtime.Exception: eDwgObjectImproperlyRead
   at Autodesk.AutoCAD.DatabaseServices.TransactionManager.GetObjectInternal(AcDbTransactionManager* pTM, ObjectId id, OpenMode mode, Boolean openErased, Boolean forceOpenOnLockedLayer)
   at Autodesk.AutoCAD.DatabaseServices.Transaction.GetObject(ObjectId id, OpenMode mode, Boolean openErased)
   at ClassLibrary.kdubTestClass.test() in K:\Visual Studio 2005 Projects\_CAD\CsMgdAcad-TestPlotConfig_01\CsMgdAcad-TestPlotConfig_01\Class.cs:line 105
   --- End of inner exception stack trace ---

perhaps something can be done with  pSet.CopyFrom( ... )  .. but thats a W.A.G.

I'll play some more if I get a chance.
/// kwb
« Last Edit: January 04, 2007, 10:14:34 PM 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: 8658
  • AKA Daniel
Re: Copying plot settings
« Reply #4 on: January 05, 2007, 12:31:08 AM »
I don’t get any exceptions with the last code(but it does not work), I don’t know enough about Plot Settings to be of much help. I do have a couple of questions though.

Why is trans.Commit(); being called when nothing is being written to the database through the transaction manager (what are you committing to?) .

Which brings up my next question.

Of you need to open the source drawing’s database to get the plot setting
Wouldn’t you need to open the target drawing’s database (through the transaction manager) to write the new plot setting your copying?

Dan
« Last Edit: January 05, 2007, 12:32:21 AM by Danielm103 »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Copying plot settings
« Reply #5 on: January 05, 2007, 12:53:44 AM »
Quote
I don’t get any exceptions with the last code

Daniel,
When you changed these, does the template have the named setup you nominate ?
 
Code: [Select]
string plotControlDWG = @"c:\Scripter.dwg";
 string pgSetupName = "CT1-A3MOD";

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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Copying plot settings
« Reply #6 on: January 05, 2007, 01:07:05 AM »
I believe you are correct about the redundancy of
trans.Commit();
 ... though I don't think it was catastrophic to leave it there :-)
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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Copying plot settings
« Reply #7 on: January 05, 2007, 01:11:12 AM »
Quote
Wouldn’t you need to open the target drawing’s database (through the transaction manager) to write the new plot setting your copying?
At this stage I hadn't considered any further than collecting the information and getting past the exception that is being generated.

Quote
... I don’t know enough about Plot Settings ...
ditto, ditto, ditto ... black magic, as Glenn would say :lol:
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: 8658
  • AKA Daniel
Re: Copying plot settings
« Reply #8 on: January 05, 2007, 01:26:04 AM »
I did not get any execption if the source drawing "c:\Scripter.dwg" is closed.

Dan

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Copying plot settings
« Reply #9 on: January 05, 2007, 01:44:07 AM »
I did not get any execption if the source drawing "c:\Scripter.dwg" is closed.

Dan


I just looked ... I had no .dwl in the folder.  Looks like this is going to be one of those weird ones ... :lol:
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: 8658
  • AKA Daniel
Re: Copying plot settings
« Reply #10 on: January 05, 2007, 03:54:41 AM »
Hehe it looks that way. maybe my memory is not protected :)

 I added this line
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(pSet.PlotSettingsName);
And got

 
Cross posting is kind of like cross dressing, I really don’t care if you do it , but people might start to look at you funny
« Last Edit: January 05, 2007, 04:00:25 AM by Danielm103 »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Copying plot settings
« Reply #11 on: January 05, 2007, 04:02:36 AM »
expletive, big expletive, expletive

I think I WILL write that book ...
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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Copying plot settings
« Reply #12 on: January 05, 2007, 04:18:39 AM »
Daniel. can you post your solution please
[ I'm clutching at straws here ]
.. you used my code, yes ?
This is SO bloody frustrating ... !!!
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 #13 on: January 05, 2007, 08:19:11 AM »
I have not actually copied straight to the layout object - I've cloned into the NOD then applied the particular plotsettings to the layout in question to plot.

Unfortunately I'm out of the country at the moment and won't be back until the 19th. If you're still having troubles, give me a shout then and I will post up something.

Until then, this is the order I would try:

open up source dwg in memory
start transaction on it
get the object in question
start transaction on destination db/object
open up destination object
copy/clone it across
end transaction on destination db
end transaction on source db

Cheers,
Glenn.

PS and Kerry is right, it's a bit of black magic, smoke and mirrors where plotting is concerned.

Bobby C. Jones

  • Swamp Rat
  • Posts: 516
  • Cry havoc and let loose the dogs of war.
Re: Copying plot settings
« Reply #14 on: January 05, 2007, 08:59:31 AM »
Not related to your question, but don't forget to .Dispose() of any databases that you create.

Code: [Select]
Database controlDb = new Database();
...Insert Black Magic Here
controlDb.Dispose();

You'll want to wrap that in a using(Database controlDb = new Database()) or a Try..Catch..Finally statement.
Bobby C. Jones