- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Can anyone tell me how I can recreate the function that focuses on an object in AutoCAD through the .NET API?
I'm trying but I only managed to do it for 2D, for 3D it gets lost and focuses on an empty place.
My code that I was testing:
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 > height && depth > 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}");
}
}
}
Solved! Go to Solution.