Offset Alignment start and End station

I am trying to update the existing offset alignment python code for start and end station but with limited knowledge of coding and API its difficult to get this going. it would be really great if i can get some help in updating code for this function. Appreciate all the support

Things i find so far


Autodesk Civil 3D Help | LockToStartStation Property | Autodesk

2 Likes

Thanks @Assem.Daaboul for the code.

Have updated the code to Create offset alignment and add a widening to it between the start and end station. An attempt to understand the code and combine it from the one of the post to add widening code. Suggestions and guidance are welcome for improvement.
"# 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 import *
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

align = IN[0]
alignmentstyle = IN[1]
offset = IN[2]
startstation = IN[3]
endstation = IN[4]
result =

def cmoalignments(align,alignmentstyle,offset):
global adoc
global editor
global civdoc

try:
    with adoc.LockDocument():
        with adoc.Database as db:
            with db.TransactionManager.StartTransaction() as t:
                alignmentId = align.InternalObjectId
                al = t.GetObject(alignmentId, OpenMode.ForRead)
                alname = al.Name
                bbsz = "TEST1"
                jbsz = "JBSZ"
                lso = offset * -1
                # for ease of code reading
                # Left offset Alignment
                leftoffsetalname = alname + "-" + bbsz
                leftoffsetId = Alignment.CreateOffsetAlignment(db,leftoffsetalname,alname,lso,alignmentstyle,startstation,endstation)
                alignid = t.GetObject(leftoffsetId, OpenMode.ForWrite, True);
                oInfo = alignid.OffsetAlignmentInfo;
                oInfo.AddWidening(110, 165, 4.6);
                r = oInfo.Regions[1];
                rent = r.EntryTransition;
                rent.TransitionType = TransitionType.Linear;
                entrydesc = r.EntryTransition.TransitionDescription;
                entrydesc.Length = 25;
                rext = r.ExitTransition;
                #rext.TransitionType(2);
                exitdesc = r.ExitTransition.TransitionDescription;
                exitdesc.Length = 45;
                result.append(leftoffsetalname)
                t.Commit()
except Exception() as ex:
    result.append(ex.message)
return result

Assign your output to the OUT variable.

OUT = cmoalignments(align,alignmentstyle,offset)"

@Assem.Daaboul this code is working fine in Civil 3D 2020, but getting bellow error when run in 2022 version. any way to resolve this error for 2022 ?

tried switching between Python 2 and Python 3 but it didnt helped much.
image

@shahid.shaikh_C3D, you are right. there are some differences between CPython3 and IronPython 2.7. I’ve managed to run the script in Civil 3D 2022 by updating the try-except. Check the attached graph and make sure to use CPython3.
image

al_.dyn (93.1 KB)

1 Like

I am really sorry to disturb you @Assem.Daaboul for this but if i change the start and end of the alignment the code is giving bellow error.

can we add any line in the code to get it working even if its same alignment and only other parameters are changed. for ex. Start station, End station or Offset distance etc.

Change name alignment

Before any test

As @hosneyalaa mentioned you can change the offset alignment names before running the script, assuming you want to keep those offset alignments and create new ones.

In case you just want to recreate the offset alignment on each run, you can delete the previous offset alignments and create new ones. Below is the additional code to do this.

                    # check if alignments exist and delete
                    ofstAlIds = al.GetChildOffsetAlignmentIds()
                    for ofstAlID in ofstAlIds:
                        ofstAl = t.GetObject(ofstAlID, OpenMode.ForWrite)
                        if ofstAl.Name.lower() == leftoffsetalname.lower() or ofstAl.Name.lower() == rightoffsetalname.lower():
                            ofstAl.Erase()

I’m attaching the graph as well.
al_.dyn (93.6 KB)

2 Likes