Hey Everyone,
I'd like to just add 'Attibute Definition' on the fly by my external program.
'Attibute Definition' seems like a Text(MTEXT) Object in AutoCAD screen, doesn't it?
I implemented the source below. However, nothing is added.
DWF file I opended doesn't have any Blocks and other Objects.
What's wrong with this code?
Here's a source code. Make method with this routine and try.
----------------------------------------------------
public static void AddAttibuteDefinition(string attName, string prompt, string tag, string value)
{
// At first, Get current AutoCAD Document and Databese
Document curDoc = Application.DocumentManager.MdiActiveDocument;
Database curDB = curDoc.Database;
using (DocumentLock docLock = curDoc.LockDocument())
{
// Start transacition
using (Transaction trans = curDB.TransactionManager.StartTransaction())
{
// Open BlockTable
BlockTable blockTbl = (BlockTable)trans.GetObject(curDB.BlockTableId, OpenMode.ForWrite);
// Get BlockTableRecord
ObjectId modelSpaceID = blockTbl[BlockTableRecord.ModelSpace];
BlockTableRecord blockTblRec = (BlockTableRecord)trans.GetObject(modelSpaceID, OpenMode.ForWrite);
blockTblRec.UpgradeOpen();
// Get BlockRefference
BlockReference blockRef = new BlockReference(Point3d.Origin, blockTblRec.Id);
BlockTableRecord modelSpace = (BlockTableRecord)trans.GetObject(modelSpaceID, OpenMode.ForWrite);
modelSpace.AppendEntity(blockRef);
trans.AddNewlyCreatedDBObject(blockRef, true);
// Create AttriuteDefine
Point3d position = new Point3d(Point3d.Origin.X, Point3d.Origin.Y, Point3d.Origin.Z);
AttributeDefinition name_def = new AttributeDefinition(position, attName, tag, prompt, modelSpaceID);
// Create AttributeReference
AttributeReference name_ref = new AttributeReference();
name_ref.SetAttributeFromBlock(name_def, Matrix3d.Identity);
name_ref.TextString = "SampleName";
// Add AttributeReference into BlockReference
blockRef.AttributeCollection.AppendAttribute(name_ref);
trans.AddNewlyCreatedDBObject(name_ref, true);
// Commit transaction
trans.Commit();
}
}
}
----------------------------------------------------
I've seen various codes, however those codes are assumed that Block with attribute has been existed.
I'm developing with Visual Studio 2010 (C#) and Object ARX.
Any advice would be greatly appreciated.
Try this working code, sorry no explanation about theory
[CommandMethod("blkonfly")] public static void CreateBlockMark() { // Get the current document and database Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; // Start a transaction using (Transaction tr = db.TransactionManager.StartTransaction()) { try { BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite); string blkName = "BLDG_MARK"; if (bt.Has(blkName)) { ed.WriteMessage("\nBlock \"BLDG_MARK\" already exist."); return; } BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); Point3d inspt = new Point3d(0, 0, 0); BlockTableRecord newBtr = new BlockTableRecord(); bt.Add(newBtr); newBtr.Name = blkName; newBtr.Origin = inspt; newBtr.BlockScaling = BlockScaling.Uniform; newBtr.Units = UnitsValue.Inches; newBtr.Explodable = true; tr.AddNewlyCreatedDBObject(newBtr, true); Polyline pline = new Polyline(); Point2dCollection pts = new Point2dCollection() { new Point2d(2.454, 3.4631), new Point2d(1.991, 2.908), new Point2d(1.991, 0), new Point2d(-1.991, 0), new Point2d (-1.991, 2.908), new Point2d (-1.991, 2.908), new Point2d (-2.454, 3.4631), new Point2d (0, 5.0096) }; int i = 0; foreach (Point2d pt in pts) { pline.AddVertexAt(i, pt, 0, 0, 0); i += 1; } pline.Closed = true; pline.Layer = "0"; pline.LinetypeId = db.ContinuousLinetype; pline.ColorIndex = 0; pline.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, 0); newBtr.AppendEntity(pline); tr.AddNewlyCreatedDBObject(pline, true); AttributeDefinition attr = new AttributeDefinition(); attr.Layer = "0"; attr.LinetypeId = db.ContinuousLinetype; attr.ColorIndex = 0; attr.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, 0); attr.Tag = "BLDG_NUMBER"; attr.Prompt = "Building number:"; attr.TextString = "900"; attr.Preset=false; //attr.TextStyle = db.Textstyle;//<-- A2009 attr.TextStyleId = db.Textstyle;//<-- A2010 attr.Height = 1.0; attr.Position = new Point3d(0, 1.0574, 0); attr.Justify = AttachmentPoint.MiddleCenter; attr.AlignmentPoint = new Point3d(0, 1.0574, 0); attr.LockPositionInBlock = true; attr.AdjustAlignment(db); newBtr.AppendEntity(attr); tr.AddNewlyCreatedDBObject(attr, true); AttributeDefinition attr2 = new AttributeDefinition(); attr2.Layer = "0"; attr2.LinetypeId = db.ContinuousLinetype; attr2.ColorIndex = 0; attr2.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, 0); attr2.Tag = "BLDG_CODE"; attr2.Prompt = "Building code:"; attr2.TextString = "DM1000"; attr2.Preset = false; //attr.TextStyle = db.Textstyle;//<-- A2009 attr2.TextStyleId = db.Textstyle;//<-- A2010 attr2.Height = 1.0; attr2.Position = new Point3d (0, 6.0678, 0); attr2.Justify = AttachmentPoint.MiddleCenter; attr2.AlignmentPoint = new Point3d(0, 6.0678, 0); attr2.LockPositionInBlock = true; attr2.AdjustAlignment(db); newBtr.AppendEntity(attr2); tr.AddNewlyCreatedDBObject(attr2, true); AttributeDefinition attr3 = new AttributeDefinition(); attr3.Layer = "0"; attr3.LinetypeId = db.ContinuousLinetype; attr3.ColorIndex = 0; attr3.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, 0); attr3.Tag = "BLDG_MARK"; attr3.Prompt = "Building mark:"; attr3.TextString = "M"; //attr.Verifiable = true; attr.Preset = false; //attr.TextStyle = db.Textstyle;//<-- A2009 attr3.TextStyleId = db.Textstyle;//<-- A2010 attr3.Height = 1.0; attr3.Position = new Point3d (2.7165, 4.5163, 0); attr3.Justify = AttachmentPoint.MiddleCenter; attr3.AlignmentPoint = new Point3d (2.7165, 4.5163, 0); attr3.LockPositionInBlock = true; attr3.AdjustAlignment(db); newBtr.AppendEntity(attr3); tr.AddNewlyCreatedDBObject(attr3, true); tr.Commit(); } catch (System.Exception ex) { ed.WriteMessage(ex.Message + "\n" + ex.StackTrace); } } }
~'J'~
Hi, Hallex.
Thank you for showing for your useful reference code.
It works! That's exactly what I want.
I'll interpret the code carefully.
I really appreciate it.
Glad to help,
Show us your attempt if you've finish it
Cheers 🙂
~'J'~