Author Topic: Best way to grab block to edit  (Read 2552 times)

0 Members and 1 Guest are viewing this topic.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Best way to grab block to edit
« on: November 30, 2010, 11:05:51 AM »
I am trying to grab a block I just inserted to edit the attributes.  This works, but I know there has got to be a better way.
Any suggestions on best way to grab the block.  I do know the ObjectId, as I just inserted it.
Code: [Select]
if (bt.Has(strBlock))
{
foreach (ObjectId entId in CurrentSpaceBTR)
{
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
);
if (bd.Name.ToUpper() == strBlock)
{
foreach (
ObjectId arId in br.AttributeCollection
)
{
DBObject obj = tr.GetObject(arId, OpenMode.ForRead);
AttributeReference ar = obj as AttributeReference;
if (ar != null)
{
ar.UpgradeOpen();
switch (ar.Tag)
{
case "TITLE1":
ar.TextString = idAttlist["TITLE1"];
break;
case "TITLE2":
ar.TextString = idAttlist["TITLE2"];
break;
case "TECH":
ar.TextString = idAttlist["TECH"];
break;
case "TECHDATE":
ar.TextString = idAttlist["TECHDATE"];
break;
}
ar.DowngradeOpen();
}
}
}
}
}
}
}
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Best way to grab block to edit
« Reply #1 on: November 30, 2010, 11:25:28 AM »
How did you insert it?  If you inserted it though code that comes before, then I would just use what was returned when you inserted it.  I would not want to step through the current layout's entities each time I inserted a block, just to fill out attributes.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Best way to grab block to edit
« Reply #2 on: November 30, 2010, 12:16:48 PM »
I agree, but I cant seem to grab the insertion. 
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Best way to grab block to edit
« Reply #3 on: November 30, 2010, 12:22:50 PM »
This is how I inserted the block
Code: [Select]
if (bt.Has(strBlock))
{
ObjectId tmpBlockId = bt[strBlock];
BlockTableRecord blkDefRecord = (BlockTableRecord)tr.GetObject(tmpBlockId, OpenMode.ForRead);
//BlockReference br = new BlockReference(ppr.Value, tmpBlockId);
BlockReference br = new BlockReference(insPt, tmpBlockId);
CurrentSpaceBTR.AppendEntity(br);
if (blkDefRecord.HasAttributeDefinitions)
{
foreach (ObjectId idAtt in blkDefRecord)
{
AttributeDefinition attDef = tr.GetObject(idAtt, OpenMode.ForRead) as AttributeDefinition;
if (attDef != null)
{
AttributeReference attRef = new AttributeReference();
attRef.SetAttributeFromBlock(attDef, br.BlockTransform);
br.AttributeCollection.AppendAttribute(attRef);
tr.AddNewlyCreatedDBObject(attRef, true);
}
}
tr.AddNewlyCreatedDBObject(br, true);
}
}
else
{
string sourceFileName = string.Concat(strPath, strBlock, ".dwg");
try
{
using (Database tempDb = new Database(false, true))
{
tempDb.ReadDwgFile(sourceFileName, FileShare.Read, true, null);
db.Insert(strBlock, tempDb, false);
}
ObjectId tmpBlockId = bt[strBlock];
BlockTableRecord blkDefRecord = (BlockTableRecord)tr.GetObject(tmpBlockId, OpenMode.ForRead);
//BlockReference br = new BlockReference(ppr.Value, tmpBlockId);
BlockReference br = new BlockReference(insPt, tmpBlockId);
CurrentSpaceBTR.AppendEntity(br);
if (blkDefRecord.HasAttributeDefinitions)
{
foreach (ObjectId idAtt in blkDefRecord)
{
AttributeDefinition attDef = tr.GetObject(idAtt, OpenMode.ForRead) as AttributeDefinition;
if (attDef != null)
{
AttributeReference attRef = new AttributeReference();
attRef.SetAttributeFromBlock(attDef, br.BlockTransform);
attRef.SetPropertiesFrom(attDef);
br.AttributeCollection.AppendAttribute(attRef);
tr.AddNewlyCreatedDBObject(attRef, true);
}
}
tr.AddNewlyCreatedDBObject(br, true);
}
}
catch (Autodesk.AutoCAD.Runtime.Exception e)
{
ed.WriteMessage(e.Message);
}
}
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

LE3

  • Guest
Re: Best way to grab block to edit
« Reply #4 on: November 30, 2010, 12:28:42 PM »
any .Commit() call somewhere?

could be....

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Best way to grab block to edit
« Reply #5 on: November 30, 2010, 12:32:52 PM »
Hey LE, yea its further down.  I didnt post the whole thing as it takes up a lot of space.  Ill post the whole thing.
Code: [Select]

