main.py 3.7 KB

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