def offset(self, dis): '''outwards if dis > 0''' typeTest([Num.const], dis) assert self.radius + dis >= 0 return arc(self.center, self.radius + dis, self.start_angle, self.end_angle)
def __init__(self, x, y, z): typeTest([Num.const, Num.const, Num.const], x, y, z) self.para = ("x", "y", "z") self.x = x self.y = y self.z = z
def is_intersect(self, another): '''return True if is intersect''' typeTest([line], another) il = self.sympy.intersection(another.sympy) if il != [] and isinstance(il[0], Point): return True return False
def is_overlapped(self, another): '''return True if is overlapped''' typeTest([line], another) il = self.sympy.intersection(another.sympy) if il != [] and isinstance(il[0], Segment): return True return False
def __mul__(self, another): typeTest([Num.const + [self.__class__]], another) if self.__class__ == another.__class__: return sum(map(lambda x, y: x * y, self.args, another.args)) else: new = tuple(x * another for x in self.args) return eval(fun(vector, new))
def move(self, v): '''move model position''' typeTest([vector], v) ret = Models() for m in self: ret.add(m.move(v)) return ret
def offset(self, dis): '''return a parallel of a line, the direction vector is clw compare with the vector(p1, p2) if dis > 0''' typeTest([Num.const], dis) v = vector(self.p2.x - self.p1.x, self.p2.y - self.p1.y).clw().unit * dis return self.move(v)
def __init__(self, center, radius, start_angle, end_angle): typeTest([point, Num.const, Num.const, Num.const], center, radius, start_angle, end_angle) self.para = ("center", "radius", "start_angle", "end_angle") self.center = center self.radius = abs(radius) self.start_angle = start_angle self.end_angle = end_angle
def move(self, v): '''move along vector''' typeTest([vector], v) ret = self for apara in self.para: if isinstance(self.__dict__[apara], point): ret.__dict__[apara] = self.__dict__[apara].move(v) return ret
def angle_between(self, another): '''Angle between two lines, measured in radians''' typeTest([line], another) p1 = eval(fun(Point, self.p1.args)) p2 = eval(fun(Point, self.p2.args)) p3 = eval(fun(Point, another.p1.args)) p4 = eval(fun(Point, another.p2.args)) l1, l2 = Line(p1, p2), Line(p3, p4) return float(l1.angle_between(l2))
def rotate2d(self, rad=math.pi / 2, clw=True): '''rotate vector, measured in radians, clw 90 deg. by default, (loss of significance)''' typeTest([bool], clw) if not clw: rad = -rad y = math.tan(rad - math.atan(self.y / self.x)) ret = vector(1, y).unit * self.length return ret
def clw(self, n=1): '''clockwise 90 degrees for n times, counter-clockwise if n<0''' typeTest([int], n) ret = self for i in range(n % 4): if isinstance(ret, vector2d): ret = vector(ret.y, -ret.x) else: ret = vector(ret.y, -ret.x, ret.z) return ret
def __init__(self, center, major_axis, ratio, start_param=0, end_param=math.pi * 2): typeTest([point, Num.const, Num.const, Num.const], center, major_axis, ratio, start_param, end_param) self.para = ("center", "major_axis", "ratio", "start_param", "end_param") self.center = center self.major_axis = abs(major_axis) self.ratio = abs(ratio) self.start_param = start_param self.end_param = end_param
def intersection(self, another): '''only for lines and lwpolylines''' typeTest([[line, lwpolyline]], another) ret = [] if isinstance(another, lwpolyline): for i in self.sympy: for j in another.sympy: ret += i.intersection(j) if isinstance(another, line): for i in self.sympy: ret += i.intersection(another.sympy) return Models(map(lambda x: eval(sympy2m(x)), list(set(ret))))
def move(self, v): '''vector move''' typeTest([vector], v) if isinstance(self, point2d): if isinstance(v, vector2d): return point(self.x + v.x, self.y + v.y) if isinstance(v, vector3d): return point(self.x + v.x, self.y + v.y, v.z) elif isinstance(self, point3d): if isinstance(v, vector2d): return point(self.x + v.x, self.y + v.y, self.z) if isinstance(v, vector3d): return point(self.x + v.x, self.y + v.y, self.z + v.z)
def __init__(self, *coords): if len(coords) not in [2, 3]: raise Exception("Expect 2 or 3 coords, given %d" % len(coords)) typeTest([Num.const] * len(coords), *coords) self.x = coords[0] self.y = coords[1] if len(coords) == 2: self.para = ("x", "y") self.__class__ = type(point2d(0, 0)) else: self.para = ("x", "y", "z") self.z = coords[2] self.__class__ = type(point3d(0, 0, 0))
def merge(self, another): '''merge two colinear lines''' typeTest([line], another) il = self.sympy.intersection(another.sympy) if il != [] and isinstance(il[0], Segment): p1, p2 = self.p1, self.p2 p3, p4 = another.p1, another.p2 pl = [p1, p2, p3, p4] if self.slope in [float("inf"), -float("inf")]: pl.sort(lambda a, b: cmp(a.y, b.y)) else: pl.sort(lambda a, b: cmp(a.x, b.x)) return line(pl[0], pl[-1]) raise ValueError("Expect overlapped lines")
def draw_Models(aModels, cad_ver="AC1015", save_name="new.dxf"): '''draw Models, cad_ver can be \"2010\" or \"AC1024\"''' typeTest([Models, str, str], aModels, cad_ver, save_name) # create dxf obj and modelspace if cad_ver in ver.const.keys(): cad_ver = ver.const[cad_ver] dxf = ezdxf.new(cad_ver) msp = dxf.modelspace() # draw models in Models for m in aModels: m.draw(msp) # save as dxf file dxf.saveas(save_name)
def __init__(self, *args): if len(args) == 2 and isinstance(args[0], point) and\ args[1].__class__ == args[1].__class__: p1 = args[0] p2 = args[1] args = [] for i in range(len(p1.args)): args.append(p2.args[i] - p1.args[i]) if len(args) not in [2, 3]: raise Exception("Expect 2 or 3 args, given %d" % len(args)) typeTest([Num.const] * len(args), *args) self.x = args[0] self.y = args[1] if len(args) == 2: self.para = ("x", "y") self.__class__ = type(vector2d(0, 0)) else: self.para = ("x", "y", "z") self.z = args[2] self.__class__ = type(vector3d(0, 0, 0))
def replace(self, old, new): "replace old to new one" typeTest([myObject, myObject], old, new) self.remove(old) self.add(new)
def draw(self, msp, dxfattribs=None): '''draw in ezdxf''' typeTest([ezdxf.modern.layouts.Layout], msp) return msp.add_line(self.p1.args, self.p2.args, dxfattribs)
def __init__(self, text): typeTest([str], text) self.para = ("text",) self.text = text
def draw(self, msp, dxfattribs=None): '''draw in ezdxf''' typeTest([ezdxf.modern.layouts.Layout], msp) return msp.add_mtext(self.text, dxfattribs)
def __init__(self, *points): typeTest([point3d] * len(points), *points) self.para = ("*points",) self.points = points
def draw(self, msp, dxfattribs=None): '''draw in ezdxf''' typeTest([ezdxf.modern.layouts.Layout], msp) return msp.add_polyline3d([x.args for x in self.args], dxfattribs)
def __init__(self, center, radius): typeTest([point, Num.const], center, radius) self.para = ("center", "radius") self.center = center self.radius = abs(radius)
def draw(self, msp, dxfattribs=None): '''draw in ezdxf''' typeTest([ezdxf.modern.layouts.Layout], msp) return msp.add_ellipse(self.center, self.major_axis, self.ratio, self.start_param, self.end_param, dxfattribs)
def offset(self, dis): '''outwards if dis > 0''' typeTest([Num.const], dis) assert self.radius + dis >= 0 return circle(self.center, self.radius + dis)
def draw(self, msp, dxfattribs=None): '''draw in ezdxf''' typeTest([ezdxf.modern.layouts.Layout], msp) return msp.add_circle(self.center.args, self.radius, start_angle, end_angle, dxfattribs)
def __init__(self, *points): typeTest([point2d] * len(points), *points) self.para = ("*points",) self.points = points assert len(self.args) >= 2