Need help to set the TARGET variable zero in C#.

Need help to set the TARGET variable zero in C#.

luckyboy82292
Advocate Advocate
538 Views
4 Replies
Message 1 of 5

Need help to set the TARGET variable zero in C#.

luckyboy82292
Advocate
Advocate

Hey everyone!

As you can see from the title, I've tried changing the TARGET value, but it also changes the view in the model. How can I do this without affecting the view in the model tab?

Any help would be greatly appreciated. Thank you!

private void ResetTarget()
{
    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        try
        {
            ViewTableRecord vtr = ed.GetCurrentView();
            vtr.Target = new Point3d(0, 0, 0);
            ed.SetCurrentView(vtr);

            tr.Commit();

        }
        catch (Exception ex)
        {
            ed.WriteMessage($"\nError in ResetTarget function: {ex.Message}");
        }
    }
}

 

0 Likes
Accepted solutions (1)
539 Views
4 Replies
Replies (4)
Message 2 of 5

ActivistInvestor
Mentor
Mentor

Your question doesn't make sense.

 

The Target property defines the center point of a view. If you apply a ViewTableRecord to the current view, it is going to modify the view to have the target specified in the ViewTableRecord.

0 Likes
Message 3 of 5

luckyboy82292
Advocate
Advocate

May we Zoom Extents in a new created side database before saving it? Or set an initial view?

0 Likes
Message 4 of 5

luckyboy82292
Advocate
Advocate
Accepted solution

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}");
    }
}

 

0 Likes
Message 5 of 5

ActivistInvestor
Mentor
Mentor

@luckyboy82292 wrote:

May we Zoom Extents in a new created side database before saving it? Or set an initial view?


You can open the ViewportTableRecord having the name "*Active" and modify it.

 

 

0 Likes