Skip to content

Commit

Permalink
Fix some memory leaks, fix attribute text margins (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Obbay2 authored Mar 28, 2023
1 parent fa98b3e commit cc8ce1a
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 73 deletions.
9 changes: 4 additions & 5 deletions FindAndReplaceCAD/CADUtil.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using Autodesk.AutoCAD.ApplicationServices.Core;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using FindAndReplaceCAD.Util;
using System;
using System.Collections.Generic;

namespace CADApp
Expand Down Expand Up @@ -35,6 +33,7 @@ public static void WriteCADItems(IEnumerable<ObjectInformation> items)
{
typeUtil.WriteMask(obj, objInfo.NewMask);
}
obj.Dispose();
}
}
myT.Commit();
Expand Down Expand Up @@ -63,8 +62,8 @@ public static IList<ObjectInformation> ReadCADItems()
{
// open each object to read
DBObject obj = myT.GetObject(id, OpenMode.ForRead);
//string t = TypeUtil.GetText(obj, myT);
textFound.Add(new ObjectInformation(obj, myT));
textFound.Add(new ObjectInformation(obj, myT));
obj.Dispose();
}
}
myT.Commit();
Expand Down Expand Up @@ -105,8 +104,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);
obj.Dispose();

myT.Commit();
}
Expand Down
56 changes: 27 additions & 29 deletions FindAndReplaceCAD/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<local:FindAndReplace Grid.Row="0" Visibility="Collapsed" />
<Separator Grid.Row="1" Visibility="Collapsed"/>
<StackPanel Margin="10" Grid.Row="2" Orientation="Horizontal">
<StackPanel Margin="10" Grid.Row="0" Orientation="Horizontal">
<StackPanel.Resources>
<Style TargetType="{x:Type CheckBox}">
<Setter Property="Margin" Value="10,2,0,0" />
Expand All @@ -43,10 +39,30 @@
<CheckBox IsChecked="{Binding Path=ShowMLeader, ElementName=MainControlWindow}" Content="Show Multi-Leader Text" />
<CheckBox IsChecked="{Binding Path=ShowDimension, ElementName=MainControlWindow}" Content="Show Dimensions" />
</StackPanel>
<StackPanel Grid.Row="3" Orientation="Horizontal" Margin="0,10,0,10">
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="0,10,0,10">
<Button Content="Refresh" Click="refreshButton_Click"/>
</StackPanel>
<DataGrid RowDetailsVisibilityMode="Collapsed" Name="DataGrid" ItemsSource="{Binding Path=Texts, ElementName=MainControlWindow}" CurrentCellChanged="DataGrid_CurrentCellChanged" SelectedCellsChanged="DataGrid_SelectedCellsChanged" SelectionMode="Extended" CanUserResizeRows="False" AutoGenerateColumns="False" Grid.Row="4">
<TabControl Grid.Row="2" Margin="10,10,10,10">
<TabItem Header="Masking">
<StackPanel Orientation="Horizontal" Margin="0,5,0,5">
<Button Content="Mask Selected" Name="btnMask" Click="btnMask_Click"/>
<Button Content="Unmask Selected" Click="btnUnmask_Click"/>
</StackPanel>
</TabItem>
<TabItem Header="Edit Text">
<StackPanel>
<local:FindAndReplace />
<Separator Height="2"/>
<StackPanel Orientation="Horizontal" Margin="0,5,0,5">
<Button Content="Strip New Lines" Click="btnStrip_Click"/>
</StackPanel>
</StackPanel>
</TabItem>
<TabItem Header="Dimension Override">

