Page 1 of 1

(DynamicBlockReferenceProperty prop in br.DynamicProperties. Error Showing. How can I solve

Posted: Mon May 05, 2025 6:27 am
by teknixelevators

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Teigha.Runtime;
using IntelliCAD.ApplicationServices;
using IntelliCAD.EditorInput;
using Teigha.DatabaseServices;
using Teigha.Geometry;
namespace ActCADPlugin
{
    namespace ActCADPlugin
    {
        public class Class1
        {
            [CommandMethod("InsertShaftBlock")]
            public void InsertShaftBlockCommand()
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Editor ed = doc.Editor;
                Database db = doc.Database;

                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                    BlockTableRecord ms = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

                    // Check if block definition exists
                    if (!bt.Has("shafttest"))
                    {
                        ed.WriteMessage("\nError: Block 'shafttest' not found in the drawing.");
                        tr.Abort();
                        return;
                    }

                    // Get the Block Table Record
                    ObjectId btrId = bt["shafttest"];

                    // Define the insertion point
                    Point3d insertionPoint = new Point3d(0, 0, 0);

                    // Create a block reference at the specified insertion point
                    BlockReference br = new BlockReference(insertionPoint, btrId);
                    ms.AppendEntity(br);
                    tr.AddNewlyCreatedDBObject(br, true);

                    // Prompt for property values and set them
                    SetDynamicBlockProperty(br, tr, "frontwalllhside");
                    SetDynamicBlockProperty(br, tr, "frontwallrhside");
                    SetDynamicBlockProperty(br, tr, "shaftdepth");
                    SetDynamicBlockProperty(br, tr, "shaftwidth");

                    tr.Commit();
                    ed.WriteMessage("\nBlock inserted and dynamic properties set.");
                }
            }

            private void SetDynamicBlockProperty(BlockReference br, Transaction tr, string propertyName)
            {
                Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
                string propertyValue = "";
                PromptResult pr;

                // Use a switch statement for property-specific prompting
                switch (propertyName.ToLower())
                {
                    case "frontwalllhside":
                        pr = ed.GetString(new PromptStringOptions($"\nEnter value for {propertyName}: "));
                        propertyValue = pr.StringResult;
                        break;
                    case "frontwallrhside":
                        pr = ed.GetString(new PromptStringOptions($"\nEnter value for {propertyName}: "));
                        propertyValue = pr.StringResult;
                        break;
                    case "shaftdepth":
                        pr = ed.GetString(new PromptStringOptions($"\nEnter value for {propertyName}: "));
                        propertyValue = pr.StringResult;
                        break;
                    case "shaftwidth":
                        pr = ed.GetString(new PromptStringOptions($"\nEnter value for {propertyName}: "));
                        propertyValue = pr.StringResult;
                        break;
                    default:
                        ed.WriteMessage($"\nWarning: Property '{propertyName}' not found.");
                        return;
                }

                if (string.IsNullOrEmpty(propertyValue))
                {
                    ed.WriteMessage($"\nError: No value provided for {propertyName}.");
                    return;
                }

                bool propertyFound = false;
                foreach (DynamicBlockReferenceProperty prop in br.DynamicProperties)  // Corrected
                {
                    if (prop.PropertyName.ToLower() == propertyName.ToLower())
                    {
                        prop.Value = propertyValue;
                        propertyFound = true;
                        break;
                    }
                }
                if (!propertyFound)
                {
                    ed.WriteMessage($"\nError: Dynamic property '{propertyName}' not found in the block.");
                }
            }
        }

    }
}

Re: (DynamicBlockReferenceProperty prop in br.DynamicProperties. Error Showing. How can I solve

Posted: Tue May 06, 2025 6:05 pm
by QuanNguyen
Maybe it's from ChatGPT?!

Try to use br.DynamicBlockReferencePropertyCollection
instead of br.DynamicProperties

BTW, please attach the drawing with the dynamic block for reference.