Creating Civil 3D Alignment in .Net throws “Alignment ID is invalid” error

Home / Uncategorized / Creating Civil 3D Alignment in .Net throws “Alignment ID is invalid” error

Question:
I am developing an IFC (Industry Foundation Classes) Import/Export Add-In for Civil 3D, which I will publish as open source later this month). The export function works completely fine already. However, I still don’t quite understand how to create objects in Civil 3D using .NET. My add-in is written in C#.

I tried the following, which is a official Autodesk example:// Uses an existing Alignment Style named "Basic" and Label Set Style named "All Labels" (for example, from
// the _AutoCAD Civil 3D (Imperial) NCS.dwt template. This call will fail if the named styles
// don’t exist.
// Uses layer 0, and no site (ObjectId.Null)
ObjectId testAlignmentID = Alignment.Create(doc, "New Alignment", ObjectId.Null, "0", "Basic", "All Labels");

source: https://knowledge.autodesk.com/support/autocad-civil-3d/learn-explore/caas/CloudHelp/cloudhelp/2017/ENU/Civil3D-DevGuide/files/GUID-F620DF41-7DF3-450F-8C2A-A92DEB1F9E9E-htm.html

However, whenever I try to run my code, I get the following error message: "Invalid Alignment ID.". My code looks like the following:var civilDatabase = Application.DocumentManager.MdiActiveDocument.Database;
var civilDocument = CivilApplication.ActiveDocument;
using (Transaction civilTransactionManager =
civilDatabase.TransactionManager.StartTransaction())
{
ObjectId civilAlignment = Alignment.Create(civilDocument, "MyName", "" , "0", "Basic", "All Labels");

I also tried to replace the "" that gives the site for the Alignment with null or ObjectID.Null, both does not work and replacing it with ObjectID.Null does even prevent me from compiling.

Anyone knows where that error comes from?


Answer:
Looking at the documentation for Alignment methods, more specifically the following overload:public static ObjectId Create(
CivilDocument document,
string alignmentName,
string siteName,
string layerName,
string styleName,
string labelSetName
)

It says:

System.ArgumentException

The name of the drawing, layer, style, labelSet or site is invalid. The name of the alignment already exists.

So it seems some of the names are not correct. For a more robust approach, you may list the styles and get a name or objectId from there. For a siteless alignment, you can pass string.empty as siteName parameter.
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *