Example #1
0
class DrawCAD:
    def __init__(self):
        self.acad = Autocad(
            create_if_not_exists=True)  # so far app is not open
        self.dimscale = 100  # annotative scale for sizes #TODO масштаб должен настраиваться автоматически
        print("AutoCad запущен. Создан документ", self.acad.doc.Name)
        self.acad.Application.Documents.open(
            "d:\Picture_ENG.dwg")  #TODO путь с пробелами вызывает трудности
        print(self.acad.doc.Name)
        self.acad.size_scale(
            self.dimscale)  # setting annotative scale for sizes
        self._clear()  #стирание из файла

    def _clear(self):
        #TODO убрать костыль так, чтобы удаление не вызывало исключений
        while True:
            try:
                for obj in self.acad.iter_objects():
                    obj.Delete()
                break
            except Exception:
                print(
                    "Exception: ошибка при удалении. Выход из исключения, продолжение работы"
                )
                continue

    def _drawLabel(self, shape):
        #TODO в зависимости от ориентации блока, вывести текст в середине
        print(shape.label)

    #drawing shapes consisting of points
    def _drawOverallDim(self, shape):
        #connecting all points of overall dimensions
        num = len(shape.points["overallDim"])
        for i in range(num - 1):
            self.acad.model.AddLine(shape.points["overallDim"][i],
                                    shape.points["overallDim"][i + 1])

        # to close the shape
        self.acad.model.AddLine(shape.points["overallDim"][num - 1],
                                shape.points["overallDim"][0])

        # draw overall sizes: horizontal and vertical #TODO проверить работу простановки размеров на повернутых объектах
        self._drawSize(shape.points["overallDim"][0],
                       shape.points["overallDim"][1], 2000)
        self._drawSize(shape.points["overallDim"][0],
                       shape.points["overallDim"][3], 1000)

    # drawing size btw 2 points and with defined indent
    def _drawSize(self, p1, p2, indent):
        self.acad.linear_size(p1.x, p1.y, p2.x, p2.y, indent)

    def draw(self, shape):
        self._drawOverallDim(shape)