Author Topic: Get Multi Page Pdf files of different parts of the same drawing  (Read 2202 times)

0 Members and 1 Guest are viewing this topic.

wilbur

  • Guest
Hi,
I want to get plot a multi page pdf files in autocad,using the different parts of the same drawing.
Here is what I get.
First I past my key code below.
Code - C#: [Select]
  1.          //get print info
  2.             //contains the target window area
  3.             List<FrameAttributeExtend> entitylist = GetFrameEntity(title);
  4.  
  5.             Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  6.             Database acCurDb = acDoc.Database;
  7.             Transaction acTrans = acCurDb.TransactionManager.StartTransaction();
  8.  
  9.             object iSys = AcadApplication.GetSystemVariable("BACKGROUNDPLOT");
  10.             try
  11.             {
  12.                 using (acTrans)
  13.                 {
  14.                     int flag = 1;
  15.  
  16.                     //AcadApplication.SetSystemVariable("BACKGROUNDPLOT", 1);
  17.                     AcadApplication.SetSystemVariable("BACKGROUNDPLOT", 0);
  18.                     if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
  19.                     {
  20.  
  21.                         PlotProgressDialog ppd = new PlotProgressDialog(false, entitylist.Count, true);
  22.                         using (ppd)
  23.                         {
  24.                             ppd.OnBeginPlot();
  25.                             PlotEngine pe = PlotFactory.CreatePublishEngine();
  26.                             pe.BeginPlot(ppd, null);
  27.  
  28.                             ppd.StatusMsgString = "Plotting..."; // add name of document, if you need it
  29.  
  30.                             ppd.LowerSheetProgressRange = 0;
  31.                             ppd.UpperSheetProgressRange = 100;
  32.                             ppd.SheetProgressPos = 0;
  33.  
  34.                             ppd.OnBeginSheet();
  35.  
  36.                             foreach (var frame in entitylist)
  37.                             {
  38.                                 //get cur layoutmanager
  39.                                 LayoutManager acLayoutMgr = LayoutManager.Current;
  40.                                 //get current layout
  41.                                 Layout acLayout = (Layout)acTrans.GetObject(acLayoutMgr.GetLayoutId(acLayoutMgr.CurrentLayout), OpenMode.ForRead);
  42.                                 //Get print layout info
  43.                                 PlotInfo pi = new PlotInfo() { Layout = acLayout.ObjectId };
  44.                                 PlotSettings ps = new PlotSettings(acLayout.ModelType);
  45.                                 ps.CopyFrom(acLayout);
  46.  
  47.                                 PlotSettingsValidator psv = PlotSettingsValidator.Current;
  48.                                 //set the plot style
  49.                                 System.Collections.Specialized.StringCollection sc = psv.GetPlotStyleSheetList();
  50.                                 psv.SetCurrentStyleSheet(ps, "TestStyle.ctb");
  51.  
  52.                                 psv.SetPlotType(ps, PlotType.Extents);
  53.                                 psv.SetUseStandardScale(ps, true);
  54.                                 psv.SetStdScaleType(ps, StdScaleType.ScaleToFit);
  55.                                 psv.SetPlotCentered(ps, true);
  56.  
  57.                                 if (!Directory.Exists(directory))
  58.                                     Directory.CreateDirectory(directory);
  59.  
  60.                                 //scale
  61.                                 string entityscale = frame.Scale;
  62.                                 //orientation
  63.                                 string vertial = frame.IsVertial;
  64.                                 //index
  65.                                 string picindex = frame.ImageIndex;
  66.                                 string imagename = "";
  67.                                 if (frame.ImageType == 0)
  68.                                 {
  69.                                     imagename = string.Format("1-{0}-{1}-{2}.pdf", entityscale, vertial, picindex);
  70.                                     totalflatimage = directory + imagename;
  71.                                 }
  72.                                 else
  73.                                 {
  74.                                     imagename = string.Format("2-{0}-{1}-{2}.pdf", entityscale, vertial, picindex);
  75.                                     imagelist.Add(directory + imagename);
  76.                                 }
  77.  
  78.  
  79.                                 ps.ShowPlotStyles = true;
  80.  
  81.                                 //set print extends
  82.                                 Extents3d extents3d = frame.Extent;
  83.                                
  84.                                 double h = double.Parse(entityscale) * 6;
  85.                                 double x = extents3d.MinPoint.X + unit * 2;
  86.                                 double y = extents3d.MaxPoint.Y + h + unit;
  87.                                 if (unit == 1)
  88.                                 {
  89.                                     x = extents3d.MinPoint.X + unit * 1;
  90.                                     y = extents3d.MaxPoint.Y + double.Parse(entityscale) / 250 + unit * 2;
  91.                                 }
  92.                                 Extents2d E2d = new Extents2d(x, extents3d.MinPoint.Y, extents3d.MaxPoint.X, y);
  93.  
  94.                                 psv.SetPlotPaperUnits(ps, PlotPaperUnit.Millimeters);
  95.                                 //psv.SetPlotOrigin(ps, new Point2d(extents3d.MinPoint.X, extents3d.MinPoint.Y));
  96.                                 psv.SetPlotWindowArea(ps, E2d);
  97.                                 //psv.SetPlotOrigin(ps, E2d.MinPoint);
  98.                                 psv.SetPlotType(ps, Autodesk.AutoCAD.DatabaseServices.PlotType.Window);
  99.  
  100.                                 psv.SetPlotConfigurationName(ps, "DWG To PDF.pc3", "ISO_expand_A4_(210.00_x_297.00_mm)");
  101.                                 psv.SetPlotRotation(ps, PlotRotation.Degrees000);
  102.  
  103.                                 PlotInfoValidator piv = new PlotInfoValidator();
  104.                                 piv.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
  105.                                 pi.OverrideSettings = ps;
  106.                                 piv.Validate(pi);
  107.  
  108.                                 ppd.IsVisible = true;
  109.                                 if (flag == 1)
  110.                                 {
  111.                                     pe.BeginDocument(pi, acDoc.Name, null, 1, true, directory + imagename);
  112.                                 }
  113.                                 ppd.LowerSheetProgressRange = 0;
  114.                                 ppd.UpperSheetProgressRange = 100;
  115.                                 ppd.SheetProgressPos = 0;
  116.  
  117.                                 PlotPageInfo ppi = new PlotPageInfo();
  118.                                 pe.BeginPage(ppi, pi, flag == entitylist.Count, null);
  119.                                 pe.BeginGenerateGraphics(null);
  120.                                 ppd.SheetProgressPos = 50;
  121.                                 pe.EndGenerateGraphics(null);
  122.  
  123.                                 pe.EndPage(null);
  124.                                 ppd.SheetProgressPos = 100;
  125.  
  126.  
  127.                                 ppd.OnEndSheet();
  128.  
  129.                                 ppd.PlotProgressPos = (100 / entitylist.Count) * flag;
  130.                                 flag++;
  131.                             }
  132.                             pe.EndDocument(null);
  133.                             ppd.PlotProgressPos = 100;
  134.                             ppd.OnEndPlot();
  135.                             pe.EndPlot(null);
  136.  
  137.                             pe.Dispose();
  138.                         }
  139.  
  140.                     }
  141.                 }
  142.                 //verify the plot state
  143.                 while (PlotFactory.ProcessPlotState != ProcessPlotState.NotPlotting)
  144.                     continue;
  145.                 MessageBox.Show("The Plot Is Complated!");
  146.             }
  147.             catch (Exception ex)
  148.             {
  149.                 return null;
  150.             }
  151.             finally
  152.             {
  153.                 AcadApplication.SetSystemVariable("BACKGROUNDPLOT", iSys);
  154.                 acTrans.Dispose();
  155.             }
  156.         }
  157.  
  158.  

