terrastruct / d2

D2 is a modern diagram scripting language that turns text to diagrams.
https://d2lang.com
Mozilla Public License 2.0
16.67k stars 417 forks source link

Feature: circuit diagram #1244

Closed akshaybabloo closed 1 year ago

akshaybabloo commented 1 year ago

It will be good to have a way to create circuit diagrams using D2.

alixander commented 1 year ago

hey @akshaybabloo thank you for the suggestion! Do you have an example of what you mean?

akshaybabloo commented 1 year ago

@alixander Certainly. Consider the following example of a circuit diagram (made with Autodesk Fusion 365):

temp

Is shows that a 9v battery connected to a resistor to step down the voltage to light up the LED

The only difference between US and NON-US is how the resistor is denoted.

I tried to do this via D2, and this is what I got:

direction: right

classes: {
  resistor: {
    shape: rectangle
    height: 10
  }
}

9v -> 350k -> led -> 9v
350k.class: resistor

https://play.d2lang.com/?script=gkevlUIRKDi5uKCpwEqhmktBoSi1OLO4JL8IwlNQAIeClQJIT2Jeek4qWBASEVYKhgZcCgq1XLVcXJZlCrp2CsamBtkgOic1BURZlnGBRPTAFljBjeYCBAAA__8%3D&layout=dagre&sketch=0&theme=0&

I think for this we would need the following:

  1. Right angled connections (sharp edge connection)
  2. As a starting point, basic analog icons - see https://www.ultralibrarian.com/2020/11/24/electronic-component-lists-and-schematic-symbols-ulc
  3. Labels that can be put outside of the icon

Digital icons can be added later once you have a proof of concept

alixander commented 1 year ago
direction: right

classes: {
  resistor: {
    shape: image
    height: 30
    width: 60
    icon: https://b2523400.smushcdn.com/2523400/wp-content/uploads/2020/11/image19.png?lossy=1&strip=1&webp=1
  }
}

US: {
  style: {
    fill: transparent
    stroke-dash: 2
  }
  9v -> 350k -> led -> 9v
  350k.class: resistor
}
Screen Shot 2023-04-25 at 7 18 51 PM

https://play.d2lang.com/?script=RJLBbsMgEETv-xVzyi0GnKaSV3L7EVU_wLFRYtUGBCRRVfnfK5Y0Pa1mDsvyZp7xMmLBSfRoAeOHgGjTnLKPVQFCgTGvw9mKUUNgHLRI2cl4raqkwbjkHBIrdWqP7eFF6yat13QZJ9eMflUPU93DfvQuW5fVNSx-mJJqdauVMUpeM10T3Pl98Sl992aXcpxDb3Z3ewq9IWCjjejzox4qDP9uLsVjCJ4wROty_cl_HIxWFgDdDfs3HI76q8zFTmV0N4J4jaDhJxTa6DcAAP__&layout=tala&sketch=0&theme=0&

it's pretty close. this type of diagram cannot be done in elk or dagre btw, because they only do hierarchical diagrams.

i think implementing ports is all that's needed: https://github.com/terrastruct/d2/issues/628

@akshaybabloo are there any comparable tools that you can create circuit diagrams with text?

akshaybabloo commented 1 year ago

Not that I know of @alixander

akshaybabloo commented 1 year ago

Also, the ends are not really connected to the resistor there, it's just hovering. It would be nice to have some internal icons that can do that.

alixander commented 1 year ago

just need ports (can then specify to only route through the left and right sides of the icon)

BarryNolte commented 1 year ago

Tools to create schematics, off the top of my head:

  1. KiCad
  2. OrCad
  3. Altium
  4. Proteus
  5. Fusion 360
  6. CircuitMaker.com
alixander commented 1 year ago

can you generate with text in any of these? @BarryNolte

BarryNolte commented 1 year ago

I think what you want is a 'net list'. https://en.wikipedia.org/wiki/Netlist. They all probably produce a net list; some might read them. I don't think it's a super well-defined standard, otherwise there wouldn't be a lot of discussion on converting files from one EDA package to another, which is the evergreen topic on electronics message boards.

akshaybabloo commented 1 year ago

Most of them use a combination of XML and binary

alixander commented 1 year ago

sorry i meant are any of these tools text-to-circuit-diagram. not whether they can export to text.

could be a good opportunity to expand d2 with no much more extra work if a comparable solution doesn't exist yet and there's demand for it

BarryNolte commented 1 year ago

I think there are some netlist -> html/svg projects around.

alixander commented 1 year ago

oh interesting, yeah: https://github.com/nturley/netlistsvg#examples

looks like they use ELK too.

BarryNolte commented 1 year ago

Yup, just found that one too.

bo-ku-ra commented 1 year ago

Verilog

dtmaidenmueller commented 1 year ago

For scripted schematic image generation, there's this library: https://github.com/cdelker/schemdraw

alixander commented 1 year ago

that one seems quite good. i'm going to close this then as not something D2 should focus on.