Author Topic: .NET Layout,ViewPort,Plot Routines  (Read 9277 times)

0 Members and 1 Guest are viewing this topic.

flyte

  • Mosquito
  • Posts: 6
Re: .NET Layout,ViewPort,Plot Routines
« Reply #15 on: September 19, 2017, 11:51:31 pm »
Publishing to 3d DWF

<Edit: adding a try/catch block in agreement with the observation of tony>
C#
Code - C#: [Select]
  1. using System.IO;
  2. using System.Text;
  3. using Autodesk.AutoCAD.ApplicationServices;
  4. using Autodesk.AutoCAD.PlottingServices;
  5.  
  6. namespace Plottings
  7. {
  8.    public class PlotTo3dDwf
  9.    {
  10.        private string dwgFile, dwfFile, dsdFile, title, outputDir;
  11.  
  12.        public PlotTo3dDwf()
  13.        {
  14.            outputDir = (string)Application.GetSystemVariable("DWGPREFIX");
  15.            string name = (string)Application.GetSystemVariable("DWGNAME");
  16.            dwgFile = Path.Combine(outputDir, name);
  17.            title = Path.GetFileNameWithoutExtension(name);
  18.            dwfFile = Path.ChangeExtension(dwgFile, "dwf");
  19.            dsdFile = Path.ChangeExtension(dwfFile, ".dsd");
  20.        }
  21.  
  22.        public PlotTo3dDwf(string outputDir)
  23.            : this()
  24.        {
  25.            this.outputDir = outputDir;
  26.        }
  27.  
  28.        public void Publish()
  29.        {
  30.            short bgPlot = (short)Application.GetSystemVariable("BACKGROUNDPLOT");
  31.            Application.SetSystemVariable("BACKGROUNDPLOT", 0);
  32.            try
  33.            {
  34.                if (!Directory.Exists(outputDir))
  35.                    Directory.CreateDirectory(outputDir);
  36.  
  37.                using (DsdData dsd = new DsdData())
  38.                using (DsdEntryCollection dsdEntries = new DsdEntryCollection())
  39.                {
  40.                    // add the Model layout to the entry collection
  41.                    DsdEntry dsdEntry = new DsdEntry();
  42.                    dsdEntry.DwgName = dwgFile;
  43.                    dsdEntry.Layout = "Model";
  44.                    dsdEntry.Title = title;
  45.                    dsdEntry.Nps = "0";
  46.                    dsdEntries.Add(dsdEntry);
  47.                    dsd.SetDsdEntryCollection(dsdEntries);
  48.  
  49.                    // set DsdData
  50.                    dsd.Dwf3dOptions.PublishWithMaterials = true;
  51.                    dsd.Dwf3dOptions.GroupByXrefHierarchy = true;
  52.                    dsd.SetUnrecognizedData("PwdProtectPublishedDWF", "FALSE");
  53.                    dsd.SetUnrecognizedData("PromptForPwd", "FALSE");
  54.                    dsd.SheetType = SheetType.SingleDwf;
  55.                    dsd.NoOfCopies = 1;
  56.                    dsd.ProjectPath = outputDir;
  57.                    dsd.IsHomogeneous = true;
  58.  
  59.                    if (File.Exists(dsdFile))
  60.                        File.Delete(dsdFile);
  61.  
  62.                    // write the DsdData file
  63.                    dsd.WriteDsd(dsdFile);
  64.  
  65.                    // get the Dsd File contents
  66.                    string str;
  67.                    using (StreamReader sr = new StreamReader(dsdFile, Encoding.Default))
  68.                    {
  69.                        str = sr.ReadToEnd();
  70.                    }
  71.                    // edit the contents
  72.                    str = str.Replace("Has3DDWF=0", "Has3DDWF=1");
  73.                    str = str.Replace("PromptForDwfName=TRUE", "PromptForDwfName=FALSE");
  74.                    // rewrite the Dsd file
  75.                    using (StreamWriter sw = new StreamWriter(dsdFile, false, Encoding.Default))
  76.                    {
  77.                        sw.Write(str);
  78.                    }
  79.  
  80.                    // import the Dsd file new contents in the DsdData
  81.                    dsd.ReadDsd(dsdFile);
  82.  
  83.                    File.Delete(dsdFile);
  84.  
  85.                    PlotConfig pc = PlotConfigManager.SetCurrentConfig("DWF6 ePlot.pc3");
  86.                    Application.Publisher.PublishExecute(dsd, pc);
  87.                }
  88.            }
  89.            finally
  90.            {
  91.                Application.SetSystemVariable("BACKGROUNDPLOT", bgPlot);
  92.            }
  93.        }
  94.    }
  95. }
  96.  

Hi gile - excellent post, but for some reason this isn't working for me. I don't have the "DWF6 ePlot.pc3" on my config so I had to change it to:

Code: [Select]
var pc = PlotConfigManager.SetCurrentConfig("Autodesk DWF Writer.pc3");

which should be available to me programatically, here it is in the options:


however when I do this, I get an error dialog:


and the following exception:
Code: [Select]
System.Exception {Autodesk.AutoCAD.Runtime.Exception }
Quote
{"eNullPtr"}

.. not sure what is causing this... any ideas?

gile

  • Water Moccasin
  • Posts: 2089
  • Marseille, France
Re: .NET Layout,ViewPort,Plot Routines
« Reply #16 on: September 20, 2017, 04:00:00 am »
Hi,

You'd rather use the _PLOTTERMANAGER command to check for installed plotters and look for one which uses the dwfplot1X.hdi driver.
Speaking English as a French Frog

flyte

  • Mosquito
  • Posts: 6
Re: .NET Layout,ViewPort,Plot Routines
« Reply #17 on: September 20, 2017, 09:59:46 am »
Hi,

You'd rather use the _PLOTTERMANAGER command to check for installed plotters and look for one which uses the dwfplot1X.hdi driver.

When I issue that command, it simply opens up an Explorer window showing the .pc3's, and indeed Autodesk DWF Writer.pc3 exists.


gile

  • Water Moccasin
  • Posts: 2089
  • Marseille, France
Re: .NET Layout,ViewPort,Plot Routines
« Reply #18 on: September 20, 2017, 11:48:09 am »
As far as I know, Autodesk DWF Writer is a plotter to be used with non Autodesk applications.

The PlotTo3dDwf class uses the "publish" mechanism with DSD files which requires the "DWF6 ePlot.pc3" plotter.
Speaking English as a French Frog