def toFormex():
    """Transform the surface model to a named Formex."""
    if not check_surface():
        return
    itemlist = [ [ 'name', PF.get('project','')] ] 
    res,accept = widgets.inputDialog(itemlist,'Name of the Formex').getResult()
    if accept:
        name = res[0][1]
        #print name
        nodes,elems = PF['surface']
        #print nodes.shape
        #print elems.shape
        PF[name] = Formex(nodes[elems])
def read_surface(fn='',types=['stl/off','stl','off','neu','smesh','gts'],convert=None,show=True):
    """Read STL model from file fn.

    If no file is given, one is asked.
    The file fn should exist and contain a valid surface model.
    
    The STL model is stored in the Formex F.
    The workdir and project name are set from the filename.
    The Formex is stored under the project basename.
    The model is displayed.

    If convert == True, the model is converted to a Formex.
    If convert == False, it will not be converted.
    The default is to ask the user.
    """
    if not (fn and os.path.exists(fn)):
        if type(types) == str:
            types = [ types ]
        types = map(utils.fileDescription,types)
        fn = askFilename(GD.cfg['workdir'],types,exist=True)
    if fn:
        chdir(fn)
        set_project(utils.projectName(fn))
        GD.message("Reading file %s" % fn)
        GD.gui.setBusy()
        t = timer.Timer()
        nodes,elems =stl.readSurface(fn)
        GD.message("Time to import surface: %s seconds" % t.seconds())
        GD.gui.setBusy(False)
        set_surface(nodes,elems)
        if show:
            show_surface(view='front')
        if convert is None:
            convert = ack('Convert to Formex?')
        if convert:
            GD.debug("GOING")
            name = toFormex(PF.get('project',''))
            # This is convenient for the user
            if name:
                formex_menu.setSelection(name)
                if show:
                    formex_menu.drawSelection()
        else:
            pass
        
    return fn
def toFormex(name=''):
    """Transform the surface model to a named Formex.

    The name of the Formex is returned, unless the user cancelled the
    operation.
    """
    if not check_surface():
        return
    
    if not name:
        itemlist = [ ['name', PF.get('project','')] ] 
        res,accept = widgets.InputDialog(itemlist,'Name of the Formex').getResult()
        if accept:
            name = res[0][1]
    if name:
        GD.debug("Converting to Formex with name '%s" % name)
        nodes,elems = PF['surface']
        PF[name] = Formex(nodes[elems])

    return name
def set_color():
    color = widgets.getColor(PF.get('stl_color','green'))
    if color:
        PF['stl_color'] = colors.GLColor(color)