plugins/svgui/svgui.py
changeset 201 520d2416ff4d
parent 198 cf99e75b6dda
child 203 cb9901076a21
equal deleted inserted replaced
200:009fc5850157 201:520d2416ff4d
   189         svgfilepath = self.GetSVGFilePath()
   189         svgfilepath = self.GetSVGFilePath()
   190         xmlfilepath = self.GetFilePath()
   190         xmlfilepath = self.GetFilePath()
   191         shutil.copy(svgfilepath, buildpath)
   191         shutil.copy(svgfilepath, buildpath)
   192         shutil.copy(xmlfilepath, buildpath)
   192         shutil.copy(xmlfilepath, buildpath)
   193         
   193         
   194         generator = _SVGUICGenerator(self.GetElementsByType(), 
   194         generator = _SVGUICGenerator(self, self.GetElementsByType(), 
   195                                      os.path.split(self.GetSVGFilePath())[1], 
   195                                      os.path.split(self.GetSVGFilePath())[1], 
   196                                      os.path.split(self.GetFilePath())[1], 
   196                                      os.path.split(self.GetFilePath())[1], 
   197                                      self.GetCurrentLocation())
   197                                      self.GetCurrentLocation())
   198         generator.GenerateProgram(window_size, buildpath, progname)
   198         generator.GenerateProgram(window_size, buildpath, progname)
   199         Gen_C_file = os.path.join(buildpath, progname+".cpp" )
   199         Gen_C_file = os.path.join(buildpath, progname+".cpp" )
   213             libs = result.strip() + " -lwxsvg"
   213             libs = result.strip() + " -lwxsvg"
   214         
   214         
   215         return [(Gen_C_file, cxx_flags)],libs,True
   215         return [(Gen_C_file, cxx_flags)],libs,True
   216     
   216     
   217     def BlockTypesFactory(self):
   217     def BlockTypesFactory(self):
       
   218         
       
   219         SVGUIBlock_Types = []
       
   220         
       
   221         def GetSVGUIBlockType(type):
       
   222             for category in SVGUIBlock_Types:
       
   223                 for blocktype in category["list"]:
       
   224                     if blocktype["name"] == type:
       
   225                         return blocktype
       
   226         setattr(self, "GetSVGUIBlockType", GetSVGUIBlockType)
       
   227         
   218         def generate_svgui_block(generator, block, body, link, order=False):
   228         def generate_svgui_block(generator, block, body, link, order=False):
   219             name = block.getinstanceName()
   229             name = block.getinstanceName()
   220             block_id = self.GetElementIdFromName(name)
   230             block_id = self.GetElementIdFromName(name)
   221             if block_id == None:
   231             if block_id == None:
   222                 raise ValueError, "No corresponding block found"
   232                 raise ValueError, "No corresponding block found"
   223             type = block.gettypeName()
   233             type = block.gettypeName()
   224             block_infos = GetBlockType(type)
   234             block_infos = GetSVGUIBlockType(type)
   225             current_location = ".".join(map(str, self.GetCurrentLocation()))
   235             current_location = ".".join(map(str, self.GetCurrentLocation()))
   226             if not generator.ComputedBlocks.get(block, False) and not order:
   236             if not generator.ComputedBlocks.get(block, False) and not order:
   227                 generator.ComputedBlocks[block] = True
   237                 generator.ComputedBlocks[block] = True
   228                 for num, variable in enumerate(block.inputVariables.getvariable()):
   238                 for num, variable in enumerate(block.inputVariables.getvariable()):
   229                     connections = variable.connectionPointIn.getconnections()
   239                     connections = variable.connectionPointIn.getconnections()
       
   240                     input_info = (generator.TagName, "block", block.getlocalId(), "input", num)
   230                     if connections and len(connections) == 1:
   241                     if connections and len(connections) == 1:
   231                         parameter = "%sQ%s%s.%d.%d"%("%", TYPECONVERSION[block_infos["inputs"][num][1]], current_location, block_id, num+1)
   242                         parameter = "%sQ%s%s.%d.%d"%("%", TYPECONVERSION[block_infos["inputs"][num][1]], current_location, block_id, num+1)
   232                         value = generator.ComputeFBDExpression(body, connections[0])
   243                         value = generator.ComputeFBDExpression(body, connections[0])
   233                         generator.Program += ("  %s := %s;\n"%(parameter, generator.ExtractModifier(variable, value)))
   244                         generator.Program += [(generator.CurrentIndent, ()),
       
   245                                               (parameter, input_info),
       
   246                                               (" := ", ())]
       
   247                         generator.Program += generator.ExtractModifier(variable, value, input_info)
       
   248                         generator.Program += [(";\n", ())]
   234             if link:
   249             if link:
   235                 connectionPoint = link.getposition()[-1]
   250                 connectionPoint = link.getposition()[-1]
   236                 for num, variable in enumerate(block.outputVariables.getvariable()):
   251                 for num, variable in enumerate(block.outputVariables.getvariable()):
   237                     blockPointx, blockPointy = variable.connectionPointOut.getrelPositionXY()
   252                     blockPointx, blockPointy = variable.connectionPointOut.getrelPositionXY()
       
   253                     output_info = (generator.TagName, "block", block.getlocalId(), "output", num)
   238                     if block.getx() + blockPointx == connectionPoint.getx() and block.gety() + blockPointy == connectionPoint.gety():
   254                     if block.getx() + blockPointx == connectionPoint.getx() and block.gety() + blockPointy == connectionPoint.gety():
   239                         return "%sI%s%s.%d.%d"%("%", TYPECONVERSION[block_infos["outputs"][num][1]], current_location, block_id, num+1)
   255                         return [("%sI%s%s.%d.%d"%("%", TYPECONVERSION[block_infos["outputs"][num][1]], current_location, block_id, num+1), output_info)]
   240                 raise ValueError, "No output variable found"
   256                 raise ValueError, "No output variable found"
   241             else:
   257             else:
   242                 return None
   258                 return None
   243 
   259 
   244         def initialise_block(type, name, block = None):
   260         def initialise_block(type, name, block = None):
   245             block_id = self.GetElementIdFromName(name)
   261             block_id = self.GetElementIdFromName(name)
   246             if block_id == None:
   262             if block_id == None:
   247                 raise ValueError, "No corresponding block found"
   263                 raise ValueError, "No corresponding block found"
   248             block_infos = GetBlockType(type)
   264             block_infos = GetSVGUIBlockType(type)
   249             current_location = ".".join(map(str, self.GetCurrentLocation()))
   265             current_location = ".".join(map(str, self.GetCurrentLocation()))
   250             variables = []
   266             variables = []
   251             if block is not None:
   267             if block is not None:
   252                 input_variables = block.inputVariables.getvariable()
   268                 input_variables = block.inputVariables.getvariable()
   253                 output_variables = block.outputVariables.getvariable()
   269                 output_variables = block.outputVariables.getvariable()
   261                     variables.append((input_type, None, "%sQ%s%s.%d.%d"%("%", TYPECONVERSION[input_type], current_location, block_id, num+1), None))
   277                     variables.append((input_type, None, "%sQ%s%s.%d.%d"%("%", TYPECONVERSION[input_type], current_location, block_id, num+1), None))
   262             for num, (output_name, output_type, output_modifier) in enumerate(block_infos["outputs"]):
   278             for num, (output_name, output_type, output_modifier) in enumerate(block_infos["outputs"]):
   263                 variables.append((output_type, None, "%sI%s%s.%d.%d"%("%", TYPECONVERSION[input_type], current_location, block_id, num+1), None))
   279                 variables.append((output_type, None, "%sI%s%s.%d.%d"%("%", TYPECONVERSION[input_type], current_location, block_id, num+1), None))
   264             return variables
   280             return variables
   265 
   281 
   266         return [{"name" : "SVGUI function blocks", "list" :
   282         SVGUIBlock_Types.extend([{"name" : "SVGUI function blocks", "list" :
   267                 [{"name" : "Container", "type" : "functionBlock", "extensible" : False, 
   283                 [{"name" : "Container", "type" : "functionBlock", "extensible" : False, 
   268                     "inputs" : [("Show","BOOL","none"),("Enable","BOOL","none")], 
   284                     "inputs" : [("Show","BOOL","none"),("Enable","BOOL","none")], 
   269                     "outputs" : [],
   285                     "outputs" : [],
   270                     "comment" : "SVGUI Container",
   286                     "comment" : "SVGUI Container",
   271                     "generate" : generate_svgui_block, "initialise" : initialise_block},
   287                     "generate" : generate_svgui_block, "initialise" : initialise_block},
   298                     "inputs" : [("Show","BOOL","none"),("Enable","BOOL","none"),("SetX","REAL","none"),("SetY","REAL","none"),("SetXScale","REAL","none"),("SetYScale","REAL","none"),("SetAngle","REAL","none")], 
   314                     "inputs" : [("Show","BOOL","none"),("Enable","BOOL","none"),("SetX","REAL","none"),("SetY","REAL","none"),("SetXScale","REAL","none"),("SetYScale","REAL","none"),("SetAngle","REAL","none")], 
   299                     "outputs" : [("X","REAL","none"),("Y","REAL","none")],
   315                     "outputs" : [("X","REAL","none"),("Y","REAL","none")],
   300                     "comment" : "SVGUI Transform",
   316                     "comment" : "SVGUI Transform",
   301                     "generate" : generate_svgui_block, "initialise" : initialise_block},
   317                     "generate" : generate_svgui_block, "initialise" : initialise_block},
   302                ]}
   318                ]}
   303         ]
   319         ])
   304 
   320 
       
   321         return SVGUIBlock_Types
   305 
   322 
   306 
   323 
   307 class _SVGUICGenerator(SVGUICGenerator):
   324 class _SVGUICGenerator(SVGUICGenerator):
   308 
   325 
   309     def __init__(self, elements, svgfile, xmlfile, current_location):
   326     def __init__(self, controler, elements, svgfile, xmlfile, current_location):
   310         SVGUICGenerator.__init__(self, elements, svgfile, xmlfile)
   327         SVGUICGenerator.__init__(self, elements, svgfile, xmlfile)
   311         
   328         
   312         self.CurrentLocation = current_location
   329         self.CurrentLocation = current_location
       
   330         self.Controler = controler
   313 
   331 
   314     def GenerateProgramHeadersPublicVars(self):
   332     def GenerateProgramHeadersPublicVars(self):
   315         text = """    void OnPlcOutEvent(wxEvent& event);
   333         text = """    void OnPlcOutEvent(wxEvent& event);
   316 
   334 
   317     void Retrieve();
   335     void Retrieve();
   328             text += "STATE_TYPE in_state_%d;\n"%element.getid()
   346             text += "STATE_TYPE in_state_%d;\n"%element.getid()
   329         text +="\n"
   347         text +="\n"
   330         current_location = "_".join(map(str, self.CurrentLocation))
   348         current_location = "_".join(map(str, self.CurrentLocation))
   331         #Declaration des variables
   349         #Declaration des variables
   332         for element in self.Elements:
   350         for element in self.Elements:
   333             block_infos = GetBlockType(SVGUIFB_Types[GetElementType(element)])
   351             block_infos = self.Controler.GetSVGUIBlockType(SVGUIFB_Types[GetElementType(element)])
   334             block_id = element.getid()
   352             block_id = element.getid()
   335             for i, input in enumerate(block_infos["inputs"]):
   353             for i, input in enumerate(block_infos["inputs"]):
   336                 element_c_type = CTYPECONVERSION[input[1]]
   354                 element_c_type = CTYPECONVERSION[input[1]]
   337                 variable = "__Q%s%s_%d_%d"%(TYPECONVERSION[input[1]], current_location, block_id, i + 1)
   355                 variable = "__Q%s%s_%d_%d"%(TYPECONVERSION[input[1]], current_location, block_id, i + 1)
   338                 text += "%s beremiz%s;\n"%(element_c_type, variable)
   356                 text += "%s beremiz%s;\n"%(element_c_type, variable)
   656         
   674         
   657         text = "void Program::Retrieve()\n{\n"
   675         text = "void Program::Retrieve()\n{\n"
   658         for element in self.Elements:
   676         for element in self.Elements:
   659             element_type = GetElementType(element)
   677             element_type = GetElementType(element)
   660             texts = {"location" : current_location, "id" : element.getid()}
   678             texts = {"location" : current_location, "id" : element.getid()}
   661             block_infos = GetBlockType(SVGUIFB_Types[GetElementType(element)])
   679             block_infos = self.Controler.GetSVGUIBlockType(SVGUIFB_Types[GetElementType(element)])
   662             if len(block_infos["outputs"]) > 0:
   680             if len(block_infos["outputs"]) > 0:
   663                 text += """  if (COMPARE_AND_SWAP_VAL(&in_state_%(id)d, CHANGED, PLC_BUSY) == CHANGED) {
   681                 text += """  if (COMPARE_AND_SWAP_VAL(&in_state_%(id)d, CHANGED, PLC_BUSY) == CHANGED) {
   664 """%texts
   682 """%texts
   665                 for i, output in enumerate(block_infos["outputs"]):
   683                 for i, output in enumerate(block_infos["outputs"]):
   666                     texts["type"] = TYPECONVERSION[output[1]]
   684                     texts["type"] = TYPECONVERSION[output[1]]
   676 
   694 
   677         text += "void Program::Publish()\n{\n  STATE_TYPE new_state;\n\n"
   695         text += "void Program::Publish()\n{\n  STATE_TYPE new_state;\n\n"
   678         for element in self.Elements:
   696         for element in self.Elements:
   679             element_type = GetElementType(element)
   697             element_type = GetElementType(element)
   680             texts = {"location" : current_location, "id" : element.getid()}
   698             texts = {"location" : current_location, "id" : element.getid()}
   681             block_infos = GetBlockType(SVGUIFB_Types[GetElementType(element)])
   699             block_infos = self.Controler.GetSVGUIBlockType(SVGUIFB_Types[GetElementType(element)])
   682             
   700             
   683             text += """  if ((new_state = COMPARE_AND_SWAP_VAL(&out_state_%(id)d, UNCHANGED, PLC_BUSY)) == UNCHANGED ||
   701             text += """  if ((new_state = COMPARE_AND_SWAP_VAL(&out_state_%(id)d, UNCHANGED, PLC_BUSY)) == UNCHANGED ||
   684        (new_state = COMPARE_AND_SWAP_VAL(&out_state_%(id)d, CHANGED, PLC_BUSY)) == CHANGED) {
   702        (new_state = COMPARE_AND_SWAP_VAL(&out_state_%(id)d, CHANGED, PLC_BUSY)) == CHANGED) {
   685 """%texts
   703 """%texts
   686             for i, input in enumerate(block_infos["inputs"]):
   704             for i, input in enumerate(block_infos["inputs"]):