angelrps
angelrps

Reputation: 77

How can I check if a family instance exists in a given phase?

I am trying to write a function that tells me if a family instance exists in a specific phase. Something like this:

public static bool FamilyExists(FamilyInstance fi, Phase phase)

And returns true or false. With "exists" I mean that it has been created prior to the input phase and it hasn´t been demolished yet.

The problem I see is that I can only get CreatedPhaseId and DemolishedPhaseId from the element. As the input phase can be different to these two, it is not enough to get the info I need. Ideally I would like the API to provide a property "ElementExists" or similar. I have been searching internet for solutions with no luck.

I would be very gratefull if you could help me out.

Thanks.

Upvotes: 0

Views: 621

Answers (2)

Matt
Matt

Reputation: 1096

Much simpler. How about using Element.GetPhaseStatus(). You give it a phase and it tells you the status of the element on that phase?

Upvotes: 2

Vahdet
Vahdet

Reputation: 43

The parameter you are looking for is the builtin parameter "PHASE_SEQUENCE_NUMBER"

Here is an example for "FamilyExistsInPhase":

public static bool FamilyExistsInPhase(Document doc, FamilyInstance fi, Phase phase)
{
    bool exists = true;
            
    var seqNumber = GetPhaseSequenceNumber(phase);

    var createdParam = fi.get_Parameter(BuiltInParameter.PHASE_CREATED);
    var createdPhaseId = createdParam.AsElementId();
    var createdPhase = doc.GetElement(createdPhaseId) as Phase;
    var createdPhaseSeqNumber = GetPhaseSequenceNumber(createdPhase);

    var demolParam = fi.get_Parameter(BuiltInParameter.PHASE_DEMOLISHED);
    var demolPhaseId = demolParam.AsElementId();
    Phase demolPhase = (demolPhaseId != null) ? doc.GetElement(demolPhaseId) as Phase : null;
    // if demolished phase equals null --> element won't be demolished --> set to high number
    var demolPhaseSeqNumber = (demolPhase != null) ? GetPhaseSequenceNumber(demolPhase) : 1000;

    // if the element is constructed before or in same phase
    if (createdPhaseSeqNumber <= seqNumber)
    {
        // if it gets demolished later
        // or not at all if 1000
        if (demolPhaseSeqNumber > seqNumber)
        {
            exists = true;
        }
    }

    return exists;
}

public static int GetPhaseSequenceNumber(Phase p)
{
    return p.get_Parameter(BuiltInParameter.PHASE_SEQUENCE_NUMBER).AsInteger();
}

i hope this works for you. not the best solution but it does the job.

Upvotes: 0

Related Questions