<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic bug in program API 21.22 ACAPI_Element_Create in Archicad C++ API</title>
    <link>https://community.graphisoft.com/t5/Archicad-C-API/bug-in-program-API-21-22-ACAPI-Element-Create/m-p/290602#M3636</link>
    <description>&lt;DIV class="actalk-migrated-content"&gt;Hello, help me find a solution, the ACAPI_Element_Create function in the Archicad version 21, 22 does not work for the zone surrounded by walls. Finding an error on my own led to the fact that I created a program that can be inserted into a standard “Geometry Test”. Result of experiments up to version 21 everything works, in 21 and 22 everything works except for the zone surrounded by walls, (the zone of lines and the zone with manual construction works) the result of work on the debager in version 21 or 22 crash somewhere in the Archicad module itself. Just in case I will give all the code.
&lt;PRE&gt;&lt;I&gt;
&lt;/I&gt;// *****************************************************************************
// Source code for the Geometry Test Add-On
// API Development Kit 21; Mac/Win
//
// Namespaces:		Contact person:
//	-None-
//
// [SG compatible] - Yes
// *****************************************************************************

#include "APIEnvir.h"
#define	_Geometry_Test_TRANSL_


// ---------------------------------- Includes ---------------------------------

//#include	&amp;lt;stdio&amp;gt;
#include	&amp;lt;string&amp;gt;

#include	"ACAPinc.h"					// also includes APIdefs.h
#include	"APICommon.h"
#include	"basicgeometry.h"

// ---------------------------------- Types ------------------------------------


// ---------------------------------- Variables --------------------------------


// ---------------------------------- Prototypes -------------------------------
bool GetZoneElement(API_Guid	 &amp;amp;OutGuid)
{
	API_SelectionInfo 	selectionInfo; // Get selection
	API_Neig			**selNeig; // Get selection
	GSErrCode			err;
	UInt32		nSel;
	UInt32		k = 0;

	err = ACAPI_Selection_Get(&amp;amp;selectionInfo, &amp;amp;selNeig, true);
	BMKillHandle((GSHandle *)&amp;amp;selectionInfo.marquee.coords); 

	if (err != NoError &amp;amp;&amp;amp; err != APIERR_NOSEL) {	
		ErrorBeep("ACAPI_Selection_GetInfo", err);  
		BMKillHandle((GSHandle *)&amp;amp; selNeig);
		return false;
	}
	if (selectionInfo.typeID != API_SelEmpty) {		
		nSel = BMGetHandleSize((GSHandle)selNeig) / sizeof(API_Neig);
		for (UInt32 i = 0; i &amp;lt; nSel &amp;amp;&amp;amp; err == NoError; i++) {		
			if (!ACAPI_Element_Filter((*selNeig)&lt;I&gt;.guid, APIFilt_IsEditable))
				continue;
			OutGuid = (*selNeig)&lt;I&gt;.guid; 
			WriteReport("Guid select: %s", static_cast&amp;lt;const char *&amp;gt; (APIGuid2GSGuid(OutGuid).ToUniString().ToCStr()));
			++k;
		}
	} 
	if (k == 0)
		WriteReport("No select %ld", k);
	BMKillHandle((GSHandle *)&amp;amp;selNeig);
	return k &amp;gt; 0 ? true : false;
}

GSErrCode	ModifyZonePos(const API_Guid&amp;amp; guid)
{
	API_Element			element;
	API_ElementMemo		memo;
	GSErrCode			err;

	BNZeroMemory(&amp;amp;element, sizeof(API_Element));
	BNZeroMemory(&amp;amp;memo, sizeof(API_ElementMemo));

	// get actual zone
	element.header.guid = guid;

	err = ACAPI_Element_Get(&amp;amp;element);
	if (err == NoError)
		err = ACAPI_Element_GetMemo(element.header.guid, &amp;amp;memo);
	if (err != NoError)
		return err;
	// delete original zone, before create a new zone, if not want get alert
	API_Elem_Head** heads = (API_Elem_Head**)BMhAllClear(sizeof(API_Elem_Head));
	(*heads)[0].guid = guid; // 0  -1 zones
	ACAPI_Element_Delete(heads, 1);
	BMhKill((GSHandle*)&amp;amp;heads);

	// modify pos and create a new zone
	WriteReport("Create %s:  %s", (ElemID_To_Name (element.header.typeID)).ToCStr().Get(), static_cast&amp;lt;const char *&amp;gt; (APIGuid2GSGuid(guid).ToUniString().ToCStr()));
	err = 	ACAPI_Element_Create (&amp;amp;element, &amp;amp;memo);
	ACAPI_DisposeElemMemoHdls(&amp;amp;memo);
	return err;
}

