.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Block Reference Problem

0 REPLIES 0
Reply
Message 1 of 1
Nawara17
156 Views, 0 Replies

Block Reference Problem

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
0 REPLIES 0

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost