robot 脚本解析
from vcScript import *
comp=getComponent()#先要获得组件也就是拖入布局的模型
robotExecutor=comp.findBehavioursByType("rRobotExecutor")[0]#获得程序里的子程序集合
robotProgram=robotExecutor.Program#获得程序里的子程序集合
mainRoutine=robotProgram.MainRoutine#获得程序里的子程序集合中的Main
subroutines=robotProgram.Routines#获得程序里的子程序集合中所有子程序
for property in mainRoutine.Properties:#获得程序里的子程序Main中的属性,如变量名
print(property.Name)
statements=mainRoutine.Statements
print(statements)
for statement in statements:
if statement.Scopes:
print(statement.Scopes)
def getAllStatementsInRoutine(scope,statements=None):
if not statements:
statements=[]
for statement in scope.Statements:
statements.append(statement)
if statement.Scopes:
for scope in statement.Scopes:
getAllStatementsInRoutine(scope,statements)#递归
return statements
print(len(getAllStatementsInRoutine(mainRoutine)))#mainRoutine中包含scope和statements,python是门神奇的语言
#print(len(getAllStatementsInRoutine(subroutines[1])))获得了不含main的子程序程序语句数量
#遍历了主程序包含的所有子程序
def getAllStatementsInRobotProgram(program):
statements=getAllStatementsInRoutine(program.MainRoutine)
for statement in statements:
subroutine=statement.getProperty("Routine")
if subroutine:
getAllStatementsInRoutine(subroutine.Value,statements)
return statements
print len(getAllStatementsInRobotProgram(robotProgram))
#获取第一个子程序下所有程序片段statement
positions=[]
for statement in subroutines[0].Statements:
print(statement)
#获取第一个子程序下所有程序片段位置
positions=[]
for statement in subroutines[0].Statements:
try:
print(statement.Positions)
except:
continue
#获取所有子程序下的位置
def getAllPositionsInRoutine(scope):
statements=getAllStatementsInRoutine(scope)
positions=[]
for statement in statements:
try:
for position in statement.Positions:
positions.append(position)
except:
pass
return positions
for subroutine in subroutines:
print(getAllPositionsInRoutine(subroutine))