|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我想用python实现cad的一些自动画图,先是写了一个基本功能,弄了一些类分别画矩形画菱形插入文字之类,运行结果是可以的,f5之后通过在idle里输入命令画出来。 、
但实际用的时候;这样肯定不行,又繁琐别人不会用而且还不如直接画省力。所以需要做图形化,做到点一下按钮就相当于输入了这个命令。
于是我就跟着小甲鱼的教程的Tkinter的部分往下写了,新弄了一个图形界面文件来实现调用之前基本功能的文件。然后中途出现很多****变量未定义、没有**方法之类的问题。
现在这些问题虽然解决了(程序能够正常运行实现功能),但是中间的代码结构感觉肯定有些问题,不够规范,理解也不深,就是一点试错改出来的。使得其繁琐,以后修改也容易出错。
所以想问问大家怎么修改一下,能使得代码变得更好一些。比如我觉得是不是不需要这么多self不需要重复定义这么多方法??因为我想同步修改另一个文件里多个类里相同的属性所以每个类都设定了方法和类中的属性,这些感觉应该可以简化?
还有在之前的提问中,有大佬说一般不要实例化,而写成:if __name__ == '__main__': 这个是为什么呢?我用实例化是当时想着运行的时候类里就要执行的命令应该放在实例化里,这个理解大概有问题,应该如何理解如何修改代码呢?
下面是代码,两个文件,分别是现基本画图插入文字功能的代码plant.py和图形界面化的代码(引用了plant.py)
plant、:from pyautocad import Autocad, APoint,aDouble
acad = Autocad(create_if_not_exists = True)
import math
import numpy as np
pyacad = Autocad(create_if_not_exists=True)
pyacad.prompt("Hello! AutoCAD from pyautocad.")
print(pyacad.doc.Name)
CPX = 500 #中心点 CenterPointX
CPY = 500 # CenterPointY
DeltaX = 50 #间距
DeltaY = 30
HW = 13 #HalfRWidth 矩形一半宽
HH = 6.5 #HalfRHight 矩形一半高
class SetPoint:
CPX = 500 #中心点 CenterPointX
CPY = 500 # CenterPointY
def creat(self):
self.juxing = Rectangle()
self.lingxing = Choose()
self.wenzi = InsertText()
self.block = Block()
def set_CP(self):
self.CPX = float(input('请输入x坐标:'))
self.CPY = float(input('请输入y坐标:'))
self.juxing.set_CP()
self.lingxing.set_CP()
self.wenzi.set_CP()
self.block.set_CP()
def set_Delta(self):
Delta = float(input('请设置间隔:'))
DeltaX = Delta + 2*HW
DeltaY = Delta + 2*HH
def set_RWH(self):
self.juxing.set_RWH()
def set_RCH(self):
self.lingxing.set_RWH()
def changeUpCP(self):
self.juxing.changeUpCP()
self.lingxing.changeUpCP()
self.wenzi.changeUpCP()
self.block.changeUpCP()
def changeDownCP(self):
self.juxing.changeDownCP()
self.lingxing.changeDownCP()
self.wenzi.changeDownCP()
self.block.changeDownCP()
def changeLeftCP(self):
self.juxing.changeLeftCP()
self.lingxing.changeLeftCP()
self.wenzi.changeLeftCP()
self.block.changeLeftCP()
def changeRightCP(self):
self.juxing.changeRightCP()
self.lingxing.changeRightCP()
self.wenzi.changeRightCP()
self.block.changeRightCP()
class Rectangle:
HRW = 13 #HalfRWidth 矩形一半宽
HRH = 6.5 #HalfRHight 矩形一半高
DeltaX = 50 #间距
DeltaY = 30
def set_CP(self):
self.CPX = aa.CPX
self.CPY = aa.CPY
def set_Delta(self):
self.Delta = aa.Delta
self.DeltaX = Delta + 2*HW
self.DeltaY = Delta + 2*HH
def set_RWH(self):
self.HRW = float(input('请输入矩形的宽度:'))/2
self.HRH = float(input('请输入矩形的高度:'))/2
def changeUpCP(self):
self.CPY = self.CPY + self.DeltaY
def changeDownCP(self):
self.CPY = self.CPY - self.DeltaY
def changeLeftCP(self):
self.CPX = self.CPX - self.DeltaX
def changeRightCP(self):
self.CPX = self.CPX + self.DeltaX
def plant(self):
x = [self.CPX-self.HRW,self.CPX+self.HRW,self.CPX+self.HRW,self.CPX-self.HRW]
y = [self.CPY+self.HRH,self.CPY+self.HRH,self.CPY-self.HRH,self.CPY-self.HRH]
pnts = [APoint(x[0], y[0]), APoint(x[1], y[1]), APoint(x[1], y[1]),
APoint(x[2], y[2]), APoint(x[2], y[2]), APoint(x[3], y[3]),
APoint(x[3], y[3]), APoint(x[0], y[0])]
pnts = [j for i in pnts for j in i] # 将各点坐标顺序变换为行数据
pnts = aDouble(pnts) # 转化为双精度浮点数
plineObj = pyacad.model.AddPolyLine(pnts)
plineObj.Closed = True # 闭合多段线
class Choose:
HCW = 13 #HalfRWidth 矩形一半宽
HCH = 6.5 #HalfRHight 矩形一半高
DeltaX = 50 #间距
DeltaY = 30
def set_CP(self):
self.CPX = aa.CPX
self.CPY = aa.CPY
def set_Delta(self):
self.Delta = aa.Delta
self.DeltaX = Delta + 2*HW
self.DeltaY = Delta + 2*HH
def set_CWH(self):
self.HCW = float(input('请输入菱形的宽度:'))/2
self.HCH = float(input('请输入菱形的高度:'))/2
def changeUpCP(self):
self.CPY = self.CPY + self.DeltaY
def changeDownCP(self):
self.CPY = self.CPY - self.DeltaY
def changeLeftCP(self):
self.CPX = self.CPX - self.DeltaX
def changeRightCP(self):
self.CPX = self.CPX + self.DeltaX
def plant(self):
x = [self.CPX,self.CPX+self.HCW,self.CPX,self.CPX-self.HCW]
y = [self.CPY+self.HCH,self.CPY,self.CPY-self.HCH,self.CPY]
pnts = [APoint(x[0], y[0]), APoint(x[1], y[1]), APoint(x[1], y[1]),
APoint(x[2], y[2]), APoint(x[2], y[2]), APoint(x[3], y[3]),
APoint(x[3], y[3]), APoint(x[0], y[0])]
pnts = [j for i in pnts for j in i] # 将各点坐标顺序变换为行数据
pnts = aDouble(pnts) # 转化为双精度浮点数
plineObj = pyacad.model.AddPolyLine(pnts)
plineObj.Closed = True # 闭合多段线
class InsertText:
DeltaX = 50 #间距
DeltaY = 30
text = '请在文本框中输入文字'
def set_CP(self):
self.CPX = aa.CPX
self.CPY = aa.CPY
def set_Delta(self):
self.Delta = aa.Delta
self.DeltaX = Delta + 2*HCW
self.DeltaY = Delta + 2*HCH
def changeUpCP(self):
self.CPY = self.CPY + self.DeltaY
def changeDownCP(self):
self.CPY = self.CPY - self.DeltaY
def changeLeftCP(self):
self.CPX = self.CPX - self.DeltaX
def changeRightCP(self):
self.CPX = self.CPX + self.DeltaX
def in_text(self):
p = APoint(self.CPX,self.CPY)
#text = acad.model.AddText('Hi !' , p, 2.5)
#insert = acad.model.AddText("{0}".format(text), p, 3)
insert = acad.model.AddText(self.text, p , 3)
AlignNum = 10
insert.Alignment = AlignNum
insertPnt = p
insert.TextAlignmentPoint = insertPnt
class Block:
HCW = 13 #HalfRWidth 矩形一半宽
HCH = 6.5 #HalfRHight 矩形一半高
DeltaX = 50 #间距
DeltaY = 30
def set_CP(self):
self.CPX = aa.CPX
self.CPY = aa.CPY
def set_Delta(self):
self.Delta = aa.Delta
self.DeltaX = Delta + 2*HW
self.DeltaY = Delta + 2*HH
def set_CWH(self):
self.HCW = float(input('请输入菱形的宽度:'))/2
self.HCH = float(input('请输入菱形的高度:'))/2
def changeUpCP(self):
self.CPY = self.CPY + self.DeltaY
def changeDownCP(self):
self.CPY = self.CPY - self.DeltaY
def changeLeftCP(self):
self.CPX = self.CPX - self.DeltaX
def changeRightCP(self):
self.CPX = self.CPX + self.DeltaX
def plant(self):
insertionPnt = APoint(self.CPX,self.CPY)
RetVal = acad.model.InsertBlock(insertionPnt, "D:\\test1.dwg", 1, 1, 1, 0 )
aa=SetPoint()
aa.creat()
aa.set_CP()
图形界面:
import tkinter as tk
import plant
class APP:
def __init__(self,master):
frame = tk.Frame(master)
frame.grid()
self.changeUpCP = tk.Button(frame,text='向上移动位置',fg ='blue',command = plant.aa.changeUpCP)
self.changeDownCP = tk.Button(frame,text='向下移动位置',fg ='blue',command = plant.aa.changeDownCP)
self.changeLeftCP = tk.Button(frame,text='向左移动位置',fg ='blue',command = plant.aa.changeLeftCP)
self.changeRightCP = tk.Button(frame,text='向右移动位置',fg ='blue',command = plant.aa.changeRightCP)
self.juxingwithtxt = tk.Button(frame,text='插入文字和矩形框',fg ='blue',command = lambda:[plant.aa.juxing.plant(),getandin_text()])
self.juxing = tk.Button(frame,text='画矩形',fg ='blue',command = plant.aa.juxing.plant)
self.lingxing = tk.Button(frame,text='画菱形',fg ='blue',command = plant.aa.lingxing.plant)
self.lingxingwithtxt = tk.Button(frame,text='插入文字和选择框',fg ='blue',command = lambda:[plant.aa.lingxing.plant(),getandin_text()])
self.wenzi = tk.Button(frame,text='插入文字',fg ='blue',command = getandin_text)
self.block = tk.Button(frame,text='插入块(测试)',fg ='blue',command = plant.aa.block.plant)
self.changeUpCP.grid(row=1,column=1)
self.changeDownCP.grid(row=3,column=1)
self.changeLeftCP.grid(row=2,column=0)
self.changeRightCP.grid(row=2,column=2)
self.juxing.grid(row=1,column=4)
self.juxingwithtxt.grid(row=1,column=5)
self.lingxingwithtxt.grid(row=2,column=5)
self.lingxing.grid(row=2,column=4)
self.wenzi.grid(row=3,column=4)
self.block.grid(row=3,column=5)
def getandin_text():
plant.aa.wenzi.text = e1.get()
plant.aa.wenzi.in_text()
root =tk.Tk()
root.title('画流程图')
e1 = tk.Entry(root)
e1.grid(row=1,column=1,padx= 0,pady =20)
app = APP(root)
root.mainloop()
python设计模式了解一下,相同的部分多了,就写抽象类
|
|