ChangeTypeId of AssemblyInstance during the DMU

ChangeTypeId of AssemblyInstance during the DMU

SUGKJ
Collaborator Collaborator
1,099 Views
3 Replies
Message 1 of 4

ChangeTypeId of AssemblyInstance during the DMU

SUGKJ
Collaborator
Collaborator

 

---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

EESignature

0 Likes
Accepted solutions (1)
1,100 Views
3 Replies
Replies (3)
Message 2 of 4

jeremytammik
Autodesk
Autodesk
Accepted solution

Dear Sugkj,

 

Thank you for your query.

 

Your English is just fine, no worries!

 

In this case, I would first explore exactly how you can achieve the desired operation manually through the user interface.

 

Does that produce the same error?

 

Does that operation require a special situation, some special context?

 

In general, if a feature is not available in the Revit product manually through the user interface, then the Revit API will not provide it either.

 

You should therefore research the optimal workflow and best practices to address your task at hand manually through the user interface first.

 

To do so, please discuss and analyse it with an application engineer, product usage expert, or product support.

 

Once you have got that part sorted out, it is time to step up into the programming environment.

 

I hope this helps.

 

Best regards,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 3 of 4

SUGKJ
Collaborator
Collaborator

Following the advice from you, I reconsidered the idea. And I rewrote the code and got the add-in I wanted. Thank you for your good advice.

SUGKJ

EESignature

0 Likes
Message 4 of 4

michal_prusF4JJ2
Explorer
Explorer

Hi, I am trying to achieve exact same thing and I am running into same Exception message.
Can you please post how you menaged to solve this?

0 Likes