[CONNECT C++] StandaloneECInstance and CreateECInstanceHolderElement

This code successfully creates a StandaloneECInstance, to which I attach some Item Type properties...

DgnElementECInstancePtr	AggregateHolder::CreateInstanceAsElement (AggregateCollectionCR aggregates)
{
    DgnElementECInstancePtr  instance;
    DgnECManagerR            dgnECManager = DgnECManager::GetManager ();
    WString		     aggregatorName	{ ItemTypes::AggregatorTypeName ().c_str () };
    DgnECInstanceEnablerP    enabler      = dgnECManager.ObtainInstanceEnablerByName (ItemTypes::SchemaInternalName ().c_str (), aggregatorName.c_str (), *Utilities::GetActiveDgnFile ());
    if (enabler->SupportsCreateInstanceAsElement ())
    {
        StandaloneECInstanceR sharedInstance = enabler->GetSharedWipInstance ();
        for (ItemDataCR	data: aggregates.Data ())
        {
            ECValue          value  (data.ECValue ());
            ECObjectsStatus  status  { sharedInstance.SetValue (data.Name ().c_str (), value) };
        }
        const bool InstanceOwnsElement	{ true };
        DgnECInstanceStatus status = enabler->CreateInstanceAsElement (&instance, sharedInstance, *Utilities::GetActiveDgnModelRefP (), !InstanceOwnsElement);
        DescribeDgnECInstanceStatus (status);
    }
    return instance;
}

The purpose of the StandaloneECInstance is to store some aggregate properties calculated from a set of Item Type instances.

However, I'm doing something wrong.  Whenever I run this code I seem to be attaching a new copy of my Item Type instead of replacing the existing property values (this shows as extra rows when I create a report).

Aggregator extra unwanted rows

Is that because I'm using CreateInstanceAsElement, which creates a new StandaloneECInstance each time?  Should I instead be retrieving the existing StandaloneECInstance?  If so, how?

  • Can you clarify whether by 'Item Type' you mean "an Item Type defined/viewable in MicroStation's Item Types dialog" or just 'some ECInstance'?

    CreateInstanceAsElement() makes a brand-new element and sticks your ECInstance onto it.

    If you know an ECInstance is already attached to a given element, you can use ReplaceInstanceOnElement() to replace it with a new one.

    This presupposes that you somehow know on which element to look for the existing ECInstance e.g., by pulling it out of the DgnElementECInstance created by CreateInstanceAsElement() or querying for the ECInstance in some scope in which you expect to find at most one.

  • Can you clarify whether by 'Item Type' you mean "an Item Type defined/viewable in MicroStation's Item Types dialog" or just 'some ECInstance'?

    The Item Type definition is visible in MicroStation's Item Types dialog.

    look for the existing ECInstance e.g., by pulling it out of the DgnElementECInstance created by CreateInstanceAsElement() or querying for the ECInstance in some scope in which you expect to find at most one

    I'm seeking to attach a single ECInstance, following your earlier suggestion.  If I search the appropriate scope and find an existing instance, I want to ReplaceInstanceOnElement.

    CreateInstanceAsElement() makes a brand-new element and sticks your ECInstance onto it

    Shouldn't I use ECInstanceHolderHandler::CreateECInstanceHolderElement() to make a stand-alone type66 element? If I've created that element, how do I use it with StandaloneECInstance?

    I'm fumbling in the dark attempting to find it, but you've helped by shining a light.

     
    Regards, Jon Summers
    LA Solutions

  • CreateInstanceAsElement() makes a brand-new element and sticks your ECInstance onto it.

    If you know an ECInstance is already attached to a given element, you can use ReplaceInstanceOnElement() to replace it with a new one

    I got it working: here's the result so far...

    aggregate report

    Although it's impossible to tell, the above Report is created from an Item Type attached to an invisible (to the user) Instance Holder Element.

     
    Regards, Jon Summers
    LA Solutions

    Answer Verified By: Jon Summers