using System;
//using System.Collections;   //Dont need this as we are using Generics
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using System.Windows.Forms;

namespace TEPTITLE
{
    public class TEPTITLE
    {
        [CommandMethod("TEPTitle")]
        public static void TEPTitleBlock()
        {
            int cbo1int;
            TitleBlock.TepTitleBlock frm = new TitleBlock.TepTitleBlock();
            Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(frm);
            cbo1int = frm.BlockTypeIndex;

            Database db = HostApplicationServices.WorkingDatabase;
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

            string strUser = System.Environment.UserName.ToUpper();

            DateTime dateToday = DateTime.Today;
            DateTime dateOnly = dateToday.Date;
            string strDate = dateOnly.ToString("d");

            Point3d insPt = new Point3d(0, 0, 0);
            string strBlock = null;
            #region TitleChoice
            switch (cbo1int)
            {
                case 0:
                    strBlock = "TEP-INFO";
                    break;
                case 1:
                    strBlock = "VTEP-INFO";
                    break;
                case 2:
                    strBlock = "TEP-CIP";
                    break;
                case 3:
                    strBlock = "VTEP-CIP";
                    break;
                case 4:
                    strBlock = "UES-INFO";
                    break;
                case 5:
                    strBlock = "VUES-INFO";
                    break;
                case 6:
                    strBlock = "UES-CIP";
                    break;
                case 7:
                    strBlock = "VUES-CIP";
                    break;
                default:
                    break;

            }
            #endregion

            #region SwitchUser
            switch (strUser)
            {
                case "UA02038":
                    strUser = "D.HALL";
                    break;
                case "UA00648":
                    strUser = "D.LIVINGSTONE";
                    break;
                case "UA00071":
                    strUser = "G.TAYLOR";
                    break;
                case "UA05645":
                    strUser = "N.MERCHANT";
                    break;
                case "UA00664":
                    strUser = "P.OLIVAS";
                    break;
                case "UA50151":
                    strUser = "L.RICHARDSON";
                    break;
                case "UA50270":
                    strUser = "A.DOMINGUEZ";
                    break;
                case "UA50050":
                    strUser = "D.LOPEZ";
                    break;
                case "UA50463":
                    strUser = "C.PADILLA";
                    break;
            }
            #endregion

            IDictionary<string, string> idAttlist = new Dictionary<string, string>();
            //idAttlist.Add("TagName", "StringValue");
            idAttlist.Add("TITLE1", "AC SCHEMATIC");
            idAttlist.Add("TITLE2", "DC SCHEMATIC");
            idAttlist.Add("TECH", strUser);
            idAttlist.Add("TECHDATE", strDate);

            string strPath = @"S:\TITLEBLOCKS\";

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

                #region InsertBlockFirstTime
                if (bt.Has(strBlock))
                {
                    ObjectId tmpBlockId = bt[strBlock];
                    BlockTableRecord blkDefRecord = (BlockTableRecord)tr.GetObject(tmpBlockId, OpenMode.ForRead);
                    //BlockReference br = new BlockReference(ppr.Value, tmpBlockId);
                    BlockReference br = new BlockReference(insPt, tmpBlockId);
                    CurrentSpaceBTR.AppendEntity(br);
                    if (blkDefRecord.HasAttributeDefinitions)
                    {
                        foreach (ObjectId idAtt in blkDefRecord)
                        {
                            AttributeDefinition attDef = tr.GetObject(idAtt, OpenMode.ForRead) as AttributeDefinition;
                            if (attDef != null)
                            {
                                AttributeReference attRef = new AttributeReference();
                                attRef.SetAttributeFromBlock(attDef, br.BlockTransform);
                                br.AttributeCollection.AppendAttribute(attRef);
                                tr.AddNewlyCreatedDBObject(attRef, true);
                            }
                        }
                        tr.AddNewlyCreatedDBObject(br, true);
                    }
                }
                else
                {
                    string sourceFileName = string.Concat(strPath, strBlock, ".dwg");
                    try
                    {
                        using (Database tempDb = new Database(false, true))
                        {
                            tempDb.ReadDwgFile(sourceFileName, FileShare.Read, true, null);
                            db.Insert(strBlock, tempDb, false);
                        }
                        ObjectId tmpBlockId = bt[strBlock];
                        BlockTableRecord blkDefRecord = (BlockTableRecord)tr.GetObject(tmpBlockId, OpenMode.ForRead);
                        //BlockReference br = new BlockReference(ppr.Value, tmpBlockId);
                        BlockReference br = new BlockReference(insPt, tmpBlockId);
                        CurrentSpaceBTR.AppendEntity(br);
                        if (blkDefRecord.HasAttributeDefinitions)
                        {
                            foreach (ObjectId idAtt in blkDefRecord)
                            {
                                AttributeDefinition attDef = tr.GetObject(idAtt, OpenMode.ForRead) as AttributeDefinition;
                                if (attDef != null)
                                {
                                    AttributeReference attRef = new AttributeReference();
                                    attRef.SetAttributeFromBlock(attDef, br.BlockTransform);
                                    attRef.SetPropertiesFrom(attDef);
                                    br.AttributeCollection.AppendAttribute(attRef);
                                    tr.AddNewlyCreatedDBObject(attRef, true);
                                }
                            }
                            tr.AddNewlyCreatedDBObject(br, true);
                        }
                    }
                    catch (Autodesk.AutoCAD.Runtime.Exception e)
                    {
                        ed.WriteMessage(e.Message);
                    }
                }
                #endregion

                #region EditAttributes
                if (bt.Has(strBlock))
                {
                    foreach (ObjectId entId in CurrentSpaceBTR)
                    {
                        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
                                );
                                if (bd.Name.ToUpper() == strBlock)
                                {
                                    foreach (
                                    ObjectId arId in br.AttributeCollection
                                    )
                                    {
                                        DBObject obj = tr.GetObject(arId, OpenMode.ForRead);
                                        AttributeReference ar = obj as AttributeReference;
                                        if (ar != null)
                                        {
                                            ar.UpgradeOpen();
                                            switch (ar.Tag)
                                            {
                                                case "TITLE1":
                                                    ar.TextString = idAttlist["TITLE1"];
                                                    break;
                                                case "TITLE2":
                                                    ar.TextString = idAttlist["TITLE2"];
                                                    break;
                                                case "TECH":
                                                    ar.TextString = idAttlist["TECH"];
                                                    break;
                                                case "TECHDATE":
                                                    ar.TextString = idAttlist["TECHDATE"];
                                                    break;
                                            }
                                            ar.DowngradeOpen();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                #endregion
                tr.Commit();
            }
        }
    }
}
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Best way to grab block to edit
« Reply #6 on: November 30, 2010, 12:34:24 PM »
This is all proof of concept code, so paths are hard-pathed, the IDictionary is being populated with bogus text, the only true variable is the combobox on the form where you can choose b/t 8 different titleblocks
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Best way to grab block to edit
« Reply #7 on: November 30, 2010, 12:51:00 PM »
Do you want a example to edit the attributes reference as you insert the block reference

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Best way to grab block to edit
« Reply #8 on: November 30, 2010, 01:27:13 PM »
Tim you are a genius!  Here is Tim's solution to my brain block!
Code: [Select]
if (!bt.Has(strBlock))
                {
                    string sourceFileName = string.Concat(strPath, strBlock, ".dwg");
                    try
                    {
                        using (Database tempDb = new Database(false, true))
                        {
                            tempDb.ReadDwgFile(sourceFileName, FileShare.Read, true, null);
                            db.Insert(strBlock, tempDb, false);
                        }
                    }
                    catch (Autodesk.AutoCAD.Runtime.Exception e)
                    {
                        ed.WriteMessage(e.Message);
                         CantInsert = true;
                    }
                }
                if (CantInsert) return;
                ObjectId tmpBlockId = bt[strBlock];
                BlockTableRecord blkDefRecord = (BlockTableRecord)tr.GetObject(tmpBlockId, OpenMode.ForRead);
                BlockReference br = new BlockReference(insPt, tmpBlockId);
                CurrentSpaceBTR.AppendEntity(br);
                if (blkDefRecord.HasAttributeDefinitions)
                {
                    foreach (ObjectId idAtt in blkDefRecord)
                    {
                        AttributeDefinition attDef = tr.GetObject(idAtt, OpenMode.ForRead) as AttributeDefinition;
                        if (attDef != null)
                        {
                            AttributeReference attRef = new AttributeReference();
                            attRef.SetAttributeFromBlock(attDef, br.BlockTransform);
                            attRef.SetPropertiesFrom(attDef);
                            switch (attRef.Tag)
                            {
                                case "TITLE1":
                                    attRef.TextString = idAttlist["TITLE1"];
                                    break;
                                case "TITLE2":
                                    attRef.TextString = idAttlist["TITLE2"];
                                    break;
                                case "TECH":
                                    attRef.TextString = idAttlist["TECH"];
                                    break;
                                case "TECHDATE":
                                    attRef.TextString = idAttlist["TECHDATE"];
                                    break;
                            }
                            br.AttributeCollection.AppendAttribute(attRef);
                            tr.AddNewlyCreatedDBObject(attRef, true);
                        }
                    }
                    tr.AddNewlyCreatedDBObject(br, true);
                }

Jeff, I was able to edit the attributes, I was just doing it VERY inefficiently, and Tim helped set me straight.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)