Error when trying to divide part based on reference plane

Error when trying to divide part based on reference plane

kasper.fuglsang.jordt
Explorer Explorer
1,021 Views
7 Replies
Message 1 of 8

Error when trying to divide part based on reference plane

kasper.fuglsang.jordt
Explorer
Explorer

Hello,

I am trying to divide elements into parts programmatically. I have a bit of experience in native Dynamo, but no experience using the Revit API.

 

The following screenshot is a prototype of what I am trying to do.   I would like to divide the part into two divisions based on the reference plane.

kasperfuglsangjordt_3-1641303316867.png

 

Here is the code I have so far:

import clr

clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

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

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('System')
from System.Collections.Generic import List

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

# Collecting element IDs of parts
partElementId = UnwrapElement(IN[0]).Id
partElementId_int = int(partElementId.ToString())
partElementIdList = List[ElementId](partElementId_int)

# Collecting element IDs of ref planes
refPlaneElementId = UnwrapElement(IN[1]).Id
refPlaneElementId_int = int(refPlaneElementId.ToString())
refPlaneElementIdList = List[ElementId](refPlaneElementId_int)

curveArray = []

# Creating XY plane
xyPlane = Plane.CreateByNormalAndOrigin(XYZ(0,0,1), XYZ(0,0,0))

# Creating transaction
TransactionManager.Instance.EnsureInTransaction(doc)

# Dividing part
xyDivisionPlane = SketchPlane.Create(doc, xyPlane)
PartUtils.DivideParts(doc, partElementIdList, refPlaneElementIdList, curveArray, xyDivisionPlane.Id)

# Closing transaction
TransactionManager.Instance.TransactionTaskDone()

OUT = []

(I used the "Select Model Element" node to collect the part and reference plane for simplication purposes)

 

When I execute the script, I get the error:

"Exception: One or more element ids was not permitted for dividing parts. Elements should be parts that are not yet divided and maximum distance from an original has not yet been reached."

 

Can someone explain to me what this error means?

 

Thanks!

 

0 Likes
1,022 Views
7 Replies
Replies (7)
Message 2 of 8

jeremy_tammik
Alumni
Alumni

Does it work when you execute the same steps manually through the end user interface?

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 8

kasper.fuglsang.jordt
Explorer
Explorer

The steps in the script (i.e. 1) collecting parts and reference plane, 2) specifying plane on which to divide part and 3) using the PartUtils method), aren't exactly doable through the UI. 

 

However, I tried the following using the UI, which worked.

1. Select part, press "Divide Parts", press "Edit Sketch".

2. Select "Pick Lines" and pick the reference plane

3. Finish and Finish

 

I suspect that the sketch plane created in the script is the issue, but I don't know how to fix it.

 

0 Likes
Message 4 of 8

RPTHOMAS108
Mentor
Mentor

The equivalent code with the same plane normal and origin works fine in CLR code. Curve array for divisions can also be empty but should be empty IList(Of Curve), not sure [] creates one of those in Python and not some other form of empty list?

 

You are using collections from the system.collections namespace which are type safe. I'm unfamiliar with Python but the lines where you place an integer in such a collection don't look right i.e. don't you have to create a new class of ElementId and place that in the collection not the integer value? ElementId has three constructors one of them is integer but does Python know this and automatically convert integer to ElementId when it goes in the list?

 

However as you are unwrapping the element it gives you Element with it's Id property as an ElementId. So there seems to be no apparent need to convert it to a string and then an integer i.e. just place the ElementId in the collection of ElementId (refPlaneElementId & partElementId are ElementIds).

 

This may not be the issue but I've tried all your values in VB and it works as you would expect it to.

 

 

0 Likes
Message 5 of 8

jeremy_tammik
Alumni
Alumni

Hah! The "sketch plane created in the script is the issue"... I think that may be true.

 

I would suggest adding a regeneration call after creating the sketch plane.

 

If that does not help, I would perform the sketch plane creation in a separate sub-transaction and commit that before proceeding with the next step.

 

Look at the numerous examples demonstrating a need to regenerate:

 

https://thebuildingcoder.typepad.com/blog/about-the-author.html#5.33

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 6 of 8

RPTHOMAS108
Mentor
Mentor

The below changes appear to work:

 

....

# Collecting element IDs of parts
partElementId = UnwrapElement(IN[0]).Id
partElementIdList = List[ElementId]([partElementId])

# Collecting element IDs of ref planes
refPlaneElementId = UnwrapElement(IN[1]).Id
refPlaneElementIdList = List[ElementId]([refPlaneElementId])

curveArray = List[Curve]()

# Creating XY plane
xyPlane = Plane.CreateByNormalAndOrigin(XYZ(0,0,1), XYZ(0,0,0))

# Creating transaction
TransactionManager.Instance.EnsureInTransaction(doc)

# Dividing part
xyDivisionPlane = SketchPlane.Create(doc, xyPlane)
PartUtils.DivideParts(doc, partElementIdList, refPlaneElementIdList, curveArray, xyDivisionPlane.Id)

# Closing transaction
TransactionManager.Instance.TransactionTaskDone()

OUT = []

 

0 Likes
Message 7 of 8

jeremy_tammik
Alumni
Alumni

So, no issue with regeneration, just too much element id massaging?

 

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 8 of 8

RPTHOMAS108
Mentor
Mentor

Yes and there are not many examples outside of Dynamo for explaining how to use .net generic collections in python. Since python has it's own list object (which most the examples explain the use of). Even the examples in Ironpython mostly cover putting primitive types from string literals in lists rather than complex types. 

 

Therefore not sure why the square brackets inside the round brackets is required but I assume someone more familiar with Python knows the reason for it or if there is some better representation for the same.

0 Likes