Skip to content

Commit

Permalink
Autotesting examples (#171)
Browse files Browse the repository at this point in the history
* More uniform schema for commands

* New commands: OpenProject, ChangeSelectionOfElements

* Autotest framework for examples

* Fixing examples and prepare them for test environment

* Expected outputs

* Store TestProject.pla using Git LFS

* Update documentation
  • Loading branch information
tlorantfy committed Sep 4, 2024
1 parent 0eacb85 commit 994052f
Show file tree
Hide file tree
Showing 67 changed files with 10,565 additions and 757 deletions.
2 changes: 2 additions & 0 deletions archicad-addon/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Build/*
Test/TestProject*
!Test/TestProject.pla
.DS_Store
.vscode
34 changes: 25 additions & 9 deletions archicad-addon/Examples/aclib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,39 @@ def RunCommand (command, parameters):
'parameters': parameters
}
request_string = json.dumps (request_data).encode ('utf8')

response_data = urllib.request.urlopen (connection_object, request_string)
response_json = json.loads (response_data.read())

if not response_json['succeeded']:

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

if 'succeeded' not in response_json or 'result' not in response_json:
return None

return response_json['result']

def RunTapirCommand (command, parameters):
return response_json['result'] if response_json['succeeded'] else None

def RunTapirCommand (command, parameters, debug = True):
if debug:
print ('Command: ' + command)
print ('Parameters:\n' + JsonDumpDictionary (parameters))

commandResult = RunCommand ('API.ExecuteAddOnCommand', {
'addOnCommandId': {
'commandNamespace': 'TapirCommand',
'commandName': command
},
'addOnCommandParameters': parameters
})
if commandResult == None:
return None
return commandResult['addOnCommandResponse']

result = None if commandResult == None else commandResult['addOnCommandResponse']
if debug:
print ('Response:\n' + JsonDumpDictionary (result))

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

return result

def JsonDumpDictionary (d):
return json.dumps (d, indent = 4)
12 changes: 3 additions & 9 deletions archicad-addon/Examples/change_gdlparameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
gdlParameterName = 'gs_cont_pen'
newValue = 95

getElementsResponse = aclib.RunCommand ('GetElementsByType ', { "elementType": "Object" })
getElementsResponse = aclib.RunCommand ('API.GetElementsByType', { 'elementType': 'Object' })
gdlParametersResponse = aclib.RunTapirCommand ('GetGDLParametersOfElements', getElementsResponse)
elements = getElementsResponse['elements']

changedGDLParameters = []
for i in range(len(elements)):
print ('GDL Parameters of ' + elements[i]['elementId']['guid'])
for name, details in gdlParametersResponse['gdlParametersOfElements'][i].items ():
print ('\t' + name + ' = ' + str (details['value']))
if 'name' == gdlParameterName:
if name == gdlParameterName:
details['value'] = newValue
changedGDLParameters.append({
'elementId' : elements[i]['elementId'],
Expand All @@ -21,8 +19,4 @@

aclib.RunTapirCommand ('SetGDLParametersOfElements', { 'elementsWithGDLParameters' : changedGDLParameters })

gdlParametersResponse = aclib.RunTapirCommand ('GetGDLParametersOfElements', getElementsResponse)
for i in range(len(elements)):
elementGuid = str (elements[i]['elementId']['guid'])
parameters = gdlParametersResponse['gdlParametersOfElements'][i]
print ('gs_cont_pen of ' + elementGuid + ' after the change: ' + parameters['gs_cont_pen']['value'])
gdlParametersResponse = aclib.RunTapirCommand ('GetGDLParametersOfElements', getElementsResponse)
11 changes: 3 additions & 8 deletions archicad-addon/Examples/create_building_materials.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import json
import aclib

buildMats = []
for i in range (1, 51):
buildMats.append ({
buildMats = [{
'name' : 'New Building Material ' + str (i),
'id' : str (i),
'manufacturer' : 'Tapir',
Expand All @@ -18,17 +15,15 @@
'heatCapacity' : 3.0,
'embodiedEnergy' : 4.0,
'embodiedCarbon' : 5.0
})
} for i in range (1, 11)]

result = aclib.RunTapirCommand ('CreateBuildingMaterials', {
'buildingMaterialDataArray' : buildMats,
'overwriteExisting' : True
})

print (result)

result = aclib.RunCommand ('API.GetBuildingMaterialAttributes', {
'attributeIds' : result['attributeIds']
})

print (result)
print ('New building materials:\n' + aclib.JsonDumpDictionary (result))
17 changes: 8 additions & 9 deletions archicad-addon/Examples/create_composites.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
lineTypes = aclib.RunCommand ('API.GetAttributesByType', { 'attributeType' : 'Line' })
lineTypeIds = lineTypes['attributeIds']

composites = []
composites.append ({
newComposite = {
'name' : 'New Composite',
'useWith' : ['Wall', 'Shell'],
'skins' : [
Expand Down Expand Up @@ -48,15 +47,15 @@
'linePen' : 4
}
]
})
}

print ({
'compositeDataArray' : composites,
'overwriteExisting' : True
})
result = aclib.RunTapirCommand ('CreateComposites', {
'compositeDataArray' : composites,
'compositeDataArray' : [newComposite],
'overwriteExisting' : True
})

print (result)
result = aclib.RunCommand ('API.GetCompositeAttributes', {
'attributeIds' : result['attributeIds']
})

print ('New composite:\n' + aclib.JsonDumpDictionary (result))
52 changes: 37 additions & 15 deletions archicad-addon/Examples/filter_elements.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
import aclib
import itertools

print ('-' * 30)
allElements = aclib.RunCommand ('API.GetAllElements', {})['elements']
print ('All elements: {}'.format (len (allElements)))
print ('All elements = {}'.format (len (allElements)))
print ('-' * 30)

for flag in ['IsEditable',
'IsVisibleByLayer',
'IsVisibleByRenovation',
'IsVisibleByStructureDisplay',
'IsVisibleIn3D',
'OnActualFloor',
'OnActualLayout',
'InMyWorkspace',
'IsIndependent',
'InCroppedView',
'HasAccessRight',
'IsOverriddenByRenovation']:
response = aclib.RunTapirCommand ('FilterElements', { 'elements': allElements, 'filters': [flag] })
print ('{} {}'.format (flag, len (response['filteredElements'])))
typesOfElements = aclib.RunCommand ('API.GetTypesOfElements', {'elements':allElements})['typesOfElements']
typeCounterDict = dict()
for typeOfElement in typesOfElements:
if 'typeOfElement' in typeOfElement and 'elementType' in typeOfElement['typeOfElement']:
elementType = typeOfElement['typeOfElement']['elementType']
if elementType not in typeCounterDict:
typeCounterDict[elementType] = 1
else:
typeCounterDict[elementType] += 1
for t,c in typeCounterDict.items ():
print ('{} = {}'.format (t, c))
print ('-' * 30)

flags = [
'IsEditable',
'IsVisibleByLayer',
'IsVisibleByRenovation',
'IsVisibleByStructureDisplay',
'IsVisibleIn3D',
'OnActualFloor',
'OnActualLayout',
'InMyWorkspace',
'IsIndependent',
'InCroppedView',
'HasAccessRight',
'IsOverriddenByRenovation'
]

for i in range (2):
for p in itertools.permutations (flags, r=i + 1):
response = aclib.RunTapirCommand ('FilterElements', { 'elements': allElements, 'filters': [f for f in p] }, debug=False)
print ('{} = {}'.format ('|'.join (p), len (response['filteredElements'])))
print ('-' * 30)
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
import json
import aclib

buildMats = aclib.RunCommand ('API.GetAttributesByType', { 'attributeType' : 'BuildingMaterial' })
print (buildMats)

commandName = 'GetBuildingMaterialPhysicalProperties'
commandParameters = buildMats

print ('Command: {commandName}'.format (commandName = commandName))
print ('Parameters:')
print (json.dumps (commandParameters, indent = 4))

response = aclib.RunTapirCommand (commandName, commandParameters)
print ('Response:')
print (json.dumps (response, indent = 4))
response = aclib.RunTapirCommand ('GetBuildingMaterialPhysicalProperties',
aclib.RunCommand ('API.GetAttributesByType', { 'attributeType' : 'BuildingMaterial' }))
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
import json
import aclib

def ExecuteTapirCommand (commandName, commandParameters = {}):
print ('Command: {}'.format (commandName))
print ('Parameters:')
print (json.dumps (commandParameters, indent = 4))
response = aclib.RunTapirCommand (commandName, commandParameters)
print ('Response:')
print (json.dumps (response, indent = 4))
return response

mainElements = ExecuteTapirCommand ('GetSelectedElements')['elements']
response = ExecuteTapirCommand ('GetSubelementsOfHierarchicalElements', {
mainElements = aclib.RunCommand ('API.GetAllElements', {})['elements']
response = aclib.RunTapirCommand ('GetSubelementsOfHierarchicalElements', {
'hierarchicalElements' : mainElements
})

Expand All @@ -24,12 +14,12 @@ def ExecuteTapirCommand (commandName, commandParameters = {}):

classificationSystems = [{'classificationSystemId': classificationSystem['classificationSystemId']} for classificationSystem in aclib.RunCommand ('API.GetAllClassificationSystems', {})['classificationSystems']]

classificationsOfMainElements = ExecuteTapirCommand ('GetClassificationsOfElements', {
classificationsOfMainElements = aclib.RunTapirCommand ('GetClassificationsOfElements', {
'elements' : mainElements,
'classificationSystemIds' : classificationSystems
})

classificationsOfSubElements = ExecuteTapirCommand ('GetClassificationsOfElements', {
classificationsOfSubElements = aclib.RunTapirCommand ('GetClassificationsOfElements', {
'elements' : allSubElements,
'classificationSystemIds' : classificationSystems
})
5 changes: 1 addition & 4 deletions archicad-addon/Examples/get_current_window.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import aclib
import json

response = aclib.RunTapirCommand ('GetCurrentWindowType', {})

print (json.dumps (response['currentWindowType'], indent = 4))
response = aclib.RunTapirCommand ('GetCurrentWindowType', {})
6 changes: 6 additions & 0 deletions archicad-addon/Examples/get_details_of_elements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import aclib

walls = aclib.RunCommand ('API.GetElementsByType', {'elementType': 'Wall'})['elements']
columns = aclib.RunCommand ('API.GetElementsByType', {'elementType': 'Column'})['elements']

response = aclib.RunTapirCommand ('GetDetailsOfElements', {'elements': walls + columns})
24 changes: 0 additions & 24 deletions archicad-addon/Examples/get_details_of_selected_elements.py

This file was deleted.

12 changes: 1 addition & 11 deletions archicad-addon/Examples/get_libraries.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
import json
import aclib

commandName = 'GetLibraries'
commandParameters = {}

print ('Command: {commandName}'.format (commandName = commandName))
print ('Parameters:')
print (json.dumps (commandParameters, indent = 4))

response = aclib.RunTapirCommand (commandName, commandParameters)
print ('Response:')
print (json.dumps (response, indent = 4))
response = aclib.RunTapirCommand ('GetLibraries', {})
16 changes: 7 additions & 9 deletions archicad-addon/Examples/get_selected_elements.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import json
import aclib

commandName = 'GetSelectedElements'
commandParameters = {}
walls = aclib.RunCommand ('API.GetElementsByType', {'elementType': 'Wall'})['elements']
columns = aclib.RunCommand ('API.GetElementsByType', {'elementType': 'Column'})['elements']

print ('Command: {commandName}'.format (commandName = commandName))
print ('Parameters:')
print (json.dumps (commandParameters, indent = 4))
aclib.RunTapirCommand (
'ChangeSelectionOfElements', {
'addElementsToSelection': walls + columns
})

response = aclib.RunTapirCommand (commandName, commandParameters)
print ('Response:')
print (json.dumps (response, indent = 4))
selectedElements = aclib.RunTapirCommand ('GetSelectedElements', {})
22 changes: 22 additions & 0 deletions archicad-addon/Examples/highlight_elements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import aclib
import time

allElements = aclib.RunCommand ('API.GetAllElements', {})

commandName = 'HighlightElements'
commandParameters = {
'elements' : allElements['elements'],
'highlightedColors' : [[(i*30) % 255, 50, 50, 255] for i in range(len(allElements['elements']))],
'wireframe3D' : True,
'nonHighlightedColor' : [0, 0, 255, 128]
}

response = aclib.RunTapirCommand (commandName, commandParameters)

time.sleep (5)

# Clear highlight
response = aclib.RunTapirCommand ('HighlightElements', {
'elements' : [],
'highlightedColors' : [],
})
Loading

0 comments on commit 994052f

Please sign in to comment.