Adding family symbol parameter causes a naming exception

Adding family symbol parameter causes a naming exception

Anonymous
Not applicable
1,256 Views
5 Replies
Message 1 of 6

Adding family symbol parameter causes a naming exception

Anonymous
Not applicable

I am using c# and revit 2016.

 

My code adds a family symbol parameter to a brand new element. However, It throws an exception "The name "XYZ" is already in use. Parameter name: parameterName". I open the target element's family editor and I find that there is no parameter called "XYZ".  I reboot revit and run the same command again it works ( I find the parameter "XYZ" appears on the target element ). Moreover, I find that the exception occurs now and then  when I open the revit.  So I want to know is there a bug in my code or revit itself ?

private void SetFamilySymbolStringParamForSingleElement(Document familyDoc, FamilySymbol familySymbol,String targetParam, String targetParamValue){
   if (familySymbol.LookupParameter(targetParam) == null)
      {
           using (Transaction trans = new Transaction(familyDoc, "Add params" + targetParam))
                {
                    trans.Start();
                    FamilyParameter target_newParam = familyDoc.FamilyManager.AddParameter(targetParam,
                        BuiltInParameterGroup.PG_IDENTITY_DATA, ParameterType.Text, false);
                    familyDoc.FamilyManager.Set(target_newParam, targetParamValue);
                    trans.Commit();
                }
            }
            else
            {
                using (Transaction trans = new Transaction(familyDoc,
                    "Set params" + targetParam + "with a new value" + targetParamValue))
                {
                    trans.Start();
                    Parameter existed_targetParam = familySymbol.LookupParameter(targetParam);
                    existed_targetParam.Set(targetParamValue);
                    trans.Commit();
                }
            }
        }

 

FamilyInstance famInst = element as FamilyInstance;
    if (famInst != null)
    {
       FamilySymbol eleSymbol = famInst.Symbol;
       Document familyDoc = document.EditFamily(famInst.Symbol.Family);
       using (TransactionGroup transGroup = new TransactionGroup(document, "create parameters for a single element"))
       {                        
transGroup.Start(); SetFamilySymbolStringParamForSingleElement(familyDoc, eleSymbol, "XYZ", XYZ); SetFamilySymbolStringParamForSingleElement(familyDoc, eleSymbol, "ABC", ABC); SetFamilySymbolStringParamForSingleElement(familyDoc, eleSymbol, "DEF", DEF); transGroup.Commit(); }
familyDoc.LoadFamily(document, new projFamLoadOption());
0 Likes
1,257 Views
5 Replies
Replies (5)
Message 2 of 6

so-chong
Advocate
Advocate

Hi,

 

In this line of code of yours

SetFamilySymbolStringParamForSingleElement(familyDoc, eleSymbol, "XYZ", XYZ);

Could it be the XYZ (the one without quotation marks) mean that you are using the XYZ Class name, accidentally?

Avoid using Class name and try to change to something like

SetFamilySymbolStringParamForSingleElement(familyDoc, eleSymbol, "GHI", GHI);

 

Hope this helps

 

0 Likes
Message 3 of 6

Anonymous
Not applicable

The XYZ (the one without without quotation marks) is just a  string variable , not a class name

0 Likes
Message 4 of 6

BardiaJahan
Advocate
Advocate

Try regenerating your familyDoc within the transaction in method SetFamilySymbolStringForSingleElement()

I would also try regenerating the target document after loading the family.

0 Likes
Message 5 of 6

Anonymous
Not applicable

As far as I know,  trans.commit() will automatically call doc.regenerate().  I do not know where to  add this method within the transaction.

0 Likes
Message 6 of 6

BardiaJahan
Advocate
Advocate

Your transaction are all in familyDoc but your main document never gets regenerated after loading the family. I would try opening a transaction in the main doc and regenerating it.

0 Likes