cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 
2024 Technology Preview Program

2024 Technology Preview Program:
Master powerful new features and shape the latest BIM-enabled innovations

Archicad Python API
About automating tasks in Archicad using the Python API.
SOLVED!

Retrieve all classifications item with python

Mathias Jonathan
Advocate

Hello everyone.

I'm pretty new to python, but I've managed to make a few scripts.

I am working on a script with a graphical interface, and I would like to be able to propose to select a classification in a Tkinter treeview style interface (keep the hierarchical system).

I've managed to retrieve the first and second levels of classifications, but my system is pretty basic in terms of code: I don't know, for example, how to find out the depth of the list retrieved by archicad.


Basically: how can I retrieve a hierarchical list with only the classification names?

 

My attempt (for the first and second levels):

 

 

from archicad import ACConnection

conn = ACConnection.connect()
acc = conn.commands
act = conn.types
acu = conn.utilities

chooseClassification = acc.GetAllClassificationSystems()

classifications = acc.GetAllClassificationsInSystem(
    chooseClassification[0].classificationSystemId)

nbEnfants =0
nbEnfants2 =0


for classification in classifications:
    print("      "+classification.classificationItem.id + "    " +
          str(len(classification.classificationItem.children))+" children")
    nbEnfants +=len(classification.classificationItem.children)

    for subelements in classification.classificationItem.children:
        try:
            print("                " + subelements.classificationItem.id + "   " +
                  str(len(subelements.classificationItem.children))+" children")
            nbEnfants2 +=len(subelements.classificationItem.children)

        except TypeError:
            print("                " + subelements.classificationItem.id)

print(nbEnfants)
print(nbEnfants2)

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Solution
poco2013
Mentor

Not sure I can be of any help --But to retrieve the Classifications tree you need to use recursion. i wrote a classification interface for grasshopper which uses the Json interface and not the built in Python interface. Attached is a Demo Script - You would have to translate to the native Python interface which does not need Json.

Note that the two native classifications in Archicad use different Ids and naming convention. i assume you are interested in the Graphisoft default.

 

import json
import  urllib.request as rb

#request = rb.Request('http://localhost:19723')
request = 'http://localhost:19723'

response = rb.urlopen(request,json.dumps({"command":"API.GetAllClassificationSystems"}).encode('UTF-8'))
results = json.loads(response.read())
result = results['result']['classificationSystems'][0]['classificationSystemId']['guid']
response.close()

response = rb.urlopen(request,json.dumps({"command":"API.GetAllClassificationsInSystem",
"parameters": {"classificationSystemId": {"guid": result }}}).encode('UTF-8'))
results = json.loads(response.read())

response.close()
seed =results['result']['classificationItems']

output = []
level = 0
def get_ids(seed,kids = 0 ):
    global level
    level += 1
    for item in seed:
        output.append( '   '*(level-1) +  item['classificationItem']['id'])
        if kids >0:
            kids -= 1
        
        if 'children' in item['classificationItem'].keys():
            depth = len(item['classificationItem']['children'])
            get_ids(item['classificationItem']['children'], kids = depth)
        else:
            if kids == 0:
                pass # Debug only
    level -= 1
    
get_ids(seed, kids = 0 )
for x in output:
        print(x)
Gerry

Windows 11 - Visual Studio 2022; ArchiCAD 27

View solution in original post

2 REPLIES 2
Solution
poco2013
Mentor

Not sure I can be of any help --But to retrieve the Classifications tree you need to use recursion. i wrote a classification interface for grasshopper which uses the Json interface and not the built in Python interface. Attached is a Demo Script - You would have to translate to the native Python interface which does not need Json.

Note that the two native classifications in Archicad use different Ids and naming convention. i assume you are interested in the Graphisoft default.

 

import json
import  urllib.request as rb

#request = rb.Request('http://localhost:19723')
request = 'http://localhost:19723'

response = rb.urlopen(request,json.dumps({"command":"API.GetAllClassificationSystems"}).encode('UTF-8'))
results = json.loads(response.read())
result = results['result']['classificationSystems'][0]['classificationSystemId']['guid']
response.close()

response = rb.urlopen(request,json.dumps({"command":"API.GetAllClassificationsInSystem",
"parameters": {"classificationSystemId": {"guid": result }}}).encode('UTF-8'))
results = json.loads(response.read())

response.close()
seed =results['result']['classificationItems']

output = []
level = 0
def get_ids(seed,kids = 0 ):
    global level
    level += 1
    for item in seed:
        output.append( '   '*(level-1) +  item['classificationItem']['id'])
        if kids >0:
            kids -= 1
        
        if 'children' in item['classificationItem'].keys():
            depth = len(item['classificationItem']['children'])
            get_ids(item['classificationItem']['children'], kids = depth)
        else:
            if kids == 0:
                pass # Debug only
    level -= 1
    
get_ids(seed, kids = 0 )
for x in output:
        print(x)
Gerry

Windows 11 - Visual Studio 2022; ArchiCAD 27
Mathias Jonathan
Advocate

It worked well, thanks a lot!

 

here is my code (it's not clean, but it seems to work!)

from archicad import ACConnection

conn = ACConnection.connect()
acc = conn.commands
act = conn.types
acu = conn.utilities

chooseClassification = acc.GetAllClassificationSystems()

classifications = acc.GetAllClassificationsInSystem(
    chooseClassification[0].classificationSystemId)

seed = classifications

output = []
level = 0


def get_ids(seed, kids=0):
    global level
    level += 1
    for item in seed:
        output.append('   '*(level-1) +
                      item.classificationItem.id + " (level " + str(level) + ")")
        if kids > 0:
            kids -= 1

        try:
            get_ids(item.classificationItem.children,
                    kids=len(item.classificationItem.children))
        except TypeError:
            pass  # Debug only
    level -= 1


get_ids(seed, kids=0)
for x in output:
    print(x)