Skip to content

Commit c4c55f4

Browse files
committed
Add Z coordinate parameter to CreateColumns command
1 parent e68ff87 commit c4c55f4

File tree

4 files changed

+75
-17
lines changed

4 files changed

+75
-17
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ acc.ExecuteAddOnCommand (act.AddOnCommandId ('AdditionalJSONCommands', 'MoveElem
198198
Creates columns. The given coordinates will be origos of the columns.
199199
### Parameters
200200
* coordinates (required)
201-
* Type: array of x,y values
201+
* Type: array of 3D coordinates with x,y,z values
202202
### Response
203203
* errorMessage
204204
* Type: string
@@ -212,7 +212,7 @@ conn = ACConnection.connect ()
212212
acc = conn.commands
213213
act = conn.types
214214

215-
origosOfNewColumns = [{'x': 1.0, 'y': 1.0}, {'x': 5.0, 'y': 5.0}]
215+
origosOfNewColumns = [{'x': 1.0, 'y': 1.0, 'z': 0.0}, {'x': 5.0, 'y': 5.0, 'z': 3.0}]
216216

217217
acc.ExecuteAddOnCommand (act.AddOnCommandId ('AdditionalJSONCommands', 'CreateColumns'), {'coordinates': origosOfNewColumns})
218218
```

Src/AdditionalJSONCommand.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,47 @@ API_Guid AdditionalJSONCommand::GetGuidFromElementIdField (const GS::ObjectState
7171
GS::String guid;
7272
os.Get (GuidField, guid);
7373
return APIGuidFromString (guid.ToCStr ());
74+
}
75+
76+
77+
namespace Utilities {
78+
79+
80+
GS::Array<GS::Pair<short, double>> GetStoryLevels ()
81+
{
82+
GS::Array<GS::Pair<short, double>> storyLevels;
83+
API_StoryInfo storyInfo = {};
84+
85+
GSErrCode err = ACAPI_Environment (APIEnv_GetStorySettingsID, &storyInfo);
86+
if (err == NoError) {
87+
const short numberOfStories = storyInfo.lastStory - storyInfo.firstStory + 1;
88+
for (short i = 0; i < numberOfStories; ++i) {
89+
storyLevels.PushNew ((*storyInfo.data)[i].index, (*storyInfo.data)[i].level);
90+
}
91+
BMKillHandle ((GSHandle*) &storyInfo.data);
92+
}
93+
return storyLevels;
94+
}
95+
96+
97+
short GetFloorIndexAndOffset (double zPos, const GS::Array<GS::Pair<short, double>>& storyLevels, double& zOffset)
98+
{
99+
if (storyLevels.IsEmpty ()) {
100+
zOffset = zPos;
101+
return 0;
102+
}
103+
104+
auto* lastStoryIndexAndLevel = &storyLevels[0];
105+
for (const auto& storyIndexAndLevel : storyLevels) {
106+
if (storyIndexAndLevel.second > zPos) {
107+
break;
108+
}
109+
lastStoryIndexAndLevel = &storyIndexAndLevel;
110+
}
111+
112+
zOffset = zPos - lastStoryIndexAndLevel->second;
113+
return lastStoryIndexAndLevel->first;
114+
}
115+
116+
74117
}

Src/AdditionalJSONCommand.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@ class AdditionalJSONCommand : public API_AddOnCommand {
2525

2626
constexpr const char* ElementIdField = "elementId";
2727

28+
namespace Utilities {
29+
GS::Array<GS::Pair<short, double>> GetStoryLevels ();
30+
short GetFloorIndexAndOffset (double zPos, const GS::Array<GS::Pair<short, double>>& storyLevels, double& zOffset);
31+
}
32+
2833
#endif

Src/CreateColumnsCommand.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,29 @@ GS::Optional<GS::UniString> CreateColumnsCommand::GetInputParametersSchema () co
1919
"properties": {
2020
"%s": {
2121
"type": "array",
22-
"description": "The 2D coordinates of the new columns.",
22+
"description": "The 3D coordinates of the new columns' origos.",
2323
"items": {
2424
"type": "object",
25-
"description" : "Position of a 2D point.",
25+
"description" : "3D coordinate.",
2626
"properties" : {
2727
"x": {
2828
"type": "number",
29-
"description" : "X value of the point."
29+
"description" : "X value of the coordinate."
3030
},
3131
"y" : {
3232
"type": "number",
33-
"description" : "Y value of the point."
33+
"description" : "Y value of the coordinate."
34+
},
35+
"z" : {
36+
"type": "number",
37+
"description" : "Z value of the coordinate."
3438
}
3539
},
3640
"additionalProperties": false,
3741
"required" : [
3842
"x",
39-
"y"
43+
"y",
44+
"z"
4045
]
4146
}
4247
}
@@ -51,11 +56,12 @@ CoordinatesParameterField);
5156
}
5257

5358

54-
static API_Coord GetCoordinateFromObjectState (const GS::ObjectState& objectState)
59+
static API_Coord3D GetCoordinateFromObjectState (const GS::ObjectState& objectState)
5560
{
56-
API_Coord coordinate = {};
61+
API_Coord3D coordinate = {};
5762
objectState.Get ("x", coordinate.x);
5863
objectState.Get ("y", coordinate.y);
64+
objectState.Get ("z", coordinate.z);
5965
return coordinate;
6066
}
6167

@@ -65,23 +71,27 @@ GS::ObjectState CreateColumnsCommand::Execute (const GS::ObjectState& parameters
6571
GS::Array<GS::ObjectState> coordinates;
6672
parameters.Get (CoordinatesParameterField, coordinates);
6773

68-
API_Coord apiCoordinate = {};
74+
API_Coord3D apiCoordinate = {};
6975
const APIErrCodes err = (APIErrCodes) ACAPI_CallUndoableCommand ("Create Columns", [&] () -> GSErrCode {
7076
API_Element element = {};
7177
API_ElementMemo memo = {};
7278
const GS::OnExit guard ([&memo] () { ACAPI_DisposeElemMemoHdls (&memo); });
7379

74-
#ifdef ServerMainVers_2600
75-
element.header.type = API_ElemType(API_ColumnID); // AC26
76-
#else
77-
element.header.typeID = API_ColumnID; // AC25
78-
#endif
80+
#ifdef ServerMainVers_2600
81+
element.header.type = API_ColumnID;
82+
#else
83+
element.header.typeID = API_ColumnID;
84+
#endif
7985
APIErrCodes err = (APIErrCodes) ACAPI_Element_GetDefaults (&element, &memo);
8086

87+
const GS::Array<GS::Pair<short, double>> storyLevels = Utilities::GetStoryLevels ();
88+
8189
for (const GS::ObjectState& coordinate : coordinates) {
8290
apiCoordinate = GetCoordinateFromObjectState (coordinate);
8391

84-
element.column.origoPos = apiCoordinate;
92+
element.header.floorInd = Utilities::GetFloorIndexAndOffset (apiCoordinate.z, storyLevels, element.column.bottomOffset);
93+
element.column.origoPos.x = apiCoordinate.x;
94+
element.column.origoPos.y = apiCoordinate.y;
8595
err = (APIErrCodes) ACAPI_Element_Create (&element, &memo);
8696

8797
if (err != NoError) {
@@ -93,7 +103,7 @@ GS::ObjectState CreateColumnsCommand::Execute (const GS::ObjectState& parameters
93103
});
94104

95105
if (err != NoError) {
96-
const GS::UniString errorMsg = GS::UniString::Printf ("Failed to create column to coordinate: {%.2f, %.2f}!", apiCoordinate.x, apiCoordinate.y);
106+
const GS::UniString errorMsg = GS::UniString::Printf ("Failed to create column to coordinate: {%.2f, %.2f, %.2f}!", apiCoordinate.x, apiCoordinate.y, apiCoordinate.z);
97107
return CreateErrorResponse (err, errorMsg);
98108
}
99109

0 commit comments

Comments
 (0)