How to change by code "phase" and "phase filter" in phasing from properties

How to change by code "phase" and "phase filter" in phasing from properties

Anonymous
Not applicable
4,485 Views
6 Replies
Message 1 of 7

How to change by code "phase" and "phase filter" in phasing from properties

Anonymous
Not applicable

Hi I've trying to change the properties "Phase filter" and "Phase" by code, when I'm on Active View with no results

 

I read these posts and others ones:

 

http://thebuildingcoder.typepad.com/blog/2009/03/create-room-on-level-in-phase.html
https://forums.autodesk.com/t5/revit-api-forum/change-phase-of-a-view/m-p/5223335

http://thebuildingcoder.typepad.com/blog/2014/08/setting-the-phase-of-a-view.html

 

I attached an image with the properties what I want to changeforos.PNG programmatically.

 

I have this code:

 

using (ViewCreatorForm viewCreator = new ViewCreatorForm(commandData)) {

                        if (viewCreator.ShowDialog() == System.Windows.Forms.DialogResult.OK) { 

                            List<PhaseFilter> _phFilters = new FilteredElementCollector(doc).OfClass(typeof(PhaseFilter)).Cast<PhaseFilter>().ToList();
                            PhaseFilter filter = _phFilters.Find(p => p.Name.ToUpper() == "Show current phase only".ToUpper());

                            using (Transaction transaction = new Transaction(doc, "Duplicate Views"))
                            {
                                transaction.Start();
                                List<View> views = tools.GetAllViews(doc);
                                List<string> newViewName = new List<string>();
                                string temp = string.Empty;
                                views = views.FindAll(p => p.ViewType == ViewType.ThreeD || p.ViewType == ViewType.FloorPlan);
                                foreach (View view in views)
                                {
                                    temp = string.Empty;
                                    try
                                    {
                                        switch (view.ViewType)
                                        {
                                            case ViewType.ThreeD:
                                            case ViewType.FloorPlan:
                                                
                                                foreach (string phaseName in viewCreator.CheckedPhases)
                                                {
                                                    temp = view.ViewName;
                                                    
                                                    if (temp.Contains(phaseName))
                                                    {
                                                        newViewName.Add(temp);
                                                    }
                                                    else
                                                    {
                                                        newViewName.Add(string.Format("{0} {1}", temp, phaseName));
                                                    }
                                                    view.ViewName = newViewName.Last();
                                                    view.Duplicate(ViewDuplicateOption.WithDetailing);
                                                    view.ViewName = temp;

/////////// I'd like to add your help in here ///////////

                                                }
                                                break;
                                        }
                                    }
                                    catch (Exception)
                                    { }
                                }

                                transaction.Commit();
                            }                            
                        }
                    }

Can you help?

 

Regards!

0 Likes
Accepted solutions (2)
4,486 Views
6 Replies
Replies (6)
Message 2 of 7

RPTHOMAS108
Mentor
Mentor

Changing these properties should be the same as changing any built-in parameter.

 

If they are ReadOnly then likely a Template has been applied. templates should also be excluded from your loop (check View.IsTemplate).

 

Information gathered via lookup tool (Revit 2019):

 

Phase: BuiltInParameter.VIEW_PHASE
-1012102, StorageType: ElementID

Phase Filter: BuiltInParameter.VIEW_PHASE_FILTER
-1012103, StorageType: ElementID

 

The storage type is ElementID therefore this implies you need to filter for the named Phase and PhaseFilter objects you want in order to get the ID to set.

 

 

0 Likes
Message 3 of 7

Anonymous
Not applicable
Tnx for answer RPTHOMAS108, but I'm rookie in revit, do you have some example?
 
 
Regards!
0 Likes
Message 4 of 7

studio-a-int
Advocate
Advocate
Accepted solution

You will need to get the phases in your active document:

PHASES = doc.Phases

iterate through, to select the Phase you want:

EX_PHASE = [i for i in PHASES if i.Name == '01 - Existing'][0] # This is the Existing Phase

then select the view, you want to change the Phase (make sure you do not have a ViewTemplate applied to that view).

MY_VIEW = [i for i in FilteredElementCollector(doc).OfClass(View).ToElements() if i.Name == 'Level 1']

after that set the phase inside of a transaction:

t = Transaction(doc,"CHANGE VIEW PHASE")
t.Start()
for i in MY_VIEW:
    i.get_Parameter(BuiltInParameter.VIEW_PHASE).Set(EX_PHASE.Id)
doc.Regenerate()    
t.Commit()

This code is Python, inside Dynamo.

 

0 Likes
Message 5 of 7

Anonymous
Not applicable

Hi studio-a-int, tnx for asnwer, i tryed your advice, but cannot make it work

this is the code:


views = tools.GetAllViews(doc); PhaseArray Phases = doc.Phases; //It have all phases
string notChanged = string.Empty;
foreach (string viewName in newViewName) { var viewsTemp = views.FindAll(p => p.ViewName.Contains(viewName)); if (viewsTemp.Count == 1) { try { viewsTemp.First().get_Parameter(BuiltInParameter.VIEW_PHASE).Set(4); //arbitrary hardcoded an existing ID } catch (Exception) { notChanged += viewsTemp.First().ViewName+","; }
} if(!string.IsNullOrEmpty(notChanged)) TaskDialog.Show("Error changing phases", notChanged);
doc.Regenerate();

but I can't see changed the propierty, do you have any idea what am I missing?

0 Likes
Message 6 of 7

studio-a-int
Advocate
Advocate
Accepted solution

VIEW_PHASE is an ElementId  storage type.

You are using an integer in your code.

Change that from "4" as you have it, to Phase-you-want-to-chage.Id

IE Phase-you-want-to-chage = '01 - Existing' or 'Project Completion'

In that round parenthesis, you should have an ElementId, not the integer "4" as you have it right now.

Look @ Phase Members and Phase Properties in Revit API.

 

0 Likes
Message 7 of 7

Anonymous
Not applicable

Hi, tnx, I was about to post that xD, and yes, that was my mistake, i handled ids like 0 to n, but it have internals id's, so I need Element.Id necessary,

 

this is the working code:

List<PhaseFilter> _phFilters1 = new FilteredElementCollector(doc).OfClass(typeof(PhaseFilter)).Cast<PhaseFilter>().ToList();
PhaseFilter filter1 = _phFilters.Find(p => p.Name.ToUpper() == "MY FILTER PHASE".ToUpper());
Phase phaseFound=null;
foreach (Phase phase in doc.Phases)
{
if (phase.Name=="MY PHASE") {
phaseFound = phase;
}
}

foreach (string viewName in newViewName) { var viewsTemp = views.FindAll(p => p.ViewName.Contains(viewName)); if (viewsTemp.Count == 1) { try { if(phaseFound!=null) viewsTemp.First().get_Parameter(BuiltInParameter.VIEW_PHASE).Set(phaseFound.Id); viewsTemp.First().get_Parameter(BuiltInParameter.VIEW_PHASE_FILTER).Set(filter1.Id); } catch (Exception) { notChanged += viewsTemp.First().ViewName+","; }
}

 

Tnx to everybody for the help!

0 Likes