C# or C++ or Python

C# or C++ or Python

94013_6912l
Contributor Contributor
789 Views
4 Replies
Message 1 of 5

C# or C++ or Python

94013_6912l
Contributor
Contributor

[ FlexSim 21.2.0 ]

Hello guys, below is my question.

I want to create an user interface to control FlexSim. Like pushing some button to add a task executer or dragging something to change the speed of task executer. Can I using c++, c# or python to construct an interface and join with FlexSim? Thank you.

0 Likes
Accepted solutions (1)
790 Views
4 Replies
Replies (4)
Message 2 of 5

arunTTT2P
Collaborator
Collaborator
Message 3 of 5

94013_6912l
Contributor
Contributor
I know that. But it is not convenient for us. We want to use python or c++ to do the graphical user interface. But I'm not sure if python or c++ can work with FlexSim or not. Thank you!
0 Likes
Message 4 of 5

mischa_spelt
Advisor
Advisor

I would use TCP/IP with a custom protocol. To a certain extent you can do this within FlexSim proper, but for maximum flexibility I'd write a C++ module that controls sockets directly.

Then you can write the UI program in whatever language you are most comfortable with and talk to FlexSim through the module.

Message 5 of 5

philboboADSK
Autodesk
Autodesk
Accepted solution

I recently wrote a Python script that used the FlexScript socket commands to control a model, and they worked really well. So you can use C++ with the DLL Maker or Module SDK if you want full control, but the FlexScript socket commands are quite effective for communicating between another language and FlexSim.

See the pdf in: how to use flexsim socket communication with server and client - FlexSim Community

Below are some snippets of my python code with socket communication that might help.

Most languages have functions for socket communication. You could use C# or something else if you wanted to.

I expect that trying to implement inter-process communication is not going to be more "convenient" for you to build UIs than using FlexSim's GUI Builder or Dashboard widgets, but you are welcome to try.

import os
import subprocess
import socket

class FlexSimConnection():

    def __init__(self, flexsimPath, modelPath, address='localhost', port=5005, verbose=False, visible=False):
        self.flexsimPath = flexsimPath
        self.modelPath = modelPath
        self.address = address
        self.port = port
        self.verbose = verbose
        self.visible = visible

        self._launch_flexsim()
        
    def _launch_flexsim(self):
        if self.verbose:
            print("Launching " + self.flexsimPath + " " + self.modelPath)

        args = [self.flexsimPath, self.modelPath]
        if self.visible == False:
            args.append("-maintenance")
            args.append("nogui")
        self.flexsimProcess = subprocess.Popen(args)

        self._socket_init(self.address, self.port)
    
    def _close_flexsim(self):
        self.flexsimProcess.kill()


    def _socket_init(self, host, port):
        if self.verbose:
            print("Waiting for FlexSim to connect to socket on " + self.address + ":" + str(self.port))

        self.serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.serversocket.bind((host, port))
        self.serversocket.listen();

        (self.clientsocket, self.socketaddress) = self.serversocket.accept()
        if self.verbose:
            print("Socket connected")
        
        if self.verbose:
            print("Waiting for READY message")
        message = self._socket_recv()
        if self.verbose:
            print(message.decode('utf-8'))
        if message != b"READY":
            raise RuntimeError("Did not receive READY! message")

    def _socket_send(self, msg):
        totalsent = 0
        while totalsent < len(msg):
            sent = self.clientsocket.send(msg[totalsent:])
            if sent == 0:
                raise RuntimeError("Socket connection broken")
            totalsent = totalsent + sent

    def _socket_recv(self):
        chunks = []
        while 1:
            chunk = self.clientsocket.recv(2048)
            if chunk == b'':
                raise RuntimeError("Socket connection broken")
            if chunk[-1] == ord('!'):
                chunks.append(chunk[:-1])
                break;
            else:
                chunks.append(chunk)
        return b''.join(chunks)

def main():
    con = FlexSimConnection(
        flexsimPath = "C:/Program Files/FlexSim 2021 Update 2/program/flexsim.exe",
        modelPath = "C:/Users/MyUser/Documents/FlexSim 2021 Projects/my_model.fsm",
        verbose = True,
        visible = True
        )

if __name__ == "__main__":
    main()


Phil BoBo
Sr. Manager, Software Development