Valued Contributor
Posts: 52
Registered: ‎03-27-2006
Message 1 of 1 (44 Views)

Block Reference Problem

44 Views, 0 Replies
08-27-2006 08:18 AM
Hi all,

Please, can anyone help?? I am trying to insert new block reference to my drawing but an error appears, tells:
"Opearation is not valid due to the current state of the object"

the following is my code:

Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
using(Transaction trans = db.TransactionManager.StartTransaction())
PromptPointOptions Pos = new PromptPointOptions("Enter Display Position");
Pos.AllowNone = false;
PromptPointResult PosRes = ed.GetPoint(Pos);
if( PosRes.Status != PromptStatus.OK )
MessageBox.Show("The process is canceled.");

and this is the code of CreateBlockRef:

Database db = HostApplicationServices.WorkingDatabase;
Transaction trans = db.TransactionManager.StartTransaction();
using(DocumentLock docLock=AcadAPP.DocumentManager.MdiActiveDocument.LockDocument())
The error is in this Line>>>>>>>>>>BlockTable bt= (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
BlockReference br = new BlockReference(pos, CreateDefinition(i));
AttributeReference attRef = new AttributeReference();

BlockTableRecord dsBtr = (BlockTableRecord)trans.GetObject(bt[MyBlockName], OpenMode.ForRead);
ObjectId layerID = CreateLayer(LayerName);

foreach(ObjectId id in dsBtr )
Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead, false);
if( ent.GetType().FullName.Equals("Autodesk.AutoCAD.DatabaseServices.AttributeDefinition") )
AttributeDefinition attDef = (AttributeDefinition)ent;
attRef.Position = new Point3d(attDef.Position.X + br.Position.X,attDef.Position.Y + br.Position.Y,attDef.Position.Z + br.Position.Z);
attRef.Height = attDef.Height;
attRef.Rotation = attDef.Rotation;
attRef.Tag = attDef.Tag;
attRef.TextString = MyText;



btr.AppendEntity(br); br.AttributeCollection.AppendAttribute(attRef);
trans.AddNewlyCreatedDBObject(attRef, true);
trans.AddNewlyCreatedDBObject(br, true); trans.Commit();
return br.ObjectId;

BTW, I am using Acad2006, vs2003

Thanks in advance
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.