Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

Options

Pretty print

billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,015 PRO
edited November 2023 in FeatureScript
I'm trying to write a simple feature script and got caught up on pretty printing maps. I wrote a simple function that parses the returned data and formats it into a readable structure. Below is a listing of the pretty print function.

</code>//pretty print</pre><pre class="CodeBlock"><code>
export function p(s)
{
    var str = splitIntoCharacters(toString(s));
    var i = 0; //indent
    var c; //cnt
    var t = "  "; //tab

    // println(size(str));

    for (c = 0; c < size(str); c += 1)
    {

        if (str[c] == "{")
        {
            print(str[c]);
            print("\n");
            i += 1;
            for (var a = 0; a < i; a += 1)
                print(t);
        }

        if (str[c] == "[")
        {
            print("\n");
            for (var a = 0; a < i; a += 1)
                print(t);
            print(str[c]);
            print("\n");
            i += 1;
            for (var a = 0; a < i; a += 1)
                print(t);
        }

        if (str[c] == "}")
        {
            i -= 1;
            print("\n");
            for (var a = 0; a < i; a += 1)
                print(t);
            print(str[c]);
        }

        if (str[c] == "]")
        {
            i -= 1;
            print("\n");
            for (var a = 0; a < i; a += 1)
                print(t);
            print(str[c]);
        }

        if (str[c] == ",")
        {
            print(str[c]);
            print("\n");
            for (var a = 0; a < i; a += 1)
                print(t);
        }

        if (str[c] != "{" && str[c] != "}" && str[c] != "[" && str[c] != "]" && str[c] != "," && str[c] != " ")
            print(str[c]);
    }
    println();
}

Below is displaying 2 simple maps. The 1st is a standard string based representation of the map. The 2nd is the same map except it's pretty printed showing the structure of the map.


Below is a more complex map showing both the simple & the pretty version of the map.


The problem comes when you're trying to access something inside the map. Without pretty print, it's hard to create an accessor to a value contained inside the map.

JSON has the same issues when working with an API. Javascript has many string -> object and object -> string operators and it's easy to create pretty print so you can understand an objects structure.

There may be better ways to understand the structure from a response in the FS console, if there is a better pattern, please let me know.

Comments

  • Options
    MichaelPascoeMichaelPascoe Member Posts: 1,724 PRO
    edited November 2023

    Legit! I will use this for sure.


    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   cadsharp.com/featurescripts 💎
  • Options
    NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,417
    Thanks Billy! I was also going to do this but didn't get around to it.
    Senior Director, Technical Services, EMEAI
  • Options
    billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,015 PRO
    Thanks guys, I've already found it helpful with understanding what OS is returning. I tend to look for the [] brackets knowing that these are lists and then trying to figure out if I can use the list. I'm already collecting a list of faces and able to dance through them trying to determine if I can work on them.


  • Options
    xTimRicexTimRice Member Posts: 59 PRO
    Super helpful! All this time I’ve just been struggling through the block of text 😂
    Handcrafted furniture in Boston
    https://thesmoothcut.com/
  • Options
    eric_pestyeric_pesty Member Posts: 1,530 PRO
    That was one of my (many!) major pain points during my brief foray into API stuff!
    This would have helped and will keep it in mind if I find myself messing around with the API for sure!
  • Options
    _anton_anton Member, Onshape Employees Posts: 279
  • Options
    billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,015 PRO
    @_anton I like it, can we make it standard vs. a function call?


  • Options
    _anton_anton Member, Onshape Employees Posts: 279
    edited November 2023
    Our internal process involves oodles of code review and testing, so, to prioritize any Onshape change, the formal way is to file an improvement request. :)

    (If this makes it into the FS standard library, it will probably end up as a function in string.fs, same as println.)
Sign In or Register to comment.