Mimicking electronic circuit board

If you have a design you're proud of, share the cfdg file here. It's also a good place to ask for feedback and collaborate.

Moderators: MtnViewJohn, chris, mtnviewmark

Post Reply
TDEF
Posts: 4
Joined: Thu Jul 12, 2018 5:59 am

Mimicking electronic circuit board

Post by TDEF »

Hello, I´m unsure how to submit a new CFDG for the gallery, so I´ll post mine below. This script is a rough attempt to mimic an electronic circuit board (PCB) with tracks and pads. It is not accurate since PCBs don´t have tracks that fork then join, creating loops. I guess I would need a system of netlists and autorouting for that. There is also no component footprints, just simple round or square pads.
Any comment, suggestion, improvements to make it more realistic welcome !

Code: Select all

Size = 20
CF::MaxNatural = 2^50
CF::Background = [ b -1 ]

hole = 0.5 // hole radius
diam = 1.7 // pad radius
width = 1 // track width
height = 4
spacing = height*1.9
turnoffset = sqrt(2)/2
probroundpad = 45 // probability of round pad vs square pad (%)
probturn45 = 60 // probability of 45° turn vs 90° turn (%)
DEBUG = 0

startshape PCB

shape PCB {

  if (DEBUG) {
    segment [y -(height*2) h 20 a 0 sat 1]
    turn45 [y (height*1) x (height)]
    turn45 []
    roundpad [ x (height*3) y (height) r 90]
  }
  else
    Rows(0, Size) []
}

shape Rows(natural top, natural n) {
    middle = bitand(randint(2^Size), bitnot(1))
    bottom = if(n, randint(2^Size), 0)
    Row(top,middle,bottom,Size) [ ]
    if (n)
        Rows(bottom, n--1) [ y -spacing ]
}

shape Row(natural top, natural middle, natural bottom, natural expo) {
    Cell(bitand(middle,1), bitand(top,1),
        bitand(middle,2), bitand(bottom,1)) []

    if (expo)
        Row(div(top,2), div(middle,2), div(bottom,2), expo--1) [ x spacing ]
}

shape Cell(natural left, natural top, natural right, natural bottom) {
    if (top && bottom && !left && !right)
        segment []
    if (!top && !bottom && left && right)
        segment [ r 90 ]
    if (bottom && right)
        turn [ ]
    if (right && top)
        turn [ r 90 ]
    if (top && left)
        turn [ r 180 ]
    if (left && bottom)
        turn [ r 270 ]

    if (left && !top && !right && !bottom)
        pad [ r 90 ]
    if (!left && top && !right && !bottom)
        pad [ ]
    if (!left && !top && right && !bottom)
        pad [ r 270 ]
    if (!left && !top && !right && bottom)
        pad [ r 180 ]
}

shape pad {
  if (randint(100) >= probroundpad)
    roundpad []
  else
    squarepad []
}

shape turn {
  if (randint(100) >= probturn45)
    turn90 []
  else
    turn45 []
}

path segment { // straight segment, x 1, y 4
    MOVETO(-width/2,-height)
    LINETO(-width/2,height)
    LINETO(width/2,height)
    LINETO(width/2,-height)
    FILL()[b 1]
}

path turn45 {
    ss = sqrt(2)/2
    ss = 0.999
    offs = width - width*sqrt(2)/2
    MOVETO(-width/2,-height)
    LINETO(-width/2,-width+offs-turnoffset)
    LINETO(width-offs+turnoffset,width/2)
    LINETO(height,width/2)
    LINETO(height,-width/2)
    LINETO(width+offs+turnoffset,-width/2)
    LINETO(width/2,-width-offs-turnoffset)
    LINETO(width/2,-height)
    CLOSEPOLY()
    FILL[b 1]
}

path turn90 {
    MOVETO(-width/2, -height)  // origin
    LINETO(-width/2,width/2)  // A
    LINETO(height,width/2)     // B
    LINETO(height,-width/2)    // C
    LINETO(width/2,-width/2) // D
    LINETO(width/2,-height)
    CLOSEPOLY()
    FILL[b 1]
}

path squarepad {
    MOVETO(-width/2, height)
    LINETO(-width/2,diam)
    LINETO(-diam,diam)
    LINETO(-diam,-diam)
    LINETO(diam,-diam)
    LINETO(diam,diam)
    LINETO(width/2,diam)
    LINETO(width/2, height)
    MOVETO(0, hole)
    ARCTO(0, -hole, -hole)
    ARCTO(0, hole, -hole)
    FILL()[b 1]
}

path roundpad {
    overlap = (diam-hole/2) // the track slightly overlap the pad
    MOVETO(0, diam)
    ARCTO(0, -diam,diam)
    ARCTO(0, diam, diam)
    LINETO(0, height)
    MOVETO(0, hole)
    ARCTO(0, -hole, -hole)
    ARCTO(0, hole, -hole)
    FILL()[b 1]
    MOVETO(-width/2, height)
    LINETO(width/2, height)
    LINETO(width/2, overlap)
    LINETO(-width/2,overlap)
    FILL()[b 1]
}
circuit.cfdg.png
circuit.cfdg.png (142.4 KiB) Viewed 44628 times

User avatar
MtnViewJohn
Site Admin
Posts: 882
Joined: Fri May 06, 2005 2:26 pm
Location: Mountain View, California
Contact:

Re: Mimicking electronic circuit board

Post by MtnViewJohn »

Log-in to the gallery using the same username/password that you use for the forums. Then click the 'Upload a Design' link and fill out the form. You can also upload directly from Context Free.

TDEF
Posts: 4
Joined: Thu Jul 12, 2018 5:59 am

Re: Mimicking electronic circuit board

Post by TDEF »

Thanks for the info. I tried it, filled up the form, chose my .cfdg script and .png image, filled up tags and omment, but when i wanted to submit the design i got the error "Upload issue: Cannot instantiate a design.".

I can´t upload directly from CF, I use Atom editor under Linux with language-context-free package to edit and render the code.

Any hint about the upload issue ? what did I do wrong ?

User avatar
MtnViewJohn
Site Admin
Posts: 882
Joined: Fri May 06, 2005 2:26 pm
Location: Mountain View, California
Contact:

Re: Mimicking electronic circuit board

Post by MtnViewJohn »

I have no idea why you can't upload. I just did a test upload and it worked. The gallery was rewritten in Python and Elm recently. But the old gallery, written in PHP, is still around.

Try logging in to the old gallery at https://www.contextfreeart.org/gallery/ and uploading there.

TDEF
Posts: 4
Joined: Thu Jul 12, 2018 5:59 am

Re: Mimicking electronic circuit board

Post by TDEF »


User avatar
MtnViewJohn
Site Admin
Posts: 882
Joined: Fri May 06, 2005 2:26 pm
Location: Mountain View, California
Contact:

Re: Mimicking electronic circuit board

Post by MtnViewJohn »

That's great. The next time you upload, please try the new gallery. I have put in instrumentation that may help me figure out why you couldn't upload.

Post Reply