# 参考file:///D:/ProgramFiles/MSC.Software/Adams/2017_2/help/wwhelp/wwhimpl/common/html/wwhelp.htm#href=adams_view/command_server.html&single=true
# 参考https://blog.csdn.net/weixin_33698043/article/details/94562370
'''
先在Adams中打开Command_Server(tool→Command Navigator→Command_Server)。
需要注意的是,指令如果没有发送成功,会造成通讯崩溃。
Command Server - Language
1. Commands:
command strings beginning with the string "cmd" are assumed to be valid Adams View
Example: "cmd file command read file='load_model.cmd'"
Returns: “cmd: 0” (on success)
“cmd: 1” (error detected in View command)
2. Queries:
command strings beginning with the string "query" are assumed to be valid Adams View Expressions;
the Command Server attempts to evaluate these expressions and return the result to the client.
Example: "query part_9.mass"
3. Binary mode: on or off.
Commands beginning with the string "binary" toggle binary mode for queries on and off.
See section Detailed Query Response Handling for binary vs text queries.
Example: "binary on"
'''
###################################################################
####Issuing Commands - Python Example
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("localhost", 5002))
# formulate valid Adams View command language that starts with the string "cmd"
cmd = 'cmd acar toolkit message message = ("TCP Connected")'
client_socket.send(cmd.encode())
# receives feedback from the server. The server responds with the string "cmd: 0" for successful command processing
response = client_socket.recv(1024)
#print(response)
#################################################################
#####Issuing Multiple Commands - Python Example
import socket
import time
cmds = list()
mname = 'test'
cmds.append('acar interface switch template_builder')
cmds.append('acar files template close template_name = ._test')
cmds.append('acar files template new template_name = '+mname)
cmds.append('defaults model model=._'+mname)
cmds.append('acar template_builder hardpoint create \
hardpoint_name=new_1 \
location=100,-100,0 \
type=left ')
for cmdi in cmds:
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
start_time = time.time()
dt = time.time() - start_time
while dt < 60: # wait for a new server connection:
dt = time.time() - start_time
try:
client_socket.connect(("localhost", 5002))
break
except socket.error:
pass
print(cmdi)
client_socket.send(('cmd '+cmdi).encode())
response = client_socket.recv(1024)
#print(response.decode())
########################################################################
####Issuing Queries
import socket
# create a socket & connect to the proper port/host
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("localhost", 5002))
# queries must always start with the string "query" followed by an Adams View expression
query = "query hpl_new_1.location"
client_socket.send(query.encode())
# server replies with a description of the data
query_description = client_socket.recv(1024)
# server waits for an "OK" command before sending the actual data
client_socket.send("OK".encode())
query_data = client_socket.recv(1024)# accepts the actual server data
#print(query_description)
description_list = query_description.decode().split(':')
data_type = description_list[1]
data_length = int(description_list[2])
data_bytes = int(description_list[3])
print("Query returned {} values of data type {}".format(data_length, data_type))
print("Query data as a string is: {}".format(query_data.decode()))