From 09cbf4081056e2f2012eeaf0fd1ce2f7c7b404f5 Mon Sep 17 00:00:00 2001 From: Nathan Wreggit Date: Fri, 31 Mar 2023 09:40:23 -0700 Subject: [PATCH 1/2] Fix implementation of GO TO --- FindAndReplaceCAD/CADUtil.cs | 22 ++--------- .../FindAndReplaceCAD.csproj.user | 2 +- FindAndReplaceCAD/Util/DBTextUtil.cs | 5 +-- FindAndReplaceCAD/Util/DimensionUtil.cs | 7 +--- FindAndReplaceCAD/Util/ITypeUtil.cs | 39 +++++++++++++++---- FindAndReplaceCAD/Util/MLeaderUtil.cs | 15 +------ FindAndReplaceCAD/Util/MTextUtil.cs | 5 +-- 7 files changed, 44 insertions(+), 51 deletions(-) diff --git a/FindAndReplaceCAD/CADUtil.cs b/FindAndReplaceCAD/CADUtil.cs index 7929d5f..4d233d6 100644 --- a/FindAndReplaceCAD/CADUtil.cs +++ b/FindAndReplaceCAD/CADUtil.cs @@ -51,8 +51,7 @@ public static IList ReadCADItems() using (Transaction myT = tm.StartTransaction()) { - - BlockTable bt = (BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForRead); + BlockTable bt = (BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForRead); BlockTableRecord btr = (BlockTableRecord)tm.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead); // iterate through block table to locate objects @@ -72,21 +71,6 @@ public static IList ReadCADItems() return textFound; } - /// - /// Replaces all standard text with the escaped version needed to place back into text contents for AutoCAD - /// https://knowledge.autodesk.com/support/autocad/learn-explore/caas/CloudHelp/cloudhelp/2020/ENU/AutoCAD-Core/files/GUID-7D8BB40F-5C4E-4AE5-BD75-9ED7112E5967-htm.html - /// - /// Text for a single element - /// String with all characters escaped for AutoCAD - //public static string ReplaceWithCADEscapeCharacters(string data) - //{ - // data = data.Replace(@"\", @"\\"); // Must come first - // data = data.Replace("\r\n", @"\P"); - // data = data.Replace(@"{", @"\{"); - // data = data.Replace(@"}", @"\}"); - // return data; - //} - /// /// Moves and scales the viewport to center on the CAD element specified by its object ID /// https://through-the-interface.typepad.com/through_the_interface/2012/12/zooming-panning-and-orbiting-the-current-autocad-view-using-net.html @@ -103,8 +87,8 @@ public static void MoveViewPort(ObjectId objId) { TypeUtil.TypeInformation t = TypeUtil.GetTypeInformation(objId); ITypeUtil typeUtil = t.TypeUtil; - DBObject obj = myT.GetObject(objId, OpenMode.ForRead); - typeUtil.MoveViewPort(ed, view, myT, obj); + Entity obj = (Entity) myT.GetObject(objId, OpenMode.ForRead); + typeUtil.MoveViewPort(ed, myT, obj); obj.Dispose(); myT.Commit(); diff --git a/FindAndReplaceCAD/FindAndReplaceCAD.csproj.user b/FindAndReplaceCAD/FindAndReplaceCAD.csproj.user index a1532db..581e742 100644 --- a/FindAndReplaceCAD/FindAndReplaceCAD.csproj.user +++ b/FindAndReplaceCAD/FindAndReplaceCAD.csproj.user @@ -18,7 +18,7 @@ C:\Program Files\Autodesk\AutoCAD 2022\acad.exe - C:\Users\natha\OneDrive\Projects\AutoCAD\FindAndReplaceCAD\config\Drawing1.dwg /b C:\Users\natha\OneDrive\Projects\AutoCAD\FindAndReplaceCAD\config\script.scr /nologo + "C:\Users\natha\OneDrive\Desktop\CI-001 - Standard\CI-001.dwg" /b C:\Users\natha\OneDrive\Projects\AutoCAD\FindAndReplaceCAD\config\script.scr /nologo Program diff --git a/FindAndReplaceCAD/Util/DBTextUtil.cs b/FindAndReplaceCAD/Util/DBTextUtil.cs index 51dde10..6fdd22c 100644 --- a/FindAndReplaceCAD/Util/DBTextUtil.cs +++ b/FindAndReplaceCAD/Util/DBTextUtil.cs @@ -44,10 +44,9 @@ public override string GetInternalContentType(DBObject obj) return TypeUtil.DBTEXT; } - public override void MoveViewPort(Editor ed, ViewTableRecord view, Transaction t, DBObject obj) + public override void MoveViewPort(Editor ed, Transaction t, Entity obj) { - DBText dbText = Cast(obj); - base.MoveViewPort(ed, view, obj, dbText.Position, dbText.Height, dbText.Bounds.Value.MaxPoint.X - dbText.Bounds.Value.MinPoint.X); + base.MoveViewPort(ed, obj); } } } diff --git a/FindAndReplaceCAD/Util/DimensionUtil.cs b/FindAndReplaceCAD/Util/DimensionUtil.cs index b58dc1e..f10afcb 100644 --- a/FindAndReplaceCAD/Util/DimensionUtil.cs +++ b/FindAndReplaceCAD/Util/DimensionUtil.cs @@ -1,9 +1,6 @@ using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using CADApp; -using System; -using System.Security.Cryptography; -using System.Threading.Tasks; namespace FindAndReplaceCAD.Util { @@ -48,7 +45,7 @@ public override string GetInternalContentType(DBObject obj) return TypeUtil.MTEXT; } - public override void MoveViewPort(Editor ed, ViewTableRecord view, Transaction t, DBObject obj) + public override void MoveViewPort(Editor ed, Transaction t, Entity obj) { Dimension dimension = Cast(obj); BlockTableRecord dimensionBlock = t.GetObject(dimension.DimBlockId, OpenMode.ForRead) as BlockTableRecord; @@ -57,7 +54,7 @@ public override void MoveViewPort(Editor ed, ViewTableRecord view, Transaction t if (TypeUtil.GetTypeInformation(subId).Type == typeof(MText)) { MText mText = t.GetObject(subId, OpenMode.ForRead) as MText; - base.MoveViewPort(ed, view, obj, mText.Location, mText.ActualHeight, mText.ActualWidth); + base.MoveViewPort(ed, obj); mText.Dispose(); } } diff --git a/FindAndReplaceCAD/Util/ITypeUtil.cs b/FindAndReplaceCAD/Util/ITypeUtil.cs index 34cdc8a..987d669 100644 --- a/FindAndReplaceCAD/Util/ITypeUtil.cs +++ b/FindAndReplaceCAD/Util/ITypeUtil.cs @@ -1,4 +1,5 @@ -using Autodesk.AutoCAD.DatabaseServices; +using Autodesk.AutoCAD.ApplicationServices; +using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; using System; @@ -14,14 +15,21 @@ internal abstract class ITypeUtil public abstract void WriteMask(DBObject entity, bool newMask); public abstract void WriteText(DBObject entity, string newText, Transaction t); public abstract string GetInternalContentType(DBObject entity); - public abstract void MoveViewPort(Editor ed, ViewTableRecord view, Transaction t, DBObject obj); - protected void MoveViewPort(Editor ed, ViewTableRecord view, DBObject entity, Point3d position, double height, double width) + public abstract void MoveViewPort(Editor ed, Transaction t, Entity entity); + protected void MoveViewPort(Editor ed, Entity entity) { + if (LayoutManager.Current.CurrentLayout != "Model") + { + var doc = Application.DocumentManager.MdiActiveDocument; + using (doc.LockDocument()) + { + LayoutManager.Current.CurrentLayout = "Model"; + } + } + Extents3d ext = entity.GeometricExtents; + ext.TransformBy(ed.CurrentUserCoordinateSystem.Inverse()); ed.SetImpliedSelection(new[] { entity.Id }); - view.CenterPoint = new Point2d(position.X, position.Y); - view.Height = height * 3; - view.Width = width * 3; - ed.SetCurrentView(view); + ZoomWin(ed, ext.MinPoint, ext.MaxPoint); ed.Regen(); // Update gizmos to be accurate after movement } @@ -34,5 +42,22 @@ protected T Cast(DBObject obj) where T : DBObject throw new InvalidOperationException(); } + + /// + /// Moves and scales the viewport to center on the CAD element + /// https://through-the-interface.typepad.com/through_the_interface/2008/06/zooming-to-a-wi.html + /// http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer's%20Guide/index.html?url=WS1a9193826455f5ff2566ffd511ff6f8c7ca-35da.htm,topicNumber=d0e44236 + /// + private static void ZoomWin(Editor ed, Point3d min, Point3d max) + { + Point2d min2d = new Point2d(min.X, min.Y); + Point2d max2d = new Point2d(max.X, max.Y); + + ViewTableRecord view = new ViewTableRecord(); + view.CenterPoint = min2d + ((max2d - min2d) / 2.0); + view.Height = (max2d.Y - min2d.Y) * 1.5; + view.Width = (max2d.X - min2d.X) * 1.5; + ed.SetCurrentView(view); + } } } \ No newline at end of file diff --git a/FindAndReplaceCAD/Util/MLeaderUtil.cs b/FindAndReplaceCAD/Util/MLeaderUtil.cs index 3052789..480178e 100644 --- a/FindAndReplaceCAD/Util/MLeaderUtil.cs +++ b/FindAndReplaceCAD/Util/MLeaderUtil.cs @@ -142,20 +142,9 @@ public override string GetInternalContentType(DBObject obj) return ""; } - public override void MoveViewPort(Editor ed, ViewTableRecord view, Transaction t, DBObject obj) + public override void MoveViewPort(Editor ed, Transaction t, Entity obj) { - MLeader mLeader = Cast(obj); - - if (mLeader.ContentType == ContentType.BlockContent) - { - throw new InvalidOperationException(); - } - else if (mLeader.ContentType == ContentType.MTextContent) - { - MText mText = mLeader.MText; - base.MoveViewPort(ed, view, obj, mText.Location, mText.ActualHeight, mText.ActualWidth); - mText.Dispose(); - } + base.MoveViewPort(ed, obj); } } } diff --git a/FindAndReplaceCAD/Util/MTextUtil.cs b/FindAndReplaceCAD/Util/MTextUtil.cs index 67c8673..4f08af0 100644 --- a/FindAndReplaceCAD/Util/MTextUtil.cs +++ b/FindAndReplaceCAD/Util/MTextUtil.cs @@ -46,10 +46,9 @@ public override string GetInternalContentType(DBObject obj) return TypeUtil.MTEXT; } - public override void MoveViewPort(Editor ed, ViewTableRecord view, Transaction t, DBObject obj) + public override void MoveViewPort(Editor ed, Transaction t, Entity obj) { - MText mText = Cast(obj); - base.MoveViewPort(ed, view, obj, mText.Location, mText.ActualHeight, mText.ActualWidth); + base.MoveViewPort(ed, obj); } } } From 36014c63060c78f10d90ef492cebf46d334dac27 Mon Sep 17 00:00:00 2001 From: Nathan Wreggit Date: Fri, 31 Mar 2023 09:44:18 -0700 Subject: [PATCH 2/2] Add label specify where the objects are --- FindAndReplaceCAD/MainWindow.xaml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/FindAndReplaceCAD/MainWindow.xaml b/FindAndReplaceCAD/MainWindow.xaml index 090685e..9939663 100644 --- a/FindAndReplaceCAD/MainWindow.xaml +++ b/FindAndReplaceCAD/MainWindow.xaml @@ -22,8 +22,8 @@ - + @@ -33,7 +33,7 @@ - + - +