Skip to content

Commit

Permalink
Update to be significantly more performant, add more useful features (#3
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
Obbay2 authored Mar 28, 2023
1 parent 73a1194 commit fa98b3e
Show file tree
Hide file tree
Showing 15 changed files with 809 additions and 306 deletions.
105 changes: 34 additions & 71 deletions FindAndReplaceCAD/CADUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,40 @@
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;

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<ObjectInformation> items)
public static void WriteCADItems(IEnumerable<ObjectInformation> items)
{
Database db = Application.DocumentManager.MdiActiveDocument.Database;

using (Transaction myT = db.TransactionManager.StartTransaction())
{
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();
}
Expand All @@ -76,13 +59,13 @@ public static IList<ObjectInformation> 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();
}
Expand Down Expand Up @@ -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
/// </summary>
/// <param name="id">Id of the AutoCAD element</param>
public static void MoveViewPort(string id)
public static void MoveViewPort(ObjectId objId)
{
Database db = Application.DocumentManager.MdiActiveDocument.Database;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Expand All @@ -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();
}
}
}
}
73 changes: 20 additions & 53 deletions FindAndReplaceCAD/FindAndReplaceCAD.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down Expand Up @@ -55,75 +57,38 @@
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<ItemGroup>
<Reference Include="AcCoreMgd, Version=24.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\AutoCAD.NET.Core.24.2.0\lib\net47\AcCoreMgd.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="AcCui, Version=24.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\AutoCAD.NET.24.2.0\lib\net47\AcCui.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="AcDbMgd, Version=24.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\AutoCAD.NET.Model.24.2.0\lib\net47\AcDbMgd.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="acdbmgdbrep, Version=24.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\AutoCAD.NET.Model.24.2.0\lib\net47\acdbmgdbrep.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="AcDx, Version=24.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\AutoCAD.NET.24.2.0\lib\net47\AcDx.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="AcMgd, Version=24.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\AutoCAD.NET.24.2.0\lib\net47\AcMgd.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="AcMr, Version=24.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\AutoCAD.NET.24.2.0\lib\net47\AcMr.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="AcSeamless, Version=24.2.0.0, Culture=neutral, processorArchitecture=AMD64">
<HintPath>..\packages\AutoCAD.NET.24.2.0\lib\net47\AcSeamless.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="AcTcMgd, Version=24.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\AutoCAD.NET.24.2.0\lib\net47\AcTcMgd.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="AcWindows, Version=24.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\AutoCAD.NET.24.2.0\lib\net47\AcWindows.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="AdUIMgd, Version=24.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\AutoCAD.NET.24.2.0\lib\net47\AdUIMgd.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="AdUiPalettes, Version=24.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\AutoCAD.NET.24.2.0\lib\net47\AdUiPalettes.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="AdWindows, Version=4.0.0.6, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\AutoCAD.NET.24.2.0\lib\net47\AdWindows.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Data" />
<Reference Include="System.Numerics" />
<Reference Include="System.Xaml" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="CAD.cs" />
<Compile Include="CADUtil.cs" />
<Compile Include="UserControl\FindAndReplace.xaml.cs">
<DependentUpon>FindAndReplace.xaml</DependentUpon>
</Compile>
<Compile Include="ObjectInformation.cs" />
<Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
</Compile>
<Compile Include="TypeUtil.cs" />
<Compile Include="Util\DimensionUtil.cs" />
<Compile Include="Util\DBTextUtil.cs" />
<Compile Include="Util\MLeaderUtil.cs" />
<Compile Include="Util\MTextUtil.cs" />
<Compile Include="Util\ITypeUtil.cs" />
</ItemGroup>
<ItemGroup>
<Page Include="UserControl\FindAndReplace.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand All @@ -133,7 +98,9 @@
<Folder Include="Properties\" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<PackageReference Include="AutoCAD.NET">
<Version>24.2.0</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion FindAndReplaceCAD/FindAndReplaceCAD.csproj.user
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<StartProgram>C:\Program Files\Autodesk\AutoCAD 2022\acad.exe</StartProgram>
<StartArguments>C:\Users\natha\OneDrive\Projects\AutoCAD\FindAndReplaceCAD\config\Drawing1.dwg /b C:\Users\natha\OneDrive\Projects\AutoCAD\FindAndReplaceCAD\config\script.scr /nologo</StartArguments>
<StartArguments>"C:\Users\natha\OneDrive\Desktop\CI-001 - Standard\CI-001.dwg" /b C:\Users\natha\OneDrive\Projects\AutoCAD\FindAndReplaceCAD\config\script.scr /nologo</StartArguments>
<StartAction>Program</StartAction>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
Expand Down
Loading

0 comments on commit fa98b3e

Please sign in to comment.