Skip to content

Commit c8ad06e

Browse files
committedAug 9, 2023
Refactoring (move duplicated codes to utilities.py) + example for GetGDLParametersOfElements and ChangeGDLParametersOfElements
1 parent d5b55a6 commit c8ad06e

8 files changed

+144
-217
lines changed
 

‎README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Ready-to-go Python scripts for Archicad
22

3-
[Download version 25.3](https://github.com/tlorantfy/archicad-python-scripts/archive/refs/tags/25.3.zip)
3+
[Download version 26.1](https://github.com/tlorantfy/archicad-python-scripts/archive/refs/tags/26.1.zip)
44

55
## recurring_publish.py
66

@@ -55,3 +55,13 @@ The script retrieves the list of hotlinks in the currently active project, opens
5555
* [Additional JSON/Python Commands Add-On](https://github.com/tlorantfy/archicad-additional-json-commands) (version 25.4 or later) is required to be loaded into Archicad.
5656
* [Download the Add-On for Windows](https://github.com/tlorantfy/archicad-additional-json-commands/releases/download/25.4/archicad-additional-json-commands.apx)
5757
* [Download the Add-On for macOS](https://github.com/tlorantfy/archicad-additional-json-commands/releases/download/25.4/archicad-additional-json-commands.bundle.zip)
58+
59+
## get_parameters_of_selected_GDLbased.py
60+
61+
### Description
62+
The script lists the GDL parameters (name and value pairs) of the selected elements.
63+
64+
### Requirements
65+
* **Requires Archicad 25 or later.**
66+
* [Additional JSON/Python Commands Add-On](https://github.com/tlorantfy/archicad-additional-json-commands) (version 26.1 or later) is required to be loaded into Archicad.
67+
* [Download the Add-On for Windows](https://github.com/tlorantfy/archicad-additional-json-commands/releases/download/26.1/archicad-additional-json-commands.apx)

‎__pycache__/utilities.cpython-311.pyc

5.99 KB
Binary file not shown.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from utilities import *
2+
3+
acConnection = ConnectArchicad ()
4+
5+
elements = acConnection.commands.GetSelectedElements()
6+
7+
elementsWithGDLParameters = [ { 'elementId' : { 'guid' : str (e.elementId.guid) }, 'gdlParameters' : { 'gs_cont_pen' : { 'value' : 95 } } } for e in elements ]
8+
9+
response = ExecuteAdditionalJSONCommand ('ChangeGDLParametersOfElements', { 'elementsWithGDLParameters' : elementsWithGDLParameters })
10+
ExitIfResponseIsError (response)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from utilities import *
2+
from tkinter import messagebox
3+
4+
acConnection = ConnectArchicad ()
5+
6+
elements = acConnection.commands.GetSelectedElements()
7+
8+
response = ExecuteAdditionalJSONCommand ('GetGDLParametersOfElements', ConvertElementsResponseToInput (elements))
9+
ExitIfResponseDoesNotContain (response, ['gdlParametersOfElements'])
10+
11+
elementIdPropertyId = acConnection.utilities.GetBuiltInPropertyId('General_ElementID')
12+
elementIdPropertiesForElements = acConnection.commands.GetPropertyValuesOfElements(elements, [elementIdPropertyId])
13+
14+
for i in range(len(elements)):
15+
elementGuid = str (elements[i].elementId.guid)
16+
parameters = response['gdlParametersOfElements'][i]
17+
propertyValue = elementIdPropertiesForElements[i].propertyValues[0].propertyValue.value
18+
messagebox.showinfo ('GDL Parameters of ' + propertyValue + ' (Guid: ' + elementGuid + ')', '\n'.join([name + ' = ' + str (details['value']) for name, details in parameters.items ()]))

‎move_all_objects.py

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,13 @@
1-
import json
2-
from archicad import ACConnection
3-
from tkinter import messagebox
1+
from utilities import *
42

5-
################################ CONFIGURATION #################################
6-
errorMessageTitleArchicadNotFound = 'Could not find Archicad!'
7-
errorMessageDetailsArchicadNotFound = 'A running Archicad instance is required to initialize the schedule.'
3+
acConnection = ConnectArchicad ()
4+
objects = acConnection.commands.GetElementsByType ('Object')
85

9-
errorMessageTitleAdditionalCommandsNotFound = 'Could not find the required additional JSON commands!'
10-
errorMessageDetailsAdditionalCommandsNotFound = 'The latest version of AdditionalJSONCommands Add-On is required.\nDownload it from github:\nhttps://github.com/tlorantfy/archicad-additional-json-commands/releases'
6+
deltaX = 1.0
7+
deltaY = 1.0
8+
deltaZ = 0.0
119

12-
errorMessageTitleCommandExecutionFailed = 'Failed to execute Archicad command!'
13-
################################################################################
10+
elementsWithMoveVectors = [{'elementId': {'guid': str (object.elementId.guid)}, 'moveVector': {'x': deltaX, 'y': deltaY, 'z': deltaZ}} for object in objects]
1411

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-
moveElementsCommandId = act.AddOnCommandId ('AdditionalJSONCommands', 'MoveElements')
25-
26-
def CheckAdditionalJSONCommands ():
27-
additionalJSONCommands = [moveElementsCommandId]
28-
29-
if not all ([acc.IsAddOnCommandAvailable (commandId) for commandId in additionalJSONCommands]):
30-
messagebox.showerror (errorMessageTitleAdditionalCommandsNotFound, errorMessageDetailsAdditionalCommandsNotFound)
31-
exit ()
32-
33-
CheckAdditionalJSONCommands ()
34-
35-
objects = acc.GetElementsByType ('Object')
36-
elementsWithMoveVectors = [{'elementId': {'guid': str (object.elementId.guid)}, 'moveVector': {'x': 1.0, 'y': 1.0, 'z': 0.0}} for object in objects]
37-
38-
response = acc.ExecuteAddOnCommand (moveElementsCommandId, { 'elementsWithMoveVectors': elementsWithMoveVectors })
39-
if response:
40-
messagebox.showerror (errorMessageTitleCommandExecutionFailed, response)
12+
response = ExecuteAdditionalJSONCommand ('MoveElements', { 'elementsWithMoveVectors': elementsWithMoveVectors })
13+
ExitIfResponseIsError (response)

‎open_all_hotlinks.py

Lines changed: 5 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,12 @@
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.\nDownload it from github:\nhttps://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']
1+
from utilities import *
532

543
def GetHotlinks ():
55-
response = acc.ExecuteAddOnCommand (act.AddOnCommandId ('AdditionalJSONCommands', 'GetHotlinks'))
56-
if not response or 'hotlinks' not in response:
57-
messagebox.showerror (errorMessageTitleCommandExecutionFailed, response)
4+
response = ExecuteAdditionalJSONCommand ('GetHotlinks')
5+
ExitIfResponseDoesNotContain (response, ['hotlinks'])
586
return response['hotlinks']
597

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-
818
def OpenHotlinkAndDo (hotlink, function):
9+
archicadLocation = GetArchicadLocation ()
8210
StopArchicad ()
8311
StartArchicadAndOpenProject (archicadLocation, hotlink['location'])
8412

@@ -91,10 +19,7 @@ def OpenHotlinkAndDo (hotlink, function):
9119
def DummyFunction ():
9220
print ("Implement here something to be executed for all hotlinks and the main project")
9321

94-
CheckAdditionalJSONCommands ()
95-
96-
archicadLocation = GetArchicadLocation ()
97-
22+
ConnectArchicad ()
9823
DummyFunction ()
9924

10025
for hotlink in GetHotlinks ():

‎recurring_publish.py

Lines changed: 11 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import os
2-
import re
3-
import tkinter as tk
4-
import subprocess
5-
import platform
1+
import os, re, platform, subprocess, tkinter as tk
2+
from utilities import *
63
from archicad import ACConnection
74
from tkinter import filedialog, messagebox
85
from datetime import datetime, timedelta
@@ -18,98 +15,27 @@
1815
textBrowseButton = 'Browse'
1916
textRecur1 = 'Recur every'
2017
textRecur2 = 'minute(s)'
21-
textRecur3 = 'Archicad will be closed after publishing and restarted before publishing.'
2218
textPublishButton = 'Start Publishing'
2319
textExitButton = 'Stop and Exit'
2420

2521
textProgressWaitingForStart = 'Waiting to start...'
26-
textProgressRestartingArchicad = 'Restarting Archicad...'
2722
textProgressPublishing = 'Publishing...'
28-
textProgressQuitArchicad = 'Quit Archicad...'
2923
textProgressSecsBackTillNextPublishing = ' second(s) till next publishing...'
3024

31-
errorMessageTitleArchicadNotFound = 'Could not find Archicad!'
32-
errorMessageDetailsArchicadNotFound = 'A running Archicad instance is required to initialize the schedule.'
33-
34-
errorMessageTitleAdditionalCommandsNotFound = 'Could not find the required additional JSON commands!'
35-
errorMessageDetailsAdditionalCommandsNotFound = 'The latest version of AdditionalJSONCommands Add-On is required.\nDownload it from github:\nhttps://github.com/tlorantfy/archicad-additional-json-commands/releases'
36-
37-
errorMessageTitleOutputPathInvalid = 'Invalid output path!'
38-
errorMessageDetailsOutputPathInvalid = 'Please input valid output path.'
39-
40-
errorMessageTitleCommandExecutionFailed = 'Failed to execute Archicad command!'
41-
4225
publishSubfolderPrefix = ''
4326
publishSubfolderDatePostfixFormat = '%Y-%m-%d_%H-%M-%S'
4427
################################################################################
4528

46-
conn = ACConnection.connect ()
47-
if not conn:
48-
messagebox.showerror (errorMessageTitleArchicadNotFound, errorMessageDetailsArchicadNotFound)
49-
exit ()
50-
51-
acc = conn.commands
52-
act = conn.types
53-
acu = conn.utilities
54-
55-
def ReconnectToArchicad ():
56-
global conn
57-
global acc
58-
global act
59-
global acu
60-
conn = ACConnection.connect ()
61-
if conn:
62-
acc = conn.commands
63-
act = conn.types
64-
acu = conn.utilities
65-
66-
def CheckAdditionalJSONCommands ():
67-
getProjectInfoCommandId = act.AddOnCommandId ('AdditionalJSONCommands', 'GetProjectInfo')
68-
publishCommandId = act.AddOnCommandId ('AdditionalJSONCommands', 'Publish')
69-
teamworkReceiveCommandId = act.AddOnCommandId ('AdditionalJSONCommands', 'TeamworkReceive')
70-
getArchicadLocationCommandId = act.AddOnCommandId ('AdditionalJSONCommands', 'GetArchicadLocation')
71-
quitCommandId = act.AddOnCommandId ('AdditionalJSONCommands', 'Quit')
72-
additionalJSONCommands = [getProjectInfoCommandId, publishCommandId, teamworkReceiveCommandId, quitCommandId, getArchicadLocationCommandId]
73-
74-
if not all ([acc.IsAddOnCommandAvailable (commandId) for commandId in additionalJSONCommands]):
75-
messagebox.showerror (errorMessageTitleAdditionalCommandsNotFound, errorMessageDetailsAdditionalCommandsNotFound)
76-
exit ()
77-
78-
def IsUsingMacOS ():
79-
return platform.system () == 'Darwin'
80-
81-
def IsUsingWindows ():
82-
return platform.system () == 'Windows'
83-
84-
def EscapeSpacesInPath (path):
85-
if IsUsingWindows ():
86-
return f'"{path}"'
87-
else:
88-
return path.replace (' ', '\\ ')
89-
90-
def GetArchicadLocation ():
91-
response = acc.ExecuteAddOnCommand (act.AddOnCommandId ('AdditionalJSONCommands', 'GetArchicadLocation'))
92-
if not response or 'archicadLocation' not in response:
93-
messagebox.showerror (errorMessageTitleCommandExecutionFailed, response)
94-
if IsUsingMacOS ():
95-
return f"{response['archicadLocation']}/Contents/MacOS/ARCHICAD"
96-
return response['archicadLocation']
97-
9829
def GetProjectInfo ():
99-
response = acc.ExecuteAddOnCommand (act.AddOnCommandId ('AdditionalJSONCommands', 'GetProjectInfo'))
100-
if not response or 'projectLocation' not in response or 'projectPath' not in response or 'isTeamwork' not in response:
101-
messagebox.showerror (errorMessageTitleCommandExecutionFailed, response)
30+
response = ExecuteAdditionalJSONCommand ('GetProjectInfo')
31+
ExitIfResponseDoesNotContain (response, ['projectLocation', 'projectPath', 'isTeamwork'])
10232
return response
10333

104-
105-
CheckAdditionalJSONCommands ()
106-
10734
archicadLocation = GetArchicadLocation ()
10835
projectInfo = GetProjectInfo ()
10936
taskScheduler = None
11037
publisherSetNames = []
11138

112-
11339
class ProgressUpdater:
11440
def __init__ (self, secondsToCountBack):
11541
self.secondsToCountBack = secondsToCountBack
@@ -146,32 +72,18 @@ def Stop (self):
14672
self.timer.cancel ()
14773
self.StopArchicad ()
14874

149-
def RestartArchicad (self):
150-
progressLabel.config (text=textProgressRestartingArchicad)
151-
ReconnectToArchicad ()
152-
global conn
153-
if not conn:
154-
subprocess.Popen (f"{EscapeSpacesInPath (archicadLocation)} {EscapeSpacesInPath (projectInfo['projectLocation'])}", start_new_session=True, shell=True)
155-
while not conn:
156-
ReconnectToArchicad ()
157-
158-
def StopArchicad (self):
159-
progressLabel.config (text=textProgressQuitArchicad)
160-
acc.ExecuteAddOnCommand (act.AddOnCommandId ('AdditionalJSONCommands', 'Quit'))
161-
16275
def Execute (self):
163-
self.RestartArchicad ()
76+
StartArchicadAndOpenProject (archicadLocation, projectInfo['projectLocation'])
16477
self.task ()
165-
self.StopArchicad ()
78+
StopArchicad ()
16679
self.ScheduleNextExecution (timedelta (minutes=int (recurEntry.get ())).total_seconds ())
16780

16881
def ExecutePublishCommand ():
16982
progressLabel.config (text=textProgressPublishing)
17083

17184
if projectInfo['isTeamwork']:
172-
response = acc.ExecuteAddOnCommand (act.AddOnCommandId ('AdditionalJSONCommands', 'TeamworkReceive'))
173-
if response:
174-
messagebox.showerror (errorMessageTitleCommandExecutionFailed, response)
85+
response = ExecuteAdditionalJSONCommand ('TeamworkReceive')
86+
ExitIfResponseIsError (response)
17587

17688
for publisherSetListIndex in publisherSetList.curselection ():
17789
publisherSetName = publisherSetNames[publisherSetListIndex]
@@ -183,9 +95,8 @@ def ExecutePublishCommand ():
18395
f'{publishSubfolderPrefix}{datetime.now ().strftime(publishSubfolderDatePostfixFormat)}'
18496
)
18597

186-
response = acc.ExecuteAddOnCommand (act.AddOnCommandId ('AdditionalJSONCommands', 'Publish'), parameters)
187-
if response:
188-
messagebox.showerror (errorMessageTitleCommandExecutionFailed, response)
98+
response = ExecuteAdditionalJSONCommand ('Publish', parameters)
99+
ExitIfResponseIsError (response)
189100

190101
def StartRecurringPublishing ():
191102
executeButton['state'] = tk.DISABLED
@@ -211,7 +122,7 @@ def GetUsernameFromProjectLocation (projectLocation):
211122

212123
def InitPublisherSetList ():
213124
global publisherSetNames
214-
publisherSetNames = acc.GetPublisherSetNames ()
125+
publisherSetNames = ConnectArchicad ().commands.GetPublisherSetNames ()
215126
publisherSetNames.sort ()
216127

217128
if publisherSetNames:

0 commit comments

Comments
 (0)