Code Red > .NET

Need help about JIG attribute block!

(1/1)

Sieu khung khiep:
Hi,I have finished reading this post: https://www.keanw.com/2009/03/jigging-an-autocad-block-with-attributes-using-net.html
and I am wondering about this code [Color red] in [CommandMethod ("BJ")] at the end of the article, It looks like the SetAttributeFromBlock (attdef, br.BlockTransform) is enough because when I removed these code then project still worked.
I understand that right? Hope you guys can explain more clearly about SetAttributeFromBlock method? I have read form objectarx sdk guide but still feel mystical, this is the code that I've shortened:


--- Code - C#: ---namespace BlockJigApplication{     class BlockJig : EntityJig    {        private Point3d _pos;        private Dictionary<ObjectId, AttInfo> _attInfo;        private Transaction _tr;         public BlockJig(Transaction tr, BlockReference br, Dictionary<ObjectId, AttInfo> attInfo) : base(br)        {            _pos = br.Position;            _attInfo = attInfo;            _tr = tr;        }         protected override bool Update()        {            BlockReference br = Entity as BlockReference;            br.Position = _pos;             if (br.AttributeCollection.Count != 0)            {                foreach (ObjectId id in br.AttributeCollection)                {                    DBObject obj = _tr.GetObject(id, OpenMode.ForRead);                    AttributeReference ar = obj as AttributeReference;                     if (ar != null)                    {                        ar.UpgradeOpen();                        AttInfo ai = _attInfo[ar.ObjectId];                        ar.Position = ai.Position.TransformBy(br.BlockTransform);                         if (ai.IsAligned)                            ar.AlignmentPoint = ai.Alignment.TransformBy(br.BlockTransform);                         if (ar.IsMTextAttribute)                            ar.UpdateMTextAttribute();                    }                }            }             return true;        }         protected override SamplerStatus Sampler(JigPrompts prompts)        {            JigPromptPointOptions opts = new JigPromptPointOptions(Constants.vbLf + "Select insertion point:");            opts.BasePoint = new Point3d(0, 0, 0);            opts.UserInputControls = UserInputControls.NoZeroResponseAccepted;            PromptPointResult ppr = prompts.AcquirePoint(opts);             if (_pos == ppr.Value)                return SamplerStatus.NoChange;             _pos = ppr.Value;            return SamplerStatus.OK;        }         public PromptStatus Run()        {            Document doc = Application.DocumentManager.MdiActiveDocument;            Editor ed = doc.Editor;            PromptResult promptResult = ed.Drag(this);            return promptResult.Status;        }    }     public class Commands    {        [CommandMethod("BJ")]        static public void BlockJigCmd()        {            Document doc = Application.DocumentManager.MdiActiveDocument;            Database db = doc.Database;            Editor ed = doc.Editor;            PromptStringOptions pso = new PromptStringOptions(Constants.vbLf + "Enter block name: ");            PromptResult pr = ed.GetString(pso);            if (pr.Status != PromptStatus.OK)                return;            Transaction tr = doc.TransactionManager.StartTransaction();             using (tr)            {                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);                 if (!bt.Has(pr.StringResult))                {                    ed.WriteMessage(Constants.vbLf + "Block \"" + pr.StringResult + "\" not found.");                    return;                }                 BlockTableRecord space = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt(pr.StringResult), OpenMode.ForRead);                BlockReference br = new BlockReference(new Point3d(), btr.ObjectId);                space.AppendEntity(br);                tr.AddNewlyCreatedDBObject(br, true);                Dictionary<ObjectId, AttInfo> attInfo = new Dictionary<ObjectId, AttInfo>();                 if (btr.HasAttributeDefinitions)                {                    foreach (ObjectId id in btr)                    {                        DBObject obj = tr.GetObject(id, OpenMode.ForRead);                        AttributeDefinition ad = obj as AttributeDefinition;                         if (ad != null && !ad.Constant)                        {                            AttributeReference ar = new AttributeReference();                            ar.SetAttributeFromBlock(ad, br.BlockTransform); //<<=====================================>>                            [color=red]ar.Position = ad.Position.TransformBy(br.BlockTransform);                                if (ad.Justify != AttachmentPoint.BaseLeft)                                ar.AlignmentPoint = ad.AlignmentPoint.TransformBy(br.BlockTransform);                             if (ar.IsMTextAttribute)                                ar.UpdateMTextAttribute();                            ar.TextString = ad.TextString;[/color]    //<<====================================>>                                                        ObjectId arId = br.AttributeCollection.AppendAttribute(ar);                            tr.AddNewlyCreatedDBObject(ar, true);                            attInfo.Add(arId, new AttInfo(ad.Position, ad.AlignmentPoint, ad.Justify != AttachmentPoint.BaseLeft));                        }                    }                }                 BlockJig myJig = new BlockJig(tr, br, attInfo);                if (myJig.Run() != PromptStatus.OK)                    return;                tr.Commit();            }        }    }} 
Thanks in advance. 

Jeff H:
Yes he updated example in later post and removes redundant code
https://through-the-interface.typepad.com/through_the_interface/2015/05/jigging-an-autocad-block-with-attributes-using-net-redux.html

Sieu khung khiep:
Hi, Jeff H
Thanks very much. I was very busy, now I can go back to .net,  I will read the article in the link you sent.  :smitten:

Navigation

[0] Message Index

Go to full version