Changeset 811a6c8 in OpenModelica


Ignore:
Timestamp:
2020-09-17T09:10:09+02:00 (4 years ago)
Author:
GitHub <noreply@…>
Branches:
Added-citation-metadata, maintenance/v1.17, maintenance/v1.18, maintenance/v1.19, maintenance/v1.20, maintenance/v1.21, maintenance/v1.22, master, omlib-staging
Children:
3f23838, 61c66f7, 73840d6b, df1d3313
Parents:
7c40379
git-author:
Adeel Asghar <adeel.asghar@…> (09/17/20 09:10:09)
git-committer:
GitHub <noreply@…> (09/17/20 09:10:09)
Message:

Sync the component info and annotation (#6751)

Fixes ticket:6112

File:
1 edited

Legend:

Unmodified
Added
Removed
  • OMCompiler/Examples/generate_icons.py

    rdfcb35f0 r811a6c8  
    124124    return res
    125125
     126def removeFirstLastCurlBrackets(value):
     127    value = value.strip()
     128    if (len(value) > 1 and value[0] == '{' and value[len(value) - 1] == '}'):
     129        value = value[1: len(value) - 1]
     130    return value
     131
     132def removeFirstLastParentheses(value):
     133    value = value.strip()
     134    if (len(value) > 1 and value[0] == '(' and value[len(value) - 1] == ')'):
     135      value = value[1: len(value) - 1]
     136    return value
     137
     138def unparseArrays(value):
     139    lst = []
     140    braceopen = 0
     141    mainbraceopen = 0
     142    i = 0
     143    value = removeFirstLastCurlBrackets(value)
     144    subbraceopen = 0
     145
     146    while i < len(value):
     147      if value[i] == ' ' or value[i] == ',':
     148        i+=1
     149        continue # ignore any kind of space
     150      if value[i] == '{' and braceopen == 0:
     151        braceopen = 1
     152        mainbraceopen = i
     153        i+=1
     154        continue
     155      if value[i] == '{':
     156        subbraceopen = 1
     157
     158      if value[i] == '}' and braceopen == 1 and subbraceopen == 0:
     159        # closing of a group
     160        braceopen = 0
     161        lst.append(value[mainbraceopen:i+1])
     162        i+=1
     163        continue
     164      if value[i] == '}':
     165        subbraceopen = 0
     166
     167      # skip the whole quotes section
     168      if value[i] == '"':
     169        i+=1
     170        while value[i] != '"':
     171          i+=1
     172          if value[i-1] == '\\' and value[i] == '"':
     173            i+=1
     174
     175      i+=1
     176
     177    return lst
     178
     179def getStrings(value, start='{', end='}'):
     180    lst = []
     181    mask = False
     182    inString = False
     183    stringEnd = '\0'
     184    begin = 0
     185    ele = 0
     186
     187    for i in range(len(value)):
     188      if inString:
     189        if mask:
     190          mask = False
     191        else:
     192          if value[i] == '\\':
     193            mask = True
     194          elif value[i] == stringEnd:
     195            inString = False
     196      else:
     197        if value[i] == '"':
     198          stringEnd = '"'
     199          inString = True
     200        elif value[i] == '\'':
     201          stringEnd = '\''
     202          inString = True
     203        elif value[i] == ',':
     204          if ele == 0:
     205            lst.append(value[begin:i].strip())
     206            begin = i+1
     207        elif value[i] == start:
     208          ele+=1
     209        elif value[i] == end:
     210          ele-=1
     211
     212    lst.append(value[begin:len(value) + 1].strip())
     213    return lst
     214
     215
     216def consumeChar(value, res, i):
     217    if value[i] == '\\':
     218      i+=1
     219      if (value[i] == '\''):
     220        res.append('\'')
     221      elif (value[i] == '"'):
     222        res.append('\"')
     223      elif (value[i] == '?'):
     224        res.append('\?')
     225      elif (value[i] == '\\'):
     226        res.append('\\')
     227      elif (value[i] == 'a'):
     228        res.append('\a')
     229      elif (value[i] == 'b'):
     230        res.append('\b')
     231      elif (value[i] == 'f'):
     232        res.append('\f')
     233      elif (value[i] == 'n'):
     234        res.append('\n')
     235      elif (value[i] == 'r'):
     236        res.append('\r')
     237      elif (value[i] == 't'):
     238        res.append('\t')
     239      elif (value[i] == 'v'):
     240        res.append('\v')
     241    else:
     242      res.append(value[i])
     243
     244    return res
     245
     246def unparseStrings(value):
     247    lst = []
     248    value = value.strip()
     249    if value[0] != '{':
     250      return lst #ERROR?
     251    i = 1
     252    res = []
     253    while value[i] == '"':
     254      i+=1
     255      while value[i] != '"':
     256        res = consumeChar(value, res, i)
     257        i+=1
     258        # if we have unexpected double quotes then, however omc should return \"
     259        # remove this block once fixed in omc
     260        if value[i] == '"' and value[i+1] != ',':
     261          if value[i+1] != '}':
     262            res = consumeChar(value, res, i)
     263            i+=1
     264        # remove this block once fixed in omc
     265      i+=1
     266      if value[i] == '}':
     267        lst.append(''.join(res))
     268        return lst
     269      if value[i] == ',':
     270        lst.append(''.join(res))
     271        i+=1
     272        res = []
     273        while value[i] == ' ': # if we have space before next value e.g {"x", "y", "z"}
     274          i+=1
     275        continue
     276      while value[i] != '"' and value[i] is not None:
     277        i+=1
     278        print("error? malformed string-list. skipping: %c" % value[i])
     279
     280    return lst
     281
     282def componentPlacement(componentAnnotations):
     283    componentAnnotations = removeFirstLastCurlBrackets(componentAnnotations)
     284    annotations = getStrings(componentAnnotations, '(', ')')
     285    for annotation in annotations:
     286        if annotation.startswith('Placement'):
     287            annotation = annotation[len('Placement'):]
     288            placementAnnotation = removeFirstLastParentheses(annotation)
     289            if placementAnnotation.lower() == 'error':
     290                return []
     291            else:
     292                return getStrings(placementAnnotation)
    126293
    127294graphics_cache = {}
    128 
    129295
    130296# get graphics objects from annotation Icon
     
    321487    return result
    322488
    323 
    324489def getGraphicsWithPortsForClass(modelicaClass):
    325490    graphics = getGraphicsForClass(modelicaClass)
    326491    graphics['className'] = modelicaClass
    327492    graphics['ports'] = []
    328     answer_full = ask_omc('getComponents', modelicaClass, parsed=False)
    329 
    330     comp_id = 0
    331     for answer in answer_full[2:].split('},{'):
    332         #print answer
    333         comp_id += 1
    334         class_name = answer[0:answer.find(',')]
    335         component_name = answer[answer.find(',') + 1:][0:answer[answer.find(',') + 1:].find(',')]
    336 
    337         if ask_omc('isConnector', class_name):
    338             try:
    339                 comp_annotation = ask_omc('getNthComponentAnnotation', modelicaClass + ', ' + str(comp_id))['SET2']['Set1']
    340             except KeyError as ex:
    341                 logger.error('KeyError: {0} componentName: {1} {2}'.format(modelicaClass, component_name, str(ex)))
    342                 continue
    343 
    344             # base class graphics for ports
    345             g_base = []
    346             base_classes = []
    347             getBaseClasses(class_name, base_classes)
    348 
    349             for base_class in base_classes:
    350                 graphics_base = getGraphicsForClass(base_class)
    351                 g_base.append(graphics_base)
    352 
    353             g = getGraphicsForClass(class_name)
    354 
    355             g_this = g['graphics']
    356 
    357             g['graphics'] = []
    358             for g_b in g_base:
    359                 for g_i in g_b['graphics']:
    360                     g['graphics'].append(g_i)
    361             for g_b in g_this:
    362                 g['graphics'].append(g_b)
    363 
    364             g['id'] = component_name
    365             g['className'] = class_name
    366 
    367             desc = ask_omc('getComponentComment', modelicaClass + ', ' + component_name)
    368             if type(desc) is dict:
    369                 g['desc'] = ''
    370             else:
    371                 g['desc'] = desc.strip().strip('"')
    372 
    373             g['classDesc'] = ask_omc('getClassComment', class_name).strip().strip('"')
    374 
    375             minX = g['coordinateSystem']['extent'][0][0]
    376             minY = g['coordinateSystem']['extent'][0][1]
    377             maxX = g['coordinateSystem']['extent'][1][0]
    378             maxY = g['coordinateSystem']['extent'][1][1]
    379 
    380             for gs in g['graphics']:
    381                 # use default values if it is not there
    382                 if not 'extent' in gs:
    383                     gs['extent'] = [[-100, -100], [100, 100]]
    384 
    385                 if not 'origin' in gs:
    386                     gs['origin'] = [0, 0]
    387 
    388                 if minX > gs['extent'][0][0] + gs['origin'][0]:
    389                     minX = gs['extent'][0][0] + gs['origin'][0]
    390                 if minX > gs['extent'][1][0] + gs['origin'][0]:
    391                     minX = gs['extent'][1][0] + gs['origin'][0]
    392                 if minY > gs['extent'][0][1] + gs['origin'][1]:
    393                     minY = gs['extent'][0][1] + gs['origin'][1]
    394                 if minY > gs['extent'][1][1] + gs['origin'][1]:
    395                     minY = gs['extent'][1][1] + gs['origin'][1]
    396                 if maxX < gs['extent'][1][0] + gs['origin'][0]:
    397                     maxX = gs['extent'][1][0] + gs['origin'][0]
    398                 if maxX < gs['extent'][0][0] + gs['origin'][0]:
    399                     maxX = gs['extent'][0][0] + gs['origin'][0]
    400                 if maxY < gs['extent'][1][1] + gs['origin'][1]:
    401                     maxY = gs['extent'][1][1] + gs['origin'][1]
    402                 if maxY < gs['extent'][0][1] + gs['origin'][1]:
    403                     maxY = gs['extent'][0][1] + gs['origin'][1]
    404 
    405             g['coordinateSystem']['extent'] = [[minX, minY], [maxX, maxY]]
    406 
    407             #print comp_annotation
    408             index_delta = 7
    409             if comp_annotation[10] == "-":
    410                 # fallback to diagram annotations
    411                 index_delta = 0
    412 
    413             for i in [1,2,7]:
    414               if comp_annotation[i + index_delta] == "-":
    415                 comp_annotation[i + index_delta] = 0
    416             origin_x = comp_annotation[1 + index_delta]
    417             origin_y = comp_annotation[2 + index_delta]
    418             x0 = comp_annotation[3 + index_delta]
    419             y0 = comp_annotation[4 + index_delta]
    420             x1 = comp_annotation[5 + index_delta]
    421             y1 = comp_annotation[6 + index_delta]
    422 
    423             rotation = comp_annotation[7 + index_delta]
    424 
    425             g['transformation'] = {}
    426             g['transformation']['origin'] = [origin_x, origin_y]
    427             g['transformation']['extent'] = [[x0, y0], [x1, y1]]
    428             if isinstance(rotation,dict):
    429               g['transformation']['rotation'] = 0.0
    430             else:
    431               g['transformation']['rotation'] = rotation
    432             graphics['ports'].append(g)
     493    componentsList = unparseArrays(ask_omc('getComponents', modelicaClass + ', useQuotes = true', parsed=False))
     494    if componentsList:
     495        componentAnnotations = ask_omc('getComponentAnnotations', modelicaClass, parsed=False)
     496        componentAnnotationsList = getStrings(removeFirstLastCurlBrackets(componentAnnotations))
     497
     498        for i in range(len(componentsList)):
     499            componentInfo = unparseStrings(componentsList[i])
     500            class_name = componentInfo[0]
     501            component_name = componentInfo[1]
     502
     503            if ask_omc('isConnector', class_name):
     504                comp_annotation = componentPlacement(componentAnnotationsList[i])
     505
     506                # base class graphics for ports
     507                g_base = []
     508                base_classes = []
     509                getBaseClasses(class_name, base_classes)
     510
     511                for base_class in base_classes:
     512                    graphics_base = getGraphicsForClass(base_class)
     513                    g_base.append(graphics_base)
     514
     515                g = getGraphicsForClass(class_name)
     516
     517                g_this = g['graphics']
     518
     519                g['graphics'] = []
     520                for g_b in g_base:
     521                    for g_i in g_b['graphics']:
     522                        g['graphics'].append(g_i)
     523                for g_b in g_this:
     524                    g['graphics'].append(g_b)
     525
     526                g['id'] = component_name
     527                g['className'] = class_name
     528
     529                g['desc'] = componentInfo[2]
     530
     531                g['classDesc'] = ask_omc('getClassComment', class_name).strip().strip('"')
     532
     533                minX = g['coordinateSystem']['extent'][0][0]
     534                minY = g['coordinateSystem']['extent'][0][1]
     535                maxX = g['coordinateSystem']['extent'][1][0]
     536                maxY = g['coordinateSystem']['extent'][1][1]
     537
     538                for gs in g['graphics']:
     539                    # use default values if it is not there
     540                    if not 'extent' in gs:
     541                        gs['extent'] = [[-100, -100], [100, 100]]
     542
     543                    if not 'origin' in gs:
     544                        gs['origin'] = [0, 0]
     545
     546                    if minX > gs['extent'][0][0] + gs['origin'][0]:
     547                        minX = gs['extent'][0][0] + gs['origin'][0]
     548                    if minX > gs['extent'][1][0] + gs['origin'][0]:
     549                        minX = gs['extent'][1][0] + gs['origin'][0]
     550                    if minY > gs['extent'][0][1] + gs['origin'][1]:
     551                        minY = gs['extent'][0][1] + gs['origin'][1]
     552                    if minY > gs['extent'][1][1] + gs['origin'][1]:
     553                        minY = gs['extent'][1][1] + gs['origin'][1]
     554                    if maxX < gs['extent'][1][0] + gs['origin'][0]:
     555                        maxX = gs['extent'][1][0] + gs['origin'][0]
     556                    if maxX < gs['extent'][0][0] + gs['origin'][0]:
     557                        maxX = gs['extent'][0][0] + gs['origin'][0]
     558                    if maxY < gs['extent'][1][1] + gs['origin'][1]:
     559                        maxY = gs['extent'][1][1] + gs['origin'][1]
     560                    if maxY < gs['extent'][0][1] + gs['origin'][1]:
     561                        maxY = gs['extent'][0][1] + gs['origin'][1]
     562
     563                g['coordinateSystem']['extent'] = [[minX, minY], [maxX, maxY]]
     564
     565                #print comp_annotation
     566                index_delta = 7
     567                if comp_annotation[10] == "-":
     568                    # fallback to diagram annotations
     569                    index_delta = 0
     570
     571                for i in [1,2,7]:
     572                    if comp_annotation[i + index_delta] == "-":
     573                        comp_annotation[i + index_delta] = 0
     574                origin_x = float(comp_annotation[1 + index_delta])
     575                origin_y = float(comp_annotation[2 + index_delta])
     576                x0 = float(comp_annotation[3 + index_delta])
     577                y0 = float(comp_annotation[4 + index_delta])
     578                x1 = float(comp_annotation[5 + index_delta])
     579                y1 = float(comp_annotation[6 + index_delta])
     580
     581                if comp_annotation[7 + index_delta] == "":
     582                    rotation = 0.0
     583                else:
     584                    rotation = float(comp_annotation[7 + index_delta])
     585
     586                g['transformation'] = {}
     587                g['transformation']['origin'] = [origin_x, origin_y]
     588                g['transformation']['extent'] = [[x0, y0], [x1, y1]]
     589                if isinstance(rotation,dict):
     590                    g['transformation']['rotation'] = 0.0
     591                else:
     592                    g['transformation']['rotation'] = rotation
     593                graphics['ports'].append(g)
    433594
    434595    return graphics
Note: See TracChangeset for help on using the changeset viewer.