main.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. from pyautocad import Autocad, APoint
  2. from ctypes import cdll
  3. from utils import getarray,GetAllModule
  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. # 这个true表示没有文件则打开一个,CAD有弹窗时会打开或者创建失败
  14. # print(acad.doc.Name)
  15. # 判断文件是否打开
  16. def is_open(filename):
  17. if not os.access(filename, os.F_OK):
  18. return False # file doesn't exist
  19. h = _sopen(filename, 0, _SH_DENYRW, 0)
  20. if h == 3:
  21. _close(h)
  22. return False # file is not opened by anyone else
  23. return True # file is already open
  24. # 1、打开dwg文件
  25. def openOriginFile():
  26. # if(is_open(_rootPath+_originFile)):
  27. # return
  28. print("begin to open origin file")
  29. os.popen("acad " + _rootPath + _originFile)
  30. time.sleep(10)
  31. print("origin file opened")
  32. def openFile(fileName):
  33. if(is_open(_rootPath+_originFile)):
  34. return
  35. print("begin to open file")
  36. os.popen("acad " + fileName)
  37. time.sleep(20)
  38. print("file opened")
  39. # 关闭autoCad
  40. def closeAcad():
  41. os.system("taskkill /F /IM acad.exe")
  42. # 2、缓存房间、门、模块
  43. def tempStorageModules():
  44. acad = Autocad(create_if_not_exists=False)
  45. acad.prompt("Hello, Autocad from Python\n")
  46. print(acad.doc.Name)
  47. cached_acad = Cached(acad)
  48. return cached_acad
  49. # 3、获取文件中的房间、门、模块参数
  50. def getModuleData():
  51. pass
  52. # 4、获取所有模块的摆法
  53. def getPutArray():
  54. pass
  55. # 5、对每一种摆法,新建dwg文件,
  56. def copyToNewFile():
  57. os.system("")
  58. pass
  59. # 6、摆放房间、门、模块后保存
  60. def saveNewFile():
  61. pass
  62. def copyfile(srcfile, dstfile):
  63. if not os.path.isfile(srcfile):
  64. print("%s not exist!" % (srcfile))
  65. else:
  66. fpath, fname = os.path.split(dstfile) # 分离文件名和路径
  67. if not os.path.exists(fpath):
  68. os.makedirs(fpath) # 创建路径
  69. shutil.copyfile(srcfile, dstfile) # 复制文件
  70. print("copy %s -> %s" % (srcfile, dstfile))
  71. def createNewFileAndMove(newFil):
  72. pass
  73. def myprocess(m, n):
  74. positons = getarray.getAllPosition(m, n)
  75. for positon in positons:
  76. indexAndPositionArray = getarray.getitemIndexAndPositionArray(positons[positon], m, n)
  77. number = getarray.getitemIndexArrayNumber(positons[positon], m, n)
  78. if (number == 1):
  79. newFileName = _rootPath + "newfile" + str(positons[positon]) + ".dwg"
  80. if(not os.path.exists(newFileName)):
  81. copyfile(_rootPath + _originFile, newFileName)
  82. moveSingleTemplate(newFileName,positons[positon])
  83. # break
  84. # else:
  85. # for index in range(number):
  86. # newFileName = _rootPath + "newfile" + str(positons[positon]) + str(index + 1) + ".dwg"
  87. # copyfile(_rootPath + _originFile, newFileName)
  88. # break
  89. def moveSingleTemplate(fileName,position):
  90. openFile(fileName)
  91. GetAllModule.moveBlockReference(position)
  92. time.sleep(5)
  93. acad = Autocad(create_if_not_exists = False)
  94. acad.prompt("Hello, Autocad from Python\n")
  95. acad.doc.SaveAs(fileName)
  96. acad.doc.Shutdown()
  97. # closeAcad()
  98. # openOriginFile()
  99. # tempStorageModules()
  100. # print(is_open(_rootPath+_originFile))
  101. myprocess(4, 3)
  102. # openFile(r"F:\autocad_temp\newfile023.dwg")