cannot read custom paper size with c# in AutoCAD 2014 above

cannot read custom paper size with c# in AutoCAD 2014 above

kuang_hf
Enthusiast Enthusiast
2,820 Views
11 Replies
Message 1 of 12

cannot read custom paper size with c# in AutoCAD 2014 above

kuang_hf
Enthusiast
Enthusiast

Hi all,

Does anyone know why I cannot read custom paper size with c# in AutoCAD 2014 and above version? I read it successfully through  PlosettingValidator.GetCanonicalMediaNameList() under Acad 2012 and 2013. But failed in 2014 and 2016. The pc3 is 'DWG To PDF.pc3'.

I also call PlosettingValidator.RefreshLists() before GetCanonicalMediaNameList. Is there anything special in this API of ACAD 2014 and above?

 

The code snippet:

 

ArrayList media_list = new ArrayList();
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
  BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead);
  Layout layout = (Layout)tr.GetObject(btr.LayoutId, OpenMode.ForRead);
  PlotSettings ps = new PlotSettings(layout.ModelType);
  ps.CopyFrom(layout);
  PlotSettingsValidator psv = PlotSettingsValidator.Current;
  psv.SetPlotConfigurationName(ps, devname, null);
  psv.RefreshLists(ps);

  StringCollection medlist = psv.GetCanonicalMediaNameList(ps);
  foreach (string medname in medlist)
  {
    psv.SetPlotConfigurationName(ps, devname, medname);
    PaperInfo pi = new PaperInfo();
    pi.name = medname;
    pi.local_name = psv.GetLocaleMediaName(ps, medname);
    pi.width = ps.PlotPaperSize.X;
    pi.height = ps.PlotPaperSize.Y;
    pi.margin = ps.PlotPaperMargins;
    media_list.Add(pi);
  }
  ps.Dispose();
  psv.Dispose();

  tr.Abort();
}

 

0 Likes
Accepted solutions (2)
2,821 Views
11 Replies
Replies (11)
Message 2 of 12

SENL1362
Advisor
Advisor
Not that i know of, but you you need to add the custom paper sizes you're self.
They are stored in the associated PMP file.
0 Likes
Message 3 of 12

SENL1362
Advisor
Advisor

