ViewSheet Set to be printed setting problem

ViewSheet Set to be printed setting problem

Anonymous
Not applicable
877 Views
1 Reply
Message 1 of 2

ViewSheet Set to be printed setting problem

Anonymous
Not applicable

I am using C# to develop an addin for Revit 2016.

The addins is to show a datagridview which contains the viewsheets in the project. Each row in the datagridview corresponds to a viewsheet, then user click a button, the code will print the viewsheet row by row with sheet size selected by the user from a combo box cell in each row.

When I run the code for the first time, every thing goes well. But when I run the code again in the same project document,  the addin always prints the viewSheet in the last row of the datagridview when printing the viewsheets in 1st, 2nd row ... of the datagridview.(that is to say.. if we have five viewsheets in the datagridview, the addin always prints the last viewsheet for 5 times.)  How to fix the problem? Folloing is the code:

 

 foreach (DataGridViewRow row in dataGridView1.Rows)
{
  ViewSheet targetViewSheet = document.GetElement(new ElementId(Convert.ToInt32(row.Cells["sheetID"].Value.ToString()))) as ViewSheet;
  if (targetViewSheet != null)
  {
string targetPaperSizeName = row.Cells["sheetType"].Value.ToString(); string fullFileName = fullPDFPath + targetViewSheet.SheetNumber + @".pdf"; string viewSheetInfo = row.Cells["sheetInfo"].Value.ToString(); PrintManager pm = document.PrintManager; using (Transaction tx = new Transaction(document, "Export the pdf of " + targetViewSheet.Name)) { tx.Start(); ViewSet targetViewSet = new ViewSet(); targetViewSet.Insert(targetViewSheet); PrintSetup pSetup = pm.PrintSetup; PrintParameters pParams = pSetup.CurrentPrintSetting.PrintParameters; pParams.PaperPlacement = PaperPlacementType.Margins; try { pm.PrintSetup.Save(); } catch { } pParams.ZoomType = ZoomType.Zoom; try { pm.PrintSetup.Save(); } catch { } if (pParams.PaperSize.Name != targetPaperSizeName) { foreach (PaperSize ps in pm.PaperSizes) { if (ps.Name == targetPaperSizeName) { pParams.PaperSize = ps; } } } try { pm.PrintSetup.Save(); } catch { } pm.Apply(); pm.PrintRange = PrintRange.Select; ViewSheetSetting viewSheetSetting = pm.ViewSheetSetting; viewSheetSetting.CurrentViewSheetSet.Views = targetViewSet; try { viewSheetSetting.SaveAs(targetViewSheet.SheetNumber); viewSheetSetting.Save(); } catch { } pm.Apply(); pm.SelectNewPrintDriver(selectedPrinterName); pm.Apply(); pm.CombinedFile = false; pm.Apply(); pm.PrintToFile = true; pm.Apply(); pm.PrintToFileName = fullFileName; pm.Apply(); if (File.Exists(fullFileName)) File.Delete(fullFileName); pm.SubmitPrint(); tx.Commit(); } } }

 

 

 

0 Likes
Accepted solutions (1)
878 Views
1 Reply
Reply (1)
Message 2 of 2

Anonymous
Not applicable
Accepted solution

I have already solved this problem.

Before   this line of code

viewSheetSetting.CurrentViewSheetSet.Views = targetViewSet;

 Add     (we need to add try catch block because if we delete the current viewsheet set views in session, it will throw an exception.)

try {
   viewSheetSetting.Delete();
} catch { }
0 Likes