The Global ARCHICAD Community

Stay informed. Get help. Share your knowledge.

Discussions about using GRAPHISOFT's tools (API DevKit) for independent software developers

Moderators: Karl Ottenstein, LaszloNagy, ejrolon, Barry Kelly, gkmethy, Tibor Lorántfy, rmasaki, MOREH Tamas, Akos Somorjai, Ed Brown, Mihály Palenik

#304489
Hi.
The access violation error occuers when I create a material attribute.
my code is
Code: Select all
API_AttrTypeID typeID = API_MaterialID;
API_Attribute		attrib;
	API_AttributeDefExt	defs;
	BNZeroMemory(&attrib, sizeof(API_Attribute));
attrib.header.typeID = typeID;
	strcpy(attrib.header.name, u8"玻璃 - 蓝色");
	err = ACAPI_Attribute_Get(&attrib);
	if (err == NoError) {
		err = ACAPI_Attribute_GetDefExt(typeID, attrib.header.index, &defs);
		if (err == APIERR_BADID) {
			BNZeroMemory(&defs, sizeof(API_AttributeDefExt));
			err = NoError;
		}
		//能见度100
		attrib.material.transpPc = 100;
		if (err == NoError) {
			strcpy(attrib.header.name, name.ToCStr().Get());
			err = ACAPI_Attribute_Create(&attrib,nullptr);
		}
		ACAPI_DisposeAttrDefsHdlsExt(&defs);
	}
Snipaste_2019-12-03_19-43-34.png
Snipaste_2019-12-03_19-43-34.png (8.82 KiB) Viewed 73 times
#304503
Hi,

Please use attrib.header.uniStringNamePtr Instead of attrib.header.name, because the .name is limited in length:
Code: Select all
GS::UniString attrName (u8"玻璃 - 蓝色");
attrib.header.uniStringNamePtr = &attrName;
and instead of strcpy(attrib.header.name, name.ToCStr().Get()); use:
Code: Select all
attrib.header.uniStringNamePtr = &name;
Best, Akos
#304524
Akos Somorjai wrote:
Tue Dec 03, 2019 2:43 pm
Hi,

Please use attrib.header.uniStringNamePtr Instead of attrib.header.name, because the .name is limited in length:
Code: Select all
GS::UniString attrName (u8"玻璃 - 蓝色");
attrib.header.uniStringNamePtr = &attrName;
and instead of strcpy(attrib.header.name, name.ToCStr().Get()); use:
Code: Select all
attrib.header.uniStringNamePtr = &name;
Best, Akos
According to your method,the problem also occuers.
Snipaste_2019-12-04_09-02-55.png
#304548
DevJeer wrote:
Wed Dec 04, 2019 2:08 am
Akos Somorjai wrote:
Tue Dec 03, 2019 2:43 pm
Hi,

Please use attrib.header.uniStringNamePtr Instead of attrib.header.name, because the .name is limited in length:
Code: Select all
GS::UniString attrName (u8"玻璃 - 蓝色");
attrib.header.uniStringNamePtr = &attrName;
and instead of strcpy(attrib.header.name, name.ToCStr().Get()); use:
Code: Select all
attrib.header.uniStringNamePtr = &name;
Best, Akos
According to your method,the problem also occuers.
I notice you've changed the call for creating the material to ACAPI_Attribute_CreateExt and you're passing a API_AttributeDefExt parameter. Materials don't use API_AttributeDefExt, so perhaps you should be passing just nullptr for that parameter?
#304568
Hi,

Here's the code that works for me:
Code: Select all
{
	GS::UniString name (u8"能见度100");

	API_AttrTypeID typeID = API_MaterialID;
	API_Attribute attrib = {};
	API_AttributeDefExt defs = {};
	GSErrCode err = NoError;

	GS::UniString attrName(u8"玻璃 - 蓝色");
	attrib.header.typeID = typeID;
	attrib.header.uniStringNamePtr = &attrName;
	//strcpy(attrib.header.name, u8"玻璃 - 蓝色");
	err = ACAPI_Attribute_Get (&attrib);
	if (err == NoError)
	{
		err = ACAPI_Attribute_GetDefExt (typeID, attrib.header.index, &defs);
		if (err == APIERR_BADID)
		{
			err = NoError;
		}
		//能见度100
		attrib.material.transpPc = 100;
		if (err == NoError)
		{
			attrib.header.uniStringNamePtr = &name;
			//strcpy(attrib.header.name, name.ToCStr().Get());
			err = ACAPI_Attribute_CreateExt (&attrib, &defs);
		}
	}
	ACAPI_DisposeAttrDefsHdlsExt(&defs);

	return;
}
I think the main problem was calling ACAPI_Attribute_Create instead of ACAPI_Attribute_CreateExt.

Best, Akos
#304587
Thanks.
The code don't work for me.
Code: Select all
API_AttrTypeID typeID = API_MaterialID;
GSErrCode err = NoError;
API_Attribute attrib={};
API_AttributeDefExt defs={};

attrib.header.typeID = typeID;
//strcpy(attrib.header.name, u8"玻璃 - 蓝色");
GS::UniString materialName(u8"玻璃 - 蓝色");
attrib.header.uniStringNamePtr = &materialName;
err = ACAPI_Attribute_Get(&attrib);
if (err == NoError) {
    err = ACAPI_Attribute_GetDefExt(typeID, attrib.header.index, &defs);
    if (err == APIERR_BADID) {
        BNZeroMemory( & defs, sizeof(API_AttributeDefExt));
        err = NoError;
    }
    //能见度100
    attrib.material.transpPc = 100;
    if (err == NoError) {
        //strcpy(attrib.header.name, name.ToCStr().Get());
        GS::UniString attrName(u8"界线");
        attrib.header.uniStringNamePtr = &attrName;
        err = ACAPI_Attribute_CreateExt( &attrib, &defs);
    }
    ACAPI_DisposeAttrDefsHdlsExt(&defs);
}
The same problem also occurs.
The version of Archicad is 22 and the version of vs is 2015.