Author Topic: Why does this fail at the end?  (Read 3066 times)

0 Members and 1 Guest are viewing this topic.

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Why does this fail at the end?
« on: November 02, 2017, 11:47:51 PM »
This routine goes through as though it succeeded, then the bubble pops up on the bottom right of the screen telling me failed. I'm not experienced enough with the plot stuff to know why. Can anyone assist?

Code - C#: [Select]
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.PlottingServices;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Windows.Forms;
  9.  
  10. namespace ProCadCommands
  11. {
  12.     public partial class UserControl1 : UserControl
  13.     {
  14.         public UserControl1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         private void pc_updateviews_Click(object sender, EventArgs e)
  20.         {
  21.  
  22.             // Get the current document and database, and start a transaction
  23.             System.IO.Stream myStream;
  24.             OpenFileDialog thisDialog = new OpenFileDialog();
  25.  
  26.             thisDialog.InitialDirectory = "C:\\";
  27.             thisDialog.FilterIndex = 2;
  28.             thisDialog.RestoreDirectory = true;
  29.             thisDialog.Multiselect = true;
  30.             thisDialog.Title = "Please Select Source File(s) for Conversion";
  31.  
  32.             var collection = new List<string>();
  33.             if (thisDialog.ShowDialog() == DialogResult.OK)
  34.             {
  35.                 foreach (String file in thisDialog.FileNames)
  36.                 {
  37.  
  38.                     if ((myStream = thisDialog.OpenFile()) != null)
  39.                     {
  40.                         using (myStream)
  41.                         {
  42.                             collection.Add(file);
  43.                         }
  44.                     }
  45.                 }
  46.             }
  47.  
  48.             for (int i = 0; i < collection.Count; i++)
  49.             {
  50.                 var dwg = collection[i];
  51.                 var dir = Path.GetDirectoryName(dwg);
  52.                 var fn = Path.GetFileNameWithoutExtension(dwg);
  53.  
  54.                 string strFileName = collection[i];
  55.                 DocumentCollection acDocMgr = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
  56.                 acDocMgr.Open(strFileName, false);
  57.  
  58.  
  59.                 Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  60.                 Database acCurDb = acDoc.Database;
  61.                 Editor ed = acDoc.Editor;
  62.  
  63.                 using (DocumentLock acLckDoc = acDoc.LockDocument())
  64.                 {
  65.  
  66.                     using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  67.                     {
  68.                         HostApplicationServices.WorkingDatabase = acCurDb;
  69.  
  70.                         LayoutManager acLayoutMgr = LayoutManager.Current;
  71.  
  72.                         // Get the current layout and output its name in the Command Line window
  73.                         Layout acLayout = acTrans.GetObject(acLayoutMgr.GetLayoutId(acLayoutMgr.CurrentLayout), OpenMode.ForRead) as Layout;
  74.  
  75.                         // Get the PlotInfo from the layout
  76.                         using (PlotInfo acPlInfo = new PlotInfo())
  77.                         {
  78.                             acPlInfo.Layout = acLayout.ObjectId;
  79.  
  80.                             // Get a copy of the PlotSettings from the layout
  81.                             using (PlotSettings acPlSet = new PlotSettings(acLayout.ModelType))
  82.                             {
  83.                                 acPlSet.CopyFrom(acLayout);
  84.  
  85.                                 // Update the PlotSettings object
  86.                                 PlotSettingsValidator acPlSetVdr = PlotSettingsValidator.Current;
  87.  
  88.                                 // Set the plot type
  89.                                 acPlSetVdr.SetPlotType(acPlSet, Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);
  90.  
  91.                                 // Set the plot scale
  92.                                 acPlSetVdr.SetUseStandardScale(acPlSet, true);
  93.                                 acPlSetVdr.SetStdScaleType(acPlSet, StdScaleType.ScaleToFit);
  94.  
  95.                                 // Center the plot
  96.                                 acPlSetVdr.SetPlotCentered(acPlSet, true);
  97.  
  98.                                 // Set the plot device to use
  99.                                 acPlSetVdr.SetPlotConfigurationName(acPlSet, "DWG To PDF.pc3", "ARCH_D_(24.00_x_36.00_Inches)");
  100.  
  101.                                 // Set the plot info as an override since it will
  102.                                 // not be saved back to the layout
  103.                                 acPlInfo.OverrideSettings = acPlSet;
  104.  
  105.                                 // Validate the plot info
  106.                                 using (PlotInfoValidator acPlInfoVdr = new PlotInfoValidator())
  107.                                 {
  108.                                     acPlInfoVdr.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
  109.                                     acPlInfoVdr.Validate(acPlInfo);
  110.  
  111.                                     // Check to see if a plot is already in progress
  112.                                     if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
  113.                                     {
  114.                                         using (PlotEngine acPlEng = PlotFactory.CreatePublishEngine())
  115.                                         {
  116.                                             // Track the plot progress with a Progress dialog
  117.                                             using (PlotProgressDialog acPlProgDlg = new PlotProgressDialog(false, 1, true))
  118.                                             {
  119.                                                 using ((acPlProgDlg))
  120.                                                 {
  121.                                                     // Define the status messages to display
  122.                                                     // when plotting starts                                                            
  123.                                                     acPlProgDlg.set_PlotMsgString(PlotMessageIndex.DialogTitle, "Plot Progress");
  124.                                                     acPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage, "Cancel Job");
  125.                                                     acPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage, "Cancel Sheet");
  126.                                                     acPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "Sheet Set Progress");
  127.                                                     acPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "Sheet Progress");
  128.  
  129.                                                     // Set the plot progress range
  130.                                                     acPlProgDlg.LowerPlotProgressRange = 0;
  131.                                                     acPlProgDlg.UpperPlotProgressRange = 100;
  132.                                                     acPlProgDlg.PlotProgressPos = 0;
  133.  
  134.                                                     // Display the Progress dialog
  135.                                                     acPlProgDlg.OnBeginPlot();
  136.                                                     acPlProgDlg.IsVisible = true;
  137.  
  138.                                                     // Start to plot the layout
  139.                                                     acPlEng.BeginPlot(acPlProgDlg, null);
  140.  
  141.                                                     // Define the plot output
  142.                                                     acPlEng.BeginDocument(acPlInfo, acDoc.Name, null, 1, true, "c:\\myplot");
  143.  
  144.                                                     // Display information about the current plot
  145.                                                     acPlProgDlg.set_PlotMsgString(PlotMessageIndex.Status, "Plotting: " + acDoc.Name + " - " + acLayout.LayoutName);
  146.  
  147.                                                     // Set the sheet progress range
  148.                                                     acPlProgDlg.OnBeginSheet();
  149.                                                     acPlProgDlg.LowerSheetProgressRange = 0;
  150.                                                     acPlProgDlg.UpperSheetProgressRange = 100;
  151.                                                     acPlProgDlg.SheetProgressPos = 0;
  152.  
  153.                                                     // Plot the first sheet/layout
  154.                                                     using (PlotPageInfo acPlPageInfo = new PlotPageInfo())
  155.                                                     {
  156.                                                         acPlEng.BeginPage(acPlPageInfo, acPlInfo, true, null);
  157.                                                     }
  158.  
  159.                                                     acPlEng.BeginGenerateGraphics(null);
  160.                                                     acPlEng.EndGenerateGraphics(null);
  161.  
  162.                                                     // Finish plotting the sheet/layout
  163.                                                     acPlEng.EndPage(null);
  164.                                                     acPlProgDlg.SheetProgressPos = 100;
  165.                                                     acPlProgDlg.OnEndSheet();
  166.  
  167.                                                     // Finish plotting the document
  168.                                                     acPlEng.EndDocument(null);
  169.  
  170.                                                     // Finish the plot
  171.                                                     acPlProgDlg.PlotProgressPos = 100;
  172.                                                     acPlProgDlg.OnEndPlot();
  173.                                                     acPlEng.EndPlot(null);
  174.                                                 }
  175.                                             }
  176.                                         }
  177.                                     }
  178.                                 }
  179.                             }
  180.                         }
  181.  
  182.                     }
  183.                 }
  184.             }
  185.         }
  186.     }
  187. }
  188.  



