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

Need urgent help [How to insert block reference]

9 REPLIES 9
Reply
Message 1 of 10
Anonymous
477 Views, 9 Replies

Need urgent help [How to insert block reference]

I am trying to insert a block reference from block definition available in a drawing.
Please see below C# code. Any reason why it is not adding block reference to drawing?
I can see it has even assigned handle number to the new block reference but not added to drawing screen.
I need a help on this badly.

Transaction trans1 = currentDoc.TransactionManager.StartTransaction();
BlockTable bTable = (BlockTable)trans1.GetObject(currentDoc.Database.BlockTableId, OpenMode.ForWrite);
BlockTableRecord tBlock = (BlockTableRecord) bTable["RoomID"].GetObject(OpenMode.ForWrite);
Autodesk.AutoCAD.Geometry.Point3d refPoint = new Autodesk.AutoCAD.Geometry.Point3d(0, 0, 0);
BlockReference newRef = new BlockReference(refPoint, tBlock.ObjectId);
newRef.Layer = "0";
newRef.Rotation = 0;
Autodesk.AutoCAD.Geometry.Scale3d sc3d = new Autodesk.AutoCAD.Geometry.Scale3d(1, 1, 1);
.ScaleFactors = sc3d;

currentDoc.Editor.WriteMessage("Block name: " + newRef.Name + " , Handle = " + newRef.Handle + "\n");
AttributeCollection attCol = newRef.AttributeCollection;
tBlock.AppendEntity(newRef);
trans1.AddNewlyCreatedDBObject(newRef, true);

foreach (ObjectId testAttId in tBlock)
{
DBObject test =testAttId.GetObject(OpenMode.ForRead, false, true);

currentDoc.Editor.WriteMessage(test.GetType() + " ,\n");
if (test.GetType().Name == "AttributeDefinition")
{
AttributeDefinition addAttDef = (AttributeDefinition)test;
AttributeReference addAttRef = new AttributeReference();
addAttRef.SetAttributeFromBlock(addAttDef, newRef.BlockTransform);
addAttRef.TextString = addAttDef.TextString;
attCol.AppendAttribute(addAttRef);
trans1.AddNewlyCreatedDBObject(addAttRef, true);
}
}
currentDoc.Editor.WriteMessage("Block name: " + newRef.Name + " , Handle = " + newRef.Handle + " , count " + attCol.Count + "\n");
trans1.Commit();
9 REPLIES 9
Message 2 of 10
chiefbraincloud
in reply to: Anonymous


BlockTableRecord tBlock = (BlockTableRecord) bTable["RoomID"].GetObject(OpenMode.ForWrite);

BlockReference newRef = new BlockReference(refPoint, tBlock.ObjectId);

tBlock.AppendEntity(newRef);

trans1.AddNewlyCreatedDBObject(newRef, true);



The above lines of code (ignoring the other property stuff in between) insert the RoomID block into the RoomID block.

Open a Write reference to the block ModelSpace, and do ModelSpace.AppendEntity(newref) instead of tblock.append.

Dave O.                                                                  Sig-Logos32.png
Message 3 of 10
Anonymous
in reply to: Anonymous

OK. I got the answer.
I need to open model space as Block table record and need to call "AppendEntity" on it.
Message 4 of 10
Anonymous
in reply to: Anonymous

Thank you very much. I need one more help.
I have a code to update existing block attrbiutes.
Why it is not updating attribute values with new value? Please help.

BlockReference bRef = (BlockReference)trans1.GetObject(blockId, OpenMode.ForWrite);
AttributeCollection attCol = bRef.AttributeCollection;
foreach (ObjectId testAttId in attCol)
{
AttributeReference attRef = (AttributeReference)testAttId.GetObject(OpenMode.ForWrite, false, true);
attRef.TextString = "Test Value";
}
Message 5 of 10
chiefbraincloud
in reply to: Anonymous




Well, I'm assuming you have a trans1.commit below there somewhere, right? Otherwise it looks like it should work. Have you stepped through it to see that there are attributes in the collection?







I am attaching an example I clipped out of my code (and ran through a c# converter, since I use vb). It is a Sub which sets the Textstring of a single attribute when passed the ObjectID of the BlockReference, the Tag of the AttributeReference to set, and the string to set to the TextString property. You should be able to use it to see where you went wrong, or modify it to suit your purpose, otherwise you'll need to post the whole method in question so I (or anyone else here) can try and track down the problem.



Edited by: chiefbraincloud on Apr 29, 2009 1:36 PM



I just noticed the conversion to c# added a comment that says it changed my 'Exit For' to 'Break', so I think you need to fix that.

Dave O.                                                                  Sig-Logos32.png
Message 6 of 10
Anonymous
in reply to: Anonymous

I see my code below. Please help.

Transaction trans1 = currentDoc.TransactionManager.StartTransaction();

BlockTable bTable = (BlockTable)trans1.GetObject(currentDoc.Database.BlockTableId, OpenMode.ForRead);

BlockTableRecord tBlock = bTable["RoomId"].GetObject(OpenMode.ForRead);


ObjectIdCollection allRefBlocks = tBlock.GetBlockReferenceIds(true, true);
workingBlockName = tBlock.Name;
foreach (ObjectId blockId in allRefBlocks)
{
BlockReference bRef = (BlockReference)trans1.GetObject(blockId, OpenMode.ForWrite);
AttributeCollection attCol = bRef.AttributeCollection;
foreach (ObjectId testAttId in attCol)
{
//AttributeReference attRef = (AttributeReference)testAttId.GetObject(OpenMode.ForWrite, false, true);
AttributeReference attRef = (AttributeReference)trans1.GetObject(testAttId , OpenMode.ForWrite);
GetValueFromDatabase(ref attRef); // This method assigns attRef.TextString from database
}
}
trans1.Commit();
Message 7 of 10
chiefbraincloud
in reply to: Anonymous

Transaction trans1 = currentDoc.TransactionManager.StartTransaction();

I think you should be using the transaction manager that comes from the Database object. That may be the problem.

GetValueFromDatabase(ref attRef); // This method assigns attRef.TextString from database

Correctly passing an Object that you already have open for write can be tricky. I avoid it like the plague. I always pass ObjectID and do the transaction and Open ForWrite in the subroutine. Perhaps you could change GetValueFromDatabase to a function that returns the proper textstring value, then set the textstring value here in this method, since you already have the reference open, or alternatively I would not open the reference in this method, and instead pass the objectID to GetValueFromDatabase and open it there.

Try changing the TransactionManager reference to the one from Database... If that doesn't work then the problem is probably in the GetVauleFromDatabase sub.
Dave O.                                                                  Sig-Logos32.png
Message 8 of 10
Anonymous
in reply to: Anonymous

Same code works when used with Document Created Event.
But not when used with a command.
Message 9 of 10
chiefbraincloud
in reply to: Anonymous

That sounds like it may be a document lock issue, which is sort of wierd in itself since it works on the created event, but try adding a document lock to the beginning and make sure you dispose it afterwards.
Dave O.                                                                  Sig-Logos32.png
Message 10 of 10
Anonymous
in reply to: Anonymous

I did add some message boxes and found that actually code is changing attribute value but not getting displayed on screen when run program thorugh command.

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