Hi All,
I try to write some code to zooom to objects but nothing happen.
Here is my Code
'--Pass array of Object ID Public Function AutoCADZoomByObjectIDs(ByRef objObjectIDs() As ObjectId, _ Optional ByVal dSizeUp As Double = 0) As Boolean Dim adblBottomLeft(2) As Double Dim adblTopRight(2) As Double Dim dblHighestY As Double Dim dblHighestX As Double Dim dblLowestY As Double Dim dblLowestX As Double Dim lngUBound As Long ' If the variant is not an array then exit the function If IsArray(objObjectIDs) = False Then Exit Function Dim objEd As Editor = acApp.DocumentManager.MdiActiveDocument.Editor Dim objEnt As Entity Dim objGExt As Extents3d Try Using objTrans As Transaction = objEd.Document.TransactionManager.StartTransaction objEnt = objTrans.GetObject(objObjectIDs(0), OpenMode.ForRead) objGExt = objEnt.GeometricExtents If dSizeUp = 0 Then dSizeUp = MathDistanceBetweenPoints(objGExt.MinPoint, objGExt.MaxPoint) dblLowestX = objGExt.MinPoint.X - dSizeUp dblLowestY = objGExt.MinPoint.Y - dSizeUp dblHighestX = objGExt.MaxPoint.X + dSizeUp dblHighestY = objGExt.MinPoint.Y + dSizeUp For lngCount As Long = objObjectIDs.GetLowerBound(0) + 1 To objObjectIDs.GetUpperBound(0) objEnt = objTrans.GetObject(objObjectIDs(lngCount), OpenMode.ForRead) objGExt = objEnt.GeometricExtents If objGExt.MinPoint.X - dSizeUp < dblLowestX Then dblLowestX = objGExt.MinPoint.X - dSizeUp If objGExt.MinPoint.Y - dSizeUp < dblLowestY Then dblLowestY = objGExt.MinPoint.Y - dSizeUp If objGExt.MaxPoint.X + dSizeUp > dblHighestX Then dblHighestX = objGExt.MaxPoint.X + dSizeUp If objGExt.MaxPoint.Y + dSizeUp > dblHighestY Then dblHighestY = objGExt.MaxPoint.Y + dSizeUp Next 'Zoom to show this entire rectangle adblBottomLeft(0) = dblHighestX adblBottomLeft(1) = dblLowestY adblTopRight(0) = dblLowestX adblTopRight(1) = dblHighestY '--g_objDrawing defined AcadDocument, it is the current open drawing. I have tested it is correct g_objDrawing.Application.ZoomWindow(adblBottomLeft, adblTopRight) objTrans.Commit() Return True End Using Catch ex As Exception Debug.Assert(False) Finally objEnt = Nothing objGExt = Nothing objEd = Nothing End Try
There is no exception, just nothing happen.
Any ideas?
Thanks very much
Solved! Go to Solution.
Solved by Hallex. Go to Solution.
[CommandMethod("ZoomToObjects")]
public void ZoomToObjects()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
using (Transaction trx = doc.Database.TransactionManager.StartTransaction())
{
PromptEntityOptions peo = new PromptEntityOptions("\nSelect Object: ");
PromptEntityResult per = doc.Editor.GetEntity(peo);
Entity ent = (Entity)per.ObjectId.GetObject(OpenMode.ForRead);
int CvId = Convert.ToInt32((Application.GetSystemVariable("CVPORT")));
using (Autodesk.AutoCAD.GraphicsSystem.Manager gm = doc.GraphicsManager)
using (Autodesk.AutoCAD.GraphicsSystem.View vw = gm.GetGsView(CvId, true))
{
Extents3d ext = ent.GeometricExtents;
vw.ZoomExtents(ext.MinPoint, ext.MaxPoint);
gm.SetViewportFromView(CvId, vw, true, true, false);
}
trx.Commit();
}
}
*************************EDITED********************************
I do think this will work in paper space I check & if not I will post a better solution later
Hi,
Here's one inspired by the sample in the AutoCAD .NET Developer's Guide > Control the AutoCAD Environment > Control the Drawing Windows > Zoom and Pan the Current View > Manipulate the Current View
It works whatever the current view.
using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime; namespace ZoomObjectSample { public class Zoom { [CommandMethod("ZO")] public void ZO() { Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; PromptSelectionResult psr = ed.GetSelection(); if (psr.Status != PromptStatus.OK) return; ObjectIdCollection idCol = new ObjectIdCollection(psr.Value.GetObjectIds()); ZoomObjects(idCol); } private void ZoomObjects(ObjectIdCollection idCol) { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; using (Transaction tr = db.TransactionManager.StartTransaction()) using (ViewTableRecord view = ed.GetCurrentView()) { Matrix3d WCS2DCS = Matrix3d.PlaneToWorld(view.ViewDirection); WCS2DCS = Matrix3d.Displacement(view.Target - Point3d.Origin) * WCS2DCS; WCS2DCS = Matrix3d.Rotation(-view.ViewTwist, view.ViewDirection, view.Target) * WCS2DCS; WCS2DCS = WCS2DCS.Inverse(); Entity ent = (Entity)tr.GetObject(idCol[0], OpenMode.ForRead); Extents3d ext = ent.GeometricExtents; ext.TransformBy(WCS2DCS); for (int i = 1; i < idCol.Count; i++) { ent = (Entity)tr.GetObject(idCol[i], OpenMode.ForRead); Extents3d tmp = ent.GeometricExtents; tmp.TransformBy(WCS2DCS); ext.AddExtents(tmp); } double ratio = view.Width / view.Height; double width = ext.MaxPoint.X - ext.MinPoint.X; double height = ext.MaxPoint.Y - ext.MinPoint.Y; if (width > (height * ratio)) height = width / ratio; Point2d center = new Point2d((ext.MaxPoint.X + ext.MinPoint.X) / 2.0, (ext.MaxPoint.Y + ext.MinPoint.Y) / 2.0); view.Height = height; view.Width = width; view.CenterPoint = center; ed.SetCurrentView(view); tr.Commit(); } } } }
Give this a try for multiple objects
using System.Linq;
public static void ZoomObjects() { Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Transaction tr = doc.TransactionManager.StartTransaction(); using (tr) { try { PromptSelectionResult sres = ed.GetSelection(); if (sres.Status != PromptStatus.OK) return; ObjectId[] ids = sres.Value.GetObjectIds(); if (ids.Length == 0) return; int tile = Convert.ToInt32(Application.GetSystemVariable("CVPORT")); var ents = from id in ids where id != null select tr.GetObject(id, OpenMode.ForRead); var minx = (from n in ents where n != null select ((Entity)n).GeometricExtents.MinPoint[0]).Min(); var maxx = (from n in ents where n != null select ((Entity)n).GeometricExtents.MinPoint[0]).Max(); var miny = (from n in ents where n != null select ((Entity)n).GeometricExtents.MinPoint[1]).Min(); var maxy = (from n in ents where n != null select ((Entity)n).GeometricExtents.MinPoint[1]).Max(); Autodesk.AutoCAD.GraphicsSystem.Manager graph = doc.GraphicsManager; using (graph) { Autodesk.AutoCAD.GraphicsSystem.View view = graph.GetGsView(tile, true); using (view) { view.ZoomExtents(new Point3d(minx,miny,0),new Point3d(maxx,maxy,0)); view.Zoom(0.8);//<--optional graph.SetViewportFromView(tile, view, true, true, false); } } tr.Commit(); } catch (Autodesk.AutoCAD.Runtime.Exception ex) { ed.WriteMessage("Error: {0}\nTrace: {1}",ex.Message,ex.StackTrace); } } }
~'J'~
Just in case someone else has the same issue...
I tried Hallex's solution which is marked as the accepted solution. It did not work. Complete failure.
I tried Gilles's. It worked just fine. If it was me i would mark Gilles's solution as the Accepted one.
Yet another fine piece of code by Gilles.
Can't find what you're looking for? Ask the community or share your knowledge.