I just found a solution when the TARGET is not equal to zero. The TARGET variable affects the plot window coordinates constantly. So, the solution is to create a translation matrix for it and always transform the plot window coordinates using this translation matrix. My code looks like this:
private Matrix3d GetTranslationMatrixFromTarget()
{
try
{
ViewTableRecord vtr = ed.GetCurrentView();
Point3d targetPoint = vtr.Target;
if (!targetPoint.IsEqualTo(Point3d.Origin))
{
Matrix3d translationMatrix = Matrix3d.Displacement(-targetPoint.GetAsVector());
return translationMatrix;
}
else
{
return Matrix3d.Identity;
}
}
catch (Exception ex)
{
ed.WriteMessage($"\nError in GetTranslationMatrixFromTarget function: {ex.Message}");
return Matrix3d.Identity;
}
}
And used like this:
private void ValidateAndPlot(Layout layOut, Point3d point0, Point3d point1, string path, string fileName, PlotProgressDialog pd)
{
Matrix3d matrix3D = GetTranslationMatrixFromTarget();
point0 = point0.TransformBy(matrix3D);
point1 = point1.TransformBy(matrix3D);
Point2d min = new Point2d(point0.X, point0.Y);
Point2d max = new Point2d(point1.X, point1.Y);
string style = layOut.CurrentStyleSheet;
if (style == string.Empty)
{
style = "acad.ctb";
}
string plotDeviceName = "DWG To PDF.pc3";
Extents2d pw = new Extents2d(min, max);
try
{
using (PlotInfo pi = new PlotInfo())
{
pi.Layout = layOut.ObjectId;
using (PlotSettings ps = new PlotSettings(layOut.ModelType))
{
ps.CopyFrom(layOut);
PlotSettingsValidator psv = PlotSettingsValidator.Current;
psv.SetPlotWindowArea(ps, pw);
psv.SetPlotType(ps, Autodesk.AutoCAD.DatabaseServices.PlotType.Window);
psv.SetPlotConfigurationName(ps, plotDeviceName, layOut.CanonicalMediaName);
if (ps.CurrentStyleSheet == string.Empty)
{
psv.SetCurrentStyleSheet(ps, style);
}
psv.SetPlotPaperUnits(ps, layOut.PlotPaperUnits);
psv.SetUseStandardScale(ps, true);
psv.SetStdScaleType(ps, StdScaleType.ScaleToFit);
psv.SetPlotCentered(ps, true);
psv.SetPlotRotation(ps, layOut.PlotRotation);
pi.OverrideSettings = ps;
using (PlotInfoValidator piv = new PlotInfoValidator())
{
piv.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
piv.Validate(pi);
if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
{
using (PlotEngine pe = PlotFactory.CreatePublishEngine())
{
pd.OnBeginPlot();
pe.BeginPlot(pd, null);
pe.BeginDocument(pi, null, null, 0, true, Path.Combine($"{path}\\{fileName}.pdf"));
pd.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, $"Plotting: {fileName}");
pd.OnBeginSheet();
PlotPageInfo ppi = new PlotPageInfo();
pe.BeginPage(ppi, pi, true, null);
pe.BeginGenerateGraphics(null);
pe.EndGenerateGraphics(null);
pd.SheetProgressPos = 100;
pd.OnEndSheet();
pe.EndPage(null);
pe.EndDocument(null);
pd.OnEndPlot();
pe.EndPlot(null);
}
}
else
{
throw new InvalidOperationException("Plot engine is in an invalid state.");
}
}
}
}
}
catch (Exception ex)
{
throw new Exception($"{ex.Message}");
}
}