Control surface triangle length

Actually I think it’s an issue between Python 2.x and Python 3.x (my fault). Try leaving the Python engine set as CPython3 and use this code instead:

# The CLR (Common Language Runtime) module needs to be imported to utilize .NET libraries.
import clr

# For this exercise, these are the three AutoCAD assemblies that need to be referenced. The three at the bottom might be required in other cases.
clr.AddReference('AcMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AeccDbMgd')
#clr.AddReference('AcCoreMgd')
#clr.AddReference('AecBaseMgd')
#clr.AddReference('AecPropDataMgd')

# These resources must be imported to access the AutoCAD database. The other three at the bottom might be needed in other cases.
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.DatabaseServices import *
#from Autodesk.AutoCAD.Runtime import *
#from Autodesk.AutoCAD.EditorInput import *
#from Autodesk.AutoCAD.Geometry import *

# These resources must be imported to access the Civil 3D database.
from Autodesk.Civil.DatabaseServices import *
from Autodesk.Civil.ApplicationServices import *

# Get active document
adoc = Application.DocumentManager.MdiActiveDocument
# In this case we don't need the editor because all we're doing is reading records in the database.
#editor = adoc.Editor

# Define a new function
def set_surface_triangle_length(surface, triangleLength, useMaxTriangleLengthToggle):

    global adoc
    # Check if input values are empty
    if not surface or not triangleLength or useMaxTriangleLengthToggle is None:
        return
    # Initiate transaction
    with adoc.LockDocument():
        with adoc.Database as db:
            with db.TransactionManager.StartTransaction() as t:
                # Get the Object ID of the input surface
                surfaceId = surface.InternalObjectId
                # Open the surface object as For Write since we need to make edits
                surf = t.GetObject(surfaceId, OpenMode.ForWrite)
                # Check if object is of type Surface
                if isinstance(surf, Surface):
                    # Get the SurfaceBuildOptions object that encapsulates options applied to building or re-building a Surface
                    buildOptions = surf.BuildOptions
                    # Set the UseMaximumTriangleLength property of the SurfaceBuildOptions class
                    buildOptions.UseMaximumTriangleLength = useMaxTriangleLengthToggle
                    # Set the MaximumTriangleLength property of the SurfaceBuildOptions class
                    buildOptions.MaximumTriangleLength = triangleLength
                    # Rebuild the surface by calling the Rebuild() method
                    surf.Rebuild()
                # Close transaction
                t.Commit()
    return surface		

OUT = set_surface_triangle_length(IN[0], IN[1], IN[2])
6 Likes