# Create MText

**URL:** <https://forum.dynamobim.com/t/create-mtext/81236>\
**Category:** Civil 3D\
**Tags:** python, autocad\
**Created:** [September 20, 2022, 10:50am UTC](https://forum.dynamobim.com/t/create-mtext/81236 "2022-09-20T10:50:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kbzawork](https://avatars.discourse-cdn.com/v4/letter/k/d26b3c/32.png) [@kbzawork](https://forum.dynamobim.com/u/kbzawork)\
**Post date:** [September 20, 2022, 10:50am UTC](https://forum.dynamobim.com/t/create-mtext/81236/1 "2022-09-20T10:50:22Z")

</div>

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

```auto
# 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])

```

---

<div class="post-metadata">

**Author:** ![mzjensen](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@mzjensen](https://forum.dynamobim.com/u/mzjensen)\
**Post date:** [September 20, 2022, 1:13pm UTC](https://forum.dynamobim.com/t/create-mtext/81236/2 "2022-09-20T13:13:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kbzawork](https://avatars.discourse-cdn.com/v4/letter/k/d26b3c/32.png) [@kbzawork](https://forum.dynamobim.com/u/kbzawork)\
**Post date:** [September 21, 2022, 7:13am UTC](https://forum.dynamobim.com/t/create-mtext/81236/3 "2022-09-21T07:13:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hosneyalaa](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/hosneyalaa/32/100865_2.png) [@hosneyalaa](https://forum.dynamobim.com/u/hosneyalaa)\
**Post date:** [September 21, 2022, 10:36am UTC](https://forum.dynamobim.com/t/create-mtext/81236/4 "2022-09-21T10:36:23Z")

</div>

hi  
if you use civil 2023 check  
from

> [@"TypeError" When Trying to Get Block Table Record of Model Space](https://forum.dynamobim.com/t/typeerror-when-trying-to-get-block-table-record-of-model-space/77729/6):
>
> Okay. I see that bt.get\_Item(BlockTableRecord.ModelSpace) or bt.get\_Item("\*Model\_Space") returns an object ID, not the block table reference its self, so I have to pass that into: t.GetObject() This works: with adoc.LockDocument(): with adoc.Database as db: with db.TransactionManager.StartTransaction() as t: bt = t.GetObject(db.BlockTableId, OpenMode.ForWrite) btrID = bt.get\_Item("\*Model\_Space") btr = t.GetObject(btrID, OpenMode.ForWrite) …

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

```

with

```auto
 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

---

<div class="post-metadata">

**Author:** ![kbzawork](https://avatars.discourse-cdn.com/v4/letter/k/d26b3c/32.png) [@kbzawork](https://forum.dynamobim.com/u/kbzawork)\
**Post date:** [September 21, 2022, 12:00pm UTC](https://forum.dynamobim.com/t/create-mtext/81236/5 "2022-09-21T12:00:52Z")

</div>

thx … I am still using 2022 . … 2022.2

---

<div class="post-metadata">

**Author:** ![hosneyalaa](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/hosneyalaa/32/100865_2.png) [@hosneyalaa](https://forum.dynamobim.com/u/hosneyalaa)\
**Post date:** [September 21, 2022, 12:15pm UTC](https://forum.dynamobim.com/t/create-mtext/81236/6 "2022-09-21T12:15:41Z")

</div>

TRY

```auto
# 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])

```

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/f/f/ffd84b77008b41a8366df6a798041b1627f556f9.png)

---

<div class="post-metadata">

**Author:** ![kbzawork](https://avatars.discourse-cdn.com/v4/letter/k/d26b3c/32.png) [@kbzawork](https://forum.dynamobim.com/u/kbzawork)\
**Post date:** [September 22, 2022, 6:08am UTC](https://forum.dynamobim.com/t/create-mtext/81236/7 "2022-09-22T06:08:39Z")

</div>

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](https://forum.dynamobim.com/uploads/short-url/kk0952XjcIOHk79Hv83SP7NAlc0.dyn) (127.8 KB)

---

<div class="post-metadata">

**Author:** ![kbzawork](https://avatars.discourse-cdn.com/v4/letter/k/d26b3c/32.png) [@kbzawork](https://forum.dynamobim.com/u/kbzawork)\
**Post date:** [September 28, 2022, 8:06am UTC](https://forum.dynamobim.com/t/create-mtext/81236/9 "2022-09-28T08:06:17Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![kbzawork](https://avatars.discourse-cdn.com/v4/letter/k/d26b3c/32.png) [@kbzawork](https://forum.dynamobim.com/u/kbzawork)\
**Post date:** [September 28, 2022, 8:10am UTC](https://forum.dynamobim.com/t/create-mtext/81236/10 "2022-09-28T08:10:45Z")

</div>

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