Python Api

Python Api

cool.stuff
Collaborator Collaborator
4,397 Views
20 Replies
Message 1 of 21

Python Api

cool.stuff
Collaborator
Collaborator

Good morning!!!

 

When I try to define a support in Python with the follwoing code,

 

sup = labels.Create(rp.IRobotLabelType.I_LT_SUPPORT, 'sup')
sup_dt=rp.IRobotNodeSupportData
sup_dt = sup.Data
sup_dt.UX = 1
sup_dt.UY = 1
sup_dt.UZ = 1
sup_dt.RX = 1
sup_dt.RY = 1
sup_dt.RZ = 1
labels.Store(sup)

 

I got the error:

 


File "<ipython-input-56-8cf91e21e0e8>", line 1, in <module>
sup_dt = sup.Data

AttributeError: '__ComObject' object has no attribute 'Data'

 

Can someone help me?

Thanks

 

0 Likes
Accepted solutions (1)
4,398 Views
20 Replies
Replies (20)
Message 2 of 21

JacquesGaudin
Advocate
Advocate

You can try casting the object returned by ``labels.Create`` to ``IRobotLabel`` to make sure it implements the Label interface. I do that often since I am running the code on CPython through PythonNet.

 

sup = IRobotLabel(labels.Create(rp.IRobotLabelType.I_LT_SUPPORT, 'sup')

sup_dt = IRobotNodeSupportData(sup.Data)

 

That should solve the issue. 🤞

Message 3 of 21

cool.stuff
Collaborator
Collaborator

Many thanks for your answer and sorry for my late one!!!!

 

I tried what you said but stil get an syntax error:

 

import clr
import math
import sys
import csv


clr.AddReference('C:\Program Files\Autodesk\Autodesk Robot Structural Analysis Professional 2019\System\Exe\Interop.RobotOM.dll')

 

import RobotOM as rbt


from System import Environment
from RobotOM import *
from System import Object


robapp=rbt.RobotApplicationClass()
struc=robapp.Project.Structure
node=struc.Nodes
bar=struc.Bars
labels=struc.Labels
stress=struc.Results.Bars.Stresses
force=struc.Results.Bars.Forces


sup = IRobotLabel(labels.Create(robapp.IRobotLabelType.I_LT_SUPPORT, 'createdSupport')
supData = IRobotNodeSupportData(sup.Data)
supData.UX = 1
supData.UY = 1
supData.UZ = 1
supData.RX = 1
supData.RY = 1
supData.RZ = 1
labels.Store(sup)

 

Any aideas why?

0 Likes
Message 4 of 21

JacquesGaudin
Advocate
Advocate

Please include the full error message including line number. It makes it much easier to spot the mistake. It may just be a typo so please copy exactly the code you are trying to run.

0 Likes
Message 5 of 21

cool.stuff
Collaborator
Collaborator

Many thanks for your answer!!

 

My full code (the is like this one):

 

import clr
import math
import sys
import csv


clr.AddReference('C:\Program Files\Autodesk\Autodesk Robot Structural Analysis Professional 2019\System\Exe\Interop.RobotOM.dll')


import RobotOM as rbt

from System import Environment
from RobotOM import *
from System import Object

 

robapp=rbt.RobotApplicationClass()
struc=robapp.Project.Structure
node=struc.Nodes
bar=struc.Bars
labels=struc.Labels
stress=struc.Results.Bars.Stresses
force=struc.Results.Bars.Forces

 

sup = IRobotLabel(labels.Create(robapp.IRobotLabelType.I_LT_SUPPORT, 'createdSupport')
supData = IRobotNodeSupportData(sup.Data)
supData.UX = 1
supData.UY = 1
supData.UZ = 1
supData.RX = 1
supData.RY = 1
supData.RZ = 1
labels.Store(sup)

 

 

 

The error:

 

 

runfile('G:/WIP/RSAP Python/RSAP_Test.py', wdir='G:/WIP/RSAP Python')
Traceback (most recent call last):

File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\IPython\core\interactiveshell.py", line 2963, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)

File "<ipython-input-2-727407a95006>", line 1, in <module>
runfile('G:/WIP/RSAP Python/RSAP_Test.py', wdir='G:/WIP/RSAP Python')

File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)

File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "G:/WIP/RSAP Python/RSAP_Test.py", line 45
supData = IRobotNodeSupportData(sup.Data)
^
SyntaxError: invalid syntax

 

 

This is what I get..

 

Thanks! 🙂

0 Likes
Message 6 of 21

JacquesGaudin
Advocate
Advocate
There is a bracket missing at the end of

sup = IRobotLabel(labels.Create(robapp.IRobotLabelType.I_LT_SUPPORT, 'createdSupport')

Message 7 of 21

JacquesGaudin
Advocate
Advocate

There is a closing bracket missing at the end of:

 

sup = IRobotLabel(labels.Create(robapp.IRobotLabelType.I_LT_SUPPORT, 'createdSupport')

Message 8 of 21

cool.stuff
Collaborator
Collaborator

Many thanks!!! 🙂

 

What a silly mistake -.-

 

But now I get the following error:

 

sup = IRobotLabel(labels.Create(robapp.IRobotLabelType.I_LT_SUPPORT, 'createdSupport'))

AttributeError: 'RobotApplicationClass' object has no attribute 'IRobotLabelType'

 

I've changed to sup = IRobotLabel(labels.Create(robapp.IRobotLabel.I_LT_SUPPORT, 'createdSupport')) but stil get the same..

0 Likes
Message 9 of 21

JacquesGaudin
Advocate
Advocate
Accepted solution

In fairness, the error was mine in the original answer 😉

 

IRobotLabelType is in the RobotOM module so it should be:

 

sup = IRobotLabel(labels.Create(RobotOM.IRobotLabelType.I_LT_SUPPORT, 'createdSupport'))

 

or since you import * from RobotOM:

 

sup = IRobotLabel(labels.Create(IRobotLabelType.I_LT_SUPPORT, 'createdSupport'))

 

 

Note if you want to look at the list of names defined in an object or module, you can use:

 

dir(myObject)

 

dir(myModule)

 

It helps locating things as the API documentation is sometimes incomplete.

Message 10 of 21

cool.stuff
Collaborator
Collaborator

Works like a charm!!! :))

 

Many thanks for solving and for teaching a few useful tricks 🙂

 

You're the man!!! 🙂

Message 11 of 21

ArunNDinesh
Enthusiast
Enthusiast

@cool.stuff Can you please share the final code which is working? I just budding some python integration with RSA. The code in the thread is not working for me.

import clr
import math
import sys
import csv
clr.AddReference('C:\Program Files\Autodesk\Robot Structural Analysis Professional 2023\Exe\Interop.RobotOM.dll')
import RobotOM as rbt
from System import Environment
from RobotOM import *
from System import Object
robapp=rbt.RobotApplicationClass()
struc=robapp.Project.Structure

RobApp  = RobotApplicationClass() # Connect to  Robot Structural Analysis
RobProj = RobApp.Project # To Get current project
RobStruc = RobProj.Structure # To Get current model

node=RobStruc.Nodes
bar=RobStruc.Bars
labels=RobStruc.Labels
stress=RobStruc.Results.Bars.Stresses
force=RobStruc.Results.Bars.Forces

sup = IRobotLabel(labels.Create(RobotOM.IRobotLabelType.I_LT_SUPPORT, 'Sup'))
supData = IRobotNodeSupportData(sup.Data)
supData.UX = 1
supData.UY = 1
supData.UZ = 1
supData.RX = 1
supData.RY = 1
supData.RZ = 1
labels.Store(sup)
0 Likes
Message 12 of 21

JacquesGaudin
Advocate
Advocate

@ArunNDinesh 

Please post the error message, we need to know how it's not working to help you.

0 Likes
Message 13 of 21

ArunNDinesh
Enthusiast
Enthusiast
Unhandled Exception: System.NotSupportedException: Cannot create uninitialized instances of types requiring managed activation.
   at System.Runtime.Serialization.FormatterServices.nativeGetUninitializedObject(RuntimeType type)
   at Python.Runtime.ClassObject.tp_new_impl(BorrowedReference tp, BorrowedReference args, BorrowedReference kw)
   at Python.Runtime.NativeCall.Call_3(IntPtr fp, BorrowedReference a1, BorrowedReference a2, BorrowedReference a3)
   at Python.Runtime.MetaType.tp_call(BorrowedReference tp, BorrowedReference args, BorrowedReference kw)

@JacquesGaudinThank you for the response. I'm just learning the python integration with RSA, So I'm just using the limited threads and curious to see how python is calling the objects in RSA. Thanks for your time in advance 🙂

0 Likes
Message 14 of 21

JacquesGaudin
Advocate
Advocate

@ArunNDinesh 

This is rather unusual! Do you know which line triggers the error?

0 Likes
Message 15 of 21

ArunNDinesh
Enthusiast
Enthusiast

I do have issues in these many lines. I don't know what is the reason and got stuck!!

@JacquesGaudin Please help if you know . . .

andi4G4Z4_0-1683260219313.png

 

0 Likes
Message 16 of 21

JacquesGaudin
Advocate
Advocate

@ArunNDinesh 

I need to understand which line causes the error. Could you add some print statements in your code to establish where it stops?

 

The messages saying the import cannot be resolved look like warnings emitted by your IDE, I wouldn't worry to much about them for now.

0 Likes
Message 17 of 21

ArunNDinesh
Enthusiast
Enthusiast

@JacquesGaudin I already copied the print statements. It seems the issue starts from Line 6. Is this working for you?😕

andi4G4Z4_0-1683538754669.png

I am able to open ROBOT application using

import subprocess
file=subprocess.Popen('C:\\Program Files\\Autodesk\\Robot Structural Analysis Professional 2023\\Exe\\robot.exe') and there is no issue. But unable to integrate other functions with ROBOT

 

0 Likes
Message 18 of 21

JacquesGaudin
Advocate
Advocate

What I mean by print statements in the code is to add some lines in the like of ```print('Robot import successful')``` after each line of your script.

 

For example:

 

 

import clr
print('clr imported')

clr.Addreference(<your path>)
print('Reference to Robot added')

import RobotOM
print('Robot imported')
...

 

This will tell you on which line the program stops.

0 Likes
Message 19 of 21

ArunNDinesh
Enthusiast
Enthusiast

Hi @JacquesGaudin Thank you for the clarification. Added the print statement and it seems like line 19 is having the issue. 

andi4G4Z4_0-1683632687022.png

import clr
print('clr imported')
import math
print('math imported')
import sys
print('sys imported')
import csv
print('csv imported')
clr.AddReference('C:\Program Files\Autodesk\Robot Structural Analysis Professional 2023\Exe\Interop.RobotOM.dll')
print('Reference to Robot added')
import RobotOM as rbt
print('Robot imported 1')
from System import Environment
print('imported 2')
from RobotOM import *
print('imported 3')
from System import Object
print('imported 4')
robapp=rbt.RobotApplicationClass()
print('imported 5')
struc=robapp.Project.Structure
print('imported 6')

RobApp  = RobotApplicationClass() # Connect to  Robot Structural Analysis
print('imported 7')
RobProj = RobApp.Project # To Get current project
print('imported 8')
RobStruc = RobProj.Structure # To Get current model
print('imported 9')

node=RobStruc.Nodes
bar=RobStruc.Bars
labels=RobStruc.Labels
stress=RobStruc.Results.Bars.Stresses
force=RobStruc.Results.Bars.Forces

sup = IRobotLabel(labels.Create(RobotOM.IRobotLabelType.I_LT_SUPPORT, 'Sup'))
supData = IRobotNodeSupportData(sup.Data)
supData.UX = 1
supData.UY = 1
supData.UZ = 1
supData.RX = 1
supData.RY = 1
supData.RZ = 1
labels.Store(sup)
0 Likes
Message 20 of 21

JacquesGaudin
Advocate
Advocate

Interesting! I usually get the RobotApplication from:

 

RobApp  = RobotApplication()

 

rather than RobotApplicationClass(). Does that solve the issue?

0 Likes