Hi All,
I think I have gotten to a point with this where I can ask this as an intelligent question and I may actually understand the answer. This is what I am trying to do:
1. Search drawing for Title block reference.
a. Get insert point, attributes
2. Insert new title block at same location as old title block
a. Get attributes
3. Compare attribute tags, get the textstring values from the old title block and put them into the new title block. Note: The tags are identically named between the old and new blocks, both blocks contain 16 attributes, labeled CTB1, CTB2, etc..
4. Delete the old titleblock.
This is my first attempt at writing a C# program for AutoCAD (at least one that is not in the tutorial) and the only reason I have gotten this far is because of folks like Kerry, Jeff and other Swampers.
I’m pretty sure I have backed myself into a corner, so I need help (of any kind)!
I do recognize that my variables are going out of scope, so I believe this should be broken down into more procedures. Any assistance/guidance is most definitely appreciated!
This is what I have so far:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.EditorInput;
namespace GetTitleBlkInfo
{
public class GetTitleBlkInfo
{
[CommandMethod("GetTitleBlk")]
public static void GetOldTitleBlk()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
string[] oldBlkName = new string[] { "ctml0012", "ctml0011", "ctml0010" };
String strBlkName = "J:\\acad\\ctml0013.dwg";
Point3d InsPoint = new Point3d();
//AttributeCollection oldAttCol;
//AttributeCollection newAttCol;
string newName = "ctml0013";
string oldName = "";
int i = 0;
//Start Transaction
using (Transaction tr = db.TransactionManager.StartTransaction())
{
//Get the block table and check for old titleblock
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
while (i < 3)
{
if (bt.Has(oldBlkName[i]))
{
oldName = oldBlkName[i];
//Open BlkTable Record for modelspace
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
foreach (ObjectId blkid in btr)
{
BlockReference oldTBlkRef = tr.GetObject(blkid, OpenMode.ForRead) as BlockReference;
if (oldTBlkRef.Name == oldName)
{
ed.WriteMessage("\nblock name is: " + oldTBlkRef.Name);
InsPoint = oldTBlkRef.Position;
Scale3d scale = oldTBlkRef.ScaleFactors;
//oldAttCol = oldTBlkRef.AttributeCollection;
}//close if oldtblkref.name
}//close foreach ObjectId
}//close if oldBlkName
i = i + 1;
}//close while i
if (!bt.Has(newName))
{
bt.UpgradeOpen();
Database tmpDb = new Database(false, true);
tmpDb.ReadDwgFile(strBlkName, System.IO.FileShare.Read, true, null);
//insert the block into the block table
ObjectId BlkId = db.Insert(strBlkName, tmpDb, true);
//Get the block Table record for the block
BlockTableRecord newBtr = (BlockTableRecord)tr.GetObject(BlkId, OpenMode.ForWrite);
//Name the block table record
newBtr.Name = newName;
//create the block reference and get modelspace Block table record
BlockReference br = new BlockReference(InsPoint, BlkId);
BlockTableRecord ms = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
br.Position = InsPoint;
ms.AppendEntity(br);
tr.AddNewlyCreatedDBObject(br, true);
//newAttCol = br.AttributeCollection;
}//close if newName
InsertAttributes(oldName);
//commit the changes and dispose of the transaction
tr.Commit();
}//close using
}//close public
public static void InsertAttributes(string oldBName)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
AttributeCollection oldAttCol;
AttributeCollection newAttCol;
BlockReference tblkNew;
string newBName = "ctml0013";
//Start Transaction
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt2 = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
//Open BlkTable Record for modelspace
BlockTableRecord btr2 = tr.GetObject(bt2[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
foreach (ObjectId blkid in btr2)
{
BlockReference TBlk = tr.GetObject(blkid , OpenMode.ForRead) as BlockReference;
if (TBlk.Name == oldBName)
{
BlockReference tblkOld = (BlockReference)tr.GetObject(blkid, OpenMode.ForRead);
oldAttCol = tblkOld.AttributeCollection;
}
if (TBlk.Name == newBName)
{
tblkNew = (BlockReference)tr.GetObject(blkid, OpenMode.ForWrite);
newAttCol = tblkNew.AttributeCollection;
}
}
foreach (ObjectId attOldId in oldAttCol)
{
foreach (ObjectId attNewId in newAttCol)
{
AttributeReference oldAtt = (AttributeReference)tr.GetObject(attOldId, OpenMode.ForRead);
AttributeReference newAtt = (AttributeReference)tr.GetObject(attNewId, OpenMode.ForWrite);
if (oldAtt.Tag == newAtt.Tag)
{
newAtt.TextString = oldAtt.TextString;
tblkNew.AttributeCollection.AppendAttribute(newAtt);
tr.AddNewlyCreatedDBObject(newAtt, true);
}
}//close foreach attNewId
}//close foreach attOldId
tr.Commit();
}//close second transaction
} //close public
}//close class
}//close namespace