leomcelroy / svg-pcb

Design PCBs in the Browser
https://www.leomcelroy.com/svg-pcb-website
GNU General Public License v3.0
64 stars 9 forks source link

Add SVG to Mods Feature #97

Closed kr15h closed 5 months ago

kr15h commented 5 months ago

At the Spain bootcamp this year a need to have a SvgPcb <> Mods connection was identified. We created this pull request with John to work on it.

Essentially we want two things.

  1. The possibility to push svg to Mods milling flow with one click. SvgPcb would spawn Mods and send over SVG of the current board.
  2. From Mods, pull in active design from an open instance of SvgPcb.

Let's do it!

kr15h commented 5 months ago

John's GitHub handle: @john-story

kr15h commented 5 months ago

We can open mods in a new window now. Next steps are:

Step 2: opening a specific mods program Step 3: modify milling program by adding a new svgToMods module Step 4: figure out how to send svg to the new mods module

kr15h commented 5 months ago

Using the localStorage or sessionStorage

Some tips from our friend ChatGPT.

The localStorage and sessionStorage objects store data that can be shared between tabs from the same origin (i.e., same protocol, domain, and port).

Storing Data: In your first tab, you can store the SVG data string in localStorage like this:

localStorage.setItem('svgData', yourSvgDataString);

Retrieving Data: In the second tab, you can retrieve this data with:

let svgData = localStorage.getItem('svgData');

Listening for Changes: Optionally, you can add an event listener in the second tab to react to changes:

window.addEventListener('storage', function(event) {
  if (event.key === 'svgData') {
    // Handle the new SVG data
    let newSvgData = event.newValue;
  }
});

Note: localStorage persists data even after closing the tab/browser, whereas sessionStorage is limited to the tab's session.

kr15h commented 5 months ago

OK, but how about opening the MODS sketch.

  1. We need to modify Mods so that it registers a function via sessionStorage.
  2. In SvgPcb, the svgToMods module would check if this function exists and based on that would open or not a new tab.
  3. From there, a specific Mods program can be opened.
  4. Meanwile, SvgPcb would store SVG date in local/session storage for the Mods program to access as soon as it loads itself.
  5. And then, we already know what happens.
kr15h commented 5 months ago

So there is a problem that there are different Mods program for different machines. There is no way to know what machine is targeted and that means that the user has to be involved. In SvgPcb svgToMods modal the user should pick the machine and based on that a Mods program would be opened.

kr15h commented 5 months ago

So now, depending on user choice, we can call Mods to open a specific program.

kr15h commented 5 months ago

So now we figured out how to open specific Mods program for each machine without the need to modify mods.

Next step: figure out a way how we can load SVG string into the SVG input module in Mods.

kr15h commented 5 months ago

At this point sending raw SVG data to a specific Mods sketch works with a customized SVG reader.

  1. I will submit a pull request to the mods project with modified module and updated machine-specific programs
  2. Meanwhile improve the SvgPcb interface "download > mods"

The user will need to choose one of the machines where PCB milling sketch exists in Mods and select the layer to be exported. For now one can modify layer colors, but ultimately, whatever color, the objects will be white on black background. I could add a flip X feature for double-sided milling too.

kr15h commented 5 months ago

Ready to merge now it is...

leomcelroy commented 5 months ago

Thanks for working on it! And nice job actually writing docs too. Seems to work well.