<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Focus on a specific ObjectId via API in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/focus-on-a-specific-objectid-via-api/m-p/13085630#M2280</link>
    <description>&lt;P&gt;Can anyone tell me how I can recreate the function that focuses on an object in AutoCAD through the .NET API?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;I'm trying but I only managed to do it for 2D, for 3D it gets lost and focuses on an empty place.&lt;BR /&gt;&lt;BR /&gt;My code that I was testing:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public static void FocusOnObject(ObjectId objId)
{
    Document doc = AcadApp.DocumentManager.MdiActiveDocument;
    Editor ed = doc.Editor;

    if (objId == ObjectId.Null)
    {
        ed.WriteMessage($"\nItem inválido!\n");
        return;
    }

    using (Transaction tr = doc.TransactionManager.StartTransaction())
    {
        try
        {
            Entity entity = tr.GetObject(objId, OpenMode.ForRead) as Entity;

            if (entity != null)
            {
                ed.SetImpliedSelection(new ObjectId[] { objId });

                if (entity.Bounds.HasValue)
                {
                    Extents3d extents = entity.GeometricExtents;

                    using (ViewTableRecord view = ed.GetCurrentView())
                    {
                        Point3d minPt = extents.MinPoint;
                        Point3d maxPt = extents.MaxPoint;

                        Point3d center = new Point3d(
                            (minPt.X + maxPt.X) / 2.0,
                            (minPt.Y + maxPt.Y) / 2.0,
                            (minPt.Z + maxPt.Z) / 2.0);

                        double margin = 0.1; // 10% of margin
                        double width = (maxPt.X - minPt.X) * (1 + margin);
                        double height = (maxPt.Y - minPt.Y) * (1 + margin);
                        double depth = (maxPt.Z - minPt.Z) * (1 + margin);

                        view.Target = center;

                        view.Height = height;
                        view.Width = width;

                        view.ViewDirection = new Vector3d(view.ViewDirection.X, view.ViewDirection.Y, 1.0);

                        if (depth &amp;gt; height &amp;amp;&amp;amp; depth &amp;gt; width)
                        {
                            view.LensLength = depth;
                        }

                        ed.SetCurrentView(view);
                    }
                }
                else
                {
                    ed.WriteMessage("\nA entidade não possui extents válidos.");
                }
            }

            tr.Commit();
        }
        catch (System.Exception ex)
        {
            ed.WriteMessage($"Erro ao focar no objeto: {ex.Message}");
        }
    }
}&lt;/LI-CODE&gt;</description>
    <pubDate>Tue, 15 Oct 2024 14:12:29 GMT</pubDate>
    <dc:creator>eduardo_salvador02</dc:creator>
    <dc:date>2024-10-15T14:12:29Z</dc:date>
    <item>
      <title>Focus on a specific ObjectId via API</title>
      <link>https://forums.autodesk.com/t5/net-forum/focus-on-a-specific-objectid-via-api/m-p/13085630#M2280</link>
      <description>&lt;P&gt;Can anyone tell me how I can recreate the function that focuses on an object in AutoCAD through the .NET API?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;I'm trying but I only managed to do it for 2D, for 3D it gets lost and focuses on an empty place.&lt;BR /&gt;&lt;BR /&gt;My code that I was testing:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public static void FocusOnObject(ObjectId objId)
{
    Document doc = AcadApp.DocumentManager.MdiActiveDocument;
    Editor ed = doc.Editor;

    if (objId == ObjectId.Null)
    {
        ed.WriteMessage($"\nItem inválido!\n");
        return;
    }

    using (Transaction tr = doc.TransactionManager.StartTransaction())
    {
        try
        {
            Entity entity = tr.GetObject(objId, OpenMode.ForRead) as Entity;

            if (entity != null)
            {
                ed.SetImpliedSelection(new ObjectId[] { objId });

                if (entity.Bounds.HasValue)
                {
                    Extents3d extents = entity.GeometricExtents;

                    using (ViewTableRecord view = ed.GetCurrentView())
                    {
                        Point3d minPt = extents.MinPoint;
                        Point3d maxPt = extents.MaxPoint;

                        Point3d center = new Point3d(
                            (minPt.X + maxPt.X) / 2.0,
                            (minPt.Y + maxPt.Y) / 2.0,
                            (minPt.Z + maxPt.Z) / 2.0);

                        double margin = 0.1; // 10% of margin
                        double width = (maxPt.X - minPt.X) * (1 + margin);
                        double height = (maxPt.Y - minPt.Y) * (1 + margin);
                        double depth = (maxPt.Z - minPt.Z) * (1 + margin);

                        view.Target = center;

                        view.Height = height;
                        view.Width = width;

                        view.ViewDirection = new Vector3d(view.ViewDirection.X, view.ViewDirection.Y, 1.0);

                        if (depth &amp;gt; height &amp;amp;&amp;amp; depth &amp;gt; width)
                        {
                            view.LensLength = depth;
                        }

                        ed.SetCurrentView(view);
                    }
                }
                else
                {
                    ed.WriteMessage("\nA entidade não possui extents válidos.");
                }
            }

            tr.Commit();
        }
        catch (System.Exception ex)
        {
            ed.WriteMessage($"Erro ao focar no objeto: {ex.Message}");
        }
    }
}&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 15 Oct 2024 14:12:29 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/focus-on-a-specific-objectid-via-api/m-p/13085630#M2280</guid>
      <dc:creator>eduardo_salvador02</dc:creator>
      <dc:date>2024-10-15T14:12:29Z</dc:date>
    </item>
    <item>
      <title>Re: Focus on a specific ObjectId via API</title>
      <link>https://forums.autodesk.com/t5/net-forum/focus-on-a-specific-objectid-via-api/m-p/13085819#M2281</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have to transform the extents of the entity from World coordinates (WCS) to the current view display coordinates (DCS) before usint it to define the view center and size.&lt;/P&gt;
