CreateOffsetAlignment Python script Fatal Error

Hi,
I started to create a script using CreateOffsetAlignment Method (Database, String, String, Double, String). Something in the code causing C3D fatal error, but interestingly it crashes even if I comment out everything leaving only one line in the block. Tried it with every line but I cannot find the problem. Other scripts are working, tried it with multiple drawings in C3D 2021.2.1 and 2020.6, same result.
image
multipleoffsetalignments.dyn (13.8 KB)
Thanks in advance.

Hi @kovacsv,

I also got a Fatal Error when trying to run your script. attached is a modified script that does not crash Civil 3D :slight_smile:

I tried to check where your script is causing Civil 3D to crash, but it is still a mystery to me.
multipleoffsetalignments.dyn (19.1 KB)

4 Likes

Thank you, @Assem.Daaboul. I wanted to try the other CreateOffsetAlignment methods, but your version gives the desired result so that will work for me :smile: , thanks.

Hi @Assem.Daaboul , do you any idea if it is possible to add the offset profiles as well? Can we access that option it via the API?

Thanks everyone! Great content in this forum!

Hi @Tiago.Caldeira,

I believe that you are referring to the option of creating offset profiles while creating the offset alignments similar to the below, right?


I’m not aware of a method to create the offset profiles while creating offset alignments. I’ve checked the documentation [here] (CreateOffsetAlignment Method) with no luck. Maybe you should create a new question for this?

Hey Guys,
I extended the code with offset profile. It is in the 2021.1 API guide.
https://help.autodesk.com/view/CIV3D/2021/ENU/?guid=176ac8e5-a883-70d8-5617-015c9d466419
Needs some refinement (for e.g. making sure the right parent profile is used, and error massages) but does the job.

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

align = IN[0]
alignmentstyle = IN[1]
offset = IN[2]
offsetprofilestyle = IN[3]
slope = IN[4]

result = []

def cmoalignments(align,alignmentstyle,offset,offsetprofilestyle,slope):
	global adoc
	global editor
	global civdoc
	
	try:	
		with adoc.LockDocument():
			with adoc.Database as db:
				with db.TransactionManager.StartTransaction() as t:
					slopepercent = slope/100
					n = align.Count
					for i in range(0,n):
						# get parent alignments to offset
						alignment = align[i]
						alignmentId = alignment.InternalObjectId
						al = t.GetObject(alignmentId, OpenMode.ForRead)
						alname = al.Name
						# offset alignment name suffix and side variables
						bbsz = "BBSZ"
						jbsz = "JBSZ"
						lso = offset * -1
						rso = offset * 1 
						# get parent profile
						profileId = al.GetProfileIds()[0]
						profile = t.GetObject(profileId, OpenMode.ForWrite)
						profilename = profile.Name
						# Left offset Alignment
						leftoffsetalname = alname + " " + bbsz
						leftoffsetId = al.CreateOffsetAlignment(lso)
						leftoffsetal = t.GetObject(leftoffsetId, OpenMode.ForWrite)
						leftoffsetal.Name = leftoffsetalname
						leftoffsetal.StyleName  = alignmentstyle
						result.append(leftoffsetalname)
						# Left offset Alignment Profile
						loprofile = profile.CreateOffsetProfileBySlope(leftoffsetalname,leftoffsetalname,offsetprofilestyle,slopepercent)
						# Right Offset Alignment
						rightoffsetalname = alname + " " + jbsz
						rightoffsetId = al.CreateOffsetAlignment(rso)
						rightoffsetal = t.GetObject(rightoffsetId, OpenMode.ForWrite)
						rightoffsetal.Name = rightoffsetalname
						rightoffsetal.StyleName  = alignmentstyle
						result.append(rightoffsetalname)
						# Right offset Alignment Profile
						roprofile = profile.CreateOffsetProfileBySlope(rightoffsetalname,rightoffsetalname,offsetprofilestyle,slopepercent)				
					t.Commit()

	except Exception() as ex:
		result.append(ex.message)
	return result

# Assign your output to the OUT variable.
OUT = cmoalignments(align,alignmentstyle,offset,offsetprofilestyle,slope)
5 Likes

@Assem.Daaboul Could you help me? how can I use the method with start and end station. I modified your code and i got “Exceptione: eDegeneratedGeometry” at last.

# 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

palign = IN[0]
offset = IN[1]
alignmentstyle = IN[2]
start = IN[3]
end = [4]

def cmoalignments(palign,offset,alignmentstyle,start,end):
    global adoc
    global editor
    global civdoc
    
    try:
        with adoc.LockDocument():
            with adoc.Database as db:
                with db.TransactionManager.StartTransaction() as t:
                    alignmentId = palign.InternalObjectId
                    al = t.GetObject(alignmentId, OpenMode.ForRead)
                    alname = al.Name
                    startname = "start"
                    endname = "end"
                    lso = offset * -1
                    rso = offset * 1 # for ease of code reading
                    # Left offset Alignment
                    offsetalname = alname.join(startname)
                    offsetId = al.CreateOffsetAlignment(offset)
                    offsetal = t.GetObject(offsetId, OpenMode.ForWrite)
                    offsetal.Name = offsetalname
                    offsetal.StyleName  = alignmentstyle
                    offsetal.StartingStation = start
                    offsetal.EndingStation = end
                    result.append(offsetalname)
                    t.Commit()
    except Exception() as ex:
        result.append(ex.message)
    return result

# Assign your output to the OUT variable.
OUT = cmoalignments(palign,offset,alignmentstyle,start,end)


I think here IN[4] should be there.

When i correct that and run the code i get bellow error.
image

Hi, I was on vacation (Lucky me :slight_smile: ) will check and get back to you

Hi,

Below is a quick (probably requires a lot of cleaning) solution to use the start and the end station while creating offset alignment. I’ve used the Alignment.CreateOffsetAlignment Method (database, offset alignment name, parent alignment name, offset, style, start station, end station) to create the offset alignments. Note the additional import [from Autodesk.Civil import *]

# 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 = "BBSZ"
                    jbsz = "JBSZ"
                    lso = offset * -1
                    rso = offset * 1 # for ease of code reading
                    # Left offset Alignment
                    leftoffsetalname = alname + "-" + bbsz
                    leftoffsetId = Alignment.CreateOffsetAlignment(db,leftoffsetalname,alname,lso,alignmentstyle,startstation,endstation)
                    result.append(leftoffsetalname)
                    # Right Offset Alignment
                    rightoffsetalname = alname + "-" + jbsz
                    rightoffsetId =  Alignment.CreateOffsetAlignment(db,rightoffsetalname,alname,rso,alignmentstyle,startstation,endstation)
                    result.append(rightoffsetalname)
                    t.Commit()
    except Exception() as ex:
        result.append(ex.message)
    return result

# Assign your output to the OUT variable.
OUT = cmoalignments(align,alignmentstyle,offset)
1 Like

Hi @Assem.Daaboul
عيدكم مبارك

Thanks for solution

1 Like

Thank You, that works perfectly