Edit: attached solution to help
« Last Edit: November 03, 2017, 01:00:42 AM by FriendFromArea51 »

Atook

  • Swamp Rat
  • Posts: 1027
  • AKA Tim
Re: Why does this fail at the end?
« Reply #1 on: November 03, 2017, 12:27:50 AM »
Any additional info on the 'Plot failed' message, maybe a screenshot or something?

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: Why does this fail at the end?
« Reply #2 on: November 03, 2017, 12:57:48 AM »
Any additional info on the 'Plot failed' message, maybe a screenshot or something?

Unnfortunatelly it just says  "1 Error show below", and below that it says "ERROR"

LMAO no details

rugaroo

  • Bull Frog
  • Posts: 378
  • The Other CAD Guy
Re: Why does this fail at the end?
« Reply #3 on: November 03, 2017, 11:30:07 PM »
Ok, so I took what you uploaded. First if I don't have the C:\myplot folder then I get the error. So I create said folder, then using the code you have, I get an error, eNoFileName on line 142. if i try:

Code: [Select]
                                                    // Define the plot output
                                                    acPlEng.BeginDocument(acPlInfo, acDoc.Name, null, 1, true, "c:\\myplot\\" + acDoc.Name);

I get the same eNoFileName... Buuuuuuuut....

Code: [Select]
                                                    // Define the plot output
                                                    acPlEng.BeginDocument(acPlInfo, acDoc.Name, null, 1, true, "c:\\myplot\\1.pdf");