// =============================================================================
//
// Main functions
//
// =============================================================================


static void		Do_Test (void)
{

	ACAPI_CallUndoableCommand ("Geometry Test -- Create elements",
		[&amp;amp;] () -&amp;gt; GSErrCode {

		GSErrCode err;
		API_Guid  guid;

		GetZoneElement (guid);
		err = ModifyZonePos(guid);
			return err;
		});

	return;
}		/* Do_Test */

// -----------------------------------------------------------------------------
// Entry points to handle Archicad events
//
// -----------------------------------------------------------------------------

GSErrCode __ACENV_CALL	MenuCommandHandler (const API_MenuParams *params)
{
	switch (params-&amp;gt;menuItemRef.itemIndex) {
		case 1:		Do_Test ();				break;
	}

	return NoError;
}		// DoCommand


// =============================================================================
//
// Required functions
//
// =============================================================================


//------------------------------------------------------
// Dependency definitions
//------------------------------------------------------
API_AddonType	__ACENV_CALL	CheckEnvironment (API_EnvirParams* envir)
{
	RSGetIndString (&amp;amp;envir-&amp;gt;addOnInfo.name, 32000, 1, ACAPI_GetOwnResModule ());
	RSGetIndString (&amp;amp;envir-&amp;gt;addOnInfo.description, 32000, 2, ACAPI_GetOwnResModule ());

	return APIAddon_Normal;
}		/* CheckEnvironment */


//------------------------------------------------------
// Interface definitions
//------------------------------------------------------
GSErrCode	__ACENV_CALL	RegisterInterface (void)
{
	ACAPI_Register_Menu (32500, 0, MenuCode_UserDef, MenuFlag_Default);

	return NoError;
}		/* RegisterInterface */


//------------------------------------------------------
// Called when the Add-On has been loaded into memory
// to perform an operation
//------------------------------------------------------
GSErrCode	__ACENV_CALL Initialize	(void)
{
	GSErrCode err = ACAPI_Install_MenuHandler (32500, MenuCommandHandler);
	if (err != NoError)
		DBPrintf ("Geometry_Test:: Initialize() ACAPI_Install_MenuHandler failed\n");

	return err;
}		/* Initialize */


// -----------------------------------------------------------------------------
// FreeData
//		called when the Add-On is going to be unloaded
// -----------------------------------------------------------------------------

GSErrCode __ACENV_CALL	FreeData (void)
{
	return NoError;
}		// FreeData
&lt;/I&gt;&lt;/I&gt;&lt;/PRE&gt;
&lt;/DIV&gt;</description>
    <pubDate>Tue, 04 Oct 2022 14:35:36 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2022-10-04T14:35:36Z</dc:date>
    <item>
      <title>bug in program API 21.22 ACAPI_Element_Create</title>
      <link>https://community.graphisoft.com/t5/Archicad-C-API/bug-in-program-API-21-22-ACAPI-Element-Create/m-p/290602#M3636</link>
      <description>&lt;DIV class="actalk-migrated-content"&gt;Hello, help me find a solution, the ACAPI_Element_Create function in the Archicad version 21, 22 does not work for the zone surrounded by walls. Finding an error on my own led to the fact that I created a program that can be inserted into a standard “Geometry Test”. Result of experiments up to version 21 everything works, in 21 and 22 everything works except for the zone surrounded by walls, (the zone of lines and the zone with manual construction works) the result of work on the debager in version 21 or 22 crash somewhere in the Archicad module itself. Just in case I will give all the code.
&lt;PRE&gt;&lt;I&gt;
&lt;/I&gt;// *****************************************************************************
// Source code for the Geometry Test Add-On
// API Development Kit 21; Mac/Win
//
// Namespaces:		Contact person:
//	-None-
//
// [SG compatible] - Yes
// *****************************************************************************

#include "APIEnvir.h"
#define	_Geometry_Test_TRANSL_


// ---------------------------------- Includes ---------------------------------

//#include	&amp;lt;stdio&amp;gt;
#include	&amp;lt;string&amp;gt;

