By Joe Ye
I cannot change the print range settings through below command. Would you please tell me how to change the print range?
public Result Execute(ExternalCommandData commandData,
ref string messages, ElementSet elements)
{
try
{
Application app = commandData.Application.Application;
Document doc = app.ActiveUIDocument.Document;
doc.PrintManager.PrintRange = PrintRange.Select;
doc.PrintManager.Apply();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(
ex.Message);
}
return Result.Succeeded;
}
Solution
Document.PrintManager returns a new PrintManager instance with the current status. Each time we call Document.PrintManager, it returns a new PrinterManger. For example, in your command you call doc.PrintManager twice. The two PrintManager instances returned are different. So the below two lines work on two different PrintManager instances. However we might easily think that we keep working on the same PrintManager.
_doc.PrintManager.PrintRange = PrintRange.Select;
_doc.PrintManager.Apply();
The former calling of changing PrintRange doesn’t take effect, because the Apply() calling is not taken place on previous PrintManager instance.
Instead, we need to assign current PrintManager to an variable. Later changes to PrintManager are all taken place on this variable. Changes to PringRange property can be saved by Apply() method.
public Result Execute(ExternalCommandData commandData,
ref string messages, ElementSet elements)
{
try
{
Application app = commandData.Application.Application;
Document doc = app.ActiveUIDocument.Document;
PrintManager pt = doc.PrintManager;
pt.PrintRange = Autodesk.Revit.DB.PrintRange.Select;
pt.Apply();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(
ex.Message);
}
return Result.Succeeded;
}