应用程序:ReadonlySharedParameters

Revit平台:所有版本

Revit版本:2015.0

首次发布:2015.0

编程语言:C#

技能等级:中级

类别:参数

类型:ExternalCommand / ExternalApplication

主题:只读共享参数

相关类:

Autodesk.Revit.UI.IExternalCommand

Autodesk.Revit.UI.IExternalApplication

Autodesk.Revit.DB.FilteredElementCollector

Autodesk.Revit.DB.FilterRule

Autodesk.Revit.DB.ElementParameterFilter

Autodesk.Revit.DB.Parameter

Autodesk.Revit.DB.DefinitionFile

Autodesk.Revit.DB.DefinitionGroup

项目文件:

SetReadonlyCost1.csSetReadonlyCost2.cs

此文件包含从IExternalCommand接口继承并实现Execute方法的类Command

 

ReadonlySharedParameterApplication.cs

此文件包含从IExternalApplication接口继承并实现OnStartupOnShutdown方法的类Command

 

SharedParameterBindingManager.cs

此类定义了一个数据类,用于实现此示例的所有功能。

源代码:

ReadonlySharedParameterApplicaton.cs

//
// (C) Copyright 2003-2015 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.UI;
namespace Revit.SDK.Samples.ReadonlySharedParameters.CS
{
    public class ReadonlySharedParameterApplication : IExternalApplication
    {
        #region IExternalApplication Members
        Result IExternalApplication.OnShutdown(UIControlledApplication application)
        {
            return Result.Succeeded;
        }
        Result IExternalApplication.OnStartup(UIControlledApplication application)
        {
            RibbonPanel panel = application.CreateRibbonPanel("Shared parameters");
            PushButtonData data = new PushButtonData("BindSP", "Bind Shared\nParameters", 
                                    this.GetType().Assembly.Location, typeof(BindNewReadonlySharedParametersToDocument).FullName);
            panel.AddItem(data);
            panel.AddSeparator();
            PushButtonData data1 = new PushButtonData("SetIds1", "Set ids: GUID",
                                    this.GetType().Assembly.Location, typeof(SetReadonlyId1).FullName);
            PushButtonData data2 = new PushButtonData("SetIds2", "Set ids: short",
                                    this.GetType().Assembly.Location, typeof(SetReadonlyId2).FullName);
            panel.AddStackedItems(data1, data2);
            panel.AddSeparator();
            data1 = new PushButtonData("SetCosts1", "Set cost: random",
                                    this.GetType().Assembly.Location, typeof(SetReadonlyCost1).FullName);
            data2 = new PushButtonData("SetCosts2", "Set cost: sequence",
                                    this.GetType().Assembly.Location, typeof(SetReadonlyCost2).FullName);
            panel.AddStackedItems(data1, data2);
            return Result.Succeeded;
        }
        #endregion
    }
}

ReadonlySharedParametersCommands.cs

//
// (C) Copyright 2003-2015 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.ApplicationServices;
using System.IO;

namespace Revit.SDK.Samples.ReadonlySharedParameters.CS
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
class SetReadonlyCost1 : IExternalCommand
{
#region IExternalCommand Members

public Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
{
Document doc = commandData.View.Document;

ReadonlyCostSetter.SetReadonlyCosts1(doc);

return Result.Succeeded;
}

#endregion
}

