コード例 #1
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");
            }
        }