Ad

Sunday, September 27, 2020

Corridor Gaps

Civil 3D doesn't always build every corridor section you might need. This is because where regions meet, if they have nearly the same station, then it won't draw the first section in the next region. To get a section at the adjacent regions you need to add a gap. This can be troublesome if you have lots of regions. The code below will prompt a user to select a corridor, enter a minimum gap distance, and then if the existing gap is less than the entered value the start station is modified to get the minimum gap. If the existing gap is larger than the minimum value then no change is made.

The code could be changed to save the last entered value and then make it the default value. This code can be found by using your favorite search engine.

Feel free to use this code. 

    public class CorridorGaps
    {
        [CommandMethod("MinimumCorridorGaps")]
        public void MinimumCorridorGapsCommand()
        {
            var doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            var ed = doc.Editor;

            try
            {
                using (var tr = doc.TransactionManager.StartTransaction())
                {
                    var corridorTypVals = new List<TypedValue>
                            {
                                new TypedValue((int)DxfCode.Operator, "<OR"),
                                new TypedValue((int)DxfCode.Start, RXClass.GetClass(typeof(Corridor)).DxfName),
                                new TypedValue((int)DxfCode.Operator, "OR>")
                            };

                    var corrObjIds = ed.GetEntities(corridorTypVals, "\nSelect corridor: ", "\nSelection objects to remove",
                                                     out List<FullSubentityPath> xrefPaths);

                    if (!corrObjIds.Any())
                    {
                        return;
                    }

                    ed.UnHighlightSelectedXREFSubEntities(xrefPaths);

                    var distRslt = ed.GetDistance("\nEnter minimum gap distance: ");

                    if (distRslt.Status != Autodesk.AutoCAD.EditorInput.PromptStatus.OK)
                    {
                        return;
                    }

                    var minGapDist = distRslt.Value;

                    foreach (var corrObjId in corrObjIds)
                    {
                        var corr = corrObjId.GetObject(OpenMode.ForWrite) as Corridor;
                        foreach (var baseline in corr.Baselines)
                        {
                            var lastRegionStation = double.NaN;
                            var baselineCount = baseline.BaselineRegions.Count;
                            for (int i = 0; i < baselineCount; i++)
                            {
                                if (i == 0)
                                {
                                    lastRegionStation = baseline.BaselineRegions[i].EndStation;
                                }
                                else
                                {
                                    var currentRegionStation = baseline.BaselineRegions[i].StartStation;
                                    var currentGap = currentRegionStation - lastRegionStation;
                                    if (currentGap < minGapDist)
                                    {
                                        baseline.BaselineRegions[i].StartStation = lastRegionStation + minGapDist;
                                    }
                                    lastRegionStation = baseline.BaselineRegions[i].EndStation;
                                }                                
                            }
                        }                        
                    }

                    tr.Commit();
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("\nError: " + ex.Message);
            }
        }

No comments:

LinkWithin

Blog Widget by LinkWithin

Ad