Skip to content

Commit

Permalink
Enhance HighlightElements command to be able to assign different colo…
Browse files Browse the repository at this point in the history
…rs for highlighted elements (#89)

* Merge MoveElements command from tlorantfy/archicad-additional-json-commands

* Fix version of MoveElements in doc

* Sync fork (merge upstream/main)

* Fix wrong resolvation of conflicts

* Fix AC 28 MAC build

* Create/Modify Layers #27

* Add isWireframe parameter for layer

* Fix typo in CreateCompositesCommand::GetInputParametersSchema

* Example and docs for Create/Modify/Delete Layers #27

* Resolve issues from Pull Request #86

* Fix descriptions in schemas

* Fix version of new command

* Enhance HighlightElements command to be able to assign different colors for highlighted elements
  • Loading branch information
tlorantfy committed Aug 8, 2024
1 parent 092fff0 commit 77e1f35
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 82 deletions.
3 changes: 2 additions & 1 deletion archicad-addon/Examples/highlight_selected_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
commandName = 'HighlightElements'
commandParameters = {
'elements' : response['elements'],
'highlightedColor' : [255, 0, 0, 255],
'highlightedColors' : [[(i*10) % 255, 0, 0, 255] for i in len(response['elements'])],
'wireframe3D' : True,
'nonHighlightedColor' : [0, 0, 255, 128]
}

Expand Down
107 changes: 64 additions & 43 deletions archicad-addon/Sources/ElementCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -781,18 +781,26 @@ GS::Optional<GS::UniString> HighlightElementsCommand::GetInputParametersSchema (
"elements": {
"$ref": "#/Elements"
},
"highlightedColor": {
"highlightedColors": {
"type": "array",
"description": "Color of the highlighted elements as an [r, g, b, a] array. Each component must be in the 0-255 range.",
"description": "A list of colors to highlight elements.",
"items": {
"type": "integer"
},
"minItems": 4,
"maxItems": 4
"type": "array",
"description": "Color of the highlighted element as an [r, g, b, a] array. Each component must be in the 0-255 range.",
"items": {
"type": "integer"
},
"minItems": 4,
"maxItems": 4
}
},
"wireframe3D": {
"type": "boolean",
"description" : "Optional parameter. Switch non highlighted elements in the 3D window to wireframe."
},
"nonHighlightedColor": {
"type": "array",
"description": "Color of the non highlighted elements as an [r, g, b, a] array. Each component must be in the 0-255 range.",
"description": "Optional parameter. Color of the non highlighted elements as an [r, g, b, a] array. Each component must be in the 0-255 range.",
"items": {
"type": "integer"
},
Expand All @@ -803,8 +811,7 @@ GS::Optional<GS::UniString> HighlightElementsCommand::GetInputParametersSchema (
"additionalProperties": false,
"required": [
"elements",
"highlightedColor",
"nonHighlightedColor"
"highlightedColors"
]
})";
}
Expand All @@ -814,48 +821,62 @@ GS::Optional<GS::UniString> HighlightElementsCommand::GetResponseSchema () const
return {};
}

static GS::Optional<API_RGBAColor> GetRGBAColorFromObjectState (const GS::ObjectState& os, const GS::String& name)
{
GS::Array<GS::Int32> color;
if (os.Get (name, color)) {
return API_RGBAColor {
color[0] / 255.0,
color[1] / 255.0,
color[2] / 255.0,
color[3] / 255.0
};
} else {
return {};
}
}

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

GS::Array<GS::Int32> highlightedColorArray;
parameters.Get ("highlightedColor", highlightedColorArray);
API_RGBAColor highlightedColor {
highlightedColorArray[0] / 255.0,
highlightedColorArray[1] / 255.0,
highlightedColorArray[2] / 255.0,
highlightedColorArray[3] / 255.0
};

GS::Array<GS::Int32> nonHighlightedColorArray;
parameters.Get ("nonHighlightedColor", nonHighlightedColorArray);
API_RGBAColor nonHighlightedColor {
nonHighlightedColorArray[0] / 255.0,
nonHighlightedColorArray[1] / 255.0,
nonHighlightedColorArray[2] / 255.0,
nonHighlightedColorArray[3] / 255.0
};

