Skip to content

Commit fb455d2

Browse files
committed
Update LockDocumentRelayCommand to use LockDocumentManager to support document locking.
1 parent 181ee30 commit fb455d2

File tree

5 files changed

+58
-23
lines changed

5 files changed

+58
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010
### Fixes
1111
- Fix `CheckURLValid` to support `http` and `https`.
1212
- Fix `SetImage` to update `Image` property.
13+
- Update `LockDocumentRelayCommand` to use `LockDocumentManager` to support document locking.
1314

1415
## [0.1.0] / 2025-07-02
1516
### Features

ricaun.AutoCAD.UI.Example/App.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,21 @@ public class App : ExtensionApplication
1414
public override void OnStartup(RibbonControl ribbonControl)
1515
{
1616
var ribbonPanel = ribbonControl.CreateOrSelectPanel(PanelName, TabName);
17-
ribbonPanel.CreateButton("Theme")
18-
.SetCommand(Commands.ThemeChange)
19-
.SetLargeImage("https://github.com/ricaun-io/Autodesk.Icon.Example/releases/download/2.0.0/Box-Red-Light.tiff");
2017

21-
ribbonPanel.CreateButton("Theme")
22-
.SetCommand(Commands.ThemeChange)
23-
.SetLargeImage("https://github.com/ricaun-io/Autodesk.Icon.Example/releases/download/2.0.0/Box-Green-Light.ico");
18+
ribbonPanel.RowStackedItems(
19+
ribbonPanel.CreateButton("Theme")
20+
.SetCommand(Commands.ThemeChange)
21+
.SetLargeImage("https://github.com/ricaun-io/Autodesk.Icon.Example/releases/download/2.0.0/Box-Red-Light.tiff"),
22+
ribbonPanel.CreateButton("Theme")
23+
.SetCommand(Commands.ThemeChange)
24+
.SetLargeImage("https://github.com/ricaun-io/Autodesk.Icon.Example/releases/download/2.0.0/Box-Green-Light.ico"),
25+
ribbonPanel.CreateButton("Theme")
26+
.SetCommand(Commands.ThemeChange)
27+
.SetLargeImage("https://github.com/ricaun-io/Autodesk.Icon.Example/releases/download/2.0.0/Box-Blue-32-Light.png")
28+
);
2429

25-
ribbonPanel.CreateButton("Theme")
26-
.SetCommand(Commands.ThemeChange)
27-
.SetLargeImage("https://github.com/ricaun-io/Autodesk.Icon.Example/releases/download/2.0.0/Box-Blue-32-Light.png");
28-
29-
ribbonPanel.CreateButton("Theme")
30-
.SetCommand(Commands.ThemeChange)
30+
ribbonPanel.CreateButton("Circle")
31+
.SetCommand(Commands.CircleCreate)
3132
.SetLargeImage("Resources/Box-Cyan-Light.tiff");
3233

3334
ribbonControl.ActiveTab = ribbonPanel.Tab;

ricaun.AutoCAD.UI.Example/Commands.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using Autodesk.AutoCAD.ApplicationServices;
2+
using Autodesk.AutoCAD.DatabaseServices;
3+
using Autodesk.AutoCAD.Geometry;
24
using Autodesk.AutoCAD.Runtime;
5+
using System;
36

47
namespace ricaun.AutoCAD.UI.Example
58
{
@@ -10,5 +13,32 @@ public static void ThemeChange()
1013
{
1114
Application.SetSystemVariable("COLORTHEME", 1 ^ (short)Application.GetSystemVariable("COLORTHEME"));
1215
}
16+
17+
[CommandMethod("CircleCreate")]
18+
public static void CircleCreate()
19+
{
20+
Document document = Application.DocumentManager.MdiActiveDocument;
21+
22+
if (document is null) return;
23+
24+
var database = document.Database;
25+
26+
using (var transaction = database.TransactionManager.StartTransaction())
27+
{
28+
var blockTable = transaction.GetObject(database.BlockTableId, OpenMode.ForRead) as BlockTable;
29+
var blockTableRecord = transaction.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
30+
31+
var circle = new Circle()
32+
{
33+
Radius = new Random().NextDouble() * 100.0,
34+
Center = new Point3d(0, 0, 0),
35+
};
36+
37+
blockTableRecord.AppendEntity(circle);
38+
39+
transaction.AddNewlyCreatedDBObject(circle, true);
40+
transaction.Commit();
41+
}
42+
}
1343
}
1444
}

