-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
88 lines (71 loc) · 3.65 KB
/
utilities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import platform, subprocess
from archicad import ACConnection
from tkinter import messagebox
################################ CONFIGURATION #################################
errorMessageTitleArchicadNotFound = 'Could not find Archicad!'
errorMessageDetailsArchicadNotFound = 'A running Archicad instance is required to initialize the schedule.'
errorMessageTitleAdditionalCommandsNotFound = 'Could not find the required commands!'
errorMessageDetailsAdditionalCommandsNotFound = 'The below commands are not available:\n{}\n\nThe latest version of AdditionalJSONCommands Add-On is required.\nDownload it from github:\nhttps://github.com/tlorantfy/archicad-additional-json-commands/releases'
errorMessageTitleCommandExecutionFailed = 'Failed to execute Archicad command!'
################################################################################
def ReconnectToArchicad ():
return ACConnection.connect ()
def ConnectArchicad ():
conn = ReconnectToArchicad ()
if not conn:
messagebox.showerror (errorMessageTitleArchicadNotFound, errorMessageDetailsArchicadNotFound)
exit ()
return conn
def CheckCommandsAvailability (acConnection, additionalJSONCommands):
notAvailableCommands = [commandId.commandName + ' (Namespace: ' + commandId.commandNamespace + ')' for commandId in additionalJSONCommands if not acConnection.commands.IsAddOnCommandAvailable (commandId)]
if notAvailableCommands:
messagebox.showerror (errorMessageTitleAdditionalCommandsNotFound, errorMessageDetailsAdditionalCommandsNotFound.format ('\n'.join (notAvailableCommands)))
exit ()
def ExitIfResponseIsError (response):
ExitIfResponseDoesNotContain (response)
def ExitIfResponseDoesNotContain (response, requiredFields = None):
missingFields = []
if requiredFields:
for f in requiredFields:
if f not in response:
missingFields.append (f)
if (len(response) > 0 and 'error' in response) or (len(missingFields) > 0):
messagebox.showerror (errorMessageTitleCommandExecutionFailed, response)
exit ()
def ConvertElementsResponseToInput (elements):
return { 'elements' : [ { 'elementId' : { 'guid' : str (e.elementId.guid) } } for e in elements ] }
def IsUsingMacOS ():
return platform.system () == 'Darwin'
def IsUsingWindows ():
return platform.system () == 'Windows'
def EscapeSpacesInPath (path):
if IsUsingWindows ():
return f'"{path}"'
else:
return path.replace (' ', '\\ ')
def StartArchicadAndOpenProject (archicadLocation, projectLocation):
acConnection = ReconnectToArchicad ()
if not acConnection:
subprocess.Popen (f"{EscapeSpacesInPath (archicadLocation)} {EscapeSpacesInPath (projectLocation)}", start_new_session=True, shell=IsUsingMacOS ())
while not acConnection:
acConnection = ReconnectToArchicad ()
def ExecuteAdditionalJSONCommand (commandName, inputParameters = None):
acConnection = ConnectArchicad ()
command = acConnection.types.AddOnCommandId ('AdditionalJSONCommands', commandName)
CheckCommandsAvailability (acConnection, [command])
return acConnection.commands.ExecuteAddOnCommand (command, inputParameters)
def StopArchicad ():
return ExecuteAdditionalJSONCommand ('Quit')
def GetArchicadLocation ():
response = ExecuteAdditionalJSONCommand ('GetArchicadLocation')
ExitIfResponseDoesNotContain (response, ['archicadLocation'])
if IsUsingMacOS ():
return f"{response['archicadLocation']}/Contents/MacOS/ARCHICAD"
return response['archicadLocation']
def GetRectangleCoordinates (centerX, centerY, width, height):
return [
{'x': centerX + width/2, 'y': centerY - height/2},
{'x': centerX + width/2, 'y': centerY + height/2},
{'x': centerX - width/2, 'y': centerY + height/2},
{'x': centerX - width/2, 'y': centerY - height/2}
]