Message 1 of 1
Problem at publish multiple layouts
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
I need again advice from knowledge programmers 😊. I have a drawing where i defind print settings layout… then i use the function for multiple publish. I was inspired function for multiple publish here:
All parameters which i sett are in layouts objects but function for public use another pc3 file (AutoCAD PDF (General Documentation).pc3) - no matter which pc3 file i sett. When i published manually then all is correct. How can i force publish layouts with my pc3 file through API.
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System.Collections.Generic;
using System.IO;
namespace AutoCAD.Command
{
public class Commands
{
[CommandMethod("NewDrawing", CommandFlags.Session)]
public static void NewDrawing()
{
string strTemplatePath = "acad.dwt";
DocumentCollection acDocMgr = Application.DocumentManager;
Document acDoc = acDocMgr.Add(strTemplatePath);
}
[CommandMethod("PrintMultiSheetsPdf", CommandFlags.Session)]
public void PrintMultiSheetsPdf()
{
try
{
Document doc = Application.DocumentManager.MdiActiveDocument; //mother autocad document
PromptResult promptResult = doc.Editor.GetString("\nEnter the parameter: "); //hardcode user command from anothert side
if (promptResult.Status != PromptStatus.OK)
return;
if (promptResult.StringResult.Contains(";") != true)
{
doc.Editor.WriteMessage($"Input parametr: {promptResult.StringResult} is invalid. Right format - (destination path;source file path).");
return;
}
string[] inputParametrs = promptResult.StringResult.Split(';'); //input parameters: destination path and source file path
if (File.Exists(inputParametrs[1]) != true)
{
doc.Editor.WriteMessage($"Source file: {inputParametrs[1]} does not exist.");
return;
}
if (!System.IO.Directory.Exists(inputParametrs[0]))
System.IO.Directory.CreateDirectory(inputParametrs[0]);
bool isValidate = false;
string PaperSize = "ISO full bleed A3 (420.00 x 297.00 MM)";
string Plottername = "DWG To PDF.pc3";
string PlotStyleName = "PDF files for instructions.ctb";
CustomScale customScale = new CustomScale(1, 1);
string PaperCode = string.Empty;
int i = 0;
bool UsePlotStyles = true;
bool UseLineWeights = true;
bool ScaleLineWeights = false;
bool HidePaperspaceObjects = false;
bool CenterPlot = true;
foreach (string plotDevice in PlotSettingsValidator.Current.GetPlotDeviceList()) // check available printer,
{
if (Plottername.Trim().ToUpper() == plotDevice.Trim().ToUpper())
{
isValidate = true;
break;
}
}
if (isValidate == false)
{
doc.Editor.WriteMessage($"Selected printer: {Plottername} is not installed on this computer. Contact your system administrator");
return;
}
using (PlotSettings acPlSet = new PlotSettings(true)) //try to find right paper value because it is no possible to set GetLocaleMediaName(this value is only show in UI)
{
PlotSettingsValidator acPlSetVdr = PlotSettingsValidator.Current;
acPlSetVdr.SetPlotConfigurationName(acPlSet, Plottername, null);
foreach (string mediaName in acPlSetVdr.GetCanonicalMediaNameList(acPlSet))
{
if (PaperSize.Trim().ToUpper() == acPlSetVdr.GetLocaleMediaName(acPlSet, i).Trim().ToUpper())
{
PaperCode = mediaName;
break;
}
i++;
}
}
if (PaperCode == string.Empty)
{
doc.Editor.WriteMessage($"Selected papersize: {PaperSize} is not available on selected printer. Contact your system administrator");
return;
}
List<Layout> layouts = new List<Layout>();
using (Database db = new Database(false, true))
{
if (Functions.IsFileAvailable(new FileInfo(inputParametrs[1])) != true)
{
doc.Editor.WriteMessage($" File: {inputParametrs[1]} is already locked by some other process.");
return;
}
db.ReadDwgFile(inputParametrs[1], System.IO.FileShare.ReadWrite, true, ""); // open select DWG file
using (Transaction tr = db.TransactionManager.StartTransaction())
{
DBDictionary layoutDict = (DBDictionary)db.LayoutDictionaryId.GetObject(OpenMode.ForRead);
foreach (DBDictionaryEntry entry in layoutDict)
{
if (entry.Key.Trim().ToUpper() != "MODEL") // set all layouts except model
{
Layout acLayout = (Layout)tr.GetObject(entry.Value, OpenMode.ForRead);
using (PlotSettings currentPlSet = new PlotSettings(acLayout.ModelType))
{
currentPlSet.CopyFrom(acLayout);
PlotSettingsValidator acPlSetVdr = PlotSettingsValidator.Current;
acPlSetVdr.SetPlotConfigurationName(currentPlSet, Plottername, PaperCode);
acPlSetVdr.SetPlotPaperUnits(currentPlSet, PlotPaperUnit.Millimeters);
acPlSetVdr.SetPlotWindowArea(currentPlSet, new Extents2d(10, 8.5, 430, 288.5));
acPlSetVdr.SetPlotType(currentPlSet, Autodesk.AutoCAD.DatabaseServices.PlotType.Window);
acPlSetVdr.SetPlotCentered(currentPlSet, CenterPlot);
acPlSetVdr.SetPlotRotation(currentPlSet, PlotRotation.Degrees000);
acPlSetVdr.SetPlotOrigin(currentPlSet, new Point2d(0, 0));
currentPlSet.ShowPlotStyles = UsePlotStyles;
try { acPlSetVdr.SetCurrentStyleSheet(currentPlSet, PlotStyleName); }
catch
{
doc.Editor.WriteMessage($"AutoCAD does not contains file - {PlotStyleName} .");
return;
}
currentPlSet.PlotHidden = HidePaperspaceObjects;
currentPlSet.PrintLineweights = UseLineWeights;
currentPlSet.ScaleLineweights = ScaleLineWeights;
acPlSetVdr.SetCustomPrintScale(currentPlSet, customScale);
acPlSetVdr.SetZoomToPaperOnUpdate(currentPlSet, true); // Zoom to show the whole paper
acLayout.UpgradeOpen(); // Update the layout
acLayout.CopyFrom(currentPlSet);
}
layouts.Add(acLayout);
}
}
tr.Commit();
}
try //try to delte old file
{
if (System.IO.File.Exists(Path.ChangeExtension(inputParametrs[1], "pdf")))
System.IO.File.Delete(Path.ChangeExtension(inputParametrs[1], "pdf"));
}
catch
{
doc.Editor.WriteMessage($"There is no possible delete old file - {Path.ChangeExtension(inputParametrs[1], "pdf")} .");
return;
}
doc.Editor.Regen();
db.SaveAs(inputParametrs[1], DwgVersion.Current); //save modify print settings back to layout
}
layouts.Sort((l1, l2) => l1.TabOrder.CompareTo(l2.TabOrder)); // Sort selected layouts to right order
MultiSheetsPdf plotter = new MultiSheetsPdf(inputParametrs[0], layouts, inputParametrs[1]); //prepare plot
plotter.Publish(); //plot selected laouys
}
catch { return; }
}
}
}
...