Replace Family Parameter with Shared Parameter

Replace Family Parameter with Shared Parameter

ahmadmahrous_1
Contributor Contributor
2,490 Views
11 Replies
Message 1 of 12

Replace Family Parameter with Shared Parameter

ahmadmahrous_1
Contributor
Contributor

Hi, I am trying to edit a loaded family in my project file by replacing family parameter withe shared one:

Document doc = this.Document;

Family family = (from f in new FilteredElementCollector(activeDocument).OfClass(typeof(Family)) where f.Name == "M_Concrete-Rectangular-Column" select f).FirstOrDefault() as Family;

Document familyDoc = activeDocument.EditFamily(family);

Application familyApp = familyDoc.Application;

FamilyParameter familyPar = (from fp in familyDoc.FamilyManager.GetParameters() where fp.Definition.Name == "b" select fp).FirstOrDefault();

ExternalDefinition extDef = CreateSharedParameterDefinition(familyApp, "General", familyPar.Definition.Name, familyPar.Definition.ParameterType);

using (Transaction transaction = new Transaction(familyDoc))
{
	if (transaction.Start("Replace Family Parameter") == TransactionStatus.Started)
	{
		try
		{
			familyDoc.FamilyManager.ReplaceParameter(familyPar, extDef, BuiltInParameterGroup.PG_GEOMETRY, false);
		}
		catch(Exception e)
		{
			TaskDialog taskDialog = new TaskDialog("Revit");
			taskDialog.MainContent = e.Message;
			taskDialog.CommonButtons = TaskDialogCommonButtons.Ok;
			taskDialog.Show();
		}
		transaction.Commit();
	}
}

but  i get exception in the FamilyManager.ReplaceParameter() Method : "Prameter Rplecement failed".

What is wrong with my code??

 

Thank You in Advance

0 Likes
Accepted solutions (2)
2,491 Views
11 Replies
Replies (11)
Message 2 of 12

RPTHOMAS108
Mentor
Mentor

Could be due to making formula invalid. The following is a scenario where that might occur:

 

You change an instance parameter for a type parameter but that existing instance parameter has a formula driven by another instance parameter (type parameter can't be driven by an instance parameter).

 

A similar issue happens when you replace multiple parameters without regeneration.

0 Likes
Message 3 of 12

ahmadmahrous_1
Contributor
Contributor

Thank You RPTHOMAS108:

 

1- I am replacing family type parameter with shared type parameter. as you can see in code of ReplaceParameter Method:

 

familyDoc.FamilyManager.ReplaceParameter(familyPar, extDef, BuiltInParameterGroup.PG_GEOMETRY, false);

 

the last argument is false (not instance).

 

2- The family parameter has no formula and so is the shared parameter.

 

3- what you mean with: "replace multiple parameters without regeneration".

 

Thank you again

0 Likes
Message 4 of 12

RPTHOMAS108
Mentor
Mentor

For (1) and (2) thanks for the clarification, please also note the type of exception it is.

 

I don't think (3) is applicable to your case since you are changing just one parameter within a transaction, it has no formula and you are using the same name. If you replace a parameter with different name then go on to replace a second one which references that parameter in it's formula then the second one fails because the parameter names are not updated in the formula yet.

 

How are you creating the new external definition? Do you close/change the DefinitionFile before using the definition?

 

0 Likes
Message 5 of 12

ahmadmahrous_1
Contributor
Contributor

Thanks Again RPTHOMAS108,

1- My Exception Type is: "InvalidOperationException".

 

2- Here is the Code for creating the new external definition:

private ExternalDefinition CreateSharedParameterDefinition(Application application, string groupName, string parameterName, ParameterType parameterType)
{
			DefinitionFile definitionFile = application.OpenSharedParameterFile();
			DefinitionGroup definitionGroup = (from dg in definitionFile.Groups where dg.Name == groupName select dg).FirstOrDefault();
			if(definitionGroup == null)
			{
				definitionGroup = definitionFile.Groups.Create(groupName);
			} 
			Definition definition = (from d in definitionGroup.Definitions where d.Name == parameterName select d).FirstOrDefault();
			if(definition == null)
			{
				ExternalDefinitionCreationOptions externalDefinitionCreationOptions =  new ExternalDefinitionCreationOptions(parameterName, parameterType);
				definition = definitionGroup.Definitions.Create(externalDefinitionCreationOptions);
			}
			return definition as ExternalDefinition;
}

 

3- How cane I close the definition file before using the definition?

 

0 Likes
Message 6 of 12

RPTHOMAS108
Mentor
Mentor

That looks ok in terms of getting the ExternalDefinition. Sometimes people create a temporary shared parameter file, switch to that, create the definitions and then switch back to previous file. When you change the shared parameter filename and open it you close the previous one and definitions obtained from it become invalid objects of null.

 

 Autodesk.Revit.Exceptions.InvalidOperationException for type parameters is associated with the formula error. Is this parameter you are changing used in any other parameters? Does values set for it cause errors e.g. when set to 0 etc?

0 Likes
Message 7 of 12

FAIR59
Advisor
Advisor
Accepted solution

Apparently there is a Name check in the replace-method. As you are replacing the family parameter "b" with a shared parameter "b", the replacement fails.

Solution: rename the family-parameter first to a non-existing name.

0 Likes
Message 8 of 12

RPTHOMAS108
Mentor
Mentor
Accepted solution

I think @FAIR59 is probably right although that should be classified as ArgumentException would have thought. 

0 Likes
Message 9 of 12

ahmadmahrous_1
Contributor
Contributor

1- the parameter not used in a formula of other parameters.

2- Yes the parameter causes error if set to zero.

0 Likes
Message 10 of 12

ahmadmahrous_1
Contributor
Contributor

but I can do the replacement through revit without any problems.

0 Likes
Message 11 of 12

RPTHOMAS108
Mentor
Mentor

Would  just try it, sometimes API places additional conditions on things. The FamilyManager has limitations that don't exist in the UI. Such as setting parameter values of current (default) type when no type exists in family.

0 Likes
Message 12 of 12

ahmadmahrous_1
Contributor
Contributor

It worked when I change the name of the parameter. I think I need to rename the family parameter first to any other rename then replace it. If I want to replace withe the same original name.

Thank you FAIR59 for this note

and thanks to RPTHOMAS108 for great help

0 Likes