Skip to content

Commit b3673bf

Browse files
authored
Fix Go to implementation (#15)
* Fix implementation of GO TO * Add label specify where the objects are
1 parent 4f25fb2 commit b3673bf

8 files changed

+48
-54
lines changed

FindAndReplaceCAD/CADUtil.cs

+3-19
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ public static IList<ObjectInformation> ReadCADItems()
5151

5252
using (Transaction myT = tm.StartTransaction())
5353
{
54-
55-
BlockTable bt = (BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForRead);
54+
BlockTable bt = (BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForRead);
5655
BlockTableRecord btr = (BlockTableRecord)tm.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
5756

5857
// iterate through block table to locate objects
@@ -72,21 +71,6 @@ public static IList<ObjectInformation> ReadCADItems()
7271
return textFound;
7372
}
7473

75-
/// <summary>
76-
/// Replaces all standard text with the escaped version needed to place back into text contents for AutoCAD
77-
/// https://knowledge.autodesk.com/support/autocad/learn-explore/caas/CloudHelp/cloudhelp/2020/ENU/AutoCAD-Core/files/GUID-7D8BB40F-5C4E-4AE5-BD75-9ED7112E5967-htm.html
78-
/// </summary>
79-
/// <param name="data">Text for a single element</param>
80-
/// <returns>String with all characters escaped for AutoCAD</returns>
81-
//public static string ReplaceWithCADEscapeCharacters(string data)
82-
//{
83-
// data = data.Replace(@"\", @"\\"); // Must come first
84-
// data = data.Replace("\r\n", @"\P");
85-
// data = data.Replace(@"{", @"\{");
86-
// data = data.Replace(@"}", @"\}");
87-
// return data;
88-
//}
89-
9074
/// <summary>
9175
/// Moves and scales the viewport to center on the CAD element specified by its object ID
9276
/// 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)
10387
{
10488
TypeUtil.TypeInformation t = TypeUtil.GetTypeInformation(objId);
10589
ITypeUtil typeUtil = t.TypeUtil;
106-
DBObject obj = myT.GetObject(objId, OpenMode.ForRead);
107-
typeUtil.MoveViewPort(ed, view, myT, obj);
90+
Entity obj = (Entity) myT.GetObject(objId, OpenMode.ForRead);
91+
typeUtil.MoveViewPort(ed, myT, obj);
10892
obj.Dispose();
10993

11094
myT.Commit();

FindAndReplaceCAD/FindAndReplaceCAD.csproj.user

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</PropertyGroup>
1919
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
2020
<StartProgram>C:\Program Files\Autodesk\AutoCAD 2022\acad.exe</StartProgram>
21-
<StartArguments>C:\Users\natha\OneDrive\Projects\AutoCAD\FindAndReplaceCAD\config\Drawing1.dwg /b C:\Users\natha\OneDrive\Projects\AutoCAD\FindAndReplaceCAD\config\script.scr /nologo</StartArguments>
21+
<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>
2222
<StartAction>Program</StartAction>
2323
</PropertyGroup>
2424
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">

FindAndReplaceCAD/MainWindow.xaml

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
<RowDefinition Height="Auto" />
2323
<RowDefinition Height="Auto" />
2424
<RowDefinition Height="Auto" />
25-
<RowDefinition Height="*" />
2625
<RowDefinition Height="Auto" />
26+
<RowDefinition Height="*" />
2727
<RowDefinition Height="Auto" />
2828
</Grid.RowDefinitions>
2929
<StackPanel Margin="10" Grid.Row="0" Orientation="Horizontal">
@@ -33,7 +33,7 @@
3333
<Setter Property="VerticalAlignment" Value="Center" />
3434
</Style>
3535
</StackPanel.Resources>
36-
<Label FontWeight="Bold">Text Types</Label>
36+
<Label FontWeight="Bold" Content="Text Types"/>
3737
<CheckBox IsChecked="{Binding Path=ShowText, ElementName=MainControlWindow}" Content="Show Text"/>
3838
<CheckBox IsChecked="{Binding Path=ShowMText, ElementName=MainControlWindow}" Content="Show Multi-Line Text" />
3939
<CheckBox IsChecked="{Binding Path=ShowMLeader, ElementName=MainControlWindow}" Content="Show Multi-Leader Text" />
@@ -83,7 +83,8 @@
8383
</StackPanel>
8484
</TabItem>
8585
</TabControl>
86-
<DataGrid Name="DataGrid" ItemsSource="{Binding Path=Texts, ElementName=MainControlWindow}" SelectionMode="Extended" CanUserResizeRows="False" AutoGenerateColumns="False" Grid.Row="3">
86+
<Label Grid.Row="3" FontWeight="Bold" Content="All items below are active in model space" />
87+
<DataGrid Name="DataGrid" ItemsSource="{Binding Path=Texts, ElementName=MainControlWindow}" SelectionMode="Extended" CanUserResizeRows="False" AutoGenerateColumns="False" Grid.Row="4">
8788
<DataGrid.Resources>
8889
<Style TargetType="DataGridRow">
8990
<Setter Property="IsSelected" Value="{Binding IsSelected}" />

FindAndReplaceCAD/Util/DBTextUtil.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ public override string GetInternalContentType(DBObject obj)
4444
return TypeUtil.DBTEXT;
4545
}
4646

47-
public override void MoveViewPort(Editor ed, ViewTableRecord view, Transaction t, DBObject obj)
47+
public override void MoveViewPort(Editor ed, Transaction t, Entity obj)
4848
{
49-
DBText dbText = Cast<DBText>(obj);
50-
base.MoveViewPort(ed, view, obj, dbText.Position, dbText.Height, dbText.Bounds.Value.MaxPoint.X - dbText.Bounds.Value.MinPoint.X);
49+
base.MoveViewPort(ed, obj);
5150
}
5251
}
5352
}