</TabItem>
</TabControl>
<DataGrid Name="DataGrid" ItemsSource="{Binding Path=Texts, ElementName=MainControlWindow}" SelectionMode="Extended" CanUserResizeRows="False" AutoGenerateColumns="False" Grid.Row="3">
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
Expand All @@ -59,22 +75,11 @@
<Setter Property="Background" Value="{Binding HasMaskChanged}" />
</Style>
</DataGrid.Resources>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel Margin="20,10">
<TextBlock Text="Here are the details for the selected mountain:"/>
<TextBlock Text="{Binding FriendlyType}"/>
<TextBlock FontSize="13" Text="{Binding ContentType}"/>
<TextBlock FontSize="13" Text="{Binding OriginalText}" TextWrapping="WrapWithOverflow" />
<TextBlock FontSize="13" Text="{Binding Id}" />
</StackPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
<DataGrid.Columns>
<DataGridTemplateColumn Width="100" CanUserResize="False" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Details" FontSize="12" Click="btnDetails_Click" CommandParameter="{Binding Path=ID}"/>
<Button Content="Details" FontSize="12" Click="btnDetails_Click"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Expand All @@ -91,22 +96,15 @@
<DataGridTemplateColumn Width="1*" Header="New Text">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding NewText}" Background="{Binding HasTextChanged}" IsEnabled="{Binding CanEditText}"/>
<TextBox Text="{Binding NewText, UpdateSourceTrigger=PropertyChanged}" Background="{Binding HasTextChanged}" IsEnabled="{Binding CanEditText}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridCheckBoxColumn Header="Masked" Width="60" CanUserResize="False" ElementStyle="{StaticResource maskStyle}" EditingElementStyle="{StaticResource maskStyle}" CellStyle="{StaticResource newMaskCellStyle}" />
</DataGrid.Columns>
</DataGrid>
<UniformGrid Grid.Row="5" Visibility="{Binding Path=HasAnySelection, ElementName=MainControlWindow}" Rows="1" Columns="2" Margin="0,5,0,5">
<Button Content="Mask Selected" Name="btnMask" Click="btnMask_Click"/>
<Button Content="Unmask Selected" Click="btnUnmask_Click"/>
</UniformGrid>
<StackPanel Grid.Row="6" Orientation="Horizontal" Margin="0,5,0,5">
<Button Content="Strip New Lines" Click="btnStrip_Click"/>
</StackPanel>
<DockPanel Grid.Row="7" HorizontalAlignment="Stretch" Margin="0,5,0,5">
<Label Width="300" Content="{Binding Path=EditedAttributes, ElementName=MainControlWindow}" Margin="0,0,10,0" />
<DockPanel Grid.Row="5" HorizontalAlignment="Stretch" Margin="10,5,0,5">
<Label Width="300" VerticalAlignment="Center" Content="{Binding Path=EditedAttributes, ElementName=MainControlWindow}" />
<Button Content="Execute Changes" Click="btnExecute_Click"/>
</DockPanel>
</Grid>
Expand Down
32 changes: 4 additions & 28 deletions FindAndReplaceCAD/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -32,23 +30,6 @@ public partial class MainWindow : Window, INotifyPropertyChanged
private bool _showDimension = true;
public bool ShowDimension { get { return _showDimension; } set { _showDimension = value; Texts?.Refresh(); } }

private string _visibility = System.Windows.Visibility.Collapsed.ToString();
public string HasAnySelection
{
get
{
return _visibility;
}
set
{
if (value != _visibility)
{
_visibility = value;
NotifyPropertyChanged(nameof(HasAnySelection));
}
}
}

private string _editedAttributes = "Edited 0 attributes on 0 records";
public string EditedAttributes
{
Expand Down Expand Up @@ -159,22 +140,17 @@ private bool FindFilter(object item)
return shouldShow;
}

private void DataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
HasAnySelection = DataGrid.SelectedItems.Count > 1 ?
System.Windows.Visibility.Visible.ToString() : System.Windows.Visibility.Collapsed.ToString();
}

private void refreshData()
{
Test.Clear();
foreach (var item in CADUtil.ReadCADItems())
{
item.EditableAttributeChanged += DataGrid_AttributesChanged;
Test.Add(item);
}
}

private void DataGrid_CurrentCellChanged(object sender, System.EventArgs e)
private void DataGrid_AttributesChanged(object sender, PropertyChangedEventArgs e)
{
int recordsChanged = Test.Count(item => item.AttributesChanged() > 0);
int attributesChanged = Test.Sum(item => item.AttributesChanged());
Expand All @@ -183,8 +159,8 @@ private void DataGrid_CurrentCellChanged(object sender, System.EventArgs e)

private void btnDetails_Click(object sender, RoutedEventArgs e)
{
DataGridRow dgr = DataGridRow.GetRowContainingElement((Button)sender);
DataGrid.SetDetailsVisibilityForItem(dgr, System.Windows.Visibility.Visible);
//DataGridRow dgr = DataGridRow.GetRowContainingElement((Button)sender);
//DataGrid.SetDetailsVisibilityForItem(dgr, System.Windows.Visibility.Visible);
}
}
}
9 changes: 9 additions & 0 deletions FindAndReplaceCAD/ObjectInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public string NewText
{
_newText = value;
NotifyPropertyChanged(nameof(NewText));
NotifyEditableAttributeChanged(nameof(NewText));
NotifyPropertyChanged(nameof(HasTextChanged));
}
}
Expand All @@ -53,6 +54,7 @@ public bool NewMask {
{
_newMask = value;
NotifyPropertyChanged(nameof(NewMask));
NotifyEditableAttributeChanged(nameof(NewMask));
NotifyPropertyChanged(nameof(HasMaskChanged));
}
}
Expand Down Expand Up @@ -80,6 +82,13 @@ public int AttributesChanged()
return (OriginalText != NewText ? 1 : 0) + (OriginalMask != NewMask ? 1 : 0);
}

