Skip to content

Commit

Permalink
Merge pull request #176 from tlorantfy/CreatePropertyGroups
Browse files Browse the repository at this point in the history
CreatePropertyGroups command #25
  • Loading branch information
tlorantfy authored Sep 14, 2024
2 parents 9263d8b + 530d67d commit 1b057b4
Show file tree
Hide file tree
Showing 12 changed files with 399 additions and 122 deletions.
2 changes: 1 addition & 1 deletion archicad-addon/Examples/aclib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def RunTapirCommand (command, parameters, debug = True):
if debug:
print ('Response:\n' + JsonDumpDictionary (result))

if 'error' in result:
if result and 'error' in result:
print ('Error:\n{}'.format (JsonDumpDictionary (result['error'])))

return result
Expand Down
14 changes: 14 additions & 0 deletions archicad-addon/Examples/create_property_groups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import aclib

newPropertyGroups = [{
'propertyGroup' : {
'name': 'Python Property Group #' + str (i),
'description': 'Created from python script'
}
} for i in range (1, 4)]

propertyGroupIds = aclib.RunTapirCommand ('CreatePropertyGroups', {'propertyGroups': newPropertyGroups})

propertyGroups = aclib.RunCommand ('API.GetPropertyGroups', propertyGroupIds)

print ('New Property Groups:\n' + aclib.JsonDumpDictionary (propertyGroups))
25 changes: 17 additions & 8 deletions archicad-addon/Sources/AddOnMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,6 @@ GSErrCode Initialize (void)
elementCommands, "1.0.2",
"Sets the given GDL parameters of the given elements."
);
err |= RegisterCommand<GetPropertyValuesOfElementsCommand> (
elementCommands, "1.0.6",
"Returns the property values of the elements for the given property. It works for subelements of hierarchal elements also."
);
err |= RegisterCommand<SetPropertyValuesOfElementsCommand> (
elementCommands, "1.0.6",
"Sets the property values of elements. It works for subelements of hierarchal elements also."
);
err |= RegisterCommand<GetClassificationsOfElementsCommand> (
elementCommands, "1.0.7",
"Returns the classification of the given elements in the given classification systems. It works for subelements of hierarchal elements also."
Expand All @@ -217,6 +209,23 @@ GSErrCode Initialize (void)
AddCommandGroup (elementCommands);
}

{ // Property Commands
CommandGroup propertyCommands ("Property Commands");
err |= RegisterCommand<GetPropertyValuesOfElementsCommand> (
propertyCommands, "1.0.6",
"Returns the property values of the elements for the given property. It works for subelements of hierarchal elements also."
);
err |= RegisterCommand<SetPropertyValuesOfElementsCommand> (
propertyCommands, "1.0.6",
"Sets the property values of elements. It works for subelements of hierarchal elements also."
);
err |= RegisterCommand<CreatePropertyGroupsCommand> (
propertyCommands, "1.0.7",
"Creates Property Groups based on the given parameters."
);
AddCommandGroup (propertyCommands);
}

{ // Attribute Commands
CommandGroup attributeCommands ("Attribute Commands");
err |= RegisterCommand<CreateLayersCommand> (
Expand Down
130 changes: 130 additions & 0 deletions archicad-addon/Sources/PropertyCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,133 @@ GS::ObjectState SetPropertyValuesOfElementsCommand::Execute (const GS::ObjectSta

return response;
}

CreatePropertyGroupsCommand::CreatePropertyGroupsCommand () :
CommandBase (CommonSchema::Used)
{
}

GS::String CreatePropertyGroupsCommand::GetName () const
{
return "CreatePropertyGroups";
}

GS::Optional<GS::UniString> CreatePropertyGroupsCommand::GetInputParametersSchema () const
{
return R"({
"type": "object",
"properties": {
"propertyGroups": {
"type": "array",
"description": "The parameters of the new property groups.",
"items": {
"type": "object",
"properties": {
"propertyGroup": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"description": {
"type": "string"
}
},
"additionalProperties": false,
"required": [
"name"
]
}
},
"additionalProperties": false,
"required": [
"propertyGroup"
]
}
}
},
"additionalProperties": false,
"required": [
"propertyGroups"
]
})";
}