...
psv.RefreshLists(plotSettings);
var canonicalMediaNames = psv.GetCanonicalMediaNameList(plotSettings);
foreach (string canonicalMediaName in canonicalMediaNames)
{
  var localMediaName = psv.GetLocaleMediaName(plotSettings, canonicalMediaName);
  psv.SetCanonicalMediaName(plotSettings, canonicalMediaName);
  ed.WriteMessage($"\nPlotPaperSize={plotSettings.PlotPaperSize.X} X {plotSettings.PlotPaperSize.Y}");
...

0 Likes
Message 4 of 12

kuang_hf
Enthusiast
Enthusiast

Surely I have added custom paper size in ACAD first.  But it seems in higher version of CAD I cannot get them through GetCanonicalMediaNameList.

0 Likes
Message 5 of 12

SENL1362
Advisor
Advisor

Is it allowed to send the PC3+PMP?

 

0 Likes
Message 6 of 12

kuang_hf
Enthusiast
Enthusiast

Hi  SENL1362,

Files attached. It's in acad 2016, and has a custom paper 880*1280 mm.

0 Likes
Message 7 of 12

SENL1362
Advisor
Advisor

You shouldn't have give away the custom paper size 🙂

 

Anyway i don't see the problem, i can read the papersize directly from the PlotSettings.

 

image.png

 

 

 

[CommandMethod("TestPlotMedia")]
public static void TestPlotMedia()
{
    Document doc = null;
    Editor ed = null;
    Database db = null;
    LayoutManager lm = null;

    var pc3Name = "DWG To PDF.pc3";

    try
    {
        doc = AcadApp.DocumentManager.MdiActiveDocument;
        ed = doc.Editor;
        db = doc.Database;
        lm = LayoutManager.Current;
        var psv = PlotSettingsValidator.Current;

        ed.WriteMessage($"\nPc3: {pc3Name}");
        using (var tr = db.TransactionManager.StartTransaction())
        {
            var layoutName = lm.CurrentLayout;
            var layoutId = lm.GetLayoutId(layoutName);
            var layout = (Layout)tr.GetObject(layoutId, OpenMode.ForRead);
            var ps = new PlotSettings(layout.ModelType);
            ps.CopyFrom(layout);
            psv.SetPlotConfigurationName(ps, pc3Name, null);
            psv.RefreshLists(ps);
            var canMedNames=psv.GetCanonicalMediaNameList(ps);
            foreach(var canMedName in canMedNames)
            {
                psv.SetCanonicalMediaName(ps, canMedName);

                var medName = psv.GetLocaleMediaName(ps, canMedName);
                var papUnts = ps.PlotPaperUnits;
                var papSize=ps.PlotPaperSize;
                var papMargins= ps.PlotPaperMargins;
                ed.WriteMessage($"\n\t Paper: {medName} ({canMedName})");
                ed.WriteMessage($"\n\t\t Units  : {papUnts.ToString()}");
                ed.WriteMessage($"\n\t\t Size   : {papSize.X} X {papSize.Y}");
                ed.WriteMessage($"\n\t\t Margins: {papMargins.MinPoint.ToString()} X {papMargins.MaxPoint.ToString()}");
            }
            tr.Commit();
        }
    }
    catch (System.Exception ex)
    {
        ed?.WriteMessage($"\n{ex.Message}");
    }
}

 

0 Likes
Message 8 of 12

SENL1362
Advisor
Advisor

 

And some additional lines of code to return a list of all the media details:

 

 

...
var plotMedias = new Dictionary<string, Dictionary<string, Object>>(StringComparer.OrdinalIgnoreCase);
ed.WriteMessage($"\nPc3: {pc3Name}");
...
foreach(var canMedName in canMedNames)
{
   var mediaDetails = new Dictionary<string, Object>(StringComparer.OrdinalIgnoreCase);
  psv.SetCanonicalMediaName(ps, canMedName);
...
  //ed.WriteMessage(...
  mediaDetails["LocalMediaName"] = medName;
  mediaDetails["CanonicalMediaName"] = canMedName;
  mediaDetails["PaperSize"] = ps.PlotPaperSize;
  mediaDetails["PaperMargins"] = ps.PlotPaperMargins;
  mediaDetails["PaperUnits"] = ps.PlotPaperUnits;
  plotMedias[medName] = mediaDetails;

 

0 Likes
Message 9 of 12

kuang_hf
Enthusiast
Enthusiast

Thanks for you code. I tried it, but in my acad 2016, it just read out standard paper size, no custom paper.  I'm sure there is only one 'DWG to PDF.pc3'. So strange:(

0 Likes
Message 10 of 12

SENL1362
Advisor
Advisor

that can be easily tested:

Snippet

var acadPref = (dynamic)AcadApp.Preferences;
var plotConfigPathname = acadPref.Files.PrinterConfigPath;


 

0 Likes
Message 11 of 12

kuang_hf
Enthusiast
Enthusiast
Accepted solution

SENL1362:

   I finally got it. It's related to acad's plot file support path. Although in acad's option dialog, the support path like :

C:\Users\xxxxx\AppData\Roaming\Autodesk\AutoCAD 2016\R20.1\chs\Plotters, and the modified pc3 file is located at that folder, acad has another default PC3 folder, like : C:\Users\xxxxx\AppData\Roaming\Autodesk\AutoCAD 2016\R20.1\chs\Plotters\AutoCAD 2016 -  PC3 File.

If that folder exists, GetCanonicalMediaNameList() call  reads pc3 from it. If it does not exist, the function reads the actual modified pc3 file. What a misleading mechanism...

   Any way, thank you for your help!

0 Likes
Message 12 of 12

SENL1362
Advisor
Advisor
Accepted solution

You could add this to find out where the PC3 comes from:

Snippet

PlotConfigManager.RefreshList(RefreshCode.RefreshDevicesList);
foreach(PlotConfigInfo pd in PlotConfigManager.Devices)
{
    ed.WriteMessage($"\n{pd.DeviceType}{pd.DeviceName}, @:{pd.FullPath}");
}