Пример #1
0
    /// <summary>
    /// Create a corridor based on a switch name
    /// </summary>
    public static CivilCorridor CreateSwitchCorridor(string alignmentName, string prefix, string[] switches, Document document)
    {
        if (string.IsNullOrEmpty(alignmentName))
        {
            return(null);
        }
        if (string.IsNullOrEmpty(prefix))
        {
            return(null);
        }
        if (switches == null)
        {
            return(null);
        }
        if (document == null)
        {
            return(null);
        }

        var db  = document.AcDocument.Database;
        var doc = CivilDocument.GetCivilDocument(db);

        using (var ctx = new DocumentContext(db))
        {
            var id = ElementBinder.GetObjectIdFromTrace(ctx.Database);

            var switchList = "Connected Switches: ";

            foreach (var @switch in switches)
            {
                switchList += "-" + @switch;
            }

            if (id.IsValid && !id.IsErased)
            {
                using (var trans = db.TransactionManager.StartTransaction())
                {
                    var switchCorridor = (Corridor)trans.GetObject(id, OpenMode.ForWrite, false, true);
                    switchCorridor.Name        = prefix + alignmentName;
                    switchCorridor.Description = switchList;
                    trans.Commit();
                }
            }
            else
            {
                using (var trans = db.TransactionManager.StartTransaction())
                {
                    var corridorName = prefix + alignmentName;
                    id = doc.CorridorCollection.Add(corridorName);
                    var switchCorridor = (Corridor)trans.GetObject(id, OpenMode.ForWrite, false, true);
                    switchCorridor.Description = switchList;
                    trans.Commit();
                }
            }

            var corridor = ctx.GetTransaction().GetObject(id, OpenMode.ForRead, false, true) as Corridor;
            return(corridor != null ? new CivilCorridor(corridor, true) : null);
        }
    }
Пример #2
0
    /// <summary>
    /// Create a <see cref="CadSolid"/> as lofted between cross sections
    /// </summary>
    /// <param name="document"></param>
    /// <param name="crossSections"></param>
    /// <param name="layer">set the layer for the solid (can be null)</param>
    /// <returns></returns>
    public static CadSolid ByLoft(Document document, Polyline3D[] crossSections, string layer)
    {
        if (document == null)
        {
            return(null);
        }
        if (crossSections?.Any() != true)
        {
            return(null);
        }

        var db = document.AcDocument.Database;

        using (var ctx = new DocumentContext(db))
        {
            var id = ElementBinder.GetObjectIdFromTrace(ctx.Database);

            var apply = new Action <Solid3d>(solid =>
            {
                var polylines = crossSections.Select(o => (Polyline3d)o.InternalDBObject).ToArray();
                solid.CreateLoftedSolid(polylines, new Entity[] {}, null, new LoftOptions());

                if (!string.IsNullOrWhiteSpace(layer))
                {
                    solid.Layer = layer;
                }
            });

            using (var trans = db.TransactionManager.StartTransaction())
            {
                if (id.IsValid & !id.IsErased)
                {
                    var solid = (Solid3d)trans.GetObject(id, OpenMode.ForWrite, false, true);
                    apply(solid);
                }
                else
                {
                    var solid = new Solid3d();
                    apply(solid);

                    var bt         = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead, false, true);
                    var modelspace = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false, true);

                    id = modelspace.AppendEntity(solid);
                    trans.AddNewlyCreatedDBObject(solid, true);
                }

                trans.Commit();
            }

            var solidObj = ctx.GetTransaction().GetObject(id, OpenMode.ForRead, false, true) as Solid3d;
            return(solidObj != null ? new CadSolid(solidObj, true) : null);
        }
    }
Пример #3
0
    /// <summary>
    /// Change an object's color based on it's handle
    /// </summary>
    public static bool SetColor(Document document, string handle, int red, int green, int blue)
    {
        if (document == null)
        {
            return(false);
        }
        if (string.IsNullOrEmpty(handle))
        {
            return(false);
        }

        var db = document.AcDocument.Database;

        // And attempt to get an ObjectId for the Handle
        var id = DocumentContext.GetObjectId(db, handle);

        using (var ctx = new DocumentContext(document.AcDocument))
        {
            var selected = (Entity)ctx.GetTransaction().GetObject(id, OpenMode.ForWrite, false, true);
            selected.Color = Color.FromRgb((byte)red, (byte)green, (byte)blue);
        }

        return(true);
    }