#include	"ACAPinc.h"					// also includes APIdefs.h
#include	"APICommon.h"
#include	"basicgeometry.h"

// ---------------------------------- Types ------------------------------------


// ---------------------------------- Variables --------------------------------


// ---------------------------------- Prototypes -------------------------------
bool GetZoneElement(API_Guid	 &amp;amp;OutGuid)
{
	API_SelectionInfo 	selectionInfo; // Get selection
	API_Neig			**selNeig; // Get selection
	GSErrCode			err;
	UInt32		nSel;
	UInt32		k = 0;

	err = ACAPI_Selection_Get(&amp;amp;selectionInfo, &amp;amp;selNeig, true);
	BMKillHandle((GSHandle *)&amp;amp;selectionInfo.marquee.coords); 

	if (err != NoError &amp;amp;&amp;amp; err != APIERR_NOSEL) {	
		ErrorBeep("ACAPI_Selection_GetInfo", err);  
		BMKillHandle((GSHandle *)&amp;amp; selNeig);
		return false;
	}
	if (selectionInfo.typeID != API_SelEmpty) {		
		nSel = BMGetHandleSize((GSHandle)selNeig) / sizeof(API_Neig);
		for (UInt32 i = 0; i &amp;lt; nSel &amp;amp;&amp;amp; err == NoError; i++) {		
			if (!ACAPI_Element_Filter((*selNeig)&lt;I&gt;.guid, APIFilt_IsEditable))
				continue;
			OutGuid = (*selNeig)&lt;I&gt;.guid; 
			WriteReport("Guid select: %s", static_cast&amp;lt;const char *&amp;gt; (APIGuid2GSGuid(OutGuid).ToUniString().ToCStr()));
			++k;
		}
	} 
	if (k == 0)
		WriteReport("No select %ld", k);
	BMKillHandle((GSHandle *)&amp;amp;selNeig);
	return k &amp;gt; 0 ? true : false;
}

GSErrCode	ModifyZonePos(const API_Guid&amp;amp; guid)
{
	API_Element			element;
	API_ElementMemo		memo;
	GSErrCode			err;

	BNZeroMemory(&amp;amp;element, sizeof(API_Element));
	BNZeroMemory(&amp;amp;memo, sizeof(API_ElementMemo));

	// get actual zone
	element.header.guid = guid;

	err = ACAPI_Element_Get(&amp;amp;element);
	if (err == NoError)
		err = ACAPI_Element_GetMemo(element.header.guid, &amp;amp;memo);
	if (err != NoError)
		return err;
	// delete original zone, before create a new zone, if not want get alert
	API_Elem_Head** heads = (API_Elem_Head**)BMhAllClear(sizeof(API_Elem_Head));
	(*heads)[0].guid = guid; // 0  -1 zones
	ACAPI_Element_Delete(heads, 1);
	BMhKill((GSHandle*)&amp;amp;heads);

	// modify pos and create a new zone
	WriteReport("Create %s:  %s", (ElemID_To_Name (element.header.typeID)).ToCStr().Get(), static_cast&amp;lt;const char *&amp;gt; (APIGuid2GSGuid(guid).ToUniString().ToCStr()));
	err = 	ACAPI_Element_Create (&amp;amp;element, &amp;amp;memo);
	ACAPI_DisposeElemMemoHdls(&amp;amp;memo);
	return err;
}

// =============================================================================
//
// Main functions
//
// =============================================================================


static void		Do_Test (void)
{

	ACAPI_CallUndoableCommand ("Geometry Test -- Create elements",
		[&amp;amp;] () -&amp;gt; GSErrCode {

		GSErrCode err;
		API_Guid  guid;

		GetZoneElement (guid);
		err = ModifyZonePos(guid);
			return err;
		});

	return;
}		/* Do_Test */

// -----------------------------------------------------------------------------
// Entry points to handle Archicad events
//
// -----------------------------------------------------------------------------

GSErrCode __ACENV_CALL	MenuCommandHandler (const API_MenuParams *params)
{
	switch (params-&amp;gt;menuItemRef.itemIndex) {
		case 1:		Do_Test ();				break;
	}

	return NoError;
}		// DoCommand


// =============================================================================
//
// Required functions
//
// =============================================================================


