My method creates a attribute, how to a create a multi-line attribute?
Please Help!!!
using (Transaction trAdding = dbCurrent.TransactionManager.StartTransaction()) { BlockTable btTable = (BlockTable)trAdding.GetObject(dbCurrent.BlockTableId, OpenMode.ForRead); string strBlockName = "ATTRBLK" + System.Convert.ToString(blockName); try { if (btTable.Has(strBlockName)) edCurrent.WriteMessage("\n A block with this name already exist."); } catch { edCurrent.WriteMessage("\n Invalid block name."); } AttributeDefinition adAttr = new AttributeDefinition(); adAttr.Position = new Point3d(x, y, 0); adAttr.WidthFactor = widthFactor; adAttr.Height = height; adAttr.Rotation = rotate; adAttr.Oblique = oblique; adAttr.Tag = attrName; BlockTableRecord btrRecord = new BlockTableRecord(); btrRecord.Name = strBlockName; btTable.UpgradeOpen(); ObjectId btrID = btTable.Add(btrRecord); trAdding.AddNewlyCreatedDBObject(btrRecord, true); btrRecord.AppendEntity(adAttr); trAdding.AddNewlyCreatedDBObject(adAttr, true); BlockTableRecord btrPapperSpace = (BlockTableRecord)trAdding.GetObject(btTable[BlockTableRecord.PaperSpace], OpenMode.ForWrite); BlockReference brRefBlock = new BlockReference(Point3d.Origin, btrID); btrPapperSpace.AppendEntity(brRefBlock); trAdding.AddNewlyCreatedDBObject(brRefBlock, true); //задаём значение атрибута AttributeReference arAttr = new AttributeReference(); arAttr.SetAttributeFromBlock(adAttr, brRefBlock.BlockTransform); arAttr.TextString = text; arAttr.Layer = "Z-TEXT"; brRefBlock.AttributeCollection.AppendAttribute(arAttr); trAdding.AddNewlyCreatedDBObject(arAttr, true); trAdding.Commit(); }
Попробуй такой код (на скорую руку - переделай под свои установкиб тестировал на А2009-2014, работает)
Try this code written from the scratch ( quick and dirty one, tested on A209 through A2014, it's working good for me)
[CommandMethod("Bma")] public void testMultAttribute() { var docCurrent = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; var dbCurrent = docCurrent.Database; var edCurrent = docCurrent.Editor; BlockTableRecord btrRecord = null; string blockName = "";double x=0;double y=0;double z=0; double widthFactor = 1.0; double height = 250; double rotate = 0.0; double oblique = 0.0; string attrName = "ATTTAG";string text="first text line\\Psecond text line\\Pthird text line"; ObjectId btrID = ObjectId.Null; try { docCurrent.TransactionManager.EnableGraphicsFlush(true); using (Transaction trAdding = dbCurrent.TransactionManager.StartTransaction()) { BlockTable btTable = (BlockTable)trAdding.GetObject(dbCurrent.BlockTableId, OpenMode.ForRead); string strBlockName = "ATTRBLK" + System.Convert.ToString(blockName); try { if (btTable.Has(strBlockName)) edCurrent.WriteMessage("\n A block with this name already exist."); else { AttributeDefinition adAttr = new AttributeDefinition(); adAttr.Position = new Point3d(x, y, z); adAttr.WidthFactor = widthFactor; adAttr.Height = height; adAttr.Rotation = rotate; adAttr.Oblique = oblique; adAttr.Tag = attrName; btTable.UpgradeOpen(); btrRecord = new BlockTableRecord(); btrRecord.Name = strBlockName; btTable.Add(btrRecord); trAdding.AddNewlyCreatedDBObject(btrRecord, true); btTable.DowngradeOpen(); adAttr.IsMTextAttributeDefinition = true; adAttr.MTextAttributeDefinition.Contents = text; btrRecord.AppendEntity(adAttr); trAdding.AddNewlyCreatedDBObject(adAttr, true); } } catch { edCurrent.WriteMessage("\n Invalid block name."); } BlockTableRecord btrPapperSpace = (BlockTableRecord)trAdding.GetObject(btTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite); BlockReference brRefBlock = new BlockReference(new Point3d(x, y, z), btrRecord.ObjectId); btrPapperSpace.AppendEntity(brRefBlock); trAdding.AddNewlyCreatedDBObject(brRefBlock, true); //задаём значение атрибута foreach (ObjectId id in btrRecord) { DBObject obj = id.GetObject(OpenMode.ForRead); AttributeDefinition adAttr = obj as AttributeDefinition; if ((adAttr != null) && (!adAttr.Constant)) { AttributeReference arAttr = new AttributeReference(); arAttr.SetAttributeFromBlock(adAttr, brRefBlock.BlockTransform); if (arAttr.IsMTextAttribute) { arAttr.Layer = "0"; arAttr.TextString = text; arAttr.UpdateMTextAttribute(); brRefBlock.RecordGraphicsModified(true); } else { // add usual attribute reference here } brRefBlock.AttributeCollection.AppendAttribute(arAttr); trAdding.AddNewlyCreatedDBObject(arAttr, true); } trAdding.TransactionManager.QueueForGraphicsFlush(); } docCurrent.TransactionManager.FlushGraphics(); trAdding.Commit(); } } catch (System.Exception ex) { edCurrent.WriteMessage("\n{0}\n{1}", ex.Message, ex.StackTrace); } finally { Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\n\t---\tSee results\t---\n"); } }
Hallex,
I was going to post a new thread but this thread caught my eye.
Do you know how to access and change the Boundary Width?
I tried adAttr.MTextAttributeDefinition.Width = 100 (along with a few other "Read Only" properties) with no luck.
You have to calculate first scale factor you need
accordingly to a text height, then just use this line:
arAttr.WidthFactor = 2.5;// put scale factor here
Or am I missed something?
Well probably this will be usefull instead:
arAttr.MTextAttribute.Width = arAttr.MTextAttribute.ActualWidth;
Sorry for the belating,
probably I've sorted this out:
if (arAttr.IsMTextAttribute) { arAttr.Layer = "0"; arAttr.TextString = text; MText mtx = arAttr.MTextAttribute; mtx.Width = 2400; arAttr.MTextAttribute = mtx; // OR // arAttr.MTextAttribute.Width = adAttr.MTextAttributeDefinition.Width; arAttr.UpdateMTextAttribute(); brRefBlock.RecordGraphicsModified(true); }
Change a width to your suit
Cheers