Python create connected alignment

I know ArkanceSystems (again thaks to @Anton_Huizinga) has a node for this but I want to tinker myself to prototype more functionality.
Its a pain and you have to be careful what python engine to run in, for me on C3D 2024 its IronPython 2.7 version 2.5 that works, I think:) although all it returns is no solution found invalid parameters??

Anyways here is graph and python code, if you get a chance to experiment, the problem is not the code i think its getting correct parameters to work with the geometry, and assistance from the community on getting a template for what works, what does not would be great, thenks.
cjm

Please note I did hard code some parameter values into this as just testing different ideas, so maybe they need changing??

# Load the Python Standard and DesignScript Libraries
import sys
import clr

# Add Assemblies for AutoCAD and Civil3D
clr.AddReference('AcMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AecBaseMgd')
clr.AddReference('AecPropDataMgd')
clr.AddReference('AeccDbMgd')
clr.AddReference('Civil3DNodes')

# Import references from AutoCAD
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *

# Import references from Civil3D
from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *

# Import references for Dynamo for Civil 3D
from Autodesk.Civil.DynamoNodes import Alignment as DynAlignment

# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor
civdoc = CivilApplication.ActiveDocument

firstAlignmentId = IN[0]           # First offset alignment
secondAlignmentId = IN[1]          # Second offset alignment
connectedAlignmentName = IN[2]     # Connected alignment name, str
connectedAlignmentStyle = IN[3]    # Connected alignment style, str
connectedAlignmentLabelSetStyle = IN[4]  # Connected alignment label set style, str
curveRadius = IN[5]                # Curve radius, double
overlapStart = IN[6]               # Overlap on incoming alignment, double
overlapEnd = IN[7]                 # Overlap on outgoing alignment, double
layerName = IN[8]                  # Layer name, str
siteId = ObjectId.Null             # Site ID, assuming it to be null for this example

result = []

def getLayerIdByName(layerName):
    global adoc
    layerId = ObjectId.Null
    
    try:
        with adoc.Database as db:
            with db.TransactionManager.StartTransaction() as t:
                layerTable = t.GetObject(db.LayerTableId, OpenMode.ForRead)
                if layerTable.Has(layerName):
                    layerId = layerTable[layerName]
                t.Commit()
    except System.Exception as ex:
        result.append(str(ex))
    
    return layerId

def createConnectedAlignment(firstAlignmentId, secondAlignmentId, connectedAlignmentName, connectedAlignmentStyle, connectedAlignmentLabelSetStyle, curveRadius, overlapStart, overlapEnd, layerName, siteId):
    global adoc
    global editor
    global civdoc
    
    try:    
        with adoc.LockDocument():
            with adoc.Database as db:
                with db.TransactionManager.StartTransaction() as t:
                    # Fetch the style and label set style objects by name
                    alignmentStyleId = civdoc.Styles.AlignmentStyles[connectedAlignmentStyle].ObjectId
                    labelSetStyleId = civdoc.Styles.LabelSetStyles.AlignmentLabelSetStyles[connectedAlignmentLabelSetStyle].ObjectId
                    layerId = getLayerIdByName(layerName)
                    
                    # Create an instance of ConnectedAlignmentParams
                    connectedAlignmentParams = ConnectedAlignmentParams()
                    connectedAlignmentParams.IncomingParentAlignmentId = firstAlignmentId.InternalObjectId
                    connectedAlignmentParams.OutgoingParentAlignmentId = secondAlignmentId.InternalObjectId
                    #connectedAlignmentParams.ArcInRadius = 5
                    #connectedAlignmentParams.ArcOutRadius = 5
                    connectedAlignmentParams.CurveRadius = curveRadius
                    connectedAlignmentParams.ConnectionOverlapLengthIn = overlapStart
                    connectedAlignmentParams.ConnectionOverlapLengthOut = overlapEnd
                    connectedAlignmentParams.OffsetIn = 0
                    connectedAlignmentParams.OffsetOut = 0
                    connectedAlignmentParams.GreaterThan180=1
                    connectedAlignmentParams.IncomingParentAlignmentStation = 412.34
                    connectedAlignmentParams.IncomingParentAlignmentStation = 57.23
                    # Create the connected alignment
                    connectedAlignmentId = Alignment.CreateConnectedAlignment(
                        connectedAlignmentName,
                        siteId,
                        layerId,
                        alignmentStyleId,
                        labelSetStyleId,
                        connectedAlignmentParams
                    )
                    
                    t.Commit()
                    result.append(connectedAlignmentId)
                    
    except Exception() as ex:
        result.append(ex.message)
    return result

