Batch Surface Profile creation - odd error with ReadDwgFile but works with DocumentManager.Open

Hi,
First - a huge embarrassing apology - the previous topic I posted
(Application alias and DocumentCollectionExtension) … : (
I realized the simple errors the very next day as soon as I got to the office - sorry for wasting posts - it should be deleted.
This topic post explains why I reverted to opening with Document Manager.
We have a folder containing DWG files with Alignment DREFs. They need Surface Profiles and Profile Views.
When I batch process using ReadDwgFile the resulting surface profiles do not have elevations.
But when I batch process using DocumentManager.Open, the results are correct.
What did I miss?
Thanks for your extended patience and guidance.
K.

Here’s the 2 scripts…


import os
import sys
import clr
from System.IO import FileShare
from traceback import format_exc

clr.AddReference('AcMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AecBaseMgd')
clr.AddReference('AecPropDataMgd')
clr.AddReference('AeccDbMgd')

from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *
from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *

tbl = IN[0]
ciset = IN[1] # # # 0 - file name col, 1 - alignment name col, 2 - MSX col, 3 - MSY col
tf = IN[2]
sp = IN[3] # # # 0 = surface name, 1 = layer name, 2 = style name, 3 = label set name

def dwgtask (path,px,py,aname):
    if not os.path.exists(path):
        return False
    adoc = Application.DocumentManager.MdiActiveDocument
    if adoc is None:
        return False
    da = None
    with adoc.LockDocument():
        with Database(False, True) as db:
            db.ReadDwgFile(path, FileShare.ReadWrite, False, "")
            HostApplicationServices.WorkingDatabase = db
            cdoc = CivilDocument.GetCivilDocument(db)
            with db.TransactionManager.StartTransaction() as t:
                pv = cdoc.Styles.ProfileViewStyles.get_Item(sp[4])
                bs = cdoc.Styles.ProfileViewBandSetStyles.get_Item(sp[5])
                aset = cdoc.GetAlignmentIds()
                for id in aset:
                    da = t.GetObject(id,OpenMode.ForWrite)
                    if da.Name == aname:
                        try:
                            spid = Profile.CreateFromSurface(aname + "-EG",cdoc,aname,sp[0],sp[1],sp[2],sp[3])
                            pvid = ProfileView.Create(id, Point3d( px, py, 0.0), aname, bs, pv)
                        except:
                            da = format_exc()
                        break
                t.Commit()
            db.SaveAs(path, DwgVersion.Current)
            HostApplicationServices.WorkingDatabase = adoc.Database
    return True

adoc = Application.DocumentManager.MdiActiveDocument

for i in range(1, len(tbl)):
    fn = tbl[i][ciset[0]]
    if fn:
        nfp = tf + '\\' + fn + '.DWG'
        if os.path.exists(nfp):
            dwgtask(nfp,tbl[i][ciset[2]],tbl[i][ciset[3]],tbl[i][ciset[1]])
HostApplicationServices.WorkingDatabase = adoc.Database
Application.ShowAlertDialog('Script run completed.')


OUT = 0


import os
import sys
import clr
from System.IO import FileShare
from traceback import format_exc

clr.AddReference('AcMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AecBaseMgd')
clr.AddReference('AecPropDataMgd')
clr.AddReference('AeccDbMgd')

from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *
from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *

from Autodesk.AutoCAD.ApplicationServices import DocumentCollectionExtension

tbl = IN[0]
ciset = IN[1] # # # 0 - file name col, 1 - alignment name col, 2 - MSX col, 3 - MSY col
tf = IN[2]
sp = IN[3] # # # 0 = surface name, 1 = layer name, 2 = style name, 3 = label set name

elog = []

def dwgtask (path,px,py,aname):
    if not os.path.exists(path):
        return False
    adoc = DocumentCollectionExtension.Open(dm, path, False)
    dm.CurrentDocument = adoc
    if adoc is None:
        return False
    da = None
    with adoc.LockDocument():
        with adoc.Database as db:
            civdoc = CivilDocument.GetCivilDocument(db)
            with db.TransactionManager.StartTransaction() as t:
                pv = civdoc.Styles.ProfileViewStyles.get_Item(sp[4])
                bs = civdoc.Styles.ProfileViewBandSetStyles.get_Item(sp[5])
                aset = civdoc.GetAlignmentIds()
                for id in aset:
                    da = t.GetObject(id,OpenMode.ForWrite)
                    if da.Name == aname:
                        try:
                            spid = Profile.CreateFromSurface(aname + "-EG",civdoc,aname,sp[0],sp[1],sp[2],sp[3])
                            pvid = ProfileView.Create(id, Point3d( px, py, 0.0), aname, bs, pv)
                        except:
                            elog.append([path,aname,format_exc()])
                        break
                t.Commit()
    if adoc:
        DocumentExtension.CloseAndSave(adoc,path)
    return True

dm = Application.DocumentManager
adoc = Application.DocumentManager.MdiActiveDocument

for i in range(1, len(tbl)):
    fn = tbl[i][ciset[0]]
    if fn:
        nfp = tf + '\\' + fn + '.DWG'
        if os.path.exists(nfp):
            dwgtask(nfp,tbl[i][ciset[2]],tbl[i][ciset[3]],tbl[i][ciset[1]])

dm.CurrentDocument = adoc

Application.ShowAlertDialog('Script run completed.')


OUT = elog

road-model-add-pvs-open.dyn (20.5 KB)
road-model-add-pvs-side.dyn (20.5 KB)

road-design-set.xlsx (15.7 KB)
R10M-R01.DWG (1.6 MB)
R16M-R01.DWG (1.6 MB)
R25M-R01.DWG (1.6 MB)
0525.58.00.Alignment Baseplan.dwg (1.2 MB)
0525.58.00.GZA.02.NGL Ext 8.dwg (1.9 MB)

Referring to the side database method, ReadDwgFile …
After the DWG files have been updated with new Surface Profiles and Profile Views…
RECOVER does not correct the Surface Profiles.
I found a method that does though …
Set the Update Mode to Static and Apply.
Then
Set the Update Mode to Dynamic and Apply.
and Viola!
They have elevations…
Now to add that mode switch in python …