有时候会涉及到组构件的修改,当是又想通过这一次的修改,可以同步到相同的组构件中。这里给大家分享一个自己封装的组构件修改的类,希望可以给大家点帮助。
public class GroupMethod
{
/// <summary>
/// 目标组(名字、相同组名称的集合)
/// </summary>
private List<Tuple<string, GroupType, IEnumerable<Group>>> TargetGroupList = new List<Tuple<string, GroupType, IEnumerable<Group>>>();
/// <summary>
/// 原本组里面的ID
/// </summary>
private List<List<ElementId>> groupIDList = new List<List<ElementId>>();
private Document Doc;
/// <summary>
/// 错误信息
/// </summary>
public string ErrorMessage = "";
public GroupUtils(Document doc)
{
Doc = doc;
}
/// <summary>
/// 添加组合
/// </summary>
/// <param name="group"></param>
public void AddGroup(Group targetGroup)
{
int findGroupIndex = TargetGroupList.FindIndex(x => x.Item1 == targetGroup.Name);
if (findGroupIndex == -1 && targetGroup != null)
{
GroupType groupType = targetGroup.GroupType;
var groups = groupType.Groups.OfType<Group>().Where(x => x.Id.IntegerValue != targetGroup.Id.IntegerValue);
TargetGroupList.Add(Tuple.Create(targetGroup.Name, groupType, groups));
//解组
Transaction trans = null;
if (!Doc.IsModifiable)
trans = new Transaction(Doc, "解组");
trans?.Start();
groupIDList.Add(targetGroup.UngroupMembers().ToList());
trans?.Commit();
}
}
/// <summary>
/// 修改组里面的构件结束
/// </summary>
/// <returns></returns>
public bool ChangeEnding()
{
try
{
Transaction trans = null;
if (!Doc.IsModifiable)
trans = new Transaction(Doc, "重新成组");
trans?.Start();
for (int i = 0; i < TargetGroupList.Count; i++)
{
var groupTuple = TargetGroupList[i];
Group nwGroup = Doc.Create.NewGroup(groupIDList[i]);
Doc.Regenerate();
GroupType nwGroupTyple = nwGroup.GroupType;
foreach (var oldGroup in groupTuple.Item3)
{
oldGroup.GroupType = nwGroupTyple;
}
//删除原本的组
Doc.Delete(groupTuple.Item2.Id);
Doc.Regenerate();
nwGroupTyple.Name = groupTuple.Item1;
}
trans?.Commit();
return true;
}
catch (Exception ex)
{
ErrorMessage += $"{ex.Message} 请联系开发人员\t\n";
return false;
}
}
}
分享就到这里了,希望对你有帮助~~~