FindAndReplaceCAD/Util/DimensionUtil.cs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using Autodesk.AutoCAD.DatabaseServices;
22
using Autodesk.AutoCAD.EditorInput;
33
using CADApp;
4-
using System;
5-
using System.Security.Cryptography;
6-
using System.Threading.Tasks;
74

85
namespace FindAndReplaceCAD.Util
96
{
@@ -48,7 +45,7 @@ public override string GetInternalContentType(DBObject obj)
4845
return TypeUtil.MTEXT;
4946
}
5047

51-
public override void MoveViewPort(Editor ed, ViewTableRecord view, Transaction t, DBObject obj)
48+
public override void MoveViewPort(Editor ed, Transaction t, Entity obj)
5249
{
5350
Dimension dimension = Cast<Dimension>(obj);
5451
BlockTableRecord dimensionBlock = t.GetObject(dimension.DimBlockId, OpenMode.ForRead) as BlockTableRecord;
@@ -57,7 +54,7 @@ public override void MoveViewPort(Editor ed, ViewTableRecord view, Transaction t
5754
if (TypeUtil.GetTypeInformation(subId).Type == typeof(MText))
5855
{
5956
MText mText = t.GetObject(subId, OpenMode.ForRead) as MText;
60-
base.MoveViewPort(ed, view, obj, mText.Location, mText.ActualHeight, mText.ActualWidth);
57+
base.MoveViewPort(ed, obj);
6158
mText.Dispose();
6259
}
6360
}

FindAndReplaceCAD/Util/ITypeUtil.cs

+32-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Autodesk.AutoCAD.DatabaseServices;
1+
using Autodesk.AutoCAD.ApplicationServices;
2+
using Autodesk.AutoCAD.DatabaseServices;
23
using Autodesk.AutoCAD.EditorInput;
34
using Autodesk.AutoCAD.Geometry;
45
using System;
@@ -14,14 +15,21 @@ internal abstract class ITypeUtil
1415
public abstract void WriteMask(DBObject entity, bool newMask);
1516
public abstract void WriteText(DBObject entity, string newText, Transaction t);
1617
public abstract string GetInternalContentType(DBObject entity);
17-
public abstract void MoveViewPort(Editor ed, ViewTableRecord view, Transaction t, DBObject obj);
18-
protected void MoveViewPort(Editor ed, ViewTableRecord view, DBObject entity, Point3d position, double height, double width)
18+
public abstract void MoveViewPort(Editor ed, Transaction t, Entity entity);
19+
protected void MoveViewPort(Editor ed, Entity entity)
1920
{
21+
if (LayoutManager.Current.CurrentLayout != "Model")
22+
{
23+
var doc = Application.DocumentManager.MdiActiveDocument;
24+
using (doc.LockDocument())
25+
{
26+
LayoutManager.Current.CurrentLayout = "Model";
27+
}
28+
}
29+
Extents3d ext = entity.GeometricExtents;
30+
ext.TransformBy(ed.CurrentUserCoordinateSystem.Inverse());
2031
ed.SetImpliedSelection(new[] { entity.Id });
21-
view.CenterPoint = new Point2d(position.X, position.Y);
22-
view.Height = height * 3;
23-
view.Width = width * 3;
24-
ed.SetCurrentView(view);
32+
ZoomWin(ed, ext.MinPoint, ext.MaxPoint);
2533
ed.Regen(); // Update gizmos to be accurate after movement
2634
}
2735

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

3543
throw new InvalidOperationException();
3644
}
45+
46+
/// <summary>
47+
/// Moves and scales the viewport to center on the CAD element
48+
/// https://through-the-interface.typepad.com/through_the_interface/2008/06/zooming-to-a-wi.html
49+
/// http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer's%20Guide/index.html?url=WS1a9193826455f5ff2566ffd511ff6f8c7ca-35da.htm,topicNumber=d0e44236
50+
/// </summary>
51+
private static void ZoomWin(Editor ed, Point3d min, Point3d max)
52+
{
53+
Point2d min2d = new Point2d(min.X, min.Y);
54+
Point2d max2d = new Point2d(max.X, max.Y);
55+
56+
ViewTableRecord view = new ViewTableRecord();
57+
view.CenterPoint = min2d + ((max2d - min2d) / 2.0);
58+
view.Height = (max2d.Y - min2d.Y) * 1.5;
59+
view.Width = (max2d.X - min2d.X) * 1.5;
60+
ed.SetCurrentView(view);
61+
}
3762
}
3863
}