ricaun.AutoCAD.UI/Input/RelayCommand.cs renamed to ricaun.AutoCAD.UI/Input/LockDocumentRelayCommand.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
namespace ricaun.AutoCAD.UI.Input
55
{
6-
internal class RelayCommand : ICommand
6+
internal class LockDocumentRelayCommand : ICommand
77
{
88
private readonly Action _execute;
99
private readonly Func<bool> _canExecute;
1010

11-
public RelayCommand(Action execute, Func<bool> canExecute = null)
11+
public LockDocumentRelayCommand(Action execute, Func<bool> canExecute = null)
1212
{
1313
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
1414
_canExecute = canExecute;
@@ -21,7 +21,10 @@ public bool CanExecute(object parameter)
2121

2222
public void Execute(object parameter)
2323
{
24-
_execute();
24+
using (new Runtime.LockDocumentManager())
25+
{
26+
_execute();
27+
}
2528
}
2629

2730
public event EventHandler CanExecuteChanged
@@ -31,12 +34,12 @@ public event EventHandler CanExecuteChanged
3134
}
3235
}
3336

34-
internal class RelayCommand<T> : ICommand
37+
internal class LockDocumentRelayCommand<T> : ICommand
3538
{
3639
private readonly Action<T> _execute;
3740
private readonly Func<T, bool> _canExecute;
3841

39-
public RelayCommand(Action<T> execute, Func<T, bool> canExecute = null)
42+
public LockDocumentRelayCommand(Action<T> execute, Func<T, bool> canExecute = null)
4043
{
4144
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
4245
_canExecute = canExecute;
@@ -49,9 +52,6 @@ public bool CanExecute(object parameter)
4952
return false;
5053
}
5154

52-
//if (!string.IsNullOrEmpty(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.CommandInProgress))
53-
// return false;
54-
5555
return _canExecute == null || _canExecute((T)parameter);
5656
}
5757

@@ -62,7 +62,10 @@ public void Execute(object parameter)
6262
throw new InvalidOperationException("Parameter cannot be null for value types.");
6363
}
6464

65-
_execute((T)parameter);
65+
using (new Runtime.LockDocumentManager())
66+
{
67+
_execute((T)parameter);
68+
}
6669
}
6770

6871
public event EventHandler CanExecuteChanged

ricaun.AutoCAD.UI/RibbonExtension.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public static RibbonButton CreateButton(this RibbonPanelSource ribbonPanel, stri
147147
public static TRibbonItem SetCommand<TRibbonItem>(this TRibbonItem ribbonItem, Action command) where TRibbonItem : RibbonCommandItem
148148
{
149149
if (command is not null)
150-
ribbonItem.CommandHandler = new RelayCommand(command);
150+
ribbonItem.CommandHandler = new LockDocumentRelayCommand(command);
151151

152152
return ribbonItem;
153153
}
@@ -162,7 +162,7 @@ public static TRibbonItem SetCommand<TRibbonItem>(this TRibbonItem ribbonItem, A
162162
public static TRibbonItem SetCommand<TRibbonItem>(this TRibbonItem ribbonItem, Action<TRibbonItem> command) where TRibbonItem : RibbonCommandItem
163163
{
164164
if (command is not null)
165-
ribbonItem.CommandHandler = new RelayCommand<TRibbonItem>(command);
165+
ribbonItem.CommandHandler = new LockDocumentRelayCommand<TRibbonItem>(command);
166166

167167
return ribbonItem;
168168
}

0 commit comments

Comments
 (0)