# Assign your output to the OUT variable.
OUT = createConnectedAlignment(firstAlignmentId, secondAlignmentId, connectedAlignmentName, connectedAlignmentStyle, connectedAlignmentLabelSetStyle, curveRadius, overlapStart, overlapEnd, layerName, siteId)

offset_profiles_test.dwg (980.6 KB)
create_connected_alignments_python.dyn (27.1 KB)

Still trying different parameters and its not working, i know i put a couple of parameters wrong such as IncomingParentAlignmentStation twice! so amended that but still no joy, do you need to provide a data point as per the gui tools , if so what is the syntax?? Any help in getting this to work appreciated.
cjm

Ok, for a simple fillet the above works with the modified correct parameters as my last response BUT you have to also have the paramenter

connectedAlignmentParams.CurveGroupType = CurbReturnCurveGroupType.Arc 

then it works.
Also You need to add to references at the start of script: `

from Autodesk.Civil import *

in order to get CurbReturnCurveGroupType.Arc
cjm

Hello,
I really appreciate your efforts and I am really learning from you.

I have tried to mimic your code, so I learn how the code works.
But I keep getting this error:

My script is here

Load the Python Standard and DesignScript Libraries

import sys
import clr

Add Assemblies for AutoCAD and Civil3D

clr.AddReference(‘AcMgd’)
clr.AddReference(‘AcCoreMgd’)
clr.AddReference(‘AcDbMgd’)
clr.AddReference(‘AecBaseMgd’)
clr.AddReference(‘AecPropDataMgd’)
clr.AddReference(‘AeccDbMgd’)

Import references from AutoCAD

from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *

Import references from Civil3D

from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *
from Autodesk.Civil import *

docs assignment

adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor
db = adoc.Database
civdoc = CivilApplication.ActiveDocument

The inputs to this node will be stored as a list in the IN variables.

firstAlignmentId = IN[0] # First offset alignment
secondAlignmentId = IN[1] # Second offset alignment
connectedAlignmentName = IN[2] # Connected alignment name, str
connectedAlignmentStyle = IN[3] # Connected alignment style, str
connectedAlignmentLabelSetStyle = IN[4] # Connected alignment label set style, str
curveRadius = IN[5] # Curve radius, double
overlapStart = IN[6] # Overlap on incoming alignment, double
overlapEnd = IN[7] # Overlap on outgoing alignment, double
layername = IN[8] # Layer name, str
offset = IN[9]
siteId = ObjectId.Null

def GetLayerId(layername):
global adoc
layerId = ObjectId.Null
try:
with adoc.LockDocument():
with db.TransactionManager.StartTransaction() as t:
layerstable = t.GetObject(db.LayerTableId, OpenMode.ForRead)
if layerstable.Has(layername):
layerId = layerstable[layername]
t.Commit()
except Exception as e:
editor.WriteMessage(“\nError: {0}”.format(e))
return layerId

def CreateCurbReturns(firstAlignmentId, secondAlignmentId, connectedAlignmentName, connectedAlignmentStyle, connectedAlignmentLabelSetStyle, curveRadius, offset, layername, siteId):
global adoc
global editor
global civdoc

try:
    with adoc.LockDocument():
        with db.TransactionManager.StartTransaction() as t:
            alignmentStyleId = civdoc.Styles.AlignmentStyles[connectedAlignmentStyle].ObjectId
            labelSetStyleId = civdoc.Styles.LabelSetStyles.AlignmentLabelSetStyles[connectedAlignmentLabelSetStyle].ObjectId
            layerId = GetLayerId(layername)
            
            connectedAlignmentParams = ConnectedAlignmentParams()
            connectedAlignmentParams.IncomingParentAlignmentId = firstAlignmentId
            connectedAlignmentParams.OutgoingParentAlignmentId = secondAlignmentId
            connectedAlignmentParams.CurveRadius = curveRadius
            connectedAlignmentParams.OffsetIn = offset
            connectedAlignmentParams.OffsetOut = offset
            connectedAlignmentParams.ConnectionOverlapLengthIn = overlapStart
            connectedAlignmentParams.ConnectionOverlapLengthOut = overlapEnd
            connectedAlignmentParams.GreaterThan180 = True
            
            connectedAlignmentId = Alignment.CreateConnectedAlignment(
                connectedAlignmentName, siteId, layerId, alignmentStyleId, labelSetStyleId, connectedAlignmentParams
            )
            
            t.Commit()
            return connectedAlignmentId
except Exception as e:
    editor.WriteMessage("\nError: {0}".format(e))
    return None

OUT = CreateCurbReturns(firstAlignmentId, secondAlignmentId, connectedAlignmentName, connectedAlignmentStyle, connectedAlignmentLabelSetStyle, curveRadius, offset, layername, siteId)

Have you faced this problem before?

Hi abr0197236,
Please follow the instructuons I gave in this other thread:
https://forum.dynamobim.com/t/blockreference-add-attributes/56486/8

Regards,
cjm

Try switching to Ironpython, maybe it will be more specific with the issue.

EDIT: oh, I just realized that’s what @cjm suggested, sorry

Hello again,
Thank you for your solution.

Now I am getting this error, have you faced it before?

Script

The inputs to this node will be stored as a list in the IN variables.

dataEnteringNode = IN

adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor
civdoc = CivilApplication.ActiveDocument

connected_alignment_name = IN[0] # First offset alignment
layername = IN[1] # Second offset alignment
connectedAlignmentLabelSetStyle = IN[2] # Connected alignment name, str
connectedAlignmentStyle = IN[3]
First_Alignment_ID= IN[4] # Connected alignment style, str
Second_Alignment_ID = IN[5] # Connected alignment label set style, str
curveRadius = IN[6]
offset = IN[7] # Curve radius, double
overlapStart = IN[8] # Overlap on incoming alignment, double
overlapEnd = IN[9] # Overlap on outgoing alignment, double
First_Alignment_station= IN[10]
Second_Alignment_station=IN[11]
siteId = ObjectId.Null # Site ID, assuming it to be null for this example

result =

def getLayerIdByName(layerName):
global adoc
layerId = ObjectId.Null

try:
    with adoc.Database as db:
        with db.TransactionManager.StartTransaction() as t:
            layerTable = t.GetObject(db.LayerTableId, OpenMode.ForRead)
            if layerTable.Has(layerName):
                layerId = layerTable[layerName]
            t.Commit()
except System.Exception as ex:
    result.append(str(ex))

return layerId

def createConnectedAlignment(First_Alignment_ID, Second_Alignment_ID, connected_alignment_name, connectedAlignmentStyle, connectedAlignmentLabelSetStyle, curveradius, overlapStart, overlapEnd,offset,First_Alignment_station,Second_Alignment_station, layername, siteId):
global adoc
global editor
global civdoc

try:    
    with adoc.LockDocument():
        with adoc.Database as db:
            with db.TransactionManager.StartTransaction() as t:
                # Fetch the style and label set style objects by name
                alignmentStyleId = civdoc.Styles.AlignmentStyles[connectedAlignmentStyle].ObjectId
                labelSetStyleId = civdoc.Styles.LabelSetStyles.AlignmentLabelSetStyles[connectedAlignmentLabelSetStyle].ObjectId
                layerId = getLayerIdByName(layername)
                
                # Create an instance of ConnectedAlignmentParams
                connectedAlignmentParams = ConnectedAlignmentParams()
                connectedAlignmentParams.IncomingParentAlignmentId = First_Alignment_ID.InternalObjectId
                connectedAlignmentParams.CurveGroupType = CurbReturnCurveGroupType.Arc
                connectedAlignmentParams.OutgoingParentAlignmentId = Second_Alignment_ID.InternalObjectId
                connectedAlignmentParams.CurveRadius = curveradius
                connectedAlignmentParams.ConnectionOverlapLengthIn = overlapStart
                connectedAlignmentParams.ConnectionOverlapLengthOut = overlapEnd
                connectedAlignmentParams.OffsetIn = offset
                connectedAlignmentParams.OffsetOut = offset
                connectedAlignmentParams.GreaterThan180=1
                connectedAlignmentParams.IncomingParentAlignmentStation = First_Alignment_station
                connectedAlignmentParams.OutgoingParentAlignmentStation = Second_Alignment_station
                # Create the connected alignment
                connectedAlignmentId = Alignment.CreateConnectedAlignment(
                    connected_alignment_name,
                    siteId,
                    layerId,
                    alignmentStyleId,
                    labelSetStyleId,
                    connectedAlignmentParams
                )
                
                t.Commit()
                result.append(connectedAlignmentId)
                
except Exception() as ex:
    result.append(ex.message)
return result

Assign your output to the OUT variable.

OUT = createConnectedAlignment(First_Alignment_ID, Second_Alignment_ID, connected_alignment_name, connectedAlignmentStyle, connectedAlignmentLabelSetStyle, curveRadius, overlapStart, overlapEnd,offset,First_Alignment_station,Second_Alignment_station, layername, siteId)

ERROR
ARCC and CCC can not be set more than 180

Update,
I have just edited Greaterthan180 to be False.
So, its basically True or False not 0 or 1

Following along with this, not very python savvy but I am learning. I’m having trouble following the different code fragments, did you manage to get one working? I’m very interested in the connected alignment / profile workflow so glad to see some progress has been made. Its a great candidate for automation.