[MS C++]设置参考模型的EC属性时,若存在该EC属性,可以修改;若不存在该EC属性,需要新增,则失败。请问怎么解决?

老师新年好,问题如题,代码如下:

bool CElementTool::SetElementEC(EditElementHandleR eeh, const WString & libName, const ElementEC& ec, DgnModelRefP model)
{
	auto mdoelId = model->GetDgnModelP()->GetModelId();
	DgnFileP dgnfile = model->GetDgnFileP();
	auto setEC = [&](EditElementHandleR eeh)->bool 
	{	
		ElementRefP oldref = eeh.GetElementRef();
		ItemTypeLibraryPtr itemTypeLibrary = ItemTypeLibrary::FindByName(libName.c_str(), *dgnfile);
		if (itemTypeLibrary == NULL)
		{
			itemTypeLibrary = ItemTypeLibrary::Create(libName.c_str(), *dgnfile);
		}

		ItemTypeP itemType = itemTypeLibrary->GetItemTypeByName(libName.GetWCharCP());
		if (itemType == NULL)
		{
			itemType = itemTypeLibrary->AddItemType(libName.GetWCharCP());
		}

		CustomPropertyP Custype = itemType->GetPropertyByName(ec.key.GetWCharCP());
		if (Custype == NULL)
		{
			Custype = itemType->AddProperty(ec.key.GetWCharCP());
			Custype->SetType(ec.valType);
		}
		
		itemTypeLibrary->Write();
		CustomItemHost itemHost(eeh, true);  // true = schedule changed
		DgnECInstancePtr pInstance = itemHost.ApplyCustomItem(*itemType);
		if (pInstance == nullptr)
		{
			//pInstance = itemHost.ApplyCustomItem(*itemType, true);
			pInstance = itemHost.GetCustomItem(libName.GetWCharCP(), libName.GetWCharCP());
			if (pInstance == nullptr)
			{
				return false;
			}
		}
		WString encodedProp;
		ECN::ECNameValidation::EncodeToValidName(encodedProp, ec.key);
		pInstance->SetValue(encodedProp.GetWCharCP(), ec.val);
		pInstance->ScheduleWriteChanges(eeh);
		eeh.ReplaceInModel(oldref);
		return true;
	};
	//如果不是参考模型文件,直接设置
	DgnAttachmentP dgnAtta = *ACTIVEMODEL->GetDgnAttachmentsP()->begin();
	if (dgnAtta->GetDgnModelP()->GetDgnFileP() == ISessionMgr::GetActiveDgnFile())
	{		
		return setEC(eeh);
	}
	//如果是参考
	else
	{
		//打开参考dgn文件
		DgnFileStatus dgnFileStatus; int defFileId = 0;
		DgnDocumentPtr dgnDoc = DgnDocument::CreateFromFileName(dgnFileStatus, dgnfile->GetFileName().data(),
			NULL, defFileId, DgnDocument::FetchMode::Write);
		DgnFilePtr dgnFilePtr = DgnFile::Create(*dgnDoc, DgnFileOpenMode::ReadWrite);
		if (dgnFilePtr.IsNull())
			return false;
			StatusInt openForWriteStatus;
		dgnFilePtr->LoadDgnFile(&openForWriteStatus);
		if (SUCCESS != openForWriteStatus)
			return false;
		StatusInt status = dgnFilePtr->FillDictionaryModel();
		if (SUCCESS != status)
		{
			return false;
		}
		//从打开的参考dgn中找当前参考模型
		//ModelId mdoelId = dgnAtta->GetDgnModelP()->GetModelId();
		DgnModelPtr dgnModelPtr = dgnFilePtr->LoadModelById(mdoelId);
		dgnFilePtr->FillSectionsInModel(dgnModelPtr.get());

		//在模型中找需要设置的元素
		auto perEleRef = dgnModelPtr->FindElementByID(eeh.GetElementId());
		EditElementHandle tmpEeh(perEleRef);
		//设置属性
		bool res = setEC(tmpEeh);
		dgnFilePtr->ProcessChanges(DgnSaveReason::FileClose);
		//重新加载
		DgnModelP pModel = ISessionMgr::GetActiveDgnModelP();
		DgnAttachmentLoadOptions options(true, true, true);
		pModel->ReadAndLoadDgnAttachments(options);  //Important for read ref info from an external dgn file
		DgnAttachmentArrayCP pAttArray = pModel->GetDgnAttachmentsCP();
		for (DgnAttachmentP pAtt : *pAttArray)
		{
			mdlRefFile_reload(pAtt, true, true);
		}
		return res;
	}
	return true;
}

调试是在这个位置pInstance为null直接返回了:

Parents Reply Children
No Data