DivideParts Method

Hi, I’m trying to do something that might seem simple (cut a wall into two parts), but I’m stuck on the DivideParts method of the PartUtils class.
I think I’m correct for the cut line and sketchplane. The rest, I don’t really understand why I’m getting an error message.

Script Python

import sys
import clr
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import HostObjectUtils,ShellLayerType,SketchPlane,XYZ,Line,PartUtils,ElementId,CurveArray

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.GeometryConversion)

import System
from System.Collections.Generic import List

clr.AddReference("RevitServices")
import RevitServices

from RevitServices.Transactions import TransactionManager


def findpts(f):
    lpts=[]
    clt=f.GetEdgesAsCurveLoops()
    for cl in clt:
        for c in cl:
            lpts.append(c.GetEndPoint(0))
    return lpts


mur=UnwrapElement(IN[0])
ht=IN[1]
lc=mur.Location.Curve
leid=List[ElementId]()
leid.Add(mur.Id)
doc=mur.Document
refplane=HostObjectUtils.GetSideFaces(mur,ShellLayerType.Exterior)[0]
faceext=mur.GetGeometryObjectFromReference(refplane)

TransactionManager.Instance.EnsureInTransaction(doc)
skplane=SketchPlane.Create(doc,refplane)
doc.Regenerate()
PartUtils.CreateParts(doc,leid)
doc.Regenerate()

pext=findpts(faceext)
pstartloc=lc.GetEndPoint(0)
newp=[XYZ(p.X,p.Y,pstartloc.Z) for p in pext]
np2=sorted(newp,key=lambda p:(p.X))
pd=XYZ(np2[0].X,np2[0].Y,np2[0].Z)
pf=XYZ(np2[-1].X,np2[-1].Y,np2[-1].Z)
orient=XYZ(pf.X-pd.X,pf.Y-pd.Y,pf.Z-pd.Z).Normalize()
pstart=pd.Add(orient.Multiply(-1/0.3048))
pend=pf.Add(orient.Multiply(1/0.3048))
linecut=Line.CreateBound(XYZ(pstart.X,pstart.Y,pstart.Z+ht/0.3048),XYZ(pend.X,pend.Y,pend.Z+ht/0.3048))
lcur=CurveArray()
lcur.Append(linecut)
intersectionElementsIds = List[ElementId]()

parts = PartUtils.GetAssociatedParts(doc, mur.Id,True, True)
partDivide = PartUtils.DivideParts(doc, parts, intersectionElementsIds, lcur, skplane.Id)

TransactionManager.Instance.TransactionTaskDone()


OUT=partDivide

Thanks in advance, kind regards,
christian.stan

hm.. i know GetAssociatedParts returns a ICollection but seemingly it’s been converted to a cpython list inexplicitly. converting it back fixes the problem for me. might have something to do with the current implementation with pythonnet.

its fine with ironpython though.

3 Likes

Hi, I followed your advice,
I changed like this.

lcur=CurveArray()
lcur.Append(linecut)
intersectionElementsIds = List[ElementId]()

lcurv=List[Curve]()
lcurv.Add(linecut)

parts = PartUtils.GetAssociatedParts(doc, mur.Id,True, True)
partDivide = PartUtils.DivideParts(doc, List[ElementId](parts), intersectionElementsIds, lcurv, skplane.Id)
doc.Regenerate()

TransactionManager.Instance.TransactionTaskDone()


OUT=PartUtils.GetAssociatedParts(doc, mur.Id,True, True)

Problem solved,thank you very much.
Sincerely,
Christian.stan

2 Likes

this conversion is specific to PythonNet2

as of PythonNet3, this conversion has been removed

so we don’t have to reconvert lists as in the present case, and what’s more, you can use LINQ extension methods (feature added by Team Dynamo).

Resource

3 Likes