From fa98b3e0f28227c3aaf286fd45648908de789a49 Mon Sep 17 00:00:00 2001 From: Nathan Wreggit Date: Mon, 27 Mar 2023 21:56:39 -0700 Subject: [PATCH] Update to be significantly more performant, add more useful features (#3) * Update to be significantly more performant, add more useful features * Add new user control * Refactor so thatat all specific type transforms are more maintainable * Fix READ of MLeader with Block contents --- FindAndReplaceCAD/CADUtil.cs | 105 ++++-------- FindAndReplaceCAD/FindAndReplaceCAD.csproj | 73 +++----- .../FindAndReplaceCAD.csproj.user | 2 +- FindAndReplaceCAD/MainWindow.xaml | 139 ++++++++------- FindAndReplaceCAD/MainWindow.xaml.cs | 141 ++++++++++++--- FindAndReplaceCAD/ObjectInformation.cs | 82 +++++++-- FindAndReplaceCAD/TypeUtil.cs | 121 ++++--------- .../UserControl/FindAndReplace.xaml | 48 ++++++ .../UserControl/FindAndReplace.xaml.cs | 26 +++ FindAndReplaceCAD/Util/DBTextUtil.cs | 53 ++++++ FindAndReplaceCAD/Util/DimensionUtil.cs | 65 +++++++ FindAndReplaceCAD/Util/ITypeUtil.cs | 39 +++++ FindAndReplaceCAD/Util/MLeaderUtil.cs | 160 ++++++++++++++++++ FindAndReplaceCAD/Util/MTextUtil.cs | 55 ++++++ FindAndReplaceCAD/packages.config | 6 - 15 files changed, 809 insertions(+), 306 deletions(-) create mode 100644 FindAndReplaceCAD/UserControl/FindAndReplace.xaml create mode 100644 FindAndReplaceCAD/UserControl/FindAndReplace.xaml.cs create mode 100644 FindAndReplaceCAD/Util/DBTextUtil.cs create mode 100644 FindAndReplaceCAD/Util/DimensionUtil.cs create mode 100644 FindAndReplaceCAD/Util/ITypeUtil.cs create mode 100644 FindAndReplaceCAD/Util/MLeaderUtil.cs create mode 100644 FindAndReplaceCAD/Util/MTextUtil.cs delete mode 100644 FindAndReplaceCAD/packages.config diff --git a/FindAndReplaceCAD/CADUtil.cs b/FindAndReplaceCAD/CADUtil.cs index 782012d..4c2bcd0 100644 --- a/FindAndReplaceCAD/CADUtil.cs +++ b/FindAndReplaceCAD/CADUtil.cs @@ -2,7 +2,7 @@ using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; -using Autodesk.AutoCAD.Runtime; +using FindAndReplaceCAD.Util; using System; using System.Collections.Generic; @@ -10,39 +10,7 @@ namespace CADApp { class CADUtil { - public static Handle StringToHandle(string strHandle) - { - Handle handle = new Handle(); - - try - { - Int64 nHandle = Convert.ToInt64(strHandle, 16); - handle = new Handle(nHandle); - } - catch (FormatException) - { - } - return handle; - } - - public static ObjectId HandleToObjectId(Database db, Handle h) - { - ObjectId id = ObjectId.Null; - try - { - id = db.GetObjectId(false, h, 0); - } - catch (Autodesk.AutoCAD.Runtime.Exception x) - { - if (x.ErrorStatus != ErrorStatus.UnknownHandle) - { - throw x; - } - } - return id; - } - - public static void WriteCADItems(IList items) + public static void WriteCADItems(IEnumerable items) { Database db = Application.DocumentManager.MdiActiveDocument.Database; @@ -50,9 +18,24 @@ public static void WriteCADItems(IList items) { foreach (ObjectInformation objInfo in items) { - ObjectId id = HandleToObjectId(db, StringToHandle(objInfo.Id)); - DBObject obj = myT.GetObject(id, OpenMode.ForWrite); - TypeUtil.WriteText(obj, objInfo.NewText); + ObjectId id = objInfo.Id; + if (TypeUtil.IsSupportedType(id)) + { + DBObject obj = myT.GetObject(id, OpenMode.ForWrite); + + TypeUtil.TypeInformation typeInfo = TypeUtil.GetTypeInformation(obj.Id); + ITypeUtil typeUtil = typeInfo.TypeUtil; + + if (typeUtil.CanTextBeEdited(obj)) + { + typeUtil.WriteText(obj, objInfo.NewText, myT); + } + + if (typeUtil.CanMaskBeEdited(obj)) + { + typeUtil.WriteMask(obj, objInfo.NewMask); + } + } } myT.Commit(); } @@ -76,13 +59,13 @@ public static IList ReadCADItems() // iterate through block table to locate objects foreach (ObjectId id in btr) { - // open each object to read - DBObject obj = myT.GetObject(id, OpenMode.ForRead); - if(TypeUtil.IsSupportedType(obj)) + if (TypeUtil.IsSupportedType(id)) { - textFound.Add(new ObjectInformation(obj)); - } - + // open each object to read + DBObject obj = myT.GetObject(id, OpenMode.ForRead); + //string t = TypeUtil.GetText(obj, myT); + textFound.Add(new ObjectInformation(obj, myT)); + } } myT.Commit(); } @@ -110,7 +93,7 @@ public static string ReplaceWithCADEscapeCharacters(string data) /// https://through-the-interface.typepad.com/through_the_interface/2012/12/zooming-panning-and-orbiting-the-current-autocad-view-using-net.html /// /// Id of the AutoCAD element - public static void MoveViewPort(string id) + public static void MoveViewPort(ObjectId objId) { Database db = Application.DocumentManager.MdiActiveDocument.Database; Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; @@ -119,34 +102,14 @@ public static void MoveViewPort(string id) using (Transaction myT = db.TransactionManager.StartTransaction()) { - ObjectId objId = HandleToObjectId(db, StringToHandle(id)); + TypeUtil.TypeInformation t = TypeUtil.GetTypeInformation(objId); + ITypeUtil typeUtil = t.TypeUtil; DBObject obj = myT.GetObject(objId, OpenMode.ForRead); + typeUtil.MoveViewPort(ed, view, myT, obj); - MText mtext = null; - if (obj is MLeader) - { - MLeader mLeader = obj as MLeader; - mtext = mLeader.MText; - } - - if (obj is MText) - { - MText mText = obj as MText; - mtext = mText; - } - - if (mtext != null) - { - ed.SetImpliedSelection(new[] { objId }); - view.CenterPoint = new Point2d(mtext.Location.X, mtext.Location.Y); - view.Height = mtext.ActualHeight * 3; - view.Width = mtext.ActualWidth * 3; - ed.SetCurrentView(view); - ed.Regen(); // Update gizmos to be accurate after movement - } - myT.Commit(); - } - } - } + myT.Commit(); + } + } + } } diff --git a/FindAndReplaceCAD/FindAndReplaceCAD.csproj b/FindAndReplaceCAD/FindAndReplaceCAD.csproj index e304f8a..64fdff9 100644 --- a/FindAndReplaceCAD/FindAndReplaceCAD.csproj +++ b/FindAndReplaceCAD/FindAndReplaceCAD.csproj @@ -13,6 +13,8 @@ true true + + AnyCPU @@ -55,75 +57,38 @@ prompt - - ..\packages\AutoCAD.NET.Core.24.2.0\lib\net47\AcCoreMgd.dll - False - - - ..\packages\AutoCAD.NET.24.2.0\lib\net47\AcCui.dll - False - - - ..\packages\AutoCAD.NET.Model.24.2.0\lib\net47\AcDbMgd.dll - False - - - ..\packages\AutoCAD.NET.Model.24.2.0\lib\net47\acdbmgdbrep.dll - False - - - ..\packages\AutoCAD.NET.24.2.0\lib\net47\AcDx.dll - False - - - ..\packages\AutoCAD.NET.24.2.0\lib\net47\AcMgd.dll - False - - - ..\packages\AutoCAD.NET.24.2.0\lib\net47\AcMr.dll - False - - - ..\packages\AutoCAD.NET.24.2.0\lib\net47\AcSeamless.dll - False - - - ..\packages\AutoCAD.NET.24.2.0\lib\net47\AcTcMgd.dll - False - - - ..\packages\AutoCAD.NET.24.2.0\lib\net47\AcWindows.dll - False - - - ..\packages\AutoCAD.NET.24.2.0\lib\net47\AdUIMgd.dll - False - - - ..\packages\AutoCAD.NET.24.2.0\lib\net47\AdUiPalettes.dll - False - - - ..\packages\AutoCAD.NET.24.2.0\lib\net47\AdWindows.dll - False - + + + + + FindAndReplace.xaml + MainWindow.xaml + + + + + + + Designer + MSBuild:Compile + MSBuild:Compile Designer @@ -133,7 +98,9 @@ - + + 24.2.0 + 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/MainWindow.xaml b/FindAndReplaceCAD/MainWindow.xaml index 334f3bd..5351d49 100644 --- a/FindAndReplaceCAD/MainWindow.xaml +++ b/FindAndReplaceCAD/MainWindow.xaml @@ -3,86 +3,111 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:local="clr-namespace:CADApp" mc:Ignorable="d" Title="Find and Replace" Height="800" - Width="918"> + Width="918" + Name="MainControlWindow"> + - - - - - + + + + + + + + - - - - - - - - - -