Rebuild Surface in Civil 3D Using Python

Hello everyone,
I’m new to learning Python for Civil 3D and Dynamo, and I’m trying to create a Python script that automatically rebuilds a surface in Civil 3D. However, I’m running into an issue, and the script is not working as expected.

Here’s the code I’ve written so far:

import sys
import clr

clr.AddReference(‘AcMgd’)
clr.AddReference(‘AcCoreMgd’)
clr.AddReference(‘AcDbMgd’)
clr.AddReference(‘AecBaseMgd’)
clr.AddReference(‘AecPropDataMgd’)
clr.AddReference(‘AeccDbMgd’)
clr.AddReference(‘AutoCADNodes’)

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 *

from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *

from Autodesk.AutoCAD.DynamoNodes import *

doc = Application.DocumentManager.MdiActiveDocument
ed = doc.Editor

dataEnteringNode = IN
surfaceName = ‘1’
surface = None
SurfaceCollection = IN[0]

if SurfaceCollection == surfaceName:
    surface = s
    
    break

Ensure we found the surface

#if surface is None:#

raise Exception(f"Surface with name ‘{surfaceName}’ not found.")#

Rebuild the surface

try:
surface.Rebuild()
print(f"Surface ‘{surfaceName}’ has been successfully rebuilt.“)
except Exception as e:
print(f"Error while rebuilding surface: {str(e)}”)

Add dynamo file

1 Like

what is your issue? could you reformat the post to see the formatted code?
Also as @hosneyalaa suggested the best would be to share the dynamo file, if you are able to.

1 Like

I’m very sorry but because I’m a new member here , it is not allowed for me to upload files .
So i uploaded the dynamo file on We Transfer , here is the link

1 Like

try

import sys
import clr

clr.AddReference('AcMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AeccDbMgd')
clr.AddReference('ProtoGeometry')

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 System.Collections.Generic import Dictionary

adoc = Application.DocumentManager.MdiActiveDocument
cdoc = CivilApplication.ActiveDocument

from Autodesk.DesignScript.Geometry import *


def get_sample_line_info(SurfaceCollection , surfaceName):
	
	global adoc
	global cdoc
	
	output = []
	

	if not SurfaceCollection:
		return
	
	if not isinstance(SurfaceCollection, list):
		sampleLines = [SurfaceCollection]

	with adoc.LockDocument():
	    with adoc.Database as db:
	        with db.TransactionManager.StartTransaction() as t:
				for Surfac in SurfaceCollection:
					vals = []
					
					sampleLineId = Surfac.InternalObjectId
					objSurfac = t.GetObject(sampleLineId, OpenMode.ForRead)
					
					if objSurfac == surfaceName:
						objSurfac.Rebuild()
						
						
				t.Commit()
	return objSurfac 		

OUT = get_sample_line_info(IN[0], IN[1] )

I’m really thankful , but unfortunately this code is not rebuilding the surface

REPLACE

WITH

objSurfac = t.GetObject(sampleLineId, OpenMode.ForWrite)