Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Error creating Shared Params

5 REPLIES 5
Reply
Message 1 of 6
carlosrdrzalns
1236 Views, 5 Replies

Error creating Shared Params

Hi there, 

 

I am getting an error when creating Shared Parameters inside my document, giving me the following Exception:

 

carlosrdrzalns_0-1633686847238.png

 

The message is "Unable to make changes to the associated shared parameter file". 

 

In the code, I read from an Excel File and create the params, checking if the groups and params exist to avoid trying to duplicate parameters or groups, as it should cause an exception to be thrown. 

 

try
                {
                    while (true)
                    {

                        string ParamName = (string)(hojaParametros.Cells[paramExcelRow, paramsHeader.ParameterColunm] as Excel.Range).Value2;
                        if (ParamName == null || ParamName == "")
                        {
                            break;
                        }
                        string Group = (string)(hojaParametros.Cells[paramExcelRow, paramsHeader.groupColunm] as Excel.Range).Value2;
                        string Type = (string)(hojaParametros.Cells[paramExcelRow, paramsHeader.typeColunm] as Excel.Range).Value2;
                        DefinitionGroups definitiongroups = definitionFile.Groups;
                        DefinitionGroup definitionGroup;
                        if (definitiongroups.get_Item(Group) != null)
                        {
                            definitionGroup = definitiongroups.get_Item(Group);
                        }
                        else
                        {
                            definitionGroup = definitiongroups.Create(Group);
                        }
                        if (definitionGroup.Definitions.get_Item(ParamName) == null)
                        {
                            ParameterType parameterType = GetParameterType(Type);
                            ExternalDefinitionCreationOptions externalDefinitionCreationOptions = new ExternalDefinitionCreationOptions(ParamName, parameterType);
                            definitionGroup.Definitions.Create(externalDefinitionCreationOptions);
                        }
                        paramExcelRow++;
                    }
                }
                catch (System.Exception exp)
                {
                    message = exp.Message;
                    Libro.Close();
                    ExcelApp.Quit();
                    return Result.Failed;
                }

 

To open the Shared Parameters File I am using the following function:

 

        public DefinitionFile setAndOpenExternalSharedParamFile(Autodesk.Revit.ApplicationServices.Application application, string sharedParamFile)
        {
            application.SharedParametersFilename = sharedParamFile;
            return application.OpenSharedParameterFile();
        }

 

The funny thing is that I have tried the code step by step and the exception is not thrown and the shared parameters created, but if I try to play it, the exception is thrown. 

 

I have already used this code in other add-ins and never gave me troubles, so I don't know what might be causing it.

 

Any help or advice would be really appreciated, maybe there is any consideration with the file I am not taking into account.

 

Best Regards, 

 

Labels (1)
5 REPLIES 5
Message 2 of 6

The external definitions can go out of scope depends what you are doing with the file i.e. if you change the path open it to get the groups and change the path back that sometimes has caused issues for me (as I would expect it to).

 

The file needs to exist and be writeable. Does debug configuration dictate location of file i.e. do you find it relative to assembly?

 

 Is GetParameterType coming from spec utils? That doesn't take a string.

Message 3 of 6

Thank you for your answer @RPTHOMAS108 , 

 

The Shared Parameters File path is defined by the user with a windows form, so it is not relative to the assembly path, should it be?

 

The GetParameterType is a function that I have developed that takes the ParameterType.Name as a string and return the ParameterType enun. 

 

BR,

 

Message 4 of 6

I asked about debug configuration because you noted you wasn't experiencing the issue during stepping through. Led me to consider if the debug configuration was pointing to a settings file in either the debug or release sub folders (with whatever dll is running).

 

The method you are using in now obsolete so you should switch to ExternalDefinitionCreationOptions(String, ForgeTypeId). Rather than still using ParameterType enum.

 

Regarding the issue you should report the shared parameter file location to output and see if it is pointing to a valid file that can be written to. Similarly would check the ParameterType is as expected after converting from excel string and not defaulting to 'Invalid' or something else that can't be used etc.

Message 5 of 6

I have tried again and then failed while debugging also, and the debug configuration file is not in the Debug output folder neither, so I do not think a problem with the debugging ... I tried different times to see if it always fails in the same parameter and may be a problem with the parameter definitions but it seems to be arbitrary. 

 

The file is writable because it always creates some parameters, but not all, and it did not stop in the same parameter each time. 

 

For the parameter ID I have used the following function:

 

public static ParameterType GetParameterType(string dataParam)
        {
            Type enumType = typeof(ParameterType);
            System.Reflection.FieldInfo[] fieldInfos = enumType.GetFields();
            foreach (System.Reflection.FieldInfo fieldInfo in fieldInfos)
            {
                if (String.Equals(fieldInfo.Name, dataParam, StringComparison.OrdinalIgnoreCase))
                {
                    return (ParameterType)fieldInfo.GetValue(null);
                }

            }

            return ParameterType.Invalid;
        }

 

which I have tried several times and always returns the correct ParamterType. 

 

Is there any way t to create the ForgeTypeId from the ParameterType name? I am new to this type and I have never used it. 

Message 6 of 6

For converting from ParameterType to SpecTypeId you can use:

 

SpecUtils.GetSpecTypeId(ParameterType)

This will not work for:

Custom 99

FamilyType 17

Invalid 0 (Works but returns nothing).

 

This utility method is likely not a long term solution since it is currently marked as being removed in the future.

 

It would not be that easy to convert from ParameterType to SpecType some other way as the two things don't always contain similar values. Enclosed are two files:

ParameterTypeVsSpecTypeId.csv shows ParameterType values paired with SpecTypeId values

SpecTypeId_RevitAPIClassObject.csv shows where the helper properties for each spec are found within the Revit API. Note that some items are nested.

 

Regarding original problem it sounds like your shared parameter file is going out of scope. I don't see the whole picture but you should see if you can limit the time disconnect between when you open the file and when you add the definitions. 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Rail Community


Autodesk Design & Make Report