コード例 #1
0
ファイル: FOFIN_open_cmd.py プロジェクト: jf---/compas_fofin
def open_file(settings):
    if not settings["file.dir"]:
        filepath = compas_rhino.select_file(folder=HERE, filter="fofin")
    else:
        filepath = compas_rhino.select_file(folder=settings["file.dir"], filter="fofin")

    if not filepath:
        return

    file_dir = os.path.dirname(filepath)
    file_name = os.path.basename(filepath)

    settings["file.dir"] = file_dir
    settings["file.name"] = file_name

    if not file_name.endswith(".fofin"):
        print("The filename is invalid: {}".format(file_name))
        return

    filepath = os.path.join(file_dir, file_name)

    with open(filepath, "r") as f:
        data = json.load(f)

        if "settings" in data and data["settings"]:
            settings.update(data["settings"])

        if "cablenet" in data and data["cablenet"]:
            cablenet = Cablenet.from_data(data["cablenet"])
            cablenet.draw(layer=settings["layer"], clear_layer=True, settings=settings)
        else:
            cablenet = None

    return cablenet
コード例 #2
0
def RunCommand(is_interactive):
    if 'TNA' not in sc.sticky:
        raise Exception("Initialise the plugin first!")

    TNA = sc.sticky['TNA']

    settings = TNA['settings']

    options = ['obj', 'json', 'lines', 'mesh']
    option = rs.GetString("Initialise FormDiagram from", options[0], options)
    if not option:
        return

    if option == 'obj':
        filepath = compas_rhino.select_file(folder=compas_tna.DATA,
                                            filter='OBJ files (*.obj)|*.obj||')
        if not filepath:
            return
        form = FormDiagram.from_obj(filepath)
    elif option == 'json':
        filepath = compas_rhino.select_file(
            folder=compas_tna.DATA, filter='JSON files (*.json)|*.json||')
        if not filepath:
            return
        form = FormDiagram.from_json(filepath)
    elif option == 'lines':
        guids = compas_rhino.select_lines()
        if not guids:
            return
        lines = compas_rhino.get_line_coordinates(guids)
        form = FormDiagram.from_lines(lines)
    elif option == 'mesh':
        guid = compas_rhino.select_mesh()
        if not guid:
            return
        form = FormDiagram.from_rhinomesh(guid)
    else:
        raise NotImplementedError

    del TNA['form']
    del TNA['force']

    TNA['form'] = form
    TNA['force'] = None

    compas_rhino.clear_layer(settings['layer.force'])
    form.draw(layer=settings['layer.form'],
              clear_layer=True,
              settings=settings)
コード例 #3
0
ファイル: helpers.py プロジェクト: edwardyanxin/compas-RV2
def select_filepath_open(root, ext):
    """Select a filepath for opening a session.

    Parameters
    ----------
    root : str
        Base directory from where the file selection is started.
        If no directory is provided, the parent folder of the current
        Rhino document will be used
    ext : str
        The type of file that can be openend.

    Returns
    -------
    tuple
        The parent directory.
        The file name.
    None
        If the procedure fails.

    Notes
    -----
    The file extension is only used to identify the type of session file.
    Regardless of the provided extension, the file contents should be in JSON format.

    """
    ext = ext.split('.')[-1]

    filepath = compas_rhino.select_file(folder=root, filter=ext)

    if not is_valid_file(filepath, ext):
        print("This is not a valid session file: {}".format(filepath))
        return

    return filepath
コード例 #4
0
ファイル: FOFIN_from_cmd.py プロジェクト: jf---/compas_fofin
def create_cablenet_from_json(settings):
    folder = settings["file.dir"] or HERE
    filepath = compas_rhino.select_file(folder=folder, filter="json")
    if not filepath:
        return
    cablenet = Cablenet.from_json(filepath)
    return cablenet
