我正在制作下面显示的Revit 2015例程。例程用于选择元素并创建视图过滤器,该过滤器将自动隐藏该系统上的所有元素。它只在项目中工作一次,因为视图过滤器只能在项目中创建一次,然后重用于其他视图。有谁知道我如何收集模型中的所有视图过滤器,并将名称与我想要创建的视图过滤器进行比较?
public void HideSystems()
{
UIDocument uidoc = this.ActiveUIDocument;
Document doc = uidoc.Document;
Reference r = uidoc.Selection.PickObject(ObjectType.Element,new SystemElementFilter(),"Select an element of a system");
if (r == null)
{
return;
}
Element elem = doc.GetElement(r.ElementId);
var systemNameParam =elem.get_Parameter(BuiltInParameter.RBS_DUCT_PIPE_SYSTEM_ABBREVIATION_PARAM);
if (systemNameParam == null)
{
}
var view = doc.ActiveView;
var categoriesWithSystem =
GetCategoriesApplicableForParameter(doc, BuiltInParameter.RBS_DUCT_PIPE_SYSTEM_ABBREVIATION_PARAM);
var categoriesWithoutSystemNameParameter =
GetCategoriesApplicableForParameter(doc, BuiltInParameter.RBS_DUCT_PIPE_SYSTEM_ABBREVIATION_PARAM, true);
var systemName = systemNameParam.AsString();
// An element can be assigned to the several systems
// In this case System Name parameter has a comma separated
// list of the systems
// Create several rules
IList<FilterRule> rules = new List<FilterRule>();
var systems = systemName.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var system in systems)
{
rules.Add(ParameterFilterRuleFactory.CreateContainsRule(new ElementId(BuiltInParameter.RBS_DUCT_PIPE_SYSTEM_ABBREVIATION_PARAM),system.Trim(),true));
}
using (var t = new Transaction(doc, "System hide"))
{
t.Start();
string sysName=string.Concat("Remove ",systemName);
// Hide elements which are in the selected system
ParameterFilterElement filter2 = ParameterFilterElement.Create(doc,sysName,categoriesWithSystem,rules);
view.AddFilter(filter2.Id);
view.SetFilterVisibility(filter2.Id, false);
vi
t.Commit();
}
}
/// <summary>
/// Returns the list of the categories what can be used in filter
/// by the specific parameter
/// </summary>
/// <param name="doc">Document on which the filter is applying</param>
/// <param name="bip">BuiltInParameter</param>
/// <param name="inverse">If true, the list will be inverted. I.e. you will get
/// the list of the categories what cannot be used in filter
/// by the specific parameter </param>
/// <returns></returns>
ICollection<ElementId> GetCategoriesApplicableForParameter(Document doc,BuiltInParameter bip,bool inverse = false)
{
// All categories available for filter
var allCategories = ParameterFilterUtilities.GetAllFilterableCategories();
allCategories.Remove(new ElementId(BuiltInCategory.OST_DuctTerminal));
allCategories.Remove(new ElementId(BuiltInCategory.OST_PlumbingFixtures));
ICollection<ElementId> retResult = new List<ElementId>();
foreach (ElementId categoryId in allCategories)
{
// get the list of parameters, compatible with the category.
var applicableParameters =
ParameterFilterUtilities.GetFilterableParametersInCommon(doc, new[] {categoryId});
// if the parameter we are interested in the collection
// add it to the result
if (applicableParameters.Contains(new ElementId(bip)))
{
retResult.Add(categoryId);
}
}
// Invert if needed.
if (inverse)
retResult =
allCategories.Where(x => !retResult.Contains(x)).ToList();
return retResult;
}
}
/// <summary>
/// Allow to select only element what has the parameter
/// 'System Name' and has value.
/// </summary>
public class SystemElementFilter : ISelectionFilter
{
public bool AllowElement(Element elem)
{
var systemNameParam =
elem.get_Parameter(BuiltInParameter.RBS_DUCT_PIPE_SYSTEM_ABBREVIATION_PARAM);
return systemNameParam != null && systemNameParam.HasValue;
}
public bool AllowReference(Reference reference, XYZ position)
{
throw new NotImplementedException();
}
}
}
`
答案 0 :(得分:1)
前段时间我为类似的任务编写了一个代码,为每个元素类别创建了不同的视图,请参阅original here,也在下面复制:
// get the active document from the command Execute param
Document doc = commandData.Application.ActiveUIDocument.Document;
// get all generic models on the current 3D View
FilteredElementCollector collGenericModels =
new FilteredElementCollector(doc, doc.ActiveView.Id);
collGenericModels.OfClass(typeof(FamilyInstance));
collGenericModels.OfCategory(BuiltInCategory.OST_GenericModel);
// but we need the families of these generic models,
// so let's create a list with the family IDs
List<int> genericModelsFamiliesIds = new List<int>();
foreach (FamilyInstance equip in collGenericModels)
{
ElementId symbolId = equip.Symbol.Id;
if (!genericModelsFamiliesIds.Contains(symbolId.IntegerValue))
genericModelsFamiliesIds.Add(symbolId.IntegerValue);
}
// get the active view
View3D activeView = doc.ActiveView as View3D;
if (activeView == null)
{
// the active view is required in this case
// as it will be duplicated with
message = "Please run command from a 3D View";
return Result.Failed;
}
// control copy numbers
int copy = 1;
// this list will store the views created
List<ElementId> viewIds = new List<ElementId>();
foreach (int genericModelId in genericModelsFamiliesIds)
{
// get the family
FamilySymbol famSymbol = doc.GetElement(
new ElementId(genericModelId)) as FamilySymbol;
// create the view by duplicating the active view
ElementId genericModelViewId = activeView.Duplicate(
ViewDuplicateOption.Duplicate);
// and add to the list
viewIds.Add(genericModelViewId);
// and open for further processing
View3D genericModelView = doc.GetElement(
genericModelViewId) as View3D;
// here you can adjust the view name
// according to internal rules
string viewName = string.Format("MM-{0}", famSymbol.Name);
try
{
// try apply a name
genericModelView.ViewName = viewName;
}
catch
{
// should not happen, except for
// wrong project standard
viewName = string.Format("{0} - Copy ({1})", viewName, copy);
genericModelView.ViewName = viewName;
copy++;
}
// the new view may come without the categories
// configured for generic models, so let's set all
// as Visible=false, then set GenericModel=true
foreach (Category cat in doc.Settings.Categories)
{
try { genericModelView.SetVisibility(cat, false); }
catch { /* não aplicável */}
}
Category catGenericModel = doc.Settings.Categories.
get_Item(BuiltInCategory.OST_GenericModel);
genericModelView.SetVisibility(catGenericModel, true);
// now create a new filter using the view name
// and prefixed by F_
ParameterFilterElement newFilter =
ParameterFilterElement.Create(
doc, string.Format("F_{0}", viewName),
new[] { catGenericModel.Id });
// with a rule to show only the family we filter
FilterRule rule = ParameterFilterRuleFactory.
CreateNotEqualsRule(
new ElementId(BuiltInParameter.ALL_MODEL_TYPE_NAME),
famSymbol.Name, true);
// and apply the rule
newFilter.SetRules(new[] { rule });
// last step is add the filter and set as false
// as the Rule is Not Equals
genericModelView.AddFilter(newFilter.Id);
genericModelView.SetFilterVisibility(newFilter.Id, false);
}
// finally we have all views, one for each element
// let's export it to DWG files using the view id list
DWGExportOptions options = DWGExportOptions.GetPredefinedOptions(
doc, "Standard");
doc.Export("c:\\temp_rvt\\", "GenericModels", viewIds, options);