在族环境里面绑定共享参数,并且用户在属性面板不可修改参数值
[Transaction(TransactionMode.Manual)]
public class SharedParameters : IExternalCommand
{
private readonly string _desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
var doc = commandData.Application.ActiveUIDocument.Document;
if (!doc.IsFamilyDocument)
{
return Result.Cancelled;
}
var familyManager = doc.FamilyManager;
var file = Path.Combine(_desktop, "test.txt");
if (!File.Exists(file))
{
File.Create(file);
}
doc.Application.SharedParametersFilename = file;
var definitionFile = doc.Application.OpenSharedParameterFile();
using (var trans = new Transaction(doc, "SharedParameters"))
{
try
{
trans.Start();
var groupName = "TestSharedParameter";
var definitionGroup = definitionFile.Groups.get_Item(groupName);
if (definitionGroup is null)
{
definitionGroup = definitionFile.Groups.Create(groupName);
}
var definitionName = "Source";
var definition = definitionGroup.Definitions.get_Item(definitionName);
if (definition is null)
{
var options = new ExternalDefinitionCreationOptions(definitionName, ParameterType.Text)
{
UserModifiable = false,
};
definition = definitionGroup.Definitions.Create(options);
}
if (familyManager.get_Parameter(definition) is null)
{
var familyParameter = familyManager.AddParameter(definition as ExternalDefinition, BuiltInParameterGroup.PG_TEXT, true);
if (familyParameter != null)
{
familyManager.Set(familyParameter, "Hello World");
}
}
trans.Commit();
}
catch (Exception)
{
if (trans.HasStarted())
{
trans.RollBack();
}
}
}
return Result.Succeeded;
}
}
标签:definition,familyManager,只读,doc,var,definitionGroup,二次开发,trans,Revit From: https://www.cnblogs.com/youngala/p/17563559.html