.NET

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

Block Reference Problem

43 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.");
trans.Abort();
return;
}
CreateBlockRef(PosRes.Value,i);
trans.Commit();
}


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);
br.LayerId=layerID;

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.SetPropertiesFrom(attDef);
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
Noora
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

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