Changing an Element's Phase Created Parameter

Changing an Element's Phase Created Parameter

62BJW
Advocate Advocate
3,750 Views
13 Replies
Message 1 of 14

Changing an Element's Phase Created Parameter

62BJW
Advocate
Advocate

I'm attempting to change the Phase Created parameter of all elements to "Existing". Sounds as simple as either of the following but neither is working.

 

                element.get_Parameter(BuiltInParameter.PHASE_CREATED).Set(phaseCreated.Id);
                element.get_Parameter(BuiltInParameter.PHASE_CREATED).SetValueString("Existing");

I've looked at many threads including the ones below but nothing works. 

https://forums.autodesk.com/t5/revit-api-forum/setting-an-elements-phase/m-p/6224664#M15088

https://thebuildingcoder.typepad.com/blog/2010/09/filter-for-view-and-phase.html

Thanks for the help.

 

 

 

0 Likes
3,751 Views
13 Replies
Replies (13)
Message 2 of 14

jeremytammik
Autodesk
Autodesk

Dear Bernie,

 

Thank you for your query.

 

Have you tried setting an element's phase created parameter manually through the user interface and monitoring what effect that has on its parameters via the API, e.g., using RevitLookup?

 

What do you observe when doing that?

 

Best regards,

 

Jeremy

 



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

0 Likes
Message 3 of 14

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @62BJW ,

To set the PhaseCreated parameter first filter for the "PhaseName" and try to set the phase to the element as mentioned in the code

 Element yourElement;
                FilteredElementCollector collector = new FilteredElementCollector(doc).OfClass(typeof(Phase));               
                var phases = from Phase phase in collector where phase.Name.Contains("Existing") select phase;
                ElementId phaseID = phases.First().Id;     
                
                //Set phaseId 
                yourElement.CreatedPhaseId = phaseID;

I hope this helps.


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

Message 4 of 14

62BJW
Advocate
Advocate

I can see that there is a parameter called CreatedPhaseId but its value is not consistent between files.

 

For example in one file the CreatedPhaseID value is <Phase  New Construction  118390> and in another it's value is <Phase  New Construction  109562>

0 Likes
Message 5 of 14

62BJW
Advocate
Advocate

Thanks for the reply Naveen. Not sure I completely understand the code you provided. Won't the FilteredElementCollector get elements that are set to "Existing"? I'm looking to get all "New Construction" and set it to "Existing". I can change it around but I'm not sure what element.CreatedPhaseId = phaseID; is attempting to do. If the collector is finding elements of a particular phase isn't phaseID set to the same phase and therefore not changing it? 

0 Likes
Message 6 of 14

jeremytammik
Autodesk
Autodesk

Dear Bernie,

 

Thank you for your update.

 

I guess phases get created as you go along and as they are needed one by one, so therefore you will find they have different phase ids in different models.

 

That is why Naveen suggests you retrieve the desired phase from the current BIM collection of phases first, in one collector, determine its id, and use that to set the desired value on the BIM element parameters.

 

What part of this are you unclear about?

 

Best regards,

 

Jeremy

 



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

0 Likes
Message 7 of 14

62BJW
Advocate
Advocate

Thanks Jeremy,

I think I understand the collector but I’m unclear how to actually change the phase of the elements in my foreach loop.

0 Likes
Message 8 of 14

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @62BJW ,

1)In your project, Filter for the phase named as "Existing".(as I suggested in my previous comments)

2)Get the "Existing" phase ID(It doesn't matter even if your PhaseId changes for every project because from the phase name you are getting the ID so your ID won't go wrong)

 

FilteredElementCollector collector = new FilteredElementCollector(doc).OfClass(typeof(Phase));
var phases = from Phase phase in collector where phase.Name.Contains("Existing") select phase;
ElementId existingPhaseID = phases.First().Id;

3)using FilteredElementCollector(let's say elementslist), collect all your elements in your project or use appropriate filters to collect particular elements present inside the project to change the Phasecreated property.

 

4)use foreach loop and iterate over each element and check if the element's phase is "existing phase", if not use the below code to change the Phasecreated property

foreach(Element e in elementsList)
                {
                    if (e.CreatedPhaseId != existingPhaseID)
                    {
                        e.CreatedPhaseId = existingPhaseID;
                    }
                }

where "elementslist" is the list of elements collected using the filteredelementcollector.

 

1)I'm not sure what element.CreatedPhaseId = phaseID???

ans:This is the way to change or set your phase name to "Existing" using the phaseId of existing phase.

 

I hope this helps and even if you have any doubts feel free to post your questions.


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

Message 9 of 14

62BJW
Advocate
Advocate

Thanks for the help Naveen.

 

"3)using FilteredElementCollector(let's say elementslist), collect all your elements in your project or use appropriate filters to collect particular elements present inside the project to change the Phasecreated property."

 

Are you saying change collector to elementlist? See attached image.

0 Likes
Message 10 of 14

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @62BJW ,

Yes, what you are doing is correct but normally I collect the elements via filteredelementcollector and add it to a LIST and I always use List elements.

Lets say suppose you want ElementID instead of elements then you have to use IList<> or Icollection<> which will help you to save your elements in list as Elementids.


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 11 of 14

jeremytammik
Autodesk
Autodesk

You should always avoid converting from a filtered element collector to a List if you are not forced to do so for some reason:

 

 

It is a total waste of memory and processing time.

 

It also forces the filtered element collector to retrieve all the elements in one go, which may be extremely undesirable in a large model.

 

 

Cheers,

 

Jeremy

 



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

Message 12 of 14

62BJW
Advocate
Advocate

Thanks Naveen and Jeremy for the replies. Sounds like I should not add element from the collector to a list in this case correct? If so I;m getting an error when I run the code. Please see attached image. Thanks.

0 Likes
Message 13 of 14

62BJW
Advocate
Advocate

After experimenting a bit and stepping through the code I found that the FilteredElementCollector is collecting the phase names in the model not elements with a particular phase ("Existing"). How do I tweak it to collect the elements with "Phase Created" set to "Existing"? Thanks.

 

            FilteredElementCollector elementslist = new FilteredElementCollector(doc).OfClass(typeof(Phase));
            var phases = from Phase phase in elementslist where phase.Name.Contains("Existing") select phase;
            ElementId existingPhaseID = phases.First().Id;
0 Likes
Message 14 of 14

stever66
Advisor
Advisor

Try this;

https://thebuildingcoder.typepad.com/blog/2010/09/filter-for-view-and-phase.html

 

And then try removing the doc.ActiveView.Id from the following;

 

 

FilteredElementCollector collector 
      = new FilteredElementCollector( 
        doc, doc.ActiveView.Id );

 

0 Likes