Create MText

Hi … I am trying to add my own python script node to create MText objects in Model Space.
The input is a list of Points (output of Points.ByCoordinates Node).
Result: Civil 3D Fatal Error. Operation is not valid due to the state of the object.
Python error warning: No method matches given arguments for OnExit

# Load the Python Standard and DesignScript Libraries
import sys
import clr

# Add Assemblies for AutoCAD and Civil3D
clr.AddReference('AcMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AecBaseMgd')
clr.AddReference('AecPropDataMgd')
clr.AddReference('AeccDbMgd')

# Import references from AutoCAD
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *

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

adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor

def create_mtext(pnts):

    ipnts = pnts
    
    global adoc
    result = False
    
    with adoc.LockDocument():
        with adoc.Database as db:
            with db.TransactionManager.StartTransaction() as t:
                # Place your code below
                # 
                #
                bt = t.GetObject(db.BlockTableId, OpenMode.ForWrite)
                ms = t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite)
                for i in ipnts:
                    with MText() as mt:
                        mt.Location = i
                        mt.Height = 2.5
                        mt.Contents = 'HELLO!'
                        ms.UpgradeOpen()
                        ms.Add(mt)
                        ms.DowngradeOpen()
                        t.AddNewlyCreatedDBObject(mt,True)
                result = True
                # Commit before end transaction
                t.Commit()
    return result

if IN[0] == None:
    OUT = False
else:
    OUT = create_mtext(IN[0])

You are already opening model space ForWrite, so there’s no need to UpgradeOpen() and DowngradeOpen() within the loop.

2 Likes

Thx … I removed those 2 lines … no impact on the error, still the same …

hi
if you use civil 2023 check
from

                #
                bt = t.GetObject(db.BlockTableId, OpenMode.ForWrite)
                ms = t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite)

with

 bt = t.GetObject(db.BlockTableId, OpenMode.ForWrite)
            btrID = bt.get_Item("*Model_Space")
            btr = t.GetObject(btrID, OpenMode.ForWrite)

and
you have points dynamo or point3d

thx … I am still using 2022 . … 2022.2

TRY

# Load the Python Standard and DesignScript Libraries
import sys
import clr

# Add Assemblies for AutoCAD and Civil3D
clr.AddReference('AcMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AecBaseMgd')
clr.AddReference('AecPropDataMgd')
clr.AddReference('AeccDbMgd')

# Import references from AutoCAD
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *

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

adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor

def create_mtext(pnts):

    ipnts = pnts
    
    global adoc
    result = False
    
    with adoc.LockDocument():
        with adoc.Database as db:
            with db.TransactionManager.StartTransaction() as t:
                # Place your code below
                # 
                #
                bt = t.GetObject(db.BlockTableId, OpenMode.ForWrite)
                ms = t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite)
                for i in ipnts:
                    with MText() as mt:
                        mt.Location = Point3d(i.X ,i.Y , i.Z )
                        mt.Height = 2.5
                        mt.Contents = 'HELLO!'
                        #ms.UpgradeOpen()
                        #ms.Add(mt)
                        #ms.DowngradeOpen()
                        ms.AppendEntity(mt)
                        t.AddNewlyCreatedDBObject(mt,True)
                result = True
                # Commit before end transaction
                t.Commit()
    return result

if IN[0] == None:
    OUT = False
else:
    OUT = create_mtext(IN[0])


Here is my very bad DYN - including the updated script you provided …
I was planning to script the MTEXT as I needed more MTEXT creation options
Since then … I have opted to use Text instead of MTEXT and all is working correctly.
Would be interesting to figure out what the problem is …
Thanks for your patience.

draw-grid-mtext-script.dyn (127.8 KB)

Hey!!! I had a chance to try C3D 2023 today … and this works!!! Thank You…

bt = t.GetObject(db.BlockTableId, OpenMode.ForWrite)
            btrID = bt.get_Item("*Model_Space")
            btr = t.GetObject(btrID, OpenMode.ForWrite)
1 Like

… I must have done some type errors … all ok now … working with 2022 as well …