Iterate views and set visibility

Dale.Bartlett
Collaborator
Collaborator

Iterate views and set visibility

Dale.Bartlett
Collaborator
Collaborator

Hi All, Is it possible to iterate all views and set the visibility of an element without making each view current/active? Similar in concept to AutoCAD's global Layer Off. ie ElementVisInAllViews(Element e, bool visibility). Thanks Dale

Courtesy Building Coder - visibility toggle:

foreach (ElementId id in ids)

{

if (id != null)

{

if (true == doc.GetElement(id).IsHidden(doc.ActiveView))

{

if (true == doc.GetElement(id).CanBeHidden(doc.ActiveView))

{

doc.ActiveView.UnhideElements(ids);

}}

else

{

doc.ActiveView.HideElements(ids);

}}}

 




______________
Yes, I'm Satoshi.
0 Likes
Reply
Accepted solutions (1)
1,132 Views
6 Replies
Replies (6)

Anonymous
Not applicable

Oops, wrong account, see below...

0 Likes

Troy_Gates
Advocate
Advocate
Accepted solution

Instead of using doc.ActiveView you could use a collection of Views and iterate through each one and use View.HideElements().

Something like:

foreach (View v in (new FilteredElementCollector(doc).OfClass(typeof(View))))
{
	v.HideElements(ids);
}

 


This would hide the objects in every single view in the model, so you would want to filter the collection of views down to just the views you want to hide the elements in.

Dale.Bartlett
Collaborator
Collaborator

That was very helpful, thank you. The following code is my end result:

<code>

public static Result HideRefPlanesAllViews(Document pDoc, bool pblnTurnOn, out int pintElements, out int pintViews)

{

Result lresResult = Result.Cancelled;

int lintViews = 0;

int lintElements = 0;

FilteredElementCollector col = new FilteredElementCollector(pDoc).OfClass(typeof(ReferencePlane));

ICollection<ElementId> ids = col.ToElementIds();

lintElements = ids.Count();

if (lintElements > 0)

{

using (Transaction tr = new Transaction(pDoc, "HideRefPlanesAllViews"))

{

tr.Start();

try

{

foreach (Autodesk.Revit.DB.View view in (new FilteredElementCollector(pDoc).OfClass(typeof(Autodesk.Revit.DB.View))))

{

if (pblnTurnOn == true)

{

view.UnhideElements(ids);

}

else

{

view.HideElements(ids);

}

lintViews = lintViews++;

}

lresResult = Result.Succeeded;

}

catch (Autodesk.Revit.Exceptions.ArgumentException)

{}

tr.Commit();

}}

pintViews = lintViews;

pintElements = lintElements;

return lresResult;

}

 

</code>




______________
Yes, I'm Satoshi.
0 Likes

Dale.Bartlett
Collaborator
Collaborator

Correction:

lintViews++;

or

lintViews = lintViews + 1;

not

lintViews = lintViews++;

 

 




______________
Yes, I'm Satoshi.
0 Likes

arnostlobel
Alumni
Alumni

Hello Dale Bartlett,

 

I have a question regarding to your code-sample. I wonder why do you expect an ArgumentException? It is typically an indication of code not written or functioning currectly. And since you ignore it, you would never know that your code may need fixing. Also, wouldn't ignoring the exception mean that sometimes you might return Result.Cancelled while in fact you have already hidden/unhidden the elements in some views already and succesfully commited those changes in a transaction?

Is an argument exception something that you have experience particularly with the hide/unhide methods you use in this sample? Or do you use it as a way of using the same collection of elements even with views where some of the elements may not be hidden/unhidden? Or is it just all part of some template you usually base your methods on?

 

Thank you

Arnošt Löbel
0 Likes

Dale.Bartlett
Collaborator
Collaborator

Hi Arnošt,

Your appraisal is very helpful for me and others, thank you. The example I provided was a first working draft, my normal process is more along the lines of the below. Any and all comments appreciated. Dale

using (Transaction tr = new Transaction(pDoc, "NewTransaction"))

{

tr.Start();

try

{

... DoSomething();

}

catch (Exception ex)

{

tr.RollBack();

UtilityLibrary.ErrorLogger(ex, MethodBase.GetCurrentMethod().Name, doc.PathName);

return Result.Cancelled;

}

tr.Commit();

}

return Result.Succeeded;

 




______________
Yes, I'm Satoshi.
0 Likes