cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 
Archicad Python API
About automating tasks in Archicad using the Python API.

Python palette - how to create a new layout?

felcunha
Expert
Hello all,

I don't know if it's the right place to post this topic... please administrators feel free to move it if you feel so!
I'm struggling with the python scripts, not knowing how to handle the commands, types, utilities...
I think it would be nice to have a tutorial of how to deal with the syntax: when do we use dots, colons, parenthesis, double cotes, etc... Also, the few examples provided didn't help that much yet.
My first goal now is how to create a layout using a script.
Does anyone could help me with this?

Cheers,
Felipe Ribeiro Cunha

AC 26, macOS Monterey
2 REPLIES 2
runxel
Legend
You can't be reasonably expecting Graphisoft to teach you Python
I think you already need at least a bit of knowledge. Don't worry, tho. Python is super easy to learn and there are thousands of free tutorials out there! If you are in a hurry, take a look at this.

And now for some code!


# Simplest example for creating a Layout in Archicad
# BOML | runxel 2020
# v1.0 | works in ac24.b3008
# create_layout.py
# for <https://archicad-talk.graphisoft.com/viewtopic.php?f=23&t=70570#p315412>

from archicad import ACConnection

conn = ACConnection.connect()
assert conn

acc = conn.commands
act = conn.types
acu = conn.utilities


#### some basic config ####
master_name = "A2 Querformat"  # the Master Layout you want to use
parent_name = "Parent"         # Parent folder name

lname = "New Layout"  # Name of the layout to be created
# DIN A2 format size; always millimeter
lhor = 594
lvert = 420
lmargin_left = lmargin_top = lmargin_right = lmargin_bottom = 0
# Declare the appropiate Layout Parameters (see API for more info)
lparam = act.LayoutParameters(lhor, lvert,
                                lmargin_left, lmargin_top, lmargin_right, lmargin_bottom,
                                "", False, False, False, 1, 1, "", "", False, False)
# which tree to checkout: 'LayoutBook', 'PublisherSets', 'ViewMap'
root_tree_loc = 'LayoutBook'

# Retrieve the Root Item
layoutbook_tree = acc.GetNavigatorItemTree(act.NavigatorTreeId(root_tree_loc))

# Now a bit weird stuff:
# For the FindInNavigatorItemTree function we need a criteria function, which gets called 
#  with the item to check as only parameter.
# We will then automatically loop over all items in the defined tree.
# In this function we can decide if the current item adheres to our criteria,
#  if so, we will return true.

def findMaster(item: act.NavigatorItem):
    return True if item.name == master_name else False

def findParent(item: act.NavigatorItem):
    return True if item.name == parent_name else False

list_master = acu.FindInNavigatorItemTree(layoutbook_tree.rootItem, findMaster)
list_parent = acu.FindInNavigatorItemTree(layoutbook_tree.rootItem, findParent)

lmaster = list_master[0].navigatorItemId
lparent = list_parent[0].navigatorItemId


# Now actually create the Layout; returns a GUID on `new_layout`
new_layout = acc.CreateLayout(lname, lparam, lmaster, lparent)

Lucas Becker | AC 27 on Mac | Author of Runxel's Archicad Wiki | Editor at SelfGDL | Developer of the GDL plugin for Sublime Text | My List of AC shortcomings & bugs | I Will Piledrive You If You Mention AI Again |

POSIWID – The Purpose Of a System Is What It Does /// «Furthermore, I consider that Carth... yearly releases must be destroyed»

Thank you @runxel for your solution! It works if I already have a subset created before.

Now I'd like to create a new subset directly under the root. I've tried with a command that I've called Nouveau_dossier, and I've set the name of the parent to "".

But, AC returns an error.

Here's the script:

from Archicad import ACConnection

conn = ACConnection.connect()
assert conn

acc = conn.commands
act = conn.types
acu = conn.utilities


#### some basic config ####
master_name = "EOC A1"  # the Master Layout you want to use
#parent_name = "GROUND FLOOR"         # Parent folder name
parent_name = ""         # Parent folder name

lname = "New Layout"  # Name of the layout to be created
# DIN A2 format size; always millimeter
lhor = 841
lvert = 594
lmargin_left = lmargin_top = lmargin_right = lmargin_bottom = 5
# Declare the appropiate Layout Parameters (see API for more info)
lparam = act.LayoutParameters(lhor, lvert,
                                lmargin_left, lmargin_top, lmargin_right, lmargin_bottom,
                                "", False, False, False, 1, 1, "", "", False, False)
# which tree to checkout: 'LayoutBook', 'PublisherSets', 'ViewMap'
root_tree_loc = 'LayoutBook'

#dossier
dossier = act.Subset("GROUND FLOOR",True,False,True,True,False,"","","1",1,"")
# Retrieve the Root Item
layoutbook_tree = acc.GetNavigatorItemTree(act.NavigatorTreeId(root_tree_loc))

# Now a bit weird stuff:
# For the FindInNavigatorItemTree function we need a criteria function, which gets called 
#  with the item to check as only parameter.
# We will then automatically loop over all items in the defined tree.
# In this function we can decide if the current item adheres to our criteria,
#  if so, we will return true.

def findMaster(item: act.NavigatorItem):
    return True if item.name == master_name else False

def findParent(item: act.NavigatorItem):
    return True if item.name == parent_name else False

list_master = acu.FindInNavigatorItemTree(layoutbook_tree.rootItem, findMaster)
list_parent = acu.FindInNavigatorItemTree(layoutbook_tree.rootItem, findParent)

lmaster = list_master[0].navigatorItemId
lparent = list_parent[0].navigatorItemId

#test création de subset
nouveau_dossier = acc.CreateLayoutSubset(dossier, lparent)


# Now actually create the Layout; returns a GUID on `new_layout`
new_layout = acc.CreateLayout(lname, lparam, lmaster, lparent)

 

Felipe Ribeiro Cunha

AC 26, macOS Monterey