Setting Custom Sheet Property Error: Value does not fall within expected range

Setting Custom Sheet Property Error: Value does not fall within expected range

robert.chan453GK
Explorer Explorer
764 Views
1 Reply
Message 1 of 2

Setting Custom Sheet Property Error: Value does not fall within expected range

robert.chan453GK
Explorer
Explorer

Hello, I'm a beginner to customizing AutoCAD with .NET, and I'm currently using Visual Studio to create Class Libraries. It's also my first time posting to the forum so please let me know if I made any mistakes in posting. The code is a bit messy right now with some useless variables I made while learning as it's mostly an experiment, and I'll clean it up after I get a working prototype going.

 

Background: I'm trying to create a custom command in AutoCAD that copies the Sheet Set Custom Properties of one sheet to another. The user specifies a Source/Reference Sheet to take the data from, then the code looks through the Sheets in the Sheet Set for other Sheets that share the same file location as the Source Sheet (these are the sheets that are in the same file). It should then iterate through the Custom Properties of these sheets and set their values to match that of the Source Sheet's.

 

Issue: Everything works as intended until the actual property setting. The debugger tells me that the value that I input (which is the variable containing the source sheet's corresponding property value) "does not fall within the expected range". I can't understand why this is the case as when I checked the data types of both the source sheet's value and the destination sheet's value, they are both indeed AcSmCustomPropertyValue objects, and when I use the GetValue() method, they both return System.String.

 

Thank you in advance for any advice. Please do let me know if I should provide some additional information to help solve this issue.

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using ACSMCOMPONENTS24Lib;

namespace ClassLibrary1
{
    public class Class1
    {
        [CommandMethod("Test")]
        public void Test()
        {
            Document activeDoc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = activeDoc.Editor;
            string activeFilePath = activeDoc.Name;
            
            PromptStringOptions promptSourceSheet = new PromptStringOptions("Please enter the source sheet number: ");
            PromptResult resultSourceSheet = ed.GetString(promptSourceSheet);
            string valueSourceSheet = resultSourceSheet.StringResult;
            
            var sheetSetMgr = new AcSmSheetSetMgr();
            var databaseEnum = sheetSetMgr.GetDatabaseEnumerator();
            AcSmDatabase database = databaseEnum.Next();
            AcSmSheetSet sheetSet = database.GetSheetSet();
            var sheetEnum = sheetSet.GetSheetEnumerator();
            AcSmSheet sheet = new AcSmSheet();

            var component = sheetEnum.Next();
            while (!component.GetName().Contains("ELEMENTS") && component != null) component = sheetEnum.Next();

            AcSmSubset subSet = component as AcSmSubset;
            var subComponentEnum = subSet.GetSheetEnumerator();
            var subComponent = subComponentEnum.Next();
            int revNum = 0;
            AcSmSheet sourceSheet = null;
            string refPath = "";

            database.LockDb(database);
            while (subComponent != null)
            {
                if (subComponent.GetTypeName() == "AcSmSheet")
                {
                    sheet = subComponent as AcSmSheet;
                    string sheetNumber = sheet.GetNumber();
                    if (sheetNumber == valueSourceSheet)
                    {
                        IAcSmAcDbObjectReference refLayout = sheet.GetLayout();
                        refPath = refLayout.GetFileName();
                        sourceSheet = sheet;
                    }
                    else
                    {
                        IAcSmAcDbObjectReference iSheet = sheet.GetLayout();
                        string iSheetPath = iSheet.GetFileName();
                        if (refPath == iSheetPath)
                        {
                            IAcSmCustomPropertyBag sourcePropertyBag = sourceSheet.GetCustomPropertyBag();
                            IAcSmEnumProperty sourcePropertyEnum = sourcePropertyBag.GetPropertyEnumerator();
                            string propName = "";
                            AcSmCustomPropertyValue propValue = new AcSmCustomPropertyValue();
                            sourcePropertyEnum.Next(out propName, out propValue);

                            IAcSmCustomPropertyBag iPropertyBag = sheet.GetCustomPropertyBag();
                            IAcSmCustomPropertyValue iValue = iPropertyBag.GetProperty(propName);
                            iPropertyBag.SetProperty(propName, propValue); //This is where the debugger tells me the "Value does not fall within expected range" error
                        }
                    }
                }
                subComponent = subComponentEnum.Next();
            }
            database.UnlockDb(database, true);
        }
    }
}

 

 

0 Likes
Accepted solutions (1)
765 Views
1 Reply
Reply (1)
Message 2 of 2

robert.chan453GK
Explorer
Explorer
Accepted solution

Never mind, I got it to work. I think I needed to include the .SetFlags() method before setting the property.

0 Likes