TheSwamp

Code Red => .NET => Topic started by: Sieu khung khiep on November 23, 2019, 04:46:50 AM

Title: Need help about JIG attribute block!
Post by: Sieu khung khiep on November 23, 2019, 04:46:50 AM
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#: [Select]
  1. namespace BlockJigApplication
  2. {
  3.  
  4.     class BlockJig : EntityJig
  5.     {
  6.         private Point3d _pos;
  7.         private Dictionary<ObjectId, AttInfo> _attInfo;
  8.         private Transaction _tr;
  9.  
  10.         public BlockJig(Transaction tr, BlockReference br, Dictionary<ObjectId, AttInfo> attInfo) : base(br)
  11.         {
  12.             _pos = br.Position;
  13.             _attInfo = attInfo;
  14.             _tr = tr;
  15.         }
  16.  
  17.         protected override bool Update()
  18.         {
  19.             BlockReference br = Entity as BlockReference;
  20.             br.Position = _pos;
  21.  
  22.             if (br.AttributeCollection.Count != 0)
  23.             {
  24.                 foreach (ObjectId id in br.AttributeCollection)
  25.                 {
  26.                     DBObject obj = _tr.GetObject(id, OpenMode.ForRead);
  27.                     AttributeReference ar = obj as AttributeReference;
  28.  
  29.                     if (ar != null)
  30.                     {
  31.                         ar.UpgradeOpen();
  32.                         AttInfo ai = _attInfo[ar.ObjectId];
  33.                         ar.Position = ai.Position.TransformBy(br.BlockTransform);
  34.  
  35.                         if (ai.IsAligned)
  36.                             ar.AlignmentPoint = ai.Alignment.TransformBy(br.BlockTransform);
  37.  
  38.                         if (ar.IsMTextAttribute)
  39.                             ar.UpdateMTextAttribute();
  40.                     }
  41.                 }
  42.             }
  43.  
  44.             return true;
  45.         }
  46.  
  47.         protected override SamplerStatus Sampler(JigPrompts prompts)
  48.         {
  49.             JigPromptPointOptions opts = new JigPromptPointOptions(Constants.vbLf + "Select insertion point:");
  50.             opts.BasePoint = new Point3d(0, 0, 0);
  51.             opts.UserInputControls = UserInputControls.NoZeroResponseAccepted;
  52.             PromptPointResult ppr = prompts.AcquirePoint(opts);
  53.  
  54.             if (_pos == ppr.Value)
  55.                 return SamplerStatus.NoChange;
  56.  
  57.             _pos = ppr.Value;
  58.             return SamplerStatus.OK;
  59.         }
  60.  
  61.         public PromptStatus Run()
  62.         {
  63.             Document doc = Application.DocumentManager.MdiActiveDocument;
  64.             Editor ed = doc.Editor;
  65.             PromptResult promptResult = ed.Drag(this);
  66.             return promptResult.Status;
  67.         }
  68.     }
  69.  
  70.     public class Commands
  71.     {
  72.         [CommandMethod("BJ")]
  73.         static public void BlockJigCmd()
  74.         {
  75.             Document doc = Application.DocumentManager.MdiActiveDocument;
  76.             Database db = doc.Database;
  77.             Editor ed = doc.Editor;
  78.             PromptStringOptions pso = new PromptStringOptions(Constants.vbLf + "Enter block name: ");
  79.             PromptResult pr = ed.GetString(pso);
  80.             if (pr.Status != PromptStatus.OK)
  81.                 return;
  82.             Transaction tr = doc.TransactionManager.StartTransaction();
  83.  
  84.             using (tr)
  85.             {
  86.                 BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
  87.  
  88.                 if (!bt.Has(pr.StringResult))
  89.                 {
  90.                     ed.WriteMessage(Constants.vbLf + "Block \"" + pr.StringResult + "\" not found.");
  91.                     return;
  92.                 }
  93.  
  94.                 BlockTableRecord space = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  95.                 BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt(pr.StringResult), OpenMode.ForRead);
  96.                 BlockReference br = new BlockReference(new Point3d(), btr.ObjectId);
  97.                 space.AppendEntity(br);
  98.                 tr.AddNewlyCreatedDBObject(br, true);
  99.                 Dictionary<ObjectId, AttInfo> attInfo = new Dictionary<ObjectId, AttInfo>();
  100.  
  101.                 if (btr.HasAttributeDefinitions)
  102.                 {
  103.                     foreach (ObjectId id in btr)
  104.                     {
  105.                         DBObject obj = tr.GetObject(id, OpenMode.ForRead);
  106.                         AttributeDefinition ad = obj as AttributeDefinition;
  107.  
  108.                         if (ad != null && !ad.Constant)
  109.                         {
  110.                             AttributeReference ar = new AttributeReference();
  111.                             ar.SetAttributeFromBlock(ad, br.BlockTransform);
  112.  
  113. //<<=====================================>>
  114.                             [color=red]ar.Position = ad.Position.TransformBy(br.BlockTransform);    
  115.                             if (ad.Justify != AttachmentPoint.BaseLeft)
  116.                                 ar.AlignmentPoint = ad.AlignmentPoint.TransformBy(br.BlockTransform);
  117.  
  118.                             if (ar.IsMTextAttribute)
  119.                                 ar.UpdateMTextAttribute();
  120.                             ar.TextString = ad.TextString;[/color]  
  121.  //<<====================================>>          
  122.                  
  123.                             ObjectId arId = br.AttributeCollection.AppendAttribute(ar);
  124.                             tr.AddNewlyCreatedDBObject(ar, true);
  125.                             attInfo.Add(arId, new AttInfo(ad.Position, ad.AlignmentPoint, ad.Justify != AttachmentPoint.BaseLeft));
  126.                         }
  127.                     }
  128.                 }
  129.  
  130.                 BlockJig myJig = new BlockJig(tr, br, attInfo);
  131.                 if (myJig.Run() != PromptStatus.OK)
  132.                     return;
  133.                 tr.Commit();
  134.             }
  135.         }
  136.     }
  137. }
  138.  

Thanks in advance. 
Title: Re: Need help about JIG attribute block!
Post by: Jeff H on December 03, 2019, 01:15:45 AM
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
Title: Re: Need help about JIG attribute block!
Post by: Sieu khung khiep on February 12, 2020, 05:59:54 PM
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: