Message 1 of 4
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
---I am not good at English.---
I want to change the assembly type of all other instances to match the changed one, then restore the assembly type name to its original name.
However, the following error appears.
"An elements was modified in a way that yields a new cross reference with another element. Such changes are not allowed in the context the document is currently in."
This is part of the code.
I am a beginner of RevitAPI.
What is wrong?
public class AssEdit : IUpdater
{
public static bool _updateActive = true;
static AddInId _appId;
static UpdaterId _updaterId;
static string rawAss = null;
static string createdAss = null;
static List<Element> toChangeAss = new List<Element>();
static ElementId sourceAss = null;
public AssEdit(AddInId id)
{
_appId = id;
_updaterId = new UpdaterId(_appId, new Guid(
"76DD8055-80CA-4400-847C-E77325936DDD"));
}
public void Execute(UpdaterData data)
{
if (_updateActive)
{
Document doc = data.GetDocument();
Application app = doc.Application;
rawAss = "AssemblyName0"; //RAW AssemblyName
foreach (ElementId id in
data.GetAddedElementIds())
{
Element elem = doc.GetElement(id);
string cat = elem.Category.Name;
if (cat == "Assemblies")
{
createdAss = elem.Name; //NEW AssemblyName
toChangeAss = new FilteredElementCollector(doc)
.OfCategory(BuiltInCategory.OST_Assemblies)
.WherePasses(new ElementClassFilter(typeof(AssemblyInstance)))
.Cast<Element>()
.Where(x => x.Name == rawAss)
.ToList();
createdAssId = new FilteredElementCollector(doc)
.OfCategory(BuiltInCategory.OST_Assemblies)
.Where(x => x.Name == createdAss)
.Select(x => x.GetTypeId())
.First();
if (toChangeAss.Count() > 0)
{
foreach (Element ass in toChangeAss)
{
try
{
ass.ChangeTypeId(createdAssId);
}
catch(Exception e)
{
TaskDialog.Show("Why?", e.Message);
}
}
return;
}
}
}
}
else
{
return;
}
}
SUGKJ
Solved! Go to Solution.