public class BlockUtils
{
public ObjectIdCollection ids = new ObjectIdCollection();
public void curdb_ObjectAppended(object sender, ObjectEventArgs e)
{
//add the object id
ids.Add(e.DBObject.ObjectId);
}
[CommandMethod("reins")]
public void ReinsertBlock()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
try
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord m_ctab = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead, false, true);
PromptStringOptions psto = new PromptStringOptions("\nEnter the new block name: ");
psto.AllowSpaces = true;
PromptResult stres;
stres = ed.GetString(psto);
if (stres.Status != PromptStatus.OK)
return;
try
{
// Validate the provided block name
SymbolUtilityServices.ValidateSymbolName(stres.StringResult, false);
}
catch
{
ed.WriteMessage("\n\"{0}\"n is an invalid name, try another one.", stres.StringResult);
return;
}
string blockname = stres.StringResult;
if (bt.Has(blockname))
{
Autodesk.AutoCAD.Runtime.ErrorStatus es = ErrorStatus.DuplicateRecordName;
ed.WriteMessage("\n{0}\n{1}\n", es.ToString(), "Block already exist, try another name");
return;
}
PromptEntityOptions peo = new PromptEntityOptions("\nSelect block: ");
peo.SetRejectMessage("\nYou have to select BlockReference only!");
peo.AllowNone = false;
peo.AllowObjectOnLockedLayer = true;
peo.AddAllowedClass(typeof(BlockReference), false);
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK) return;
ObjectId objId = per.ObjectId;
if (!objId.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(BlockReference))))
{
ed.WriteMessage( "\nYou didn't select a BlockReference, please try again...\n");
return;
}
DBObject blkobj = (DBObject)tr.GetObject(objId, OpenMode.ForRead, false);
BlockReference bref = blkobj as BlockReference;
if (bref == null) return;
BlockTableRecord btrec = null;
btrec = tr.GetObject(bref.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
DBObjectCollection explobjs = new DBObjectCollection();
ids.Clear();
// Add event
db.ObjectAppended += new ObjectEventHandler(curdb_ObjectAppended);
bref.ExplodeToOwnerSpace();
// Remove event
db.ObjectAppended -= new ObjectEventHandler(curdb_ObjectAppended);
ObjectIdCollection newids = new ObjectIdCollection();
// Create BlockTableRecord
Matrix3d ucs2wcs = ed.CurrentUserCoordinateSystem;
Matrix3d wcs2ucs = ucs2wcs.Inverse();
// Add BlockTableRecord to Database
BlockTableRecord newbtr = new BlockTableRecord();
newbtr.Origin = btrec.Origin;
newbtr.Name = blockname;
newbtr.Units = bref.BlockUnit;
newbtr.Annotative = btrec.Annotative;
bt.UpgradeOpen();
ObjectId newblkid = bt.Add(newbtr);
tr.AddNewlyCreatedDBObject(newbtr, true);
// Pick displacement point, use pan and zoom to find an empty area for copy of the block
PromptPointOptions ppo = new PromptPointOptions("\nPick a displacement point: ");
ppo.BasePoint = bref.Position;
ppo.UseBasePoint = true;
ppo.UseDashedLine = true;
PromptPointResult pres = ed.GetPoint(ppo);
if (pres.Status != PromptStatus.OK) return;
Point3d movept = pres.Value;
//create vector for displacement
Vector3d vectrans = new Vector3d();
vectrans = movept.GetAsVector().Subtract(bref.Position.GetAsVector());
Matrix3d mat = Matrix3d.Displacement(vectrans);
foreach (ObjectId id in ids)
{
Entity ent = tr.GetObject(id, OpenMode.ForWrite) as Entity;
ent.TransformBy(mat);
tr.TransactionManager.QueueForGraphicsFlush();
}
PromptEntityOptions pxo = new PromptEntityOptions("\nSelect entity to remove from block: ");
pxo.AllowNone = false;
pxo.AllowObjectOnLockedLayer = true;
PromptEntityResult pxr = ed.GetEntity(pxo);
if (pxr.Status != PromptStatus.OK) return;
ObjectId exId = pxr.ObjectId;
ids.Remove(exId);
Entity exnt = (Entity)tr.GetObject(exId, OpenMode.ForWrite);
exnt.Erase();
exnt.Dispose();
newbtr.Origin = movept;
newbtr.AssumeOwnershipOf(ids);
tr.TransactionManager.QueueForGraphicsFlush();
if (btrec.HasAttributeDefinitions)
{
// Transform objects now in block from WCS to UCS
// and collect attributes
System.Collections.Generic.List<AttributeDefinition> attdefs =
new System.Collections.Generic.List<AttributeDefinition>();
foreach (ObjectId oid in newbtr)
{
Entity ent = (Entity)tr.GetObject(oid, OpenMode.ForWrite);
ent.TransformBy(wcs2ucs);
AttributeDefinition ad = ent as AttributeDefinition;
if (ad != null && !ad.Constant) attdefs.Add(ad);
}
// Create BlockReference
using (BlockReference newbref = new BlockReference(Point3d.Origin, bt[blockname]))
{
newbref.TransformBy(ucs2wcs * Matrix3d.Displacement(movept.GetAsVector()));
ObjectContextCollection occ = db.ObjectContextManager.GetContextCollection("ACDB_ANNOTATIONSCALES");
if (occ.HasContext(db.Cannoscale.Name))
Autodesk.AutoCAD.Internal.ObjectContexts.AddContext(
newbref, occ.GetContext(db.Cannoscale.Name));
// Add BlockReference to current space
m_ctab.AppendEntity(newbref);
tr.AddNewlyCreatedDBObject(newbref, true);
// Add attributes to BlockReference, if any
foreach (AttributeDefinition ad in attdefs)
using (AttributeReference attref = new AttributeReference())
{
attref.SetAttributeFromBlock(ad, newbref.BlockTransform);
if (ad.IsMTextAttributeDefinition)
attref.UpdateMTextAttribute();
attref.AdjustAlignment(db);
newbref.AttributeCollection.AppendAttribute(attref);
tr.AddNewlyCreatedDBObject(attref, true);
}
}
}
doc.TransactionManager.FlushGraphics();
tr.TransactionManager.QueueForGraphicsFlush();
tr.Commit();
}
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage
(ex.Message + "\n" + ex.StackTrace);
}
finally
{
}
}
}
~'J'~