-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiBlockReplace.cs
More file actions
132 lines (116 loc) · 5.88 KB
/
Copy pathMultiBlockReplace.cs
File metadata and controls
132 lines (116 loc) · 5.88 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
125
126
127
128
129
130
131
132
// <copyright company="Vermessungsamt Winterthur">
// Author: Edgar Butwilowski
// Copyright (c) 2021 Vermessungsamt Winterthur. All rights reserved.
// </copyright>
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System.Collections.Generic;
using System.Text.RegularExpressions;
[assembly: CommandClass(typeof(WIN.MULTIBLOCKREPLACE.MultiBlockReplace))]
namespace WIN.MULTIBLOCKREPLACE
{
public class MultiBlockReplace
{
[CommandMethod("win_multiblockreplace")]
public void MultiBlockReplaceCommand()
{
Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
PromptResult blocksToReplaceNameResult = editor.GetString("\n" + typeof(MultiBlockReplace).Name +
": " + LocalizedStrings.GetLocalizedStringFor(LocalizedStrings.enter_names_blocks_to_replace));
string blocksToReplaceName = MultiBlockReplace
._GetPromptResultString(blocksToReplaceNameResult, editor);
if(blocksToReplaceName == null)
{
return;
}
// add start and end delimiters for regex, so
// that only exact strings are matched:
string blocksToReplaceNameRegex = "^" + blocksToReplaceName + "$";
// wildcard in regex is .* instead of just *, so replace:
blocksToReplaceNameRegex = blocksToReplaceNameRegex.Replace("*", ".*");
Regex regexPattern = new Regex(blocksToReplaceNameRegex);
Database database = HostApplicationServices.WorkingDatabase;
using (Transaction trans = database.TransactionManager.StartTransaction())
{
BlockTable blockTable = (BlockTable)trans.GetObject(database.BlockTableId, OpenMode.ForRead);
List<BlockTableRecord> blocksToReplace = new List<BlockTableRecord>();
foreach (ObjectId blockId in blockTable)
{
BlockTableRecord blockRecord = (BlockTableRecord)trans.GetObject(blockId, OpenMode.ForRead);
// exclude model and paper space blocks:
if (!blockRecord.Name.StartsWith("*Model_Space") && !blockRecord.Name.StartsWith("*PaperSpace"))
{
// test if the name of the block fits the regex definition:
if (regexPattern.IsMatch(blockRecord.Name))
{
blocksToReplace.Add(blockRecord);
}
}
}
PromptResult isProcedeResult = editor.GetString("\n" + typeof(MultiBlockReplace).Name + ": " +
blocksToReplace.Count + " " + LocalizedStrings.GetLocalizedStringFor(LocalizedStrings.blocks_will_be_replaced));
string isProcede = isProcedeResult.StringResult;
if (isProcede != "J")
{
return;
}
PromptResult replacementBlockNameResult = editor.GetString("\n" + typeof(MultiBlockReplace).Name +
": " + LocalizedStrings.GetLocalizedStringFor(LocalizedStrings.enter_name_of_replace_block));
string replacementBlockName = MultiBlockReplace
._GetPromptResultString(replacementBlockNameResult, editor);
if (replacementBlockName == null)
{
return;
}
ObjectId sigStBlockId;
try
{
sigStBlockId = blockTable[replacementBlockName];
foreach (BlockTableRecord blockToReplace in blocksToReplace)
{
foreach (ObjectId blockToReplaceRefId in blockToReplace.GetBlockReferenceIds(true, true))
{
BlockReference blockToReplaceRef = (BlockReference)trans.GetObject(blockToReplaceRefId, OpenMode.ForWrite);
blockToReplaceRef.BlockTableRecord = sigStBlockId;
}
}
trans.Commit();
editor.WriteMessage("\n" + typeof(MultiBlockReplace).Name + ": " +
LocalizedStrings.GetLocalizedStringFor(LocalizedStrings.successfully_completed));
}
catch (Exception ex)
{
trans.Abort();
editor.WriteMessage("\n" + typeof(MultiBlockReplace).Name + ": " + ex.Message);
}
}
}
// Helper method to check prompt result for validity and to transform to a string.
private static string _GetPromptResultString(PromptResult stringPromptResult, Editor editor)
{
if (stringPromptResult == null || stringPromptResult.Status == PromptStatus.Cancel)
{
editor.WriteMessage("\n" + typeof(MultiBlockReplace).Name + ": " +
LocalizedStrings.GetLocalizedStringFor(LocalizedStrings.no_input_or_canceled_by_user));
return null;
}
string promptResultString = stringPromptResult.StringResult;
if (promptResultString == null)
{
editor.WriteMessage("\n" + typeof(MultiBlockReplace).Name + ": " +
LocalizedStrings.GetLocalizedStringFor(LocalizedStrings.no_input));
return null;
}
promptResultString = promptResultString.Trim();
if (promptResultString.Length == 0)
{
editor.WriteMessage("\n" + typeof(MultiBlockReplace).Name + ": " +
LocalizedStrings.GetLocalizedStringFor(LocalizedStrings.no_input));
return null;
}
return promptResultString;
}
}
}