[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
class SetReadonlyCost2 : IExternalCommand
{
#region IExternalCommand Members

public Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
{
Document doc = commandData.View.Document;

ReadonlyCostSetter.SetReadonlyCosts2(doc);

return Result.Succeeded;
}

#endregion
}

class ReadonlyCostSetter
{

public static void SetReadonlyCosts1(Document doc)
{
SetReadonlyCosts(doc, GetReadonlyCostFromId);
}

public static void SetReadonlyCosts2(Document doc)
{
SetReadonlyCosts(doc, GetReadonlyCostFromIncrements);
}

private static double GetReadonlyCostFromId(Element elem, int seed)
{
int costRoot = elem.Id.IntegerValue % 100;
return (double)costRoot * 100.0 + 0.99;
}

private static double GetReadonlyCostFromIncrements(Element elem, int seed)
{
return (double)seed * 100.0 + 0.88;
}

private static void SetReadonlyCosts(Document doc, Func<Element, int, double> valueGetter)
{
FilteredElementCollector collector = new FilteredElementCollector(doc);
collector.WhereElementIsElementType();
FilterRule rule = ParameterFilterRuleFactory.CreateSharedParameterApplicableRule("ReadonlyCost");
ElementParameterFilter filter = new ElementParameterFilter(rule);
collector.WherePasses(filter);

int increment = 1;

using (Transaction t = new Transaction(doc, "Apply ReadonlyCost"))
{
t.Start();
foreach (Element elem in collector)
{
Parameter p = elem.LookupParameter("ReadonlyCost");
if (p != null)
{
p.Set(valueGetter(elem, increment));
}
increment++;
}
t.Commit();
}
}

}

[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
class SetReadonlyId1 : IExternalCommand
{
#region IExternalCommand Members

public Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
{
Document doc = commandData.View.Document;

ReadonlyIdSetter.SetReadonlyIds1(doc);

return Result.Succeeded;
}

#endregion
}

[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
class SetReadonlyId2 : IExternalCommand
{
#region IExternalCommand Members

public Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
{
Document doc = commandData.View.Document;

ReadonlyIdSetter.SetReadonlyIds2(doc);

return Result.Succeeded;
}

#endregion
}

class ReadonlyIdSetter
{
private static string GetReadonlyIdUniqueId(Element elem)
{
return elem.UniqueId;
}

private static string GetReadonlyIdFromElementId(Element elem)
{
Element eType = elem.Document.GetElement(elem.GetTypeId());

return eType.Name.Substring(0, 2) + elem.Id.IntegerValue;
}

public static void SetReadonlyIds1(Document doc)
{
SetReadonlyIds(doc, GetReadonlyIdUniqueId);
}

public static void SetReadonlyIds2(Document doc)
{
SetReadonlyIds(doc, GetReadonlyIdFromElementId);
}

private static void SetReadonlyIds(Document doc, Func<Element, string> idGetter)
{
FilteredElementCollector collector = new FilteredElementCollector(doc);
FilterRule rule = ParameterFilterRuleFactory.CreateSharedParameterApplicableRule("ReadonlyId");
ElementParameterFilter filter = new ElementParameterFilter(rule);
collector.WherePasses(filter);

using (Transaction t = new Transaction(doc, "Apply ReadonlyId"))
{
t.Start();
foreach (Element elem in collector)
{
Parameter p = elem.LookupParameter("ReadonlyId");
if (p != null)
{
p.Set(idGetter(elem));
}
}
t.Commit();
}
}

}

[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
class BindNewReadonlySharedParametersToDocument : IExternalCommand
{
#region IExternalCommand Members

public Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
{
Document doc = commandData.View.Document;

AddSetOfSharedParameters(doc);

return Result.Succeeded;
}

#endregion

private List<SharedParameterBindingManager> BuildSharedParametersToCreate()
{
List<SharedParameterBindingManager> sharedParametersToCreate =
new List<SharedParameterBindingManager>();

SharedParameterBindingManager manager = new SharedParameterBindingManager();
manager.Name = "ReadonlyId";
manager.Type = ParameterType.Text;
manager.UserModifiable = false;
manager.Description = "A read-only instance parameter used for coordination with external content.";
manager.Instance = true;
manager.AddCategory(BuiltInCategory.OST_Walls);
manager.AddCategory(BuiltInCategory.OST_Floors);
manager.AddCategory(BuiltInCategory.OST_Ceilings);
manager.AddCategory(BuiltInCategory.OST_Roofs);
manager.ParameterGroup = BuiltInParameterGroup.PG_IDENTITY_DATA;

sharedParametersToCreate.Add(manager); // Look up syntax for this automatic initialization.

manager = new SharedParameterBindingManager();
manager.Name = "ReadonlyCost";
manager.Type = ParameterType.Currency;
manager.UserModifiable = false;
manager.Description = "A read-only type parameter used to list the cost of a type.";
manager.Instance = false;

manager.AddCategory(BuiltInCategory.OST_Furniture);
manager.AddCategory(BuiltInCategory.OST_Planting);
manager.ParameterGroup = BuiltInParameterGroup.PG_MATERIALS;

sharedParametersToCreate.Add(manager);

return sharedParametersToCreate;
}

public void AddSetOfSharedParameters(Document doc)
{
Application app = doc.Application;

String filePath = GetRandomSharedParameterFileName();

app.SharedParametersFilename = filePath;

DefinitionFile dFile = app.OpenSharedParameterFile();
DefinitionGroup dGroup = dFile.Groups.Create("Demo group");
List<SharedParameterBindingManager> managers = BuildSharedParametersToCreate();
using (Transaction t = new Transaction(doc, "Bind parameters"))
{
t.Start();
foreach (SharedParameterBindingManager manager in managers)
{
manager.Definition = dGroup.Definitions.Create(manager.GetCreationOptions());
manager.AddBindings(doc);
}
t.Commit();
}
}


private String GetRandomSharedParameterFileName()
{

String randomFileName = System.IO.Path.GetRandomFileName();
String fileRoot = Path.GetFileNameWithoutExtension(randomFileName);
String spFile = Path.ChangeExtension(randomFileName, "txt");
String filePath = Path.Combine(@"c:\tmp\Meridian\", spFile);
StreamWriter writer = File.CreateText(filePath);
writer.Close();
return filePath;
}
}
}

SharedParameterBindingManager.cs

//
// (C) Copyright 2003-2015 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
namespace Revit.SDK.Samples.ReadonlySharedParameters.CS
{
    class SharedParameterBindingManager
{
public String Name { get; set; }
public ParameterType Type { get; set; }
public bool UserModifiable  { get; set; }
public bool UserVisible { get; set; }
public String Description { get; set; }
public bool Instance { get; set; }
public Definition Definition { get; set; }
public BuiltInParameterGroup ParameterGroup { get; set; }
public SharedParameterBindingManager ()
{
Name = "Invalid";
Type = ParameterType.Invalid;
UserModifiable = true;
UserVisible = true;
Description = "";
Instance = true;
Definition = null;
ParameterGroup = BuiltInParameterGroup.PG_IDENTITY_DATA;
}
List<BuiltInCategory> m_categories = new List<BuiltInCategory>();
public ExternalDefinitionCreationOptions GetCreationOptions()
{
ExternalDefinitionCreationOptions options = new ExternalDefinitionCreationOptions (Name, Type);
options.UserModifiable = UserModifiable;
options.Visible = UserVisible;
options.Description = Description;
return options;
}
public void AddCategory(BuiltInCategory category)
{
m_categories.Add(category);
}
private CategorySet GetCategories(Document doc)
{
Categories categories = doc.Settings.Categories;
CategorySet categorySet = new CategorySet();
foreach (BuiltInCategory bic in m_categories)
{
categorySet.Insert(categories.get_Item(bic));
}
return categorySet;
}
public void AddBindings(Document doc)
{
Binding binding;
if (Instance)
{
binding = new InstanceBinding(GetCategories(doc));
}
else
{
binding = new TypeBinding(GetCategories(doc));
}
// assumes transaction open
doc.ParameterBindings.Insert(Definition, binding, ParameterGroup);
}
}
    class Utils
    {
        
    }
}