.NET
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Re: Block-Func tions (Select, Explode, etc.)
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Sorry, try converted on C# instead
public static void ApplyAttributes(Database db, Transaction tr, BlockReference bref)
{
BlockTableRecord btrec = tr.GetObject(bref.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
if (btrec.HasAttributeDefinitions)
{
Autodesk.AutoCAD.DatabaseServices.AttributeCollect ion atcoll = bref.AttributeCollection;
foreach (ObjectId subid in btrec)
{
Entity ent = (Entity)subid.GetObject(OpenMode.ForRead);
AttributeDefinition attDef = ent as AttributeDefinition;
if (attDef != null)
{
AttributeReference attRef = new AttributeReference();
attRef.SetDatabaseDefaults();
attRef.SetAttributeFromBlock(attDef, bref.BlockTransform);
attRef.Position = attDef.Position.TransformBy(bref.BlockTransform);
attRef.Tag = attDef.Tag;
attRef.TextString = attDef.TextString;
attRef.AdjustAlignment(db);
atcoll.AppendAttribute(attRef);
tr.AddNewlyCreatedDBObject(attRef, true);
}
}
}
}
[CommandMethod("insBlock", CommandFlags.Modal | CommandFlags.UsePickSet)]
public void TestInsert()
{
string blkname = "VT_VZ_VZ52_10a_ATT";
//'<-- as per as on your drawing
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.D ocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
try
{
using (DocumentLock docloc = doc.LockDocument())
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
if (!bt.Has(blkname))
{
ed.WriteMessage("\nBlock does not exists");
return;
}
PromptPointOptions pto = new PromptPointOptions("\nPick a block insertion point: ");
PromptPointResult ptres = ed.GetPoint(pto);
Point3d ipt = default(Point3d);
if (ptres.Status != PromptStatus.Cancel)
{
ipt = ptres.Value;
}
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, false);
BlockTableRecord blk = (BlockTableRecord)tr.GetObject(bt[blkname], OpenMode.ForRead, false);
BlockReference bref = new BlockReference(ipt, blk.ObjectId);
bref.BlockUnit = UnitsValue.Millimeters;
bref.Rotation = 0;
bref.ScaleFactors = new Scale3d(1000.0);
//'<-- as per as on your drawing
btr.AppendEntity(bref);
tr.AddNewlyCreatedDBObject(bref, true);
ApplyAttributes(db, tr, bref);
ed.Regen();
tr.Commit();
}
}
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.Message);
}
}
~'J'~
C6309D9E0751D165D0934D0621DFF27919
Re: Block-Func tions (Select, Explode, etc.)
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
I forgot all about "\n".
Shame on me. My lisp programming skills are getting too rusty.

Re: Block-Func tions (Select, Explode, etc.)
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
But you know many other stuffs,
Regards,
Oleg
C6309D9E0751D165D0934D0621DFF27919
Re: Block-Func tions (Select, Explode, etc.)
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
What about Copy-Paste, just a quick code almost not tested
[CommandMethod("copypaste", CommandFlags.Modal | CommandFlags.UsePickSet)]
public void CopyPaste()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.D ocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Matrix3d ucs = ed.CurrentUserCoordinateSystem;
try
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
PromptEntityOptions peo = new PromptEntityOptions("\nSelect a block >>");
peo.SetRejectMessage("\nSelect block only >>");
peo.AddAllowedClass(typeof(BlockReference), false);
PromptEntityResult res;
res = ed.GetEntity(peo);
if (res.Status != PromptStatus.OK)
return;
Entity ent = (Entity)tr.GetObject(res.ObjectId, OpenMode.ForRead);
if (ent == null) return;
ObjectIdCollection ids = new ObjectIdCollection();
ids.Add(ent.ObjectId);
ObjectId chkId = ent.ObjectId;
BlockReference bref = ent as BlockReference;
Point3d pt = Point3d.Origin;
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
PromptPointOptions pto = new PromptPointOptions("\nPick a block insertion point: ");
PromptPointResult ptres = ed.GetPoint(pto);
Point3d ipt=Point3d.Origin;
if (ptres.Status != PromptStatus.Cancel)
{
ipt = ptres.Value;
}
using (IdMapping map = new IdMapping())
{
db.DeepCloneObjects(ids, bref.BlockId, map, false);
ObjectId clid = map[bref.ObjectId].Value;
BlockReference brefclone = (BlockReference)tr.GetObject(clid, OpenMode.ForWrite);
brefclone.Position =ipt;
}
tr.Commit();
}
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.Message);
}
}
~'J'~
C6309D9E0751D165D0934D0621DFF27919
Re: Block-Func tions (Select, Explode, etc.)
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
EDIT: Wow, wow, wow, I was reading the first page and didn't see the second page ![]()
That solved the second problem! ![]()
How should I use chr(13)?
PromptPointOptions pto = new PromptPointOptions(chr(13) + "Pick a block insertion point: ");
gives the same problem
Greets
Klaus
Re: Block-Func tions (Select, Explode, etc.)
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Well, I am surprised the chr function is not automatically in c#, but anyway, Oleg's code reminded me of the appropriate way to send a newline in c# (and lisp) use "\nPick a block insertion point: "

Re: Block-Func tions (Select, Explode, etc.)
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
That's great!
The block is pasted dreamlike. ![]()
BUT: When I refresh my blocklist, I havn't got a new entry for the pasted block. That means, in my list is only the old entry.
public void setBlockList()
{
BlockList = new List<String>();
using (tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
foreach (ObjectId objId in bt)
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(objId, OpenMode.ForRead);
if (isAttDef(btr))
{
BlockList.Add(btr.Name);
}
}
}
}That is my Blocklist-Function.
Do you know why it fails?
Greets
Klaus
Re: Block-Func tions (Select, Explode, etc.)
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
I didn't see nothing wrong with your code
Perhaps you might be want to refresh BlockList first
Something like:
public void setBlockList()
{
List<String> BlockList = new List<String>();
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.D ocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
foreach (ObjectId objId in bt)
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(objId, OpenMode.ForRead);
if (btr.HasAttributeDefinitions)
// if (isAttDef(btr))
{
BlockList.Add(btr.Name);
}
}
}
foreach (string bname in BlockList)
{
ed.WriteMessage("\n{0}", bname);
}
}
~'J'~
C6309D9E0751D165D0934D0621DFF27919
Re: Block-Func tions (Select, Explode, etc.)
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
I call my setBlockList()-Function direkt after the Commit. No Copy-Blockname.....
In the commandline (
ed.WriteMessage("\n{0}", bname);) is only one blockname too, the blockname of blk (using the function you posted).
Greets
Klaus
Re: Block-Func tions (Select, Explode, etc.)
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
I don't think it is failing.
I think you still don't quite understand the difference between a BlockReference and a BlockTableRecord.
You are listing BlockTableRecords in your Block List. When you copy/paste, you are creating a new BlockReference pointing to the same BlockTableRecord as the original copied BlockReference. This does not create a new BlockTableRecord.
There can only be one BlockTableRecord with a given name, but there can (and usually will be) many BlockReferences that point to same BlockTableRecord. That is really the point of Blocks, that you store the definition of the block (BlockTableRecord) once in the BlockTable, and then you create multiple instances of the block (BlockReference) as many times as needed in the Model or Paper spaces. This saves space in the database and keeps the drawing size smaller, because the BlockReferences just contain a pointer to the BlockTableRecord, instead of containing all of the information for the lines, polylines and whatever other geometry they might contain.
I'm still looking for the old post of mine that explains the difference in greater detail...