Creates the PDF just fine....

Maybe this will help get you in the right direction...

Enjoy!
Roland
LDD06-09 | C3D 04-19 | Infraworks 360 | VS2012-VS2017

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Why does this fail at the end?
« Reply #4 on: November 03, 2017, 11:47:53 PM »
Make sure you have set Plot and Publish Log File checked.
Then run your code and go to PlotandPublishLog.CSV file(if you set for log for continuous)
If your not sure where the file is you can get it or set it on the Files tab in the Options dialog box

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: Why does this fail at the end?
« Reply #5 on: November 04, 2017, 08:24:32 PM »
Thanks will give it a shot :)

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: Why does this fail at the end?
« Reply #6 on: November 04, 2017, 08:46:50 PM »
Ok, so I took what you uploaded. First if I don't have the C:\myplot folder then I get the error. So I create said folder, then using the code you have, I get an error, eNoFileName on line 142. if i try:

Code: [Select]
                                                    // Define the plot output
                                                    acPlEng.BeginDocument(acPlInfo, acDoc.Name, null, 1, true, "c:\\myplot\\" + acDoc.Name);

I get the same eNoFileName... Buuuuuuuut....

Code: [Select]
                                                    // Define the plot output
                                                    acPlEng.BeginDocument(acPlInfo, acDoc.Name, null, 1, true, "c:\\myplot\\1.pdf");

Creates the PDF just fine....

Maybe this will help get you in the right direction...

Enjoy!
Roland


Still no luck...guess it's just something unique on my end.

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: Why does this fail at the end?
« Reply #7 on: November 04, 2017, 09:11:42 PM »
Make sure you have set Plot and Publish Log File checked.
Then run your code and go to PlotandPublishLog.CSV file(if you set for log for continuous)
If your not sure where the file is you can get it or set it on the Files tab in the Options dialog box

Thanks! Didn't know about this. Appreciate it :)

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Why does this fail at the end?
« Reply #8 on: November 12, 2017, 01:26:24 PM »
Code - C#: [Select]
  1. acadApp.SetSystemVariable("EXPORTPAPERSPACE", 1);
  2.                 doc.SendStringToExecute("ExportPdf ", false, true, true);
  3. I use this but first you must set the layout to have the right page set up
  4. lo.CopyFrom(pageSetup);
  5.  
  6. I also use what you have but add
  7.        string sdwg = (string)acadApp.GetSystemVariable("Dwgname");
  8.                  sFile +="\\"+ sdwg.Replace("dwg", "pdf");
  9.            
  10.                 if (File.Exists(sFile))
  11.                 {
  12.                     DialogResult dr = MessageBox.Show("The file " + sFile + " exists, choose yes to replace", "Replace file", MessageBoxButtons.YesNo);
  13.                     if (dr == DialogResult.No) return;
  14.                     File.Delete(sFile);
  15.                 }
Works  fine


EDIT (John Kaul): Added code tags.
« Last Edit: November 13, 2017, 07:43:35 AM by John Kaul (Se7en) »