コード例 #5
0
 def assembly_from_json(self):
     path = compas_rhino.select_file(folder=compas_rbe.DATA,
                                     filter='JSON files (*.json)|*.json||')
     if not path:
         return
     self.assembly = assembly = Assembly.from_json(path)
     self.assembly.draw(self.settings['layer'])
コード例 #6
0
def RunCommand(is_interactive):
    try:
        if 'compas_assembly' not in sc.sticky:
            raise Exception('Initialise the Assembly plugin first!')

        path = compas_rhino.select_file(folder=SESSIONS,
                                        filter='JSON files (*.json)|*.json||')
        if not path:
            return

        with open(path, 'r') as fo:
            session = json.load(fo)

        if 'blocks' not in session:
            raise Exception('Session data is incomplete.')
        if 'assembly' not in session:
            raise Exception('Session data is incomplete.')
        if 'settings' not in session:
            raise Exception('Session data is incomplete.')

        sc.sticky['compas_assembly']['settings'].update(session['settings'])
        settings = sc.sticky['compas_assembly']['settings']

        assembly = Assembly.from_data(session['assembly'])
        assembly.blocks = {
            key: Block.from_data(data)
            for key, data in session['blocks'].items()
        }
        assembly.draw(settings)

        sc.sticky['compas_assembly']['assembly'] = assembly

    except Exception as error:
        print(error)
        print(traceback.format_exc())
コード例 #7
0
def RunCommand(is_interactive):
    try:
        if 'compas_assembly' not in sc.sticky:
            raise Exception('Initialise the Assembly plugin first!')

        settings = sc.sticky['compas_assembly']['settings']

        # the default path points to the wrong location
        path = compas_rhino.select_file(folder=compas_assembly.DATA,
                                        filter='JSON files (*.json)|*.json||')
        if not path:
            return

        assembly = Assembly.from_json(path)
        sc.sticky['compas_assembly']['assembly'] = assembly

        assembly.draw(settings)

    except Exception as error:
        print(error)
        print(traceback.format_exc())
