from pyautocad import Autocad, APoint from ctypes import cdll from utils import getarray,GetAllModule,getHandler import os, time, shutil from pyautocad.cache import Cached _sopen = cdll.msvcrt._sopen _close = cdll.msvcrt._close _SH_DENYRW = 0x10 _rootPath = "F:\\autocad_temp\\" _originFile = "origin.dwg" templateIndex = {'md_xs','md_py','md_rc'} doorName = 'ssdr_800' correctFilePath = "newfile3\\" errorFilePath = "errorfile3\\" # 这个true表示没有文件则打开一个,CAD有弹窗时会打开或者创建失败 # print(acad.doc.Name) # 判断文件是否打开 def is_open(filename): if not os.access(filename, os.F_OK): print("file doesn't exist") return False # file doesn't exist h = _sopen(filename, 0, _SH_DENYRW, 0) if h == 3: _close(h) print("file is not opened by anyone else") return False # file is not opened by anyone else print("file is already open") return True # file is already open # 1、打开dwg文件 def openOriginFile(): # if(is_open(_rootPath+_originFile)): # return print("begin to open origin file") os.popen("acad " + _rootPath + _originFile) time.sleep(10) print("origin file opened") def openFile(fileName): # if(is_open(_rootPath+_originFile)): # return print("begin to open file") os.popen("acad " + fileName) time.sleep(20) print("file opened") def startAcadAndGetHandler(): print("begin to start acad") pass # 关闭autoCad def closeAcad(): os.system("taskkill /F /IM acad.exe") # 2、缓存房间、门、模块 def tempStorageModules(): acad = Autocad(create_if_not_exists=False) acad.prompt("Hello, Autocad from Python\n") print(acad.doc.Name) cached_acad = Cached(acad) return cached_acad # 3、获取文件中的房间、门、模块参数 def getModuleData(): pass # 4、获取所有模块的摆法 def getPutArray(): pass # 5、对每一种摆法,新建dwg文件, def copyToNewFile(): os.system("") pass # 6、摆放房间、门、模块后保存 def saveNewFile(): pass def copyfile(srcfile, dstfile): if not os.path.isfile(srcfile): print("%s not exist!" % (srcfile)) else: fpath, fname = os.path.split(dstfile) # 分离文件名和路径 if not os.path.exists(fpath): os.makedirs(fpath) # 创建路径 shutil.copyfile(srcfile, dstfile) # 复制文件 print("copy %s -> %s" % (srcfile, dstfile)) def createNewFileAndMove(newFil): pass def myprocess(m, n): positons = getarray.getAllPosition(m, n) for positon in positons: # indexAndPositionArray = getarray.getitemIndexAndPositionArray(positons[positon], m, n) number = getarray.getitemIndexArrayNumber(positons[positon], m, n) if (number == 1): newFileName = _rootPath + correctFilePath + "newfile" + str(positons[positon]) + ".dwg" if(not os.path.exists(newFileName)): moveSingleTemplate(newFileName,positons[positon]) # break else: moduleIndexs = getarray.getModuleIndex(positons[positon],m,n) if(GetAllModule.isWorkable(positons[positon])): for moduleIndex in moduleIndexs: newFileName = _rootPath + correctFilePath+ "newfile" + str(positons[positon]) + str(moduleIndex) + ".dwg" if(not os.path.exists(newFileName)): # copyfile(_rootPath + _originFile, newFileName) moveMultipleTemplate(newFileName,positons[positon],moduleIndex) # break # break else: print(positons[positon],"is not workable !!") # break GetAllModule.scoreModule() def moveSingleTemplate(fileName,position): # openFile(fileName) GetAllModule.resetConfUsed() result = GetAllModule.moveBlockReference(position) time.sleep(1) acad = Autocad(create_if_not_exists = False) acad.prompt("Hello, Autocad from Python\n") if(result == 0): fileName = fileName.replace(correctFilePath,errorFilePath) copyfile(_rootPath + _originFile, fileName) acad.doc.SaveAs(fileName) time.sleep(1) getHandler.backToOrigin() def moveMultipleTemplate(fileName,position,moduleIndex): GetAllModule.resetConfUsed() result = GetAllModule.moveBlockReferenceWithNewIndex(position,moduleIndex) time.sleep(1) acad = Autocad(create_if_not_exists = False) acad.prompt("Hello, Autocad from Python\n") if(result == 0): fileName = fileName.replace(correctFilePath,errorFilePath) copyfile(_rootPath + _originFile, fileName) acad.doc.SaveAs(fileName) time.sleep(1) getHandler.backToOrigin() # openOriginFile() # tempStorageModules() # print(is_open(_rootPath+_originFile)) # myprocess(4, 3) # openFile(r"F:\autocad_temp\newfile023.dwg") # is_open(r"F:\autocad_temp\origin11.dwg") # acad = Autocad(create_if_not_exists = False) # acad.prompt("Hello, Autocad from Python\n") # print(acad.doc.Name) # moveMultipleTemplate(r"F:\autocad_temp\newfile2\newfile.dwg",'001',[0,1,2]) # moveMultipleTemplate(r"F:\autocad_temp\newfile2\newfile3.dwg",'020',[2,1,0]) # moveMultipleTemplate(r"F:\autocad_temp\newfile2\newfile4.dwg",'222',[1,2,0]) # moveMultipleTemplate(r"F:\autocad_temp\newfile2\newfile5.dwg",'110',[1,0,2]) # moveMultipleTemplate(r"F:\autocad_temp\newfile2\newfile5.dwg",'222',[1,2,0]) # moveSingleTemplate(r"F:\autocad_temp\newfile1113\newfile2.dwg",'310')