Deleting views with a yes/no parameter

Deleting views with a yes/no parameter

aclarke
Advocate Advocate
863 Views
3 Replies
Message 1 of 4

Deleting views with a yes/no parameter

aclarke
Advocate
Advocate

Hi all,

I am having some difficulty understanding an issue.  I have a list of views I want to keep by a yes/no parameter.  problem is, all views are deleted.  If I swap out the delete with a print to list, the output looks fine.  The views I want to delete are listed and the ones I want to keep are not --  the views are not dependent on a parent view but all views go bye-bye.

I have tried !v.LookupParameter(paramterName.AsValueString().Equals("Yes"))

.AsInteger(1));

 

Thanks for any assistance

 

string parameterName = "ViewUse";

List<ViewPlan> deleteViews = new List<ViewPlan>();
foreach (ViewPlan v in myFunctions.CollectedViewPlans(doc))
{
    if (v.LookupParameter(parameterName).AsValueString() != "Yes")
    {
        deleteViews.Add(v);
    }
}

foreach (ViewPlan v in deleteViews)
{
    try
    {
        doc.Delete(v.Id);
    }
    catch { }
}

 

0 Likes
Accepted solutions (1)
864 Views
3 Replies
Replies (3)
Message 2 of 4

TripleM-Dev.net
Advisor
Advisor

Hi,

 

the line: 

v.LookupParameter(parameterName).AsValueString() != "Yes"

 

Checks if the value isn't "Yes", well in Revit if a parameter hasn't been used it actually doesn't have a state so for a boolean it would return No (See in Revit properties it's a grey'd box with a x in it, but reads no).

 

Use this instead, should work (I also always check if the variable is not Null):

Parameter Validate = v.LookupParameter(parameterName);
 if (Validate != null && Validate.HasValue && Validate.AsValueString() != "Yes")

 

- Michel

0 Likes
Message 3 of 4

Sean_Page
Collaborator
Collaborator

I think you could also just take the "string" out of the equation and just use the integer value.

 

//Use an Element Id list and you can delete them all at once rather than iterate the list again.
List<ElementId> deleteViewIds = new List<ElementId>();

//Yes/No params have 1 or 0 as a value for thier boolean as well.
if(v.LookupParameter(param).AsInteger() != 1)
{
    //Add the Element Id to the list, not the ViewPlan
    deleteViews.add(v.Id);
}

try
{
    //Delete method can take a list of element Ids 
    doc.Delete(deleteViews);
}
catch
{

}

 

Sean Page, AIA, NCARB, LEED AP
Partner, Computational Designer, Architect
0 Likes
Message 4 of 4

aclarke
Advocate
Advocate
Accepted solution

Thanks for the replies

 

The issue was a callout view

So we have:

'Primary View' and 'Dependent View', which is a duplicate as dependent view

and

'Parent View' and 'Child View', which is a callout view 'child' on a master plan 'parent' view

 

When the parent view gets deleted the child view goes with it

 

 

 

0 Likes