main.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. from pyautocad import Autocad, APoint
  2. from ctypes import cdll
  3. from utils import getarray,GetAllModule,getHandler
  4. import os, time, shutil
  5. from pyautocad.cache import Cached
  6. _sopen = cdll.msvcrt._sopen
  7. _close = cdll.msvcrt._close
  8. _SH_DENYRW = 0x10
  9. _rootPath = "F:\\autocad_temp\\"
  10. _originFile = "origin.dwg"
  11. templateIndex = {'md_xs','md_py','md_rc'}
  12. doorName = 'ssdr_800'
  13. correctFilePath = "newfile3\\"
  14. errorFilePath = "errorfile3\\"
  15. # 这个true表示没有文件则打开一个,CAD有弹窗时会打开或者创建失败
  16. # print(acad.doc.Name)
  17. # 判断文件是否打开
  18. def is_open(filename):
  19. if not os.access(filename, os.F_OK):
  20. print("file doesn't exist")
  21. return False # file doesn't exist
  22. h = _sopen(filename, 0, _SH_DENYRW, 0)
  23. if h == 3:
  24. _close(h)
  25. print("file is not opened by anyone else")
  26. return False # file is not opened by anyone else
  27. print("file is already open")
  28. return True # file is already open
  29. # 1、打开dwg文件
  30. def openOriginFile():
  31. # if(is_open(_rootPath+_originFile)):
  32. # return
  33. print("begin to open origin file")
  34. os.popen("acad " + _rootPath + _originFile)
  35. time.sleep(10)
  36. print("origin file opened")
  37. def openFile(fileName):
  38. # if(is_open(_rootPath+_originFile)):
  39. # return
  40. print("begin to open file")
  41. os.popen("acad " + fileName)
  42. time.sleep(20)
  43. print("file opened")
  44. def startAcadAndGetHandler():
  45. print("begin to start acad")
  46. pass
  47. # 关闭autoCad
  48. def closeAcad():
  49. os.system("taskkill /F /IM acad.exe")
  50. # 2、缓存房间、门、模块
  51. def tempStorageModules():
  52. acad = Autocad(create_if_not_exists=False)
  53. acad.prompt("Hello, Autocad from Python\n")
  54. print(acad.doc.Name)
  55. cached_acad = Cached(acad)
  56. return cached_acad
  57. # 3、获取文件中的房间、门、模块参数
  58. def getModuleData():
  59. pass
  60. # 4、获取所有模块的摆法
  61. def getPutArray():
  62. pass
  63. # 5、对每一种摆法,新建dwg文件,
  64. def copyToNewFile():
  65. os.system("")
  66. pass
  67. # 6、摆放房间、门、模块后保存
  68. def saveNewFile():
  69. pass
  70. def copyfile(srcfile, dstfile):
  71. if not os.path.isfile(srcfile):
  72. print("%s not exist!" % (srcfile))
  73. else:
  74. fpath, fname = os.path.split(dstfile) # 分离文件名和路径
  75. if not os.path.exists(fpath):
  76. os.makedirs(fpath) # 创建路径
  77. shutil.copyfile(srcfile, dstfile) # 复制文件
  78. print("copy %s -> %s" % (srcfile, dstfile))
  79. def createNewFileAndMove(newFil):
  80. pass
  81. def myprocess(m, n):
  82. positons = getarray.getAllPosition(m, n)
  83. for positon in positons:
  84. # indexAndPositionArray = getarray.getitemIndexAndPositionArray(positons[positon], m, n)
  85. number = getarray.getitemIndexArrayNumber(positons[positon], m, n)
  86. if (number == 1):
  87. newFileName = _rootPath + correctFilePath + "newfile" + str(positons[positon]) + ".dwg"
  88. if(not os.path.exists(newFileName)):
  89. moveSingleTemplate(newFileName,positons[positon])
  90. # break
  91. else:
  92. moduleIndexs = getarray.getModuleIndex(positons[positon],m,n)
  93. if(GetAllModule.isWorkable(positons[positon])):
  94. for moduleIndex in moduleIndexs:
  95. newFileName = _rootPath + correctFilePath+ "newfile" + str(positons[positon]) + str(moduleIndex) + ".dwg"
  96. if(not os.path.exists(newFileName)):
  97. # copyfile(_rootPath + _originFile, newFileName)
  98. moveMultipleTemplate(newFileName,positons[positon],moduleIndex)
  99. # break
  100. # break
  101. else:
  102. print(positons[positon],"is not workable !!")
  103. # break
  104. GetAllModule.scoreModule()
  105. def moveSingleTemplate(fileName,position):
  106. # openFile(fileName)
  107. GetAllModule.resetConfUsed()
  108. result = GetAllModule.moveBlockReference(position)
  109. time.sleep(1)
  110. acad = Autocad(create_if_not_exists = False)
  111. acad.prompt("Hello, Autocad from Python\n")
  112. if(result == 0):
  113. fileName = fileName.replace(correctFilePath,errorFilePath)
  114. copyfile(_rootPath + _originFile, fileName)
  115. acad.doc.SaveAs(fileName)
  116. time.sleep(1)
  117. getHandler.backToOrigin()
  118. def moveMultipleTemplate(fileName,position,moduleIndex):
  119. GetAllModule.resetConfUsed()
  120. result = GetAllModule.moveBlockReferenceWithNewIndex(position,moduleIndex)
  121. time.sleep(1)
  122. acad = Autocad(create_if_not_exists = False)
  123. acad.prompt("Hello, Autocad from Python\n")
  124. if(result == 0):
  125. fileName = fileName.replace(correctFilePath,errorFilePath)
  126. copyfile(_rootPath + _originFile, fileName)
  127. acad.doc.SaveAs(fileName)
  128. time.sleep(1)
  129. getHandler.backToOrigin()
  130. # openOriginFile()
  131. # tempStorageModules()
  132. # print(is_open(_rootPath+_originFile))
  133. # myprocess(4, 3)
  134. # openFile(r"F:\autocad_temp\newfile023.dwg")
  135. # is_open(r"F:\autocad_temp\origin11.dwg")
  136. # acad = Autocad(create_if_not_exists = False)
  137. # acad.prompt("Hello, Autocad from Python\n")
  138. # print(acad.doc.Name)
  139. # moveMultipleTemplate(r"F:\autocad_temp\newfile2\newfile.dwg",'001',[0,1,2])
  140. # moveMultipleTemplate(r"F:\autocad_temp\newfile2\newfile3.dwg",'020',[2,1,0])
  141. # moveMultipleTemplate(r"F:\autocad_temp\newfile2\newfile4.dwg",'222',[1,2,0])
  142. # moveMultipleTemplate(r"F:\autocad_temp\newfile2\newfile5.dwg",'110',[1,0,2])
  143. # moveMultipleTemplate(r"F:\autocad_temp\newfile2\newfile5.dwg",'222',[1,2,0])
  144. # moveSingleTemplate(r"F:\autocad_temp\newfile1113\newfile2.dwg",'310')