problem with FamilyManager.ReplaceParameter

problem with FamilyManager.ReplaceParameter

james.levieux
Advocate Advocate
314 Views
3 Replies
Message 1 of 4

problem with FamilyManager.ReplaceParameter

james.levieux
Advocate
Advocate

Hi All,

I’m working on a program that cleans up “orphaned” shared parameters that get introduced into a model from third-party families.  

 

I’ve got the whole thing working (just barely….not a lot of guardrails yet), but I’m having problems with the ReplaceParameter method.  It doesn't behave as nicely as the Revit interface where all you have to do is just select the family parameter radio button (see screen shot).  Instead, it throws an exception if you replace the old shared parameter with a new family parameter that uses the same name.  I’ve taken a few stabs at changing the name before and after using ReplaceParameter but have failed both ways.

Does anyone know if there is a way rename the parameter after or before the replacement?  You will see some REMed lines in my attached code that show one attempt to change the parameter name to “temp” prior to replacing. 

 

jameslevieux_0-1708051749230.png

using (Transaction tx = new Transaction(famDoc, "editing parameter in: " + f.Name))
{
    tx.Start();
    //oldName = p.Definition.Name;
    //famDoc.FamilyManager.RenameParameter(p, "temp");
    newParam =famDoc.FamilyManager.ReplaceParameter(p, p.Definition.Name + "1", p.Definition.ParameterGroup, p.IsInstance);
    //famDoc.FamilyManager.RenameParameter(newParam, oldName);
    tx.Commit();
}

 

Regards,

James

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

jeremy_tammik
Alumni
Alumni

So, in the UI, can this just be achieved by clicking the radio button to switch from shared to family? If you do so, what happens to the shared parameter? Do you have to delete that afterwards? Do you initially end up with both?

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 4

Mohamed_Arshad
Advisor
Advisor
Accepted solution

HI @james.levieux 

 

   What you have wrote is correct, To Change SharedParameter to FamilyParameter you need use ReplaceParameter Method, First replace the SharedParameter with the new name and then Rename the Parameter. Kindly check the below reference code. I just copied your code modified little bit

 

Reference Code

 

 

using (Transaction replaceParameter = new Transaction(doc, "Replace Parameter"))
            {
                replaceParameter.Start();

                string oldName = p.Definition.Name;

                var newParam = familyManager.ReplaceParameter(sharedParameter, $"{oldName}-New", p.Definition.ParameterGroup, p.IsInstance);

                familyManager.RenameParameter(newParam, p.Definition.Name);

                replaceParameter.Commit();
            }

 

 

Hope this will Helps 🙂


Mohamed Arshad K
Software Developer (CAD & BIM)

Message 4 of 4

james.levieux
Advocate
Advocate

Thank you Mohamed,

 

I definitely tried that tactic first.  I think some other bug (that I was not yet aware of) made me think it didn't work.  Thank you for getting me back on track!

 

James

0 Likes