Control surface triangle length

Can you guide me on how to control triangle length with code block or python script?

1 Like

Hi @manrajan.kalimuthu,

Take a look at the SurfaceBuildOptions class in the Civil 3D API. The properties you’ll want are UseMaximumTriangleLength and MaximumTriangleLength.

2 Likes

How to write in dynamo code block or python script?

Here is some heavily-commented Python code to help you out.

IN[0] = surface object
IN[1] = maximum triangle length value (double)
IN[2] = maximum triangle length toggle (boolean)

# 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

Very readable. Good example Zachri! Thanks for sharing.

This is really informative and a good starting point for beginners like me. Thanks for sharing this.

1 Like

MaximumTrainaglelength v0.01.dyn (11.4 KB)

I tried to use code. it’s not working. can you please correct the dynamo step where i am missing the step.

The Python node in your graph does not contain any of the code that was posted. You need to copy and paste the above code into the Python node.

3 Likes

Hi Zachri,

Thanks for the support. Sorry, i was forgot to save the changes.
It’s working fine.

Huge Generate the code but I get an error does not work
MaximumTrainaglelength.dyn (15.1 KB)
@zachri.jensen

@Christhian can you share a little more info about what exactly isn’t working? It’s hard to tell what is going on without a screenshot or DWG file.

Thanks for the answer I am attaching an image and the file
MaximumTrainaglelength.dyn (15.1 KB)
ARCHIVO.dwg (1.0 MB)


@zachri.jensen

@Christhian make sure you are using the boolean toggle. This controls whether the “Use maximum triangle length” setting is on or off.

Surface

1 Like

It does not work for me and I am using the same documents that I sent you

Uploading: recording.gif…

@zachri.jensen

Ah I see, looks like you are using Civil 3D 2022 with CPython3. In the bottom of the Python script window, try changing the engine to IronPython2.

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

@zachri.jensen Thank you very much It works very well

love your work!

2 Likes

@zachri.jensen thanks for sharing your code, but it only works on single surface not a list of surfaces

Correct. That’s what it was written to do. You’ll need to adjust the code to loop through a list of surfaces.