Although I used two different size of plot paper size ,
Code - C#: [Select]
  1. if (frame.ImageType == 0)
  2.                                 {
  3.                                     imagename = string.Format("1-{0}-{1}-{2}.pdf", entityscale, vertial, picindex);
  4.                                     totalflatimage = directory + imagename;
  5.                                 }
  6.                                 else
  7.                                 {
  8.                                     imagename = string.Format("2-{0}-{1}-{2}.pdf", entityscale, vertial, picindex);
  9.                                     imagelist.Add(directory + imagename);
  10.                                 }
  11.  
I actually only use one size in my test.

Here is some problems,and wished to get some help.
(1).if I  called the psv.SetPlotOrigin function,I got a multi page pdf file in a real fast speed,but the content is not correct, actually,it's blank in each page.
(2).I tried not to call psv.SetPlotOrigin function,and I get the multi pages files i want,but the problem is it takes about 20 minutes to generate the file with 4 pages(I used 4 to test).
(3).I tried another way to creat PlotEngine each time,and it takes too much time.
My current problem is whatever i try to do,and it will takes too much time to plot the pdf file.And I use the printer in cad manually,the cad only take abount less than 1 second to generate a one page pdf file.

Note:the list<FrameAttributeExtend> object contains my target region infos I want to plot.

