C# copy plot settings from template (model space)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
...