コード例 #8
0
def RunCommand(is_interactive):
    if 'TNA' not in sc.sticky:
        raise Exception("Initialise the plugin first!")

    TNA = sc.sticky['TNA']

    form = TNA['form']
    force = TNA['force']

    settings = TNA['settings']

    options = ['save', 'save_as', 'open']
    option = rs.GetString("Initialise FormDiagram from", options[0], options)

    if not option:
        return

    if option == 'save':
        # save the current file in the current directory

        # get the components of the current filepath
        if not settings['file.dir']:
            file_dir = compas_rhino.select_folder(default=HERE)
        else:
            file_dir = compas_rhino.select_folder(default=settings['file.dir'])

        if not os.path.isdir(file_dir):
            print('The selected directory is invalid: {}'.format(file_dir))
            return

        settings['file.dir'] = file_dir

        if not settings['file.name']:
            file_name = compas_rhino.select_file(folder=settings['file.dir'])
            if not file_name:
                print('The filename is invalid: {}'.format(file_name))
                return
            settings['file.name'] = file_name

        file_name = settings['file.name']

        if not file_name.endswith('.json'):
            print('The filename is invalid: {}'.format(file_name))
            return

        # compile the filepath
        filepath = os.path.join(file_dir, file_name)

        # compile the data dict
        data = {'settings': settings}
        if form:
            data['form'] = form.to_data()
        if force:
            data['force'] = force.to_data()

        # write the data dict to the specified file
        with open(filepath, 'w') as f:
            json.dump(data, f)

    elif option == 'save_as':
        # save the current file using a different name and location

        # get the components of the current filepath
        if not settings['file.dir']:
            file_dir = compas_rhino.select_folder(default=HERE)
        else:
            file_dir = compas_rhino.select_folder(default=settings['file.dir'])

        settings['file.dir'] = file_dir

        if not os.path.isdir(file_dir):
            print('The selected directory is invalid: {}'.format(file_dir))
            return

        file_name = rs.GetString('File name')
        if not file_name:
            print('The filename is invalid: {}'.format(file_name))
            return

        if not file_name.endswith('.json'):
            print('The filename is invalid: {}'.format(file_name))
            return

        settings['file.dir'] = file_dir
        settings['file.name'] = file_name

        if not file_name.endswith('.json'):
            print('The filename is invalid: {}'.format(file_name))
            return

        # compile the filepath
        filepath = os.path.join(file_dir, file_name)

        data = {'settings': settings}

        if form:
            data['form'] = form.to_data()
        if force:
            data['force'] = force.to_data()

        with open(filepath, 'w') as f:
            json.dump(data, f)

    elif option == 'open':
        # open a specified file
        # and update the current file properties in the settings

        # get the dirname of the current filepath
        if not settings['file.dir']:
            filepath = compas_rhino.select_file(
                folder=HERE, filter='JSON files (*.json)|*.json||')
        else:
            filepath = compas_rhino.select_file(
                folder=settings['file.dir'],
                filter='JSON files (*.json)|*.json||')

        if not filepath:
            return

        file_dir = os.path.dirname(filepath)
        file_name = os.path.basename(filepath)

        settings['file.dir'] = file_dir
        settings['file.name'] = file_name

        if not file_name.endswith('.json'):
            print('The filename is invalid: {}'.format(file_name))
            return

        # compile the filepath
        filepath = os.path.join(file_dir, file_name)

        compas_rhino.clear_layer(settings['layer.form'])
        compas_rhino.clear_layer(settings['layer.force'])

        with open(filepath, 'r') as f:
            data = json.load(f)

            settings.update(data['settings'])

            if 'form' in data and data['form']:
                form = FormDiagram.from_data(data['form'])
                form.draw(layer=settings['layer.form'],
                          clear_layer=True,
                          settings=settings)
            else:
                form = None

            if form and 'force' in data and data['force']:
                force = ForceDiagram.from_data(data['force'])
                force.draw(layer=settings['layer.force'], clear_layer=True)
            else:
                force = None

            del TNA['form']
            del TNA['force']

            TNA['form'] = form
            TNA['force'] = force
            TNA['settings'] = settings

    else:
        # any other options are invalid
        raise NotImplementedError
コード例 #9
0
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os

import compas_rhino
from compas_assembly.datastructures import Assembly

HERE = os.path.dirname(__file__)
DATA = os.path.join(HERE, '../data')

path = compas_rhino.select_file(folder=DATA, filter='JSON files (*.json)|*.json||')

assembly = Assembly.from_json(path)
assembly.draw({
    'layer': 'Assembly',
    'show.vertices': True,
    'show.interfaces': True,
    'show.forces': True,
    'show.forces_as_vectors': False,
    'mode.interface': 0,
    'scale.force': 1.0
})
コード例 #10
0
def RunCommand(is_interactive):

    if '3GS' not in sc.sticky:
        compas_rhino.display_message('3GS has not been initialised yet.')
        return

    system = sc.sticky['3GS']['system']
    scene = sc.sticky['3GS']['scene']

    filepath = compas_rhino.select_file(folder=system['session.dirname'],
                                        filter=system['session.extension'])

    if not filepath:
        return
    if not os.path.exists(filepath):
        return
    if not os.path.isfile(filepath):
        return
    if not filepath.endswith(".{}".format(system['session.extension'])):
        return

    dirname, basename = os.path.split(filepath)
    filename, extension = os.path.splitext(basename)

    system['session.dirname'] = dirname
    system['session.filename'] = filename

    with open(filepath, "r") as f:
        session = json.load(f, cls=DataDecoder)

    if not session['data']['force']:
        compas_rhino.display_message('The session file has no force diagram.')

    forcediagram = ForceVolMesh.from_data(session['data']['force'])

    force_id = scene.add_forcevolmesh(forcediagram,
                                      name='force',
                                      layer='3GS::ForceDiagram')
    force = scene.find(force_id)

    if 'settings' in session['scene']['force']:
        force.settings.update(session['scene']['force']['settings'])

    if 'anchor' in session['scene']['force']:
        force.anchor = session['scene']['force']['anchor']

    if 'location' in session['scene']['force']:
        force.location = session['scene']['force']['location']

    if 'scale' in session['scene']['form']:
        force.scale = session['scene']['force']['scale']

    if session['data']['form']:

        formdiagram = FormNetwork.from_data(session['data']['form'])

        form_id = scene.add_formnetwork(formdiagram,
                                        name='form',
                                        layer='3GS::FormDiagram')
        form = scene.find(form_id)

        forcediagram.primal = formdiagram
        formdiagram.dual = forcediagram

        if 'settings' in session['scene']['form']:
            form.settings.update(session['scene']['form']['settings'])

        if 'anchor' in session['scene']['form']:
            form.anchor = session['scene']['form']['anchor']

        if 'location' in session['scene']['form']:
            form.location = session['scene']['form']['location']

        if 'scale' in session['scene']['form']:
            form.scale = session['scene']['form']['scale']

    scene.update()
    scene.save()
