#21 circuit module

closed-accepted
nobody
None
5
2010-09-27
2006-08-07
Anonymous
No

I would be nice to have a module
for drawing simpel circuit diagrams.

Discussion

  • Kazuo Teramoto

    Kazuo Teramoto - 2006-10-16

    Logged In: YES
    user_id=1621319

    I vote for that.
    I'm using pst-circ now, but an asy module is gonna be better.

     
  • fedja30

    fedja30 - 2007-08-28

    Logged In: YES
    user_id=1872477
    Originator: NO

    I just wonder if this is close to what you are looking for (the number of available elements in this sample is just 3 and the diagram in the testing part is a bit stupid but it just shows the possibilities). If you like it, I can develop it into a full package though you will have to tell me what circuit elements you'd like to see and what notation you'd like to have for them. In this case we'll not have to bother the Asymptote development team with this request at all. If you are dissatisfied with something, it would be interesting to know what is unsatisfactory, so that I (or someone else) will have a better idea of what you are really after.

    Currently the drawing goes as follows: you use join() to put the elements on the wires. The elements are coded by strings like "bat2resibat1" with 4 symbol codes for elements. Also, each element on wire requires a label (possibly empty), which will be automatically placed near the corresponding element at the distance governed by labeldistance or by the dist variable in join(). The variable sizefactor controls the sizes of the elements. The idea is that one assigns its value once in the beginning and then all elements on all wires will be of the same size, which looks sort of neat. Once the sizefactor is chosen, you adjust labeldistance to have the labels at reasonable places. Everything else should be self-explnatory. Of course, my drawings of the circuit elements may be not the best possible ones, but I just wanted to give an idea of what is relatively easy to do.

    What do you think?

    real sizefactor=0.15;
    real labeldistance=4;
    pen elementpen=currentpen, elementlabelpen=currentpen, wirepen=currentpen;

    struct element{picture pict; real size; pair[] connectors;};

    //batteries

    element battery1(pen p)
    {
    element el;
    fill(el.pict,(0,2.5)--(0,-2.5)--(0.2,-2.5)--(0.2,2.5)--cycle,p);
    fill(el.pict,(1,1.2)--(1,-1.2)--(1.5,-1.2)--(1.5,1.2)--cycle,p);
    el.size=1.5;
    return el;
    }

    element battery2(pen p)
    {
    element el;
    el.size=1.5;
    el.pict=shift(1.5)*rotate(180)*battery1(p).pict;
    return el;
    }

    //resistor

    element resistor(pen p)
    {
    element el;
    fill(el.pict,(0,1.2)--(0,-1.2)--(12,-1.2)--(12,1.2)--cycle,p);
    el.size=12;
    return el;
    }

    //triodes

    element triode1(pen p)
    {
    pen q=linewidth(0.7)+p;
    element el;
    draw(el.pict,circle((3,0),3),q);
    draw(el.pict,(0,0)--(2,0),q);
    draw(el.pict,arc((-1.5,0),3.5,-25,25),q);
    draw(el.pict,(4,0)--(6,0)^^(4,1.6)--(4,-1.6),q);
    draw(el.pict,(3,2.25)--(3,1.8)^^(3,1.25)--(3,0.8)^^(3,0.25)--(3,-0.25)^^
    (3,-0.8)--(3,-1.25)^^(3,-1.8)--(3,-4),q);
    el.size=6;
    el.connectors[0]=(3,-4);
    return el;
    }

    element triode2(pen p)
    {
    element el;
    el.pict=shift(6,0)*xscale(-1)*triode1(p).pict;
    el.size=6;
    el.connectors[0]=(3,-4);
    return el;
    }

    element triode3(pen p)
    {
    element el;
    el.pict=shift(6,0)*rotate(180)*triode1(p).pict;
    el.size=6;
    el.connectors[0]=(3,4);
    return el;
    }

    element triode4(pen p)
    {
    element el;
    el.pict=shift(6,0)*rotate(180)*triode2(p).pict;
    el.size=6;
    el.connectors[0]=(3,4);
    return el;
    }

    pair[] join(pair A, pair B, pen w=wirepen, pen p=elementpen,
    pen q=elementlabelpen, real dist=labeldistance, string s ... Label[] lab)
    {
    pair[] connections;
    picture pict;
    int len=lab.length,count=0;
    element[] elements;
    real sum=0, relsizefactor=sizefactor/abs(B-A),pos;

    //Putting all kinds of elements on wire
    for (int i=0;i<len;++i)
    {
    if (substr(s,4*i,4)=='bat1') elements[i]=battery1(p);
    if (substr(s,4*i,4)=='bat2') elements[i]=battery2(p);
    if (substr(s,4*i,4)=='resi') elements[i]=resistor(p);
    if (substr(s,4*i,4)=='tri1') elements[i]=triode1(p);
    if (substr(s,4*i,4)=='tri2') elements[i]=triode2(p);
    if (substr(s,4*i,4)=='tri3') elements[i]=triode3(p);
    if (substr(s,4*i,4)=='tri4') elements[i]=triode4(p);
    }

    for (int i=0;i<len;++i) sum+=relsizefactor*elements[i].size;
    real h=(1-sum)/(len+3);
    draw(pict,(0,0)--(2*h,0),w);
    pos=2*h;
    for (int i=0;i<len;++i)
    {
    add(pict,shift(pos)*scale(relsizefactor)*elements[i].pict);
    real cursize=relsizefactor*elements[i].size;
    draw(pict,(pos+cursize,0)--(pos+cursize+h,0),w);
    label(pict,rotate(-degrees(B-A))*lab[i],(pos+cursize/2,0),(0,-dist),q);
    pair[] temp=elements[i].connectors;
    for(int j;j<temp.length;++j)
    {
    connections[count]=(pos,0)+relsizefactor*temp[j];
    ++count;
    }
    pos+=cursize+h;
    }
    draw(pict,(pos,0)--(1,0),w);
    pict=shift(A)*rotate(degrees(B-A))*scale(abs(B-A))*pict;
    add(pict);
    for (int i=0;i<connections.length;++i) connections[i]=A+(B-A)*connections[i];
    return connections;
    }

    //testing part

    size(300);
    pair A=(0,0), B=(10,10), C=(10,0);
    join(A,B,blue,red,fontsize(8),'bat1bat2resi',"5V","7V","8Ohm");
    pair[]u=join(A,C,blue,red,fontsize(8),'tri3tri4',"","");
    for(int i=0;i<2;++i)draw(B--u[i],blue);
    join(C,B,blue,red,fontsize(8),'bat1resibat2',"$3V$","This is a resistor","$5V$");

     
  • Kazuo Teramoto

    Kazuo Teramoto - 2007-08-30

    Logged In: YES
    user_id=1621319
    Originator: NO

    I like to see someone working on this (I tried to no successes.. )
    But I like to ask if it can be made to don't distorter if used with IgnoreAspect,
    I like to use size(246.000pt,138.375pt,IgnoreAspect) (the size o my columns and in 16:9 proportion) but if i try I got a rhombus shape... Can this be done that the wires be resized but the components maintain they aspect?

     
  • fedja30

    fedja30 - 2007-08-31

    Logged In: YES
    user_id=1872477
    Originator: NO

    I believe that this will be a tremendous headache. The reason is that you not only need to maintain the aspect ratio of the elements (this, probably, could be accomplished using frames instead of pictures), but also to rotate them appropriately when the picture gets rescaled. Now, the rotation angle depends on the slope of the wire on which the element is put, not just on the resizing factors, so you need to remember which element is on which wire and do the appropriate transformations in the end before the output is produced. The only possible way for that I see is to create a huge array of all nodes, wires, and elements, determine the size of the picture and rescaling factors and, only after that, draw the wires and the elements. I would strongly prefer not to have to encode the entire circuit in some way before executing the first draw command but rather to draw things one by one as soon as they appear . On the other hand, I am not a real authority here, so I'd like to see what the "Asymptote gurus" have to say on both whether such thing is feasible at all and whether it is worth the effort. Anyway, here is the first question we should decide upon:

    Should we encode the entire curcuit before starting to draw it or should we just make every command a drawing command like I tried to do in my examples?

    Actually, this is what I'll be doing a lot at this stage: asking a lot of (stupid) questions. I hope that this way I'll be able to understand what is really desired and say either "OK, I'll do it this way" or "No, that's well over my head" in the end :)

    I posted a code in the help forum, which is (in my humble opinion) better than the one I posted here, so you may want to look at it.

    Thanks for your comments, folks! I have enough enthusiasm to devote my free time (alas, I don't have too much of it) to the project but my "technical expertise" may be not as good as needed. Anyway, I'm willing to give it a try if you are ready to help by answering my stupid questions and showing me some programming tricks now and then.

     
  • John Bowman

    John Bowman - 2007-09-14

    Logged In: YES
    user_id=808099
    Originator: NO

    Automatic resizing should be possible using pictures and deferred drawing,
    as is done in this example:

    http://asymptote.sourceforge.net/gallery/flowchartdemo.asy

    In fact, the flowchart module could probably be adapted for drawing circuits.

     
  • John Bowman

    John Bowman - 2010-09-27
    • status: open --> closed-accepted
     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks