AutoCAD Hatch Boundary by Layer

So I got there eventually, the code is butt ugly, it would be great if anyone could help tidy it…

Cheers,

Mark

#LinkDWG Core DYF by Koz Jono YEOH
#kozmosovia@hotmail.com
#Copyright(C) 1994-2018 KozMos Inc.
#Copyright(C) 2011-2018 Neila Heaven Networks
#Cioyright(c) 2017-2018 Tachyon Intelligent Design Institute

import clr
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager

def _GetOrCreateAutoCAD(progid):
    try:
        from System.Runtime.InteropServices import Marshal
        return Marshal.GetActiveObject(progid)
    except:
        try:
            from System import Type, Activator
            t = Type.GetTypeFromProgID(progid)
            return Activator.CreateInstance(t)
        except: pass

def _ValidFile(filename):
    try:
        with open(filename) as f:
            return True
    except IOError:
        return False

UIDOC = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
DWG = UnwrapElement(IN[0])
VIS = IN[1]
RTN = []

if DWG != None:
	LNK=UIDOC.Document.GetElement(DWG.GetTypeId())
	PATH=ModelPathUtils.ConvertModelPathToUserVisiblePath(LNK.GetExternalFileReference().GetAbsolutePath())
	if _ValidFile(PATH):
		RRR = []
		CAD=_GetOrCreateAutoCAD("Autocad.Application")
		CAD.Visible=VIS
		DOC=CAD.Documents.Open(PATH)

		#if i could get select previous to work after I've exploded and joined the regions I wouldn't need any of this next part
		
		#we will eventually make polylines from the regions, so we don't want any in there right now
		#to ensure we can delete polylines, we need at least one in the file... should use an if not but this seems faster not knowing the language!		
		DOC.SendCommand("_pline 0 0,1  ")	
		#lisp! select POLYLINES
		DOC.SendCommand("(ssget \"_X\" \'((0 . \"*POLYLINE\"))) ")	
		
		#delete any polylines in the file, because I can't find a way of selecting them after the join command... not ideal		
		#this selects the section set (SSGET) we just made
		DOC.SendCommand("SELECT (SSGET\"_p\")  ")		
		DOC.SendCommand("DELETE ")


		#lisp! select hatch and create search set
		DOC.SendCommand("(SSGET\"_x\"'((-4 . \"<OR\")(0 . \"HATCH\")(-4 . \"<AND\")(0 . \"INSERT\")(66 . 1)(-4 . \"AND>\")(-4 . \"OR>\"))) ")
		#this selects the section set (SSGET) we just made
		DOC.SendCommand("SELECT (SSGET\"_p\")  ")
		
		#make the outlines of the hatch
		#in python \" means python will not include the \, but it will include the ", without thinking that it's the termination of the quote.
		#find replace is handy for converting from lisp to the version in python!
		#\"p\" tells it to be a polyline
		DOC.SendCommand("(setq SSET (ssget))(setq CNT -1)(while (setq OBJ (ssname SSET (setq CNT (1+ CNT))))(setvar 'clayer (cdr (assoc 8 (entget OBJ))))(command \"-hatchedit\" OBJ \"b\" \"p\" \"y\"))(setvar \"cmdecho\" OLDCE) ")


		#select all polylines, which are now the new polylines.
		DOC.SendCommand("(ssget \"_X\" \'((0 . \"*POLYLINE\"))) ")	
		#this is a new section set (SSGET) we just made
		DOC.SendCommand("SELECT (SSGET\"_p\")  ")	
		
		#this gets the active selection		
		SEL=DOC.PickfirstSelectionSet
		for regionLine in SEL:
			RRR.append([regionLine.Layer, regionLine])
		RTN.append(RRR)	

OUT = RTN
5 Likes