1

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?

FlixFix
  • 63
  • 9

2 Answers2

0

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.

Augusto Goncalves
  • 8,493
  • 2
  • 17
  • 44
0

Thank you for your answer Augusto! I thought so too, because the error did point into that direction.

Hoever, Jeff from Autodesk was able to help me and provided a working solution. Apparently this was part of a bigger problem with me main code, or the way I approached things. With Jeff's solution provided in the following post, I got everything to work:

https://forums.autodesk.com/t5/autocad-civil-3d-customization/creating-alignment-throws-quot-alignment-id-is-invalid-quot/m-p/7302387/highlight/false#M13831

FlixFix
  • 63
  • 9