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: 

Create OffSet Alignment Error

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
alexisgacia
1064 Views, 3 Replies

Create OffSet Alignment Error

Hi,

 

I am new to Civil 3D programming and Im trying to create an offset alignment but no luck.

 

I'm having error in Civil3D when I try to create an OffSet Alignment using the below code.

 

 

        private void Button3_Click(object sender, EventArgs e)
        {
            CivilDocument civDoc = CivilApplication.ActiveDocument;
            Autodesk.AutoCAD.ApplicationServices.Document AcadDoc= Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Database db = Application.DocumentManager.MdiActiveDocument.Database;

            
            var styleId = civDoc.Styles.AlignmentStyles["Offsets"];
            ss = o.Select_MultipleEntities("Select Alignments:");

            foreach (ObjectId _oID in ss.GetObjectIds())
            {
                Alignment _oEnt = null;
                try
                {

                    using (Transaction tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
                    {
                        _oEnt = (Alignment)_oID.GetObject(OpenMode.ForRead);
if (_oEnt is Alignment)
                        {
                            ObjectId oOffSetID;
                            //ObjectId oOffSetID1= _oEnt.CreateOffsetAlignment(10);
                            //ObjectId oOffSetID = Alignment.CreateOffsetAlignment("Offset_Alignment_12", _oID, 2, styleId  );
                            //ObjectId oOffSetID2 = Alignment.CreateOffsetAlignment(HostApplicationServices.WorkingDatabase,"S",_oEnt.Name, 2, "Offsets");
                            o.SendMessage_ToEditor("\nAlignment Name:" + _oEnt.Name);
                            o.SendMessage_ToEditor("\nAlignment Offset:" + _oEnt.AlignmentType.ToString());
                            o.SendMessage_ToEditor("\nIs Alignment OffSet:" + _oEnt.IsOffsetAlignment);
                            if (!_oEnt.IsOffsetAlignment)
                            {
                                //ObjectId oOffSetID1 = _oEnt.CreateOffsetAlignment(2);
                                oOffSetID = Alignment.CreateOffsetAlignment("Offset_Alignment_1322", _oID, 12, styleId);

                                Alignment offsetAlignment = oOffSetID.GetObject(OpenMode.ForWrite) as Alignment;

                                OffsetAlignmentInfo offsetInfo1 = offsetAlignment.OffsetAlignmentInfo;

                                offsetInfo1.LockToStartStation = true;
                                offsetInfo1.LockToEndStation = true;
                            }

                        }
                    }
                }
                catch (System.Exception ex)
                {
                    o.SendMessage_ToEditor("Error: ==>\n{0}\nTrace: ==>\n{1}" + ex.Message + ex.StackTrace);
                }

 

 

I tried different methods but still giving me error. 

 

1}Value does not fall within the expected range.   at Autodesk.Civil.DatabaseServices.Alignment.InternalCreateOffsetAlignment(AcDbDatabase* pDb, AecRmCString* alignmentName, AcDbObjectId parentAlignmentId, Double offset, AcDbObjectId styleId, Double startStaion, Double endStation)
   at Autodesk.Civil.DatabaseServices.Alignment.CreateOffsetAlignment(String alignmentName, ObjectId parentAlignmentId, Double offset, ObjectId styleId, Double startStation, Double endStation)
   at Autodesk.Civil.DatabaseServices.Alignment.CreateOffsetAlignment(String alignmentName, ObjectId parentAlignmentId, Double offset, ObjectId styleId)

 

 

Labels (1)
3 REPLIES 3
Message 2 of 4
Jeff_M
in reply to: alexisgacia

@alexisgacia Your code is working for me. Some alignments are not able to be offset due to their geometry. Can the alignment you are working with be offset using C3D commands?

 

Why are you using the HostApplicationServices.Database when you already have the Document.Database as the db variable? Note that you didn't add tr.Commit(); so even if the widening completes without error it won't be in the drawing when done.

 

I've found that creating small test commands to make sure my code works before creating the more robust tools which include forms and such really helps to actually cut down on development time. This is one that I created a while back for testing offsets and widenings:

        [CommandMethod("TestOffsetAlign")]
        public void offalign()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var ed = doc.Editor;
            var db = doc.Database;
            var civDoc = CivilApplication.ActiveDocument;
            var entOpts = new PromptEntityOptions("\nSelect an alignment to offset:");
            entOpts.SetRejectMessage("...not an alignment, try again!");
            entOpts.AddAllowedClass(typeof(Alignment), true);
            var entSel = ed.GetEntity(entOpts);
            if (entSel.Status != PromptStatus.OK)
                return;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                var align1 = (Alignment)tr.GetObject(entSel.ObjectId, OpenMode.ForRead);
                var align2id = Alignment.CreateOffsetAlignment("OffsetAlignment", align1.ObjectId, 25.0, align1.StyleId);
                var align2 = (Alignment)tr.GetObject(align2id, OpenMode.ForWrite);
                var oInfo = align2.OffsetAlignmentInfo;
                oInfo.AddWidening(150, 250, 37);
                var t2 = oInfo.Regions[1];
                t2.EntryTransition.TransitionType = Autodesk.Civil.TransitionType.CurveLineCurve;
                CurveLineCurveTransitionDescription entrydesc = (CurveLineCurveTransitionDescription)t2.EntryTransition.TransitionDescription;
                entrydesc.StartStation = 100;
                entrydesc.Length = 50;
                entrydesc.EntryCurveRadius = 15;
                entrydesc.ExitCurveRadius = 15;
                t2.ExitTransition.TransitionType = Autodesk.Civil.TransitionType.Linear;
                LinearTransitionDescription exitdesc = (LinearTransitionDescription)t2.ExitTransition.TransitionDescription;
                exitdesc.StartStation = 250;
                exitdesc.Length = 25;
                tr.Commit();
            }
        }

 

Jeff_M, also a frequent Swamper
EESignature
Message 3 of 4
Jeff_M
in reply to: alexisgacia

Oh, one other thing, the API for offset alignments fails if the parent alignment has a starting station other than 0. Offsetting in C3D does not suffer from this.

Jeff_M, also a frequent Swamper
EESignature
Message 4 of 4
alexisgacia
in reply to: Jeff_M

Hi Jeff,

 

Thank you for your reply.

 

What I did is to add the following code:

using (AcadDoc.LockDocument())
{
using (Transaction tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
{

....

}

}

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

Post to forums  

Rail Community


 

Autodesk Design & Make Report