Пример #4
0
    /// <summary>
    /// Create a profile based on an asset
    /// </summary>
    public static CivilProfile CreateFromAsset(string assetName, string assetDescription, double assetStartStation, double assetEndStation,
                                               Autodesk.Civil.DynamoNodes.Profile dynProfile, ObjectId layerId, ObjectId styleId, ObjectId labelSetId, Document document)
    {
        if (string.IsNullOrEmpty(assetName))
        {
            return(null);
        }
        if (string.IsNullOrEmpty(assetDescription))
        {
            return(null);
        }
        if (dynProfile == null)
        {
            return(null);
        }
        if (document == null)
        {
            return(null);
        }

        var profile = (Profile)dynProfile.InternalDBObject;
        var db      = document.AcDocument.Database;

        using (var ctx = new DocumentContext(db))
        {
            var id = ElementBinder.GetObjectIdFromTrace(ctx.Database);
            if (id.IsValid && !id.IsErased)
            {
                using (var trans = db.TransactionManager.StartTransaction())
                {
                    var existingProfile = (Profile)trans.GetObject(id, OpenMode.ForWrite, false, true);
                    existingProfile.Entities.Clear();

                    var startLevel = profile.ElevationAt(assetStartStation);
                    var endLevel   = profile.ElevationAt(assetEndStation);
                    var startPoint = new Point2d(assetStartStation, startLevel);
                    var endPoint   = new Point2d(assetEndStation, endLevel);

                    existingProfile.Entities.AddFixedTangent(startPoint, endPoint);
                    existingProfile.Description = assetDescription;

                    trans.Commit();
                }
            }
            else
            {
                if (layerId.IsNull)
                {
                    return(null);
                }
                if (styleId.IsNull)
                {
                    return(null);
                }
                if (labelSetId.IsNull)
                {
                    return(null);
                }

                using (var trans = db.TransactionManager.StartTransaction())
                {
                    id = Profile.CreateByLayout(assetName, profile.AlignmentId, layerId, styleId, labelSetId);
                    var assetProfile = (Profile)trans.GetObject(id, OpenMode.ForWrite, false, true);

                    var startLevel = profile.ElevationAt(assetStartStation);
                    var endLevel   = profile.ElevationAt(assetEndStation);
                    var startPoint = new Point2d(assetStartStation, startLevel);
                    var endPoint   = new Point2d(assetEndStation, endLevel);

                    assetProfile.Entities.AddFixedTangent(startPoint, endPoint);
                    assetProfile.Description = assetDescription;

                    trans.Commit();
                }
            }

            var profileObject = ctx.GetTransaction().GetObject(id, OpenMode.ForRead, false, true) as Profile;
            return(profileObject != null ? new CivilProfile(profileObject, true) : null);
        }
    }
Пример #5
0
    /// <summary>
    /// Create an AutoCAD Table
    /// </summary>
    public static CadTable Create(Document document, Point insertPoint, string title, string[] columns, int columnWidth, List <string>[] data)
    {
        if (document == null)
        {
            return(null);
        }
        if (insertPoint == null)
        {
            return(null);
        }
        if (string.IsNullOrEmpty(title))
        {
            return(null);
        }
        if (columns == null)
        {
            return(null);
        }
        if (data == null)
        {
            return(null);
        }

        var AcDocument = document.AcDocument;
        var db         = AcDocument.Database;

        using (var ctx = new DocumentContext(db))
        {
            var id = ElementBinder.GetObjectIdFromTrace(ctx.Database);

            if (id.IsValid & !id.IsErased)
            {
                using (var trans = db.TransactionManager.StartTransaction())
                {
                    var table = (Table)trans.GetObject(id, OpenMode.ForWrite, false, true);

                    table.TableStyle = db.Tablestyle;
                    table.SetSize(data.Length + 2, columns.Length);
                    table.SetColumnWidth(columnWidth);
                    table.Position = new Point3d(insertPoint.X, insertPoint.Y, insertPoint.Z);

                    for (var i = 0; i <= data.Length + 1; i++)
                    {
                        if (i == 0)
                        {
                            table.Cells[i, 0].TextString = title;
                        }
                        else if (i == 1)
                        {
                            for (var j = 0; j <= columns.Length - 1; j++)
                            {
                                table.Cells[i, j].TextString = columns[j];
                            }
                        }
                        else
                        {
                            for (var j = 0; j <= columns.Length - 1; j++)
                            {
                                table.Cells[i, j].TextString = data[i - 2][j];
                            }
                        }
                    }

                    trans.Commit();
                }
            }
            else
            {
                using (var trans = db.TransactionManager.StartTransaction())
                {
                    var table = new Table();

                    table.TableStyle = db.Tablestyle;
                    table.SetSize(data.Length + 2, columns.Length);
                    table.SetColumnWidth(columnWidth);
                    table.Position = new Point3d(insertPoint.X, insertPoint.Y, insertPoint.Z);

                    for (var i = 0; i <= data.Length + 1; i++)
                    {
                        if (i == 0)
                        {
                            table.Cells[i, 0].TextString = title;
                        }
                        else if (i == 1)
                        {
                            for (var j = 0; j <= columns.Length - 1; j++)
                            {
                                table.Cells[i, j].TextString = columns[j];
                            }
                        }
                        else
                        {
                            for (var j = 0; j <= columns.Length - 1; j++)
                            {
                                table.Cells[i, j].TextString = data[i - 2][j];
                            }
                        }
                    }

                    table.GenerateLayout();

                    var bt         = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead, false, true);
                    var modelspace = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false, true);

                    id = modelspace.AppendEntity(table);
                    trans.AddNewlyCreatedDBObject(table, true);
                    trans.Commit();
                }
            }

            var tableObj = ctx.GetTransaction().GetObject(id, OpenMode.ForRead, false, true) as Table;
            return(tableObj != null ? new CadTable(tableObj, true) : null);
        }
    }