Search code examples
c#sdkrevit-api

Checking to see if definition exists in Revit API


Running into an issue where I can't figure out how to tell this to check if the definition or shared parameter exists before adding. I've tried combinations of if else statements as well as coalescing. There is still a lot that I am learning so any help would be greatly appreciated. ` public Autodesk.Revit.UI.Result Execute( ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements) {

        Transaction transaction = new Transaction(commandData.Application.ActiveUIDocument.Document, "External Tool");
        try
        {

            transaction.Start();

            //Create a clear file as parameter file.
            String path = Assembly.GetExecutingAssembly().Location;
            int index = path.LastIndexOf("\\");
            String newPath = path.Substring(0, index);
            newPath += "\\TPMechanicalRevitParameters.txt";
            if (File.Exists(newPath))
            {
                File.Delete(newPath);
            }
            FileStream fs = File.Create(newPath);
            fs.Close();

            //cache application handle
            Application revitApp = commandData.Application.Application;
            //prepare shared parameter file
            commandData.Application.Application.SharedParametersFilename = newPath;

            //Open shared parameter file
            DefinitionFile parafile = revitApp.OpenSharedParameterFile();
            
            //get Fabricaation Pipe category
            Category TpCat = commandData.Application.ActiveUIDocument.Document.Settings.Categories.get_Item(BuiltInCategory.OST_FabricationPipework);
            CategorySet categories = revitApp.Create.NewCategorySet();
            categories.Insert(TpCat);

            InstanceBinding binding = revitApp.Create.NewInstanceBinding(categories);
         
            //Create a group
            DefinitionGroup apiGroup = parafile.Groups.Create("TpFabricationPipe");


            //Create a visible "VisibleParam" of text type.
            ExternalDefinitionCreationOptions ExternalDefinitionCreationOptions1 = new ExternalDefinitionCreationOptions("FullFabricationServiceName", ParameterType.Text);
                Definition visibleParamDef = apiGroup.Definitions.Create
                    (ExternalDefinitionCreationOptions1);

            BindingMap bindingMap = commandData.Application.ActiveUIDocument.Document.ParameterBindings;
                bindingMap.Insert(visibleParamDef, binding);

                //Create a invisible "InvisibleParam" of text type.
                ExternalDefinitionCreationOptions ExternalDefinitionCreationOptions2 = new ExternalDefinitionCreationOptions("InvisibleParam", ParameterType.Text);
                Definition invisibleParamDef = apiGroup.Definitions.Create
                    (ExternalDefinitionCreationOptions2);
                bindingMap.Insert(invisibleParamDef, binding);

           

        }

        catch (Exception e)
        {
            transaction.RollBack();
            message = e.ToString();
            return Autodesk.Revit.UI.Result.Cancelled;
        }
        finally
        {
            transaction.Commit();
        }
        return Autodesk.Revit.UI.Result.Succeeded;
    }`

Solution

  • Think I've found a solution. I just changed it to see if the file exists instead of the definition & it worked. If there is a better way to achieve this let me know. Here is a sample of the code I used.

            public readonly string sharedFileLocation = "C:\\ProgramData\\Autodesk\\Revit\\Addins\\2021\\TpMechanical\\bin\\Debug";
            public readonly string sharedFileName = "TPMechanicalRevitParameters.txt";
            // Code in Execute Method
            var abc = "No Tp parameters  present";
            var def = "Add shared parameters to the model using the Add TPSPs button.";
            try
            {
                //Check for parameter file.
                var path = Assembly.GetExecutingAssembly().Location;
                var index = path.LastIndexOf("\\");
                var newPath = path.Substring(0, index);
                newPath += "\\TPMechanicalRevitParameters.txt";
                if (!File.Exists(Path.Combine(sharedFileLocation, sharedFileName)))
                {
                    TaskDialog.Show(abc, def);
                    return Result.Cancelled;
    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error: {ex.Message}");
            }