C# copy plot settings from template (model space)

C# copy plot settings from template (model space)

snappyjazz
Collaborator Collaborator
283 Views
1 Reply
Message 1 of 2

C# copy plot settings from template (model space)

snappyjazz
Collaborator
Collaborator

Hello,

I have a template file that has a lot of layouts. These layouts contain the plot page setup I need to make printing buttons. In my code, I open the template and find the layout I need and use the “plset.copyfrom” on the main transaction to copy over the templates layout.

 

I added quite a few page setups to the model space recently. I would like to get the model space plot settings so I can use them. Like in the layout process I open the template and can look through the plot settings to find the right one. I’m running into trouble here through, the “pltset.copyfrom” doesn’t seem to accept the object I pass to it. I have the code below to help explain. There are also a few lines of code of other failed ideas to try and get this working.

 

This seems like it would be much simpler since you don’t have to put the dictionary entry into a layout object. Is there a way to extract the plot settings from template and copy over?

 

private static void Model_Dd_FitToA()
{
    Initiate();
    // Get and set the BackgroundPlot system variable value
    Object backPlot = CadApp.Application.GetSystemVariable("BACKGROUNDPLOT"); CadApp.Application.SetSystemVariable("BACKGROUNDPLOT", 0);
    // Get and set the plot config system variable value
    Object SysCfg = CadApp.Application.GetSystemVariable("SYSPLOTCFG"); CadApp.Application.SetSystemVariable("SYSPLOTCFG", 1);

            CadDBS.Transaction Trans = g_AcDatBas.TransactionManager.StartTransaction();  main transaction
            CadDBS.BlockTableRecord BlTblRec = (CadDBS.BlockTableRecord)Trans.GetObject(g_AcDatBas.CurrentSpaceId, CadDBS.OpenMode.ForRead);  block table record for main trans
            CadPrint.PlotInfo PlotInfo = new CadPrint.PlotInfo();  plot info for main trans
            CadDBS.PlotSettings PlSet = new CadDBS.PlotSettings(g_ActiveLayout.Layout.ModelType);  plot settings for main trans
            CadDBS.PlotSettings _TmptPlSet = new CadDBS.PlotSettings(true);  plot settings for secondary or template transaction
            CadDBS.PlotSettingsValidator PlSetVal = CadDBS.PlotSettingsValidator.Current;  validator for main trans

            //try
            //{
            PlotInfo.Layout = BlTblRec.LayoutId;
            //PlotInfo.Layout =  g_ActiveLayout.Layout.Id

            // API makes sure that the new "plotsettings" is initialized correctly.
            //PlSet.CopyFrom( g_ActiveLayout.Layout)
            CadDBS.DBDictionary PlSets = (CadDBS.DBDictionary)Trans.GetObject(g_AcDatBas.PlotSettingsDictionaryId, CadDBS.OpenMode.ForRead);

            // Check to see if the page setup exists
            if (g_ActiveLayout.Layout.PlotConfigurationName.Contains("SysPrint_DD") && g_ActiveLayout.Layout.PlotConfigurationName.Contains("ANSI_A"))
            {
                // Use CopyFrom property of the plotsetting to initialize it on the layout
                PlSet.CopyFrom(g_ActiveLayout.Layout);
            }
            else
            {
                #region Copy the print page setup from the template
                // Open in memory the template dwt
                // Create an empty database object
                CadDBS.Database NewDb = new CadDBS.Database(false, true);

                // Open the template for read as the database object
                NewDb.ReadDwgFile($"{LocalFolderPath}\\CustomApps\\SysPrint_Template.dwg", CadDBS.FileOpenMode.OpenForReadAndAllShare, false, null);

                // Define a new transaction to operate in the template
                CadDBS.Transaction _TmptTrans = NewDb.TransactionManager.StartTransaction();

                // Create a layout object
                CadDBS.Layout _TmptLayout = new CadDBS.Layout();  tried using a layout object to copy over the plot settings but it failed
               // Get the plot settings dictionary
                CadDBS.DBDictionary _NewPlSets = (CadDBS.DBDictionary)_TmptTrans.GetObject(NewDb.PlotSettingsDictionaryId, CadDBS.OpenMode.ForRead);
                foreach (CadDBS.DBDictionaryEntry _Entry in (CadDBS.DBDictionary)_NewPlSets)
                {
                    if (PrintDetails) { Msg.Print(CodeLocation, PrintType.Output, $"\nDict Entry: {_Entry.Key}"); }
                    if (_Entry.Key.Contains("DD_Printer-A")) 
                    {
                        _TmptPlSet = (CadDBS.PlotSettings)_TmptTrans.GetObject(_Entry.Value, CadDBS.OpenMode.ForRead);
                        //_TmptLayout = (CadDBS.Layout)_TmptTrans.GetObject(_Entry.Value, CadDBS.OpenMode.ForRead);
                        _Entry.Value.id
                        break;
                    }
                }

                // Close the file now that its in memory
                NewDb.CloseInput(true);

                _TmptTrans.Commit();

                // Use CopyFrom property of the plotsetting to initialize it on the layout
               // PlSet.CopyFrom(_TmptLayout);  fails Autodesk.Autocad.Runtime.Exception: eWrongObjectType
PlSet.CopyFrom(_TmptPlSet);  fails “Internal error !dbobji.cpp@8703: eNotOpenForWrite”

                // Refreshing the plot settings with plot settings validator
                // PlSetVal.RefreshLists(_TmptPlSet);  fails
...

 

0 Likes
284 Views
1 Reply
Reply (1)
Message 2 of 2

ActivistInvestor
Mentor
Mentor

I didn't study your code in any detail, but what sticks out is that you call Commit() before calling CopyFrom().

 

Transaction.Commit() closes any objects that were opened in the transaction.

 

Try refactoring so that Transaction.Commit() is called after CopyFrom() is called.

0 Likes