Connecting to Siemens NX server from python (NXOpen); pythonnet GetObject TypeError

3.8k views Asked by At

I am trying to grab the Session object from a Siemens NX server (on the same machine, i.e. localhost). I am using python 3.6 with pythonnet, since the remote connection (apparently) only works through .NET - I am VERY new to pythonnet (only tried it for this one function so far).

I need the Session object via remote because I want to grab some user inputs from the interface, therefore batch via run_journal is not an option.

The approach is based on what i found here: https://github.com/pythonnet/pythonnet/issues/276, but i get the error "TypeError: No method matches given arguments for GetObject"

import NXOpen
import clr
import System

theSession = System.Activator.GetObject(NXOpen.Session, "http://localhost:4574/Session")

I have also tried with GetObject(type(NXOpen.Session), ...) with the same error.

How do I have to feed the NXOpen object type into the GetObject function?

alternatively, are there any better ways to remotely connect to a Siemens NX Session using Python?

The server is the sample NXOpen .NET remote server as found in \Siemens\NX 12.0\UGOPEN\SampleNXOpenApplications.NET\RemotingExample\Server

1

There are 1 answers

0
Essigwurst On

Late, but it seems i found a solution after all. First, as i installed python 'for all users', i copied all NXOpen libraries in the NX python module directory (i doesn't use an IDE, just Notepad++).

Here's a working example, runs fine with python.exe file.py and a .NET remoting server.

import clr
from System import Activator

import sys
import math

# Managed NXOpen DLL's goes also in this directory
sys.path.append('C:/Siemens/NX 11/NXBIN/python')

clr.AddReference('NXOpen')
import NXOpen

def main() : 

    m_Session = Activator.GetObject(NXOpen.Session, "http://127.0.0.1:4567/NXOpenSession")
    m_WorkPart = m_Session.Parts.Work

    print(m_Session.Parts.Display.FullPath)

if __name__ == '__main__':
    main()