For loop to get diffrent dimensions of rectangles with a fixed area

Hi,

May I ask for your feedback?

I am starting with Python so I have to still learn a lot:)

I have my input:

  • a list with areas of rectangles
  • sliders with width and length.

I would like to create many rectangles with different dimensions that have the same areas as the areas in the list.
As output, I would like to get width and length which meets the condition W*L=rooms area

I wrote this code, but it doesn’t work…

May I ask you what I am doing wrong?

Many thanks for your support!

Best regards,

Julia

Hi,
After the for you need to use a tab:
For …:
[Tab] if:
[Tab][tab]
[Tab]else:
[Tab][tab]

Im using my phone, can’t explain it better haha.
Btw the out is wrong too, the : needs between the " "

2 Likes

Hi @Julia_Ratajczak ,

Have you thought approaching this from the other way around?

Let’s say you want to create a rectangle with an area of 20 m2 but with random lengths.
You could simply create a shuffled list of x amount of items for the amount of rectangle you want.
Then from there simply do 20/x for each item to determine its width (assuming x is the height).

2 Likes

Hi,

thank you for your reply. I applied all tabs, but it doesn’t work:(

What is the warning?

1 Like

Hi Jacob,
there was a problemm with synatx - result_1 and result_2.
I changed it and it works now:)

It’s solved:)

Sorry, I realized that the script doesn’t work as I wish…the output was not what I intended…hahaha
I changed the approach using @Daan 's suggestion.

Now it’s working as I need:)

Thank you for all the support you have given me!!!

2 Likes

Hi Daan,

thank you for your suggestion.
Indeed, it works better:)
I put the solution at the end of the topic.

Many thanks!

Glad to hear that!

PS: You don’t really need a Python script here, you can just use the / node:

2 Likes

Hello,
To reinforce your research in python (I am myself in the learning phase)

here are 2 scripts:

Script 1: Test

# Charger les bibliothèques DesignScript et Standard Python
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

# Les entrées effectuées dans ce noeud sont stockées sous forme de liste dans les variables IN.
Area_Room=IN[0]
Width=IN[1]
Lenght=IN[2]
Result=[]

# Placer votre code au-dessous de cette ligne
for i in Area_Room:
	if i == Width*Lenght:
		Result.append({"Surface good":i})
	else :
		Result.append("Change Width or Lenght")
	
# Affectez la sortie à la variable OUT.
OUT = Result

Script 2 with ratio:

# Charger les bibliothèques DesignScript et Standard Python
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

# Les entrées effectuées dans ce noeud sont stockées sous forme de liste dans les variables IN.
Area=IN[0]
Ratio=IN[1]
Width=[]
Lenght=[]
# Placer votre code au-dessous de cette ligne
for i in Area:
	L=round((i/Ratio)**0.5,3)
	W=round(L*Ratio,3)
	Lenght.append(L)
	Width.append(W)
# Affectez la sortie à la variable OUT.
OUT = {"Lenght":Lenght,"Width":Width}

Cordially
Christian

1 Like

Hi Christian,

thank you so much:) I will check your approach. It’s great to have this knowledge exchange:)

Have a nice day!

BR,

Julia

1 Like

Hi Christian,

the second script works great for me! Thank you for this approach!!!

Kind regards,

Julia

1 Like