Home > front end >  Add properties to each function (extend function)
Add properties to each function (extend function)

Time:10-18

I am making class for phyton script to manipulate AutoCAD. I had different function before for each action but I decided to make it as object to be more readable in script. I found myself repeating part of the code all the time and I was wondering how I can code this different way so that each function get LAYER and LINETYPE_SCALE automatically so I do not need to rewrite it all the time because there will be much more functions and all of them will have that part of code. Also I want to be able to change the code in one place if I need to. Thank you

class Draw:
    def Rectangle(X, Y, WIDTH, HEIGHT, LAYER=0, LINETYPE_SCALE=1):
        rectangle = acad.model.AddLightWeightPolyline(aDouble(
            X, Y,
            X, Y   HEIGHT,
            X   WIDTH, Y   HEIGHT,
            X   WIDTH, Y,
            X, Y
        ))
        rectangle.Layer = LAYER
        rectangle.LinetypeScale = LINETYPE_SCALE
        return rectangle

    def Square(X, Y, WIDTH, LAYER=0, LINETYPE_SCALE=1):
        square = acad.model.AddLightWeightPolyline(aDouble(
            X, Y, 
            X, Y   WIDTH,
            X   WIDTH, Y   WIDTH,
            X   WIDTH, Y,
            X, Y
        ))
        square.Layer = LAYER
        square.LinetypeScale = LINETYPE_SCALE
        return square

    def Circle(X, Y, RADIUS, LAYER=0, LINETYPE_SCALE=1):
        circle = acad.model.AddCircle(APoint(X, Y), RADIUS)
        circle.Layer = LAYER
        circle.LinetypeScale = LINETYPE_SCALE
        return circle

    def Polyline(POINTS, LAYER=0, LINETYPE_SCALE=1):
        polyline = acad.model.AddLightWeightPolylin(aDouble(POINTS))
        polyline.Layer = LAYER
        polyline.LinetypeScale = LINETYPE_SCALE
        return polyline

CodePudding user response:

You are searching for solution which could help you to avoid copy paste of same code and here it is:

class Draw:
    @staticmethod
    def _finalize(ENTITY, LAYER=0, LINETYPE_SCALE=1):
        ENTITY.Layer = LAYER
        ENTITY.LinetypeScale = LINETYPE_SCALE
        return ENTITY

    @staticmethod
    def Rectangle(X, Y, WIDTH, HEIGHT, LAYER=0, LINETYPE_SCALE=1):
        rectangle = acad.model.AddLightWeightPolyline(aDouble(
            X, Y,
            X, Y   HEIGHT,
            X   WIDTH, Y   HEIGHT,
            X   WIDTH, Y,
            X, Y
        ))
        return Draw._finalize(rectangle, LAYER, LINETYPE_SCALE)

    @staticmethod
    def Square(X, Y, WIDTH, LAYER=0, LINETYPE_SCALE=1):
        return Draw.Rectangle(X, Y, WIDTH, WIDTH, LAYER, LINETYPE_SCALE)

    @staticmethod
    def Circle(X, Y, RADIUS, LAYER=0, LINETYPE_SCALE=1):
        circle = acad.model.AddCircle(APoint(X, Y), RADIUS)
        return Draw._finalize(circle, LAYER, LINETYPE_SCALE)

    @staticmethod
    def Polyline(POINTS, LAYER=0, LINETYPE_SCALE=1):
        polyline = acad.model.AddLightWeightPolylin(aDouble(POINTS))
        return Draw._finalize(polyline, LAYER, LINETYPE_SCALE)

BUT! Class with bunch of static methods shouldcould be easily replaced with module containing functions. At all, I'd prefer to have separate class for each shape, it seems to be better solution.

Anyway, improving working code is not an object at Stack Overflow, you can post a question at Code Review.

CodePudding user response:

Something like the below: use abstract base class . Code was not tested since I dont have acad API.

from abc import ABC, abstractmethod


class Shape(ABC):
    def __init__(self, layer=0, line_type_scale=1):
        self.layer = layer
        self.line_type_scale = line_type_scale

    @abstractmethod
    def draw():
        pass


def Circle(Shape):
    def __init__(self, x, y, radius,layer,line_type_scale):
        super().__init__(layer,line_type_scale)
        self.x = x
        self.y = y
        self.raduis = radius
    def draw():
        circle = acad.model.AddCircle(APoint(self.x, self.y), self.radius)
        circle.Layer = self.layer
        circle.LinetypeScale = self.line_type_scale
        return circle
  • Related