Any of your help will do me a big favor.
« Last Edit: March 30, 2016, 02:25:29 AM by wilbur »

wilbur

  • Guest
Re: Get Multi Page Pdf files of different parts of the same drawing
« Reply #1 on: March 28, 2016, 10:10:31 PM »
Still waiting for your help...

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: Get Multi Page Pdf files of different parts of the same drawing
« Reply #2 on: March 29, 2016, 12:27:04 AM »
Do you really need this?

Code - C#: [Select]
  1. //verify the plot state
  2.                 while (PlotFactory.ProcessPlotState != ProcessPlotState.NotPlotting)
  3.                     continue;

that will loop if it is anything other than NotPlotting and who knows what is going on in the background or when the plotter instance is disposed (or some other dependency)?
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

wilbur

  • Guest
Re: Get Multi Page Pdf files of different parts of the same drawing
« Reply #3 on: March 29, 2016, 01:38:48 AM »
Do you really need this?

Code - C#: [Select]
  1. //verify the plot state
  2.                 while (PlotFactory.ProcessPlotState != ProcessPlotState.NotPlotting)
  3.                     continue;

that will loop if it is anything other than NotPlotting and who knows what is going on in the background or when the plotter instance is disposed (or some other dependency)?

Actually, I use this loop to ensure that the plot is complete, and I don't know if it has influence  here,
and also,I just call it for once.
« Last Edit: March 29, 2016, 02:16:11 AM by wilbur »

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: Get Multi Page Pdf files of different parts of the same drawing
« Reply #4 on: March 29, 2016, 06:06:33 PM »
Just for kicks, can you place this code in its place? At least, you can perhaps eliminate that part as a problem and the cause of why it takes so long.

Code - C#: [Select]
  1. //verify the plot state
  2. while (PlotFactory.ProcessPlotState != ProcessPlotState.NotPlotting)
  3. {
  4.      acDoc.Editor.WriteMessage("The Plot is still going!");
  5. }
  6.  

You don't need the continue, the 'while' will continue until the condition is met.
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

wilbur

  • Guest
Re: Get Multi Page Pdf files of different parts of the same drawing
« Reply #5 on: March 29, 2016, 09:28:04 PM »
Just for kicks, can you place this code in its place? At least, you can perhaps eliminate that part as a problem and the cause of why it takes so long.

Code - C#: [Select]
  1. //verify the plot state
  2. while (PlotFactory.ProcessPlotState != ProcessPlotState.NotPlotting)
  3. {
  4.      acDoc.Editor.WriteMessage("The Plot is still going!");
  5. }
  6.  
You don't need the continue, the 'while' will continue until the condition is met.

actually,I test the code befor using below code:
Code - C#: [Select]
  1. while(true)
  2. {
  3.        if(PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
  4.               break;
  5. }
  6.  
And I find it's still not working...
That's why I have to post the question here....

wilbur

  • Guest
Re: Get Multi Page Pdf files of different parts of the same drawing
« Reply #6 on: March 29, 2016, 09:59:07 PM »
Hi  everyone,
Recently, I did some test and I find something strange...
I found that the PlotEngine.BeginGenerateGraphics() function takes too much time,and the time has no relation to the drawing,because I use a simple drawing to do the test and it still takes that much time, and I think it has something influence with the target paper size.when i tried the A4 size (which in the sample is "ISO_expand_A4_(210.00_x_297.00_mm)") and it really takes too much time in my test
Any help is helpful to me,
Wish to get your help...

wilbur

  • Guest
Re: Get Multi Page Pdf files of different parts of the same drawing
« Reply #7 on: March 30, 2016, 02:24:28 AM »
Hi, everyone, I found the problem.And it's really  really strange,and I found it in a coincidence.
In fact the pasted code is correct.
I always debug in visual studio and I start the autocad in Visual Studio so I wouldn't able to do it correctly,and I don't know why.
At last, I netload the dll and start autocad without visual studio, I get what I want and the speed is much faster than the visual stuio way.
And more strange.
I start AutoCad with Autocad and do not netload my debug dll,I use cad printer to print a drawing to pdf ,and it takes a much long time as I usually do.But when I start autocad directlly and I user the same drawing the same printer config as before,It would take only about less than 3 seconds to get the pdf.
And for sure, the BackgroundPlot is set to 0 in both situation.
And I solve my problem this way
sorry I do not know why.