<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="fr">
	<id>http://wiki.archi-cadlink.fr/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mathias+J</id>
	<title>Wiki-Cadlink - Contributions de l’utilisateur [fr]</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.archi-cadlink.fr/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mathias+J"/>
	<link rel="alternate" type="text/html" href="http://wiki.archi-cadlink.fr/index.php?title=Sp%C3%A9cial:Contributions/Mathias_J"/>
	<updated>2026-05-07T11:46:31Z</updated>
	<subtitle>Contributions de l’utilisateur</subtitle>
	<generator>MediaWiki 1.32.0</generator>
	<entry>
		<id>http://wiki.archi-cadlink.fr/index.php?title=Script_in_python_for_archicad_101&amp;diff=68</id>
		<title>Script in python for archicad 101</title>
		<link rel="alternate" type="text/html" href="http://wiki.archi-cadlink.fr/index.php?title=Script_in_python_for_archicad_101&amp;diff=68"/>
		<updated>2023-02-28T21:52:12Z</updated>

		<summary type="html">&lt;p&gt;Mathias J : &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This article follows the [[Introduction to the use of the Archicad-Python connection]].&lt;br /&gt;
&lt;br /&gt;
This first script is directly taken from the tutorial of Tibor Lorántfy, a former developer at graphisoft :&lt;br /&gt;
&lt;br /&gt;
https://archicadapi.graphisoft.com/getting-started-with-archicad-python-connection&lt;br /&gt;
&lt;br /&gt;
This first script will display in the archicad console the number of walls in the project.&lt;br /&gt;
&lt;br /&gt;
I will first show you a &amp;quot;raw&amp;quot; version of this script, then the optimal version as proposed by Tibor, and finally propose a graphical interface (very light) with Tkinter.&lt;br /&gt;
&lt;br /&gt;
Let's start!&lt;br /&gt;
&lt;br /&gt;
== &amp;quot;raw&amp;quot; version ==&lt;br /&gt;
This version is proposed to better explain the methodology, but it is not at all recommended. This is the script written with the minimum number of lines possible.&lt;br /&gt;
&lt;br /&gt;
It is done... in 3 lines!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 1st Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Generally speaking, a python script does not load all the functionality allowed by python, and it is necessary to indicate the additional modules that you wish to integrate at the beginning of the script.&lt;br /&gt;
&lt;br /&gt;
It can be a module allowing to work with excel files (for example openpyxl):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from openpyxl import Workbook&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can also be a module allowing to create a graphic interface (for example Tkinter):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from tkinter import *&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
They can be modules allowing to use images, create randomness, etc, etc.&lt;br /&gt;
&lt;br /&gt;
These modules are sometimes already integrated in the basic installation of python and do not require additional installation, but must still be called (this is the case of the graphical interface Tkinter), and sometimes it is necessary to have previously installed this additional module.&lt;br /&gt;
&lt;br /&gt;
In our case, ''this first line is absolutely necessary for all python scripts for archicad'': It tells python to load the python/archicad module installed on the computer which will allow access to all commands dedicated to archicad.&lt;br /&gt;
&lt;br /&gt;
=== 2nd Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this second line, we create a variable named &amp;quot;walls&amp;quot; and we use the command to get all the elements created with the wall tool. I have drawn in my archicad file 4 walls on the ground floor.&lt;br /&gt;
&lt;br /&gt;
==== The command to retrieve the elements according to their type ====&lt;br /&gt;
&lt;br /&gt;
To get all the walls of the project we will use the command GetElementsByType().&lt;br /&gt;
&lt;br /&gt;
To get more information about this function you can either go to the python site for archicad:&lt;br /&gt;
https://archicadapi.graphisoft.com/archicadPythonPackage/archicad.releases.ac24.html#archicad.releases.ac24.b2310commands.Commands.GetElementsByType&lt;br /&gt;
&lt;br /&gt;
or go to Visual Studio code:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python11.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Visual Studio Code, we have:&lt;br /&gt;
# the name of the command (1),&lt;br /&gt;
# what it does (2)&lt;br /&gt;
# the type of function (3): a command,&lt;br /&gt;
# the type of argument to be put between the () and in what form (4): here a string indicating the type of the element. So you have to write the type between two quotation marks &amp;quot; &amp;quot;.&lt;br /&gt;
# the result that will be returned (5): a list of type &amp;quot;ElementIdArrayItem&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== How to launch the command? ====&lt;br /&gt;
If we follow the previous part we must therefore write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GetElementsByType(&amp;quot;Wall&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To launch this function, however, you need to add some information:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python12.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We tell python that we want to use the python module for archicad (1), then that we want to connect to archicad (2), that we want to use a command (3) and finally which command we want to use (4).&lt;br /&gt;
&lt;br /&gt;
==== The result obtained with the 2nd line ====&lt;br /&gt;
&lt;br /&gt;
We have created a walls variable that retrieves the result of the GetElementsByType() command&lt;br /&gt;
&lt;br /&gt;
If we used the &amp;quot;print&amp;quot; function to see what the &amp;quot;walls&amp;quot; variable retrieves, it would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[ElementIdArrayItem {'elementId': {'guid': '86E4F938-8783-4E1C-881E-91C60E902526'}}, ElementIdArrayItem {'elementId': {'guid': 'E794D806-E94C-49D1-9854-A238F49D4AE3'}}, ElementIdArrayItem {'elementId': {'guid': 'B848DBB3-E432-4AEE-8AD1-A8FB0F559BBE'}}, ElementIdArrayItem {'elementId': {'guid': 'FF09403B-BA6D-43D3-B936-6C44AD229F85'}}]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is &amp;quot;almost&amp;quot; JSON. If we &amp;quot;clean up&amp;quot; this answer a bit to turn it into JSON we get this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
        &amp;quot;elements&amp;quot;: [&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;86E4F938-8783-4E1C-881E-91C60E902526&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;E794D806-E94C-49D1-9854-A238F49D4AE3&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;B848DBB3-E432-4AEE-8AD1-A8FB0F559BBE&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;FF09403B-BA6D-43D3-B936-6C44AD229F85&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        ]&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Schematically, this is what it looks like:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python13.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This means that we have a list of 4 values (our 4 walls), going from index 0 to index 3 (which corresponds to the 4 walls: wall index 0, wall index 1, wall index 2, wall index 3 )&lt;br /&gt;
&lt;br /&gt;
This notion of index (in 1) on the schema is not explicitly indicated in the JSON format, it is an automatic attribution: the first value is assigned index 0, the second index 1, etc... Like a python list.&lt;br /&gt;
&lt;br /&gt;
To each of these elements is associated a GUID: it is a unique identifier generated by the software that allows to find this element. This identifier remains constant and will not change.&lt;br /&gt;
&lt;br /&gt;
So we obtain a list of 4 unique identifiers (GUID) corresponding to the 4 walls of the project.&lt;br /&gt;
&lt;br /&gt;
=== 3rd Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
print('Number of Walls: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This third line is the simplest, it is pure python, and very basic.&lt;br /&gt;
&lt;br /&gt;
==== The len() function which counts the number of elements ====&lt;br /&gt;
&lt;br /&gt;
If we use len on the walls variable, this will be written:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
len(walls)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and will give as result the number of elements in the list, that is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The print() function and a concatenation ==== &lt;br /&gt;
&lt;br /&gt;
We use the print() function and we make a concatenation between the text &amp;quot;Number of Walls:&amp;quot; and the actual number of walls (using the len() function)&lt;br /&gt;
&lt;br /&gt;
To concatenate the values, they must be of the same type. The number obtained by the len() function being of integer type, we will use the str() function to transform it into a string.&lt;br /&gt;
&lt;br /&gt;
This gives:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str(len(walls))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We concatenate using the + symbol between the two parts of the same type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we enter this concatenation in the print() function.&lt;br /&gt;
&lt;br /&gt;
Here is the result:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python14.jpg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== The &amp;quot;right method&amp;quot;. ==&lt;br /&gt;
&lt;br /&gt;
The script as presented by the Python developer for archicad:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
&lt;br /&gt;
print(f'Number of Walls: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Compared to the &amp;quot;Raw&amp;quot; method that I presented to you just before:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It's longer in the official way, but that's for the sake of the script!&lt;br /&gt;
&lt;br /&gt;
=== The part to keep for any script ===&lt;br /&gt;
&lt;br /&gt;
In general, all your python scripts for archicad must include these first lines:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Why do you want to do this? Simply to avoid writing long commands, as seen before.&lt;br /&gt;
This allows to use ''acc'' instead of ''ACConnection.connect().commands''.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python15.jpeg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This allows the whole script to use only ''acc.CommandName'', ''acu.UtilityName'' or ''act.TypeName''.&lt;br /&gt;
&lt;br /&gt;
The instruction &amp;quot;''assert''&amp;quot; is a code help: if the connection with archicad is not successful, the script will give an error of type &amp;quot;''AssertionError''&amp;quot; which will allow to understand where the error comes from more easily.&lt;br /&gt;
&lt;br /&gt;
For the text part, concatenation is not recommended in Python, it is advised to use the &amp;quot;''f-Strings''&amp;quot; method&lt;br /&gt;
It starts with an ''f'' followed by the text between two quotation marks (''&amp;lt;nowiki&amp;gt;'&amp;lt;/nowiki&amp;gt;''). The fixed text is written as is and the variables are written between braces (''{}'').&lt;br /&gt;
&lt;br /&gt;
== Adding functions to our script ==&lt;br /&gt;
&lt;br /&gt;
Here are some examples of possible script improvements.&lt;br /&gt;
&lt;br /&gt;
=== A pop-up message to display the result ===&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python16.jpeg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is the corresponding code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
from tkinter import *&lt;br /&gt;
from tkinter import messagebox&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
&lt;br /&gt;
messagebox.showinfo(&amp;quot;Informations&amp;quot;,f'Number of Walls: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== What are the changes? ====&lt;br /&gt;
&lt;br /&gt;
===== Importing a new module =====&lt;br /&gt;
We import the tkinter module at the beginning, as well as message box. This is written:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from tkinter import *&lt;br /&gt;
from tkinter import messagebox&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Using messagebox =====&lt;br /&gt;
&lt;br /&gt;
To display a pop-up message, we will use the messagebox function previously imported and its showinfo function (other types of functions exist: https://docs.python.org/3/library/tkinter.messagebox.html).&lt;br /&gt;
&lt;br /&gt;
This function asks for at least 1 piece of information; the text that will be displayed in the title of the Pop-Up. The second text will be displayed in the body of the text. These two texts must be separated by a comma.&lt;br /&gt;
&lt;br /&gt;
This gives in our case:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
messagebox.showinfo(&amp;quot;Informations&amp;quot;,f'Number of Walls: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Copy the result to the clipboard ===&lt;br /&gt;
&lt;br /&gt;
In this version, the result is displayed in the console and copied to the clipboard. You just have to do cmd+v or ctrl+v to paste the result where you want.&lt;br /&gt;
&lt;br /&gt;
To find this part of the code, I had to do some google research to find a methodology that doesn't require the installation of an add-on (Pyperclip) on my computer. It appears to be very heavy to use tkinter for this, but it works!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
from tkinter import *&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
text = f'Number of Walls: {len(walls)}'&lt;br /&gt;
&lt;br /&gt;
print(text)&lt;br /&gt;
&lt;br /&gt;
r = Tk()&lt;br /&gt;
r.clipboard_clear()&lt;br /&gt;
r.clipboard_append(text)&lt;br /&gt;
r.after(300, r.destroy)&lt;br /&gt;
r.mainloop()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mathias J</name></author>
		
	</entry>
	<entry>
		<id>http://wiki.archi-cadlink.fr/index.php?title=Script_in_python_for_archicad_101&amp;diff=67</id>
		<title>Script in python for archicad 101</title>
		<link rel="alternate" type="text/html" href="http://wiki.archi-cadlink.fr/index.php?title=Script_in_python_for_archicad_101&amp;diff=67"/>
		<updated>2023-02-28T21:52:02Z</updated>

		<summary type="html">&lt;p&gt;Mathias J : /* 3rd Line Analysis */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This article follows the[[Introduction to the use of the Archicad-Python connection]].&lt;br /&gt;
&lt;br /&gt;
This first script is directly taken from the tutorial of Tibor Lorántfy, a former developer at graphisoft :&lt;br /&gt;
&lt;br /&gt;
https://archicadapi.graphisoft.com/getting-started-with-archicad-python-connection&lt;br /&gt;
&lt;br /&gt;
This first script will display in the archicad console the number of walls in the project.&lt;br /&gt;
&lt;br /&gt;
I will first show you a &amp;quot;raw&amp;quot; version of this script, then the optimal version as proposed by Tibor, and finally propose a graphical interface (very light) with Tkinter.&lt;br /&gt;
&lt;br /&gt;
Let's start!&lt;br /&gt;
&lt;br /&gt;
== &amp;quot;raw&amp;quot; version ==&lt;br /&gt;
This version is proposed to better explain the methodology, but it is not at all recommended. This is the script written with the minimum number of lines possible.&lt;br /&gt;
&lt;br /&gt;
It is done... in 3 lines!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 1st Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Generally speaking, a python script does not load all the functionality allowed by python, and it is necessary to indicate the additional modules that you wish to integrate at the beginning of the script.&lt;br /&gt;
&lt;br /&gt;
It can be a module allowing to work with excel files (for example openpyxl):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from openpyxl import Workbook&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can also be a module allowing to create a graphic interface (for example Tkinter):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from tkinter import *&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
They can be modules allowing to use images, create randomness, etc, etc.&lt;br /&gt;
&lt;br /&gt;
These modules are sometimes already integrated in the basic installation of python and do not require additional installation, but must still be called (this is the case of the graphical interface Tkinter), and sometimes it is necessary to have previously installed this additional module.&lt;br /&gt;
&lt;br /&gt;
In our case, ''this first line is absolutely necessary for all python scripts for archicad'': It tells python to load the python/archicad module installed on the computer which will allow access to all commands dedicated to archicad.&lt;br /&gt;
&lt;br /&gt;
=== 2nd Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this second line, we create a variable named &amp;quot;walls&amp;quot; and we use the command to get all the elements created with the wall tool. I have drawn in my archicad file 4 walls on the ground floor.&lt;br /&gt;
&lt;br /&gt;
==== The command to retrieve the elements according to their type ====&lt;br /&gt;
&lt;br /&gt;
To get all the walls of the project we will use the command GetElementsByType().&lt;br /&gt;
&lt;br /&gt;
To get more information about this function you can either go to the python site for archicad:&lt;br /&gt;
https://archicadapi.graphisoft.com/archicadPythonPackage/archicad.releases.ac24.html#archicad.releases.ac24.b2310commands.Commands.GetElementsByType&lt;br /&gt;
&lt;br /&gt;
or go to Visual Studio code:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python11.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Visual Studio Code, we have:&lt;br /&gt;
# the name of the command (1),&lt;br /&gt;
# what it does (2)&lt;br /&gt;
# the type of function (3): a command,&lt;br /&gt;
# the type of argument to be put between the () and in what form (4): here a string indicating the type of the element. So you have to write the type between two quotation marks &amp;quot; &amp;quot;.&lt;br /&gt;
# the result that will be returned (5): a list of type &amp;quot;ElementIdArrayItem&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== How to launch the command? ====&lt;br /&gt;
If we follow the previous part we must therefore write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GetElementsByType(&amp;quot;Wall&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To launch this function, however, you need to add some information:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python12.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We tell python that we want to use the python module for archicad (1), then that we want to connect to archicad (2), that we want to use a command (3) and finally which command we want to use (4).&lt;br /&gt;
&lt;br /&gt;
==== The result obtained with the 2nd line ====&lt;br /&gt;
&lt;br /&gt;
We have created a walls variable that retrieves the result of the GetElementsByType() command&lt;br /&gt;
&lt;br /&gt;
If we used the &amp;quot;print&amp;quot; function to see what the &amp;quot;walls&amp;quot; variable retrieves, it would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[ElementIdArrayItem {'elementId': {'guid': '86E4F938-8783-4E1C-881E-91C60E902526'}}, ElementIdArrayItem {'elementId': {'guid': 'E794D806-E94C-49D1-9854-A238F49D4AE3'}}, ElementIdArrayItem {'elementId': {'guid': 'B848DBB3-E432-4AEE-8AD1-A8FB0F559BBE'}}, ElementIdArrayItem {'elementId': {'guid': 'FF09403B-BA6D-43D3-B936-6C44AD229F85'}}]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is &amp;quot;almost&amp;quot; JSON. If we &amp;quot;clean up&amp;quot; this answer a bit to turn it into JSON we get this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
        &amp;quot;elements&amp;quot;: [&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;86E4F938-8783-4E1C-881E-91C60E902526&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;E794D806-E94C-49D1-9854-A238F49D4AE3&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;B848DBB3-E432-4AEE-8AD1-A8FB0F559BBE&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;FF09403B-BA6D-43D3-B936-6C44AD229F85&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        ]&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Schematically, this is what it looks like:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python13.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This means that we have a list of 4 values (our 4 walls), going from index 0 to index 3 (which corresponds to the 4 walls: wall index 0, wall index 1, wall index 2, wall index 3 )&lt;br /&gt;
&lt;br /&gt;
This notion of index (in 1) on the schema is not explicitly indicated in the JSON format, it is an automatic attribution: the first value is assigned index 0, the second index 1, etc... Like a python list.&lt;br /&gt;
&lt;br /&gt;
To each of these elements is associated a GUID: it is a unique identifier generated by the software that allows to find this element. This identifier remains constant and will not change.&lt;br /&gt;
&lt;br /&gt;
So we obtain a list of 4 unique identifiers (GUID) corresponding to the 4 walls of the project.&lt;br /&gt;
&lt;br /&gt;
=== 3rd Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
print('Number of Walls: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This third line is the simplest, it is pure python, and very basic.&lt;br /&gt;
&lt;br /&gt;
==== The len() function which counts the number of elements ====&lt;br /&gt;
&lt;br /&gt;
If we use len on the walls variable, this will be written:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
len(walls)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and will give as result the number of elements in the list, that is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The print() function and a concatenation ==== &lt;br /&gt;
&lt;br /&gt;
We use the print() function and we make a concatenation between the text &amp;quot;Number of Walls:&amp;quot; and the actual number of walls (using the len() function)&lt;br /&gt;
&lt;br /&gt;
To concatenate the values, they must be of the same type. The number obtained by the len() function being of integer type, we will use the str() function to transform it into a string.&lt;br /&gt;
&lt;br /&gt;
This gives:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str(len(walls))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We concatenate using the + symbol between the two parts of the same type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we enter this concatenation in the print() function.&lt;br /&gt;
&lt;br /&gt;
Here is the result:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python14.jpg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== The &amp;quot;right method&amp;quot;. ==&lt;br /&gt;
&lt;br /&gt;
The script as presented by the Python developer for archicad:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
&lt;br /&gt;
print(f'Number of Walls: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Compared to the &amp;quot;Raw&amp;quot; method that I presented to you just before:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It's longer in the official way, but that's for the sake of the script!&lt;br /&gt;
&lt;br /&gt;
=== The part to keep for any script ===&lt;br /&gt;
&lt;br /&gt;
In general, all your python scripts for archicad must include these first lines:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Why do you want to do this? Simply to avoid writing long commands, as seen before.&lt;br /&gt;
This allows to use ''acc'' instead of ''ACConnection.connect().commands''.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python15.jpeg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This allows the whole script to use only ''acc.CommandName'', ''acu.UtilityName'' or ''act.TypeName''.&lt;br /&gt;
&lt;br /&gt;
The instruction &amp;quot;''assert''&amp;quot; is a code help: if the connection with archicad is not successful, the script will give an error of type &amp;quot;''AssertionError''&amp;quot; which will allow to understand where the error comes from more easily.&lt;br /&gt;
&lt;br /&gt;
For the text part, concatenation is not recommended in Python, it is advised to use the &amp;quot;''f-Strings''&amp;quot; method&lt;br /&gt;
It starts with an ''f'' followed by the text between two quotation marks (''&amp;lt;nowiki&amp;gt;'&amp;lt;/nowiki&amp;gt;''). The fixed text is written as is and the variables are written between braces (''{}'').&lt;br /&gt;
&lt;br /&gt;
== Adding functions to our script ==&lt;br /&gt;
&lt;br /&gt;
Here are some examples of possible script improvements.&lt;br /&gt;
&lt;br /&gt;
=== A pop-up message to display the result ===&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python16.jpeg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is the corresponding code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
from tkinter import *&lt;br /&gt;
from tkinter import messagebox&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
&lt;br /&gt;
messagebox.showinfo(&amp;quot;Informations&amp;quot;,f'Number of Walls: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== What are the changes? ====&lt;br /&gt;
&lt;br /&gt;
===== Importing a new module =====&lt;br /&gt;
We import the tkinter module at the beginning, as well as message box. This is written:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from tkinter import *&lt;br /&gt;
from tkinter import messagebox&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Using messagebox =====&lt;br /&gt;
&lt;br /&gt;
To display a pop-up message, we will use the messagebox function previously imported and its showinfo function (other types of functions exist: https://docs.python.org/3/library/tkinter.messagebox.html).&lt;br /&gt;
&lt;br /&gt;
This function asks for at least 1 piece of information; the text that will be displayed in the title of the Pop-Up. The second text will be displayed in the body of the text. These two texts must be separated by a comma.&lt;br /&gt;
&lt;br /&gt;
This gives in our case:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
messagebox.showinfo(&amp;quot;Informations&amp;quot;,f'Number of Walls: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Copy the result to the clipboard ===&lt;br /&gt;
&lt;br /&gt;
In this version, the result is displayed in the console and copied to the clipboard. You just have to do cmd+v or ctrl+v to paste the result where you want.&lt;br /&gt;
&lt;br /&gt;
To find this part of the code, I had to do some google research to find a methodology that doesn't require the installation of an add-on (Pyperclip) on my computer. It appears to be very heavy to use tkinter for this, but it works!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
from tkinter import *&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
text = f'Number of Walls: {len(walls)}'&lt;br /&gt;
&lt;br /&gt;
print(text)&lt;br /&gt;
&lt;br /&gt;
r = Tk()&lt;br /&gt;
r.clipboard_clear()&lt;br /&gt;
r.clipboard_append(text)&lt;br /&gt;
r.after(300, r.destroy)&lt;br /&gt;
r.mainloop()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mathias J</name></author>
		
	</entry>
	<entry>
		<id>http://wiki.archi-cadlink.fr/index.php?title=Script_in_python_for_archicad_101&amp;diff=66</id>
		<title>Script in python for archicad 101</title>
		<link rel="alternate" type="text/html" href="http://wiki.archi-cadlink.fr/index.php?title=Script_in_python_for_archicad_101&amp;diff=66"/>
		<updated>2023-02-28T21:51:33Z</updated>

		<summary type="html">&lt;p&gt;Mathias J : /* Using messagebox */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This article follows the[[Introduction to the use of the Archicad-Python connection]].&lt;br /&gt;
&lt;br /&gt;
This first script is directly taken from the tutorial of Tibor Lorántfy, a former developer at graphisoft :&lt;br /&gt;
&lt;br /&gt;
https://archicadapi.graphisoft.com/getting-started-with-archicad-python-connection&lt;br /&gt;
&lt;br /&gt;
This first script will display in the archicad console the number of walls in the project.&lt;br /&gt;
&lt;br /&gt;
I will first show you a &amp;quot;raw&amp;quot; version of this script, then the optimal version as proposed by Tibor, and finally propose a graphical interface (very light) with Tkinter.&lt;br /&gt;
&lt;br /&gt;
Let's start!&lt;br /&gt;
&lt;br /&gt;
== &amp;quot;raw&amp;quot; version ==&lt;br /&gt;
This version is proposed to better explain the methodology, but it is not at all recommended. This is the script written with the minimum number of lines possible.&lt;br /&gt;
&lt;br /&gt;
It is done... in 3 lines!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 1st Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Generally speaking, a python script does not load all the functionality allowed by python, and it is necessary to indicate the additional modules that you wish to integrate at the beginning of the script.&lt;br /&gt;
&lt;br /&gt;
It can be a module allowing to work with excel files (for example openpyxl):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from openpyxl import Workbook&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can also be a module allowing to create a graphic interface (for example Tkinter):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from tkinter import *&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
They can be modules allowing to use images, create randomness, etc, etc.&lt;br /&gt;
&lt;br /&gt;
These modules are sometimes already integrated in the basic installation of python and do not require additional installation, but must still be called (this is the case of the graphical interface Tkinter), and sometimes it is necessary to have previously installed this additional module.&lt;br /&gt;
&lt;br /&gt;
In our case, ''this first line is absolutely necessary for all python scripts for archicad'': It tells python to load the python/archicad module installed on the computer which will allow access to all commands dedicated to archicad.&lt;br /&gt;
&lt;br /&gt;
=== 2nd Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this second line, we create a variable named &amp;quot;walls&amp;quot; and we use the command to get all the elements created with the wall tool. I have drawn in my archicad file 4 walls on the ground floor.&lt;br /&gt;
&lt;br /&gt;
==== The command to retrieve the elements according to their type ====&lt;br /&gt;
&lt;br /&gt;
To get all the walls of the project we will use the command GetElementsByType().&lt;br /&gt;
&lt;br /&gt;
To get more information about this function you can either go to the python site for archicad:&lt;br /&gt;
https://archicadapi.graphisoft.com/archicadPythonPackage/archicad.releases.ac24.html#archicad.releases.ac24.b2310commands.Commands.GetElementsByType&lt;br /&gt;
&lt;br /&gt;
or go to Visual Studio code:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python11.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Visual Studio Code, we have:&lt;br /&gt;
# the name of the command (1),&lt;br /&gt;
# what it does (2)&lt;br /&gt;
# the type of function (3): a command,&lt;br /&gt;
# the type of argument to be put between the () and in what form (4): here a string indicating the type of the element. So you have to write the type between two quotation marks &amp;quot; &amp;quot;.&lt;br /&gt;
# the result that will be returned (5): a list of type &amp;quot;ElementIdArrayItem&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== How to launch the command? ====&lt;br /&gt;
If we follow the previous part we must therefore write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GetElementsByType(&amp;quot;Wall&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To launch this function, however, you need to add some information:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python12.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We tell python that we want to use the python module for archicad (1), then that we want to connect to archicad (2), that we want to use a command (3) and finally which command we want to use (4).&lt;br /&gt;
&lt;br /&gt;
==== The result obtained with the 2nd line ====&lt;br /&gt;
&lt;br /&gt;
We have created a walls variable that retrieves the result of the GetElementsByType() command&lt;br /&gt;
&lt;br /&gt;
If we used the &amp;quot;print&amp;quot; function to see what the &amp;quot;walls&amp;quot; variable retrieves, it would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[ElementIdArrayItem {'elementId': {'guid': '86E4F938-8783-4E1C-881E-91C60E902526'}}, ElementIdArrayItem {'elementId': {'guid': 'E794D806-E94C-49D1-9854-A238F49D4AE3'}}, ElementIdArrayItem {'elementId': {'guid': 'B848DBB3-E432-4AEE-8AD1-A8FB0F559BBE'}}, ElementIdArrayItem {'elementId': {'guid': 'FF09403B-BA6D-43D3-B936-6C44AD229F85'}}]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is &amp;quot;almost&amp;quot; JSON. If we &amp;quot;clean up&amp;quot; this answer a bit to turn it into JSON we get this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
        &amp;quot;elements&amp;quot;: [&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;86E4F938-8783-4E1C-881E-91C60E902526&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;E794D806-E94C-49D1-9854-A238F49D4AE3&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;B848DBB3-E432-4AEE-8AD1-A8FB0F559BBE&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;FF09403B-BA6D-43D3-B936-6C44AD229F85&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        ]&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Schematically, this is what it looks like:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python13.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This means that we have a list of 4 values (our 4 walls), going from index 0 to index 3 (which corresponds to the 4 walls: wall index 0, wall index 1, wall index 2, wall index 3 )&lt;br /&gt;
&lt;br /&gt;
This notion of index (in 1) on the schema is not explicitly indicated in the JSON format, it is an automatic attribution: the first value is assigned index 0, the second index 1, etc... Like a python list.&lt;br /&gt;
&lt;br /&gt;
To each of these elements is associated a GUID: it is a unique identifier generated by the software that allows to find this element. This identifier remains constant and will not change.&lt;br /&gt;
&lt;br /&gt;
So we obtain a list of 4 unique identifiers (GUID) corresponding to the 4 walls of the project.&lt;br /&gt;
&lt;br /&gt;
=== 3rd Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This third line is the simplest, it is pure python, and very basic.&lt;br /&gt;
&lt;br /&gt;
==== The len() function which counts the number of elements ====&lt;br /&gt;
&lt;br /&gt;
If we use len on the walls variable, this will be written:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
len(walls)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and will give as result the number of elements in the list, that is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The print() function and a concatenation ==== &lt;br /&gt;
&lt;br /&gt;
We use the print() function and we make a concatenation between the text &amp;quot;Walls number:&amp;quot; and the actual number of walls (using the len() function)&lt;br /&gt;
&lt;br /&gt;
To concatenate the values, they must be of the same type. The number obtained by the len() function being of integer type, we will use the str() function to transform it into a string.&lt;br /&gt;
&lt;br /&gt;
This gives:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str(len(walls))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We concatenate using the + symbol between the two parts of the same type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we enter this concatenation in the print() function.&lt;br /&gt;
&lt;br /&gt;
Here is the result:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python14.jpg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== The &amp;quot;right method&amp;quot;. ==&lt;br /&gt;
&lt;br /&gt;
The script as presented by the Python developer for archicad:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
&lt;br /&gt;
print(f'Number of Walls: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Compared to the &amp;quot;Raw&amp;quot; method that I presented to you just before:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It's longer in the official way, but that's for the sake of the script!&lt;br /&gt;
&lt;br /&gt;
=== The part to keep for any script ===&lt;br /&gt;
&lt;br /&gt;
In general, all your python scripts for archicad must include these first lines:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Why do you want to do this? Simply to avoid writing long commands, as seen before.&lt;br /&gt;
This allows to use ''acc'' instead of ''ACConnection.connect().commands''.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python15.jpeg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This allows the whole script to use only ''acc.CommandName'', ''acu.UtilityName'' or ''act.TypeName''.&lt;br /&gt;
&lt;br /&gt;
The instruction &amp;quot;''assert''&amp;quot; is a code help: if the connection with archicad is not successful, the script will give an error of type &amp;quot;''AssertionError''&amp;quot; which will allow to understand where the error comes from more easily.&lt;br /&gt;
&lt;br /&gt;
For the text part, concatenation is not recommended in Python, it is advised to use the &amp;quot;''f-Strings''&amp;quot; method&lt;br /&gt;
It starts with an ''f'' followed by the text between two quotation marks (''&amp;lt;nowiki&amp;gt;'&amp;lt;/nowiki&amp;gt;''). The fixed text is written as is and the variables are written between braces (''{}'').&lt;br /&gt;
&lt;br /&gt;
== Adding functions to our script ==&lt;br /&gt;
&lt;br /&gt;
Here are some examples of possible script improvements.&lt;br /&gt;
&lt;br /&gt;
=== A pop-up message to display the result ===&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python16.jpeg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is the corresponding code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
from tkinter import *&lt;br /&gt;
from tkinter import messagebox&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
&lt;br /&gt;
messagebox.showinfo(&amp;quot;Informations&amp;quot;,f'Number of Walls: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== What are the changes? ====&lt;br /&gt;
&lt;br /&gt;
===== Importing a new module =====&lt;br /&gt;
We import the tkinter module at the beginning, as well as message box. This is written:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from tkinter import *&lt;br /&gt;
from tkinter import messagebox&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Using messagebox =====&lt;br /&gt;
&lt;br /&gt;
To display a pop-up message, we will use the messagebox function previously imported and its showinfo function (other types of functions exist: https://docs.python.org/3/library/tkinter.messagebox.html).&lt;br /&gt;
&lt;br /&gt;
This function asks for at least 1 piece of information; the text that will be displayed in the title of the Pop-Up. The second text will be displayed in the body of the text. These two texts must be separated by a comma.&lt;br /&gt;
&lt;br /&gt;
This gives in our case:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
messagebox.showinfo(&amp;quot;Informations&amp;quot;,f'Number of Walls: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Copy the result to the clipboard ===&lt;br /&gt;
&lt;br /&gt;
In this version, the result is displayed in the console and copied to the clipboard. You just have to do cmd+v or ctrl+v to paste the result where you want.&lt;br /&gt;
&lt;br /&gt;
To find this part of the code, I had to do some google research to find a methodology that doesn't require the installation of an add-on (Pyperclip) on my computer. It appears to be very heavy to use tkinter for this, but it works!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
from tkinter import *&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
text = f'Number of Walls: {len(walls)}'&lt;br /&gt;
&lt;br /&gt;
print(text)&lt;br /&gt;
&lt;br /&gt;
r = Tk()&lt;br /&gt;
r.clipboard_clear()&lt;br /&gt;
r.clipboard_append(text)&lt;br /&gt;
r.after(300, r.destroy)&lt;br /&gt;
r.mainloop()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mathias J</name></author>
		
	</entry>
	<entry>
		<id>http://wiki.archi-cadlink.fr/index.php?title=Script_in_python_for_archicad_101&amp;diff=65</id>
		<title>Script in python for archicad 101</title>
		<link rel="alternate" type="text/html" href="http://wiki.archi-cadlink.fr/index.php?title=Script_in_python_for_archicad_101&amp;diff=65"/>
		<updated>2023-02-28T21:51:21Z</updated>

		<summary type="html">&lt;p&gt;Mathias J : /* Copy the result to the clipboard */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This article follows the[[Introduction to the use of the Archicad-Python connection]].&lt;br /&gt;
&lt;br /&gt;
This first script is directly taken from the tutorial of Tibor Lorántfy, a former developer at graphisoft :&lt;br /&gt;
&lt;br /&gt;
https://archicadapi.graphisoft.com/getting-started-with-archicad-python-connection&lt;br /&gt;
&lt;br /&gt;
This first script will display in the archicad console the number of walls in the project.&lt;br /&gt;
&lt;br /&gt;
I will first show you a &amp;quot;raw&amp;quot; version of this script, then the optimal version as proposed by Tibor, and finally propose a graphical interface (very light) with Tkinter.&lt;br /&gt;
&lt;br /&gt;
Let's start!&lt;br /&gt;
&lt;br /&gt;
== &amp;quot;raw&amp;quot; version ==&lt;br /&gt;
This version is proposed to better explain the methodology, but it is not at all recommended. This is the script written with the minimum number of lines possible.&lt;br /&gt;
&lt;br /&gt;
It is done... in 3 lines!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 1st Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Generally speaking, a python script does not load all the functionality allowed by python, and it is necessary to indicate the additional modules that you wish to integrate at the beginning of the script.&lt;br /&gt;
&lt;br /&gt;
It can be a module allowing to work with excel files (for example openpyxl):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from openpyxl import Workbook&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can also be a module allowing to create a graphic interface (for example Tkinter):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from tkinter import *&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
They can be modules allowing to use images, create randomness, etc, etc.&lt;br /&gt;
&lt;br /&gt;
These modules are sometimes already integrated in the basic installation of python and do not require additional installation, but must still be called (this is the case of the graphical interface Tkinter), and sometimes it is necessary to have previously installed this additional module.&lt;br /&gt;
&lt;br /&gt;
In our case, ''this first line is absolutely necessary for all python scripts for archicad'': It tells python to load the python/archicad module installed on the computer which will allow access to all commands dedicated to archicad.&lt;br /&gt;
&lt;br /&gt;
=== 2nd Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this second line, we create a variable named &amp;quot;walls&amp;quot; and we use the command to get all the elements created with the wall tool. I have drawn in my archicad file 4 walls on the ground floor.&lt;br /&gt;
&lt;br /&gt;
==== The command to retrieve the elements according to their type ====&lt;br /&gt;
&lt;br /&gt;
To get all the walls of the project we will use the command GetElementsByType().&lt;br /&gt;
&lt;br /&gt;
To get more information about this function you can either go to the python site for archicad:&lt;br /&gt;
https://archicadapi.graphisoft.com/archicadPythonPackage/archicad.releases.ac24.html#archicad.releases.ac24.b2310commands.Commands.GetElementsByType&lt;br /&gt;
&lt;br /&gt;
or go to Visual Studio code:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python11.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Visual Studio Code, we have:&lt;br /&gt;
# the name of the command (1),&lt;br /&gt;
# what it does (2)&lt;br /&gt;
# the type of function (3): a command,&lt;br /&gt;
# the type of argument to be put between the () and in what form (4): here a string indicating the type of the element. So you have to write the type between two quotation marks &amp;quot; &amp;quot;.&lt;br /&gt;
# the result that will be returned (5): a list of type &amp;quot;ElementIdArrayItem&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== How to launch the command? ====&lt;br /&gt;
If we follow the previous part we must therefore write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GetElementsByType(&amp;quot;Wall&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To launch this function, however, you need to add some information:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python12.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We tell python that we want to use the python module for archicad (1), then that we want to connect to archicad (2), that we want to use a command (3) and finally which command we want to use (4).&lt;br /&gt;
&lt;br /&gt;
==== The result obtained with the 2nd line ====&lt;br /&gt;
&lt;br /&gt;
We have created a walls variable that retrieves the result of the GetElementsByType() command&lt;br /&gt;
&lt;br /&gt;
If we used the &amp;quot;print&amp;quot; function to see what the &amp;quot;walls&amp;quot; variable retrieves, it would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[ElementIdArrayItem {'elementId': {'guid': '86E4F938-8783-4E1C-881E-91C60E902526'}}, ElementIdArrayItem {'elementId': {'guid': 'E794D806-E94C-49D1-9854-A238F49D4AE3'}}, ElementIdArrayItem {'elementId': {'guid': 'B848DBB3-E432-4AEE-8AD1-A8FB0F559BBE'}}, ElementIdArrayItem {'elementId': {'guid': 'FF09403B-BA6D-43D3-B936-6C44AD229F85'}}]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is &amp;quot;almost&amp;quot; JSON. If we &amp;quot;clean up&amp;quot; this answer a bit to turn it into JSON we get this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
        &amp;quot;elements&amp;quot;: [&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;86E4F938-8783-4E1C-881E-91C60E902526&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;E794D806-E94C-49D1-9854-A238F49D4AE3&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;B848DBB3-E432-4AEE-8AD1-A8FB0F559BBE&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;FF09403B-BA6D-43D3-B936-6C44AD229F85&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        ]&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Schematically, this is what it looks like:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python13.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This means that we have a list of 4 values (our 4 walls), going from index 0 to index 3 (which corresponds to the 4 walls: wall index 0, wall index 1, wall index 2, wall index 3 )&lt;br /&gt;
&lt;br /&gt;
This notion of index (in 1) on the schema is not explicitly indicated in the JSON format, it is an automatic attribution: the first value is assigned index 0, the second index 1, etc... Like a python list.&lt;br /&gt;
&lt;br /&gt;
To each of these elements is associated a GUID: it is a unique identifier generated by the software that allows to find this element. This identifier remains constant and will not change.&lt;br /&gt;
&lt;br /&gt;
So we obtain a list of 4 unique identifiers (GUID) corresponding to the 4 walls of the project.&lt;br /&gt;
&lt;br /&gt;
=== 3rd Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This third line is the simplest, it is pure python, and very basic.&lt;br /&gt;
&lt;br /&gt;
==== The len() function which counts the number of elements ====&lt;br /&gt;
&lt;br /&gt;
If we use len on the walls variable, this will be written:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
len(walls)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and will give as result the number of elements in the list, that is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The print() function and a concatenation ==== &lt;br /&gt;
&lt;br /&gt;
We use the print() function and we make a concatenation between the text &amp;quot;Walls number:&amp;quot; and the actual number of walls (using the len() function)&lt;br /&gt;
&lt;br /&gt;
To concatenate the values, they must be of the same type. The number obtained by the len() function being of integer type, we will use the str() function to transform it into a string.&lt;br /&gt;
&lt;br /&gt;
This gives:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str(len(walls))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We concatenate using the + symbol between the two parts of the same type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we enter this concatenation in the print() function.&lt;br /&gt;
&lt;br /&gt;
Here is the result:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python14.jpg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== The &amp;quot;right method&amp;quot;. ==&lt;br /&gt;
&lt;br /&gt;
The script as presented by the Python developer for archicad:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
&lt;br /&gt;
print(f'Number of Walls: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Compared to the &amp;quot;Raw&amp;quot; method that I presented to you just before:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It's longer in the official way, but that's for the sake of the script!&lt;br /&gt;
&lt;br /&gt;
=== The part to keep for any script ===&lt;br /&gt;
&lt;br /&gt;
In general, all your python scripts for archicad must include these first lines:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Why do you want to do this? Simply to avoid writing long commands, as seen before.&lt;br /&gt;
This allows to use ''acc'' instead of ''ACConnection.connect().commands''.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python15.jpeg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This allows the whole script to use only ''acc.CommandName'', ''acu.UtilityName'' or ''act.TypeName''.&lt;br /&gt;
&lt;br /&gt;
The instruction &amp;quot;''assert''&amp;quot; is a code help: if the connection with archicad is not successful, the script will give an error of type &amp;quot;''AssertionError''&amp;quot; which will allow to understand where the error comes from more easily.&lt;br /&gt;
&lt;br /&gt;
For the text part, concatenation is not recommended in Python, it is advised to use the &amp;quot;''f-Strings''&amp;quot; method&lt;br /&gt;
It starts with an ''f'' followed by the text between two quotation marks (''&amp;lt;nowiki&amp;gt;'&amp;lt;/nowiki&amp;gt;''). The fixed text is written as is and the variables are written between braces (''{}'').&lt;br /&gt;
&lt;br /&gt;
== Adding functions to our script ==&lt;br /&gt;
&lt;br /&gt;
Here are some examples of possible script improvements.&lt;br /&gt;
&lt;br /&gt;
=== A pop-up message to display the result ===&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python16.jpeg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is the corresponding code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
from tkinter import *&lt;br /&gt;
from tkinter import messagebox&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
&lt;br /&gt;
messagebox.showinfo(&amp;quot;Informations&amp;quot;,f'Number of Walls: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== What are the changes? ====&lt;br /&gt;
&lt;br /&gt;
===== Importing a new module =====&lt;br /&gt;
We import the tkinter module at the beginning, as well as message box. This is written:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from tkinter import *&lt;br /&gt;
from tkinter import messagebox&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Using messagebox =====&lt;br /&gt;
&lt;br /&gt;
To display a pop-up message, we will use the messagebox function previously imported and its showinfo function (other types of functions exist: https://docs.python.org/3/library/tkinter.messagebox.html).&lt;br /&gt;
&lt;br /&gt;
This function asks for at least 1 piece of information; the text that will be displayed in the title of the Pop-Up. The second text will be displayed in the body of the text. These two texts must be separated by a comma.&lt;br /&gt;
&lt;br /&gt;
This gives in our case:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
messagebox.showinfo(&amp;quot;Informations&amp;quot;,f'Walls number: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Copy the result to the clipboard ===&lt;br /&gt;
&lt;br /&gt;
In this version, the result is displayed in the console and copied to the clipboard. You just have to do cmd+v or ctrl+v to paste the result where you want.&lt;br /&gt;
&lt;br /&gt;
To find this part of the code, I had to do some google research to find a methodology that doesn't require the installation of an add-on (Pyperclip) on my computer. It appears to be very heavy to use tkinter for this, but it works!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
from tkinter import *&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
text = f'Number of Walls: {len(walls)}'&lt;br /&gt;
&lt;br /&gt;
print(text)&lt;br /&gt;
&lt;br /&gt;
r = Tk()&lt;br /&gt;
r.clipboard_clear()&lt;br /&gt;
r.clipboard_append(text)&lt;br /&gt;
r.after(300, r.destroy)&lt;br /&gt;
r.mainloop()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mathias J</name></author>
		
	</entry>
	<entry>
		<id>http://wiki.archi-cadlink.fr/index.php?title=Script_in_python_for_archicad_101&amp;diff=64</id>
		<title>Script in python for archicad 101</title>
		<link rel="alternate" type="text/html" href="http://wiki.archi-cadlink.fr/index.php?title=Script_in_python_for_archicad_101&amp;diff=64"/>
		<updated>2023-02-28T21:51:10Z</updated>

		<summary type="html">&lt;p&gt;Mathias J : /* A pop-up message to display the result */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This article follows the[[Introduction to the use of the Archicad-Python connection]].&lt;br /&gt;
&lt;br /&gt;
This first script is directly taken from the tutorial of Tibor Lorántfy, a former developer at graphisoft :&lt;br /&gt;
&lt;br /&gt;
https://archicadapi.graphisoft.com/getting-started-with-archicad-python-connection&lt;br /&gt;
&lt;br /&gt;
This first script will display in the archicad console the number of walls in the project.&lt;br /&gt;
&lt;br /&gt;
I will first show you a &amp;quot;raw&amp;quot; version of this script, then the optimal version as proposed by Tibor, and finally propose a graphical interface (very light) with Tkinter.&lt;br /&gt;
&lt;br /&gt;
Let's start!&lt;br /&gt;
&lt;br /&gt;
== &amp;quot;raw&amp;quot; version ==&lt;br /&gt;
This version is proposed to better explain the methodology, but it is not at all recommended. This is the script written with the minimum number of lines possible.&lt;br /&gt;
&lt;br /&gt;
It is done... in 3 lines!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 1st Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Generally speaking, a python script does not load all the functionality allowed by python, and it is necessary to indicate the additional modules that you wish to integrate at the beginning of the script.&lt;br /&gt;
&lt;br /&gt;
It can be a module allowing to work with excel files (for example openpyxl):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from openpyxl import Workbook&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can also be a module allowing to create a graphic interface (for example Tkinter):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from tkinter import *&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
They can be modules allowing to use images, create randomness, etc, etc.&lt;br /&gt;
&lt;br /&gt;
These modules are sometimes already integrated in the basic installation of python and do not require additional installation, but must still be called (this is the case of the graphical interface Tkinter), and sometimes it is necessary to have previously installed this additional module.&lt;br /&gt;
&lt;br /&gt;
In our case, ''this first line is absolutely necessary for all python scripts for archicad'': It tells python to load the python/archicad module installed on the computer which will allow access to all commands dedicated to archicad.&lt;br /&gt;
&lt;br /&gt;
=== 2nd Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this second line, we create a variable named &amp;quot;walls&amp;quot; and we use the command to get all the elements created with the wall tool. I have drawn in my archicad file 4 walls on the ground floor.&lt;br /&gt;
&lt;br /&gt;
==== The command to retrieve the elements according to their type ====&lt;br /&gt;
&lt;br /&gt;
To get all the walls of the project we will use the command GetElementsByType().&lt;br /&gt;
&lt;br /&gt;
To get more information about this function you can either go to the python site for archicad:&lt;br /&gt;
https://archicadapi.graphisoft.com/archicadPythonPackage/archicad.releases.ac24.html#archicad.releases.ac24.b2310commands.Commands.GetElementsByType&lt;br /&gt;
&lt;br /&gt;
or go to Visual Studio code:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python11.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Visual Studio Code, we have:&lt;br /&gt;
# the name of the command (1),&lt;br /&gt;
# what it does (2)&lt;br /&gt;
# the type of function (3): a command,&lt;br /&gt;
# the type of argument to be put between the () and in what form (4): here a string indicating the type of the element. So you have to write the type between two quotation marks &amp;quot; &amp;quot;.&lt;br /&gt;
# the result that will be returned (5): a list of type &amp;quot;ElementIdArrayItem&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== How to launch the command? ====&lt;br /&gt;
If we follow the previous part we must therefore write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GetElementsByType(&amp;quot;Wall&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To launch this function, however, you need to add some information:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python12.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We tell python that we want to use the python module for archicad (1), then that we want to connect to archicad (2), that we want to use a command (3) and finally which command we want to use (4).&lt;br /&gt;
&lt;br /&gt;
==== The result obtained with the 2nd line ====&lt;br /&gt;
&lt;br /&gt;
We have created a walls variable that retrieves the result of the GetElementsByType() command&lt;br /&gt;
&lt;br /&gt;
If we used the &amp;quot;print&amp;quot; function to see what the &amp;quot;walls&amp;quot; variable retrieves, it would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[ElementIdArrayItem {'elementId': {'guid': '86E4F938-8783-4E1C-881E-91C60E902526'}}, ElementIdArrayItem {'elementId': {'guid': 'E794D806-E94C-49D1-9854-A238F49D4AE3'}}, ElementIdArrayItem {'elementId': {'guid': 'B848DBB3-E432-4AEE-8AD1-A8FB0F559BBE'}}, ElementIdArrayItem {'elementId': {'guid': 'FF09403B-BA6D-43D3-B936-6C44AD229F85'}}]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is &amp;quot;almost&amp;quot; JSON. If we &amp;quot;clean up&amp;quot; this answer a bit to turn it into JSON we get this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
        &amp;quot;elements&amp;quot;: [&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;86E4F938-8783-4E1C-881E-91C60E902526&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;E794D806-E94C-49D1-9854-A238F49D4AE3&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;B848DBB3-E432-4AEE-8AD1-A8FB0F559BBE&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;FF09403B-BA6D-43D3-B936-6C44AD229F85&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        ]&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Schematically, this is what it looks like:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python13.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This means that we have a list of 4 values (our 4 walls), going from index 0 to index 3 (which corresponds to the 4 walls: wall index 0, wall index 1, wall index 2, wall index 3 )&lt;br /&gt;
&lt;br /&gt;
This notion of index (in 1) on the schema is not explicitly indicated in the JSON format, it is an automatic attribution: the first value is assigned index 0, the second index 1, etc... Like a python list.&lt;br /&gt;
&lt;br /&gt;
To each of these elements is associated a GUID: it is a unique identifier generated by the software that allows to find this element. This identifier remains constant and will not change.&lt;br /&gt;
&lt;br /&gt;
So we obtain a list of 4 unique identifiers (GUID) corresponding to the 4 walls of the project.&lt;br /&gt;
&lt;br /&gt;
=== 3rd Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This third line is the simplest, it is pure python, and very basic.&lt;br /&gt;
&lt;br /&gt;
==== The len() function which counts the number of elements ====&lt;br /&gt;
&lt;br /&gt;
If we use len on the walls variable, this will be written:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
len(walls)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and will give as result the number of elements in the list, that is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The print() function and a concatenation ==== &lt;br /&gt;
&lt;br /&gt;
We use the print() function and we make a concatenation between the text &amp;quot;Walls number:&amp;quot; and the actual number of walls (using the len() function)&lt;br /&gt;
&lt;br /&gt;
To concatenate the values, they must be of the same type. The number obtained by the len() function being of integer type, we will use the str() function to transform it into a string.&lt;br /&gt;
&lt;br /&gt;
This gives:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str(len(walls))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We concatenate using the + symbol between the two parts of the same type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we enter this concatenation in the print() function.&lt;br /&gt;
&lt;br /&gt;
Here is the result:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python14.jpg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== The &amp;quot;right method&amp;quot;. ==&lt;br /&gt;
&lt;br /&gt;
The script as presented by the Python developer for archicad:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
&lt;br /&gt;
print(f'Number of Walls: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Compared to the &amp;quot;Raw&amp;quot; method that I presented to you just before:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It's longer in the official way, but that's for the sake of the script!&lt;br /&gt;
&lt;br /&gt;
=== The part to keep for any script ===&lt;br /&gt;
&lt;br /&gt;
In general, all your python scripts for archicad must include these first lines:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Why do you want to do this? Simply to avoid writing long commands, as seen before.&lt;br /&gt;
This allows to use ''acc'' instead of ''ACConnection.connect().commands''.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python15.jpeg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This allows the whole script to use only ''acc.CommandName'', ''acu.UtilityName'' or ''act.TypeName''.&lt;br /&gt;
&lt;br /&gt;
The instruction &amp;quot;''assert''&amp;quot; is a code help: if the connection with archicad is not successful, the script will give an error of type &amp;quot;''AssertionError''&amp;quot; which will allow to understand where the error comes from more easily.&lt;br /&gt;
&lt;br /&gt;
For the text part, concatenation is not recommended in Python, it is advised to use the &amp;quot;''f-Strings''&amp;quot; method&lt;br /&gt;
It starts with an ''f'' followed by the text between two quotation marks (''&amp;lt;nowiki&amp;gt;'&amp;lt;/nowiki&amp;gt;''). The fixed text is written as is and the variables are written between braces (''{}'').&lt;br /&gt;
&lt;br /&gt;
== Adding functions to our script ==&lt;br /&gt;
&lt;br /&gt;
Here are some examples of possible script improvements.&lt;br /&gt;
&lt;br /&gt;
=== A pop-up message to display the result ===&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python16.jpeg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is the corresponding code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
from tkinter import *&lt;br /&gt;
from tkinter import messagebox&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
&lt;br /&gt;
messagebox.showinfo(&amp;quot;Informations&amp;quot;,f'Number of Walls: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== What are the changes? ====&lt;br /&gt;
&lt;br /&gt;
===== Importing a new module =====&lt;br /&gt;
We import the tkinter module at the beginning, as well as message box. This is written:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from tkinter import *&lt;br /&gt;
from tkinter import messagebox&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Using messagebox =====&lt;br /&gt;
&lt;br /&gt;
To display a pop-up message, we will use the messagebox function previously imported and its showinfo function (other types of functions exist: https://docs.python.org/3/library/tkinter.messagebox.html).&lt;br /&gt;
&lt;br /&gt;
This function asks for at least 1 piece of information; the text that will be displayed in the title of the Pop-Up. The second text will be displayed in the body of the text. These two texts must be separated by a comma.&lt;br /&gt;
&lt;br /&gt;
This gives in our case:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
messagebox.showinfo(&amp;quot;Informations&amp;quot;,f'Walls number: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Copy the result to the clipboard ===&lt;br /&gt;
&lt;br /&gt;
In this version, the result is displayed in the console and copied to the clipboard. You just have to do cmd+v or ctrl+v to paste the result where you want.&lt;br /&gt;
&lt;br /&gt;
To find this part of the code, I had to do some google research to find a methodology that doesn't require the installation of an add-on (Pyperclip) on my computer. It appears to be very heavy to use tkinter for this, but it works!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
from tkinter import *&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
text = f'Walls number: {len(walls)}'&lt;br /&gt;
&lt;br /&gt;
print(text)&lt;br /&gt;
&lt;br /&gt;
r = Tk()&lt;br /&gt;
r.clipboard_clear()&lt;br /&gt;
r.clipboard_append(text)&lt;br /&gt;
r.after(300, r.destroy)&lt;br /&gt;
r.mainloop()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mathias J</name></author>
		
	</entry>
	<entry>
		<id>http://wiki.archi-cadlink.fr/index.php?title=Script_in_python_for_archicad_101&amp;diff=63</id>
		<title>Script in python for archicad 101</title>
		<link rel="alternate" type="text/html" href="http://wiki.archi-cadlink.fr/index.php?title=Script_in_python_for_archicad_101&amp;diff=63"/>
		<updated>2023-02-28T21:50:41Z</updated>

		<summary type="html">&lt;p&gt;Mathias J : /* Adding functions to our script */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This article follows the[[Introduction to the use of the Archicad-Python connection]].&lt;br /&gt;
&lt;br /&gt;
This first script is directly taken from the tutorial of Tibor Lorántfy, a former developer at graphisoft :&lt;br /&gt;
&lt;br /&gt;
https://archicadapi.graphisoft.com/getting-started-with-archicad-python-connection&lt;br /&gt;
&lt;br /&gt;
This first script will display in the archicad console the number of walls in the project.&lt;br /&gt;
&lt;br /&gt;
I will first show you a &amp;quot;raw&amp;quot; version of this script, then the optimal version as proposed by Tibor, and finally propose a graphical interface (very light) with Tkinter.&lt;br /&gt;
&lt;br /&gt;
Let's start!&lt;br /&gt;
&lt;br /&gt;
== &amp;quot;raw&amp;quot; version ==&lt;br /&gt;
This version is proposed to better explain the methodology, but it is not at all recommended. This is the script written with the minimum number of lines possible.&lt;br /&gt;
&lt;br /&gt;
It is done... in 3 lines!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 1st Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Generally speaking, a python script does not load all the functionality allowed by python, and it is necessary to indicate the additional modules that you wish to integrate at the beginning of the script.&lt;br /&gt;
&lt;br /&gt;
It can be a module allowing to work with excel files (for example openpyxl):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from openpyxl import Workbook&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can also be a module allowing to create a graphic interface (for example Tkinter):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from tkinter import *&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
They can be modules allowing to use images, create randomness, etc, etc.&lt;br /&gt;
&lt;br /&gt;
These modules are sometimes already integrated in the basic installation of python and do not require additional installation, but must still be called (this is the case of the graphical interface Tkinter), and sometimes it is necessary to have previously installed this additional module.&lt;br /&gt;
&lt;br /&gt;
In our case, ''this first line is absolutely necessary for all python scripts for archicad'': It tells python to load the python/archicad module installed on the computer which will allow access to all commands dedicated to archicad.&lt;br /&gt;
&lt;br /&gt;
=== 2nd Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this second line, we create a variable named &amp;quot;walls&amp;quot; and we use the command to get all the elements created with the wall tool. I have drawn in my archicad file 4 walls on the ground floor.&lt;br /&gt;
&lt;br /&gt;
==== The command to retrieve the elements according to their type ====&lt;br /&gt;
&lt;br /&gt;
To get all the walls of the project we will use the command GetElementsByType().&lt;br /&gt;
&lt;br /&gt;
To get more information about this function you can either go to the python site for archicad:&lt;br /&gt;
https://archicadapi.graphisoft.com/archicadPythonPackage/archicad.releases.ac24.html#archicad.releases.ac24.b2310commands.Commands.GetElementsByType&lt;br /&gt;
&lt;br /&gt;
or go to Visual Studio code:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python11.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Visual Studio Code, we have:&lt;br /&gt;
# the name of the command (1),&lt;br /&gt;
# what it does (2)&lt;br /&gt;
# the type of function (3): a command,&lt;br /&gt;
# the type of argument to be put between the () and in what form (4): here a string indicating the type of the element. So you have to write the type between two quotation marks &amp;quot; &amp;quot;.&lt;br /&gt;
# the result that will be returned (5): a list of type &amp;quot;ElementIdArrayItem&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== How to launch the command? ====&lt;br /&gt;
If we follow the previous part we must therefore write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GetElementsByType(&amp;quot;Wall&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To launch this function, however, you need to add some information:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python12.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We tell python that we want to use the python module for archicad (1), then that we want to connect to archicad (2), that we want to use a command (3) and finally which command we want to use (4).&lt;br /&gt;
&lt;br /&gt;
==== The result obtained with the 2nd line ====&lt;br /&gt;
&lt;br /&gt;
We have created a walls variable that retrieves the result of the GetElementsByType() command&lt;br /&gt;
&lt;br /&gt;
If we used the &amp;quot;print&amp;quot; function to see what the &amp;quot;walls&amp;quot; variable retrieves, it would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[ElementIdArrayItem {'elementId': {'guid': '86E4F938-8783-4E1C-881E-91C60E902526'}}, ElementIdArrayItem {'elementId': {'guid': 'E794D806-E94C-49D1-9854-A238F49D4AE3'}}, ElementIdArrayItem {'elementId': {'guid': 'B848DBB3-E432-4AEE-8AD1-A8FB0F559BBE'}}, ElementIdArrayItem {'elementId': {'guid': 'FF09403B-BA6D-43D3-B936-6C44AD229F85'}}]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is &amp;quot;almost&amp;quot; JSON. If we &amp;quot;clean up&amp;quot; this answer a bit to turn it into JSON we get this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
        &amp;quot;elements&amp;quot;: [&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;86E4F938-8783-4E1C-881E-91C60E902526&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;E794D806-E94C-49D1-9854-A238F49D4AE3&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;B848DBB3-E432-4AEE-8AD1-A8FB0F559BBE&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;FF09403B-BA6D-43D3-B936-6C44AD229F85&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        ]&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Schematically, this is what it looks like:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python13.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This means that we have a list of 4 values (our 4 walls), going from index 0 to index 3 (which corresponds to the 4 walls: wall index 0, wall index 1, wall index 2, wall index 3 )&lt;br /&gt;
&lt;br /&gt;
This notion of index (in 1) on the schema is not explicitly indicated in the JSON format, it is an automatic attribution: the first value is assigned index 0, the second index 1, etc... Like a python list.&lt;br /&gt;
&lt;br /&gt;
To each of these elements is associated a GUID: it is a unique identifier generated by the software that allows to find this element. This identifier remains constant and will not change.&lt;br /&gt;
&lt;br /&gt;
So we obtain a list of 4 unique identifiers (GUID) corresponding to the 4 walls of the project.&lt;br /&gt;
&lt;br /&gt;
=== 3rd Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This third line is the simplest, it is pure python, and very basic.&lt;br /&gt;
&lt;br /&gt;
==== The len() function which counts the number of elements ====&lt;br /&gt;
&lt;br /&gt;
If we use len on the walls variable, this will be written:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
len(walls)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and will give as result the number of elements in the list, that is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The print() function and a concatenation ==== &lt;br /&gt;
&lt;br /&gt;
We use the print() function and we make a concatenation between the text &amp;quot;Walls number:&amp;quot; and the actual number of walls (using the len() function)&lt;br /&gt;
&lt;br /&gt;
To concatenate the values, they must be of the same type. The number obtained by the len() function being of integer type, we will use the str() function to transform it into a string.&lt;br /&gt;
&lt;br /&gt;
This gives:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str(len(walls))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We concatenate using the + symbol between the two parts of the same type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we enter this concatenation in the print() function.&lt;br /&gt;
&lt;br /&gt;
Here is the result:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python14.jpg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== The &amp;quot;right method&amp;quot;. ==&lt;br /&gt;
&lt;br /&gt;
The script as presented by the Python developer for archicad:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
&lt;br /&gt;
print(f'Number of Walls: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Compared to the &amp;quot;Raw&amp;quot; method that I presented to you just before:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It's longer in the official way, but that's for the sake of the script!&lt;br /&gt;
&lt;br /&gt;
=== The part to keep for any script ===&lt;br /&gt;
&lt;br /&gt;
In general, all your python scripts for archicad must include these first lines:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Why do you want to do this? Simply to avoid writing long commands, as seen before.&lt;br /&gt;
This allows to use ''acc'' instead of ''ACConnection.connect().commands''.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python15.jpeg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This allows the whole script to use only ''acc.CommandName'', ''acu.UtilityName'' or ''act.TypeName''.&lt;br /&gt;
&lt;br /&gt;
The instruction &amp;quot;''assert''&amp;quot; is a code help: if the connection with archicad is not successful, the script will give an error of type &amp;quot;''AssertionError''&amp;quot; which will allow to understand where the error comes from more easily.&lt;br /&gt;
&lt;br /&gt;
For the text part, concatenation is not recommended in Python, it is advised to use the &amp;quot;''f-Strings''&amp;quot; method&lt;br /&gt;
It starts with an ''f'' followed by the text between two quotation marks (''&amp;lt;nowiki&amp;gt;'&amp;lt;/nowiki&amp;gt;''). The fixed text is written as is and the variables are written between braces (''{}'').&lt;br /&gt;
&lt;br /&gt;
== Adding functions to our script ==&lt;br /&gt;
&lt;br /&gt;
Here are some examples of possible script improvements.&lt;br /&gt;
&lt;br /&gt;
=== A pop-up message to display the result ===&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python16.jpeg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is the corresponding code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
from tkinter import *&lt;br /&gt;
from tkinter import messagebox&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
&lt;br /&gt;
messagebox.showinfo(&amp;quot;Informations&amp;quot;,f'Walls number: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== What are the changes? ====&lt;br /&gt;
&lt;br /&gt;
===== Importing a new module =====&lt;br /&gt;
We import the tkinter module at the beginning, as well as message box. This is written:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from tkinter import *&lt;br /&gt;
from tkinter import messagebox&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Using messagebox =====&lt;br /&gt;
&lt;br /&gt;
To display a pop-up message, we will use the messagebox function previously imported and its showinfo function (other types of functions exist: https://docs.python.org/3/library/tkinter.messagebox.html).&lt;br /&gt;
&lt;br /&gt;
This function asks for at least 1 piece of information; the text that will be displayed in the title of the Pop-Up. The second text will be displayed in the body of the text. These two texts must be separated by a comma.&lt;br /&gt;
&lt;br /&gt;
This gives in our case:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
messagebox.showinfo(&amp;quot;Informations&amp;quot;,f'Walls number: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Copy the result to the clipboard ===&lt;br /&gt;
&lt;br /&gt;
In this version, the result is displayed in the console and copied to the clipboard. You just have to do cmd+v or ctrl+v to paste the result where you want.&lt;br /&gt;
&lt;br /&gt;
To find this part of the code, I had to do some google research to find a methodology that doesn't require the installation of an add-on (Pyperclip) on my computer. It appears to be very heavy to use tkinter for this, but it works!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
from tkinter import *&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
text = f'Walls number: {len(walls)}'&lt;br /&gt;
&lt;br /&gt;
print(text)&lt;br /&gt;
&lt;br /&gt;
r = Tk()&lt;br /&gt;
r.clipboard_clear()&lt;br /&gt;
r.clipboard_append(text)&lt;br /&gt;
r.after(300, r.destroy)&lt;br /&gt;
r.mainloop()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mathias J</name></author>
		
	</entry>
	<entry>
		<id>http://wiki.archi-cadlink.fr/index.php?title=Script_in_python_for_archicad_101&amp;diff=62</id>
		<title>Script in python for archicad 101</title>
		<link rel="alternate" type="text/html" href="http://wiki.archi-cadlink.fr/index.php?title=Script_in_python_for_archicad_101&amp;diff=62"/>
		<updated>2023-02-28T21:50:23Z</updated>

		<summary type="html">&lt;p&gt;Mathias J : /* Using messagebox */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This article follows the[[Introduction to the use of the Archicad-Python connection]].&lt;br /&gt;
&lt;br /&gt;
This first script is directly taken from the tutorial of Tibor Lorántfy, a former developer at graphisoft :&lt;br /&gt;
&lt;br /&gt;
https://archicadapi.graphisoft.com/getting-started-with-archicad-python-connection&lt;br /&gt;
&lt;br /&gt;
This first script will display in the archicad console the number of walls in the project.&lt;br /&gt;
&lt;br /&gt;
I will first show you a &amp;quot;raw&amp;quot; version of this script, then the optimal version as proposed by Tibor, and finally propose a graphical interface (very light) with Tkinter.&lt;br /&gt;
&lt;br /&gt;
Let's start!&lt;br /&gt;
&lt;br /&gt;
== &amp;quot;raw&amp;quot; version ==&lt;br /&gt;
This version is proposed to better explain the methodology, but it is not at all recommended. This is the script written with the minimum number of lines possible.&lt;br /&gt;
&lt;br /&gt;
It is done... in 3 lines!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 1st Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Generally speaking, a python script does not load all the functionality allowed by python, and it is necessary to indicate the additional modules that you wish to integrate at the beginning of the script.&lt;br /&gt;
&lt;br /&gt;
It can be a module allowing to work with excel files (for example openpyxl):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from openpyxl import Workbook&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can also be a module allowing to create a graphic interface (for example Tkinter):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from tkinter import *&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
They can be modules allowing to use images, create randomness, etc, etc.&lt;br /&gt;
&lt;br /&gt;
These modules are sometimes already integrated in the basic installation of python and do not require additional installation, but must still be called (this is the case of the graphical interface Tkinter), and sometimes it is necessary to have previously installed this additional module.&lt;br /&gt;
&lt;br /&gt;
In our case, ''this first line is absolutely necessary for all python scripts for archicad'': It tells python to load the python/archicad module installed on the computer which will allow access to all commands dedicated to archicad.&lt;br /&gt;
&lt;br /&gt;
=== 2nd Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this second line, we create a variable named &amp;quot;walls&amp;quot; and we use the command to get all the elements created with the wall tool. I have drawn in my archicad file 4 walls on the ground floor.&lt;br /&gt;
&lt;br /&gt;
==== The command to retrieve the elements according to their type ====&lt;br /&gt;
&lt;br /&gt;
To get all the walls of the project we will use the command GetElementsByType().&lt;br /&gt;
&lt;br /&gt;
To get more information about this function you can either go to the python site for archicad:&lt;br /&gt;
https://archicadapi.graphisoft.com/archicadPythonPackage/archicad.releases.ac24.html#archicad.releases.ac24.b2310commands.Commands.GetElementsByType&lt;br /&gt;
&lt;br /&gt;
or go to Visual Studio code:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python11.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Visual Studio Code, we have:&lt;br /&gt;
# the name of the command (1),&lt;br /&gt;
# what it does (2)&lt;br /&gt;
# the type of function (3): a command,&lt;br /&gt;
# the type of argument to be put between the () and in what form (4): here a string indicating the type of the element. So you have to write the type between two quotation marks &amp;quot; &amp;quot;.&lt;br /&gt;
# the result that will be returned (5): a list of type &amp;quot;ElementIdArrayItem&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== How to launch the command? ====&lt;br /&gt;
If we follow the previous part we must therefore write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GetElementsByType(&amp;quot;Wall&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To launch this function, however, you need to add some information:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python12.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We tell python that we want to use the python module for archicad (1), then that we want to connect to archicad (2), that we want to use a command (3) and finally which command we want to use (4).&lt;br /&gt;
&lt;br /&gt;
==== The result obtained with the 2nd line ====&lt;br /&gt;
&lt;br /&gt;
We have created a walls variable that retrieves the result of the GetElementsByType() command&lt;br /&gt;
&lt;br /&gt;
If we used the &amp;quot;print&amp;quot; function to see what the &amp;quot;walls&amp;quot; variable retrieves, it would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[ElementIdArrayItem {'elementId': {'guid': '86E4F938-8783-4E1C-881E-91C60E902526'}}, ElementIdArrayItem {'elementId': {'guid': 'E794D806-E94C-49D1-9854-A238F49D4AE3'}}, ElementIdArrayItem {'elementId': {'guid': 'B848DBB3-E432-4AEE-8AD1-A8FB0F559BBE'}}, ElementIdArrayItem {'elementId': {'guid': 'FF09403B-BA6D-43D3-B936-6C44AD229F85'}}]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is &amp;quot;almost&amp;quot; JSON. If we &amp;quot;clean up&amp;quot; this answer a bit to turn it into JSON we get this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
        &amp;quot;elements&amp;quot;: [&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;86E4F938-8783-4E1C-881E-91C60E902526&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;E794D806-E94C-49D1-9854-A238F49D4AE3&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;B848DBB3-E432-4AEE-8AD1-A8FB0F559BBE&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;FF09403B-BA6D-43D3-B936-6C44AD229F85&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        ]&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Schematically, this is what it looks like:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python13.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This means that we have a list of 4 values (our 4 walls), going from index 0 to index 3 (which corresponds to the 4 walls: wall index 0, wall index 1, wall index 2, wall index 3 )&lt;br /&gt;
&lt;br /&gt;
This notion of index (in 1) on the schema is not explicitly indicated in the JSON format, it is an automatic attribution: the first value is assigned index 0, the second index 1, etc... Like a python list.&lt;br /&gt;
&lt;br /&gt;
To each of these elements is associated a GUID: it is a unique identifier generated by the software that allows to find this element. This identifier remains constant and will not change.&lt;br /&gt;
&lt;br /&gt;
So we obtain a list of 4 unique identifiers (GUID) corresponding to the 4 walls of the project.&lt;br /&gt;
&lt;br /&gt;
=== 3rd Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This third line is the simplest, it is pure python, and very basic.&lt;br /&gt;
&lt;br /&gt;
==== The len() function which counts the number of elements ====&lt;br /&gt;
&lt;br /&gt;
If we use len on the walls variable, this will be written:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
len(walls)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and will give as result the number of elements in the list, that is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The print() function and a concatenation ==== &lt;br /&gt;
&lt;br /&gt;
We use the print() function and we make a concatenation between the text &amp;quot;Walls number:&amp;quot; and the actual number of walls (using the len() function)&lt;br /&gt;
&lt;br /&gt;
To concatenate the values, they must be of the same type. The number obtained by the len() function being of integer type, we will use the str() function to transform it into a string.&lt;br /&gt;
&lt;br /&gt;
This gives:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str(len(walls))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We concatenate using the + symbol between the two parts of the same type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we enter this concatenation in the print() function.&lt;br /&gt;
&lt;br /&gt;
Here is the result:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python14.jpg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== The &amp;quot;right method&amp;quot;. ==&lt;br /&gt;
&lt;br /&gt;
The script as presented by the Python developer for archicad:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
&lt;br /&gt;
print(f'Number of Walls: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Compared to the &amp;quot;Raw&amp;quot; method that I presented to you just before:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It's longer in the official way, but that's for the sake of the script!&lt;br /&gt;
&lt;br /&gt;
=== The part to keep for any script ===&lt;br /&gt;
&lt;br /&gt;
In general, all your python scripts for archicad must include these first lines:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Why do you want to do this? Simply to avoid writing long commands, as seen before.&lt;br /&gt;
This allows to use ''acc'' instead of ''ACConnection.connect().commands''.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python15.jpeg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This allows the whole script to use only ''acc.CommandName'', ''acu.UtilityName'' or ''act.TypeName''.&lt;br /&gt;
&lt;br /&gt;
The instruction &amp;quot;''assert''&amp;quot; is a code help: if the connection with archicad is not successful, the script will give an error of type &amp;quot;''AssertionError''&amp;quot; which will allow to understand where the error comes from more easily.&lt;br /&gt;
&lt;br /&gt;
For the text part, concatenation is not recommended in Python, it is advised to use the &amp;quot;''f-Strings''&amp;quot; method&lt;br /&gt;
It starts with an ''f'' followed by the text between two quotation marks (''&amp;lt;nowiki&amp;gt;'&amp;lt;/nowiki&amp;gt;''). The fixed text is written as is and the variables are written between braces (''{}'').&lt;br /&gt;
&lt;br /&gt;
== Adding functions to our script ==&lt;br /&gt;
&lt;br /&gt;
Here are some examples of possible script improvements.&lt;br /&gt;
&lt;br /&gt;
=== A pop-up message to display the result ===&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python16.jpeg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is the corresponding code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
from tkinter import *&lt;br /&gt;
from tkinter import messagebox&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
&lt;br /&gt;
messagebox.showinfo(&amp;quot;Informations&amp;quot;,f'Nombre de murs: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== What are the changes? ====&lt;br /&gt;
&lt;br /&gt;
===== Importing a new module =====&lt;br /&gt;
We import the tkinter module at the beginning, as well as message box. This is written:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from tkinter import *&lt;br /&gt;
from tkinter import messagebox&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Using messagebox =====&lt;br /&gt;
&lt;br /&gt;
To display a pop-up message, we will use the messagebox function previously imported and its showinfo function (other types of functions exist: https://docs.python.org/3/library/tkinter.messagebox.html).&lt;br /&gt;
&lt;br /&gt;
This function asks for at least 1 piece of information; the text that will be displayed in the title of the Pop-Up. The second text will be displayed in the body of the text. These two texts must be separated by a comma.&lt;br /&gt;
&lt;br /&gt;
This gives in our case:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
messagebox.showinfo(&amp;quot;Informations&amp;quot;,f'Walls number: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Copy the result to the clipboard ===&lt;br /&gt;
&lt;br /&gt;
In this version, the result is displayed in the console and copied to the clipboard. You just have to do cmd+v or ctrl+v to paste the result where you want.&lt;br /&gt;
&lt;br /&gt;
To find this part of the code, I had to do some google research to find a methodology that doesn't require the installation of an add-on (Pyperclip) on my computer. It appears to be very heavy to use tkinter for this, but it works!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
from tkinter import *&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
text = f'Walls number: {len(walls)}'&lt;br /&gt;
&lt;br /&gt;
print(text)&lt;br /&gt;
&lt;br /&gt;
r = Tk()&lt;br /&gt;
r.clipboard_clear()&lt;br /&gt;
r.clipboard_append(text)&lt;br /&gt;
r.after(300, r.destroy)&lt;br /&gt;
r.mainloop()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mathias J</name></author>
		
	</entry>
	<entry>
		<id>http://wiki.archi-cadlink.fr/index.php?title=Script_in_python_for_archicad_101&amp;diff=61</id>
		<title>Script in python for archicad 101</title>
		<link rel="alternate" type="text/html" href="http://wiki.archi-cadlink.fr/index.php?title=Script_in_python_for_archicad_101&amp;diff=61"/>
		<updated>2023-02-28T21:50:05Z</updated>

		<summary type="html">&lt;p&gt;Mathias J : /* Copy the result to the clipboard */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This article follows the[[Introduction to the use of the Archicad-Python connection]].&lt;br /&gt;
&lt;br /&gt;
This first script is directly taken from the tutorial of Tibor Lorántfy, a former developer at graphisoft :&lt;br /&gt;
&lt;br /&gt;
https://archicadapi.graphisoft.com/getting-started-with-archicad-python-connection&lt;br /&gt;
&lt;br /&gt;
This first script will display in the archicad console the number of walls in the project.&lt;br /&gt;
&lt;br /&gt;
I will first show you a &amp;quot;raw&amp;quot; version of this script, then the optimal version as proposed by Tibor, and finally propose a graphical interface (very light) with Tkinter.&lt;br /&gt;
&lt;br /&gt;
Let's start!&lt;br /&gt;
&lt;br /&gt;
== &amp;quot;raw&amp;quot; version ==&lt;br /&gt;
This version is proposed to better explain the methodology, but it is not at all recommended. This is the script written with the minimum number of lines possible.&lt;br /&gt;
&lt;br /&gt;
It is done... in 3 lines!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 1st Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Generally speaking, a python script does not load all the functionality allowed by python, and it is necessary to indicate the additional modules that you wish to integrate at the beginning of the script.&lt;br /&gt;
&lt;br /&gt;
It can be a module allowing to work with excel files (for example openpyxl):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from openpyxl import Workbook&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can also be a module allowing to create a graphic interface (for example Tkinter):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from tkinter import *&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
They can be modules allowing to use images, create randomness, etc, etc.&lt;br /&gt;
&lt;br /&gt;
These modules are sometimes already integrated in the basic installation of python and do not require additional installation, but must still be called (this is the case of the graphical interface Tkinter), and sometimes it is necessary to have previously installed this additional module.&lt;br /&gt;
&lt;br /&gt;
In our case, ''this first line is absolutely necessary for all python scripts for archicad'': It tells python to load the python/archicad module installed on the computer which will allow access to all commands dedicated to archicad.&lt;br /&gt;
&lt;br /&gt;
=== 2nd Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this second line, we create a variable named &amp;quot;walls&amp;quot; and we use the command to get all the elements created with the wall tool. I have drawn in my archicad file 4 walls on the ground floor.&lt;br /&gt;
&lt;br /&gt;
==== The command to retrieve the elements according to their type ====&lt;br /&gt;
&lt;br /&gt;
To get all the walls of the project we will use the command GetElementsByType().&lt;br /&gt;
&lt;br /&gt;
To get more information about this function you can either go to the python site for archicad:&lt;br /&gt;
https://archicadapi.graphisoft.com/archicadPythonPackage/archicad.releases.ac24.html#archicad.releases.ac24.b2310commands.Commands.GetElementsByType&lt;br /&gt;
&lt;br /&gt;
or go to Visual Studio code:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python11.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Visual Studio Code, we have:&lt;br /&gt;
# the name of the command (1),&lt;br /&gt;
# what it does (2)&lt;br /&gt;
# the type of function (3): a command,&lt;br /&gt;
# the type of argument to be put between the () and in what form (4): here a string indicating the type of the element. So you have to write the type between two quotation marks &amp;quot; &amp;quot;.&lt;br /&gt;
# the result that will be returned (5): a list of type &amp;quot;ElementIdArrayItem&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== How to launch the command? ====&lt;br /&gt;
If we follow the previous part we must therefore write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GetElementsByType(&amp;quot;Wall&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To launch this function, however, you need to add some information:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python12.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We tell python that we want to use the python module for archicad (1), then that we want to connect to archicad (2), that we want to use a command (3) and finally which command we want to use (4).&lt;br /&gt;
&lt;br /&gt;
==== The result obtained with the 2nd line ====&lt;br /&gt;
&lt;br /&gt;
We have created a walls variable that retrieves the result of the GetElementsByType() command&lt;br /&gt;
&lt;br /&gt;
If we used the &amp;quot;print&amp;quot; function to see what the &amp;quot;walls&amp;quot; variable retrieves, it would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[ElementIdArrayItem {'elementId': {'guid': '86E4F938-8783-4E1C-881E-91C60E902526'}}, ElementIdArrayItem {'elementId': {'guid': 'E794D806-E94C-49D1-9854-A238F49D4AE3'}}, ElementIdArrayItem {'elementId': {'guid': 'B848DBB3-E432-4AEE-8AD1-A8FB0F559BBE'}}, ElementIdArrayItem {'elementId': {'guid': 'FF09403B-BA6D-43D3-B936-6C44AD229F85'}}]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is &amp;quot;almost&amp;quot; JSON. If we &amp;quot;clean up&amp;quot; this answer a bit to turn it into JSON we get this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
        &amp;quot;elements&amp;quot;: [&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;86E4F938-8783-4E1C-881E-91C60E902526&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;E794D806-E94C-49D1-9854-A238F49D4AE3&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;B848DBB3-E432-4AEE-8AD1-A8FB0F559BBE&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;FF09403B-BA6D-43D3-B936-6C44AD229F85&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        ]&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Schematically, this is what it looks like:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python13.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This means that we have a list of 4 values (our 4 walls), going from index 0 to index 3 (which corresponds to the 4 walls: wall index 0, wall index 1, wall index 2, wall index 3 )&lt;br /&gt;
&lt;br /&gt;
This notion of index (in 1) on the schema is not explicitly indicated in the JSON format, it is an automatic attribution: the first value is assigned index 0, the second index 1, etc... Like a python list.&lt;br /&gt;
&lt;br /&gt;
To each of these elements is associated a GUID: it is a unique identifier generated by the software that allows to find this element. This identifier remains constant and will not change.&lt;br /&gt;
&lt;br /&gt;
So we obtain a list of 4 unique identifiers (GUID) corresponding to the 4 walls of the project.&lt;br /&gt;
&lt;br /&gt;
=== 3rd Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This third line is the simplest, it is pure python, and very basic.&lt;br /&gt;
&lt;br /&gt;
==== The len() function which counts the number of elements ====&lt;br /&gt;
&lt;br /&gt;
If we use len on the walls variable, this will be written:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
len(walls)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and will give as result the number of elements in the list, that is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The print() function and a concatenation ==== &lt;br /&gt;
&lt;br /&gt;
We use the print() function and we make a concatenation between the text &amp;quot;Walls number:&amp;quot; and the actual number of walls (using the len() function)&lt;br /&gt;
&lt;br /&gt;
To concatenate the values, they must be of the same type. The number obtained by the len() function being of integer type, we will use the str() function to transform it into a string.&lt;br /&gt;
&lt;br /&gt;
This gives:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str(len(walls))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We concatenate using the + symbol between the two parts of the same type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we enter this concatenation in the print() function.&lt;br /&gt;
&lt;br /&gt;
Here is the result:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python14.jpg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== The &amp;quot;right method&amp;quot;. ==&lt;br /&gt;
&lt;br /&gt;
The script as presented by the Python developer for archicad:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
&lt;br /&gt;
print(f'Number of Walls: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Compared to the &amp;quot;Raw&amp;quot; method that I presented to you just before:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It's longer in the official way, but that's for the sake of the script!&lt;br /&gt;
&lt;br /&gt;
=== The part to keep for any script ===&lt;br /&gt;
&lt;br /&gt;
In general, all your python scripts for archicad must include these first lines:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Why do you want to do this? Simply to avoid writing long commands, as seen before.&lt;br /&gt;
This allows to use ''acc'' instead of ''ACConnection.connect().commands''.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python15.jpeg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This allows the whole script to use only ''acc.CommandName'', ''acu.UtilityName'' or ''act.TypeName''.&lt;br /&gt;
&lt;br /&gt;
The instruction &amp;quot;''assert''&amp;quot; is a code help: if the connection with archicad is not successful, the script will give an error of type &amp;quot;''AssertionError''&amp;quot; which will allow to understand where the error comes from more easily.&lt;br /&gt;
&lt;br /&gt;
For the text part, concatenation is not recommended in Python, it is advised to use the &amp;quot;''f-Strings''&amp;quot; method&lt;br /&gt;
It starts with an ''f'' followed by the text between two quotation marks (''&amp;lt;nowiki&amp;gt;'&amp;lt;/nowiki&amp;gt;''). The fixed text is written as is and the variables are written between braces (''{}'').&lt;br /&gt;
&lt;br /&gt;
== Adding functions to our script ==&lt;br /&gt;
&lt;br /&gt;
Here are some examples of possible script improvements.&lt;br /&gt;
&lt;br /&gt;
=== A pop-up message to display the result ===&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python16.jpeg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is the corresponding code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
from tkinter import *&lt;br /&gt;
from tkinter import messagebox&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
&lt;br /&gt;
messagebox.showinfo(&amp;quot;Informations&amp;quot;,f'Nombre de murs: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== What are the changes? ====&lt;br /&gt;
&lt;br /&gt;
===== Importing a new module =====&lt;br /&gt;
We import the tkinter module at the beginning, as well as message box. This is written:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from tkinter import *&lt;br /&gt;
from tkinter import messagebox&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Using messagebox =====&lt;br /&gt;
&lt;br /&gt;
To display a pop-up message, we will use the messagebox function previously imported and its showinfo function (other types of functions exist: https://docs.python.org/3/library/tkinter.messagebox.html).&lt;br /&gt;
&lt;br /&gt;
This function asks for at least 1 piece of information; the text that will be displayed in the title of the Pop-Up. The second text will be displayed in the body of the text. These two texts must be separated by a comma.&lt;br /&gt;
&lt;br /&gt;
This gives in our case:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
messagebox.showinfo(&amp;quot;Informations&amp;quot;,f'Nombre de murs: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Copy the result to the clipboard ===&lt;br /&gt;
&lt;br /&gt;
In this version, the result is displayed in the console and copied to the clipboard. You just have to do cmd+v or ctrl+v to paste the result where you want.&lt;br /&gt;
&lt;br /&gt;
To find this part of the code, I had to do some google research to find a methodology that doesn't require the installation of an add-on (Pyperclip) on my computer. It appears to be very heavy to use tkinter for this, but it works!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
from tkinter import *&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
text = f'Walls number: {len(walls)}'&lt;br /&gt;
&lt;br /&gt;
print(text)&lt;br /&gt;
&lt;br /&gt;
r = Tk()&lt;br /&gt;
r.clipboard_clear()&lt;br /&gt;
r.clipboard_append(text)&lt;br /&gt;
r.after(300, r.destroy)&lt;br /&gt;
r.mainloop()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mathias J</name></author>
		
	</entry>
	<entry>
		<id>http://wiki.archi-cadlink.fr/index.php?title=Script_in_python_for_archicad_101&amp;diff=60</id>
		<title>Script in python for archicad 101</title>
		<link rel="alternate" type="text/html" href="http://wiki.archi-cadlink.fr/index.php?title=Script_in_python_for_archicad_101&amp;diff=60"/>
		<updated>2023-02-28T21:49:30Z</updated>

		<summary type="html">&lt;p&gt;Mathias J : Page créée avec « This article follows theIntroduction to the use of the Archicad-Python connection.  This first script is directly taken from the tutorial of Tibor Lorántfy, a former... »&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This article follows the[[Introduction to the use of the Archicad-Python connection]].&lt;br /&gt;
&lt;br /&gt;
This first script is directly taken from the tutorial of Tibor Lorántfy, a former developer at graphisoft :&lt;br /&gt;
&lt;br /&gt;
https://archicadapi.graphisoft.com/getting-started-with-archicad-python-connection&lt;br /&gt;
&lt;br /&gt;
This first script will display in the archicad console the number of walls in the project.&lt;br /&gt;
&lt;br /&gt;
I will first show you a &amp;quot;raw&amp;quot; version of this script, then the optimal version as proposed by Tibor, and finally propose a graphical interface (very light) with Tkinter.&lt;br /&gt;
&lt;br /&gt;
Let's start!&lt;br /&gt;
&lt;br /&gt;
== &amp;quot;raw&amp;quot; version ==&lt;br /&gt;
This version is proposed to better explain the methodology, but it is not at all recommended. This is the script written with the minimum number of lines possible.&lt;br /&gt;
&lt;br /&gt;
It is done... in 3 lines!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 1st Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Generally speaking, a python script does not load all the functionality allowed by python, and it is necessary to indicate the additional modules that you wish to integrate at the beginning of the script.&lt;br /&gt;
&lt;br /&gt;
It can be a module allowing to work with excel files (for example openpyxl):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from openpyxl import Workbook&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can also be a module allowing to create a graphic interface (for example Tkinter):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from tkinter import *&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
They can be modules allowing to use images, create randomness, etc, etc.&lt;br /&gt;
&lt;br /&gt;
These modules are sometimes already integrated in the basic installation of python and do not require additional installation, but must still be called (this is the case of the graphical interface Tkinter), and sometimes it is necessary to have previously installed this additional module.&lt;br /&gt;
&lt;br /&gt;
In our case, ''this first line is absolutely necessary for all python scripts for archicad'': It tells python to load the python/archicad module installed on the computer which will allow access to all commands dedicated to archicad.&lt;br /&gt;
&lt;br /&gt;
=== 2nd Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this second line, we create a variable named &amp;quot;walls&amp;quot; and we use the command to get all the elements created with the wall tool. I have drawn in my archicad file 4 walls on the ground floor.&lt;br /&gt;
&lt;br /&gt;
==== The command to retrieve the elements according to their type ====&lt;br /&gt;
&lt;br /&gt;
To get all the walls of the project we will use the command GetElementsByType().&lt;br /&gt;
&lt;br /&gt;
To get more information about this function you can either go to the python site for archicad:&lt;br /&gt;
https://archicadapi.graphisoft.com/archicadPythonPackage/archicad.releases.ac24.html#archicad.releases.ac24.b2310commands.Commands.GetElementsByType&lt;br /&gt;
&lt;br /&gt;
or go to Visual Studio code:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python11.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Visual Studio Code, we have:&lt;br /&gt;
# the name of the command (1),&lt;br /&gt;
# what it does (2)&lt;br /&gt;
# the type of function (3): a command,&lt;br /&gt;
# the type of argument to be put between the () and in what form (4): here a string indicating the type of the element. So you have to write the type between two quotation marks &amp;quot; &amp;quot;.&lt;br /&gt;
# the result that will be returned (5): a list of type &amp;quot;ElementIdArrayItem&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== How to launch the command? ====&lt;br /&gt;
If we follow the previous part we must therefore write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GetElementsByType(&amp;quot;Wall&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To launch this function, however, you need to add some information:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python12.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We tell python that we want to use the python module for archicad (1), then that we want to connect to archicad (2), that we want to use a command (3) and finally which command we want to use (4).&lt;br /&gt;
&lt;br /&gt;
==== The result obtained with the 2nd line ====&lt;br /&gt;
&lt;br /&gt;
We have created a walls variable that retrieves the result of the GetElementsByType() command&lt;br /&gt;
&lt;br /&gt;
If we used the &amp;quot;print&amp;quot; function to see what the &amp;quot;walls&amp;quot; variable retrieves, it would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[ElementIdArrayItem {'elementId': {'guid': '86E4F938-8783-4E1C-881E-91C60E902526'}}, ElementIdArrayItem {'elementId': {'guid': 'E794D806-E94C-49D1-9854-A238F49D4AE3'}}, ElementIdArrayItem {'elementId': {'guid': 'B848DBB3-E432-4AEE-8AD1-A8FB0F559BBE'}}, ElementIdArrayItem {'elementId': {'guid': 'FF09403B-BA6D-43D3-B936-6C44AD229F85'}}]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is &amp;quot;almost&amp;quot; JSON. If we &amp;quot;clean up&amp;quot; this answer a bit to turn it into JSON we get this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
        &amp;quot;elements&amp;quot;: [&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;86E4F938-8783-4E1C-881E-91C60E902526&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;E794D806-E94C-49D1-9854-A238F49D4AE3&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;B848DBB3-E432-4AEE-8AD1-A8FB0F559BBE&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;elementId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;FF09403B-BA6D-43D3-B936-6C44AD229F85&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        ]&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Schematically, this is what it looks like:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python13.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This means that we have a list of 4 values (our 4 walls), going from index 0 to index 3 (which corresponds to the 4 walls: wall index 0, wall index 1, wall index 2, wall index 3 )&lt;br /&gt;
&lt;br /&gt;
This notion of index (in 1) on the schema is not explicitly indicated in the JSON format, it is an automatic attribution: the first value is assigned index 0, the second index 1, etc... Like a python list.&lt;br /&gt;
&lt;br /&gt;
To each of these elements is associated a GUID: it is a unique identifier generated by the software that allows to find this element. This identifier remains constant and will not change.&lt;br /&gt;
&lt;br /&gt;
So we obtain a list of 4 unique identifiers (GUID) corresponding to the 4 walls of the project.&lt;br /&gt;
&lt;br /&gt;
=== 3rd Line Analysis ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This third line is the simplest, it is pure python, and very basic.&lt;br /&gt;
&lt;br /&gt;
==== The len() function which counts the number of elements ====&lt;br /&gt;
&lt;br /&gt;
If we use len on the walls variable, this will be written:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
len(walls)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and will give as result the number of elements in the list, that is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The print() function and a concatenation ==== &lt;br /&gt;
&lt;br /&gt;
We use the print() function and we make a concatenation between the text &amp;quot;Walls number:&amp;quot; and the actual number of walls (using the len() function)&lt;br /&gt;
&lt;br /&gt;
To concatenate the values, they must be of the same type. The number obtained by the len() function being of integer type, we will use the str() function to transform it into a string.&lt;br /&gt;
&lt;br /&gt;
This gives:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
str(len(walls))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We concatenate using the + symbol between the two parts of the same type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we enter this concatenation in the print() function.&lt;br /&gt;
&lt;br /&gt;
Here is the result:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python14.jpg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== The &amp;quot;right method&amp;quot;. ==&lt;br /&gt;
&lt;br /&gt;
The script as presented by the Python developer for archicad:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
&lt;br /&gt;
print(f'Number of Walls: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Compared to the &amp;quot;Raw&amp;quot; method that I presented to you just before:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
walls = ACConnection.connect().commands.GetElementsByType('Wall')&lt;br /&gt;
print('Nombre de murs: ' + str(len(walls)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It's longer in the official way, but that's for the sake of the script!&lt;br /&gt;
&lt;br /&gt;
=== The part to keep for any script ===&lt;br /&gt;
&lt;br /&gt;
In general, all your python scripts for archicad must include these first lines:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Why do you want to do this? Simply to avoid writing long commands, as seen before.&lt;br /&gt;
This allows to use ''acc'' instead of ''ACConnection.connect().commands''.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python15.jpeg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This allows the whole script to use only ''acc.CommandName'', ''acu.UtilityName'' or ''act.TypeName''.&lt;br /&gt;
&lt;br /&gt;
The instruction &amp;quot;''assert''&amp;quot; is a code help: if the connection with archicad is not successful, the script will give an error of type &amp;quot;''AssertionError''&amp;quot; which will allow to understand where the error comes from more easily.&lt;br /&gt;
&lt;br /&gt;
For the text part, concatenation is not recommended in Python, it is advised to use the &amp;quot;''f-Strings''&amp;quot; method&lt;br /&gt;
It starts with an ''f'' followed by the text between two quotation marks (''&amp;lt;nowiki&amp;gt;'&amp;lt;/nowiki&amp;gt;''). The fixed text is written as is and the variables are written between braces (''{}'').&lt;br /&gt;
&lt;br /&gt;
== Adding functions to our script ==&lt;br /&gt;
&lt;br /&gt;
Here are some examples of possible script improvements.&lt;br /&gt;
&lt;br /&gt;
=== A pop-up message to display the result ===&lt;br /&gt;
&lt;br /&gt;
[[Fichier:python16.jpeg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is the corresponding code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
from tkinter import *&lt;br /&gt;
from tkinter import messagebox&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
&lt;br /&gt;
messagebox.showinfo(&amp;quot;Informations&amp;quot;,f'Nombre de murs: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== What are the changes? ====&lt;br /&gt;
&lt;br /&gt;
===== Importing a new module =====&lt;br /&gt;
We import the tkinter module at the beginning, as well as message box. This is written:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from tkinter import *&lt;br /&gt;
from tkinter import messagebox&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Using messagebox =====&lt;br /&gt;
&lt;br /&gt;
To display a pop-up message, we will use the messagebox function previously imported and its showinfo function (other types of functions exist: https://docs.python.org/3/library/tkinter.messagebox.html).&lt;br /&gt;
&lt;br /&gt;
This function asks for at least 1 piece of information; the text that will be displayed in the title of the Pop-Up. The second text will be displayed in the body of the text. These two texts must be separated by a comma.&lt;br /&gt;
&lt;br /&gt;
This gives in our case:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
messagebox.showinfo(&amp;quot;Informations&amp;quot;,f'Nombre de murs: {len(walls)}')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Copy the result to the clipboard ===&lt;br /&gt;
&lt;br /&gt;
In this version, the result is displayed in the console and copied to the clipboard. You just have to do cmd+v or ctrl+v to paste the result where you want.&lt;br /&gt;
&lt;br /&gt;
To find this part of the code, I had to do some google research to find a methodology that doesn't require the installation of an add-on (Pyperclip) on my computer. It appears to be very heavy to use tkinter for this, but it works!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from archicad import ACConnection&lt;br /&gt;
from tkinter import *&lt;br /&gt;
&lt;br /&gt;
conn = ACConnection.connect()&lt;br /&gt;
assert conn&lt;br /&gt;
&lt;br /&gt;
acc = conn.commands&lt;br /&gt;
act = conn.types&lt;br /&gt;
acu = conn.utilities&lt;br /&gt;
&lt;br /&gt;
walls = acc.GetElementsByType('Wall')&lt;br /&gt;
text = f'Nombre de murs: {len(walls)}'&lt;br /&gt;
&lt;br /&gt;
print(text)&lt;br /&gt;
&lt;br /&gt;
r = Tk()&lt;br /&gt;
r.clipboard_clear()&lt;br /&gt;
r.clipboard_append(text)&lt;br /&gt;
r.after(300, r.destroy)&lt;br /&gt;
r.mainloop()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mathias J</name></author>
		
	</entry>
	<entry>
		<id>http://wiki.archi-cadlink.fr/index.php?title=Introduction_to_the_use_of_the_Archicad-Python_connection&amp;diff=59</id>
		<title>Introduction to the use of the Archicad-Python connection</title>
		<link rel="alternate" type="text/html" href="http://wiki.archi-cadlink.fr/index.php?title=Introduction_to_the_use_of_the_Archicad-Python_connection&amp;diff=59"/>
		<updated>2023-02-28T21:30:37Z</updated>

		<summary type="html">&lt;p&gt;Mathias J : /* More... */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;((tutorial originally written by [https://www.archi-cadlink.fr/memberlist.php?mode=viewprofile&amp;amp;u=70 MathiasJ] on the [https://www.archi-cadlink.fr/ Archi-cadlink] forum and then re-formatted here). &lt;br /&gt;
== Introduction ==&lt;br /&gt;
=== What is python? ===&lt;br /&gt;
It is a programming language particularly used for the automation of simple but tedious tasks. It was created in 1991 and is now at version 3.12 (date of the post). In general, we speak of version 3 for the current version. It is the one used for archicad.&lt;br /&gt;
&lt;br /&gt;
To use python, you need to install it (voir le site officiel https://www.python.org/).&lt;br /&gt;
&lt;br /&gt;
=== How to work with python in archicad? ===&lt;br /&gt;
&lt;br /&gt;
* Install the latest version of the python language on the official website (Latest version 3.XX)&lt;br /&gt;
* Activate Python in the experimental options (Options &amp;gt; Working environment &amp;gt; Other options &amp;amp; check the box &amp;quot; Activate Python Palette&amp;quot;)&lt;br /&gt;
* Display the Python palette now available.&lt;br /&gt;
* Follow the indications of the palette to finalize the installation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery&amp;gt;&lt;br /&gt;
Fichier:Python1.png&lt;br /&gt;
Fichier:Python2.png&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== How does the connection work? ===&lt;br /&gt;
The connection works by using the python palette.&lt;br /&gt;
&lt;br /&gt;
This palette is composed of two parts: a part where you select your scripts (on top) and launch the desired script, and a part named &amp;quot;console&amp;quot; where the computer will give you information.&lt;br /&gt;
&lt;br /&gt;
==== The script section ====&lt;br /&gt;
We select the folder icon and indicate where the scripts to be used are located. A button is used to delete the folder, and another one is used to update the list.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python3.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To launch a script, just select the chosen script (1), and click on launch (2). You can also double click on the script name to launch it. &lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python4.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The console section ====&lt;br /&gt;
This is a very important part for the user of the script and indispensable for the coder.&lt;br /&gt;
&lt;br /&gt;
You can find here an indication of the launch of the script (1), the information related to the activation of the script (for example, desired information such as quantities, or informations such as error reports)(2), and if the script has finished its work (3).&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python5.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== How to create/modify a script? ===&lt;br /&gt;
For this, any text editor is needed (Notepad on PC for example). On Mac, you can double click on the script to open a dedicated code editor that gives you rudimentary tools to modify the script (see screenshot below), such as coloring according to the type, the number of the selected line, the possibility to &amp;quot;launch&amp;quot; the script.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python6.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, it is recommended to work on a dedicated code editor.&lt;br /&gt;
I recommend Visual Studio Code which works on Mac and PC (which has an extension allowing to work on GDL.)&lt;br /&gt;
The advantages of this type of tool are numerous.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python7.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some for coding in general, some specific to the python/archicad connection.&lt;br /&gt;
For example:&lt;br /&gt;
* All lines are numbered (bugs often indicate which line the script bugged, which helps to fix it),&lt;br /&gt;
* In case of &amp;quot;big&amp;quot; errors (a forgotten parenthesis, a missing line break...), the software alerts and helps to avoid stupid mistakes.&lt;br /&gt;
* After installing the dedicated package (https://pypi.org/project/archicad/#description), the software recognizes all the available commands, with additional information that helps to use them (on the needs of the command, and what will result from using this command).&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python8.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
For example, on Get2DBOundingBoxes, we learn that it is a command, that it needs the identifiers of the elements to be retrieved in the form ElementArrayItem and that it will give in return a list containing the 2D outlines of the elements or errors if there is none.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== HTTP &amp;amp; JSON ===&lt;br /&gt;
&lt;br /&gt;
archicad, but passes via Http using messages in JSON format (https://fr.wikipedia.org/wiki/JavaScript_Object_Notation).&lt;br /&gt;
&lt;br /&gt;
Graphisoft offers a package for python that &amp;quot;hides&amp;quot; the communication in JSON.&lt;br /&gt;
&lt;br /&gt;
For example, if you use the GetAllPropertyIds command which allows you to retrieve all the property identifiers of an archicad file, you will have to write in python:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
acc.GetAllPropertyIds(&amp;quot;UserDefined&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will be translated by the &amp;quot;package&amp;quot; into JSON in this form:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;command&amp;quot;: &amp;quot;API.GetAllPropertyIds&amp;quot;,&lt;br /&gt;
    &amp;quot;parameters&amp;quot;: {&lt;br /&gt;
        &amp;quot;propertyType&amp;quot;: &amp;quot;UserDefined&amp;quot;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the result will look like this (a list of unique identifiers - GUIDs - each corresponding to an archicad property):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;succeeded&amp;quot;: true,&lt;br /&gt;
    &amp;quot;result&amp;quot;: {&lt;br /&gt;
        &amp;quot;propertyIds&amp;quot;: [&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;E480E81E-EDE3-43FC-9C52-B55A4CA1A85C&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;13A61253-66A9-4494-9393-9E8F2E19D55E&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;BCB5813F-2115-4B8B-A12F-16CFE37C7B7F&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;6F4A46AC-AE91-47E6-BF4A-9F9AB01A4986&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;F6F67733-1DC1-442A-8CF4-ACD2DF7E62C6&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;52D7923A-E5D7-47DF-9319-834B2CB68A6C&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;331D26A3-8168-460C-B7F5-0FA11B596B60&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;78B73923-1B87-460B-8D9E-6E3041CF38D6&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;2FAB57AB-40D6-4B7B-A7F7-31FAE42BCFBD&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;3D9EF415-8D5E-42C3-999F-3CE138DF341F&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;9CC16F4D-9754-B744-B3F8-20BA074A3B2D&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        ]&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The result may look a bit cumbersome, but this form of JSON code allows to keep a hierarchical structure.&lt;br /&gt;
For example, if you copy and paste the &amp;quot;result&amp;quot; part into a JSON viewer that you can find on the internet, the lines of code look like this:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python9.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Conclusion: the results we receive may seem unclear at first, but we can extract the data we are interested in!&lt;br /&gt;
Also, before giving values to the script, we have to &amp;quot;package&amp;quot; them properly.&lt;br /&gt;
&lt;br /&gt;
==== Why didn't they create a direct connection without using JSON? ====&lt;br /&gt;
&lt;br /&gt;
During the first beta, the connection was made via python, without any intermediary.&lt;br /&gt;
The choice was finally made to use HTTP/JSON in order to leave the opportunity to develop bridges with any computer language later on. It would be &amp;quot;enough&amp;quot; to create a new package that allows to produce code in JSON.&lt;br /&gt;
&lt;br /&gt;
== How do I learn python? ==&lt;br /&gt;
I don't have a &amp;quot;high level&amp;quot;, I tried several times to get into it, and I finally found a quite effective resource in English called ''Automate the Boring Stuff with Python''. &lt;br /&gt;
It's a book that has the particularity to be available for free and legally on a website : https://automatetheboringstuff.com/&lt;br /&gt;
&lt;br /&gt;
Here are the chapters I read (and followed the small exercises)&lt;br /&gt;
=== The basics ===&lt;br /&gt;
https://automatetheboringstuff.com/2e/chapter1/&lt;br /&gt;
&lt;br /&gt;
* Data types (integers, floats, strings )&lt;br /&gt;
* The creation and use of variables using the = sign&lt;br /&gt;
* The creation of comments with the # symbol&lt;br /&gt;
* Important functions like len() to measure a number of elements and especially the print() function which is going to be the basis of the script development under python. It's the print() function that will allow to display information in the archicad console&lt;br /&gt;
&lt;br /&gt;
=== Conditional instructions ===&lt;br /&gt;
https://automatetheboringstuff.com/2e/chapter2/&lt;br /&gt;
&lt;br /&gt;
* Comparison operators, in particular the difference between == (equality check) and = (value assignment)&lt;br /&gt;
* The different conditions of type &amp;quot;If&amp;quot; &amp;quot;else&amp;quot; &amp;quot;elif&amp;quot; &amp;quot;while&amp;quot; &amp;quot;break&amp;quot; loops based on &amp;quot;for&amp;quot;...&lt;br /&gt;
* Import of modules (additional functions to python) that must be called.&lt;br /&gt;
&lt;br /&gt;
=== Functions ===&lt;br /&gt;
https://automatetheboringstuff.com/2e/chapter3/&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
https://automatetheboringstuff.com/2e/chapter4/&lt;br /&gt;
Lists are a way to store data. Each data has an index and a value. Indexes are integers and the first value of a list has index 0 (not 1).&lt;br /&gt;
&lt;br /&gt;
You have to learn how to retrieve a value from one (or more) indexes, adding or deleting values in this list.&lt;br /&gt;
&lt;br /&gt;
=== Dictionnary ===&lt;br /&gt;
https://automatetheboringstuff.com/2e/chapter5/&lt;br /&gt;
&lt;br /&gt;
Dictionaries are another way of storing data. Instead of having indexes in integer form, each value is associated with a &amp;quot;key&amp;quot;. This key can be of any type.&lt;br /&gt;
&lt;br /&gt;
=== Strings modifications ===&lt;br /&gt;
https://automatetheboringstuff.com/2e/chapter6/&lt;br /&gt;
&lt;br /&gt;
== What is the status of the python/archicad connection ? ==&lt;br /&gt;
The possibilities are (still) limited.&lt;br /&gt;
The archicad/python connection appeared in archicad 24, some functions appeared in 25 (access to bearer/non-bearer information; interior/exterior...) and in 26 (Possibility to retrieve the identifiers of the elements selected by the user...)&lt;br /&gt;
&lt;br /&gt;
In general, the users of this connection find that the development of this part of archicad is very slow.&lt;br /&gt;
&lt;br /&gt;
There is however a clue on the continuation of the development on this side, it is the RoadMap presented by Graphisoft which indicates automation for 2025.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python10.jpeg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===What exactly can we do?===&lt;br /&gt;
&lt;br /&gt;
To know what is possible, you have to look at:&lt;br /&gt;
&lt;br /&gt;
* the site dedicated to the JSON interface : https://archicadapi.graphisoft.com/JSONInterfaceDocumentation/#Introduction&lt;br /&gt;
* the site dedicated to the python &amp;quot;packer&amp;quot;  : https://archicadapi.graphisoft.com/archicadPythonPackage/archicad.html&lt;br /&gt;
&lt;br /&gt;
The site for JSON is a bit more easily understandable and gives a good overview.&lt;br /&gt;
Most of the commands start with &amp;quot;Get&amp;quot; followed by a text. These are commands to get information. For example get the name of :&lt;br /&gt;
a layer, a property, the value of this property etc...&lt;br /&gt;
&lt;br /&gt;
The commands that will really act in Archicad are much less numerous.&lt;br /&gt;
&lt;br /&gt;
The majority of these commands allow you to modify views and layouts:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DeleteNavigatorItems&lt;br /&gt;
RenameNavigatorItem&lt;br /&gt;
MoveNavigatorItem&lt;br /&gt;
CloneProjectMapItemToViewMap&lt;br /&gt;
CreateViewMapFolder&lt;br /&gt;
CreateLayoutSubset&lt;br /&gt;
CreateLayout&lt;br /&gt;
SetLayoutSettings&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And for the rest, only two commands:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SetClassificationsOfElements&lt;br /&gt;
SetPropertyValuesOfElements&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One to modify parameters of an archicad element (custom property or built-in parameter (BuiltIn) like layers...) the other is the classification of elements.&lt;br /&gt;
&lt;br /&gt;
It's not much!&lt;br /&gt;
&lt;br /&gt;
=== Tapir ===&lt;br /&gt;
https://www.archicad-api.com/&lt;br /&gt;
&lt;br /&gt;
Some archicad users decided to develop an opensource plugin to use in Grasshopper (Rhinoceros software tool) the possibilities offered by the python plugin.&lt;br /&gt;
&lt;br /&gt;
To increase the capabilities of this plugin, they decided to improve the python plugin by offering other commands to the software.&lt;br /&gt;
&lt;br /&gt;
For example, they added the possibility to retrieve project informations (which is not possible in the basic version).&lt;br /&gt;
&lt;br /&gt;
Since the development is based on volunteers, it is not very fast, but it seems that for developers who know C++, the possibility to create new features is not very complicated, and a former developer of graphisoft shared a tutorial about it in the project discord.&lt;br /&gt;
&lt;br /&gt;
== More... ==&lt;br /&gt;
[[Script in python for archicad 101]]&lt;/div&gt;</summary>
		<author><name>Mathias J</name></author>
		
	</entry>
	<entry>
		<id>http://wiki.archi-cadlink.fr/index.php?title=Introduction_%C3%A0_l%27utilisation_de_la_connexion_Archicad-Python&amp;diff=58</id>
		<title>Introduction à l'utilisation de la connexion Archicad-Python</title>
		<link rel="alternate" type="text/html" href="http://wiki.archi-cadlink.fr/index.php?title=Introduction_%C3%A0_l%27utilisation_de_la_connexion_Archicad-Python&amp;diff=58"/>
		<updated>2023-02-28T21:29:47Z</updated>

		<summary type="html">&lt;p&gt;Mathias J : Annulation des modifications 56 de Mathias J (discussion)&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;(tutoriel initialement rédigé par [https://www.archi-cadlink.fr/memberlist.php?mode=viewprofile&amp;amp;u=70 MathiasJ] sur le forum [https://www.archi-cadlink.fr/ Archi-cadlink] puis remis en forme ici.)&lt;br /&gt;
== Introduction ==&lt;br /&gt;
=== Qu'est ce que python? ===&lt;br /&gt;
C'est un langage de programmation particulièrement utilisé pour l’automatisation de tâches simples mais fastidieuses. Il a été créé en 1991 et en est aujourd'hui à la version 3.12 (date du post). De manière générale, on parle de version 3 pour la version actuelle. C'est celle qui est utilisée pour archicad.&lt;br /&gt;
&lt;br /&gt;
Pour utiliser python, il faut l'installer (voir le site officiel https://www.python.org/).&lt;br /&gt;
&lt;br /&gt;
=== Comment travailler avec python sur archicad ? ===&lt;br /&gt;
&lt;br /&gt;
* Installer la dernière version du langage python sur le site officiel (Dernière version 3.XX)&lt;br /&gt;
* Activer Python dans les options expérimentales (Options &amp;gt; Environnement de travail &amp;gt; Autres options &amp;amp; cocher la case « Activer palette Python Palette»)&lt;br /&gt;
* Afficher la palette python maintenant accessible.&lt;br /&gt;
* Suivre les indications de la palette pour finaliser l'installation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery&amp;gt;&lt;br /&gt;
Fichier:Python1.png&lt;br /&gt;
Fichier:Python2.png&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Comment marche la connexion? ===&lt;br /&gt;
La connexion marche en utilisant la palette python.&lt;br /&gt;
&lt;br /&gt;
Cette palette est composée de deux parties: une partie où l'on sélectionne ses scripts (en haut) et on lance le script voulu , et une partie nommée &amp;quot;console&amp;quot; ou l'ordinateur va vous donner des informations.&lt;br /&gt;
&lt;br /&gt;
==== La Partie script ====&lt;br /&gt;
On sélectionne l'icone dossier et on indique où se trouvent les scripts à utiliser. Un bouton permet de supprimer le dossier, et un autre sert à actualiser la liste.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python3.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pour lancer un script, il suffit de sélectionner le script choisi (1), et de cliquer sur lancer (2). On peut aussi double cliquer sur le nom du script pour le lancer. &lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python4.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== La Partie console ====&lt;br /&gt;
C'est une partie très importante pour l'utilisateur du script et indispensable pour le 'codeur'.&lt;br /&gt;
&lt;br /&gt;
On peut y retrouver une indication de lancement du script (1) les informations liées à l'activation du script (Par exemple des informations souhaitées comme des quantités, ou subies comme des rapports d'erreurs)(2), et si le script a bien fini son travail (3).&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python5.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Comment créer/modifier un script? ===&lt;br /&gt;
&lt;br /&gt;
Pour cela, tout éditeur de texte suffit (Bloc note sur PC par exemple). Sous Mac, on peut en double cliquant sur le script ouvrir un éditeur de code dédié qui donne des outils rudimentaires pour modifier le script (voir capture d'écran ci-dessous), comme la coloration selon le type, le numéro de la ligne sélectionnée, la possibilité de &amp;quot;lancer&amp;quot; le script.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python6.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Toutefois, il est recommandé de travailler sur un éditeur de code dédié.&lt;br /&gt;
Je vous recommande Visual Studio Code qui marche sur Mac et PC et qui possède une extension permettant de travailler aussi sur du GDL.&lt;br /&gt;
Les avantages pour ce type d'outil sont nombreux.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python7.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Certains pour coder en général, d'autres spécifiques à la connexion python/archicad.&lt;br /&gt;
Par exemple:&lt;br /&gt;
* L'ensemble des lignes sont numérotés (les bugs indiquent souvent à quelle ligne le script a buggé, ce qui aide à le corriger),&lt;br /&gt;
* Lors d'erreurs &amp;quot;grossières&amp;quot; (une parenthèse oubliée, un retour à la ligne manquant...), le logiciel alerte et aide à éviter les erreurs bêtes.&lt;br /&gt;
* Après avoir installé le package dédié (https://pypi.org/project/archicad/#description), le logiciel reconnait l'ensemble des commandes disponibles, avec des informations complémentaires qui aident à les utiliser (sur les besoins de la commande, et ce qui résultera de l'utilisation de cette commande).&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python8.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Par exemple, sur Get2DBOundingBoxes, on apprends que c'est une commande, qu'il lui faut les identifiants des éléments à récupérer sous la forme ElementArrayItem et qu'elle donnera en retour une liste contenant les contours 2D des éléments ou des erreurs s'il n'y en a pas.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== HTTP &amp;amp; JSON ===&lt;br /&gt;
&lt;br /&gt;
Une des première chose à savoir, c'est que python n'agit pas directement sur archicad, mais passe via Http en utilisant des messages au format JSON (https://fr.wikipedia.org/wiki/JavaScript_Object_Notation) .&lt;br /&gt;
&lt;br /&gt;
Graphisoft propose un package pour python qui &amp;quot;cache&amp;quot; la communication en JSON.&lt;br /&gt;
&lt;br /&gt;
Par exemple, si on utilise la commande GetAllPropertyIds qui permet de récupérer l'ensemble des identifiant des propriétés d'un fichier archicad, il faudra écrire en python:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
acc.GetAllPropertyIds(&amp;quot;UserDefined&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ce qui sera traduit par le &amp;quot;package&amp;quot; en JSON sous cette forme:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;command&amp;quot;: &amp;quot;API.GetAllPropertyIds&amp;quot;,&lt;br /&gt;
    &amp;quot;parameters&amp;quot;: {&lt;br /&gt;
        &amp;quot;propertyType&amp;quot;: &amp;quot;UserDefined&amp;quot;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Et le résultat donnera quelque chose qui ressemblera à ça (une liste d'identifiant unique - Les GUID - correspondant pour chacun à une propriété d'archicad):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;succeeded&amp;quot;: true,&lt;br /&gt;
    &amp;quot;result&amp;quot;: {&lt;br /&gt;
        &amp;quot;propertyIds&amp;quot;: [&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;E480E81E-EDE3-43FC-9C52-B55A4CA1A85C&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;13A61253-66A9-4494-9393-9E8F2E19D55E&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;BCB5813F-2115-4B8B-A12F-16CFE37C7B7F&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;6F4A46AC-AE91-47E6-BF4A-9F9AB01A4986&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;F6F67733-1DC1-442A-8CF4-ACD2DF7E62C6&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;52D7923A-E5D7-47DF-9319-834B2CB68A6C&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;331D26A3-8168-460C-B7F5-0FA11B596B60&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;78B73923-1B87-460B-8D9E-6E3041CF38D6&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;2FAB57AB-40D6-4B7B-A7F7-31FAE42BCFBD&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;3D9EF415-8D5E-42C3-999F-3CE138DF341F&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;9CC16F4D-9754-B744-B3F8-20BA074A3B2D&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        ]&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Le résultat peux paraitre un peu indigeste, mais cette forme de code en JSON permet de conserver une structure hiérarchique.&lt;br /&gt;
Par exemple si on copie colle la partie &amp;quot;résultat&amp;quot; et qu'on la colle dans un &amp;quot;viewer JSON&amp;quot; trouvable sur internet, les lignes de codes donnent çà:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python9.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Conclusion: les résultat qu'on reçoit peuvent apparaitre au premier abord peu clairs, mais on peut en extraire les données qui nous interessent!&lt;br /&gt;
De même, avant de donner des valeurs au script, il faut les &amp;quot;empaqueter&amp;quot; de manière adéquate.&lt;br /&gt;
&lt;br /&gt;
==== Pourquoi ne pas avoir créé une passerelle directe sans utiliser JSON? ====&lt;br /&gt;
&lt;br /&gt;
Lors de la première bêta, la connexion se faisait directement par python, sans intermédiaire.&lt;br /&gt;
Le choix s'est finalement porté sur HTTP/JSON pour laisser l'opportunité de développer ultérieurement des passerelles avec n'importe quel langage informatique. Il &amp;quot;suffirait&amp;quot; de créer un nouvel package qui permet de produire du code en JSON.&lt;br /&gt;
&lt;br /&gt;
== Comment apprendre python? ==&lt;br /&gt;
Je n'ai pas un &amp;quot;gros niveau&amp;quot;, j'ai essayé plusieurs fois de m'y mettre, et j'ai trouvé enfin une source assez efficace en anglais qui s’appelle ''Automate the Boring Stuff with Python''. C'est un livre (en anglais) qui a la particularité d'être disponible gratuitement et légalement sur un site internet : https://automatetheboringstuff.com/&lt;br /&gt;
&lt;br /&gt;
Pour (re)créer le script sur le total des surfaces de zones, je n'ai eu besoin d'apprendre que finalement peu de concepts propre à python. Ci-dessous les chapitres que j'ai lu (et dont j'ai suivi les petits exercices).&lt;br /&gt;
Vous pouvez surement trouver de la documentation/tuto en Français, surtout depuis que Python peut être enseigné (une option si j'ai bien compris?) au Lycée.&lt;br /&gt;
&lt;br /&gt;
=== Les basiques ===&lt;br /&gt;
https://automatetheboringstuff.com/2e/chapter1/&lt;br /&gt;
&lt;br /&gt;
* Les types de données (entiers, décimaux, chaîne ce qui donne en entier integers, Floating point numbers, strings)&lt;br /&gt;
* La création et l'utilisation de variable en utilisant le signe =&lt;br /&gt;
* La création de commentaires avec le symbole #&lt;br /&gt;
* Des fonctions importantes comme len() pour mesurer un nombre d'éléments et surtout la fonction print() qui va être la base de l'élaboration du script sous python. C'est la fonctions print() qui va permettre d'afficher des informations dans la console d'archicad&lt;br /&gt;
&lt;br /&gt;
=== Les instructions conditionnelles ===&lt;br /&gt;
https://automatetheboringstuff.com/2e/chapter2/&lt;br /&gt;
&lt;br /&gt;
* Les opérateurs de comparaison, en particulier la différence entre == (vérification d'égalité) et = (attribution de valeur)&lt;br /&gt;
* Les différentes conditions de type &amp;quot;If&amp;quot; &amp;quot;else&amp;quot; &amp;quot;elif&amp;quot; &amp;quot;while&amp;quot; &amp;quot;break&amp;quot; les boucles basées sur &amp;quot;for&amp;quot;...&lt;br /&gt;
* L'import de modules (des fonctions complémentaires à python) qu'il faut &amp;quot;appeler&amp;quot; pour pouvoir utiliser&lt;br /&gt;
&lt;br /&gt;
=== Les fonctions ===&lt;br /&gt;
https://automatetheboringstuff.com/2e/chapter3/&lt;br /&gt;
&lt;br /&gt;
=== Les listes ===&lt;br /&gt;
https://automatetheboringstuff.com/2e/chapter4/&lt;br /&gt;
Les listes sont une façon de stocker des données. Chaque donnée à un index et une valeur. Les index sont des entiers et la première valeur d'une liste a pour index 0 (et pas 1).&lt;br /&gt;
&lt;br /&gt;
Il faut apprendre comment récupérer une valeur à partir d'un (ou de plusieurs) index, l'ajout ou la suppression de valeurs dans cette liste.&lt;br /&gt;
&lt;br /&gt;
=== Les dictionnaires ===&lt;br /&gt;
https://automatetheboringstuff.com/2e/chapter5/&lt;br /&gt;
&lt;br /&gt;
Les dictionnaires sont une autre façon de stocker des données. au lieu d'avoir index sous forme de nombre entier, chaque valeur est associée à une &amp;quot;clé&amp;quot;. Cette clé peut être de n'importe quel type.&lt;br /&gt;
&lt;br /&gt;
=== La modification des &amp;quot;chaines&amp;quot; ===&lt;br /&gt;
https://automatetheboringstuff.com/2e/chapter6/&lt;br /&gt;
&lt;br /&gt;
== Ou en est la connexion python/archicad ==&lt;br /&gt;
&lt;br /&gt;
Les possibilités sont (encore) limitées.&lt;br /&gt;
La connexion archicad/python est apparue en archicad 24, quelques fonctions sont apparues en 25 (accès aux informations porteur/non porteur; interieur/extérieur...) et en 26 (Possibilité de récupérer les identifiant des éléments sélectionnés par l'utilisateur, accès aux classifications des éléments...)&lt;br /&gt;
&lt;br /&gt;
De manière générale, les utilisateurs de cette connexion trouvent que le développement de cette partie d'archicad est très lente.&lt;br /&gt;
Le développeur qui a mis en place cette connexion est partie depuis de chez Graphisoft.&lt;br /&gt;
&lt;br /&gt;
Il y'a quand même un indice sur la poursuite du développement de ce côté, c'est la RoadMap présentée par graphisoft qui indique de l'automatisation pour 2025.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python10.jpeg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Que peut-on faire exactement? ===&lt;br /&gt;
&lt;br /&gt;
Pour savoir ce qu'il est possible de faire, il faut regarder:&lt;br /&gt;
&lt;br /&gt;
* le site dédié à l'interface JSON (voir précédent post) : https://archicadapi.graphisoft.com/JSONInterfaceDocumentation/#Introduction&lt;br /&gt;
* le site dédié à l'&amp;quot;empaqueteur&amp;quot; python (voir précédent post) : https://archicadapi.graphisoft.com/archicadPythonPackage/archicad.html&lt;br /&gt;
&lt;br /&gt;
Le site pour JSON est un peu plus digeste et donne un bon aperçu.&lt;br /&gt;
La majorité des commandes commencent par &amp;quot;Get&amp;quot; suivi d'un texte. Ce sont des commandes pour récupérer des informations. Par exemple récupérer le nom d'un calque, d'une propriété, la valeur de cette propriétén etc...&lt;br /&gt;
&lt;br /&gt;
Les commandes qui vont réellement agir dans archicad sont beaucoup moins nombreuses.&lt;br /&gt;
&lt;br /&gt;
La majorité de ces commandes permettent de modifier les vues et les mises en pages:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DeleteNavigatorItems&lt;br /&gt;
RenameNavigatorItem&lt;br /&gt;
MoveNavigatorItem&lt;br /&gt;
CloneProjectMapItemToViewMap&lt;br /&gt;
CreateViewMapFolder&lt;br /&gt;
CreateLayoutSubset&lt;br /&gt;
CreateLayout&lt;br /&gt;
SetLayoutSettings&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Et pour le reste, seulement deux commandes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SetClassificationsOfElements&lt;br /&gt;
SetPropertyValuesOfElements&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
L'une pour modifier des paramètres d'un élément archicad (propriété personnalisée ou paramètre intégré (BuiltIn) comme les calques...) l'autre (et seulement depuis archicad 26) les classification des éléments.&lt;br /&gt;
&lt;br /&gt;
Ca reste peu!&lt;br /&gt;
&lt;br /&gt;
=== Tapir ===&lt;br /&gt;
https://www.archicad-api.com/&lt;br /&gt;
&lt;br /&gt;
Des utilisateur d'archicad ont décidé de développer en opensource un plugin permettant d'utiliser dans Grasshopper ( Outil du logiciel Rhinoceros) les possibilités offertes par le plugin python.&lt;br /&gt;
&lt;br /&gt;
Pour augmenter les capacité de ce plugin, ils ont décidé d'améliorer le plugin python en offrant d'autres commandes au logiciel.&lt;br /&gt;
&lt;br /&gt;
Par exemple, ils ont ajouté la possibilité de récupérer les informations projet (ce qui n'est pas possible dans la version de base).&lt;br /&gt;
&lt;br /&gt;
Le développement se reposant sur des bénévole, ce n'est pas très rapide, mais il semblerait que pour des développeur s'y connaissant en C++, la possibilité de créer de nouvelles fonctionnalités ne soit pas très compliqué, et un ancien développeur de graphisoft a partagé un tutoriel à ce sujet dans le discord du projet.&lt;br /&gt;
&lt;br /&gt;
== La suite... ==&lt;br /&gt;
[[Premier script en python, un exemple expliqué de A à Z]]&lt;/div&gt;</summary>
		<author><name>Mathias J</name></author>
		
	</entry>
	<entry>
		<id>http://wiki.archi-cadlink.fr/index.php?title=Introduction_to_the_use_of_the_Archicad-Python_connection&amp;diff=57</id>
		<title>Introduction to the use of the Archicad-Python connection</title>
		<link rel="alternate" type="text/html" href="http://wiki.archi-cadlink.fr/index.php?title=Introduction_to_the_use_of_the_Archicad-Python_connection&amp;diff=57"/>
		<updated>2023-02-28T21:29:37Z</updated>

		<summary type="html">&lt;p&gt;Mathias J : Page créée avec « ((tutorial originally written by [https://www.archi-cadlink.fr/memberlist.php?mode=viewprofile&amp;amp;u=70 MathiasJ] on the [https://www.archi-cadlink.fr/ Archi-cadlink] forum an... »&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;((tutorial originally written by [https://www.archi-cadlink.fr/memberlist.php?mode=viewprofile&amp;amp;u=70 MathiasJ] on the [https://www.archi-cadlink.fr/ Archi-cadlink] forum and then re-formatted here). &lt;br /&gt;
== Introduction ==&lt;br /&gt;
=== What is python? ===&lt;br /&gt;
It is a programming language particularly used for the automation of simple but tedious tasks. It was created in 1991 and is now at version 3.12 (date of the post). In general, we speak of version 3 for the current version. It is the one used for archicad.&lt;br /&gt;
&lt;br /&gt;
To use python, you need to install it (voir le site officiel https://www.python.org/).&lt;br /&gt;
&lt;br /&gt;
=== How to work with python in archicad? ===&lt;br /&gt;
&lt;br /&gt;
* Install the latest version of the python language on the official website (Latest version 3.XX)&lt;br /&gt;
* Activate Python in the experimental options (Options &amp;gt; Working environment &amp;gt; Other options &amp;amp; check the box &amp;quot; Activate Python Palette&amp;quot;)&lt;br /&gt;
* Display the Python palette now available.&lt;br /&gt;
* Follow the indications of the palette to finalize the installation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery&amp;gt;&lt;br /&gt;
Fichier:Python1.png&lt;br /&gt;
Fichier:Python2.png&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== How does the connection work? ===&lt;br /&gt;
The connection works by using the python palette.&lt;br /&gt;
&lt;br /&gt;
This palette is composed of two parts: a part where you select your scripts (on top) and launch the desired script, and a part named &amp;quot;console&amp;quot; where the computer will give you information.&lt;br /&gt;
&lt;br /&gt;
==== The script section ====&lt;br /&gt;
We select the folder icon and indicate where the scripts to be used are located. A button is used to delete the folder, and another one is used to update the list.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python3.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To launch a script, just select the chosen script (1), and click on launch (2). You can also double click on the script name to launch it. &lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python4.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The console section ====&lt;br /&gt;
This is a very important part for the user of the script and indispensable for the coder.&lt;br /&gt;
&lt;br /&gt;
You can find here an indication of the launch of the script (1), the information related to the activation of the script (for example, desired information such as quantities, or informations such as error reports)(2), and if the script has finished its work (3).&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python5.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== How to create/modify a script? ===&lt;br /&gt;
For this, any text editor is needed (Notepad on PC for example). On Mac, you can double click on the script to open a dedicated code editor that gives you rudimentary tools to modify the script (see screenshot below), such as coloring according to the type, the number of the selected line, the possibility to &amp;quot;launch&amp;quot; the script.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python6.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, it is recommended to work on a dedicated code editor.&lt;br /&gt;
I recommend Visual Studio Code which works on Mac and PC (which has an extension allowing to work on GDL.)&lt;br /&gt;
The advantages of this type of tool are numerous.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python7.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some for coding in general, some specific to the python/archicad connection.&lt;br /&gt;
For example:&lt;br /&gt;
* All lines are numbered (bugs often indicate which line the script bugged, which helps to fix it),&lt;br /&gt;
* In case of &amp;quot;big&amp;quot; errors (a forgotten parenthesis, a missing line break...), the software alerts and helps to avoid stupid mistakes.&lt;br /&gt;
* After installing the dedicated package (https://pypi.org/project/archicad/#description), the software recognizes all the available commands, with additional information that helps to use them (on the needs of the command, and what will result from using this command).&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python8.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
For example, on Get2DBOundingBoxes, we learn that it is a command, that it needs the identifiers of the elements to be retrieved in the form ElementArrayItem and that it will give in return a list containing the 2D outlines of the elements or errors if there is none.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== HTTP &amp;amp; JSON ===&lt;br /&gt;
&lt;br /&gt;
archicad, but passes via Http using messages in JSON format (https://fr.wikipedia.org/wiki/JavaScript_Object_Notation).&lt;br /&gt;
&lt;br /&gt;
Graphisoft offers a package for python that &amp;quot;hides&amp;quot; the communication in JSON.&lt;br /&gt;
&lt;br /&gt;
For example, if you use the GetAllPropertyIds command which allows you to retrieve all the property identifiers of an archicad file, you will have to write in python:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
acc.GetAllPropertyIds(&amp;quot;UserDefined&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will be translated by the &amp;quot;package&amp;quot; into JSON in this form:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;command&amp;quot;: &amp;quot;API.GetAllPropertyIds&amp;quot;,&lt;br /&gt;
    &amp;quot;parameters&amp;quot;: {&lt;br /&gt;
        &amp;quot;propertyType&amp;quot;: &amp;quot;UserDefined&amp;quot;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the result will look like this (a list of unique identifiers - GUIDs - each corresponding to an archicad property):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;succeeded&amp;quot;: true,&lt;br /&gt;
    &amp;quot;result&amp;quot;: {&lt;br /&gt;
        &amp;quot;propertyIds&amp;quot;: [&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;E480E81E-EDE3-43FC-9C52-B55A4CA1A85C&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;13A61253-66A9-4494-9393-9E8F2E19D55E&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;BCB5813F-2115-4B8B-A12F-16CFE37C7B7F&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;6F4A46AC-AE91-47E6-BF4A-9F9AB01A4986&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;F6F67733-1DC1-442A-8CF4-ACD2DF7E62C6&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;52D7923A-E5D7-47DF-9319-834B2CB68A6C&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;331D26A3-8168-460C-B7F5-0FA11B596B60&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;78B73923-1B87-460B-8D9E-6E3041CF38D6&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;2FAB57AB-40D6-4B7B-A7F7-31FAE42BCFBD&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;3D9EF415-8D5E-42C3-999F-3CE138DF341F&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;9CC16F4D-9754-B744-B3F8-20BA074A3B2D&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        ]&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The result may look a bit cumbersome, but this form of JSON code allows to keep a hierarchical structure.&lt;br /&gt;
For example, if you copy and paste the &amp;quot;result&amp;quot; part into a JSON viewer that you can find on the internet, the lines of code look like this:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python9.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Conclusion: the results we receive may seem unclear at first, but we can extract the data we are interested in!&lt;br /&gt;
Also, before giving values to the script, we have to &amp;quot;package&amp;quot; them properly.&lt;br /&gt;
&lt;br /&gt;
==== Why didn't they create a direct connection without using JSON? ====&lt;br /&gt;
&lt;br /&gt;
During the first beta, the connection was made via python, without any intermediary.&lt;br /&gt;
The choice was finally made to use HTTP/JSON in order to leave the opportunity to develop bridges with any computer language later on. It would be &amp;quot;enough&amp;quot; to create a new package that allows to produce code in JSON.&lt;br /&gt;
&lt;br /&gt;
== How do I learn python? ==&lt;br /&gt;
I don't have a &amp;quot;high level&amp;quot;, I tried several times to get into it, and I finally found a quite effective resource in English called ''Automate the Boring Stuff with Python''. &lt;br /&gt;
It's a book that has the particularity to be available for free and legally on a website : https://automatetheboringstuff.com/&lt;br /&gt;
&lt;br /&gt;
Here are the chapters I read (and followed the small exercises)&lt;br /&gt;
=== The basics ===&lt;br /&gt;
https://automatetheboringstuff.com/2e/chapter1/&lt;br /&gt;
&lt;br /&gt;
* Data types (integers, floats, strings )&lt;br /&gt;
* The creation and use of variables using the = sign&lt;br /&gt;
* The creation of comments with the # symbol&lt;br /&gt;
* Important functions like len() to measure a number of elements and especially the print() function which is going to be the basis of the script development under python. It's the print() function that will allow to display information in the archicad console&lt;br /&gt;
&lt;br /&gt;
=== Conditional instructions ===&lt;br /&gt;
https://automatetheboringstuff.com/2e/chapter2/&lt;br /&gt;
&lt;br /&gt;
* Comparison operators, in particular the difference between == (equality check) and = (value assignment)&lt;br /&gt;
* The different conditions of type &amp;quot;If&amp;quot; &amp;quot;else&amp;quot; &amp;quot;elif&amp;quot; &amp;quot;while&amp;quot; &amp;quot;break&amp;quot; loops based on &amp;quot;for&amp;quot;...&lt;br /&gt;
* Import of modules (additional functions to python) that must be called.&lt;br /&gt;
&lt;br /&gt;
=== Functions ===&lt;br /&gt;
https://automatetheboringstuff.com/2e/chapter3/&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
https://automatetheboringstuff.com/2e/chapter4/&lt;br /&gt;
Lists are a way to store data. Each data has an index and a value. Indexes are integers and the first value of a list has index 0 (not 1).&lt;br /&gt;
&lt;br /&gt;
You have to learn how to retrieve a value from one (or more) indexes, adding or deleting values in this list.&lt;br /&gt;
&lt;br /&gt;
=== Dictionnary ===&lt;br /&gt;
https://automatetheboringstuff.com/2e/chapter5/&lt;br /&gt;
&lt;br /&gt;
Dictionaries are another way of storing data. Instead of having indexes in integer form, each value is associated with a &amp;quot;key&amp;quot;. This key can be of any type.&lt;br /&gt;
&lt;br /&gt;
=== Strings modifications ===&lt;br /&gt;
https://automatetheboringstuff.com/2e/chapter6/&lt;br /&gt;
&lt;br /&gt;
== What is the status of the python/archicad connection ? ==&lt;br /&gt;
The possibilities are (still) limited.&lt;br /&gt;
The archicad/python connection appeared in archicad 24, some functions appeared in 25 (access to bearer/non-bearer information; interior/exterior...) and in 26 (Possibility to retrieve the identifiers of the elements selected by the user...)&lt;br /&gt;
&lt;br /&gt;
In general, the users of this connection find that the development of this part of archicad is very slow.&lt;br /&gt;
&lt;br /&gt;
There is however a clue on the continuation of the development on this side, it is the RoadMap presented by Graphisoft which indicates automation for 2025.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python10.jpeg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===What exactly can we do?===&lt;br /&gt;
&lt;br /&gt;
To know what is possible, you have to look at:&lt;br /&gt;
&lt;br /&gt;
* the site dedicated to the JSON interface : https://archicadapi.graphisoft.com/JSONInterfaceDocumentation/#Introduction&lt;br /&gt;
* the site dedicated to the python &amp;quot;packer&amp;quot;  : https://archicadapi.graphisoft.com/archicadPythonPackage/archicad.html&lt;br /&gt;
&lt;br /&gt;
The site for JSON is a bit more easily understandable and gives a good overview.&lt;br /&gt;
Most of the commands start with &amp;quot;Get&amp;quot; followed by a text. These are commands to get information. For example get the name of :&lt;br /&gt;
a layer, a property, the value of this property etc...&lt;br /&gt;
&lt;br /&gt;
The commands that will really act in Archicad are much less numerous.&lt;br /&gt;
&lt;br /&gt;
The majority of these commands allow you to modify views and layouts:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DeleteNavigatorItems&lt;br /&gt;
RenameNavigatorItem&lt;br /&gt;
MoveNavigatorItem&lt;br /&gt;
CloneProjectMapItemToViewMap&lt;br /&gt;
CreateViewMapFolder&lt;br /&gt;
CreateLayoutSubset&lt;br /&gt;
CreateLayout&lt;br /&gt;
SetLayoutSettings&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And for the rest, only two commands:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SetClassificationsOfElements&lt;br /&gt;
SetPropertyValuesOfElements&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One to modify parameters of an archicad element (custom property or built-in parameter (BuiltIn) like layers...) the other is the classification of elements.&lt;br /&gt;
&lt;br /&gt;
It's not much!&lt;br /&gt;
&lt;br /&gt;
=== Tapir ===&lt;br /&gt;
https://www.archicad-api.com/&lt;br /&gt;
&lt;br /&gt;
Some archicad users decided to develop an opensource plugin to use in Grasshopper (Rhinoceros software tool) the possibilities offered by the python plugin.&lt;br /&gt;
&lt;br /&gt;
To increase the capabilities of this plugin, they decided to improve the python plugin by offering other commands to the software.&lt;br /&gt;
&lt;br /&gt;
For example, they added the possibility to retrieve project informations (which is not possible in the basic version).&lt;br /&gt;
&lt;br /&gt;
Since the development is based on volunteers, it is not very fast, but it seems that for developers who know C++, the possibility to create new features is not very complicated, and a former developer of graphisoft shared a tutorial about it in the project discord.&lt;br /&gt;
&lt;br /&gt;
== More... ==&lt;br /&gt;
[[]]&lt;/div&gt;</summary>
		<author><name>Mathias J</name></author>
		
	</entry>
	<entry>
		<id>http://wiki.archi-cadlink.fr/index.php?title=Introduction_%C3%A0_l%27utilisation_de_la_connexion_Archicad-Python&amp;diff=56</id>
		<title>Introduction à l'utilisation de la connexion Archicad-Python</title>
		<link rel="alternate" type="text/html" href="http://wiki.archi-cadlink.fr/index.php?title=Introduction_%C3%A0_l%27utilisation_de_la_connexion_Archicad-Python&amp;diff=56"/>
		<updated>2023-02-28T21:27:46Z</updated>

		<summary type="html">&lt;p&gt;Mathias J : &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;((tutorial originally written by [https://www.archi-cadlink.fr/memberlist.php?mode=viewprofile&amp;amp;u=70 MathiasJ] on the [https://www.archi-cadlink.fr/ Archi-cadlink] forum and then re-formatted here). &lt;br /&gt;
== Introduction ==&lt;br /&gt;
=== What is python? ===&lt;br /&gt;
It is a programming language particularly used for the automation of simple but tedious tasks. It was created in 1991 and is now at version 3.12 (date of the post). In general, we speak of version 3 for the current version. It is the one used for archicad.&lt;br /&gt;
&lt;br /&gt;
To use python, you need to install it (voir le site officiel https://www.python.org/).&lt;br /&gt;
&lt;br /&gt;
=== How to work with python in archicad? ===&lt;br /&gt;
&lt;br /&gt;
* Install the latest version of the python language on the official website (Latest version 3.XX)&lt;br /&gt;
* Activate Python in the experimental options (Options &amp;gt; Working environment &amp;gt; Other options &amp;amp; check the box &amp;quot; Activate Python Palette&amp;quot;)&lt;br /&gt;
* Display the Python palette now available.&lt;br /&gt;
* Follow the indications of the palette to finalize the installation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery&amp;gt;&lt;br /&gt;
Fichier:Python1.png&lt;br /&gt;
Fichier:Python2.png&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== How does the connection work? ===&lt;br /&gt;
The connection works by using the python palette.&lt;br /&gt;
&lt;br /&gt;
This palette is composed of two parts: a part where you select your scripts (on top) and launch the desired script, and a part named &amp;quot;console&amp;quot; where the computer will give you information.&lt;br /&gt;
&lt;br /&gt;
==== The script section ====&lt;br /&gt;
We select the folder icon and indicate where the scripts to be used are located. A button is used to delete the folder, and another one is used to update the list.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python3.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To launch a script, just select the chosen script (1), and click on launch (2). You can also double click on the script name to launch it. &lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python4.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The console section ====&lt;br /&gt;
This is a very important part for the user of the script and indispensable for the coder.&lt;br /&gt;
&lt;br /&gt;
You can find here an indication of the launch of the script (1), the information related to the activation of the script (for example, desired information such as quantities, or informations such as error reports)(2), and if the script has finished its work (3).&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python5.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== How to create/modify a script? ===&lt;br /&gt;
For this, any text editor is needed (Notepad on PC for example). On Mac, you can double click on the script to open a dedicated code editor that gives you rudimentary tools to modify the script (see screenshot below), such as coloring according to the type, the number of the selected line, the possibility to &amp;quot;launch&amp;quot; the script.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python6.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, it is recommended to work on a dedicated code editor.&lt;br /&gt;
I recommend Visual Studio Code which works on Mac and PC (which has an extension allowing to work on GDL.)&lt;br /&gt;
The advantages of this type of tool are numerous.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python7.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some for coding in general, some specific to the python/archicad connection.&lt;br /&gt;
For example:&lt;br /&gt;
* All lines are numbered (bugs often indicate which line the script bugged, which helps to fix it),&lt;br /&gt;
* In case of &amp;quot;big&amp;quot; errors (a forgotten parenthesis, a missing line break...), the software alerts and helps to avoid stupid mistakes.&lt;br /&gt;
* After installing the dedicated package (https://pypi.org/project/archicad/#description), the software recognizes all the available commands, with additional information that helps to use them (on the needs of the command, and what will result from using this command).&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python8.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
For example, on Get2DBOundingBoxes, we learn that it is a command, that it needs the identifiers of the elements to be retrieved in the form ElementArrayItem and that it will give in return a list containing the 2D outlines of the elements or errors if there is none.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== HTTP &amp;amp; JSON ===&lt;br /&gt;
&lt;br /&gt;
archicad, but passes via Http using messages in JSON format (https://fr.wikipedia.org/wiki/JavaScript_Object_Notation).&lt;br /&gt;
&lt;br /&gt;
Graphisoft offers a package for python that &amp;quot;hides&amp;quot; the communication in JSON.&lt;br /&gt;
&lt;br /&gt;
For example, if you use the GetAllPropertyIds command which allows you to retrieve all the property identifiers of an archicad file, you will have to write in python:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
acc.GetAllPropertyIds(&amp;quot;UserDefined&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will be translated by the &amp;quot;package&amp;quot; into JSON in this form:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;command&amp;quot;: &amp;quot;API.GetAllPropertyIds&amp;quot;,&lt;br /&gt;
    &amp;quot;parameters&amp;quot;: {&lt;br /&gt;
        &amp;quot;propertyType&amp;quot;: &amp;quot;UserDefined&amp;quot;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the result will look like this (a list of unique identifiers - GUIDs - each corresponding to an archicad property):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;succeeded&amp;quot;: true,&lt;br /&gt;
    &amp;quot;result&amp;quot;: {&lt;br /&gt;
        &amp;quot;propertyIds&amp;quot;: [&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;E480E81E-EDE3-43FC-9C52-B55A4CA1A85C&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;13A61253-66A9-4494-9393-9E8F2E19D55E&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;BCB5813F-2115-4B8B-A12F-16CFE37C7B7F&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;6F4A46AC-AE91-47E6-BF4A-9F9AB01A4986&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;F6F67733-1DC1-442A-8CF4-ACD2DF7E62C6&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;52D7923A-E5D7-47DF-9319-834B2CB68A6C&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;331D26A3-8168-460C-B7F5-0FA11B596B60&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;78B73923-1B87-460B-8D9E-6E3041CF38D6&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;2FAB57AB-40D6-4B7B-A7F7-31FAE42BCFBD&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;3D9EF415-8D5E-42C3-999F-3CE138DF341F&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
                &amp;quot;propertyId&amp;quot;: {&lt;br /&gt;
                    &amp;quot;guid&amp;quot;: &amp;quot;9CC16F4D-9754-B744-B3F8-20BA074A3B2D&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        ]&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The result may look a bit cumbersome, but this form of JSON code allows to keep a hierarchical structure.&lt;br /&gt;
For example, if you copy and paste the &amp;quot;result&amp;quot; part into a JSON viewer that you can find on the internet, the lines of code look like this:&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python9.png|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Conclusion: the results we receive may seem unclear at first, but we can extract the data we are interested in!&lt;br /&gt;
Also, before giving values to the script, we have to &amp;quot;package&amp;quot; them properly.&lt;br /&gt;
&lt;br /&gt;
==== Why didn't they create a direct connection without using JSON? ====&lt;br /&gt;
&lt;br /&gt;
During the first beta, the connection was made via python, without any intermediary.&lt;br /&gt;
The choice was finally made to use HTTP/JSON in order to leave the opportunity to develop bridges with any computer language later on. It would be &amp;quot;enough&amp;quot; to create a new package that allows to produce code in JSON.&lt;br /&gt;
&lt;br /&gt;
== How do I learn python? ==&lt;br /&gt;
I don't have a &amp;quot;high level&amp;quot;, I tried several times to get into it, and I finally found a quite effective resource in English called ''Automate the Boring Stuff with Python''. &lt;br /&gt;
It's a book that has the particularity to be available for free and legally on a website : https://automatetheboringstuff.com/&lt;br /&gt;
&lt;br /&gt;
Here are the chapters I read (and followed the small exercises)&lt;br /&gt;
=== The basics ===&lt;br /&gt;
https://automatetheboringstuff.com/2e/chapter1/&lt;br /&gt;
&lt;br /&gt;
* Data types (integers, floats, strings )&lt;br /&gt;
* The creation and use of variables using the = sign&lt;br /&gt;
* The creation of comments with the # symbol&lt;br /&gt;
* Important functions like len() to measure a number of elements and especially the print() function which is going to be the basis of the script development under python. It's the print() function that will allow to display information in the archicad console&lt;br /&gt;
&lt;br /&gt;
=== Conditional instructions ===&lt;br /&gt;
https://automatetheboringstuff.com/2e/chapter2/&lt;br /&gt;
&lt;br /&gt;
* Comparison operators, in particular the difference between == (equality check) and = (value assignment)&lt;br /&gt;
* The different conditions of type &amp;quot;If&amp;quot; &amp;quot;else&amp;quot; &amp;quot;elif&amp;quot; &amp;quot;while&amp;quot; &amp;quot;break&amp;quot; loops based on &amp;quot;for&amp;quot;...&lt;br /&gt;
* Import of modules (additional functions to python) that must be called.&lt;br /&gt;
&lt;br /&gt;
=== Functions ===&lt;br /&gt;
https://automatetheboringstuff.com/2e/chapter3/&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
https://automatetheboringstuff.com/2e/chapter4/&lt;br /&gt;
Lists are a way to store data. Each data has an index and a value. Indexes are integers and the first value of a list has index 0 (not 1).&lt;br /&gt;
&lt;br /&gt;
You have to learn how to retrieve a value from one (or more) indexes, adding or deleting values in this list.&lt;br /&gt;
&lt;br /&gt;
=== Dictionnary ===&lt;br /&gt;
https://automatetheboringstuff.com/2e/chapter5/&lt;br /&gt;
&lt;br /&gt;
Dictionaries are another way of storing data. Instead of having indexes in integer form, each value is associated with a &amp;quot;key&amp;quot;. This key can be of any type.&lt;br /&gt;
&lt;br /&gt;
=== Strings modifications ===&lt;br /&gt;
https://automatetheboringstuff.com/2e/chapter6/&lt;br /&gt;
&lt;br /&gt;
== What is the status of the python/archicad connection ? ==&lt;br /&gt;
The possibilities are (still) limited.&lt;br /&gt;
The archicad/python connection appeared in archicad 24, some functions appeared in 25 (access to bearer/non-bearer information; interior/exterior...) and in 26 (Possibility to retrieve the identifiers of the elements selected by the user...)&lt;br /&gt;
&lt;br /&gt;
In general, the users of this connection find that the development of this part of archicad is very slow.&lt;br /&gt;
&lt;br /&gt;
There is however a clue on the continuation of the development on this side, it is the RoadMap presented by Graphisoft which indicates automation for 2025.&lt;br /&gt;
&lt;br /&gt;
[[Fichier:Python10.jpeg|thumb|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===What exactly can we do?===&lt;br /&gt;
&lt;br /&gt;
To know what is possible, you have to look at:&lt;br /&gt;
&lt;br /&gt;
* the site dedicated to the JSON interface : https://archicadapi.graphisoft.com/JSONInterfaceDocumentation/#Introduction&lt;br /&gt;
* the site dedicated to the python &amp;quot;packer&amp;quot;  : https://archicadapi.graphisoft.com/archicadPythonPackage/archicad.html&lt;br /&gt;
&lt;br /&gt;
The site for JSON is a bit more easily understandable and gives a good overview.&lt;br /&gt;
Most of the commands start with &amp;quot;Get&amp;quot; followed by a text. These are commands to get information. For example get the name of :&lt;br /&gt;
a layer, a property, the value of this property etc...&lt;br /&gt;
&lt;br /&gt;
The commands that will really act in Archicad are much less numerous.&lt;br /&gt;
&lt;br /&gt;
The majority of these commands allow you to modify views and layouts:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DeleteNavigatorItems&lt;br /&gt;
RenameNavigatorItem&lt;br /&gt;
MoveNavigatorItem&lt;br /&gt;
CloneProjectMapItemToViewMap&lt;br /&gt;
CreateViewMapFolder&lt;br /&gt;
CreateLayoutSubset&lt;br /&gt;
CreateLayout&lt;br /&gt;
SetLayoutSettings&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And for the rest, only two commands:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SetClassificationsOfElements&lt;br /&gt;
SetPropertyValuesOfElements&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One to modify parameters of an archicad element (custom property or built-in parameter (BuiltIn) like layers...) the other is the classification of elements.&lt;br /&gt;
&lt;br /&gt;
It's not much!&lt;br /&gt;
&lt;br /&gt;
=== Tapir ===&lt;br /&gt;
https://www.archicad-api.com/&lt;br /&gt;
&lt;br /&gt;
Some archicad users decided to develop an opensource plugin to use in Grasshopper (Rhinoceros software tool) the possibilities offered by the python plugin.&lt;br /&gt;
&lt;br /&gt;
To increase the capabilities of this plugin, they decided to improve the python plugin by offering other commands to the software.&lt;br /&gt;
&lt;br /&gt;
For example, they added the possibility to retrieve project informations (which is not possible in the basic version).&lt;br /&gt;
&lt;br /&gt;
Since the development is based on volunteers, it is not very fast, but it seems that for developers who know C++, the possibility to create new features is not very complicated, and a former developer of graphisoft shared a tutorial about it in the project discord.&lt;br /&gt;
&lt;br /&gt;
== More... ==&lt;br /&gt;
[[]]&lt;/div&gt;</summary>
		<author><name>Mathias J</name></author>
		
	</entry>
	<entry>
		<id>http://wiki.archi-cadlink.fr/index.php?title=Accueil&amp;diff=55</id>
		<title>Accueil</title>
		<link rel="alternate" type="text/html" href="http://wiki.archi-cadlink.fr/index.php?title=Accueil&amp;diff=55"/>
		<updated>2023-02-28T21:01:44Z</updated>

		<summary type="html">&lt;p&gt;Mathias J : &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== GDL ==&lt;br /&gt;
* [[Le GDL pour les nuls]]&lt;br /&gt;
** [[Objet &amp;quot;Test&amp;quot;]]&lt;br /&gt;
** [[Symbole de trémie]]&lt;br /&gt;
** [[Premier objet en 3D]]&lt;br /&gt;
** [[Table]]&lt;br /&gt;
** [[Gradin]]&lt;br /&gt;
* [[Code snippet]]&lt;br /&gt;
&lt;br /&gt;
== Python ==&lt;br /&gt;
* [[Introduction à l'utilisation de la connexion Archicad-Python]]&lt;br /&gt;
* [[Premier script en python, un exemple expliqué de A à Z]]&lt;br /&gt;
&lt;br /&gt;
== Python (English) ==&lt;br /&gt;
* [[Introduction to the use of the Archicad-Python connection]]&lt;br /&gt;
* [[Script in python for archicad 101]]&lt;/div&gt;</summary>
		<author><name>Mathias J</name></author>
		
	</entry>
</feed>