Combine two existing corridor into one using Python

I tried this but not working , can you please help

import clr
clr.AddReference('AcMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AeccDbMgd')
import Autodesk.AutoCAD.ApplicationServices as acApp
import Autodesk.AutoCAD.DatabaseServices as acDb
import Autodesk.Civil.DatabaseServices as civDb

def get_corridor(corridor_name):
    """ Function to get a corridor by name """
    doc = acApp.Application.DocumentManager.MdiActiveDocument
    db = doc.Database
    with acDb.TransactionManager.StartTransaction() as tr:
        corridors = civDb.Corridor.GetCorridors(db)
        for corridor in corridors:
            if corridor.Name == corridor_name:
                return corridor
        tr.Commit()
    return None

def combine_corridors(corridor_name1, corridor_name2, new_corridor_name):
    """ Function to combine two corridors into a new one """
    doc = acApp.Application.DocumentManager.MdiActiveDocument
    db = doc.Database
    with acDb.TransactionManager.StartTransaction() as tr:
        corridor1 = get_corridor(corridor_name1)
        corridor2 = get_corridor(corridor_name2)
        
        if not corridor1 or not corridor2:
            raise Exception("One or both corridors not found.")
        
        # Create new corridor
        new_corridor = civDb.Corridor.Create(db, new_corridor_name, acDb.ObjectId.Null)
        
        # Copy baselines and regions from corridor1
        for baseline in corridor1.Baselines:
            new_baseline = new_corridor.Baselines.Add(baseline.AlignmentId, baseline.ProfileId)
            for region in baseline.Regions:
                new_baseline.AddRegion(region.RegionId)
        
        # Copy baselines and regions from corridor2
        for baseline in corridor2.Baselines:
            new_baseline = new_corridor.Baselines.Add(baseline.AlignmentId, baseline.ProfileId)
            for region in baseline.Regions:
                new_baseline.AddRegion(region.RegionId)
        
        # Rebuild the new corridor
        new_corridor.Rebuild()
        
        tr.Commit()
    return new_corridor

# Inputs
corridor_name1 = IN[0] if isinstance(IN[0], str) else None  # First corridor name
corridor_name2 = IN[1] if isinstance(IN[1], str) else None  # Second corridor name
new_corridor_name = IN[2] if isinstance(IN[2], str) else None  # Name of the new combined corridor

# Validate Inputs
if not corridor_name1 or not corridor_name2 or not new_corridor_name:
    raise ValueError("All three inputs (corridor_name1, corridor_name2, new_corridor_name) must be provided as strings.")

# Call function to combine corridors
result = combine_corridors(corridor_name1, corridor_name2, new_corridor_name)

# Output
OUT = result.Name if result else "Error: Corridor creation failed."

I made a script for this a while ago. Have a look at it, it might help you.
Corridor_Merge.dyn (130.1 KB)

similar I use a Corridor_Duplicate to create a copy wich I ca split and cleanup later.
Corridor_Duplicate.dyn (51.8 KB)

I think it is based on C3D 2022 so maybe there are a few bugs to fix before it works.

1 Like

thank you very much

1 Like