public event PropertyChangedEventHandler EditableAttributeChanged;

protected void NotifyEditableAttributeChanged(string propertyName)
{
EditableAttributeChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

public event PropertyChangedEventHandler PropertyChanged;

protected void NotifyPropertyChanged(string propertyName)
Expand Down
7 changes: 4 additions & 3 deletions FindAndReplaceCAD/Util/DimensionUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ public override string GetInternalContentType(DBObject obj)
public override void MoveViewPort(Editor ed, ViewTableRecord view, Transaction t, DBObject obj)
{
Dimension dimension = Cast<Dimension>(obj);
var dimensionBlock = t.GetObject(dimension.DimBlockId, OpenMode.ForRead) as BlockTableRecord;
foreach (var subId in dimensionBlock)
BlockTableRecord dimensionBlock = t.GetObject(dimension.DimBlockId, OpenMode.ForRead) as BlockTableRecord;
foreach (ObjectId subId in dimensionBlock)
{
if (TypeUtil.GetTypeInformation(subId).Type == typeof(MText))
{
var mText = t.GetObject(subId, OpenMode.ForRead) as MText;
MText mText = t.GetObject(subId, OpenMode.ForRead) as MText;
base.MoveViewPort(ed, view, obj, mText.Location, mText.ActualHeight, mText.ActualWidth);
mText.Dispose();
}
}
}
Expand Down
1 change: 0 additions & 1 deletion FindAndReplaceCAD/Util/ITypeUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Security.Cryptography;

namespace FindAndReplaceCAD.Util
{
Expand Down
21 changes: 14 additions & 7 deletions FindAndReplaceCAD/Util/MLeaderUtil.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.GraphicsSystem;
using CADApp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Documents;

namespace FindAndReplaceCAD.Util
{
Expand All @@ -20,7 +16,10 @@ public override string GetText(DBObject obj, Transaction t)
}
else if (mLeader.ContentType == ContentType.MTextContent)
{
return mLeader.MText.Text;
MText mText = mLeader.MText;
string output = mText.Contents;
mText.Dispose();
return output;
}

throw new InvalidOperationException();
Expand All @@ -38,6 +37,7 @@ public override void WriteText(DBObject obj, string newText, Transaction t)
MText newMText = mLeader.MText.Clone() as MText;
newMText.Contents = CADUtil.ReplaceWithCADEscapeCharacters(newText);
mLeader.MText = newMText;
newMText.Dispose();
}

throw new InvalidOperationException();
Expand All @@ -52,7 +52,10 @@ public override bool GetMask(DBObject obj)
}
else if (mLeader.ContentType == ContentType.MTextContent)
{
return mLeader.MText.BackgroundFill && mLeader.MText.UseBackgroundColor;
MText mText = mLeader.MText;
bool output = mText.BackgroundFill && mText.UseBackgroundColor;
mText.Dispose();
return output;
}

throw new InvalidOperationException();
Expand All @@ -70,6 +73,7 @@ public override void WriteMask(DBObject obj, bool newMask)
newMText.BackgroundFill = newMask;
newMText.UseBackgroundColor = true;
mLeader.MText = newMText;
newMText.Dispose();
}

throw new InvalidOperationException();
Expand Down Expand Up @@ -116,7 +120,9 @@ private string GetMLeaderBlockText(MLeader obj, Transaction myT)
{
AttributeDefinition attDef = myT.GetObject(id2, OpenMode.ForRead) as AttributeDefinition;
AttributeReference attRef = obj.GetBlockAttribute(attDef.Id);
return attRef.TextString;
string output = attRef.TextString;
attRef.Dispose();
return output;
}
}
}
Expand Down Expand Up @@ -152,6 +158,7 @@ public override void MoveViewPort(Editor ed, ViewTableRecord view, Transaction t
{
MText mText = mLeader.MText;
base.MoveViewPort(ed, view, obj, mText.Location, mText.ActualHeight, mText.ActualWidth);
mText.Dispose();
}


Expand Down

0 comments on commit cc8ce1a

Please sign in to comment.