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

placing dynamic blocks relative to one another with .NET

4 REPLIES 4
Reply
Message 1 of 5
mspan
336 Views, 4 Replies

placing dynamic blocks relative to one another with .NET

Can you point me to a simple example (on this forum or elsewhere) using c# to insert one dynamic block on a drawing, then inserting a different type of dynamic block on the same drawing at a particular point on the first block. An example would be:
1) insert a dynamic block that is a square. Set side length to 3000mm.
2) at each corner of the square, insert a dynamic block that is a different sized circle. the center of each circle is located at each corner of the square. circle radius 1 = 500mm, 2 is 550 mm, 3 is 600mm, 4 is 650mm.
The dynamic blocks that I am using are quite complex however so any help in describing how to work with insertion points within .NET is greatly appreciated.
4 REPLIES 4
Message 2 of 5
cadMeUp
in reply to: mspan

If you can calculate the Point3d coordinate of each corner point, and I am assuming that the circle block has it's def/insert point at the center of the circle, when you add a BlockReference of the 'circle block' to the dwg just set the position of the block to one of the corner points:

Point3d rectCornerPnt = somePoint;
BlockReference circleBlk = new BlockReference(rectCornerPnt, blkRecId);

or if you want to set/change the position:

circleBlk.Position = rectCornerPnt;
Message 3 of 5
mspan
in reply to: mspan

Thanks. Rather than calculating the corner positions, can I somehow add an invisible attribute or parameter that gets moved along with the other dynamic elements in the block? What is the method for finding the position of an attribute within a dynamic block that is inserted into a drawing?
Thanks again.
Kindest Regards,
mspan
Message 4 of 5
cadMeUp
in reply to: mspan

AttributeReference ar = someAttRef;
Point3d attPos = someAttRef.Position;

I have not had a chance to work with dynamic blocks programmatically yet so I am not sure about what is involved with them along those lines.

The attribute ref position is relative to the position of the block that contains it. When you place a block reference that has an attribute definition programmatically, in order for the attribute def to be placed correctly you have to figure for the block's position (transformation matrix). The attribute would be transformed using the block ref 'BlockTransform' property. This is a sample of what I have used before:

using (Transaction trans = transMgr.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

string blockName = "SOME BLOCK NAME";

if (!bt.Has(blockName))
{
editor.WriteMessage("\nFunction Cancelled! --> Block '" + blockName + "' not found.");
trans.Dispose();
return;
}
DatabaseServ.ObjectId bdId = bt[blockName];


DatabaseServ.BlockReference blockRef = new DatabaseServ.BlockReference(new Point3d(5, 5, 0), bdId);


BlockTableRecord bdr = (BlockTableRecord)trans.GetObject(bdId, OpenMode.ForRead);
foreach (DatabaseServ.ObjectId id in bdr)
{
DatabaseServ.Entity ent = (DatabaseServ.Entity)trans.GetObject(id, OpenMode.ForRead);
if (ent is AttributeDefinition)
{
AttributeDefinition ad = (AttributeDefinition)ent;
AttributeReference ar = new AttributeReference();

// This is the key here:
ar.SetAttributeFromBlock(ad, blockRef.BlockTransform);

blockRef.AttributeCollection.AppendAttribute(ar);
trans.AddNewlyCreatedDBObject(ar, true);
}
}

trans.commit();

}
Message 5 of 5
mspan
in reply to: mspan

Thank you for a thoughtful reply. I will give it a whirl.
--mspan

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