【MSCE C++】读取DgnAttachment的XAttribute

如题,我有一个a.dgn,在a.dgn的file和designmodel上分别写入了xattribute

现在在b.dgn中参考了a.dgn的designmodel。使用以下代码,在b.dgn中无法读取xattribute。

DgnAttachmentP att = ACTIVEMODEL->GetDgnAttachmentsCP()->at(0);
DgnFileP dgnFileP = att->GetDgnFileP();
ApplicationSettings appSet = IDgnSettings::GetCurrentSettings().GetFileApplicationSettings(*dgnFileP);
//ApplicationSettings appSet = IDgnSettings::GetCurrentSettings().GetModelApplicationSettings(*att);
//改成读取model也不行
XAttributeHandlerId xAttHandlerId(100, 1);

ElementRefP elRef = appSet.GetElementRef();
XAttributeHandle xah(elRef, xAttHandlerId, 3);

if (xah.IsValid())//这里判断无效,无法读取
{
	char* chars = (char*)xah.PeekData();
}

看起来GetCurrentSettings只对当前dgn有效?

请问以上需求该如何实现?

Parents
  • 试试调用一下DgnFile的LoadDgnFile和FillDictionaryModel

  • 两个方法均返回success,但结果还是一样xah.IsValid()返回false

  • 我用如下代码测试完全没有问题,测试步骤在被参考的文件中函数参数传递0完成添加属性的操作,然后在主文件中参数传递1能够读取到添加的属性:

    void applicationSettingTest(int condition)
    {
    	/*DgnAttachmentP dgnAtta = *ACTIVEMODEL->GetDgnAttachmentsP()->begin();
    	ApplicationSettings appSet = IDgnSettings::GetCurrentSettings().GetModelApplicationSettings(*dgnAtta);*/
    	ApplicationSettings appSet = IDgnSettings::GetCurrentSettings().GetModelApplicationSettings(*ACTIVEMODEL);
    	XAttributeHandlerId handlerId(900, 800);  //MajorId, MinorId
    	UInt32              xattrId = 1;
    	char *appData = "My Application Data";
    	switch (condition)
    	{
    	case 0:  //--Create ApplicationSetting
    		appSet.SaveSetting(handlerId, xattrId, appData, (UInt32)strlen(appData));
    		break;
    	case 1:  //--Display ApplicationSetting
    	{
    		ElementRefP elRef = appSet.GetElementRef();
    		ElementId  elId = elementRef_getElemID(elRef);
    		printf("%d\n", elId);
    
    		XAttributeHandle xah(elRef, handlerId, xattrId);
    		if (xah.IsValid())
    		{
    			printf("%s\n", xah.PeekData());
    		}
    		break;
    	}
    	case 2:  //--Remove ApplicationSetting
    	{
    		ElementRefP elRef = appSet.GetElementRef();
    		XAttributeHandle xah(elRef, handlerId, xattrId);
    		if (xah.IsValid())
    		{
    			ITxnManager::GetManager().GetCurrentTxn().DeleteXAttribute(xah);
    		}
    		break;
    	}
    	}
    }

    Answer Verified By: Chao Cheng 

Reply
  • 我用如下代码测试完全没有问题,测试步骤在被参考的文件中函数参数传递0完成添加属性的操作,然后在主文件中参数传递1能够读取到添加的属性:

    void applicationSettingTest(int condition)
    {
    	/*DgnAttachmentP dgnAtta = *ACTIVEMODEL->GetDgnAttachmentsP()->begin();
    	ApplicationSettings appSet = IDgnSettings::GetCurrentSettings().GetModelApplicationSettings(*dgnAtta);*/
    	ApplicationSettings appSet = IDgnSettings::GetCurrentSettings().GetModelApplicationSettings(*ACTIVEMODEL);
    	XAttributeHandlerId handlerId(900, 800);  //MajorId, MinorId
    	UInt32              xattrId = 1;
    	char *appData = "My Application Data";
    	switch (condition)
    	{
    	case 0:  //--Create ApplicationSetting
    		appSet.SaveSetting(handlerId, xattrId, appData, (UInt32)strlen(appData));
    		break;
    	case 1:  //--Display ApplicationSetting
    	{
    		ElementRefP elRef = appSet.GetElementRef();
    		ElementId  elId = elementRef_getElemID(elRef);
    		printf("%d\n", elId);
    
    		XAttributeHandle xah(elRef, handlerId, xattrId);
    		if (xah.IsValid())
    		{
    			printf("%s\n", xah.PeekData());
    		}
    		break;
    	}
    	case 2:  //--Remove ApplicationSetting
    	{
    		ElementRefP elRef = appSet.GetElementRef();
    		XAttributeHandle xah(elRef, handlerId, xattrId);
    		if (xah.IsValid())
    		{
    			ITxnManager::GetManager().GetCurrentTxn().DeleteXAttribute(xah);
    		}
    		break;
    	}
    	}
    }

    Answer Verified By: Chao Cheng 

Children