Reverse Engineering on blocks

605 views
Skip to first unread message

Suraj Narwade

unread,
Nov 21, 2017, 11:06:54 AM11/21/17
to Blockly
Hello guys,

I am experimenting some stuff with blockly. So, by creating custom blocks, I am getting JSON data, with which I am doing further processing. But is there way to create block structure by feeding JSON data to some kind of function.
It will be damn useful for me if it's there.

Thanks in advance,
Suraj Narwade

Andrew n marshall

unread,
Nov 21, 2017, 12:02:00 PM11/21/17
to blo...@googlegroups.com
I'm not sure exactly what you are trying to do, but this example (copied from the other thread) seems like "create block structure by fedding JSON data":

Blockly.defineBlocksWithJsonArray([
  // Pasted JSON:
  {
    "type": "my_new_block",
    "message0": "My new block",
    "colour": 230,
    "tooltip": "This is an example block",
    "helpUrl": "http://fake.help"
  }
  // End pasted JSON.
]);

You will probably want a dynamic Toolbox category to reference any block you create at runtime.  See the dynamic category docs.

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Suraj Narwade

unread,
Nov 21, 2017, 1:02:24 PM11/21/17
to blo...@googlegroups.com
So for example, my blocks generates this information: [1,2,3,4]
Now, If I feed this information to Blockly.defineBlocksWithJsonArray([
will it generate my blocks on workspace ? That's my question ?
>> email to blockly+u...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Blockly" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/blockly/xzQ4P_UTO44/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> blockly+u...@googlegroups.com.

Andrew n marshall

unread,
Nov 21, 2017, 1:36:03 PM11/21/17
to blo...@googlegroups.com
You will need to transform your [1,2,3,4] JSON array into JSON block definition. For example, if you want it to populate a drop down, the constructed block definition command with JSON might look like:

Blockly.defineBlocksWithJsonArray([
  {
    "type": "block1234",  // Unique block type name
    "message0": "Options %1",
    "args0": [
      {
        "type": "field_dropdown",
        "name": "OPTIONS",
        "options": [
          // Display name, Option ID constructed from input
          ["1", "1"],
          ["2", "2"],
          ["3", "3"],
          ["4", "4"]
        ]
      }
    ],
    "colour": 230
  }
]);

All the blue text is the block definition. The yellow highlight is dependent on the data you want to display. You will need to write an intermediate function that takes your [1,2,3,4] array and constructs the blue text block definition from it.



One caveat: You will need to save this block definition JSON somewhere in order to be able to load any such blocks in another session.

An alternative that solves this issue might use mutator domToMutation() and mutationToDom methods. In such case the details to construct the block will be stored in the mutation (possibly the 1..4 array, in this case), and thus serialized and deserialized with the block and workspace.


>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Blockly" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/blockly/xzQ4P_UTO44/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to

> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+unsubscribe@googlegroups.com.

Andrew n marshall

unread,
Nov 21, 2017, 1:56:21 PM11/21/17
to blo...@googlegroups.com
One more point...
The above code just defines the type of the block.  To add it to the workspace, you will need to call:

workspace.addTopBlock(new Block(workspace, 'block1234'));

Where workspace will often be Blockly.mainWorkspace.




Unless......

You really want to add four math_number blocks to the workspace. In that case:

function addNumberBlocks(workspace, numbersArray) {
  for (var i = 0; i < numbersArray.length; ++i) {
    var block = new Block(workspace, 'math_number');  // Create the block
    block.setFieldValue(numbersArray[i], 'NUM');      // Assign the number value
    // TODO: set position using block.moveBy(x, y);
    workspace.addTopBlock(block);                     // Add to workspace
  }
}

Sethuraman

unread,
Nov 30, 2017, 12:50:47 AM11/30/17
to Blockly
Hi 

   Without drag and drop you need to add blocks in workspace is that your requiement .

   then you need to know the name of block that you need to drop 

                        var newBlock = workspace.newBlock(blockName);
newBlock.initSvg();
newBlock.render();

Suraj Narwade

unread,
Nov 30, 2017, 3:24:53 AM11/30/17
to Blockly

Hi Sethu,

Currently my blocks from workspace generates some code which is in JSON. (life is easy) but Now I want reverse thing, I want some function, to which I will feed my code (which is in JSON, life is going to be hell again) and respective blocks will be there on Workspace, is it possible ?

For example, let's goto blockly homepage, https://developers.google.com/blockly/
There are some blocks which generate Javascript code(in the right). Now I want something, I will give this Javascript code, show me the blocks.

Thanks in advance,

Maziarser

unread,
Dec 8, 2017, 2:33:24 PM12/8/17
to Blockly
Hi Suraj,

Did you manage to get what you asked for?
Please let me know if there is a way to do so...

Regards,
maziar

Suraj Narwade

unread,
Dec 10, 2017, 11:49:10 PM12/10/17
to blo...@googlegroups.com
Hi Maziar,

No I didn't get any solution. I am thinking of writing some fuction
which will take input and construct XML for the blocks
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Blockly" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/blockly/xzQ4P_UTO44/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> blockly+u...@googlegroups.com.

Maziarser

unread,
Dec 11, 2017, 12:51:05 PM12/11/17
to Blockly
Hi Suraj,

I am also looking for a solution to tackle this issue. 
Please let me know if you have found any solution in this matter

Regards,
Mazyar

Suraj Narwade

unread,
Dec 11, 2017, 11:19:31 PM12/11/17
to Blockly
Hey,

Microsoft's https://makecode.microbit.org has similar feature that we want.

Block to code and code to block.

You can join https://t.co/jsNDMdmp24 (gitter chat) for more.

Mark Friedman

unread,
Dec 12, 2017, 1:04:09 PM12/12/17
to blo...@googlegroups.com
There's also the droplet editor, which is part of Pencil Code.

-Mark

--
You received this message because you are subscribed to the Google Groups "Blockly" group.

To unsubscribe from this group and stop receiving emails from it, send an email to blockly+unsubscribe@googlegroups.com.

Maziarser

unread,
Dec 15, 2017, 8:03:11 AM12/15/17
to Blockly
Hey,

could you integrate PXT library into your project ?

I have tried, but it's failed :(

Dhanashree Revagade

unread,
May 15, 2020, 1:09:51 AM5/15/20
to Blockly
Hey Suraj,
I am also finding a way to construct the Blockly XML . Could you please let me know if you know how to proceed. 
Any help would be highly appreciated.

Thank You


On Monday, December 11, 2017 at 10:19:10 AM UTC+5:30, Suraj Narwade wrote:
Hi Maziar,

No I didn't get any solution. I am thinking of writing some fuction
which will take input and construct XML for the blocks

Reply all
Reply to author
Forward
0 new messages