GS::HashTable<API_Guid, API_RGBAColor> elements;
for (const GS::ObjectState& elementIdArrayItem : elementIdArray) {
GS::ObjectState elementId;
elementIdArrayItem.Get ("elementId", elementId);
GS::Array<GS::ObjectState> elements;
parameters.Get ("elements", elements);

if (elements.IsEmpty ()) {
ACAPI_UserInput_ClearElementHighlight ();
// need to call redraw for changes to take effect
ACAPI_View_Redraw ();
return {};
}

GS::UniString guidStr;
elementId.Get ("guid", guidStr);
GS::Array<GS::ObjectState> highlightedColors;
parameters.Get ("highlightedColors", highlightedColors);

GS::Guid guid (guidStr);
API_Guid apiGuid = GSGuid2APIGuid (guid);
elements.Add (apiGuid, highlightedColor);
if (highlightedColors.GetSize () != elements.GetSize ()) {
return CreateErrorResponse (APIERR_BADPARS, "The size of 'elements' array and 'highlightedColors' array does not match.");
}

if (!elements.IsEmpty ()) {
ACAPI_UserInput_SetElementHighlight (elements, GS::NoValue, nonHighlightedColor);
} else {
ACAPI_UserInput_ClearElementHighlight ();
GS::HashTable<API_Guid, API_RGBAColor> elementsWithColors;
for (USize i = 0; i < elements.GetSize (); ++i) {
GS::ObjectState elementId;
if (elements[i].Get ("elementId", elementId)) {
const API_Guid elemGuid = GetGuidFromObjectState (elementId);
const GS::Optional<API_RGBAColor> color = GetRGBAColorFromObjectState (highlightedColors[i], "nonHighlightedColor");
if (color.HasValue ()) {
elementsWithColors.Add (elemGuid, *color);
}
}
}

GS::Optional<bool> wireframe3D;
bool tmp;
if (parameters.Get ("wireframe3D", tmp)) {
wireframe3D = tmp;
}

const GS::Optional<API_RGBAColor> nonHighlightedColor = GetRGBAColorFromObjectState (parameters, "nonHighlightedColor");

ACAPI_UserInput_SetElementHighlight (elementsWithColors, wireframe3D, nonHighlightedColor);

// need to call redraw for changes to take effect
ACAPI_View_Redraw ();

Expand Down
73 changes: 40 additions & 33 deletions docs/archicad-addon/command_definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,41 +366,48 @@
},
{
name : "HighlightElements",
version : "1.0.1",
version : "1.0.3",
description : "Highlights the elements given in the elements array. In case of empty elements array removes all previously set highlights.",
inputScheme : {
"type": "object",
"properties": {
"elements": {
"$ref": "#/Elements"
},
"highlightedColor": {
"type": "array",
"description": "Color of the highlighted elements as an [r, g, b, a] array. Each component must be in the 0-255 range.",
"items": {
"type": "integer"
},
"minItems": 4,
"maxItems": 4
},
"nonHighlightedColor": {
"type": "array",
"description": "Color of the non highlighted elements as an [r, g, b, a] array. Each component must be in the 0-255 range.",
"items": {
"type": "integer"
},
"minItems": 4,
"maxItems": 4
}
},
"additionalProperties": false,
"required": [
"elements",
"highlightedColor",
"nonHighlightedColor"
]
},
outputScheme : null
"type": "object",
"properties": {
"elements": {
"$ref": "#/Elements"
},
"highlightedColors": {
"type": "array",
"description": "A list of colors to highlight elements.",
"items": {
"type": "array",
"description": "Color of the highlighted element as an [r, g, b, a] array. Each component must be in the 0-255 range.",
"items": {
"type": "integer"
},
"minItems": 4,
"maxItems": 4
}
},
"wireframe3D": {
"type": "boolean",
"description" : "Optional parameter. Switch non highlighted elements in the 3D window to wireframe."
},
"nonHighlightedColor": {
"type": "array",
"description": "Optional parameter. Color of the non highlighted elements as an [r, g, b, a] array. Each component must be in the 0-255 range.",
"items": {
"type": "integer"
},
"minItems": 4,
"maxItems": 4
}
},
"additionalProperties": false,
"required": [
"elements",
"highlightedColors"
]
},
outputScheme : null
},
{
name : "MoveElements",
Expand Down
12 changes: 7 additions & 5 deletions python-package/src/tapir_py/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,24 +242,26 @@ def GetProductInfo(self):
result = response.get_result()
return result['version'], result['buildNumber'], result['languageCode']

def HighlightElements(self, elements, highlightedColor = [0, 150, 0, 100], nonHighlightedColor = [150, 0, 0, 100]):
def HighlightElements(self, elements, highlightedColors, wireframe3D = False, nonHighlightedColor = [150, 0, 0, 100]):
"""Highlights specified elements in current ArchiCAD document.
Args:
elements (:obj:`list` of :obj:`Element`): A list of elements.
highlightedColor (:obj:`list` of :int:): RGBA Color for highlighted objects.
nonHighlightedColor (:obj:`list` of :int:): RGBA Color for non-highlighted objects.
highlightedColors (:obj:`list` of :int:): RGBA Colors for highlighted elements.
wireframe3D (:bool:): Optional parameter. Switch non-highlighted elements in the 3D window to wireframe
nonHighlightedColor (:obj:`list` of :int:): Optional parameter. RGBA Color for non-highlighted elements.
Returns:
None
Raises:
Exception: If command was unsuccessful.
"""
param_selected_color = Parameter("highlightedColor", highlightedColor)
param_selected_color = Parameter("highlightedColors", highlightedColors)
param_wireframe3D = Parameter("wireframe3D", wireframe3D)
param_unselected_color = Parameter("nonHighlightedColor", nonHighlightedColor)
param_elements = Parameter("elements", [element.ToDictionary() for element in elements])
packed_params = Parameter.pack([param_elements, param_selected_color, param_unselected_color])
packed_params = Parameter.pack([param_elements, param_selected_color, param_wireframe3D, param_unselected_color])

cmd = Command.FormatAddOnCommand("HighlightElements", packed_params)

Expand Down

0 comments on commit 77e1f35

Please sign in to comment.