[CONNECT U8] SystemCallback::SetElmDscrToFileFunction() and ItemTypes

I am using SystemCallback::SetElmDscrToFileFunction() to catch when a user creates new elements. My "command" establishes the callback function and then open a DialogBox where one of any number of ItemTypes can be selected. As new elements are created, the desire is to add the selected ItemType to the "new" element automatically. The callback function provides the "proposed" MSElementDescrP and a "replacement" MSElementDescrPP for when you return ELMDTF_STATUS_REPLACE. I am unable to add the ItemType successfully to the replacement MSElementDescrPP. I'm thinking it's because the EditElementHandle created from the "proposed" MSElementDescrP (newEdP), although "IsValid()", is not persistent. I get a MicroStation crash. Is there another way to attach the ItemType ?

static		ItemTypeLibraryPtr		itemTypeLib;

ElmDscrToFile_Status		AddFeatureItemOld(ElmDscrToFile_Actions action, 
											DgnModelRefP modelRef, 
											UInt32 filePos, 
											MSElementDescrP newEdP,			// new elem being written to file (NULL when ELMDTF_ACTION_DELETE)
											MSElementDescrCP oldEdP,		// elem in original state (NULL when ELMDTF_ACTION_APPEND)
											MSElementDescrH replacementEdPP)// used if we return ELMDTF_STATUS_REPLACE
{
    // ...
    
    EditElementHandle				eeh(newEdP, false, false);	// eeh does NOT own the elementDescr
    WString				            itemTypeName(L"Air Tunnel");
    LookForItemTypes(itemTypeName);		// searches for ItemTypeLibrary with "Air Tunnel",sets "itemTypeLib"
    if (itemTypeLib.IsValid())
    {
    	wprintf(L"itemTypeLib.IsValid()\n");
    	ItemTypeP			pItemType = itemTypeLib->GetItemTypeByName(itemTypeName.c_str() );
    	if (pItemType != nullptr)
    	{
    		wprintf(L"pItemType != nullptr\n");
    		wprintf(L"eeh.IsValid %d\n", eeh.IsValid());
    		wprintf(L"eeh.IsPersistent %d\n", eeh.IsPersistent());
    
    		CustomItemHost				itemHost = CustomItemHost(eeh);
    		wprintf(L"itemHost %08x\n", itemHost);
    		DgnECInstancePtr			pInstance = itemHost.ApplyCustomItem(*pItemType, true); //CRASHES Here
    		wprintf(L"pInstance.IsValid %d\n", pInstance.IsValid());    // Never gets to here...
    	}
    	else
    	{
    		wprintf(L"pItemType == nullptr\n");
    	}
    }
    else
    {
    	wprintf(L"NOT itemTypeLib.IsValid()\n");
    }
    mdlElmdscr_duplicate(replacementEdPP, eeh.GetElementDescrP());
    return ELMDTF_STATUS_REPLACE;	// use the update element descriptor
}    

Bruce

Parents Reply Children