Filtering Parameters where keynote is empty

Filtering Parameters where keynote is empty

Anonymous
Not applicable
844 Views
1 Reply
Message 1 of 2

Filtering Parameters where keynote is empty

Anonymous
Not applicable

Hi,
I"m trying to use the FilteredElementCollector to find all WallTypes that have an empty Keynote (which has StorageType = string).

 

My code is as follow:

 

var collector = newFilteredElementCollector(doc);

collector.OfClass(typeof(WallType));

 

var keynoteValueProvider = newParameterValueProvider(newElementId(BuiltInParameter.KEYNOTE_PARAM));

 

var noKeynoteRule = newFilterStringRule(keynoteValueProvider, newFilterStringEquals(), String.Empty, false);

 

var noKeynoteFilter = newElementParameterFilter(noKeynoteRule);

collector.WherePasses(noKeynoteFilter);

 

still my collector always returns 0 element. If I change the String.Empty to an existing value. It works acordingly. And if I change String.Empty to null it throws an exception.

 

Is there any way to do this?

 

Thanks,

Felipe

0 Likes
845 Views
1 Reply
Reply (1)
Message 2 of 2

jeremytammik
Autodesk
Autodesk

Dear Felipe,

 

Yes, I have struggled with using parameter filters for filtered element collectors to search for empty versus non-empty strings in the past. Here is the only code I can find right now that seems to reflect my struggles. It includes an assertion, which may or may not validate whatever it is I am trying to achieve here. Have a look and see whether it helps:

 

    /// <summary>
    /// Retrieve all circuit elements in current active document,
    /// which we identify as all family instance or electrical system
    /// elements with a non-empty RBS_ELEC_CIRCUIT_NUMBER or "Circuit Number"
    /// parameter.
    /// </summary>
    public static IList<Element> GetCircuitElements( Document doc )
    {
      //
      // prepend as many 'fast' filters as possible, because parameter access is 'slow':
      //
      ElementClassFilter f1 = new ElementClassFilter( typeof( FamilyInstance ) );
      ElementClassFilter f2 = new ElementClassFilter( typeof( ElectricalSystem ) );
      LogicalOrFilter f3 = new LogicalOrFilter( f1, f2 );
      FilteredElementCollector collector = new FilteredElementCollector( doc ).WherePasses( f3 );

      BuiltInParameter bip = BuiltInParameter.RBS_ELEC_CIRCUIT_NUMBER;

#if DEBUG
      int n1 = collector.ToElements().Count;

      List<Element> a = new List<Element>();
      foreach( Element e in collector )
      {
        Parameter p = e.get_Parameter( BuiltInParameter.RBS_ELEC_CIRCUIT_NUMBER );
        if( null != p && 0 < p.AsString().Length )
        {
          a.Add( e );
        }
      }
      int n2 = a.Count;
      Debug.Assert( n1 > n2, "expected filter to eliminate something" );

      List<Element> b = (
        from e in collector.ToElements()
        where ( null != e.get_Parameter( bip ) ) && ( 0 < e.get_Parameter( bip ).AsString().Length )
        select e ).ToList<Element>();

      int n3 = b.Count;
      Debug.Assert( n2 == n3, "expected to reproduce same result" );
#endif // DEBUG

      //
      // this is unclear ... negating the rule that says the parameter
      // exists and is empty could mean that elements pass if the parameter
      // is non-empty, but also if the parameter does not exist ...
      // so maybe returning the collection b instead of c would be a safer bet?
      //
      ParameterValueProvider provider = new ParameterValueProvider( new ElementId( bip ) );
      FilterStringRuleEvaluator evaluator = new FilterStringEquals();
      FilterRule rule = new FilterStringRule( provider, evaluator, string.Empty, false );
      ElementParameterFilter filter = new ElementParameterFilter( rule, true );

      collector.WherePasses( filter );
      IList<Element> c = collector.ToElements();
      int n4 = c.Count;
      Debug.Assert( n2 == n4, "expected to reproduce same result" );

      return c;
    }

Cheers, Jeremy.

--
Jeremy Tammik
Autodesk Developer Network -- http://www.autodesk.com/joinadn
The Building Coder -- http://thebuildingcoder.typepad.com



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes