-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectService.cs
More file actions
61 lines (52 loc) · 2.01 KB
/
SelectService.cs
File metadata and controls
61 lines (52 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using MyApp.Core;
using System;
namespace MyApp.Services;
public class SelectService : ISelectService
{
public (int SelectedCount, double TotalLength, string Message) SelectObjects(SelectOptions selectOptions)
{
try
{
// Get the current document and editor
var document = Application.DocumentManager.MdiActiveDocument;
var editor = document.Editor;
// Define a selection filter for specific object types
var filter = new SelectionFilter(new[]
{
new TypedValue((int)DxfCode.Start,selectOptions.ToString())
});
// Prompt the user to select objects
var result = editor.GetSelection(filter);
if (result.Status == PromptStatus.OK)
{
var selectedObjects = result.Value;
int selectedCount = selectedObjects.Count;
double totalLength = 0.0;
using (var transaction = document.TransactionManager.StartTransaction())
{
foreach (var id in selectedObjects.GetObjectIds())
{
var entity = transaction.GetObject(id, OpenMode.ForRead) as Entity;
if (entity is Curve curve)
{
totalLength += curve.GetDistanceAtParameter(curve.EndParam) - curve.GetDistanceAtParameter(curve.StartParam);
}
}
transaction.Commit();
}
return (selectedCount, totalLength, $"Selected {selectedCount} objects. Total Length: {totalLength:F2}");
}
else
{
return (0, 0.0, "No objects selected.");
}
}
catch (Exception ex)
{
return (0, 0.0, $"Error: {ex.Message}");
}
}
}