Ejemplo n.º 1
0
        public static ObjectId Ensure(string name, short colorIndex)
        {
            using (var transaction = ModelSpace.StartTransaction())
                using (var table = (LayerTable)transaction
                                   .GetObject(
                           HostApplicationServices.WorkingDatabase.LayerTableId,
                           OpenMode.ForRead))
                {
                    if (table.Has(name))
                    {
                        var layer = transaction.GetObject(
                            table[name],
                            OpenMode.ForWrite) as LayerTableRecord;

                        layer.Color = Color.FromColorIndex(ColorMethod.ByLayer, colorIndex);
                        transaction.Commit();
                        return(table[name]);
                    }

                    var record = new LayerTableRecord()
                    {
                        Name  = name,
                        Color = Color.FromColorIndex(ColorMethod.ByLayer, colorIndex),
                    };

                    table.UpgradeOpen();
                    ObjectId id = table.Add(record);

                    transaction.AddNewlyCreatedDBObject(record, true);
                    transaction.Commit();

                    return(id);
                }
        }
Ejemplo n.º 2
0
        public void InsertAt(Point3d position, string model, bool sideWall = false, double angle = 0.0)
        {
            BlockTableRecord record = Define(sideWall);

            var blockRef = new BlockReference(position, Define(sideWall).Id)
            {
                Layer      = Layer,
                ColorIndex = ColorIndices.ByLayer,
                Rotation   = angle,
            };

            ModelSpace.From(transaction).AppendEntity(blockRef);
            transaction.AddNewlyCreatedDBObject(blockRef, true);

            foreach (ObjectId id in record)
            {
                using (var def = id.GetObject(OpenMode.ForRead) as AttributeDefinition)
                {
                    if ((def != null) && (!def.Constant) &&
                        def.Tag.ToUpper() == "MODEL")
                    {
                        using (var ar = new AttributeReference())
                        {
                            ar.SetAttributeFromBlock(def, blockRef.BlockTransform);
                            ar.TextString = $"{model}-{coverage}";
                            ar.Rotation   = 0;

                            blockRef.AttributeCollection.AppendAttribute(ar);
                            transaction.AddNewlyCreatedDBObject(ar, true);
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        static ObjectId ArialStyle()
        {
            using (var transaction = ModelSpace.StartTransaction())
                using (var styles = (TextStyleTable)transaction.GetObject(
                           HostApplicationServices.WorkingDatabase.TextStyleTableId,
                           OpenMode.ForWrite))
                    using (var currentStyle = (TextStyleTableRecord)transaction.GetObject(
                               HostApplicationServices.WorkingDatabase.Textstyle,
                               OpenMode.ForWrite))
                    {
                        var style = new TextStyleTableRecord()
                        {
                            Font = new Autodesk.AutoCAD.GraphicsInterface.FontDescriptor(
                                "ARIAL",
                                bold: false,
                                italic: false,
                                characters: currentStyle.Font.CharacterSet,
                                pitchAndFamily: currentStyle.Font.PitchAndFamily),
                        };

                        ObjectId styleId = styles.Add(style);

                        transaction.AddNewlyCreatedDBObject(style, true);
                        transaction.Commit();

                        return(styleId);
                    }
        }
Ejemplo n.º 4
0
        public static List <FloorTag> GetFloorTags()
        {
            using (var transaction = ModelSpace.StartTransaction())
            {
                var floorTags = new List <FloorTag>();
                foreach (ObjectId id in GetFloorTagIds(transaction))
                {
                    var block           = transaction.GetObject(id, OpenMode.ForRead) as BlockReference;
                    var elevationString = AttributeReader.TextString(
                        transaction,
                        id,
                        tag: "ELEVATION");

                    int.TryParse(elevationString, out int elevation);
                    floorTags.Add(new FloorTag
                    {
                        Position = new Point3d(
                            x: block.Position.X,
                            y: block.Position.Y,
                            z: block.Position.Z),
                        Name = AttributeReader.TextString(
                            transaction,
                            id,
                            tag: "NAME"),

                        Elevation = elevation
                    });
                }

                floorTags = floorTags.OrderBy(t => t.Elevation).ToList();

                return(floorTags);
            }
        }
Ejemplo n.º 5
0
        public static int HighestNumber()
        {
            using (var transaction = ModelSpace.StartTransaction())
            {
                int lastNumber = 0;
                var labelIds   = GetRiserLabelIds(transaction);

                foreach (var id in labelIds)
                {
                    string text  = AttributeReader.TextString(transaction, id, tag: TagName);
                    var    match = Regex.Match(text, @"R\.(\d+)\.[A-Z]");
                    if (match.Success)
                    {
                        string numberString = match.Groups[1].Value;
                        int    number       = int.Parse(numberString);
                        if (number > lastNumber)
                        {
                            lastNumber = number;
                        }
                    }
                }

                return(lastNumber);
            }
        }
Ejemplo n.º 6
0
        public static JobData Load()
        {
            using (var trans = ModelSpace.StartTransaction())
            {
                foreach (var objectId in ModelSpace.From(trans))
                {
                    if (IsJobDataBlock(trans, objectId))
                    {
                        var data = new JobData
                        {
                            CalculatedByCompany        = AttributeReader.TextString(trans, objectId, "CALCULATED_BY_COMPANY"),
                            JobNumber                  = AttributeReader.TextString(trans, objectId, "LEAD_NUMBER"),
                            JobName                    = AttributeReader.TextString(trans, objectId, "JOB_NAME"),
                            JobSiteAddress             = AttributeReader.TextString(trans, objectId, "SITE_LOCATION"),
                            SupplyStaticPressure       = AttributeReader.TextString(trans, objectId, "STATIC_PRESSURE"),
                            SupplyResidualPressure     = AttributeReader.TextString(trans, objectId, "RESIDUAL_PRESSURE"),
                            SupplyAvailableFlow        = AttributeReader.TextString(trans, objectId, "AVAILABLE_FLOW"),
                            SupplyElevation            = AttributeReader.TextString(trans, objectId, "METER_ELEVATION"),
                            SupplyPipeLength           = AttributeReader.TextString(trans, objectId, "METER_PIPE_LENGTH"),
                            SupplyPipeInternalDiameter = AttributeReader.TextString(trans, objectId, "METER_PIPE_INTERNAL_DIAMETER"),
                        };

                        return(data);
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 7
0
        static List <ElevationBox> GetElevationBoxes(Transaction transaction)
        {
            var boxes  = new List <ObjectId>();
            var labels = new List <ObjectId>();

            foreach (var objectId in ModelSpace.From(transaction))
            {
                if (IsElevationBoxPolyline(transaction, objectId))
                {
                    boxes.Add(objectId);
                }
                else if (IsElevationBoxLabel(transaction, objectId))
                {
                    labels.Add(objectId);
                }
            }

            var elevationBoxes = BuildElevationBoxesFrom(transaction, boxes, labels);
            var floorTags      = FloorTag.GetFloorTags();

            foreach (var box in elevationBoxes)
            {
                foreach (var floorTag in floorTags)
                {
                    if (box.IsInside(floorTag.Position))
                    {
                        box.FloorTag = floorTag;
                    }
                }
            }

            return(elevationBoxes);
        }
Ejemplo n.º 8
0
        public static void Insert(int coverage)
        {
            using (var transaction = ModelSpace.StartTransaction())
            {
                new Head(transaction, coverage).Define();
                transaction.Commit();
            }

            // TODO: int number = HeadLabel.HighestNumber() + 1;
            //int number = 1;
            var jobData = JobData.Load();

            using (var transaction = ModelSpace.StartTransaction())
            {
                var table = (BlockTable)transaction.GetObject(
                    Editor().Document.Database.BlockTableId,
                    OpenMode.ForRead);

                var jigBlock = (BlockTableRecord)transaction.GetObject(
                    table[$"{Head.BlockName}{coverage}"],
                    OpenMode.ForRead);

                var          jig = new BlockJig();
                PromptResult res = jig.DragMe(jigBlock.ObjectId, out Point3d point);

                // This point will be disposed outside of this block, so clone it
                var pointClone = new Point3d(
                    x: point.X,
                    y: point.Y,
                    z: point.Z);

                if (res.Status == PromptStatus.OK)
                {
                    new Head(transaction, coverage)
                    .InsertAt(pointClone, model: jobData?.HeadModelDefault ?? "");
                }

                //var labeler = new Labeler(
                //        new LabelSpecs
                //        {
                //            Tag = "HEADNUMBER", //RiserLabel.TagName,
                //            BlockName = "HeadLabel", //RiserLabel.BlockName,
                //            Layer = "HeadLabels", //RiserLabel.LayerName,
                //            LayerColorIndex = ColorIndices.Red,
                //            TextHeight = 5.0,
                //            XOffset = 15.0,
                //        });

                //labeler.CreateLabel($"H.{number}", position: pointClone);

                transaction.Commit();
            }

            ////number++;
        }
Ejemplo n.º 9
0
        /*
         * public static void SetKeyValue(string dictionaryName, string key, string value)
         * {
         *  using (var transaction = ModelSpace.StartTransaction())
         *  using (var db = HostApplicationServices.WorkingDatabase)
         *  using (var namedObjectDict = (DBDictionary)transaction.GetObject(
         *          db.NamedObjectsDictionaryId,
         *          OpenMode.ForRead))
         *  {
         *      if (!namedObjectDict.Contains(dictionaryName))
         *      {
         *          throw new Exception(
         *              $"The dictionary named '{dictionaryName}' does not exist in this DWG file");
         *      }
         *
         *      var dictionary = transaction.GetObject(
         *          namedObjectDict.GetAt(dictionaryName),
         *          OpenMode.ForWrite) as DBDictionary;
         *
         *      if (!dictionary.Contains(key))
         *      {
         *          dictionary.
         *      }
         *
         *      var xRecord = (Xrecord)transaction.GetObject(
         *          dictionary.GetAt(key),
         *          OpenMode.ForRead);
         *
         *      //string value = string.Empty;
         *      foreach (TypedValue typedValue in xRecord.Data.AsArray())
         *      {
         *          if (typedValue.TypeCode == 1)
         *          {
         *              value = typedValue.Value as string;
         *          }
         *      }
         *  }
         * }*/

        public static bool HasDictionaryNamed(string dictionaryName)
        {
            using (var transaction = ModelSpace.StartTransaction())
                using (var db = HostApplicationServices.WorkingDatabase)
                    using (var namedObjectDict = (DBDictionary)transaction.GetObject(
                               db.NamedObjectsDictionaryId,
                               OpenMode.ForRead))
                    {
                        return(namedObjectDict.Contains(dictionaryName));
                    }
        }
Ejemplo n.º 10
0
        static void HideShow(ObjectId id, bool isHidden)
        {
            using (var transaction = ModelSpace.StartTransaction())
            {
                var layer = transaction.GetObject(
                    id,
                    OpenMode.ForWrite) as LayerTableRecord;

                layer.IsOff = isHidden;

                transaction.Commit();
            }
        }
Ejemplo n.º 11
0
        void InsertAt(Point3d position)
        {
            var labelBlockDef = new BlockReference(position, Define().Id)
            {
                Layer      = "Floor Connectors",
                ColorIndex = ColorIndices.ByLayer
            };

            ModelSpace.From(transaction).AppendEntity(labelBlockDef);
            transaction.AddNewlyCreatedDBObject(labelBlockDef, true);

            transaction.Commit();
        }
Ejemplo n.º 12
0
        static List <ObjectId> GetFloorTagIds(Transaction transaction)
        {
            var floorTags = new List <ObjectId>();

            foreach (var objectId in ModelSpace.From(transaction))
            {
                if (IsFloorTagLabel(transaction, objectId))
                {
                    floorTags.Add(objectId);
                }
            }

            return(floorTags);
        }
Ejemplo n.º 13
0
        static List <ObjectId> GetRiserLabelIds(Transaction transaction)
        {
            var labels = new List <ObjectId>();

            foreach (var objectId in ModelSpace.From(transaction))
            {
                if (IsRiserLabel(transaction, objectId))
                {
                    labels.Add(objectId);
                }
            }

            return(labels);
        }
Ejemplo n.º 14
0
        static List <string> GetRiserLabelTexts()
        {
            using (var transaction = ModelSpace.StartTransaction())
            {
                var texts    = new List <string>();
                var labelIds = GetRiserLabelIds(transaction);

                foreach (var id in labelIds)
                {
                    string text = AttributeReader.TextString(transaction, id, tag: TagName);
                    texts.Add(text);
                }

                return(texts);
            }
        }
Ejemplo n.º 15
0
        public static List <ElevationBox> InsideElevationBoxes(Point3d point)
        {
            using (var transaction = ModelSpace.StartTransaction())
            {
                var boxes  = GetElevationBoxes(transaction);
                var inside = new List <ElevationBox>();
                foreach (var box in boxes)
                {
                    if (box.IsInside(point))
                    {
                        inside.Add(box);
                    }
                }

                return(inside);
            }
        }
Ejemplo n.º 16
0
        void NewNodeLabel(string text, Point3d position)
        {
            using (var transaction = ModelSpace.StartTransaction())
                using (var table = transaction.GetObject(
                           HostApplicationServices.WorkingDatabase.BlockTableId,
                           OpenMode.ForWrite) as BlockTable)
                    using (var modelSpace = transaction.GetObject(
                               table[BlockTableRecord.ModelSpace],
                               OpenMode.ForWrite) as BlockTableRecord)
                    {
                        using (var labelBlockDef = transaction.GetObject(
                                   labelBlockDefId,
                                   OpenMode.ForRead) as BlockTableRecord)
                            using (var blockRef = new BlockReference(position, labelBlockDefId)
                            {
                                Layer = specs.Layer,
                                ColorIndex = ColorIndices.ByLayer
                            })
                            {
                                modelSpace.AppendEntity(blockRef);
                                transaction.AddNewlyCreatedDBObject(blockRef, true);

                                foreach (ObjectId id in labelBlockDef)
                                {
                                    using (var def = id.GetObject(OpenMode.ForRead) as AttributeDefinition)
                                    {
                                        if ((def != null) && (!def.Constant) &&
                                            def.Tag.ToUpper() == specs.Tag)
                                        {
                                            using (var ar = new AttributeReference())
                                            {
                                                ar.SetAttributeFromBlock(def, blockRef.BlockTransform);
                                                ar.TextString = text;

                                                blockRef.AttributeCollection.AppendAttribute(ar);
                                                transaction.AddNewlyCreatedDBObject(ar, true);
                                            }
                                        }
                                    }
                                }

                                transaction.Commit();
                            }
                    }
        }
Ejemplo n.º 17
0
        ObjectId ExistingOrNewLabelDefId()
        {
            using (var trans = ModelSpace.StartTransaction())
                using (var tab = (BlockTable)trans.GetObject(
                           HostApplicationServices.WorkingDatabase.BlockTableId,
                           OpenMode.ForRead))
                {
                    if (tab.Has(specs.BlockName))
                    {
                        using (var existing = tab[specs.BlockName].GetObject(OpenMode.ForRead) as BlockTableRecord)
                        {
                            if (!PropertiesMatch(trans, existing))
                            {
                                existing.UpgradeOpen();
                                existing.Erase(true);
                                trans.Commit();
                            }
                            else
                            {
                                return(existing.Id);
                            }
                        }
                    }
                }

            using (var trans = ModelSpace.StartTransaction())
                using (var tab = (BlockTable)trans.GetObject(
                           HostApplicationServices.WorkingDatabase.BlockTableId,
                           OpenMode.ForRead))
                {
                    BlockTableRecord record = NewLabelDefIdFrom();

                    tab.UpgradeOpen();
                    tab.Add(record);
                    trans.AddNewlyCreatedDBObject(record, true);

                    var attDef = NewAttributeDef();
                    record.AppendEntity(attDef);
                    trans.AddNewlyCreatedDBObject(attDef, true);

                    trans.Commit();
                    return(record.Id);
                }
        }
Ejemplo n.º 18
0
        public static int Run()
        {
            var labeler = new Labeler(
                new LabelSpecs
            {
                Tag             = "HEADNUMBER",
                BlockName       = "HeadLabel",
                Layer           = "HeadLabels",
                LayerColorIndex = ColorIndices.Blue
            });

            using (var trans = ModelSpace.StartTransaction())
            {
                foreach (var objectId in ModelSpace.From(trans))
                {
                    if (IsLabel(trans, objectId))
                    {
                        var block = trans.GetObject(objectId, OpenMode.ForWrite) as BlockReference;

                        block.Erase(true);
                    }
                }

                trans.Commit();
            }

            using (var trans = ModelSpace.StartTransaction())
            {
                int headNumber = 1;
                foreach (var objectId in ModelSpace.From(trans))
                {
                    if (IsHead(trans, objectId))
                    {
                        var block = trans.GetObject(objectId, OpenMode.ForRead) as BlockReference;

                        labeler.CreateLabel($"H.{headNumber++}", block.Position);
                    }
                }

                trans.Commit();
                return(headNumber);
            }
        }
Ejemplo n.º 19
0
        public static void HideShow(string name, bool isHidden)
        {
            using (var transaction = ModelSpace.StartTransaction())
                using (var table = (LayerTable)transaction
                                   .GetObject(
                           HostApplicationServices.WorkingDatabase.LayerTableId,
                           OpenMode.ForRead))
                {
                    if (table.Has(name))
                    {
                        var layer = transaction.GetObject(
                            table[name],
                            OpenMode.ForWrite) as LayerTableRecord;

                        layer.IsHidden = isHidden;
                        transaction.Commit();
                    }
                }
        }
Ejemplo n.º 20
0
        public static string KeyValue(string dictionaryName, string key)
        {
            using (var transaction = ModelSpace.StartTransaction())
                using (var db = HostApplicationServices.WorkingDatabase)
                    using (var namedObjectDict = (DBDictionary)transaction.GetObject(
                               db.NamedObjectsDictionaryId,
                               OpenMode.ForRead))
                    {
                        if (!namedObjectDict.Contains(dictionaryName))
                        {
                            throw new Exception(
                                      $"The dictionary named '{dictionaryName}' does not exist in this DWG file");
                        }

                        var dictionary = transaction.GetObject(
                            namedObjectDict.GetAt(dictionaryName),
                            OpenMode.ForRead) as DBDictionary;

                        if (!dictionary.Contains(key))
                        {
                            throw new Exception(
                                      $"Key '{key}' does not exist in the dictionary '{dictionaryName}'");
                        }

                        var xRecord = (Xrecord)transaction.GetObject(
                            dictionary.GetAt(key),
                            OpenMode.ForRead);

                        string value = string.Empty;
                        foreach (TypedValue typedValue in xRecord.Data.AsArray())
                        {
                            if (typedValue.TypeCode == 1)
                            {
                                value = typedValue.Value as string;
                            }
                        }

                        return(value);
                    }
        }
Ejemplo n.º 21
0
        public static char HighestSuffix()
        {
            using (var transaction = ModelSpace.StartTransaction())
            {
                byte lastNumber = 'A' - 1;

                foreach (string text in GetRiserLabelTexts())
                {
                    var match = Regex.Match(text, @"R\.(\d+)\.([A-Z])");
                    if (match.Success)
                    {
                        string suffixString = match.Groups[2].Value;
                        byte   number       = (byte)suffixString[0];
                        if (number > lastNumber)
                        {
                            lastNumber = number;
                        }
                    }
                }

                return((char)lastNumber);
            }
        }
Ejemplo n.º 22
0
        public static int LabelAllPipes()
        {
            var pipeLabeler = new Labeler(
                new LabelSpecs
            {
                Tag             = "PIPENUMBER",
                BlockName       = "PipeLabel",
                Layer           = "PipeLabels",
                LayerColorIndex = ColorIndices.Blue,
                TextHeight      = 4.0,
                XOffset         = 0.0,
                YOffset         = 0.0,
                HorizontalMode  = TextHorizontalMode.TextCenter
            });

            int pipeNumber = 1;

            Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("  LabelAllPipes starting...");
            var ids = new List <ObjectId>();

            using (var trans = ModelSpace.StartTransaction())
            {
                foreach (ObjectId id in ModelSpace.From(trans))
                {
                    ids.Add(id);
                }
            }

            using (var trans = ModelSpace.StartTransaction())
            {
                Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("  Finding labels...");
                foreach (ObjectId objectId in ids)
                {
                    if (IsLabel(trans, objectId))
                    {
                        Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"    label found, erase block...");
                        var block       = trans.GetObject(objectId, OpenMode.ForWrite) as BlockReference;
                        var blkrefClass = RXObject.GetClass(typeof(BlockReference));
                        if (block != null && objectId.ObjectClass == blkrefClass)
                        {
                            block.Erase(true);
                        }

                        Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"    label found, erase DBTtext...");
                        var text = trans.GetObject(objectId, OpenMode.ForWrite) as DBText;
                        if (text != null)
                        {
                            text.Erase(true);
                        }
                    }
                }

                Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("  Finding pipes...");
                foreach (ObjectId objectId in ids)
                {
                    var vertices = new List <Point3d>();
                    if (IsPipe(trans, objectId))
                    {
                        var polyline = trans.GetObject(objectId, OpenMode.ForRead) as Polyline;

                        Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"    Adding {polyline.NumberOfVertices} vertices...");
                        for (int i = 0; i < polyline.NumberOfVertices; i++)
                        {
                            vertices.Add(polyline.GetPoint3dAt(i));
                        }

                        pipeNumber++;
                    }

                    Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"    Creating label: p{pipeNumber}...");
                    for (int i = 1; i < vertices.Count; i++)
                    {
                        pipeLabeler.CreateLabel(
                            text: $"P{pipeNumber}",
                            position: Midpoint(vertices[i], vertices[i - 1]));
                    }
                }

                trans.Commit();

                return(pipeNumber);
            }
        }