Python cannot call table constructor (Dynamo Node)

Python cannot call table constructor (Dynamo Node)

triekGJKRK
Participant Participant
513 Views
7 Replies
Message 1 of 8

Python cannot call table constructor (Dynamo Node)

triekGJKRK
Participant
Participant

I want to create a table via python

https://help.autodesk.com/view/OARX/2024/ENU/?guid=OARX-ManagedRefGuide-Autodesk_AutoCAD_DatabaseSer...

 

I have the correct library in python to construct a table:

from Autodesk.AutoCAD.DatabaseServices import *

 But these commands both fail:

tbl = Autodesk.AutoCAD.DatabaseServices.Table()
tbl02 = Table()

Python Claims that it does not know what Autodesk is, and running the constructor alone it says there is no public constructor. All examples online say otherwise, I have tried running this Dynamo Python node via IronPython2/3 & Cpython3 to no avail. Any tips as what is wrong with my workspace? Thank you!

0 Likes
Accepted solutions (2)
514 Views
7 Replies
Replies (7)
Message 2 of 8

essam-salah
Advisor
Advisor
Accepted solution

hi @triekGJKRK 

do this instead:

import Autodesk.AutoCAD.DatabaseServices as acDb

 

myTable = acDb.Table()

 

 

 

Message 3 of 8

norman.yuan
Mentor
Mentor

@triekGJKRK  I can reproduce the issue you showed:

in spite of 

from Autodesk.AutoCAD.DatabaseServices import *

tbl = Table()  causes error.

 

But once this line added, as @essam-salah suggested:

import Autodesk.AutoCAD.DatabaseServices as acDb

 and the table creating line changed to

tbl = acDb.Table()

the error is gone away and the Table entity can be created successfully.

 

However, this change (i.e. import Autodesk.AutoCAD.DatabaseServices as acDb) is not needed for creating other entities (DBText, Line...). Do not know why the Dynamo's python script interpreter could not recognize Table with "from Autodesk.AutoCAD.DatabaseServices import *" in place, yet it still recognize other entity classes. Maybe there are other few ones that would behave the same as Table, but with my limited Dynamo learning knowledge, I cannot say.

 

Norman Yuan

Drive CAD With Code

EESignature

Message 4 of 8

triekGJKRK
Participant
Participant

I am so glad you said this I am not going crazy - yes I have noticed this behavior as well! I don't know what causes it but it's good to have this in writing, I should post this in the Dynamo forums!

0 Likes
Message 5 of 8

essam-salah
Advisor
Advisor

@norman.yuan wrote:

 

..

However, this change (i.e. import Autodesk.AutoCAD.DatabaseServices as acDb) is not needed for creating other entities (DBText, Line...). Do not know why the Dynamo's python script interpreter could not recognize Table with "from Autodesk.AutoCAD.DatabaseServices import *" in place, yet it still recognize other entity classes. Maybe there are other few ones that would behave the same as Table, but with my limited Dynamo learning knowledge, I cannot say.


i always use full name (acDb.Line, acDb.DBText, ...) in my code, only for small scripts and and teaching snippets i write Line, DBText, but it is a rule of thumb to reduce ambiguity and run time errors is to use full name (acDb.Line not Line). specially for type like Table it's very common to be exist in other namespaces/modules.

 

import Autodesk.AutoCAD.Runtime as _AcRt              #type:ignore
import Autodesk.AutoCAD.ApplicationServices as _AcAp  #type:ignore
import Autodesk.AutoCAD.EditorInput as _AcEd          #type:ignore
import Autodesk.AutoCAD.DatabaseServices as _AcDb     #type:ignore
import Autodesk.AutoCAD.Geometry as _AcGe             #type:ignore
from Autodesk.AutoCAD.DatabaseServices import DxfCode as typeCode #type:ignore
# Import references from Civil3D
import Autodesk.Civil.ApplicationServices as _AccAp   #type:ignore
import Autodesk.Civil.DatabaseServices as _AccDb      #type:ignore
Message 6 of 8

norman.yuan
Mentor
Mentor
Accepted solution

Very good tip. Thanks.

 

Update:

 

I just realized that since the boiler plate code Dynamo automatically added for the Python Script Node imports both Autodesk.AutoCAD.DatabaseServices and Autodesk.Civil.DatabaseServices:

 

from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *

# Import references from Civil3D
from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *

 therefore, create a namespace alias, as @essam-salah pointed out, is necessary, because both namespace has Table class, thus the error. Obviously, if the Python Script node only deals with AutoCAD, not Civil object, then removing "from Autodesk.Civil.Databases import *" would allow tbl = Table() work.

Norman Yuan

Drive CAD With Code

EESignature

Message 7 of 8

triekGJKRK
Participant
Participant

@norman.yuan  This conversation exaclty, from your truly 😄 This is probably the more correct awnser I will set it as the correct one

Python Node - is the Table class exposed to python? - Civil 3D - Dynamo

0 Likes
Message 8 of 8

triekGJKRK
Participant
Participant

@essam-salah we have come to the core of the problem, which is that we imported two conflicting libraries. See the thread

0 Likes