1
+ import json , platform , subprocess
2
+ from archicad import ACConnection
3
+ from tkinter import messagebox
4
+
5
+ ################################ CONFIGURATION #################################
6
+ errorMessageTitleArchicadNotFound = 'Could not find Archicad!'
7
+ errorMessageDetailsArchicadNotFound = 'A running Archicad instance is required to initialize the schedule.'
8
+
9
+ errorMessageTitleAdditionalCommandsNotFound = 'Could not find the required additional JSON commands!'
10
+ errorMessageDetailsAdditionalCommandsNotFound = 'The latest version of AdditionalJSONCommands Add-On is required.\n Download it from github:\n https://github.com/tlorantfy/archicad-additional-json-commands/releases'
11
+
12
+ errorMessageTitleCommandExecutionFailed = 'Failed to execute Archicad command!'
13
+ ################################################################################
14
+
15
+ conn = ACConnection .connect ()
16
+ if not conn :
17
+ messagebox .showerror (errorMessageTitleArchicadNotFound , errorMessageDetailsArchicadNotFound )
18
+ exit ()
19
+
20
+ acc = conn .commands
21
+ act = conn .types
22
+ acu = conn .utilities
23
+
24
+ def IsUsingMacOS ():
25
+ return platform .system () == 'Darwin'
26
+
27
+ def IsUsingWindows ():
28
+ return platform .system () == 'Windows'
29
+
30
+ def EscapeSpacesInPath (path ):
31
+ if IsUsingWindows ():
32
+ return f'"{ path } "'
33
+ else :
34
+ return path .replace (' ' , '\\ ' )
35
+
36
+ def CheckAdditionalJSONCommands ():
37
+ getArchicadLocationCommandId = act .AddOnCommandId ('AdditionalJSONCommands' , 'GetArchicadLocation' )
38
+ getHotlinksCommandId = act .AddOnCommandId ('AdditionalJSONCommands' , 'GetHotlinks' )
39
+ quitCommandId = act .AddOnCommandId ('AdditionalJSONCommands' , 'Quit' )
40
+ additionalJSONCommands = [getHotlinksCommandId , getArchicadLocationCommandId , quitCommandId ]
41
+
42
+ if not all ([acc .IsAddOnCommandAvailable (commandId ) for commandId in additionalJSONCommands ]):
43
+ messagebox .showerror (errorMessageTitleAdditionalCommandsNotFound , errorMessageDetailsAdditionalCommandsNotFound )
44
+ exit ()
45
+
46
+ def GetArchicadLocation ():
47
+ response = acc .ExecuteAddOnCommand (act .AddOnCommandId ('AdditionalJSONCommands' , 'GetArchicadLocation' ))
48
+ if not response or 'archicadLocation' not in response :
49
+ messagebox .showerror (errorMessageTitleCommandExecutionFailed , response )
50
+ if IsUsingMacOS ():
51
+ return f"{ response ['archicadLocation' ]} /Contents/MacOS/ARCHICAD"
52
+ return response ['archicadLocation' ]
53
+
54
+ def GetHotlinks ():
55
+ response = acc .ExecuteAddOnCommand (act .AddOnCommandId ('AdditionalJSONCommands' , 'GetHotlinks' ))
56
+ if not response or 'hotlinks' not in response :
57
+ messagebox .showerror (errorMessageTitleCommandExecutionFailed , response )
58
+ return response ['hotlinks' ]
59
+
60
+ def ReconnectToArchicad ():
61
+ global conn
62
+ global acc
63
+ global act
64
+ global acu
65
+ conn = ACConnection .connect ()
66
+ if conn :
67
+ acc = conn .commands
68
+ act = conn .types
69
+ acu = conn .utilities
70
+
71
+ def StartArchicadAndOpenProject (archicadLocation , projectLocation ):
72
+ global conn
73
+ subprocess .Popen (f"{ EscapeSpacesInPath (archicadLocation )} { EscapeSpacesInPath (projectLocation )} " , start_new_session = True , shell = IsUsingMacOS ())
74
+ ReconnectToArchicad ()
75
+ while not conn :
76
+ ReconnectToArchicad ()
77
+
78
+ def StopArchicad ():
79
+ acc .ExecuteAddOnCommand (act .AddOnCommandId ('AdditionalJSONCommands' , 'Quit' ))
80
+
81
+ def OpenHotlinkAndDo (hotlink , function ):
82
+ StopArchicad ()
83
+ StartArchicadAndOpenProject (archicadLocation , hotlink ['location' ])
84
+
85
+ function ()
86
+
87
+ if 'children' in hotlink :
88
+ for childHotlink in hotlink ['children' ]:
89
+ OpenHotlinkAndDo (childHotlink , function )
90
+
91
+ def DummyFunction ():
92
+ print ("Implement here something to be executed for all hotlinks and the main project" )
93
+
94
+ CheckAdditionalJSONCommands ()
95
+
96
+ archicadLocation = GetArchicadLocation ()
97
+
98
+ DummyFunction ()
99
+
100
+ for hotlink in GetHotlinks ():
101
+ OpenHotlinkAndDo (hotlink , DummyFunction )
102
+
103
+ StopArchicad ()
0 commit comments