-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextTransaction.cs
More file actions
124 lines (99 loc) · 3.96 KB
/
Copy pathTextTransaction.cs
File metadata and controls
124 lines (99 loc) · 3.96 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using TextCap.Core;
namespace TextCap.Api
{
public static class TextTransaction
{
public static Result ProcessLowerCaseConversion(ExternalCommandData commandData, Func<string, string> TextConverter)
{
var uiDoc = commandData.Application.ActiveUIDocument;
var doc = uiDoc.Document;
Transaction transaction = new Transaction(doc, "Convert Text to Lowercase");
try
{
var selectedElements = uiDoc.Selection.GetElementIds();
if (selectedElements.Count > 0)
{
UpdateCase(doc, selectedElements, TextConverter);
}
else
{
var selectContinue = true;
while (selectContinue)
{
Element pickedElement = null;
try
{
var pickedElementRef = uiDoc.Selection.PickObject(ObjectType.Element);
pickedElement = doc.GetElement(pickedElementRef);
selectContinue = TextTransaction.UpdateSingleText(doc, pickedElement, TextConverter);
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
{
return Result.Succeeded;
}
}
}
return Result.Succeeded;
}
catch
{
transaction.RollBack();
throw; // Re-throwing the exception for handling in the calling method
}
}
public static void UpdateCase(Document doc, ICollection<ElementId> selectedElements, Func<string, string> TextConverter)
{
using (Transaction tx = new Transaction(doc, "Change TextNote to Uppercase"))
{
tx.Start();
foreach (var selectedElementId in selectedElements)
{
Element selectedElement = doc.GetElement(selectedElementId);
if (selectedElement is TextNote textNote)
{
// Get text from the TextNote and convert to uppercase
string originalText = textNote.Text;
string upperCaseText = TextConverter(originalText);
// Set the text of the TextNote to uppercase
textNote.Text = upperCaseText;
// Do something with the text (e.g., print it)
Debug.WriteLine("Text from the selected TextNote: " + upperCaseText);
}
else
{
continue;
}
}
tx.Commit();
}
}
public static bool UpdateSingleText(Document doc, Element element, Func<string, string> TextConverter)
{
// Check if the picked element is a TextNote
if (element is TextNote textNote)
{
// Get text from the TextNote and convert to uppercase
string originalText = textNote.Text;
string upperCaseText = TextConverter(originalText);
// Set the text of the TextNote to uppercase
using (Transaction tx = new Transaction(doc, "Change TextNote to Uppercase"))
{
tx.Start();
textNote.Text = upperCaseText;
tx.Commit();
}
return true;
}
else
{
return false;
}
}
}
}