- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Dear all,
I have written (prompted) an application macro to retrieve shared parameters (from shared parameters file) and assign to a specific family. However, I keep getting similar error messages [see attached]. Any help would be very much appreciated.
MACRO
using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.UI.Selection;
using System.IO;
using System.Collections.Generic;
using System.Linq;
namespace DDA_CSharpModule8
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.DB.Macros.AddInId("4BE8D9CF-2B20-4F03-A56B-6B9C6AA11B06")]
public partial class ThisApplication
{
private void Module_Startup(object sender, EventArgs e)
{
}
private void Module_Shutdown(object sender, EventArgs e)
{
}
#region Revit Macros generated code
private void InternalStartup()
{
this.Startup += new System.EventHandler(Module_Startup);
this.Shutdown += new System.EventHandler(Module_Shutdown);
}
#endregion
public void AssignSharedParametersMarkers()
{
UIApplication uiapp = new UIApplication(this.Application);
UIDocument uidoc = uiapp.ActiveUIDocument;
Document doc = uidoc.Document;
string sharedParameterFilePath = "C:\\Users\\dda\\OneDrive - XXXXX\\Documents\\01. Revit Families\\XXXX_Shared_Parameters_1.1.txt";
string familyName = "TestIVCoordCube";
string parameterGroupName = "Parametrics";
string[] parameters = { "CoordIssue_Eastings", "CoordIssue_Northings", "CoordIssue_Elev" };
try
{
// Check if shared parameter file exists
if (!File.Exists(sharedParameterFilePath))
{
TaskDialog.Show("Error", string.Format("Shared parameter file not found at: {0}", sharedParameterFilePath));
return;
}
// Set the shared parameter file in Revit
Autodesk.Revit.ApplicationServices.Application app = doc.Application;
app.SharedParametersFilename = sharedParameterFilePath;
// Open the shared parameter file
DefinitionFile sharedParameterFile = app.OpenSharedParameterFile();
if (sharedParameterFile == null)
{
TaskDialog.Show("Error", "Unable to open the shared parameter file.");
return;
}
using (Transaction transaction = new Transaction(doc, "Assign Shared Parameters to Family"))
{
transaction.Start();
// Find the required family in the project
FilteredElementCollector familyInstances = new FilteredElementCollector(doc)
.OfCategory(BuiltInCategory.OST_SpecialityEquipment)
.WhereElementIsNotElementType();
foreach (Element element in familyInstances)
{
FamilyInstance familyInstance = element as FamilyInstance;
if (familyInstance != null && familyInstance.Name.Equals(familyName, StringComparison.OrdinalIgnoreCase))
{
Family family = familyInstance.Symbol.Family;
// Open the family document for editing
Document familyDoc = doc.EditFamily(family);
foreach (string parameterName in parameters)
{
// Check if the parameter already exists
bool parameterExists = familyDoc.FamilyManager.Parameters
.Cast<FamilyParameter>()
.Any(param => param.Definition.Name == parameterName);
if (!parameterExists)
{
// Get the shared parameter definition
DefinitionGroup group = sharedParameterFile.Groups.get_Item(parameterGroupName);
if (group == null)
{
TaskDialog.Show("Error", string.Format("Parameter group not found: {0}", parameterGroupName));
return;
}
Definition definition = group.Definitions.get_Item(parameterName);
if (definition is ExternalDefinition externalDefinition)
{
// Create a new shared parameter
familyDoc.FamilyManager.AddParameter(
externalDefinition,
GroupTypeId.Data,
true
);
}
else
{
TaskDialog.Show("Error", string.Format("Invalid definition for parameter: {0}", parameterName));
return;
}
}
}
// Save and reload the family into the project
familyDoc.Save();
familyDoc.Close();
}
}
transaction.Commit();
TaskDialog.Show("Success", string.Format("Shared parameters assigned successfully to family: {0}", familyName));
}
}
catch (Exception ex)
{
TaskDialog.Show("Error", string.Format("An error occurred: {0}", ex.Message));
}
}
}
}
Solved! Go to Solution.