123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- from comtypes.gen import STKUtil, STKObjects
- from comtypes.client import CreateObject, GetActiveObject
- import json
- import datetime
- import pythoncom
- # get_data_from_json:解析想定文件
- def get_data_from_json(xdjson):
- begintime = xdjson["starttime"]
- endtime = xdjson["endtime"]
- timestep = xdjson["steptime"]
- id = xdjson["id"]
- name = xdjson["xdname"]
- redunit = json.loads(xdjson["redunit"])
- blueunit = json.loads(xdjson["blueunit"])
- satellite = json.loads(xdjson["satellite"])
- center = json.loads(xdjson["center"])
- return begintime, endtime, timestep, id, name, redunit, blueunit, satellite, center
- # datetime_transform:转换时间格式
- def datetime_transform(begintime, endtime):
- date_time1 = datetime.datetime.strptime(begintime, "%Y-%m-%d %H:%M:%S")
- date_time2 = datetime.datetime.strptime(endtime, "%Y-%m-%d %H:%M:%S")
- # 去掉日期中的前导0
- formatted_day1 = date_time1.strftime("%d").lstrip("0")
- # 去掉日期中的前导0
- formatted_day2 = date_time2.strftime("%d").lstrip("0")
- # 计算两个日期之间的时间差
- time_difference = date_time2 - date_time1
- microseconds = ".00"
- starttime = date_time1.strftime(f"{formatted_day1} %B %Y %H:%M:%S")
- stoptime = date_time2.strftime(f"{formatted_day2} %B %Y %H:%M:%S")
- starttime = starttime + microseconds
- stoptime = stoptime + microseconds
- return starttime, stoptime, time_difference
- # activeSTK:启动STK,返回句柄
- def activeSTK():
- pythoncom.CoInitialize()
- # uiApplication=CreateObject("STK11.Application")
- uiApplication = GetActiveObject("STK11.Application")
- # uiApplication.Visible=False
- # uiApplication.Visible=True
- uiApplication.UserControl = True
- root = uiApplication.Personality2
- return root
- # createScenario:创建场景
- def createScenario(root, xdname, starttime, stoptime, duration):
- # if root.CurrentScenario is not None:
- # # If there is an existing scenario, close all scenarios
- # root.CloseScenario()
- # print("Closed existing scenarios.")
- # root.NewScenario(xdname)
- scenario = root.CurrentScenario
- # 2. Set the analytical time period.
- scenario2 = scenario.QueryInterface(STKObjects.IAgScenario)
- # 计算相差的小时数
- hours_difference = int(duration.total_seconds() / 3600)
- hours_number = "+" + str(hours_difference) + "hr"
- # scenario2.SetTimePeriod('Today','+24hr')
- scenario2.SetTimePeriod(starttime, hours_number)
- # scenario2.StartTime = begintime
- # scenario2.StopTime = endtime
- # 3. Reset the animation time.
- root.Rewind()
- print("创建场景成功")
- return 1
- # exportMovementFile: 导出运动轨迹
- def exportMovementFile(root, redunit, satelliteJson, starttime, stoptime, steptime):
- scenario = root.CurrentScenario
- # 依次创建弹道导弹模型
- for i in range(len(redunit)):
- missile = scenario.Children.New(STKObjects.eMissile, redunit[i].get("name"))
- missile2 = missile.QueryInterface(STKObjects.IAgMissile)
- command = (
- "Missile */Missile/"
- + redunit[i].get("name")
- + ' Trajectory "'
- + starttime
- + '" 10.0 LnLatGeoD '
- + str(
- redunit[i]
- .get("component_movementjson")
- .get("properties")
- .get("launch_lat")
- )
- + " "
- + str(
- redunit[i]
- .get("component_movementjson")
- .get("properties")
- .get("launch_lon")
- )
- + " "
- + str(
- redunit[i]
- .get("component_movementjson")
- .get("properties")
- .get("launch_h")
- )
- + " ApogeeAlt "
- + str(
- redunit[i]
- .get("component_movementjson")
- .get("properties")
- .get("apogee_height")
- )
- + " ImLatGeoD "
- + str(
- redunit[i]
- .get("component_movementjson")
- .get("properties")
- .get("target_lat")
- )
- + " "
- + str(
- redunit[i]
- .get("component_movementjson")
- .get("properties")
- .get("target_lon")
- )
- + " "
- + str(
- redunit[i]
- .get("component_movementjson")
- .get("properties")
- .get("target_h")
- )
- )
- root.ExecuteCommand(command)
- # #依次导出弹道导弹模型轨迹文件
- for i in range(len(redunit)):
- root.ExecuteCommand(
- "ReportCreate */Missile/"
- + redunit[i].get("name")
- + ' Type Save Style "Installed Styles/Fixed Position Velocity" File "C:\\fire\\simulation\\'
- + redunit[i].get("name")
- + '.csv" TimePeriod "'
- + starttime
- + '" "'
- + stoptime
- + '" TimeStep 10.0'
- )
- # 依次创建预警卫星模型
- for i in range(len(satelliteJson)):
- satellite = scenario.Children.New(
- STKObjects.eSatellite, satelliteJson[i].get("name")
- )
- satellite2 = satellite.QueryInterface(STKObjects.IAgSatellite)
- satname = satelliteJson[i].get("name")
- satobj = satelliteJson[i].get(satname)
- command = (
- "SetState */Satellite/"
- + satname
- + ' TLE "'
- + satobj.get("properties").get("TLE_LINE1")
- + '" "'
- + satobj.get("properties").get("TLE_LINE2")
- + '" TimePeriod "'
- + starttime
- + '" "'
- + stoptime
- + '" TimeStep 10.0'
- )
- root.ExecuteCommand(command)
- # 依次导出预警卫星模型轨迹文件
- for i in range(len(satelliteJson)):
- root.ExecuteCommand(
- "ReportCreate */Satellite/"
- + satelliteJson[i].get("name")
- + ' Type Save Style "Installed Styles/Fixed Position Velocity" File "C:\\fire\\simulation\\'
- + satelliteJson[i].get("name")
- + '.csv" TimePeriod "'
- + starttime
- + '" "'
- + stoptime
- + '" TimeStep 10.0'
- )
- print("导出文件成功")
|