GS::Optional<GS::UniString> CreatePropertyGroupsCommand::GetResponseSchema () const
{
return R"({
"type": "object",
"properties": {
"propertyGroupIds": {
"type": "array",
"description": "The identifiers of the created property groups.",
"items": {
"type": "object",
"properties": {
"propertyGroupId": {
"type": "object",
"properties": {
"guid": {
"$ref": "#/Guid"
}
},
"additionalProperties": false,
"required": [
"guid"
]
}
},
"additionalProperties": false,
"required": [
"propertyGroupId"
]
}
}
},
"additionalProperties": false,
"required": [
"propertyGroupIds"
]
})";
}

GS::ObjectState CreatePropertyGroupsCommand::Execute (const GS::ObjectState& parameters, GS::ProcessControl& /*processControl*/) const
{
GS::Array<GS::ObjectState> propertyGroups;
parameters.Get ("propertyGroups", propertyGroups);

GS::Array<GS::ObjectState> properties;
parameters.Get ("properties", properties);

GS::ObjectState response;
const auto& propertyGroupIds = response.AddList<GS::ObjectState> ("propertyGroupIds");

ACAPI_CallUndoableCommand ("CreatePropertyGroups", [&] () -> GSErrCode {
for (const GS::ObjectState& g : propertyGroups) {
const GS::ObjectState* propertyGroup = g.Get ("propertyGroup");
if (propertyGroup == nullptr) {
propertyGroupIds (CreateErrorResponse (APIERR_BADPARS, "propertyGroup is missing"));
continue;
}

API_PropertyGroup apiPropertyGroup;
if (!propertyGroup->Get ("name", apiPropertyGroup.name) || apiPropertyGroup.name.IsEmpty ()) {
propertyGroupIds (CreateErrorResponse (APIERR_BADPARS, "name is missing or empty"));
continue;
}

propertyGroup->Get ("description", apiPropertyGroup.description);
GSErrCode err = ACAPI_Property_CreatePropertyGroup (apiPropertyGroup);
if (err != NoError) {
propertyGroupIds (CreateErrorResponse (APIERR_BADPARS, "name is missing or empty"));
continue;
}

propertyGroupIds (CreateIdObjectState ("propertyGroupId", apiPropertyGroup.guid));
}

return NoError;
});

return response;
}
10 changes: 10 additions & 0 deletions archicad-addon/Sources/PropertyCommands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@ class SetPropertyValuesOfElementsCommand : public CommandBase
virtual GS::Optional<GS::UniString> GetInputParametersSchema () const override;
virtual GS::Optional<GS::UniString> GetResponseSchema () const override;
virtual GS::ObjectState Execute (const GS::ObjectState& parameters, GS::ProcessControl& processControl) const override;
};

class CreatePropertyGroupsCommand : public CommandBase
{
public:
CreatePropertyGroupsCommand ();
virtual GS::String GetName () const override;
virtual GS::Optional<GS::UniString> GetInputParametersSchema () const override;
virtual GS::Optional<GS::UniString> GetResponseSchema () const override;
virtual GS::ObjectState Execute (const GS::ObjectState& parameters, GS::ProcessControl& processControl) const override;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
Command: CreatePropertyGroups
Parameters:
{
"propertyGroups": [
{
"propertyGroup": {
"name": "Python Property Group #1",
"description": "Created from python script"
}
},
{
"propertyGroup": {
"name": "Python Property Group #2",
"description": "Created from python script"
}
},
{
"propertyGroup": {
"name": "Python Property Group #3",
"description": "Created from python script"
}
}
]
}
Response:
{
"propertyGroupIds": [
{
"propertyGroupId": {
"guid": "<GUID>"
}
},
{
"propertyGroupId": {
"guid": "<GUID>"
}
},
{
"propertyGroupId": {
"guid": "<GUID>"
}
}
]
}
New Property Groups:
{
"propertyGroups": [
{
"propertyGroup": {
"propertyGroupId": {
"guid": "<GUID>"
},
"name": "Python Property Group #1"
}
},
{
"propertyGroup": {
"propertyGroupId": {
"guid": "<GUID>"
},
"name": "Python Property Group #2"
}
},
{
"propertyGroup": {
"propertyGroupId": {
"guid": "<GUID>"
},
"name": "Python Property Group #3"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Command: GenerateDocumentation
Parameters:
{
"destinationFolder": "c:\\Users\\loran\\GitRepos\\tapir-archicad-automation\\docs\\archicad-addon"
"destinationFolder": "<PATH>archicad-addon"
}
Response:
{
Expand Down
Loading

0 comments on commit 1b057b4

Please sign in to comment.