stkcom.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. from comtypes.gen import STKUtil, STKObjects
  2. from comtypes.client import CreateObject, GetActiveObject
  3. import json
  4. import datetime
  5. import pythoncom
  6. # get_data_from_json:解析想定文件
  7. def get_data_from_json(xdjson):
  8. begintime = xdjson["starttime"]
  9. endtime = xdjson["endtime"]
  10. timestep = xdjson["steptime"]
  11. id = xdjson["id"]
  12. name = xdjson["xdname"]
  13. redunit = json.loads(xdjson["redunit"])
  14. blueunit = json.loads(xdjson["blueunit"])
  15. satellite = json.loads(xdjson["satellite"])
  16. center = json.loads(xdjson["center"])
  17. return begintime, endtime, timestep, id, name, redunit, blueunit, satellite, center
  18. # datetime_transform:转换时间格式
  19. def datetime_transform(begintime, endtime):
  20. date_time1 = datetime.datetime.strptime(begintime, "%Y-%m-%d %H:%M:%S")
  21. date_time2 = datetime.datetime.strptime(endtime, "%Y-%m-%d %H:%M:%S")
  22. # 去掉日期中的前导0
  23. formatted_day1 = date_time1.strftime("%d").lstrip("0")
  24. # 去掉日期中的前导0
  25. formatted_day2 = date_time2.strftime("%d").lstrip("0")
  26. # 计算两个日期之间的时间差
  27. time_difference = date_time2 - date_time1
  28. microseconds = ".00"
  29. starttime = date_time1.strftime(f"{formatted_day1} %B %Y %H:%M:%S")
  30. stoptime = date_time2.strftime(f"{formatted_day2} %B %Y %H:%M:%S")
  31. starttime = starttime + microseconds
  32. stoptime = stoptime + microseconds
  33. return starttime, stoptime, time_difference
  34. # activeSTK:启动STK,返回句柄
  35. def activeSTK():
  36. pythoncom.CoInitialize()
  37. # uiApplication=CreateObject("STK11.Application")
  38. uiApplication = GetActiveObject("STK11.Application")
  39. # uiApplication.Visible=False
  40. # uiApplication.Visible=True
  41. uiApplication.UserControl = True
  42. root = uiApplication.Personality2
  43. return root
  44. # createScenario:创建场景
  45. def createScenario(root, xdname, starttime, stoptime, duration):
  46. # if root.CurrentScenario is not None:
  47. # # If there is an existing scenario, close all scenarios
  48. # root.CloseScenario()
  49. # print("Closed existing scenarios.")
  50. # root.NewScenario(xdname)
  51. scenario = root.CurrentScenario
  52. # 2. Set the analytical time period.
  53. scenario2 = scenario.QueryInterface(STKObjects.IAgScenario)
  54. # 计算相差的小时数
  55. hours_difference = int(duration.total_seconds() / 3600)
  56. hours_number = "+" + str(hours_difference) + "hr"
  57. # scenario2.SetTimePeriod('Today','+24hr')
  58. scenario2.SetTimePeriod(starttime, hours_number)
  59. # scenario2.StartTime = begintime
  60. # scenario2.StopTime = endtime
  61. # 3. Reset the animation time.
  62. root.Rewind()
  63. print("创建场景成功")
  64. return 1
  65. # exportMovementFile: 导出运动轨迹
  66. def exportMovementFile(root, redunit, satelliteJson, starttime, stoptime, steptime):
  67. scenario = root.CurrentScenario
  68. # 依次创建弹道导弹模型
  69. for i in range(len(redunit)):
  70. missile = scenario.Children.New(STKObjects.eMissile, redunit[i].get("name"))
  71. missile2 = missile.QueryInterface(STKObjects.IAgMissile)
  72. command = (
  73. "Missile */Missile/"
  74. + redunit[i].get("name")
  75. + ' Trajectory "'
  76. + starttime
  77. + '" 10.0 LnLatGeoD '
  78. + str(
  79. redunit[i]
  80. .get("component_movementjson")
  81. .get("properties")
  82. .get("launch_lat")
  83. )
  84. + " "
  85. + str(
  86. redunit[i]
  87. .get("component_movementjson")
  88. .get("properties")
  89. .get("launch_lon")
  90. )
  91. + " "
  92. + str(
  93. redunit[i]
  94. .get("component_movementjson")
  95. .get("properties")
  96. .get("launch_h")
  97. )
  98. + " ApogeeAlt "
  99. + str(
  100. redunit[i]
  101. .get("component_movementjson")
  102. .get("properties")
  103. .get("apogee_height")
  104. )
  105. + " ImLatGeoD "
  106. + str(
  107. redunit[i]
  108. .get("component_movementjson")
  109. .get("properties")
  110. .get("target_lat")
  111. )
  112. + " "
  113. + str(
  114. redunit[i]
  115. .get("component_movementjson")
  116. .get("properties")
  117. .get("target_lon")
  118. )
  119. + " "
  120. + str(
  121. redunit[i]
  122. .get("component_movementjson")
  123. .get("properties")
  124. .get("target_h")
  125. )
  126. )
  127. root.ExecuteCommand(command)
  128. # #依次导出弹道导弹模型轨迹文件
  129. for i in range(len(redunit)):
  130. root.ExecuteCommand(
  131. "ReportCreate */Missile/"
  132. + redunit[i].get("name")
  133. + ' Type Save Style "Installed Styles/Fixed Position Velocity" File "C:\\fire\\simulation\\'
  134. + redunit[i].get("name")
  135. + '.csv" TimePeriod "'
  136. + starttime
  137. + '" "'
  138. + stoptime
  139. + '" TimeStep 10.0'
  140. )
  141. # 依次创建预警卫星模型
  142. for i in range(len(satelliteJson)):
  143. satellite = scenario.Children.New(
  144. STKObjects.eSatellite, satelliteJson[i].get("name")
  145. )
  146. satellite2 = satellite.QueryInterface(STKObjects.IAgSatellite)
  147. satname = satelliteJson[i].get("name")
  148. satobj = satelliteJson[i].get(satname)
  149. command = (
  150. "SetState */Satellite/"
  151. + satname
  152. + ' TLE "'
  153. + satobj.get("properties").get("TLE_LINE1")
  154. + '" "'
  155. + satobj.get("properties").get("TLE_LINE2")
  156. + '" TimePeriod "'
  157. + starttime
  158. + '" "'
  159. + stoptime
  160. + '" TimeStep 10.0'
  161. )
  162. root.ExecuteCommand(command)
  163. # 依次导出预警卫星模型轨迹文件
  164. for i in range(len(satelliteJson)):
  165. root.ExecuteCommand(
  166. "ReportCreate */Satellite/"
  167. + satelliteJson[i].get("name")
  168. + ' Type Save Style "Installed Styles/Fixed Position Velocity" File "C:\\fire\\simulation\\'
  169. + satelliteJson[i].get("name")
  170. + '.csv" TimePeriod "'
  171. + starttime
  172. + '" "'
  173. + stoptime
  174. + '" TimeStep 10.0'
  175. )
  176. print("导出文件成功")