icon

Support Forum

[Frage] Communication between PythonPart and External Application

Schlagworte:
  • Allplan
  • PythonParts
  • Interactor
  • Sockets
  • Http
  • Communication
  • API
  • Automation

Hello everyone,

For a personal project, I want to visualize walls that have been drawn in Allplan in my own 3D environment which is a local running javascript application (which runs as a native app so i can access files on my pc).
I would like to communicate with Allplan real-time, but i cant seem to find any official documentation on how to do this.

I tried making a PythonPart interactor that sets up a http server (localhost) but when trying to do a very simple GET request, i cant even get a response.

I also red about some other options like using files (let PythonPart export geometry of a wall to a file on the computer and read the file with my javascript application).
And using sockets.

But still, i found nothing about this in offical documentation

So my question: does anyone have an idea on how to set up this communication? Or does anybody can link me to the official documentation that explains how this should be done?

Thanks.
With kind regards,
Jesper

Hilfreichste Antwort anzeigen Hilfreichste Antwort verbergen

Zitiert von: uid-478232
So my question: does anyone have an idea on how to set up this communication? Or does anybody can link me to the official documentation that explains how this should be done

Hi Jesper,

Connecting two applications, in this case ALLPLAN with your JS app, would require an HTTP connection in the first place. One of them must be the server, the other one has to be the requester. In this constellation, your app will probably be the requester as it will pull the data from ALLPLAN. So ALLPLAN would be the server listening for the requests from your app.

Your initial approach was right: you will need to start a local HTTP server within ALLPLAN. You can use frameworks like FastAPI with uvicorn for this. And run the server on a separate deamon thread (to make sure, it gets terminated, when ALLPLAN closes):

Initialize the Flask app as a global, like:

APP = FastAPI()

APP.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

Define a function for running the server:

def run_server(app: FastAPI):
    config = uvicorn.Config(app, host="127.0.0.1", port=5679, log_level="info", ws_ping_interval=0, ws_max_size=16777216)
    server = uvicorn.Server(config)
    server.run()

And start the server in the __init__ method of your ScriptObject on a separate thread:

self.app = APP
self.thread = Thread(target = run_server, args=(self.app,))
self.thread.daemon = True
self.thread.start()

Remember to stop the host in the on_cancel_function

def on_cancel_function(self) -> OnCancelFunctionResult:
    os.kill(os.getpid(), signal.SIGINT)
    print("Python Host is stopped")
    return OnCancelFunctionResult.CANCEL_INPUT

IMPORTANT: server is running only as long as PythonPart is running. Only in this case, your app may receive the data from ALLPLAN. This is a limitation you have to take into account. I am not aware, that a permanent background connection can be opened between ALLPLAN and another application.

Hopefully, after that you will be able to make a GET request from your app.

If you want to establish a persistent real-time two-way connection between your app and ALLPLAN, you will need to upgrade the HTTP connection to a websocket.

But getting an HTTP server running in ALLPLAN is the first step.

Cheers,
Bart

Hi,

You may search a tool like requests to establish a communication with your app - requests is installed with Allplan.

With a PythonPart (ScriptObject or Interactor) you can extract the geometry (here the examples depending of what you want).

Be careful with the selection, a global wall will return an 1D object (line - as explain here). If want the real geometry you need to select the wall tiers.

Best
Christophe

Zitiert von: uid-478232
So my question: does anyone have an idea on how to set up this communication? Or does anybody can link me to the official documentation that explains how this should be done

Hi Jesper,

Connecting two applications, in this case ALLPLAN with your JS app, would require an HTTP connection in the first place. One of them must be the server, the other one has to be the requester. In this constellation, your app will probably be the requester as it will pull the data from ALLPLAN. So ALLPLAN would be the server listening for the requests from your app.

Your initial approach was right: you will need to start a local HTTP server within ALLPLAN. You can use frameworks like FastAPI with uvicorn for this. And run the server on a separate deamon thread (to make sure, it gets terminated, when ALLPLAN closes):

Initialize the Flask app as a global, like:

APP = FastAPI()

APP.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

Define a function for running the server:

def run_server(app: FastAPI):
    config = uvicorn.Config(app, host="127.0.0.1", port=5679, log_level="info", ws_ping_interval=0, ws_max_size=16777216)
    server = uvicorn.Server(config)
    server.run()

And start the server in the __init__ method of your ScriptObject on a separate thread:

self.app = APP
self.thread = Thread(target = run_server, args=(self.app,))
self.thread.daemon = True
self.thread.start()

Remember to stop the host in the on_cancel_function

def on_cancel_function(self) -> OnCancelFunctionResult:
    os.kill(os.getpid(), signal.SIGINT)
    print("Python Host is stopped")
    return OnCancelFunctionResult.CANCEL_INPUT

IMPORTANT: server is running only as long as PythonPart is running. Only in this case, your app may receive the data from ALLPLAN. This is a limitation you have to take into account. I am not aware, that a permanent background connection can be opened between ALLPLAN and another application.

Hopefully, after that you will be able to make a GET request from your app.

If you want to establish a persistent real-time two-way connection between your app and ALLPLAN, you will need to upgrade the HTTP connection to a websocket.

But getting an HTTP server running in ALLPLAN is the first step.

Cheers,
Bart