Example #1
0
        protected void SetPropertyOnObject(string name, object value)
        {
            Transaction    acTrans   = _database.TransactionManager.TopTransaction;
            BlockReference reference = (BlockReference)acTrans.GetObject(BaseObject, OpenMode.ForWrite);

            if (reference.IsDynamicBlock)
            {
                DynamicBlockReferencePropertyCollection pc = reference.DynamicBlockReferencePropertyCollection;
                foreach (DynamicBlockReferenceProperty dynamicBlockReferenceProperty in pc)
                {
                    if (dynamicBlockReferenceProperty.PropertyName == name)
                    {
                        dynamicBlockReferenceProperty.Value = value;
                        return;
                    }
                }

                foreach (ObjectId attId in reference.AttributeCollection)
                {
                    AttributeReference attRef = (AttributeReference)acTrans.GetObject(attId, OpenMode.ForRead);
                    if (attRef.Tag == name)
                    {
                        attRef.UpgradeOpen();
                        attRef.TextString = (string)value;
                        return;
                    }
                }

                throw new InvalidOperationException("Property not found");
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Example #2
0
        public static bool SetAttributeReferenceValue(this BlockReference block, string attTag, string val)
        {
            bool result = false;

            using (Transaction tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
            {
                var atts = block.AttributeCollection.Cast <ObjectId>()
                           .Select(id => (AttributeReference)tr.GetObject(id, OpenMode.ForRead));
                AttributeReference attref = atts.FirstOrDefault(a => a.Tag.ToUpper() == attTag.ToUpper());
                if (attref != null)
                {
                    attref.UpgradeOpen();
                    if (attref.HasFields)
                    {
                        attref.RemoveField();
                    }
                    attref.TextString = val;
                    result            = true;
                }

                tr.Commit();
            }

            return(result);
        }
Example #3
0
        protected override bool Update()
        {
            BlockReference br = Entity as BlockReference;

            br.Position = _pos;

            if (br.AttributeCollection.Count != 0)
            {
                foreach (ObjectId id in br.AttributeCollection)
                {
                    DBObject           obj = _tr.GetObject(id, OpenMode.ForRead);
                    AttributeReference ar  = obj as AttributeReference;

                    // Apply block transform to att def position
                    if (ar != null)
                    {
                        ar.UpgradeOpen();
                        AttInfo ai = _attInfo[ar.ObjectId];
                        ar.Position = ai.Position.TransformBy(br.BlockTransform);

                        if (ai.IsAligned)
                        {
                            ar.AlignmentPoint = ai.Alignment.TransformBy(br.BlockTransform);
                        }

                        if (ar.IsMTextAttribute)
                        {
                            ar.UpdateMTextAttribute();
                        }
                    }
                }
            }
            return(true);
        }
Example #4
0
        private void SetBlockAttrib(ObjectId oid, string attName, string val)
        {
            if (oid == ObjectId.Null)
            {
                return;
            }

            using (Transaction myT = _TransMan.StartTransaction())
            {
                BlockReference blockEnt = _TransMan.GetObject(oid, OpenMode.ForRead) as BlockReference;
                if (blockEnt != null)
                {
                    AttributeReference attRef = null;

                    attRef = GetBlockAttribute(attName, blockEnt);

                    if (attRef != null)
                    {
                        attRef.UpgradeOpen();
                        attRef.TextString = val;
                    }
                    else
                    {
                        throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Attribut '{0}' nicht gefunden!", attName));
                    }
                }

                myT.Commit();
            }
        }
Example #5
0
        public static void ChangeNameBR()
        {
            // Get the current document editor
            Editor   acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;
            Database db      = Application.DocumentManager.MdiActiveDocument.Database;

            // Create a TypedValue array to define the filter criteria
            TypedValue[] acTypValAr = new TypedValue[1];
            acTypValAr.SetValue(new TypedValue((int)DxfCode.Start, "MTEXT"), 0);
            // Assign the filter criteria to a SelectionFilter object
            SelectionFilter acSelFtr = new SelectionFilter(acTypValAr);
            // Request for objects to be selected in the drawing area
            PromptSelectionResult acSSPrompt = acDocEd.GetSelection(acSelFtr);

            // If the prompt status is OK, objects were selected
            if (acSSPrompt.Status == PromptStatus.OK)
            {
                SelectionSet     acSSet         = acSSPrompt.Value;
                SelectedObject[] chosenMtextArr = new SelectedObject[1];
                acSSet.CopyTo(chosenMtextArr, 0);
                Transaction trans     = db.TransactionManager.StartTransaction();
                MText       mtext     = trans.GetObject(chosenMtextArr[0].ObjectId, OpenMode.ForRead, true) as MText;
                string      mtextText = mtext.Contents.ToString();
                Application.ShowAlertDialog("Selected object: " + mtextText);

                //Block reference
                PromptEntityResult promptEntity = acDocEd.GetEntity("Choose a Block Reference");
                if (acSSPrompt.Status != PromptStatus.OK)
                {
                    acDocEd.WriteMessage("Block Reference choosing failed!");
                    return;
                }

                string         attbName       = "HEIGHT";
                BlockReference blockReference = trans.GetObject(promptEntity.ObjectId, OpenMode.ForRead, true) as BlockReference;
                foreach (ObjectId arId in blockReference.AttributeCollection)
                {
                    AttributeReference ar = trans.GetObject(arId, OpenMode.ForRead) as AttributeReference;
                    if (null == ar)
                    {
                        acDocEd.WriteMessage("AttributeReference getting failed!");
                        return;
                    }
                    if (ar.Tag.ToUpper() == attbName)
                    {
                        ar.UpgradeOpen();
                        ar.TextString = mtextText;
                        ar.DowngradeOpen();
                    }
                }

                trans.Commit();
            }
            else
            {
                Application.ShowAlertDialog("Number of objects selected: 0");
            }
        }
Example #6
0
 /// <summary>
 /// Поворот атрибута в 0
 /// </summary>
 public static void Normalize([NotNull] this AttributeReference atr)
 {
     if (Math.Abs(atr.Rotation) > 0.0001)
     {
         if (!atr.IsWriteEnabled)
         {
             atr.UpgradeOpen();
         }
         atr.Rotation = 0;
     }
 }
        private void ChangeAttributeValue(Transaction trx, Database db, string blockName, string attributeTag, string oldString, string newString)
        {
            ObjectId   psId;
            BlockTable bt = (BlockTable)trx.GetObject(db.BlockTableId, OpenMode.ForRead);

            psId = bt[BlockTableRecord.PaperSpace];

            BlockTableRecord btr = (BlockTableRecord)trx.GetObject(psId, OpenMode.ForRead);

            foreach (var entId in btr)
            {
                Entity ent = trx.GetObject(entId, OpenMode.ForRead) as Entity;
                if (ent != null)
                {
                    BlockReference br = ent as BlockReference;
                    if (br != null)
                    {
                        BlockTableRecord bd = (BlockTableRecord)trx.GetObject(br.BlockTableRecord, OpenMode.ForRead);

                        // ... to see whether it's a block with the name we're after

                        if (bd.Name.ToUpper() == blockName.ToUpper())
                        {
                            // Check each of the attributes...

                            foreach (ObjectId arId in br.AttributeCollection)
                            {
                                DBObject           obj = trx.GetObject(arId, OpenMode.ForRead);
                                AttributeReference ar  = obj as AttributeReference;
                                if (ar != null)
                                {
                                    // ... to see whether it has
                                    // the tag we're after

                                    if (ar.Tag.ToUpper() == attributeTag.ToUpper())
                                    {
                                        // check if attribute has correct value
                                        if (ar.TextString == oldString)
                                        {
                                            // If so, update the value
                                            ar.UpgradeOpen();
                                            ar.TextString = newString;
                                            ar.DowngradeOpen();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Example #8
0
        //same as the UpdateAttributesinBlock function I have in the Attribute class, but I had to copy it over here for slight changes
        //here it gets the Rev number in the title block and adds that to the attribute in the rev triangle block
        private static void UpdateAttributesInRevBlock(string blockName, string attbName, string attbValue)
        {
            Document    doc = Application.DocumentManager.MdiActiveDocument;
            Database    db  = doc.Database;
            Editor      ed  = doc.Editor;
            Transaction tr  = doc.TransactionManager.StartTransaction();

            using (tr)
            {
                //get the current space
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead);
                // Test each entity in the container...
                foreach (ObjectId entId in btr)
                {
                    Entity ent = tr.GetObject(entId, OpenMode.ForRead) as Entity;

                    if (ent != null)
                    {
                        BlockReference br = ent as BlockReference;
                        if (br != null)
                        {
                            BlockTableRecord bd = (BlockTableRecord)tr.GetObject(br.BlockTableRecord, OpenMode.ForRead);

                            // ... to see whether it's a block with
                            // the name we're after
                            if (bd.Name.ToUpper() == blockName)
                            {
                                // Check each of the attributes...
                                foreach (ObjectId arId in br.AttributeCollection)
                                {
                                    DBObject           obj = tr.GetObject(arId, OpenMode.ForRead);
                                    AttributeReference ar  = obj as AttributeReference;

                                    if (ar != null)
                                    {
                                        // ... to see whether it has
                                        // the tag we're after
                                        if (ar.Tag.ToUpper() == attbName)
                                        {
                                            ar.UpgradeOpen();
                                            ar.TextString = attbValue;
                                            ar.DowngradeOpen();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                tr.Commit();
            }
        }
Example #9
0
        private void UpdateAttributesInBlock(ObjectId btrId, string blockName, Dictionary <string, string> attributeTagValue)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;

            using (Transaction trans = doc.TransactionManager.StartTransaction())
            {
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(btrId, OpenMode.ForRead);

                foreach (ObjectId entId in btr)
                {
                    Entity ent = trans.GetObject(entId, OpenMode.ForRead) as Entity;

                    if (ent != null)
                    {
                        BlockReference br = ent as BlockReference;
                        if (br != null)
                        {
                            BlockTableRecord btrec = (BlockTableRecord)trans.GetObject(br.BlockTableRecord, OpenMode.ForRead);
                            if (string.Compare(btrec.Name, blockName, true) == 0)
                            {
                                foreach (ObjectId arObjectIdId in br.AttributeCollection)
                                {
                                    DBObject           obj = trans.GetObject(arObjectIdId, OpenMode.ForRead);
                                    AttributeReference ar  = obj as AttributeReference;
                                    if (ar != null)
                                    {
                                        foreach (KeyValuePair <string, string> kvp in attributeTagValue)
                                        {
                                            if (string.Compare(ar.Tag, kvp.Key, true) == 0)
                                            {
                                                ar.UpgradeOpen();
                                                ar.TextString = kvp.Value;
                                                ar.DowngradeOpen();
                                            }
                                        }
                                    }
                                }
                            }

                            // Recurse for nested blocks
                            UpdateAttributesInBlock(br.BlockTableRecord, blockName, attributeTagValue);
                        }
                    }
                }

                trans.Commit();
            }
        }
        void ChangeAttributeValue(String AttributeTagName, String OldValue, String NewValue)
        {
            Document activeDoc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;

            Database db = activeDoc.Database;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;

                foreach (ObjectId oid in bt)
                {
                    BlockTableRecord btr = tr.GetObject(oid, OpenMode.ForWrite) as BlockTableRecord;

                    if (!btr.IsLayout)
                    {
                        ObjectIdCollection brefIds = btr.GetBlockReferenceIds(false, false);
                        foreach (ObjectId brId in brefIds)
                        {
                            BlockReference bref = tr.GetObject(brId, OpenMode.ForRead) as BlockReference;

                            foreach (ObjectId arId in bref.AttributeCollection)
                            {
                                AttributeReference ar = tr.GetObject(arId, OpenMode.ForRead) as AttributeReference;
                                if (ar != null)
                                {
                                    if (ar.Tag.ToUpper().Equals(AttributeTagName.ToUpper()))
                                    {
                                        if (ar.TextString.ToUpper().Equals(OldValue))
                                        {
                                            ar.UpgradeOpen();
                                            ar.TextString = NewValue;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                tr.Commit();
            }
        }
Example #11
0
        public void ChangeBlock(string blockName, string roomNumber)
        {
            Editor   ed = Application.DocumentManager.MdiActiveDocument.Editor;
            Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                //打开块表
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                //打开块表中名为blockName的块表记录
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[blockName], OpenMode.ForRead);
                //获取所有名为blockName的块参照
                ObjectIdCollection blcokRefIds = btr.GetBlockReferenceIds(true, true);
                //循环遍历块参照
                foreach (ObjectId blockRefId in blcokRefIds)
                {
                    //打开当前块参照
                    BlockReference blockRef = (BlockReference)trans.GetObject(blockRefId, OpenMode.ForRead);
                    //获取当前块参照的属性集合
                    AttributeCollection blockRefAtts = blockRef.AttributeCollection;
                    //循环遍历属性集合
                    foreach (ObjectId attId in blockRefAtts)
                    {
                        //获取当前属性参照对象
                        AttributeReference attRef = (AttributeReference)trans.GetObject(attId, OpenMode.ForRead);
                        //只改变NUMBER属性值为"0000"的属性值为roomNumber
                        switch (attRef.Tag)
                        {
                        case "NUMBER":
                            if (attRef.TextString == "0000")
                            {
                                attRef.UpgradeOpen();    //切换属性参照对象为可写状态
                                attRef.TextString = roomNumber;
                            }
                            break;
                        }
                    }
                }
                trans.Commit();
            }
        }
        public static void FillAttribute(string blkName, string attrTag, string attrValue)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db  = doc.Database;
            Editor   ed  = doc.Editor;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                if (!bt.Has(blkName))
                {
                    return;                   // Replace "parent" by the parent block name
                }
                BlockTableRecord btr =
                    (BlockTableRecord)tr.GetObject(bt[blkName], OpenMode.ForRead);
                foreach (ObjectId id in btr)
                {
                    BlockReference br = tr.GetObject(id, OpenMode.ForRead) as BlockReference;
                    if (br != null && br.Name == blkName) // replace "nested" withe the nested block name
                    {
                        foreach (ObjectId attId in br.AttributeCollection)
                        {
                            AttributeReference att =
                                (AttributeReference)tr.GetObject(attId, OpenMode.ForRead);
                            if (att.Tag == attrTag) // replace "TAG" with the attribute tag
                            {
                                att.UpgradeOpen();
                                att.TextString = attrValue; // repace "foo" with the new attribute value
                                break;
                            }
                        }
                        break;
                    }
                }
                tr.Commit();
            }
            ed.Regen();
        }
Example #13
0
        /// <summary>
        /// 更新块参照中的属性值
        /// </summary>
        /// <param name="blockRefId">块参照的Id</param>
        /// <param name="attNameValues">需要更新的属性名称与取值</param>
        public static void UpdateAttributesInBlock(this ObjectId blockRefId, Dictionary <string, string> attNameValues)
        {
            //获取块参照对象
            BlockReference blockRef = blockRefId.GetObject(OpenMode.ForRead) as BlockReference;

            if (blockRef != null)
            {
                //遍历块参照中的属性
                foreach (ObjectId id in blockRef.AttributeCollection)
                {
                    //获取属性
                    AttributeReference attref = id.GetObject(OpenMode.ForRead) as AttributeReference;
                    //判断是否包含指定的属性名称
                    if (attNameValues.ContainsKey(attref.Tag.ToUpper()))
                    {
                        attref.UpgradeOpen();//切换属性对象为写的状态
                        //设置属性值
                        attref.TextString = attNameValues[attref.Tag.ToUpper()].ToString();
                        attref.DowngradeOpen();//为了安全,将属性对象的状态改为读
                    }
                }
            }
        }
Example #14
0
        public void UpdateAttributesInBlock(Database db, ObjectId blockRefId, Dictionary <string, string> attNameValues)
        {
            using (var trans = db.TransactionManager.StartTransaction()) {
                BlockReference blockRef = trans.GetObject(blockRefId, OpenMode.ForRead) as BlockReference;

                if (blockRef != null)
                {
                    foreach (ObjectId id in blockRef.AttributeCollection)
                    {
                        AttributeReference attref = trans.GetObject(id, OpenMode.ForRead) as AttributeReference;

                        if (attNameValues.ContainsKey(attref.Tag.ToUpper()))
                        {
                            attref.UpgradeOpen();

                            attref.TextString = attNameValues[attref.Tag.ToUpper()].ToString();

                            attref.DowngradeOpen();
                        }
                    }
                }
                trans.Commit();
            }
        }
Example #15
0
        protected override bool Update()
        {
            if (bref is BlockReference)
            {
                BlockReference brf = (BlockReference)bref;
                brf.Position = insertPoint;

                if (atts != null)
                {
                    foreach (ObjectId id in brf.AttributeCollection)
                    {
                        AttributeReference attRef = (AttributeReference)tr.GetObject(id, OpenMode.ForRead);
                        if (attRef != null)
                        {
                            attRef.UpgradeOpen();
                            AttributeDefinition adef = (AttributeDefinition)tr.GetObject(atts[attRef.ObjectId], OpenMode.ForRead);
                            attRef.SetAttributeFromBlock(adef, brf.BlockTransform);
                            attRef.AdjustAlignment(brf.Database);
                        }
                    }
                }
            }
            return(true);
        }
Example #16
0
        public void Update()
        {
            Document acDoc   = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;

            Transaction tr = acCurDb.TransactionManager.TopTransaction;

            //Centre line
            Curve c = tr.GetObject(ObjectId, OpenMode.ForRead) as Curve;

            //Delete existing
            NegativeFoundationId.GetObject(OpenMode.ForWrite, true).Erase();
            PositiveFoundationId.GetObject(OpenMode.ForWrite, true).Erase();

            //Offset it
            LayerTable acLyrTbl = tr.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;
            ObjectId   current  = acCurDb.Clayer;

            acCurDb.Clayer = acLyrTbl[Utilities.FoundationLayer];

            // Open the Block table for read
            BlockTable acBlkTbl = tr.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;

            // Open the Block table record Model space for write
            BlockTableRecord acBlkTblRec = tr.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

            DBObjectCollection offsets  = c.GetOffsetCurves(FoundationWidth / 2);
            DBObjectCollection offsets2 = c.GetOffsetCurves(-(FoundationWidth / 2));

            foreach (Entity e in offsets)
            {
                acBlkTblRec.AppendEntity(e);
                tr.AddNewlyCreatedDBObject(e, true);
                e.LayerId            = acCurDb.Clayer;
                PositiveFoundationId = e.ObjectId;
            }
            foreach (Entity e in offsets2)
            {
                acBlkTblRec.AppendEntity(e);
                tr.AddNewlyCreatedDBObject(e, true);
                e.LayerId            = acCurDb.Clayer;
                NegativeFoundationId = e.ObjectId;
            }


            acCurDb.Clayer = current;

            //Update formation tag
            if (FormationTagId != null)
            {
                BlockReference acBlkRef = tr.GetObject(FormationTagId, OpenMode.ForRead) as BlockReference;

                //Set value
                AttributeCollection attCol = acBlkRef.AttributeCollection;
                foreach (ObjectId attId in attCol)
                {
                    AttributeReference att = tr.GetObject(attId, OpenMode.ForRead) as AttributeReference;
                    if (att.Tag == "LEVEL")
                    {
                        att.UpgradeOpen();
                        att.TextString = Parent.FormationLevel.ToString("F3");
                    }
                }
            }
        }
Example #17
0
        public void Numbering()
        {
            if (!CheckLicense.Check())
            {
                return;
            }

            Autodesk.AutoCAD.ApplicationServices.Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Autodesk.AutoCAD.DatabaseServices.Database    db  = doc.Database;
            Matrix3d ucs2wcs = AcadUtility.AcadGraphics.UcsToWcs;
            Matrix3d wcs2ucs = AcadUtility.AcadGraphics.WcsToUcs;

            using (NumberingForm form = new NumberingForm())
            {
                // Read settings
                form.SelectObjects = (NumberingForm.SelectNumberingObjects)Properties.Settings.Default.Command_NUMARALANDIR_SelectObjects;
                form.AttributeName = Properties.Settings.Default.Command_NUMARALANDIR_AttributeName;
                form.Ordering      = (NumberingForm.CoordinateOrdering)Properties.Settings.Default.Command_NUMARALANDIR_Order;
                form.Prefix        = Properties.Settings.Default.Command_NUMARALANDIR_Prefix;
                form.StartNumber   = Properties.Settings.Default.Command_NUMARALANDIR_StartNumber;
                form.Increment     = Properties.Settings.Default.Command_NUMARALANDIR_Increment;
                form.Format        = Properties.Settings.Default.Command_NUMARALANDIR_Format;
                form.Suffix        = Properties.Settings.Default.Command_NUMARALANDIR_Suffix;

                if (Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(form) == System.Windows.Forms.DialogResult.OK)
                {
                    // Save changes
                    Properties.Settings.Default.Command_NUMARALANDIR_SelectObjects = (int)form.SelectObjects;
                    Properties.Settings.Default.Command_NUMARALANDIR_AttributeName = form.AttributeName;
                    Properties.Settings.Default.Command_NUMARALANDIR_Order         = (int)form.Ordering;
                    Properties.Settings.Default.Command_NUMARALANDIR_Prefix        = form.Prefix;
                    Properties.Settings.Default.Command_NUMARALANDIR_StartNumber   = form.StartNumber;
                    Properties.Settings.Default.Command_NUMARALANDIR_Increment     = form.Increment;
                    Properties.Settings.Default.Command_NUMARALANDIR_Format        = form.Format;
                    Properties.Settings.Default.Command_NUMARALANDIR_Suffix        = form.Suffix;

                    // Select objects
                    List <TypedValue> tvs = new List <TypedValue>();
                    switch (form.SelectObjects)
                    {
                    case NumberingForm.SelectNumberingObjects.Text:
                        tvs.Add(new TypedValue((int)DxfCode.Operator, "<OR"));
                        tvs.Add(new TypedValue((int)DxfCode.Start, "TEXT"));
                        tvs.Add(new TypedValue((int)DxfCode.Start, "MTEXT"));
                        tvs.Add(new TypedValue((int)DxfCode.Operator, "OR>"));
                        break;

                    default:
                        tvs.Add(new TypedValue((int)DxfCode.Operator, "<OR"));
                        tvs.Add(new TypedValue((int)DxfCode.Start, "INSERT"));
                        tvs.Add(new TypedValue((int)DxfCode.Operator, "OR>"));
                        break;
                    }
                    SelectionFilter filter = new SelectionFilter(tvs.ToArray());

                    PromptSelectionResult selRes = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.GetSelection(filter);
                    if (selRes.Status == PromptStatus.OK)
                    {
                        using (Transaction tr = db.TransactionManager.StartTransaction())
                        {
                            try
                            {
                                List <Tuple <ObjectId, Point3d> > items = new List <Tuple <ObjectId, Point3d> >();
                                // Read objects
                                foreach (ObjectId id in selRes.Value.GetObjectIds())
                                {
                                    if (id.ObjectClass.UnmanagedObject == RXClass.GetClass(typeof(DBText)).UnmanagedObject)
                                    {
                                        DBText obj = tr.GetObject(id, OpenMode.ForRead) as DBText;
                                        items.Add(new Tuple <ObjectId, Point3d>(id, obj.Position.TransformBy(wcs2ucs)));
                                    }
                                    else if (id.ObjectClass.UnmanagedObject == RXClass.GetClass(typeof(MText)).UnmanagedObject)
                                    {
                                        MText obj = tr.GetObject(id, OpenMode.ForRead) as MText;
                                        items.Add(new Tuple <ObjectId, Point3d>(id, obj.Location.TransformBy(wcs2ucs)));
                                    }
                                    else if (id.ObjectClass.UnmanagedObject == RXClass.GetClass(typeof(BlockReference)).UnmanagedObject)
                                    {
                                        BlockReference obj = tr.GetObject(id, OpenMode.ForRead) as BlockReference;
                                        items.Add(new Tuple <ObjectId, Point3d>(id, obj.Position.TransformBy(wcs2ucs)));
                                    }
                                }
                                // Sort items by coordinates
                                items.Sort((p1, p2) =>
                                {
                                    switch (form.Ordering)
                                    {
                                    case NumberingForm.CoordinateOrdering.IncreasingX:
                                        return(p1.Item2.X < p2.Item2.X ? -1 : 1);

                                    case NumberingForm.CoordinateOrdering.IncreasingY:
                                        return(p1.Item2.Y < p2.Item2.Y ? -1 : 1);

                                    case NumberingForm.CoordinateOrdering.DecreasingX:
                                        return(p1.Item2.X > p2.Item2.X ? -1 : 1);

                                    case NumberingForm.CoordinateOrdering.DecreasingY:
                                        return(p1.Item2.Y > p2.Item2.Y ? -1 : 1);

                                    default:
                                        return(0);
                                    }
                                });
                                // Write numbering text
                                double num    = form.StartNumber;
                                string format = form.Format;
                                foreach (Tuple <ObjectId, Point3d> item in items)
                                {
                                    bool     found  = false;
                                    ObjectId id     = item.Item1;
                                    string   numstr = num.ToString(format);
                                    string   text   = form.Prefix + numstr + form.Suffix;
                                    if (id.ObjectClass.UnmanagedObject == RXClass.GetClass(typeof(DBText)).UnmanagedObject)
                                    {
                                        DBText obj = tr.GetObject(id, OpenMode.ForWrite) as DBText;
                                        obj.TextString = text;
                                        found          = true;
                                    }
                                    else if (id.ObjectClass.UnmanagedObject == RXClass.GetClass(typeof(MText)).UnmanagedObject)
                                    {
                                        MText obj = tr.GetObject(id, OpenMode.ForWrite) as MText;
                                        obj.Contents = text;
                                        found        = true;
                                    }
                                    else if (id.ObjectClass.UnmanagedObject == RXClass.GetClass(typeof(BlockReference)).UnmanagedObject)
                                    {
                                        BlockReference obj = tr.GetObject(id, OpenMode.ForRead) as BlockReference;
                                        foreach (ObjectId attId in obj.AttributeCollection)
                                        {
                                            AttributeReference attRef = (AttributeReference)tr.GetObject(attId, OpenMode.ForRead);
                                            if (string.Compare(attRef.Tag, form.AttributeName, StringComparison.OrdinalIgnoreCase) == 0)
                                            {
                                                attRef.UpgradeOpen();
                                                attRef.TextString = text;
                                                found             = true;
                                            }
                                        }
                                    }
                                    if (found)
                                    {
                                        // Only increment if a change was made
                                        num += form.Increment;
                                    }
                                }
                            }
                            catch (System.Exception ex)
                            {
                                MessageBox.Show("Error: " + ex.ToString(), "XCOM", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }

                            tr.Commit();
                        }
                    }
                }
            }
        }
Example #18
0
        public static void TagFoundations(List <Curve> lines, Plot p)
        {
            Database acCurDb;

            acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
            ObjectContextCollection occ = acCurDb.ObjectContextManager.GetContextCollection("ACDB_ANNOTATIONSCALES");

            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                LayerTable acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;
                ObjectId   current  = acCurDb.Clayer;
                acCurDb.Clayer = acLyrTbl[Main.FoundationTextLayer];

                // Open the Block table for read
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;

                ObjectId blkRecId = ObjectId.Null;

                if (!acBlkTbl.Has("FormationTag"))
                {
                    Main.LoadBlocks();
                }

                foreach (Curve c in lines)
                {
                    Matrix3d           curUCSMatrix = Application.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem;
                    CoordinateSystem3d curUCS       = curUCSMatrix.CoordinateSystem3d;

                    //Get lable point
                    Point3d labelPoint3d = c.GetPointAtDist(c.GetDistanceAtParameter(c.EndParam) / 2);
                    Point3d labelPoint   = new Point3d(labelPoint3d.X, labelPoint3d.Y, 0);

                    // Insert the block into the current space
                    using (BlockReference acBlkRef = new BlockReference(labelPoint, acBlkTbl["FormationTag"]))
                    {
                        //Calculate Line Angle
                        double y     = c.EndPoint.Y - c.StartPoint.Y;
                        double x     = c.EndPoint.X - c.StartPoint.X;
                        double angle = Math.Atan(Math.Abs(y) / Math.Abs(x));
                        if (angle >= Math.PI / 2)
                        {
                            acBlkRef.TransformBy(Matrix3d.Rotation(0, curUCS.Zaxis, labelPoint));
                        }
                        else
                        {
                            acBlkRef.TransformBy(Matrix3d.Rotation(Math.PI / 2, curUCS.Zaxis, labelPoint));
                        }
                        acBlkRef.AddContext(occ.GetContext("10:1"));

                        //Set value
                        AttributeCollection attCol = acBlkRef.AttributeCollection;
                        foreach (ObjectId attId in attCol)
                        {
                            AttributeReference att = acTrans.GetObject(attId, OpenMode.ForRead, false) as AttributeReference;
                            if (att.Tag == "LEVEL")
                            {
                                att.UpgradeOpen();
                                att.TextString = p.FormationLevel.ToString();
                            }
                        }

                        BlockTableRecord acCurSpaceBlkTblRec;
                        acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                        acCurSpaceBlkTblRec.AppendEntity(acBlkRef);
                        acTrans.AddNewlyCreatedDBObject(acBlkRef, true);
                    }
                }

                acCurDb.Clayer = current;

                acTrans.Commit();
            }
        }
Example #19
0
        private void FindReplaceItem(Transaction tr, ObjectId id)
        {
            try
            {
                foreach (FindReplaceOptions item in options)
                {
                    string replacedText = string.Empty;

                    if (frText && id.ObjectClass.IsDerivedFrom(RXObject.GetClass(typeof(DBText))))
                    {
                        // Single line text
                        DBText text = (DBText)tr.GetObject(id, OpenMode.ForRead);
                        if (GetReplacedText(item.Find, item.Replace, text.TextString, out replacedText))
                        {
                            text.UpgradeOpen();
                            text.TextString = replacedText;
                        }
                    }
                    else if (frText && id.ObjectClass.IsDerivedFrom(RXObject.GetClass(typeof(MText))))
                    {
                        // Multiline text
                        MText mtext = (MText)tr.GetObject(id, OpenMode.ForRead);
                        if (GetReplacedText(item.Find, item.Replace, mtext.Contents, out replacedText))
                        {
                            mtext.UpgradeOpen();
                            mtext.Contents = replacedText;
                        }
                    }
                    else if (frDimension && id.ObjectClass.IsDerivedFrom(RXObject.GetClass(typeof(Dimension))))
                    {
                        // Dimension text
                        Dimension dimension = (Dimension)tr.GetObject(id, OpenMode.ForRead);
                        if (GetReplacedText(item.Find, item.Replace, dimension.DimensionText, out replacedText))
                        {
                            dimension.UpgradeOpen();
                            dimension.DimensionText = replacedText;
                        }
                    }
                    else if (frLeader && id.ObjectClass.IsDerivedFrom(RXObject.GetClass(typeof(MLeader))))
                    {
                        // Multileader
                        MLeader leader = (MLeader)tr.GetObject(id, OpenMode.ForRead);
                        if (leader.ContentType == ContentType.MTextContent)
                        {
                            if (GetReplacedText(item.Find, item.Replace, leader.MText.Contents, out replacedText))
                            {
                                leader.UpgradeOpen();
                                leader.MText.Contents = replacedText;
                            }
                        }
                    }
                    else if (frAttribute && id.ObjectClass.IsDerivedFrom(RXObject.GetClass(typeof(BlockReference))))
                    {
                        // Block attributes
                        BlockReference blockRef = (BlockReference)tr.GetObject(id, OpenMode.ForRead);
                        foreach (ObjectId attid in blockRef.AttributeCollection)
                        {
                            AttributeReference attRef = (AttributeReference)tr.GetObject(attid, OpenMode.ForRead);
                            if (GetReplacedText(item.Find, item.Replace, attRef.TextString, out replacedText))
                            {
                                attRef.UpgradeOpen();
                                attRef.TextString = replacedText;
                            }
                        }
                    }
                    else if (frTable && id.ObjectClass.IsDerivedFrom(RXObject.GetClass(typeof(Table))))
                    {
                        // Table text
                        Table table = (Table)tr.GetObject(id, OpenMode.ForWrite);
                        for (int i = 0; i < table.Rows.Count; i++)
                        {
                            for (int j = 0; j < table.Columns.Count; j++)
                            {
                                if (GetReplacedText(item.Find, item.Replace, table.Cells[i, j].TextString, out replacedText))
                                {
                                    table.Cells[i, j].TextString = replacedText;
                                }
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                OnError(ex);
            }
        }
Example #20
0
        private void click_btn_AddPart(object sender, EventArgs e)
        {
            DocumentLock loc = doc.LockDocument();

            using (loc)
            {
                drawingToAdd.Replace(@"/", @"\");
                string drawingFileName = drawingToAdd.Substring(drawingToAdd.IndexOf(@"/", (drawingToAdd.IndexOf(@"/") + 1)) + 1, drawingToAdd.Length - drawingToAdd.IndexOf(@"/", (drawingToAdd.IndexOf(@"/") + 1)) - 1);

                Database tmpDb = new Database(false, true);
                tmpDb.ReadDwgFile(@"\\thehaskellco.net\Remote\AtlantaData\_Disciplines\Controls\4 Controls Programming\DVBs and DLLs\Footprints\Panel\" + drawingToAdd + ".dwg", System.IO.FileShare.Read, true, "");
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);

                    if (!bt.Has(drawingFileName))
                    {
                        db.Insert(drawingFileName, tmpDb, true);
                    }
                    tr.Commit();
                }

                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    bt  = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                    btr = (BlockTableRecord)bt[drawingFileName].GetObject(OpenMode.ForRead);
                    bool tagFound = false;

                    if (btr != null)
                    {
                        foreach (ObjectId objId in btr)
                        {
                            AttributeDefinition attDef = tr.GetObject(objId, OpenMode.ForRead) as AttributeDefinition;
                            if (attDef != null)
                            {
                                if (attDef.Tag.ToUpper() == "TABLE_PART_NUM")
                                {
                                    tagFound = true;
                                }
                                else if (attDef.Tag.ToUpper() == "TABLE_DESCRIPTION")
                                {
                                    tagFound = true;
                                }
                            }
                        }
                        try
                        {
                            if (!tagFound)
                            {
                                AttributeDefinition acAttDef = new AttributeDefinition();

                                acAttDef.Position  = new Point3d(0, 0, 0);
                                acAttDef.Prompt    = "Part Number?";
                                acAttDef.Tag       = "TABLE_PART_NUM";
                                acAttDef.Invisible = true;
                                acAttDef.Height    = 1;
                                acAttDef.Justify   = AttachmentPoint.MiddleCenter;

                                AttributeDefinition acAttDef2 = new AttributeDefinition();

                                acAttDef2.Position  = new Point3d(0, 0, 0);
                                acAttDef2.Prompt    = "Description?";
                                acAttDef2.Tag       = "TABLE_DESCRIPTION";
                                acAttDef2.Invisible = true;
                                acAttDef2.Height    = 1;
                                acAttDef2.Justify   = AttachmentPoint.MiddleCenter;

                                AttributeDefinition acAttDef3 = new AttributeDefinition();

                                acAttDef3.Position   = new Point3d(0, -10, 0);
                                acAttDef3.Prompt     = "DO NOT CHANGE";
                                acAttDef3.Tag        = "AUTO_UPDATE";
                                acAttDef3.TextString = "1";
                                acAttDef3.Invisible  = true;
                                acAttDef3.Height     = 1;
                                acAttDef3.Justify    = AttachmentPoint.MiddleCenter;

                                btr.UpgradeOpen();
                                btr.AppendEntity(acAttDef);
                                btr.AppendEntity(acAttDef2);
                                btr.AppendEntity(acAttDef3);
                                tr.AddNewlyCreatedDBObject(acAttDef, true);
                                tr.AddNewlyCreatedDBObject(acAttDef2, true);
                                tr.AddNewlyCreatedDBObject(acAttDef3, true);
                                btr.DowngradeOpen();
                                tr.Commit();
                            }
                        }
                        catch
                        {
                            ed.WriteMessage("Could not add attributes to block.");
                        }
                    }
                }

                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    if (btr != null)
                    {
                        MyBlockJig   blockJig = new MyBlockJig();
                        Point3d      point;
                        PromptResult res = blockJig.DragMe(btr.ObjectId, out point);

                        if (res.Status == PromptStatus.OK)
                        {
                            BlockTableRecord curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                            BlockReference   insert   = new BlockReference(point, btr.ObjectId);

                            curSpace.AppendEntity(insert);
                            tr.AddNewlyCreatedDBObject(insert, true);

                            foreach (ObjectId id in btr)
                            {
                                DBObject            obj    = id.GetObject(OpenMode.ForWrite);
                                AttributeDefinition attDef = obj as AttributeDefinition;
                                if ((attDef != null) && (!attDef.Constant))
                                {
                                    using (AttributeReference attRef = new AttributeReference())
                                    {
                                        attRef.SetAttributeFromBlock(attDef, insert.BlockTransform);
                                        insert.AttributeCollection.AppendAttribute(attRef);
                                        tr.AddNewlyCreatedDBObject(attRef, true);
                                    }
                                }
                            }
                        }
                    }
                    tr.Commit();
                    doc.SendStringToExecute("regenall ", true, false, false);
                }

                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    ObjectId msId = bt[BlockTableRecord.ModelSpace];

                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(msId, OpenMode.ForRead);
                    foreach (ObjectId entId in btr)
                    {
                        Entity ent = (Entity)tr.GetObject(entId, OpenMode.ForWrite);
                        if (ent != null)
                        {
                            BlockReference br = ent as BlockReference;
                            if (br != null)
                            {
                                BlockTableRecord bd = (BlockTableRecord)tr.GetObject(br.BlockTableRecord, OpenMode.ForRead);
                                if (bd.Name.ToUpper() == drawingFileName.ToUpper())
                                {
                                    foreach (ObjectId arId in br.AttributeCollection)
                                    {
                                        DBObject           obj = tr.GetObject(arId, OpenMode.ForWrite);
                                        AttributeReference ar  = obj as AttributeReference;
                                        if (ar != null)
                                        {
                                            if (ar.Tag == "AUTO_UPDATE")
                                            {
                                                if (ar.TextString == "1")
                                                {
                                                    foreach (ObjectId attRefId in br.AttributeCollection)
                                                    {
                                                        DBObject           objRef = tr.GetObject(attRefId, OpenMode.ForWrite);
                                                        AttributeReference aRef   = objRef as AttributeReference;
                                                        if (aRef.Tag == "TABLE_PART_NUM")
                                                        {
                                                            aRef.UpgradeOpen();
                                                            aRef.TextString = partNumberToAdd;
                                                            aRef.DowngradeOpen();
                                                        }
                                                        else if (aRef.Tag == "TABLE_DESCRIPTION")
                                                        {
                                                            aRef.UpgradeOpen();
                                                            aRef.TextString = descriptionToAdd;
                                                            aRef.DowngradeOpen();
                                                        }
                                                    }
                                                }

                                                ar.TextString = "0";
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    tr.Commit();
                }
            }
        }
        public static int UpdateAttributesInBlock(

            Database db,

            ObjectId btrId,

            string blockName,

            string attbName,

            string attbValue

            )

        {
            // Will return the number of attributes modified

            int changedCount = 0;

            Transaction tr =

                db.TransactionManager.StartTransaction();

            using (tr)

            {
                BlockTableRecord btr =

                    (BlockTableRecord)tr.GetObject(

                        btrId,

                        OpenMode.ForRead

                        );

                // Test each entity in the container...

                foreach (ObjectId entId in btr)

                {
                    Entity ent =

                        tr.GetObject(entId, OpenMode.ForRead)

                        as Entity;

                    if (ent != null)

                    {
                        BlockReference br = ent as BlockReference;

                        if (br != null)

                        {
                            BlockTableRecord bd =

                                (BlockTableRecord)tr.GetObject(

                                    br.BlockTableRecord,

                                    OpenMode.ForRead

                                    );

                            // ... to see whether it's a block with

                            // the name we're after
                            Debug.WriteLine(bd.Name);
                            if (bd.Name.ToUpper() == blockName.ToUpper())

                            {
                                // Check each of the attributes...

                                foreach (

                                    ObjectId arId in br.AttributeCollection

                                    )

                                {
                                    DBObject obj =

                                        tr.GetObject(

                                            arId,

                                            OpenMode.ForRead

                                            );

                                    AttributeReference ar =

                                        obj as AttributeReference;

                                    if (ar != null)

                                    {
                                        Debug.WriteLine(ar.TextString);
                                        // ... to see whether it has

                                        // the tag we're after

                                        if (ar.Tag.ToUpper() == attbName.ToUpper())

                                        {
                                            // If so, update the value

                                            // and increment the counter

                                            ar.UpgradeOpen();

                                            ar.TextString = attbValue;

                                            ar.DowngradeOpen();

                                            changedCount++;
                                        }
                                    }
                                }
                            }

                            // Recurse for nested blocks

                            changedCount +=

                                UpdateAttributesInBlock(

                                    db,

                                    br.BlockTableRecord,

                                    blockName,

                                    attbName,

                                    attbValue

                                    );
                        }
                    }
                }

                tr.Commit();
            }

            return(changedCount);
        }
Example #22
0
        public string CHECKATTRIBUTEVALUE(
            string blockName,
            string attbName
            )
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor   ed  = doc.Editor;
            Database db  = doc.Database;

            doc.LockDocument();
            string attributeValue = "";

            // Get the IDs of the spaces we want to process
            // and simply call a function to process each

            ObjectId    psId;
            Transaction tr1 =
                db.TransactionManager.StartTransaction();

            using (tr1)
            {
                BlockTable bt =
                    (BlockTable)tr1.GetObject(
                        db.BlockTableId,
                        OpenMode.ForRead
                        );
                psId =
                    bt[BlockTableRecord.PaperSpace];

                // Not needed, but quicker than aborting
                tr1.Commit();
            }


            // Will return the value of the named attribute.

            Transaction tr2 =
                doc.TransactionManager.StartTransaction();

            using (tr2)
            {
                BlockTableRecord btr =
                    (BlockTableRecord)tr2.GetObject(
                        psId,
                        OpenMode.ForRead
                        );

                // Test each entity in the container...

                foreach (ObjectId entId in btr)
                {
                    Entity ent =
                        tr2.GetObject(entId, OpenMode.ForRead)
                        as Entity;

                    if (ent != null)
                    {
                        //////////attributeValue = "1";

                        BlockReference br = ent as BlockReference;
                        if (br != null)
                        {
                            ////////////attributeValue = "2";
                            BlockTableRecord bd =
                                (BlockTableRecord)tr2.GetObject(
                                    br.BlockTableRecord,
                                    OpenMode.ForRead
                                    );

                            // ... to see whether it's a block with
                            // the name we're after

                            ////////////attributeValue = bd.Name.ToUpper() + " = " + blockName;

                            if (bd.Name.ToUpper() == blockName)
                            {
                                // Check each of the attributes...

                                foreach (ObjectId arId in br.AttributeCollection)
                                {
                                    DBObject           obj = tr2.GetObject(arId, OpenMode.ForRead);
                                    AttributeReference ar  =
                                        obj as AttributeReference;
                                    if (ar != null)
                                    {
                                        //attributeValue = ar.Tag.ToUpper() + " = " + attbName;
                                        // ... to see whether it has
                                        // the tag we're after

                                        if (ar.Tag.ToUpper() == attbName)
                                        {
                                            ////////////attributeValue = "5";
                                            // If so, update the value
                                            // and increment the counter

                                            ar.UpgradeOpen();
                                            attributeValue = ar.TextString;
                                            ar.DowngradeOpen();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                tr2.Commit();
            }
            return(attributeValue);
        }