Edit Wall Profiles by not deleting the exiting walls

import clr

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

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

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

doc = DocumentManager.Instance.CurrentDBDocument

walls = UnwrapElement(IN[0])
w_curves = IN[1]

OUT = []

class SKetchWarningSwallower(IFailuresPreprocessor):
    def PreprocessFailures(self, failuresAccessor):
        # Get all failure messages
        fail_list = failuresAccessor.GetFailureMessages()

        for failure in fail_list:
            fail_id = failure.GetFailureDefinitionId()

            # Check for Room Not Enclosed warning
            if fail_id == BuiltInFailures.SketchFailiures.OpenLoop:
                failuresAccessor.DeleteWarning(failure)

        return FailureProcessingResult.Continue

for wall, curves in zip(walls, w_curves):

    # Ensure sketch exists
    if wall.SketchId == ElementId.InvalidElementId:
        TransactionManager.Instance.EnsureInTransaction(doc)
        sketch = wall.CreateProfileSketch()
        TransactionManager.Instance.TransactionTaskDone()
    else:
        sketch_id = wall.SketchId
        sketch = doc.GetElement(sketch_id)

    # Start SketchEditScope
    ses = SketchEditScope(doc, "Edit Wall Profile")
    ses.Start(sketch.Id)

    TransactionManager.Instance.EnsureInTransaction(doc)

    # ✅ Correct way: delete all elements inside sketch
    for eid in sketch.Profile:
        for c in eid:
            doc.Delete(c.Reference.ElementId)

    # Create new curves
    plane = sketch.SketchPlane

    for c in curves:
        rc = c.ToRevitType()
        doc.Create.NewModelCurve(rc, plane)

    TransactionManager.Instance.TransactionTaskDone()

    # Commit sketch edit
    ses.Commit(SKetchWarningSwallower())

    OUT.append(wall)

I am trying to use this code to update my wall profiles which do not have profiles currently. But I am getting error at ses.Commit() line. Not sure how to resolve this. Can anyone please guide?

I am getting this error.

Interfaces are problematic in the CPython engine - try the PythonNet3 engine (from package manager or integrated as the default in newer builds).

used IronPython, the issue reverted to a transaction problem. On the same Commit Line

this is when I broke the CreateProfileSketch() transaction to one python node and the sketch modification to another.

had the same error with them being in one node. I would like to know if the problem is in my code. I am using the above code.

Tried regenerating the Document, didn’t help

Update: I had to Force Close the Transaction and it worked. But I am still not sure why transaction was not ending before the commit was called.

And it broke again, after running once, now at the start of the editscope.

And it ran successfully again (out of no where - No changes in the code). Why is this happening I would like to know.

OK…so as I said, I had split the code into two Python nodes. I used Transaction.End node after the first one (where I used the CreateProfileSketch() method on walls) and it appears to be working smoothly now.

Hi,

Here is an example that might be useful to you.