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

Block Table record Block reference

7 REPLIES 7
Reply
Message 1 of 8
wen30
6268 Views, 7 Replies

Block Table record Block reference

Hi , i am having some problem for the Lab 4, at the CreateEmployeeDefinition() part.
i can see that the CreateEmployeeDefinition() function create a block table record name "EmployeeBlock" and add an entity to it,
but what is a block reference? somehow, when the "EmployeeBlock" already exist, the CreateEmployeeDefinition() function just return the ObjectID of the EmployeeBlock and the program never enters the create entity part coding, yet another employee entity is created. This is so confusing me. can anybody help? Thanks

private ObjectId CreateEmployeeDefinition()
{
ObjectId newBtrId = new ObjectId(); //The return value for this function
Database db = HostApplicationServices.WorkingDatabase; //save some space
using (Transaction trans = db.TransactionManager.StartTransaction())//begin the transaction
{
//Now, drill into the database and obtain a reference to the BlockTable
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);
if ((bt.Has("EmployeeBlock")))
{
newBtrId = bt["EmployeeBlock"];
}
else
{
Point3d center = new Point3d(10, 10, 0); // convenient declaration...
// Declare and define the entities we want to add:
//Circle:
Circle circle = new Circle(center, Vector3d.ZAxis, 2);
//Text:
MText text = new MText();
text.Contents = "Earnest Shackleton";
text.Location = center;
//Ellipse:
Ellipse ellipse = new Ellipse(center, Vector3d.ZAxis, new Vector3d(3, 0, 0), 0.5, 0, 0);

//Next, create a layer with the helper function, and assign
//the layer to our entities.
ObjectId empId = CreateLayer();
text.LayerId = empId;
circle.LayerId = empId;
ellipse.LayerId = empId;
//Set the color for each entity irrespective of the layer's color.
text.ColorIndex = 2;
circle.ColorIndex = 1;
ellipse.ColorIndex = 3;

//Create a new block definition called EmployeeBlock
BlockTableRecord newBtr = new BlockTableRecord();
newBtr.Name = "EmployeeBlock";
newBtrId = bt.Add(newBtr); //Add the block, and set the id as the return value of our function
trans.AddNewlyCreatedDBObject(newBtr, true); //Let the transaction know about any object/entity you add to the database!

newBtr.AppendEntity(circle); //Append our entities...
newBtr.AppendEntity(text);
newBtr.AppendEntity(ellipse);
trans.AddNewlyCreatedDBObject(circle, true); //Again, let the transaction know about our newly added entities.
trans.AddNewlyCreatedDBObject(text, true);
trans.AddNewlyCreatedDBObject(ellipse, true);

trans.Commit(); //All done, no errors? Go ahead and commit!
}
}

CreateDivision(); //Create the Employee Division dictionaries.

return newBtrId;
}
7 REPLIES 7
Message 2 of 8
BillZndl
in reply to: wen30

Hi wen,

It appears that your code is doing exactly what it is supposed to.

You are opening the BlockTable in the drawing database,
then checking to see if it contains a BlockTableRecord named "EmployeeBlock".

If it does, it returns the ObjectId for the existing blockrecord,
else otherwise, it creates a new blocktablerecord with that name and returns the ObjectId for that.

It doesn't show in what you posted but I'm assuming that in some other part of your code
it takes that returned ObjectId and creates the BlockReference (which is the graphical representation of the BTR) in the drawing.

Sounds like you need to learn some more about AutoCAD as well as .Net to understand what is going on in that code.

HTH

Bill
Message 3 of 8
wen30
in reply to: wen30

Hi Bill, thanks for the reply. yes the code takes the objectId returned to create a block reference in the modelspace just like what you said but what i don't understand is when the code use the same block table record id, the emplyeeblock to create another reference. so does it mean that the modelspace contains the block reference and the block reference contains the EmployeeBlock and the EmployeeBlock contains the circle? why is a circle created every time i create another block reference? it seems that the code doesn't create another circle instead it return the EmployeeBlock BTR Id.
Message 4 of 8
BillZndl
in reply to: wen30

If the block table contains a block record,
you can simply insert the block reference into the drawing like so
(inside your transaction).
{code}

BlockTable table = (BlockTable)transaction.GetObject(database.BlockTableId, OpenMode.ForRead);
if (table.Has(NameOfBlock))
{
BlockTableRecord record = (BlockTableRecord)transaction.GetObject(table[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
BlockReference reference = new BlockReference(point.Value, table[NameOfBlock]);

record.AppendEntity(reference);
transaction.AddNewlyCreatedDBObject(reference, true);
transaction.Commit();
editor.UpdateScreen();
}
{code}

Note: table[NameOfBlock] returns the ObjectId for the BlockTableRecord,
in your case you could do something like this:
{code}
//create instance of class
ObjectId BtrId = CreateEmployeeDefinition();
BlockReference reference = new BlockReference(point.Value, BtrId);
{code}


There are plenty of examples on how to do this on this site.

Bill
Message 5 of 8
wen30
in reply to: wen30

Hi bill, thanks for the information and explanation. They helped a lot. Edited by: wen30 on Feb 25, 2010 5:37 PM
Message 6 of 8
BillZndl
in reply to: wen30

You're welcome.
I'm glad that it helped.
Message 7 of 8
BKSpurgeon
in reply to: wen30

Hi Wen, this is my understanding and it may help you - folks please correct me if i got this wrong:

 

Understanding by ANALOGY - birth certificates are like your block table record

Imagine you have a really important document like your GREEN card/birth certificate etc. you have to keep this document locked away in your solicitor's office. this green card is exactly like a block table record. you only have one of this document. but let's just say you want to have many copies of your green card. Are you going to recreate a green card (and have many originals) so you can give one to the IRS, another to the department of immigration, another to the department of homeland security etc etc? No! Of course not: you are not going to recreate it: you are going to make photocopies of it. It makes sense because photocopies are cheap and easy and don't cost (much) memory.

 

The photocopies of your green card are like lock references. you can have many photocopies of your green card, and you can paste them anywhere you want: on the street, in your home etc. you can even delete them. you can delete or destroy all of them, but you will still have a copy of your green card located safely somewhere in your house, or your lawyers office. If you destroy the original green card then guess what - you will no longer be able to make copies of it (i.e. you won't be able to make any block references if you don't have a block table record which it points to.)

 

summary:

 

You have (1) block definitions - also called block table records, and then you have: (2) block references. Block references and block definitions are similar but related things.

 

The block definition is in the block table. This does not appear anywhere in your drawing. if you want to put it in your drawing then what you do is: you create a block reference (which points to the block definition) and you then put the block reference into the model space.

 

 

hope that helps.

 

BK

 

 

Message 8 of 8
SENL1362
in reply to: BKSpurgeon

 

The block definition is in the block table. This does not appear anywhere in your drawing. if you want to put it in your drawing then what you do is: you create a block reference (which points to the block definition) and you then put the block reference into the model space.

 

Or in a PaperSpace or in any other BlockTable(Record) -- Nested Blocks.

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