Formatting syntax list of grafted points

I’m trying to edit the format in which a couple of list of points can be written into a .txt
On one side I have a list of center, on the other corners grouped to make polygons (degree 1)

My goal is achieve this syntax:

[{center:[x,y],polygon:[[x,y],[x,y],[x,y],…]},{center:[x,y],polygon:[[x,y],[x,y],[x,y],…]},…]

The List of centers I have is flatten, while the one of corners is grafted, with several branches, with not the same amount of points in it.

The image to explain better, hopefully!

Hi!
You could do something like this. I used integers…

Concat.gh (16.7 KB)

Thanks Raul!
I was thinking to use concatenate but I was missing the text join to repeat the square braketc between points.

About the point coordinates that I have: I’d like to compile my .txt without the z coordinate; probably the solution would be use the path mapper to extract only [x,y] coordinates? Or something simpler?
Any suggestions?

why not using Deconstruct Point component? You dont need to take the Z value

Oh yes! right!

Following on From Raul’s work. This is i one way i would do it using Format expressions and Component so you don’t end up with long concatenate components. The rounding in the expressions are optional i just did it so it was easier to read.

Format Polygon Points Text.gh (8.5 KB)

yep! This is much better

thanks Raul and mattgaydon!
Well I’m learning!

I’ve a question about how to write the Format in F input of Format component:
this is what you did

[center:{0},polygon:{1}]

and I understand that each kind of brackets has a meaning, and I cannot change it in

{center:{0},polygon:{1}}

since I get the error

Is there a way to have curly brackets instead of square ones?
One this is sorted…, in order to have all the list of centers+corners of polygons grouped within square bracket, I tried the concatenate.

What I need:
[{center:[x,y],polygon:[x,y],[x,y], …},{center:[x,y],polygon:[x,y],[x,y], …},{center:[x,y],polygon:[x,y],[x,y], …},…]

But I’m doing it wrongly!! Square brackets should be only at the very start and end of the entire list we get from the Format component.

Ah sorry wrong symbols. Changed the F input to read {{center:{0},polygon:{1}}} Note the Double Curly Brackets at either end. Then just flattened the list and added another Text Join and finally the Square brackets at the end. Updated Code attached also.

Format Polygon Points Text.gh (12.1 KB)

Thanks. Why do we need double curly brackets? only to understand better!

This seems to best explain it that i found on StackOverflow website for formatting in Python.

Format strings contain “replacement fields” surrounded by curly braces {}. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }}.

My guess is that formatting syntax will be similar for other languages as well as i think GH is written in dot net. I originally found out about double brackets when i was trying to format paths for split tree so i needed the curly brackets.

2 Likes

Yeah you can look at the string.Format method which is the one ultimately used by Grasshopper.

The curly brackets represent placeholders within the formatting string. So assuming you are formatting with two values (one a floating point value at index=0, the other an integer at index=1).

The expression for this will always look like this: Format(pattern, x, y)

If the pattern is just a string without any curly brackets, it always yields the same result, regardless of the values of x and y. For example Format("X=x", x, y) just gives you the text “X=x”.

For the actual value of x to be inserted into the result, you need to write Format("X={0}", x, y). Of course now y is still ignored because it isn’t referenced in the pattern. To use both values, you’d write something like Format("X={0}, Y={1}", x, y).

You are of course welcome to use the same value more than once as well: Format("X={0}, Y={1} (and yes, x really equals {0})", x, y)

Assuming values of x=3.14159265359 and y=12, all the above patterns would yield the following results:

  1. “X=x”
  2. “X=3.14159265359”
  3. “X=3.14159265359, Y=12”
  4. “X=3.14159265359, Y=12 (and yes, x really equals 3.14159265359)”

You can include a formatting mask or instruction along with a placeholder. These bits are included within the curly brackets, separated from the placeholder index by a colon. It allows us for example to format data in a more readable fashion: Format("X={0:0.00}, Y={1} (x actually equals {0:R}), which would yield “X=3.14, Y=12 (x actually equals 3.14159265359)”. The first time x is inserted it is forcefully rounded to two decimal places. The second time x is inserted the R instruction is used. R stands for “Roundtrip formatting” meaning there is guaranteed no loss of information during formatting. It shows the number as it really is.

There are loads and loads of standard and custom formatting instructions, some designed for numbers, others for dates or times, currency amounts, etc. etc.

4 Likes

Thanks David and Matt!