cdelker / schemdraw

MIT License
103 stars 20 forks source link

when `elm.Ic` instance are multi, their anchors are broken #2

Closed Cartman0 closed 1 year ago

Cartman0 commented 1 year ago

(I want to align Ic chips to row(vertical) direction. (to express 8-bit memory))

The following code has drawn the following diagram.

%config InlineBackend.figure_format = 'svg'
%matplotlib inline

import schemdraw
from schemdraw import logic
import schemdraw.elements as elm

with schemdraw.Drawing() as d:
    D = []

    i=0
    _Dl1 = elm.Ic(pins=[elm.IcPin(name='s', side='left', anchorname=f's{i+1}'),
              elm.IcPin(name=f'i{i+1}', side='left'),
              elm.IcPin(name='$\\overline{Q}$', side='right', anchorname='QBAR'),
              elm.IcPin(name='Q', side='right')],
                 edgepadW = .1, pinspacing=1).right()
    d += _Dl1
    d += elm.Line().left(d.unit*.1).at(_Dl1.anchors[f's{i+1}'])
    D.append(_Dl1)
    d += elm.Line().down().length(2)
    # print(_Dl1.__dict__)

    i=1
    d.move_from(_Dl1.xy, dy=-3)
    _Dl2 = elm.Ic(pins=[elm.IcPin(name='s', side='left', anchorname=f's{i+1}'),
              elm.IcPin(name=f'i{i+1}', side='left'),
              elm.IcPin(name='$\\overline{Q}$', side='right', anchorname='QBAR'),
              elm.IcPin(name='Q', side='right')],
                 edgepadW = .1, pinspacing=1).right()
    d += _Dl2
    d += elm.Line().left().at(_Dl2.anchors[f's{i+1}']).tox(0).dot()

IcChips

In the code, it will draw line to the left from 2nd Ic.s, but has drawn line to the left from 1st Ic.s in the result diagram.

my usage is wrong?

my env:

cdelker commented 1 year ago

The coordinates stored in the Element.anchors dictionary are relative to the element, not to the drawing. You can use Element.absanchors to get the absolute position of the anchor relative to the drawing:

d += elm.Line().left().at(_Dl2.absanchors[f's{i+1}']).tox(0).dot()

Alternatively, use the attribute that is created when the element is added to the drawing:

d += elm.Line().left().at(_Dl2.s2).tox(0).dot()
Cartman0 commented 1 year ago

@cdelker thx for rep! what are the differences between .s2, .anchors['s2'] and .absanchors['s2'] ?

cdelker commented 1 year ago

.anchors are set in the element definition, and are relative to the element's local origin. absanchors are the anchor points translated to global coordinates once an element is placed in a drawing, and are relative to the drawing origin. Attributes like s2 attribute store the same value as the absanchors['s2'] but is set as an attribute for convenience.