Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Go to implementation #15

Merged
merged 2 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 3 additions & 19 deletions FindAndReplaceCAD/CADUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public static IList<ObjectInformation> 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
Expand All @@ -72,21 +71,6 @@ public static IList<ObjectInformation> ReadCADItems()
return textFound;
}

/// <summary>
/// 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
/// </summary>
/// <param name="data">Text for a single element</param>
/// <returns>String with all characters escaped for AutoCAD</returns>
//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;
//}

/// <summary>
/// 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
Expand All @@ -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();
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
7 changes: 4 additions & 3 deletions FindAndReplaceCAD/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Margin="10" Grid.Row="0" Orientation="Horizontal">
Expand All @@ -33,7 +33,7 @@
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</StackPanel.Resources>
<Label FontWeight="Bold">Text Types</Label>
<Label FontWeight="Bold" Content="Text Types"/>
<CheckBox IsChecked="{Binding Path=ShowText, ElementName=MainControlWindow}" Content="Show Text"/>
<CheckBox IsChecked="{Binding Path=ShowMText, ElementName=MainControlWindow}" Content="Show Multi-Line Text" />
<CheckBox IsChecked="{Binding Path=ShowMLeader, ElementName=MainControlWindow}" Content="Show Multi-Leader Text" />
Expand Down Expand Up @@ -83,7 +83,8 @@
</StackPanel>
</TabItem>
</TabControl>
<DataGrid Name="DataGrid" ItemsSource="{Binding Path=Texts, ElementName=MainControlWindow}" SelectionMode="Extended" CanUserResizeRows="False" AutoGenerateColumns="False" Grid.Row="3">
<Label Grid.Row="3" FontWeight="Bold" Content="All items below are active in model space" />
<DataGrid Name="DataGrid" ItemsSource="{Binding Path=Texts, ElementName=MainControlWindow}" SelectionMode="Extended" CanUserResizeRows="False" AutoGenerateColumns="False" Grid.Row="4">
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
Expand Down
5 changes: 2 additions & 3 deletions FindAndReplaceCAD/Util/DBTextUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DBText>(obj);
base.MoveViewPort(ed, view, obj, dbText.Position, dbText.Height, dbText.Bounds.Value.MaxPoint.X - dbText.Bounds.Value.MinPoint.X);
base.MoveViewPort(ed, obj);
}
}
}
7 changes: 2 additions & 5 deletions FindAndReplaceCAD/Util/DimensionUtil.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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<Dimension>(obj);
BlockTableRecord dimensionBlock = t.GetObject(dimension.DimBlockId, OpenMode.ForRead) as BlockTableRecord;
Expand All @@ -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();
}
}
Expand Down
39 changes: 32 additions & 7 deletions FindAndReplaceCAD/Util/ITypeUtil.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
}

Expand All @@ -34,5 +42,22 @@ protected T Cast<T>(DBObject obj) where T : DBObject

throw new InvalidOperationException();
}

/// <summary>
/// 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
/// </summary>
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);
}
}
}
15 changes: 2 additions & 13 deletions FindAndReplaceCAD/Util/MLeaderUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MLeader>(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);
}
}
}
5 changes: 2 additions & 3 deletions FindAndReplaceCAD/Util/MTextUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MText>(obj);
base.MoveViewPort(ed, view, obj, mText.Location, mText.ActualHeight, mText.ActualWidth);
base.MoveViewPort(ed, obj);
}
}
}