//------------------------------------------------------
// Dependency definitions
//------------------------------------------------------
API_AddonType	__ACENV_CALL	CheckEnvironment (API_EnvirParams* envir)
{
	RSGetIndString (&amp;amp;envir-&amp;gt;addOnInfo.name, 32000, 1, ACAPI_GetOwnResModule ());
	RSGetIndString (&amp;amp;envir-&amp;gt;addOnInfo.description, 32000, 2, ACAPI_GetOwnResModule ());

	return APIAddon_Normal;
}		/* CheckEnvironment */


//------------------------------------------------------
// Interface definitions
//------------------------------------------------------
GSErrCode	__ACENV_CALL	RegisterInterface (void)
{
	ACAPI_Register_Menu (32500, 0, MenuCode_UserDef, MenuFlag_Default);

	return NoError;
}		/* RegisterInterface */


//------------------------------------------------------
// Called when the Add-On has been loaded into memory
// to perform an operation
//------------------------------------------------------
GSErrCode	__ACENV_CALL Initialize	(void)
{
	GSErrCode err = ACAPI_Install_MenuHandler (32500, MenuCommandHandler);
	if (err != NoError)
		DBPrintf ("Geometry_Test:: Initialize() ACAPI_Install_MenuHandler failed\n");

	return err;
}		/* Initialize */


// -----------------------------------------------------------------------------
// FreeData
//		called when the Add-On is going to be unloaded
// -----------------------------------------------------------------------------

GSErrCode __ACENV_CALL	FreeData (void)
{
	return NoError;
}		// FreeData
&lt;/I&gt;&lt;/I&gt;&lt;/PRE&gt;
&lt;/DIV&gt;</description>
      <pubDate>Tue, 04 Oct 2022 14:35:36 GMT</pubDate>
      <guid>https://community.graphisoft.com/t5/Archicad-C-API/bug-in-program-API-21-22-ACAPI-Element-Create/m-p/290602#M3636</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2022-10-04T14:35:36Z</dc:date>
    </item>
    <item>
      <title>Re: bug in program API 21.22 ACAPI_Element_Create</title>
      <link>https://community.graphisoft.com/t5/Archicad-C-API/bug-in-program-API-21-22-ACAPI-Element-Create/m-p/290603#M3637</link>
      <description>Hi,&lt;BR /&gt;
I believe the issue is not within &lt;A href="http://archicadapi.graphisoft.com/documentation/acapi_element_create?s=element%20create" target="_blank"&gt;ACAPI_Element_Create&lt;/A&gt;, but with the &lt;A href="http://archicadapi.graphisoft.com/documentation/acapi_callundoablecommand?s=undoable" target="_blank"&gt;CallUndoAbleCommand&lt;/A&gt;. The CallUndoAbleCommand scope encapsulates a series of undoable steps, normally database modification operations. In ModifyZonePos you delete the zone element, then you try to create an element with the same guid.  This happens within the same database transaction scope, and causes a collision. &lt;BR /&gt;
To fix the issue, you can use either &lt;A href="http://archicadapi.graphisoft.com/documentation/acapi_element_change?s=element%20change" target="_blank"&gt;ACAPI_Element_Change&lt;/A&gt;, that way you keep the guid, or in ModifyZonePos you can set the element.header.guid to APINULLGuid, since ACAPI_Element_Create fills the guid field when it's empty.</description>
      <pubDate>Wed, 20 Mar 2019 16:34:27 GMT</pubDate>
      <guid>https://community.graphisoft.com/t5/Archicad-C-API/bug-in-program-API-21-22-ACAPI-Element-Create/m-p/290603#M3637</guid>
      <dc:creator>sznagy</dc:creator>
      <dc:date>2019-03-20T16:34:27Z</dc:date>
    </item>
    <item>
      <title>Re: bug in program API 21.22 ACAPI_Element_Create</title>
      <link>https://community.graphisoft.com/t5/Archicad-C-API/bug-in-program-API-21-22-ACAPI-Element-Create/m-p/290604#M3638</link>
      <description>Thanks, APINULLGuid really worked.</description>
      <pubDate>Sat, 20 Apr 2019 16:57:17 GMT</pubDate>
      <guid>https://community.graphisoft.com/t5/Archicad-C-API/bug-in-program-API-21-22-ACAPI-Element-Create/m-p/290604#M3638</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2019-04-20T16:57:17Z</dc:date>
    </item>
  </channel>
</rss>