FindAndReplaceCAD/Util/MLeaderUtil.cs

+2-13
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,9 @@ public override string GetInternalContentType(DBObject obj)
142142
return "";
143143
}
144144

145-
public override void MoveViewPort(Editor ed, ViewTableRecord view, Transaction t, DBObject obj)
145+
public override void MoveViewPort(Editor ed, Transaction t, Entity obj)
146146
{
147-
MLeader mLeader = Cast<MLeader>(obj);
148-
149-
if (mLeader.ContentType == ContentType.BlockContent)
150-
{
151-
throw new InvalidOperationException();
152-
}
153-
else if (mLeader.ContentType == ContentType.MTextContent)
154-
{
155-
MText mText = mLeader.MText;
156-
base.MoveViewPort(ed, view, obj, mText.Location, mText.ActualHeight, mText.ActualWidth);
157-
mText.Dispose();
158-
}
147+
base.MoveViewPort(ed, obj);
159148
}
160149
}
161150
}

FindAndReplaceCAD/Util/MTextUtil.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ public override string GetInternalContentType(DBObject obj)
4646
return TypeUtil.MTEXT;
4747
}
4848

49-
public override void MoveViewPort(Editor ed, ViewTableRecord view, Transaction t, DBObject obj)
49+
public override void MoveViewPort(Editor ed, Transaction t, Entity obj)
5050
{
51-
MText mText = Cast<MText>(obj);
52-
base.MoveViewPort(ed, view, obj, mText.Location, mText.ActualHeight, mText.ActualWidth);
51+
base.MoveViewPort(ed, obj);
5352
}
5453
}
5554
}

0 commit comments

Comments
 (0)