コード例 #11
0
ファイル: TNA_file_cmd.py プロジェクト: feihln/compas_tna
def RunCommand(is_interactive):
    if 'TNA' not in sc.sticky:
        raise Exception("Initialise the plugin first!")

    TNA = sc.sticky['TNA']

    form = TNA['form']
    force = TNA['force']

    settings = TNA['settings']

    options = ['save', 'save as', 'open']
    option = rs.GetString("Initialise FormDiagram from", options[0], options)

    if not option:
        return

    if option == 'save':
        if not settings['file.dir']:
            file_dir = compas_rhino.select_folder(default=compas_tna.DATA)
        else:
            file_dir = settings['file.dir']
        if not os.path.isdir(file_dir):
            return

        if not settings['file.name']:
            file_name = rs.GetString('File name')
            if not file_name:
                return
        else:
            file_name = settings['file.name']
        if not file_name.endswith('.json'):
            file_name += '.json'

        filepath = os.path.join(file_dir, file_name)
        if not filepath:
            return

        data = {'settings': settings}

        if form:
            data['form'] = form.to_data()
        if force:
            data['force'] = force.to_data()

        with open(filepath, 'w') as f:
            json.dump(data, f)

    elif option == 'save as':
        file_dir = compas_rhino.select_folder(default=compas_tna.DATA)
        if not file_dir:
            return
        if not os.path.isdir(file_dir):
            return

        file_name = rs.GetString('File name')
        if not file_name:
            return
        if not file_name.endswith('.json'):
            file_name += '.json'

        settings['file.dir'] = file_dir
        settings['file.name'] = file_name

        filepath = os.path.join(file_dir, file_name)
        if not filepath:
            return

        data = {'settings': settings}

        if form:
            data['form'] = form.to_data()
        if force:
            data['force'] = force.to_data()

        with open(filepath, 'w') as f:
            json.dump(data, f)

    elif option == 'open':
        filepath = compas_rhino.select_file(folder=settings['file.dir'], filter='json')
        if not filepath:
            return

        compas_rhino.clear_layer(settings['layer.form'])
        compas_rhino.clear_layer(settings['layer.force'])

        with open(filepath, 'r') as f:
            data = json.load(f)

            settings.update(data['settings'])

            if 'form' in data:
                if data['form']:
                    form = FormDiagram.from_data(data['form'])
                    form.draw(layer=settings['layer.form'], clear_layer=True, settings=settings)
                else:
                    form = None
            else:
                form = None

            if form:
                if 'force' in data:
                    if data['force']:
                        force = ForceDiagram.from_data(data['force'])
                        force.draw(layer=settings['layer.force'], clear_layer=True)
                    else:
                        force = None
                else:
                    force = None

            del TNA['form']
            del TNA['force']

            TNA['form'] = form
            TNA['force'] = force
            TNA['settings'] = settings

    else:
        raise NotImplementedError