Community
Civil 3D Customization
Welcome to Autodesk’s AutoCAD Civil 3D Forums. Share your knowledge, ask questions, and explore popular AutoCAD Civil 3D Customization topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Cannot run C# code in Civil(Inexperienced Programmer)

6 REPLIES 6
Reply
Message 1 of 7
Beka_Tchigladze
494 Views, 6 Replies

Cannot run C# code in Civil(Inexperienced Programmer)

Hello,

I want to test some codes for practice I have VS Code I made what to do with this stupid code lots of errors

 

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.Civil.ApplicationServices;
using Autodesk.Civil.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using System.Linq;

namespace MyOffsetNamespace
{
    public class CustomOffsetExample
    {
        [CommandMethod("CustomOffset")]
        public void CustomOffset()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor editor = doc.Editor;
            CivilDocument civDoc = CivilApplication.ActiveDocument;

            // Prompt user for alignment name
            PromptStringOptions alignmentNameOptions = new PromptStringOptions("\nEnter existing alignment name: ");
            PromptResult alignmentNameResult = editor.GetString(alignmentNameOptions);

            if (alignmentNameResult.Status != PromptStatus.OK)
            {
                editor.WriteMessage("\nInvalid input. Offset alignment creation canceled.");
                return;
            }

            string existingAlignmentName = alignmentNameResult.StringResult;

            // Get the existing alignment
            ObjectId alignmentId = ObjectId.Null;
            foreach (ObjectId id in civDoc.GetAlignmentIds())
            {
                Alignment alignment = id.GetObject(OpenMode.ForRead) as Alignment;
                if (alignment != null && alignment.Name == existingAlignmentName)
                {
                    alignmentId = id;
                    break;
                }
            }

            if (alignmentId.IsNull)
            {
                editor.WriteMessage($"\nAlignment '{existingAlignmentName}' not found. Offset alignment creation canceled.");
                return;
            }

            using (Transaction tr = doc.TransactionManager.StartTransaction())
            {
                Alignment alignment = alignmentId.GetObject(OpenMode.ForRead) as Alignment;

                if (alignment == null)
                {
                    editor.WriteMessage("\nError: Unable to access the alignment object.");
                    return;
                }

                // Prompt user for offset distance
                PromptDoubleOptions offsetOptions = new PromptDoubleOptions("\nEnter offset distance (in meters): ");
                offsetOptions.AllowNegative = true;
                PromptDoubleResult offsetResult = editor.GetDouble(offsetOptions);

                if (offsetResult.Status != PromptStatus.OK)
                {
                    editor.WriteMessage("\nInvalid input. Offset alignment creation canceled.");
                    return;
                }

                double offsetDistance = offsetResult.Value;

                try
                {
                    // Create a new alignment
                    ObjectId newAlignmentId = civDoc.Alignments.Add(alignment.Name + "_Offset", alignment.AlignmentType, alignment.LayerId, alignment.StyleId, alignment.LabelSetId);
                    Alignment newAlignment = newAlignmentId.GetObject(OpenMode.ForWrite) as Alignment;

                    // Copy alignment entities to the new alignment with offset
                    foreach (AlignmentEntity entity in alignment.Entities)
                    {
                        if (entity is AlignmentLine line)
                        {
                            Point3d startPoint = new Point3d(line.StartPoint.X, line.StartPoint.Y + offsetDistance, line.StartPoint.Z);
                            Point3d endPoint = new Point3d(line.EndPoint.X, line.EndPoint.Y + offsetDistance, line.EndPoint.Z);
                            AlignmentLine newLine = new AlignmentLine(startPoint, endPoint);
                            newAlignment.Entities.Add(newLine);
                        }
                        else if (entity is AlignmentArc arc)
                        {
                            Point3d startPoint = new Point3d(arc.StartPoint.X, arc.StartPoint.Y + offsetDistance, arc.StartPoint.Z);
                            Point3d endPoint = new Point3d(arc.EndPoint.X, arc.EndPoint.Y + offsetDistance, arc.EndPoint.Z);
                            AlignmentArc newArc = new AlignmentArc(startPoint, endPoint, arc.CenterPoint, arc.Radius, arc.IsClockwise);
                            newAlignment.Entities.Add(newArc);
                        }
                        // Handle other types of AlignmentEntity here
                    }

                    tr.Commit();
                    editor.WriteMessage("\nOffset alignment created successfully.");
                }
                catch (System.Exception ex)
                {
                    editor.WriteMessage($"\nError creating offset alignment: {ex.Message}");
                    tr.Abort();
                }
            }
        }
    }
}

 

 

 

 

Beka Tchigladze
Road/Highway Technical Designer

AEC Collection/C3D 2025
WIN 11 64 PRO
Predator Helios 18, Intel Core i9 14900HX, 32 GB
NVIDIA RTX 4080, 12 GB
YouTube--->Linkedin
6 REPLIES 6
Message 2 of 7

You should provide more details of the errors you get, not just post code to let other to guess what would happen when the code runs.

 

Since you mention you use VS Code, you may also need to be very specific: have you built the code into a DLL file successfully? Have you actually loaded the code into ACAD/C3D and run the command "CustomOffset"? If you were not able to build the code into a DLL, then posting the code is meaningless. Regardless whether you managed to compile the code or not, you SHOULD use Visual Studio, instead of VS Code.

 

Please to be more specific about what the errors/issues you have.

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 7

hi @Beka_Tchigladze 

using vs code as newbie programmer for Autoacad/Civil3d IS Not a good idea, you do alot of works manually such as define target platform and .net version cuz we use .net framework 4.8 or less, as @norman.yuan  said you should use Visual Studio instead, it's much easier.

Message 4 of 7

@Beka_Tchigladze Hello, I've been wondering lately how do I load these C# command lines? I'm familiar with .LSP .VLX with .DLL but not with this line of C# code; where do I paste them so they can work? There are 2 posts on this forum that I would like to use the codes for but I haven't been able to, because I don't know how to use them. Could you help me with that?

Message 6 of 7
joaquin.cajalv
in reply to: hosneyalaa

Thanks for the help, just one question, is it possible to convert the codes to a .DLL file with C# and VB?

Message 7 of 7
hosneyalaa
in reply to: joaquin.cajalv

@joaquin.cajalv 

Better put a question in New topic Add a drawing for the example

 

There are new Method for offset 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Rail Community


 

Autodesk Design & Make Report