Gap causes draw to fail

Issue #63 resolved
Derek Kozel created an issue

It looks like a gap isn’t adding a segment but the Gap._place defers to the parent Element2Term._place which assumes that it has a segment available to index on. I also tested with an additional line placed after the gap and the same error was thrown on draw. I’m using the latest git HEAD in a Jupyter Notebook.

import schemdraw
import schemdraw.elements as elm

with schemdraw.Drawing() as d:
    d += elm.Line()
    d += elm.Gap()

Throws the following traceback:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Input In [48], in <cell line: 4>()
      4 with schemdraw.Drawing() as d:
      5     d += elm.Line()
----> 6     d += elm.Gap()

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\schemdraw\schemdraw.py:178, in Drawing.__exit__(self, exc_type, exc_val, exc_tb)
    176 if self.show:
    177     try:
--> 178         display(self.draw())
    179     except NameError:  # Not in Jupyter/IPython
    180         self.draw()

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\schemdraw\schemdraw.py:366, in Drawing.draw(self, showframe, show, ax, backend)
    364 self.backend = backend
    365 for element in self.elements:
--> 366     element._draw(self.fig)
    368 if show:
    369     # Show figure in window if not inline/Jupyter mode
    370     self.fig.show()  # type: ignore

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\schemdraw\elements\elements.py:669, in Element._draw(self, fig)
    667 ''' Draw the element on a Figure '''
    668 if len(self.segments) == 0:
--> 669     self._place((0, 0), 0)
    670 for segment in self.segments:
    671     segment.draw(fig, self.transform, **self._cparams)

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\schemdraw\elements\lines.py:95, in Gap._place(self, dwgxy, dwgtheta, **dwgparams)
     93 def _place(self, dwgxy: XY, dwgtheta: float, **dwgparams) -> tuple[Point, float]:
     94     ''' Calculate element placement, adding lead extensions '''
---> 95     result = super()._place(dwgxy, dwgtheta, **dwgparams)
     96     self.segments = self.segments[1:]  # Remove line segment, but keep any text
     97     return result

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\schemdraw\elements\elements.py:832, in Element2Term._place(self, dwgxy, dwgtheta, **dwgparams)
    829     totlen = util.dist(xy, endpt)
    830     theta = -90 if xy.y > y else 90
--> 832 self.anchors['istart'] = self.segments[0].path[0]  # type: ignore
    833 self.anchors['iend'] = self.segments[0].path[-1]  # type: ignore
    834 if self._cparams.get('extend', True):

IndexError: list index out of range

Comments (2)

  1. cdelker repo owner

    Looks like it fails when Gap is placed without specifying endpoints. 79e151e should fix it - by setting a new ‘visible’ parameter on Segment, rather than trying to mess with removing Segments from the list.

  2. Log in to comment