<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Tag Parameter Updating the Dynamically in all CAD Sessions in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/tag-parameter-updating-the-dynamically-in-all-cad-sessions/m-p/12816710#M3752</link>
    <description>&lt;P&gt;If you post code here and you want someone to look at it, you need to use the insert code button on the toolbar so that the code is formatted and readable.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 04 Jun 2024 04:34:47 GMT</pubDate>
    <dc:creator>ActivistInvestor</dc:creator>
    <dc:date>2024-06-04T04:34:47Z</dc:date>
    <item>
      <title>Tag Parameter Updating the Dynamically in all CAD Sessions</title>
      <link>https://forums.autodesk.com/t5/net-forum/tag-parameter-updating-the-dynamically-in-all-cad-sessions/m-p/12811581#M3751</link>
      <description>&lt;P&gt;using System;&lt;BR /&gt;using System.Collections.Generic;&lt;BR /&gt;using System.Xml.Linq;&lt;BR /&gt;using Autodesk.AutoCAD.ApplicationServices;&lt;BR /&gt;using Autodesk.AutoCAD.DatabaseServices;&lt;BR /&gt;using Autodesk.AutoCAD.EditorInput;&lt;BR /&gt;using Autodesk.AutoCAD.Geometry;&lt;BR /&gt;using Autodesk.AutoCAD.Runtime;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;[assembly: CommandClass(typeof(DuctBlockTagger.Plugin))]&lt;/P&gt;&lt;P&gt;namespace DuctBlockTagger&lt;BR /&gt;{&lt;BR /&gt;public class Plugin&lt;BR /&gt;{&lt;BR /&gt;private Dictionary&amp;lt;ObjectId, ObjectId&amp;gt; blockToTextMap = new Dictionary&amp;lt;ObjectId, ObjectId&amp;gt;();&lt;BR /&gt;private ObjectId textStyleId;&lt;BR /&gt;private string layer;&lt;BR /&gt;private double textHeight;&lt;/P&gt;&lt;P&gt;[CommandMethod("DT")]&lt;BR /&gt;public void DuctTagger()&lt;BR /&gt;{&lt;BR /&gt;Document doc = Application.DocumentManager.MdiActiveDocument;&lt;BR /&gt;Database db = doc.Database;&lt;BR /&gt;Editor ed = doc.Editor;&lt;/P&gt;&lt;P&gt;// Check if the sample MText properties are set&lt;BR /&gt;if (textStyleId == ObjectId.Null || string.IsNullOrEmpty(layer) || textHeight &amp;lt;= 0)&lt;BR /&gt;{&lt;BR /&gt;ed.WriteMessage("\nSample MText parameters not set. Use the ST command to set them.");&lt;BR /&gt;return;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;using (Transaction tr = db.TransactionManager.StartTransaction())&lt;BR /&gt;{&lt;BR /&gt;// Prompt user to select multiple duct block references&lt;BR /&gt;PromptSelectionOptions pso = new PromptSelectionOptions();&lt;BR /&gt;pso.MessageForAdding = "\nSelect duct block references: ";&lt;BR /&gt;pso.RejectObjectsOnLockedLayers = true;&lt;BR /&gt;SelectionFilter filter = new SelectionFilter(new TypedValue[] { new TypedValue((int)DxfCode.Start, "INSERT") });&lt;BR /&gt;PromptSelectionResult psr = ed.GetSelection(pso, filter);&lt;/P&gt;&lt;P&gt;if (psr.Status != PromptStatus.OK)&lt;BR /&gt;{&lt;BR /&gt;ed.WriteMessage("\nNo valid duct block references selected.");&lt;BR /&gt;return;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Process each selected block reference&lt;BR /&gt;foreach (SelectedObject selObj in psr.Value)&lt;BR /&gt;{&lt;BR /&gt;if (selObj != null)&lt;BR /&gt;{&lt;BR /&gt;BlockReference blkRef = tr.GetObject(selObj.ObjectId, OpenMode.ForRead) as BlockReference;&lt;BR /&gt;if (blkRef != null)&lt;BR /&gt;{&lt;BR /&gt;// Get duct parameters&lt;BR /&gt;double cfm = GetPropertyValue(blkRef, "CFM");&lt;BR /&gt;double width = GetPropertyValue(blkRef, "Width");&lt;BR /&gt;double height = GetPropertyValue(blkRef, "Height");&lt;/P&gt;&lt;P&gt;// Check if parameters are valid&lt;BR /&gt;if (cfm &amp;lt;= 0 || width &amp;lt;= 0 || height &amp;lt;= 0)&lt;BR /&gt;{&lt;BR /&gt;ed.WriteMessage($"\nError: Duct parameters for block {blkRef.Handle} cannot be zero or negative.");&lt;BR /&gt;continue;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Construct tag value&lt;BR /&gt;string tagValue = $"{cfm} CFM,\n{width} x {height} mm";&lt;/P&gt;&lt;P&gt;// Calculate the center of the block reference&lt;BR /&gt;Point3d tagPosition = GetBlockReferenceCenter(blkRef);&lt;/P&gt;&lt;P&gt;// Create text annotation using MText&lt;BR /&gt;ObjectId mTextId = CreateTextAnnotation(tagValue, tagPosition, tr, db.BlockTableId, textStyleId, layer, textHeight);&lt;/P&gt;&lt;P&gt;// Map block reference to MText for future updates&lt;BR /&gt;blockToTextMap[blkRef.ObjectId] = mTextId;&lt;/P&gt;&lt;P&gt;// Subscribe to BlockReference modification event&lt;BR /&gt;blkRef.Modified += new EventHandler(OnBlockReferenceModified);&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;tr.Commit();&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;[CommandMethod("ST")]&lt;BR /&gt;public void SetSampleText()&lt;BR /&gt;{&lt;BR /&gt;Document doc = Application.DocumentManager.MdiActiveDocument;&lt;BR /&gt;Database db = doc.Database;&lt;BR /&gt;Editor ed = doc.Editor;&lt;/P&gt;&lt;P&gt;// Ask user to select a sample MText for parameters&lt;BR /&gt;PromptEntityOptions mtextPeo = new PromptEntityOptions("\nSelect a sample MText for parameters: ");&lt;BR /&gt;mtextPeo.SetRejectMessage("\nInvalid selection. Please select an MText entity.");&lt;BR /&gt;mtextPeo.AddAllowedClass(typeof(MText), true);&lt;BR /&gt;PromptEntityResult mtextPer = ed.GetEntity(mtextPeo);&lt;/P&gt;&lt;P&gt;if (mtextPer.Status != PromptStatus.OK)&lt;BR /&gt;{&lt;BR /&gt;ed.WriteMessage("\nNo valid MText selected.");&lt;BR /&gt;return;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;using (Transaction tr = db.TransactionManager.StartTransaction())&lt;BR /&gt;{&lt;BR /&gt;MText sampleMText = tr.GetObject(mtextPer.ObjectId, OpenMode.ForRead) as MText;&lt;/P&gt;&lt;P&gt;if (sampleMText == null)&lt;BR /&gt;{&lt;BR /&gt;ed.WriteMessage("\nInvalid MText entity selected.");&lt;BR /&gt;return;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Store the MText properties for future use&lt;BR /&gt;textStyleId = sampleMText.TextStyleId;&lt;BR /&gt;layer = sampleMText.Layer;&lt;BR /&gt;textHeight = sampleMText.TextHeight;&lt;/P&gt;&lt;P&gt;ed.WriteMessage("\nSample MText parameters set successfully.");&lt;BR /&gt;tr.Commit();&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private double GetPropertyValue(BlockReference blkRef, string propertyName)&lt;BR /&gt;{&lt;BR /&gt;foreach (DynamicBlockReferenceProperty prop in blkRef.DynamicBlockReferencePropertyCollection)&lt;BR /&gt;{&lt;BR /&gt;if (prop.PropertyName.Equals(propertyName, StringComparison.InvariantCultureIgnoreCase))&lt;BR /&gt;{&lt;BR /&gt;return Convert.ToDouble(prop.Value);&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;return 0;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private Point3d GetBlockReferenceCenter(BlockReference blkRef)&lt;BR /&gt;{&lt;BR /&gt;Extents3d extents = blkRef.GeometricExtents;&lt;BR /&gt;Point3d center = new Point3d(&lt;BR /&gt;(extents.MinPoint.X + extents.MaxPoint.X) / 2,&lt;BR /&gt;(extents.MinPoint.Y + extents.MaxPoint.Y) / 2,&lt;BR /&gt;(extents.MinPoint.Z + extents.MaxPoint.Z) / 2&lt;BR /&gt;);&lt;BR /&gt;return center;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private ObjectId CreateTextAnnotation(string tagValue, Point3d tagPosition, Transaction tr, ObjectId blockTableId, ObjectId textStyleId, string layer, double textHeight)&lt;BR /&gt;{&lt;BR /&gt;BlockTable bt = tr.GetObject(blockTableId, OpenMode.ForRead) as BlockTable;&lt;BR /&gt;BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;&lt;/P&gt;&lt;P&gt;// Create a new MText entity for the tag&lt;BR /&gt;using (MText mText = new MText())&lt;BR /&gt;{&lt;BR /&gt;mText.Location = tagPosition;&lt;BR /&gt;mText.TextHeight = textHeight;&lt;BR /&gt;mText.Contents = tagValue;&lt;BR /&gt;mText.TextStyleId = textStyleId;&lt;BR /&gt;mText.Layer = layer;&lt;BR /&gt;mText.SetDatabaseDefaults();&lt;/P&gt;&lt;P&gt;// Add the MText entity to the model space&lt;BR /&gt;btr.AppendEntity(mText);&lt;BR /&gt;tr.AddNewlyCreatedDBObject(mText, true);&lt;/P&gt;&lt;P&gt;return mText.ObjectId;&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void OnBlockReferenceModified(object sender, EventArgs e)&lt;BR /&gt;{&lt;BR /&gt;BlockReference blkRef = sender as BlockReference;&lt;BR /&gt;if (blkRef == null)&lt;BR /&gt;return;&lt;/P&gt;&lt;P&gt;Document doc = Application.DocumentManager.MdiActiveDocument;&lt;BR /&gt;Database db = doc.Database;&lt;/P&gt;&lt;P&gt;using (Transaction tr = db.TransactionManager.StartTransaction())&lt;BR /&gt;{&lt;BR /&gt;try&lt;BR /&gt;{&lt;BR /&gt;UpdateTextAnnotation(blkRef, tr);&lt;BR /&gt;tr.Commit();&lt;BR /&gt;}&lt;BR /&gt;catch (System.Exception ex)&lt;BR /&gt;{&lt;BR /&gt;Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog($"Error updating annotation: {ex.Message}");&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void UpdateTextAnnotation(BlockReference blkRef, Transaction tr)&lt;BR /&gt;{&lt;BR /&gt;if (!blockToTextMap.ContainsKey(blkRef.ObjectId))&lt;BR /&gt;return;&lt;/P&gt;&lt;P&gt;ObjectId mTextId = blockToTextMap[blkRef.ObjectId];&lt;BR /&gt;MText mText = tr.GetObject(mTextId, OpenMode.ForWrite) as MText;&lt;/P&gt;&lt;P&gt;if (mText != null)&lt;BR /&gt;{&lt;BR /&gt;double cfm = GetPropertyValue(blkRef, "CFM");&lt;BR /&gt;double width = GetPropertyValue(blkRef, "Width");&lt;BR /&gt;double height = GetPropertyValue(blkRef, "Height");&lt;/P&gt;&lt;P&gt;if (cfm &amp;gt; 0 &amp;amp;&amp;amp; width &amp;gt; 0 &amp;amp;&amp;amp; height &amp;gt; 0)&lt;BR /&gt;{&lt;BR /&gt;mText.Contents = $"{cfm} CFM,\n{width} x {height} mm";&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in this above code i can extract parameter value and i can modify the Mtext as a annotation dynamically but this plug in code only working in one time i need to work on Multiple Autocad Sessions. Kindly help anyone where i need to change and what line&amp;nbsp; i need to modify&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jun 2024 07:45:35 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/tag-parameter-updating-the-dynamically-in-all-cad-sessions/m-p/12811581#M3751</guid>
      <dc:creator>vivekanandanXVLYD</dc:creator>
      <dc:date>2024-06-01T07:45:35Z</dc:date>
    </item>
    <item>
      <title>Re: Tag Parameter Updating the Dynamically in all CAD Sessions</title>
      <link>https://forums.autodesk.com/t5/net-forum/tag-parameter-updating-the-dynamically-in-all-cad-sessions/m-p/12816710#M3752</link>
      <description>&lt;P&gt;If you post code here and you want someone to look at it, you need to use the insert code button on the toolbar so that the code is formatted and readable.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jun 2024 04:34:47 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/tag-parameter-updating-the-dynamically-in-all-cad-sessions/m-p/12816710#M3752</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2024-06-04T04:34:47Z</dc:date>
    </item>
  </channel>
</rss>

