Convert multiple lwpolylines into multiple alignments (C#)

How to convert multiple lwpolylines into multiple alignments was a question in the Autodesk Civil 3D Customization forum. The original code was in VB.NET, but had some problems with the transactions, as I am more confident in C#, I was providing first the solution in C#.

The first part is just to create a selection with polylines only. The main part is encapsuled in a transaction. Inside this transaction it is ensured that a valid layer, an alignment style and an alignment label style exist, options are setted and than each polyline of the selection is converted to an alignment with a unique name.

// (C) Copyright 2018 by  
// Lu An Jie (Andreas Luka)
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;

using Autodesk.Civil;
using Autodesk.Civil.ApplicationServices;
using Autodesk.Civil.DatabaseServices;
using Autodesk.Civil.Settings;


// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(ALC_AlignmentFromPolyLine.MyCommands))]

namespace ALC_AlignmentFromPolyLine
{

    public class MyCommands
    {

        // Modal Command 
        [CommandMethod("MyAlignmentsFromPolylines", CommandFlags.Modal)]
        public void MyAlignmentsFromPolylines() // This method can have any name
        {
            // Get the current document and database
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            CivilDocument civdoc = CivilApplication.ActiveDocument;

            // Build a filter list so that only polylines can be selected
            TypedValue[] listTV = new TypedValue[1] { new TypedValue((int)DxfCode.Start, "LWPOLYLINE") };
            SelectionFilter filter = new SelectionFilter(listTV);
            PromptSelectionOptions pso = new PromptSelectionOptions();
            pso.MessageForAdding = "Select polylines: ";

            // Get thea selection
            PromptSelectionResult result = ed.GetSelection(pso, filter);

            if (result.Status == PromptStatus.OK)
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    try
                    {
                        // Get id  of layer "alignments" or use id of current layer  
                        ObjectId idLayer = db.Clayer;
                        LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                        if (lt.Has("alignments")) idLayer = (lt["alignments"]);

                        // Get id  of the 1st Alignment style or Basic
                        ObjectId idStyle = civdoc.Styles.AlignmentStyles[0];
                        if (civdoc.Styles.AlignmentStyles.Contains("Basic")) idStyle = civdoc.Styles.AlignmentStyles["Basic"];

                        // Get id of the 1st AlignmentLabelSetStyle or Basic
                        ObjectId idLabelSet = civdoc.Styles.LabelSetStyles.AlignmentLabelSetStyles[0];
                        if (civdoc.Styles.LabelSetStyles.AlignmentLabelSetStyles.Contains("Basic")) idLabelSet = civdoc.Styles.LabelSetStyles.AlignmentLabelSetStyles["Basic"];


                        SelectionSet ss = result.Value;

                        // Step through the objects in the selection set
                        foreach (SelectedObject sob in ss)
                        {

                            // Check list is not empty & object is from the expected type
                            if (sob != null && sob.ObjectId.ObjectClass.DxfName == "LWPOLYLINE")
                            {

                                // Set options
                                PolylineOptions plos = new PolylineOptions();
                                plos.AddCurvesBetweenTangents = true;
                                plos.EraseExistingEntities = true;
                                plos.PlineId = sob.ObjectId;


                                // Create unique name
                                String nAlign = Alignment.GetNextUniqueName(civdoc.Settings.GetSettings<SettingsCmdCreateAlignmentEntities>().DefaultNameFormat.AlignmentNameTemplate.Value);

                                // Create alignment
                                ObjectId idAlign = Alignment.Create(civdoc, plos, nAlign, ObjectId.Null, idLayer, idStyle, idLabelSet);
                                ed.WriteMessage("\nAlignment created with id: {0}", idAlign);
                            }
                        }

                        // Save the new objects to the database
                        tr.Commit();
                    }
                    catch (Autodesk.AutoCAD.Runtime.Exception ex)
                    {
                        ed.WriteMessage("/n Exception message :" + ex.Message);
                    }
                }
            }
            else
            {
                ed.WriteMessage("No Polylines selected");
            }
        }
    }
}