To run a script, there are some prerequisites:
Now you can create and run your first script.
To get started, we can try to run a "Hello world" program:
def run(self): print "Hello world!" |
Result:
Hello world! ===={ Script Execution Terminated }========================== |
It is important to know that all the code you write must be in a function with a parameter self (the function can be named as you want). The value of self represents a reference to the object where this script is run (near the root object in view all).
Create a simple architecture with flow connecting functions allocated to systems.
import pyark def CreateTypes (self): # creating base type called System above current Type #in this examples current type = top level project current_object = self current_type=pyark.GetArkMatrixType(current_object.GetArkType()) current_type.AddRule("System") system_type=pyark.GetArkMatrixType("System")#Same as system_type=current_type.AddRule("System") #Creating Type "Function" above type "System" function_type=system_type.AddRule("Function") #Creating type "Flow" flow exchanged by instance of "Functions" function_type.AddRule("Flow",pyark.ARK_RULE_FLOW) |
import pyark def CreateTypes (self): #Get Reference of rule "System" and "Function" previously created system_type=pyark.GetArkMatrixType("System") function_type=pyark.GetArkMatrixType("Function") #Adding attributes to "System" system_type.AddAttribute("Description",pyark.ARK_ATTRIB_MEMO) system_type.AddAttribute("Date",pyark.ARK_ATTRIB_DATE) #Checking attributes have been added for attribute in system_type.GetAttributeList(): print attribute.GetName() #Description and Date have now been created, they can be used and added to any types # Adding them to type "Function" for attribute in system_type.GetAttributeList(): function_type.AddAttribute(attribute) function_type.AddAttribute(attribute) |
Result:
Date Description ===={ Script Execution Terminated }========================== |
We can see that the attributes have been created by a script.
import pyark def CreateTypes (self): #Get Reference of rule "System" and "Function" previously created system_type=pyark.GetArkMatrixType("System") function_type=pyark.GetArkMatrixType("Function") #Getting properties of "System" print system_type.GetProperty(pyark.ARK_ATYPE_CLRLINE) print system_type.GetProperty(pyark.ARK_ATYPE_OBJSHAPE) #Changing properties of "System" system_type.SetProperty(pyark.ARK_ATYPE_OBJSHAPE,pyark.ARK_OBJSHAPE_RECT_RE) system_type.SetProperty(pyark.ARK_ATYPE_CLRLINE,(0,255,0)) system_type.FlashProperties() #Write definitly properties before this line #Changing properties of "Function" function_type.SetProperty(pyark.ARK_ATYPE_CLRFILL,(255,0,255)) function_type.FlashProperties() #Write definitly properties before this line #Checking properties of "System" print system_type.GetProperty(pyark.ARK_ATYPE_CLRLINE) print system_type.GetProperty(pyark.ARK_ATYPE_OBJSHAPE) |
Result:
(0, 0, 0) Rectangle (0, 255, 0) Rounded Rectangle Description ===={ Script Execution Terminated }========================== |
The roperties have been changed.
import pyark def CreateTypes (self): #Getting the root tree rootTree=pyark.GetRootTreeViewObj() rootTreeChildren=rootTree.GetChildList() #Get actual filters for view in rootTreeChildren: print view.GetName() rootTree.NewArkTreeViewObj("System and Functions",True) #Create a new filter and check all rules rootTree.NewArkTreeViewObj("Functions only",False) #Create a new filter but any rules are checked #Check new filters have been created for view in rootTree.GetChildList(): print view.GetName() |
Result:
all Functions only all System and Functions ===={ Script Execution Terminated }========================== |
In the filter Functions, no rules are checked, so we need check the rule Function in this filter.
import pyark def CreateTypes (self): #Get Reference of rule "System" and "Function" previously created system_type=pyark.GetArkMatrixType("System") function_type=pyark.GetArkMatrixType("Function") #Get Reference of filter "Functions only" functionsOnlyFilter=pyark.GetArkTreeViewObj("Functions only") #Check the rule "Function" functionsOnlyFilter.CheckRule([pyark.GetArkMatrixType(self.GetArkType()),system_type,function_type],True) |
We will try to add some data to check if our meta-model works fine.
import pyark def CreateTypes (self): #Get root in view "System and Functions" systemAndFunctions=pyark.GetRoot("System and Functions") #Create a system and set attributes values system=systemAndFunctions.AddChildObject("System", "MyOwnSystem") system.GetAttribute("Date").SetValue("21/12/2012") system.GetAttribute("Description").SetValue("This is an example to learn python script") #Create two functions under system f1=system.AddChildObject("Function", "Learn python script in arKItect") f2=system.AddChildObject("Function", "Develop my own script") #Create a flow to link both functions f1.AddChildObject("Flow","ToDo","output") f2.AddChildObject("Flow","ToDo","input") |
You can write all previous scripts in only one script:
import pyark def CreateTypes (self): # creating base type called System above current Type #in this examples current type = top level project current_object = self current_type=pyark.GetArkMatrixType(current_object.GetArkType()) system_type=current_type.AddRule("System") #Creating Type "Function" above type "System" function_type=system_type.AddRule("Function") #Creating type "Flow" flow exchanged by instance of "Functions" function_type.AddRule("Flow",pyark.ARK_RULE_FLOW) #Adding attributes to "System" system_type.AddAttribute("Description",pyark.ARK_ATTRIB_MEMO) system_type.AddAttribute("Date",pyark.ARK_ATTRIB_DATE) #Description and Date have now been created, they can be used and added to any types # Adding them to type "Function" for attribute in system_type.GetAttributeList(): function_type.AddAttribute(attribute) function_type.AddAttribute(attribute) #Changing properties of "System" system_type.SetProperty(pyark.ARK_ATYPE_OBJSHAPE,pyark.ARK_OBJSHAPE_RECT_RE) system_type.SetProperty(pyark.ARK_ATYPE_CLRLINE,(0,255,0)) system_type.FlashProperties() #Write definitly properties before this line #Changing properties of "Function" function_type.SetProperty(pyark.ARK_ATYPE_CLRFILL,(255,0,255)) function_type.FlashProperties() #Getting the root tree rootTree=pyark.GetRootTreeViewObj() rootTree.NewArkTreeViewObj("System and Functions",True) #Create a new filter and check all rules functionsOnlyFilter=rootTree.NewArkTreeViewObj("Functions only",False) #Create a new filter but any rules are checked #Check the rule "Function" functionsOnlyFilter.CheckRule([current_type,system_type,function_type],True) #Get root in view "System and Functions" systemAndFunctions=pyark.GetRoot("System and Functions") #Create a system and set attributes values system=systemAndFunctions.AddChildObject("System", "MyOwnSystem") system.GetAttribute("Date").SetValue("21/12/2012") system.GetAttribute("Description").SetValue("This is an example to learn python script") #Create two functions under system f1=system.AddChildObject("Function", "Learn python script in arKItect") f2=system.AddChildObject("Function", "Develop my own script") #Create a flow to link both functions f1.AddChildObject("Flow","ToDo","output") f2.AddChildObject("Flow","ToDo","input") |