Dynamo for returning area of AutoCAD/Civil 3D polyline or hatch

Hello all,
I’m trying to get Dynamo to return the area of a hatch or polyline In AutoCAD/Civil3D so that I can use that information in a list for further processing. Additionally, is there a way to accomplish standard ACAD commands like “explode” and such?
Thank you

Which application you’re using Civil 3D or AutoCAD?

Hello!
Civil3D 2020 is what I use with Dynamo (until the inevitable upgrade ha).

@Yellowjacket98 here are 3 options for you using the Civil 3D Toolkit or OOTB nodes.

And here is a Python script if you want to wrap it all up into a single node.

import clr

# Add Assemblies for AutoCAD
clr.AddReference('AcMgd')
clr.AddReference('AcDbMgd')

# Import references from AutoCAD
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.DatabaseServices import *

adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor

def hatch_area(hatches):
	
	output=[]
	obj=[]
	errorReport = None
	
	if not isinstance(hatches,list):
		hatches = [hatches]
	
	global adoc
	
	with adoc.LockDocument():
		with adoc.Database as db:
			with db.TransactionManager.StartTransaction() as t:
				for hatch in hatches:
					handle=hatch.Handle
					oid=(db.GetObjectId(False,Handle(int(handle,16)),0))
					obj=t.GetObject(oid, OpenMode.ForRead)					
					try:
						if isinstance(obj, Hatch):
							area=obj.Area
							output.append(area)							
					# Error handling
					except:
						import traceback
						errorReport = traceback.format_exc()						
				t.Commit()			
	if errorReport == None:
		return output
	else:
		return errorReport
	
OUT = hatch_area(IN[0])
3 Likes

For exploding objects, there is already a node for that.

But for sending other commands to the command line, here’s a quick Python script. The input should be a string just like you would type it in AutoCAD.

import System
from System import *

input = IN[0]

app = System.Runtime.InteropServices.Marshal.GetActiveObject("Autocad.Application")
adoc = app.ActiveDocument

adoc.SendCommand(input + "\n")							

OUT = str(input) + " sent to command line"

Man thanks for that! I ended up getting something very similar to your first posting to work yesterday morning. The good thing is you confirmed my route was pretty close.
But as far as “explode”, I didn’t even type that into the search bar for some reason.
Those are both super helpful. I had tried something similar a few years back with LISP and got very close, but never could get it to treat the hatches properly. Visual scripting just makes it so much easier (plus I can explain it to other folks).
Thanks again!

What impressed me with the Dynamo route was that it properly subtracted regions—my LISP routine would never process a solid hatch and its boundary properly when I was trying to show overlaps and differences. The closest I ever got was using a dot pattern and then ensuring it was to a proper scale so I could explode it and literally count the dots so as to estimate area. Believe it or not it was pretty accurate (at least for small sites), but the more dots, the more bogged down the computer would get. Thus, large acreages would have to be less accurate.

Sounds similar to this.

That is pretty much what my aim was! Sometimes I need previous vs impervious, and other times I need it broken out over more than two coefficients. Early on I had even tried using 3D (flat) surfaces to compare, set at different elevations based on coefficients, but it would combine some of the surfaces (and even putting a max or min triangle length would not help, plus it was extremely complex for what I needed).

What worked for me was selecting the polyline, creating the hatches, and then converting those to regions for the calculations. Then it’s a matter of clearing the items on the hatch list (by layer) if you don’t want them anymore… especially if you need to rerun the calculation later once an area has changed.

The send to command line python script is interesting. Is there a way to send options along with it as well? I tried sending a list of commands but that doesn’t work.

What I am trying to do is: Use your python script, send the : HATCHGENERATEBOUNDARY and send All with it.
Something like:

for i in input:
adoc.SendCommand(i + “\n”)

This didn’t work. Any suggestions?

If you can type it with your keyboard at the command line, then it should be possible to send any command and its options.

2 Likes

Thank you!

this is an excelent little nugget.

1 Like