Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Iterate views and set visibility

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
Dale.Bartlett
928 Views, 6 Replies

Iterate views and set visibility

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.
6 REPLIES 6
Message 2 of 7
tgates
in reply to: Dale.Bartlett

Oops, wrong account, see below...

Message 3 of 7
Troy_Gates
in reply to: Dale.Bartlett

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.

Message 4 of 7
Dale.Bartlett
in reply to: Troy_Gates

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.
Message 5 of 7

Correction:

lintViews++;

or

lintViews = lintViews + 1;

not

lintViews = lintViews++;

 

 




______________
Yes, I'm Satoshi.
Message 6 of 7
arnostlobel
in reply to: Dale.Bartlett

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
Message 7 of 7
Dale.Bartlett
in reply to: arnostlobel

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.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community