&lt;P&gt;Here's an example from your code integrating some code from &lt;A href="https://github.com/gileCAD/Gile.AutoCAD.Extension" target="_blank" rel="noopener"&gt;this library&lt;/A&gt; ( specifically &lt;A href="https://github.com/gileCAD/Gile.AutoCAD.Extension/blob/master/Gile.AutoCAD.R20.Extension/AbstractViewTableRecordExtension.cs" target="_blank" rel="noopener"&gt;AbstractViewTableRecordExtension&lt;/A&gt; and &lt;A href="https://github.com/gileCAD/Gile.AutoCAD.Extension/blob/master/Gile.AutoCAD.R20.Extension/EditorExtension.cs" target="_blank" rel="noopener"&gt;EditorExtension ).&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;public static void FocusOnObject(ObjectId objId)
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    Editor ed = doc.Editor;

    if (objId.IsNull)
    {
        ed.WriteMessage($"\nItem inválido!\n");
        return;
    }

    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        try
        {
            Entity entity = tr.GetObject(objId, OpenMode.ForRead) as Entity;

            if (entity != null)
            {
                if (entity.Bounds.HasValue)
                {
                    Extents3d extents = entity.GeometricExtents;

                    using (ViewTableRecord view = ed.GetCurrentView())
                    {
                        Matrix3d worldToEye =
                            Matrix3d.WorldToPlane(view.ViewDirection) *
                            Matrix3d.Displacement(view.Target.GetAsVector().Negate()) *
                            Matrix3d.Rotation(view.ViewTwist, view.ViewDirection, view.Target);

                        extents.TransformBy(worldToEye);
                        view.Width = extents.MaxPoint.X - extents.MinPoint.X;
                        view.Height = extents.MaxPoint.Y - extents.MinPoint.Y;
                        view.CenterPoint = new Point2d(
                            (extents.MaxPoint.X + extents.MinPoint.X) / 2.0,
                            (extents.MaxPoint.Y + extents.MinPoint.Y) / 2.0);
                        ed.SetCurrentView(view);
                    }
                }
                else
                {
                    ed.WriteMessage("\nA entidade não possui extents válidos.");
                }
            }

            tr.Commit();
        }
        catch (System.Exception ex)
        {
            ed.WriteMessage($"Erro ao focar no objeto: {ex.Message}");
        }
    }
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Referencing Gile.AutoCAD.R20.Extension.dll (or Gile.AutoCAD.R25.Extension.dll for AutoCAD 2025), you could simply write:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;public static void FocusOnObject(ObjectId objId)
{
    var ed = Active.Editor;

    if (objId.IsNull)
    {
        ed.WriteMessage($"\nItem inválido!\n");
        return;
    }

    using (var tr = Active.Database.TransactionManager.StartTransaction())
    {
        try
        {
            if (objId.TryGetObject(tr, out Entity entity))
            {
                if (entity.Bounds.HasValue)
                {
                    ed.Zoom(entity.GeometricExtents);
                }
                else
                {
                    ed.WriteMessage("\nA entidade não possui extents válidos.");
                }
            }

            tr.Commit();
        }
        catch (System.Exception ex)
        {
            ed.WriteMessage($"Erro ao focar no objeto: {ex.Message}");
        }
    }
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Oct 2024 18:44:43 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/focus-on-a-specific-objectid-via-api/m-p/13085819#M2281</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2024-10-15T18:44:43Z</dc:date>
    </item>
    <item>
      <title>Re: Focus on a specific ObjectId via API</title>
      <link>https://forums.autodesk.com/t5/net-forum/focus-on-a-specific-objectid-via-api/m-p/13097420#M2282</link>
      <description>&lt;P&gt;Thank you very much, this solved my need.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2024 11:14:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/focus-on-a-specific-objectid-via-api/m-p/13097420#M2282</guid>
      <dc:creator>eduardo_salvador02</dc:creator>
      <dc:date>2024-10-21T11:14:04Z</dc:date>
    </item>
  </channel>
</rss>

